Fix incorrect path traversal
[mfat.git] / FAT.md
blobed998ee598cd286d318021570b793ea908236d15
1 # FAT documentation
3 This document summarizes some aspects of the FAT file system, and acts as a reference for the implementation.
5 The following sources of information were used during the development of MFAT:
7 * [FAT32 File System Specification, FAT: General Overview of On-Disk Format](https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc)
8 * [Wikipedia: Master boot record](https://en.wikipedia.org/wiki/Master_boot_record)
9 * [Wikipedia: GUID Partition Table](https://en.wikipedia.org/wiki/GUID_Partition_Table)
10 * [Application Note for Fat16 Interface for MSP430](https://teslabs.com/openplayer/docs/docs/prognotes/appnote_fat16.pdf)
11 * [FAT32 Structure Information - MBR, FAT32 Boot Sector Introduction](https://www.easeus.com/resource/fat32-disk-structure.htm)
13 ## Terminology
15 * Block - One block of data. Usually 512 bytes. Possibly 1024, 2048 or 4096 bytes.
16 * Sector - Term used interchangeably with "block".
17 * Cluster - An integer multiple of blocks. Files are divided into clusters. One cluster belongs to one file.
19 ## Master Boot Record (MBR)
21 The Master Boot Record, if present, is the first 512-byte block of the storage medium.
23 **Note**: Some storage media may be formatted without an MBR. In this case, there is only a single FAT partition, and the first block is the BIOS Parameter Block.
25 The layout of the MBR is as follows:
27 | Offset | Size | Description |
28 | --- | --- | --- |
29 | 0 | 446 | Boot code (usually 8086 code, unused) |
30 | 446 | 16 | Partition entry 0 (see below) |
31 | 462 | 16 | Partition entry 1 (--"--) |
32 | 478 | 16 | Partition entry 2 (--"--) |
33 | 494 | 16 | Partition entry 3 (--"--) |
34 | 510 | 2 | MBR signature: `[0x55, 0xaa]` |
36 The layout of each Partition entry is as follows:
38 | Offset | Size | Description |
39 | --- | --- | --- |
40 | 0 | 1 | Status (bit 7 = 1 indicates "active"/bootable) |
41 | 1 | 3 | CHS addr. of first block in partition (unused) |
42 | 4 | 1 | Partition type |
43 | 5 | 3 | CHS addr. of last block in partition (unused) |
44 | 8 | 4 | LBA of first block in partition (location of the BPB) |
45 | 12 | 4 | Number of blocks in partition |
47 ## BIOS Parameter Block (BPB)
49 The BIOS Parameter Block, which is sometimes also refered to as the "Boot Record", is the first block of the partition.
51 There are essentially two different version of the BPB: One for FAT16 and one for FAT32. The first 36 bytes have the same layout for FAT16 and FAT32:
53 | Offset | Size | Description |
54 | --- | --- | --- |
55 | 0 | 3 | Jump code (`[0xEB,0x??,0x90]` or `[0xE9,0x??,0x??]`) |
56 | 3 | 8 | OEM name (unused) |
57 | 11 | 2 | Bytes per sector (512, 1024, 2048 or 4096) |
58 | 13 | 1 | Sectors per cluster (1, 2, 4, 8, 16, 32, 64 or 128) |
59 | 14 | 2 | Reserved sectors |
60 | 16 | 1 | Number of copies of FAT (set to 2) |
61 | 17 | 2 | Number of root directory entries (valid for FAT16, zero for FAT32) |
62 | 19 | 2 | Number of sectors in partition smaller than 32MB (valid for FAT16, zero for FAT32) |
63 | 21 | 1 | Media descriptor (0xf8 for hard disks) |
64 | 22 | 2 | Sectors per FAT (valid for FAT16, zero for FAT32) |
65 | 24 | 2 | Sectors per track |
66 | 26 | 2 | Number of heads |
67 | 28 | 4 | Number of hidden sectors in partition |
68 | 32 | 4 | Number of sectors in partition (valid for FAT32, may be zero for FAT16) |
70 ### FAT16 BPB (offset 36-511)
72 | Offset | Size | Description |
73 | --- | --- | --- |
74 | 36 | 1 | Logical drive number of partition |
75 | 37 | 1 | Reserved (set to zero)) |
76 | 38 | 1 | Extended boot signature: If 0x29 the following three fields are present |
77 | 39 | 4 | Volume ID (serial number) of partition |
78 | 43 | 11 | Volume label of partition |
79 | 54 | 8 | FAT name ("FAT16") |
80 | 62 | 448 | Executable code (unused) |
81 | 510 | 2 | Executable marker: `[0x55, 0xaa]` |
83 ### FAT32 BPB (offset 36-511)
85 | Offset | Size | Description |
86 | --- | --- | --- |
87 | 36 | 4 | Number of sectors per FAT |
88 | 40 | 2 | ExFlags (see below) |
89 | 42 | 2 | Version of FAT32 drive: `[major, minor]` |
90 | 44 | 4 | Cluster number of the start of the root directory |
91 | 48 | 2 | Sector number (relative to start of partition) of the FileSystem Information Sector (see below) |
92 | 50 | 2 | Sector number (relative to start of partition) of the Backup Boot Sector |
93 | 52 | 12 | Reserved (set to zero) |
94 | 64 | 1 | Logical drive number of partition |
95 | 65 | 1 | Reserved (set to zero) |
96 | 66 | 1 | Extended boot signature: If 0x29 the following three fields are present |
97 | 67 | 4 | Volume ID (serial number) of partition |
98 | 71 | 11 | Volume label of partition |
99 | 82 | 8 | FAT name ("FAT32") |
100 | 90 | 420 | Executable code (unused) |
101 | 510 | 2 | Executable marker: `[0x55, 0xaa]` |
103 ExFlags:
105 | Bits | Description |
106 | --- | --- |
107 | 0-3 | Zero-based number of active FAT (only valid if mirroring is disabled) |
108 | 4 | Reserved |
109 | 7 | Disable FAT mirroring if 1: The FAT information is only written to the copy indicated by bits 0-3 |
111 ## Directory Entry Structure
113 | Offset | Size | Description |
114 | --- | --- | --- |
115 | 0 | 11 | Short name |
116 | 11 | 1 | File attributes (see below) |
117 | 12 | 1 | Reserved (set to zero) |
118 | 13 | 1 | Creation time stamp, milliseconds (optional) |
119 | 14 | 6 | Reserved (?) |
120 | 20 | 2 | High word of cluster number (zero for FAT16) |
121 | 22 | 2 | Time of last write |
122 | 24 | 2 | Date of last write |
123 | 26 | 2 | Low word of cluster number |
124 | 28 | 4 | File size in bytes |
126 File attributes:
128 | Name | Value |
129 | --- | --- |
130 | ATTR_READ_ONLY | 0x01 |
131 | ATTR_HIDDEN | 0x02 |
132 | ATTR_SYSTEM | 0x04 |
133 | ATTR_VOLUME_ID | 0x08 |
134 | ATTR_DIRECTORY | 0x10 |
135 | ATTR_ARCHIVE | 0x20 |
136 | ATTR_LONG_NAME | 0x0f |