Search code examples
filetype-conversionbytefat16

Reading a FAT16 file system


I am trying to read a FAT16 file system to gain information about it like number of sectors, clusters, bytespersector etc...

I am trying to read it like this:

FILE *floppy;
unsigned char bootDisk[512];
floppy = fopen(name, "r");
fread(bootDisk, 1, 512, floppy);

int i;
for (i = 0; i < 80; i++){
    printf("%u,",bootDisk[i]);  
}

and it outputs this:

235,60,144,109,107,100,111,115,102,115,0,0,2,1,1,0,2,224,0,64,11,240,9,0,18,0,2,0,0,0,0,0,0,0,0,0,0,0,41,140,41,7,68,32,32,32,32,32,32,32,32,32,32,32,70,65,84,49,50,32,32,32,14,31,190,91,124,172,34,192,116,11,86,180,14,187,7,0,205,16,

What do these numbers represent and what type are they? Bytes?


Solution

  • You are not reading the values properly. Most of them are longer than 1 byte.

    From the spec you can obtain the length and meaning of every attributes in the boot sector:

    Offset  Size (bytes)    Description
    0000h   3 Code to jump to the bootstrap code.
    0003h   8 Oem ID - Name of the formatting OS
    000Bh   2 Bytes per Sector
    000Dh   1 Sectors per Cluster - Usual there is 512 bytes per sector.
    000Eh   2 Reserved sectors from the start of the volume.
    0010h   1 Number of FAT copies - Usual 2 copies are used to prevent data loss.
    0011h   2 Number of possible root entries - 512 entries are recommended.
    0013h   2 Small number of sectors - Used when volume size is less than 32 Mb.
    0015h   1 Media Descriptor
    0016h   2 Sectors per FAT
    0018h   2 Sectors per Track
    001Ah   2 Number of Heads
    001Ch   4 Hidden Sectors
    0020h   4 Large number of sectors - Used when volume size is greater than 32 Mb.
    0024h   1 Drive Number - Used by some bootstrap code, fx. MS-DOS.
    0025h   1 Reserved - Is used by Windows NT to decide if it shall check disk integrity.
    0026h   1 Extended Boot Signature - Indicates that the next three fields are available.
    0027h   4 Volume Serial Number
    002Bh   11 Volume Label - Should be the same as in the root directory.
    0036h   8 File System Type - The string should be 'FAT16 '
    003Eh   448 Bootstrap code - May schrink in the future.
    01FEh   2   Boot sector signature - This is the AA55h signature
    

    You should probably use a custom struct to read the boot sector.

    Like:

    typedef struct {
    unsigned char jmp[3];
    char oem[8];
    unsigned short sector_size;
    unsigned char sectors_per_cluster;
    unsigned short reserved_sectors;
    unsigned char number_of_fats;
    unsigned short root_dir_entries;
    [...]
    } my_boot_sector;
    

    Keep in mind your endianness and padding rules in your implementation. This struct is an example only.

    If you need more details this is a thorough example.