FAT File Systems
This chapter describes the FAT16 and FAT32 file systems and shows how to obtain information about a file from the file allocation tables.
Inside a FAT16 Volume
Media with capacities from 16 MB to 2 GB can use the FAT16 file system. Every FAT16 volume has these components:
• Reserved region, which contains the boot sector.
• FAT region, which contains two copies of the file allocation tables (FATs).
• Root-directory region.
• File and directory data region, which can hold files and subdirectories.
These components are stored in sequence in the volume. To format a volume, or logical drive, under Windows XP, right-click the drive in Windows Explorer and click Format. The Window that appears
Figure 8-1: Windows Explorer enables formatting logical drives.
(Figure 8-1) shows the media’s capacity and a combo box that enables selecting FAT (for FAT16) or FAT32. File systems that aren’t suitable for the media don’t display in the combo box. The default allocation size (cluster size) is likely to be the only option presented. You can also enter a Volume label for the media. Click Start to format the volume. Another option is to use the format command at a command prompt. The /a parameter enables setting a cluster size. Type help format at the command prompt for a list of supported parameters. Remember that formatting destroys all data in the volume.
The first region in a FAT16 volume is the reserved region, which consists of a single sector called the boot sector. The boot sector begins at the LBA value stored in the MBR sector’s partition table, in byte 8 of the volume’s entry in the table. In media that doesn’t have a master boot record, the boot sector is the first sector. The sector contains a BIOS Parameter Block (BPB), an area reserved for boot code, and a boot signature.
BIOS Parameter Block
Table 8-1 shows the first 62 bytes in a FAT16 boot sector. Technically, the BIOS parameter block (BPB) consists of the data in bytes 11–35, while the remaining bytes are included in every boot sector but are not part of the BPB. A computer that is formatting a volume for FAT16 fills these locations with appropriate values. The information in the BPB enables a computer to locate the volume’s FATs, the root directory, and the file-and-directory data region, also called the data area.
Byte 13 in the BPB specifies how many sectors are in a data cluster. Each cluster holds data that belongs to a single file. Large files can use multiple clusters. Any extra space at the end of a file’s final cluster is unavailable for other uses. The choice of cluster size is a compromise between efficient access and efficient use of the storage media. A large cluster size is more efficient when accessing large files because the file-system software needs to locate and access fewer clusters. A small cluster size wastes less space, especially if there are many very small files.
The number of data clusters determines what file system to use, as described later in this chapter.
For maximum compatibility with mass-storage host software, several items in a FAT16 boot sector have required or recommended values, shown in Table 8-2. In theory all of the values except the number of reserved sectors can vary, but straying from the recommended values may cause problems with some hosts.
The location of the value that specifies the total number of sectors in the volume varies depending on the volume’s capacity. If less than 32 KB, the value is in the two bytes beginning at byte 19 in the BPB. If equal to or greater than 32 KB, the value is in the four bytes beginning at byte 32. Program code generally uses these values rather than the equivalent value stored in the partition-table entry.
Offset zero can contain a jump instruction to boot code. For PCs, the instruction is 80×86 machine code. FAT16 volumes typically contain EBh 3Ch 90h, which means jump ahead 60 bytes to the boot code, which begins at byte 3Eh in the BPB. EBh is the jump instruction, 3Ch (60) is the number of bytes to jump, and 90h is a NOP (no operation).
Table 8-1: The boot sector contains low-level information about the media’s formatting. These are the fields for a FAT16 volume (Sheet 1 of 2).
Table 8-1: The boot sector contains low-level information about the media’s formatting. These are the fields for a FAT16 volume (Sheet 2 of 2).
The bytes beginning at offset 54 can store text that identifies the file system, but program code shouldn’t rely on this text to identify the file system. As I’ll explain later in this chapter, the choice of file system depends entirely on the number of data clusters in the volume.
Locations 62 through 509 hold the boot code (448 bytes). As Chapter 7 explained, if the partition table indicates that a volume is bootable, on boot up, the executable code in the MBR jumps to the boot code in the volume’s boot sector. The boot code loads the operating system. Embedded systems typically don’t boot from storage devices and can ignore this code.
In a valid boot sector, byte 510 contains 55h and byte 511 contains AAh. For media with sectors larger than 512 bytes, these locations remain the same even though they aren’t the last bytes in the sector.
File Allocation Table Region
Following the reserved sector are two identical copies of the file allocation table (FAT). A FAT16 table has a 16-bit entry for each data cluster in the volume. Files that require multiple clusters use the FAT to maintain a record of the clusters used by each file. Later in this chapter, I explain how program code can use the FAT to locate the clusters used by a file. The second FAT is a backup for use in repairing a damaged first copy.
Table 8-2: Recommended and required values for fields in a FAT16 boot sector.