lab9 Filesystem
  • Introduction
  • Important Commands
    • File Permissions
    • Disk Duplication
    • Create FAT Filesystems
    • Check and Repair FAT filesystem
    • Mount
    • Demo
    • Viewing with Hex Editor
      • Endian-ness in FAT32
  • Linux File System Calls
  • Directory Related Calls in C Language
  • Overview of FAT32
  • Accessing FAT using C
    • C Header of FAT
      • Header: Boot Sector
      • Header: Dir Entry
      • Read the header
    • 8+3 File Name
    • Traversing Cluster
    • Finding Next Cluster
    • Reference
Powered by GitBook
On this page

Was this helpful?

  1. Accessing FAT using C
  2. C Header of FAT

Header: Boot Sector

 struct fat_boot_sector {
         __u8    ignored[3];     /* Boot strap short or near jump */
         __u8    system_id[8];   /* Name - can be used to special case
                                    partition manager volumes */
         __u8    sector_size[2]; /* bytes per logical sector */
         __u8    sec_per_clus;   /* sectors/cluster */
         __le16  reserved;       /* reserved sectors */
         __u8    fats;           /* number of FATs */
         __u8    dir_entries[2]; /* root directory entries */
         __u8    sectors[2];     /* number of sectors */
         __u8    media;          /* media code */
         __le16  fat_length;     /* sectors/FAT */
         __le16  secs_track;     /* sectors per track */
         __le16  heads;          /* number of heads */
         __le32  hidden;         /* hidden sectors (unused) */
         __le32  total_sect;     /* number of sectors (if sectors == 0) */

         /* The following fields are only used by FAT32 */
         __le32  fat32_length;   /* sectors/FAT */
         __le16  flags;          /* bit 8: fat mirroring, low 4: active fat */
         __u8    version[2];     /* major, minor filesystem version */
         __le32  root_cluster;   /* first cluster in root directory */
         __le16  info_sector;    /* filesystem info sector */
         __le16  backup_boot;    /* backup boot sector */
         __le16  reserved2[6];   /* Unused */
 };

To use it, first declare afat_boot_sectorstructure, then usefread()to read the first n bytes of data to that structure, wherenis the size offat_boot_sectorstructure. After reading all the parameters are read into the variables respectively.

// Declaration
struct fat_boot_sector boot_entry;

Getting the information

The following can be retrieved directly from the structure:

  1. Number of FATs (fats)

  2. Number of sectors per cluster (sec_per_clus)

  3. Number of Reserved Sector(reserved)

And the following information requires some manipulations/calculations

  1. Bytes Per Sector (sector_size[0] + sector_size[1] << 8)

  2. First FAT Starting Position (Bytes Per Sector * Number of Reserved Sector)

  3. Data Area Starting Position (Bytes Per Sector * (Number of Reserved Sector + Number of FATs * fat_length))

Here, fat_length is the number of sectors per FAT.

PreviousC Header of FATNextHeader: Dir Entry

Last updated 5 years ago

Was this helpful?