ExFAT 文件系统结构详解:分区布局、引导扇区与 FAT 区域
文章目录
| 文件系统 |
|---|
| 虚拟文件系统 |
| 磁盘文件系统 |
| CD/DVD 文件系统 |
| 网络文件系统 |
| 闪存文件系统 |
ExFAT,顾名思义,是传统 FAT 文件系统的扩展。与 FAT16 等旧版本相比,exFAT 已大幅现代化,从编程角度来看具有更多便利性。
在长期将官方 exFAT 规范秘而不宣之后,微软终于在 https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/FileIO/exfat-specification.md 发布了它,日期为 2019 年 8 月 27 日。该文件系统的主要结构描述如下。
分区布局
| 区域 | 内容 | 偏移(扇区号) | 大小(以扇区计) |
|---|---|---|---|
| 主引导区域 | 引导扇区 | 0 | 1 |
| 扩展引导扇区 | 1 | 8 | |
| OEM 参数 | 9 | 1 | |
| 保留扇区 | 10 | 1 | |
| 引导校验和 | 11 | 1 | |
| 备份引导区域 | 引导扇区 | 12 | 1 |
| 扩展引导扇区 | 13 | 8 | |
| OEM 参数 | 21 | 1 | |
| 保留扇区 | 22 | 1 | |
| 引导校验和 | 23 | 1 | |
| FAT 区域 | FAT 对齐 | 24 | FATOffset - 24 |
| 第一个 FAT | FATOffset | FATLength | |
| 第二个 FAT | FATOffset + FATLength | FATLength * (NumberOfFATs - 1) | |
| 数据区域 | 簇堆对齐 | FATOffset + FATLength * NumberOfFATs | ClusterHeapOffset - (FATOffset + FATLength * NumberOfFATs) |
| 簇堆 | ClusterheapOffset | ClusterCount * 2SectorsPerClusterShift | |
| 剩余空间(至分区或磁盘末尾) | ClusterheapOffset + ClusterCount * 2SectorsPerClusterShift | VolumeLength - (ClusterHeapOffset + ClusterCount * 2SectorsPerClusterShift) |
备份引导区域是主引导区域的副本。
FATAlignment 和 Cluster Heap Alignment 区域似乎是填充物,用于确保 FAT 和簇堆都从良好的边界开始。对于闪存驱动器等可移动介质,这比硬盘驱动器更有意义。
结构
引导扇区结构
| 偏移 | 大小 | 名称 | 描述 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x000 | 3 | JumpBoot | 跳转到引导代码起始位置的跳转指令。对于 x86 CPU,字节序列通常为 EB 76 90。 | ||||||||||||||||||
| 0x003 | 8 | FileSystemName | "EXFAT " 签名。 | ||||||||||||||||||
| 0x00B | 53 | MustBeZero | 此范围的字节被置为 null,以帮助现有 FAT 驱动程序避免在假定其中包含有效 FAT16 或 FAT32 数据的情况下错误解析 exFAT 分区。 | ||||||||||||||||||
| 0x040 | 8 | PartitionOffset | 此分区起始位置相对于卷起始位置的扇区偏移。可以为 null。 | ||||||||||||||||||
| 0x048 | 8 | VolumeLength | 此 exFAT 分区的总大小(以扇区计)。不应小于 1 MiB。 | ||||||||||||||||||
| 0x050 | 4 | FATOffset | 从分区起始处到第一个 FAT 的扇区偏移量 | ||||||||||||||||||
| 0x054 | 4 | FATLength | 每个 FAT 的大小(以扇区为单位) | ||||||||||||||||||
| 0x058 | 4 | ClusterHeapOffset | 从分区起始处到"簇堆"(Cluster Heap)起始处的扇区偏移量,在以前的 FAT 版本中称为"数据区"(Data Area)。 | ||||||||||||||||||
| 0x05C | 4 | ClusterCount | 簇堆中的簇总数。 | ||||||||||||||||||
| 0x060 | 4 | FirstClusterOfRootDirectory | 根目录簇链的起始簇。 | ||||||||||||||||||
| 0x064 | 4 | VolumeSerialNumber | 此分区的序列号,通常通过将格式化的日期和时间计算为单个 64 位值来构成。此计算没有标准;每个 FAT 驱动程序都可以自由使用自己的技术。 | ||||||||||||||||||
| 0x068 | 2 | FileSystemRevision | 指定此分区中的结构所遵循的 exFAT 规范版本。目前只有一个 exFAT 规范,即版本 1.00。但将来可能会有更多版本,驱动程序不应尝试解析其不熟悉的 exFAT 规范版本的 exFAT 结构。 | ||||||||||||||||||
| 0x06A | 2 | VolumeFlags |
此字不包含在校验和计算中。 | ||||||||||||||||||
| 0x06C | 1 | BytesPerSectorShift | 将字节转换为扇区或将扇区转换为字节所需的移位位数。作为辅助手段,允许使用移位代替乘法或除法。 | ||||||||||||||||||
| 0x06D | 1 | SectorsPerClusterShift | 与上述类似,但用于计算每簇的扇区数。 | ||||||||||||||||||
| 0x06E | 1 | NumberOfFATs | 在实现事务安全机制时有用 1 - 分区仅使用单个 FAT 和分配位图 | ||||||||||||||||||
| 0x06F | 1 | DriveSelect | 类似于以前 FAT 版本中位于偏移量 0x24 处的驱动器号字段。对于硬盘通常为 0x80。 | ||||||||||||||||||
| 0x070 | 1 | PercentInUse | 簇堆中已分配簇的百分比,向下取整到最接近的整数。此字节不包含在校验和计算中。 | ||||||||||||||||||
| 0x071 | 7 | 保留 | 请勿使用 | ||||||||||||||||||
| 0x078 | 390 | BootCode | 由结构开头的初始跳转指令指向的引导代码 | ||||||||||||||||||
| 0x1FE | 2 | BootSignature | 一系列字节(0x55、0xAA),表明这是一个正确的引导扇区,与以前的 FAT 版本类似。在依赖引导扇区中的任何其他字段之前应先验证此签名。 |
扩展引导扇区结构
| 偏移量 | 大小 | 名称 | 描述 |
|---|---|---|---|
| 0x00 | 2BytesPerSectorShift - 4 | ExtendedBootCode | 引导代码的额外空间。 |
| 2BytesPerSectorShift - 4 | 4 | ExtendedBootSignature | 一系列字节(0x00、0x00、0x55、0xAA),表明这是一个正确的引导扇区,与以前的 FAT 版本类似,但现在是 32 位的值而不是 16 位。在依赖扩展引导扇区中的任何其他字段之前应先验证此签名。注意此签名将出现在 每个 扩展引导扇区上。 |
OEM 参数结构
| 偏移量 | 大小 | 名称 | 描述 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x000 | 48 | Parameters[0] | 一组采用通用参数(Generic Parameters)格式的参数。
每个 48 字节的条目可以是以下之一:
或
在撰写本文时,exFAT 规范仅定义了上述两个 GUID;未来版本中可能会定义更多 GUID。 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x030 | 48 | Parameters[1] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x060 | 48 | Parameters[2] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x090 | 48 | Parameters[3] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x0C0 | 48 | Parameters[4] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x0F0 | 48 | Parameters[5] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x120 | 48 | Parameters[6] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x150 | 48 | Parameters[7] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x180 | 48 | Parameters[8] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x1B0 | 48 | Parameters[9] | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 0x1E0 | 2BytesPerSectorShift - 480 | Reserved | 请勿使用 |
FAT 区域结构
| 偏移量 | 大小 | 名称 | 描述 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x00 | 4 | FatEntry[0] | 由一个媒体描述符字节(始终为 0xF8)后跟三个 0xFF 字节组成 | ||||||||||
| 0x04 | 4 | FatEntry[1] | 始终为 0xFFFFFFFF。请勿使用、解释或更改。 | ||||||||||
| 0x08 | 4 | FatEntry[2] | 以下之一:
这些值与以前的 FAT 版本中使用的值相同,区别在于 0x00000001、0xFFFFFFF0 - 0xFFFFFFF6 和 0xFFFFFFF8 - 0xFFFFFFFE 不再被赋予任何特殊含义。 | ||||||||||
| ... | ... | ... | ... |
C 代码示例
文件系统头
struct bootsector { char jump[3]; char fsid[8]; char pad[53]; uint64_t unk; // sectors before first cluster too? uint64_t fsSizeInSectors; uint32_t sectorsToStartOfFat; uint32_t sectorsUsedForFat; uint32_t sectorStartOfFirstCluster; uint32_t clusterCountInFs; uint32_t clusterForRootDirectory; uint32_t unk[2]; uint8_t logBytesPerSector, logSectorsPerCluster, fatCount, driveId; // 2-log for both. bytesPerCluster = 1 << (logBytesPerSector + logSectorsPerCluster). uint8_t percentInUse; // for display ala windows computer explorer uint8_t unk[7]; char bootcode[390]; char bootsign[2]; };
块位图位于簇 2,大写表位于簇 3。
引导校验和计算
UInt32 BootChecksum (UCHAR * Sectors, USHORT BytesPerSector) { /* UCHAR * Sectors, above, points to an in-memory copy of the 11 sectors That is, 0 - 10 for the Main Boot Region, or 12 - 22 for the Backup Boot Region */ UInt32 NumberOfBytes = (UInt32)BytesPerSector * 11; UInt32 Checksum = 0; UInt32 Index; for (Index = 0; Index < NumberOfBytes; Index++) { if ((Index == 106) || (Index == 107) || (Index == 112)) { continue; } Checksum = ((Checksum&1) ? 0x80000000 : 0) + (Checksum>>1) + (UInt32)Sectors[Index]; } return Checksum; }
目录项
32 字节的项。项包含若干可能的项类型之一,第一个字节标识其类型。第二个字节标识后续有多少个项属于该项的一部分。之后的所有字节取决于项的类型。entrytype 最高位为 1 的任何项均不使用。
struct { uint8_t entrytype = 0x85; uint8_t entrycount; // This is the number of subsequent entries that also belong to this "file" entry. Should be at least 2, one 0xC0 and one 0xC1 for info and filename. uint16_t checksum; uint16_t flags; // 0x10 == directory uint16_t rsvd; // Reserved uint32_t creation, modification, access; char pad[12]; } fileEntry;
struct { uint8_t entryType = 0xC0; uint8_t flags; uint8_t reserved; uint8_t filenameLengthInBytes; uint16_t filenameHash; uint16_t reserved_2; uint64_t valid_filesize; uint32_t reserved3; uint32_t startCluster; // minus 2! Same as fat12/16/32. uint64_t filesize; } fileInfoEntry;
struct { uint8_t entrytype = 0xC1, entrycount = 0x00; uint16_t name[15]; } filenameEntry;
磁盘十六进制转储示例
短文件名
01040300 85 02 c8 f9 10 00 00 00 5b a9 47 45 6f a9 47 45 |........[.GEo.GE| 01040310 6f a9 47 45 25 25 88 88 88 00 00 00 00 00 00 00 |o.GE%%..........| 01040320 c0 03 00 05 ae 26 00 00 00 00 02 00 00 00 00 00 |.....&..........| 01040330 00 00 00 00 17 00 00 00 00 00 02 00 00 00 00 00 |................| 01040340 c1 00 69 00 6d 00 61 00 67 00 65 00 00 00 00 00 |..i.m.a.g.e.....| 01040350 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
较长的文件名
01080060 85 03 34 aa 10 00 00 00 1c a5 47 45 92 0a 48 45 |..4.......GE..HE| 01080070 92 0a 48 45 94 94 88 88 88 00 00 00 00 00 00 00 |..HE............| 01080080 c0 03 00 18 23 20 00 00 00 00 02 00 00 00 00 00 |....# ..........| 01080090 00 00 00 00 07 00 00 00 00 00 02 00 00 00 00 00 |................| 010800a0 c1 00 63 00 6f 00 6d 00 2e 00 67 00 6f 00 6f 00 |..c.o.m...g.o.o.| 010800b0 67 00 6c 00 65 00 2e 00 61 00 6e 00 64 00 72 00 |g.l.e...a.n.d.r.| 010800c0 c1 00 6f 00 69 00 64 00 2e 00 6d 00 75 00 73 00 |..o.i.d...m.u.s.| 010800d0 69 00 63 00 00 00 00 00 00 00 00 00 00 00 00 00 |i.c.............|
非常长的文件名
01100640 85 05 a9 6f 20 00 00 00 25 38 48 45 26 38 48 45 |...o ...%8HE&8HE| 01100650 25 38 48 45 64 64 00 00 00 00 00 00 00 00 00 00 |%8HEdd..........| 01100660 c0 01 00 32 c6 a5 00 00 d8 52 76 00 00 00 00 00 |...2.....Rv.....| 01100670 00 00 00 00 14 46 00 00 d8 52 76 00 00 00 00 00 |.....F...Rv.....| 01100680 c1 00 30 00 30 00 33 00 20 00 2d 00 20 00 4c 00 |..0.0.3. .-. .L.| 01100690 65 00 64 00 20 00 5a 00 65 00 70 00 70 00 65 00 |e.d. .Z.e.p.p.e.| 011006a0 c1 00 6c 00 69 00 6e 00 20 00 2d 00 20 00 53 00 |..l.i.n. .-. .S.| 011006b0 74 00 61 00 69 00 72 00 77 00 61 00 79 00 20 00 |t.a.i.r.w.a.y. .| 011006c0 c1 00 74 00 6f 00 20 00 68 00 65 00 61 00 76 00 |..t.o. .h.e.a.v.| 011006d0 65 00 6e 00 20 00 2d 00 20 00 31 00 39 00 37 00 |e.n. .-. .1.9.7.| 011006e0 c1 00 32 00 2e 00 6d 00 70 00 33 00 00 00 00 00 |..2...m.p.3.....| 011006f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:ExFAT;离线修订号:30517。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论