• AVR Freaks

Helpful ReplyHot!Reading the first sector of FAT32 formatted sd card.

Author
BHUSHAN
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
2019/07/02 21:44:06 (permalink)
0

Reading the first sector of FAT32 formatted sd card.

Namaste to all,
I am using PIC32MX795F512L for interfacing with 4GB SDHC card. Previously I was able to initialize and also read/write single block from/to sdhc. Now I want to implement FAT32 file system on my card. I have formatted my sdhc with FAT32 format on my PC. After reading some journals on FAT32 I found that sector0 of sdhc card is a boot sector if first byte among 512bytes of this sector is either 0xE9 or 0xEB. So I tried reading the sector0 of my sdhc card and I found 1st byte equal to 0xEB(confirming that this is a boot sector). I read the sector0 in both ASCII and hex format. As I read in journals that last 2 bytes among 512bytes of sector0 are "Boot End signature",which are equal to 0xaa and 0x55 but I am getting some message like "Disk errorÿ " and "Press any key to restart" (journals didn't mention this messages). Also the boot end signature 0xaa and 0x55 are found on byte number 119 and 120 respectively. Can anyone kindly tell me what I am missing.
I have attached my reading of sector0 both in ASCII and hex format with this post.
Thanks.       
 
#1
ric
Super Member
  • Total Posts : 23859
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/02 22:29:13 (permalink) ☄ Helpfulby BHUSHAN 2019/07/02 23:33:51
0
In the hex format, I only see 511 bytes, and #511 is 55, so the missing #512 could be AA.
I suspect your code to read in ASCII is falling off the end due to using a string function looking for a NULL.
(Can only guess as you did not show the code that produced this message.)

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
al_bin
Super Member
  • Total Posts : 169
  • Reward points : 0
  • Joined: 2011/02/11 06:28:47
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/02 22:31:56 (permalink) ☄ Helpfulby BHUSHAN 2019/07/02 23:47:39
4 (1)
Everything is OK.
These messages are part of the  boot code.
 
Albert
#3
BHUSHAN
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/02 23:47:25 (permalink)
0
ric
In the hex format, I only see 511 bytes, and #511 is 55, so the missing #512 could be AA.
I suspect your code to read in ASCII is falling off the end due to using a string function looking for a NULL.
(Can only guess as you did not show the code that produced this message.)


Thanks Ric. You are right. I was reading only 511 bytes so I got 0xaa on 512th byte.
Thanks
#4
BHUSHAN
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/02 23:49:00 (permalink)
0
al_bin
Everything is OK.
These messages are part of the  boot code.
 
Albert


Thanks Albert. Do I have to take care of these error messages?
#5
ric
Super Member
  • Total Posts : 23859
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/02 23:55:46 (permalink) ☄ Helpfulby BHUSHAN 2019/07/03 01:50:26
0
BHUSHAN
Thanks Albert. Do I have to take care of these error messages?

What do you mean "take care of"?
They are embedded in the boot code that is contained in that boot sector. Naturally you are not going to try to execute 8086 assembly code on your PIC32, so you may as well just leave it all as-is.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
BHUSHAN
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/03 01:56:10 (permalink)
0
ric
BHUSHAN
Thanks Albert. Do I have to take care of these error messages?

What do you mean "take care of"?
They are embedded in the boot code that is contained in that boot sector. Naturally you are not going to try to execute 8086 assembly code on your PIC32, so you may as well just leave it all as-is.
 




Hi Ric,
By saying,"do I have to take care of this error messages", I meant that are these error messages have any information (I am trying FAT32 for fist time and no journals mentioned these error messages). After your reply I got the answer so thanks a lot.
#7
LdB_ECM
Senior Member
  • Total Posts : 148
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/04 09:58:50 (permalink)
5 (1)
Its a BIOS parameter block
https://en.wikipedia.org/wiki/BIOS_parameter_block
here is the struct for it just
[/code]/*--------------------------------------------------------------------------}
{          FAT12/16/32 UNIONIZED BIOS PARAMETER BLOCK STRUCTURE                }
{--------------------------------------------------------------------------*/
#pragma pack(4)
// Full unionized BPB struct valid for 12/16/32 bit
struct FAT_BPB_struct {            // OFFSET LOCATIONS
    uint8_t            BS_JmpBoot[3];            // 0-2   (eb xx 90, or e9 xx xx)
    char            BS_OEMName[8];            // 3-10
    uint16_t        BytesPerSector;            // 11-12 (valid 512, 1024, 2048, 4096)
    uint8_t            SectorsPerCluster;        // 13    (valid 1, 2, 4, 8, 16, 32, 64, 128, 32768, 65536)
    uint16_t        ReservedSectorCount;    // 14-15
    uint8_t            NumFATs;                // 16    (2)
    uint16_t        RootEntryCount;            // 17-18 (224 = FAT12, 512 = FAT16, 0 = FAT 32**)
    uint16_t        TotalSectors16;            // 19-20
    uint8_t            Media;                    // 21    (Media descriptor f0: 1.4 MB floppy, f8 : hard disk; etc)
    uint16_t        FATSize16;                // 22-23 (Number of sectors per FAT .. 0 on FAT32**)
    uint16_t        SectorsPerTrack;        // 24-25
    uint16_t        NumberOfHeads;            // 26-27
    uint32_t        HiddenSectors;            // 28-31
    uint32_t        TotalSectors32;            // 32-35
    union {                                    // Unionize the two structures at offset 36
        struct BPBFAT1612_struct fat1612;    // Fat 16/12 structure
        struct BPBFAT32_struct     fat32;        // Fat 32 structure
    } FSTypeData;
};
#pragma pack()[/code]
#8
LdB_ECM
Senior Member
  • Total Posts : 148
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/04 10:03:10 (permalink)
5 (1)
Sorry should give you the little structs at bottom as well .... that completes it in it' full detail

/*--------------------------------------------------------------------------}
{                        FAT12/16 SPECIFIC STRUCTURE                         }
{--------------------------------------------------------------------------*/
#pragma pack(1)
// Offset 36 into the BPB, structure for a FAT16/12 table entry varies this defines it
struct BPBFAT1612_struct {
    uint8_t            BS_DriveNumber;            // 36       ( As used in INT 13)
    uint8_t            BS_Reserved1;           // 37
    uint8_t            BS_BootSig;             // 38        (0x29)
    uint32_t        BS_VolumeID;            // 42
    uint8_t            BS_VolumeLabel[11];     // 43-53    (Label or  "NO NAME    ")
    char            BS_FileSystemType[8];   // 54-61    ("FAT16   ", "FAT     ", or all zero.)
};
#pragma pack()

/*--------------------------------------------------------------------------}
{                         FAT32 SPECIFIC STRUCTURE                            }
{--------------------------------------------------------------------------*/
#pragma pack(1)
// Offset 36 into the BPB, structure for a FAT32 table entry varies this defines it
struct BPBFAT32_struct {
    uint32_t        FATSize32;                // 36-39   (Number of sectors per FAT on FAT32)
    uint16_t        ExtFlags;                // 40-41   (Mirror flags Bits 0-3: number of active FAT (if bit 7 is 1) Bit 7: 1 = single active FAT; zero: all FATs are updated at runtime; Bits 4-6 & 8-15 : reserved)
    uint16_t        FSVersion;                // 42-43
    uint32_t        RootCluster;            // 44-47   (usually 2)
    uint16_t        FSInfo;                    // 48-49   (usually 1)
    uint16_t        BkBootSec;                // 50-51   (usually 6)
    uint8_t            Reserved[12];            // 52-63
    uint8_t            BS_DriveNumber;            // 64
    uint8_t            BS_Reserved1;           // 65
    uint8_t            BS_BootSig;                // 66      (0x29: extend signature .. Early FAT32 stop here )
    uint32_t        BS_VolumeID;            // 67-70
    char            BS_VolumeLabel[11];     // 71-81   (Label or  "NO NAME    ")
    char            BS_FileSystemType[8];   // 82-89   ("FAT32   ", "FAT     ", or all zero.)
};
#pragma pack()

#9
LdB_ECM
Senior Member
  • Total Posts : 148
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/04 10:23:39 (permalink) ☄ Helpfulby BHUSHAN 2019/08/17 02:44:30
0
Sector 0 is always a BPB or MBR they are the only two choices.
Just yell if you need any of the struct's I have them all including the long file name stuff.
#10
BHUSHAN
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: Reading the first sector of FAT32 formatted sd card. 2019/07/17 03:25:41 (permalink)
0
LdB_ECM
Sector 0 is always a BPB or MBR they are the only two choices.
Just yell if you need any of the struct's I have them all including the long file name stuff.




Sorry for late reply. As I am working on FAT32 format so I created FAT32 related data structures.
Thanks again.
#11
Jump to:
© 2019 APG vNext Commercial Version 4.5