ExFAT

From OSDev.wiki
Revision as of 23:53, 12 September 2019 by osdev>Mercury0x000d (→‎Structures: Added Main Boot Sector Structure (Start of information additions from Microsoft's newly publicly-released exFAT spec))
Jump to navigation Jump to search
Filesystems
Virtual Filesystems

VFS

Disk Filesystems
CD/DVD Filesystems
Network Filesystems
Flash Filesystems
This page is a stub.
You can help the wiki by accurately adding more contents to it.
This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.

ExFAT, contrary to its name, is not a version of FAT, but it's own filesystem.

After a long period of keeping the official exFAT spec under their collective hats, Microsoft has finally published the official specification at https://docs.microsoft.com/en-us/windows/win32/fileio/exfat-specification. The major structures of the file system are described below.

Structures

Main Boot Sector Structure

Offset Size Name Description
0x000 3 JumpBoot Jump instruction to start of boot code. For x86 CPUs, the byte sequence is usually EB 76 90.
0x003 8 FileSystemName The "EXFAT   " signature.
0x00B 53 MustBeZero This range of bytes is set null to help existing FAT drivers from mistakenly parsing an exFAT partition under the assumption that it contains valid FAT16 or FAT32 data.
0x040 8 PartitionOffset The sector offset of the start of this partition relative to the start of the volume. Can be null.
0x048 8 VolumeLength The total size of this exFAT partition in sectors. Should not be smaller than 1 MiB.
0x050 4 FatOffset Offset from the start of the partition to the first FAT
0x054 4 FatLength Size of each FAT in sectors
0x058 4 ClusterHeapOffset Offset from the start of the partition to the start of the "Cluster Heap", better known as the "Data Area" on former FAT implementations.
0x05C 4 ClusterCount The total number of clusters in the Cluster Heap.
0x060 4 FirstClusterOfRootDirectory The starting cluster of the root directory's cluster chain.
0x064 4 VolumeSerialNumber A serial number for this partition, usually comprised by calculating the date and time of format into a single 64-bit value. There is no standard for this claculation; each FAT driver is free to use its own technique.
0x068 2 FileSystemRevision Specifies the version of exFAT specification to which the structures in this partition adhere. For now, there is only a single exFAT specification; version 1.00. However in the future there may be more than this, and a driver should not attempt to parse exFAT structures of an exFAT specification version with which it is not familiar.
0x06A 2 VolumeFlags
Bit Name Description
0 ActiveFAT Used to implement transaction-safe mechanisms

0 - First FAT and first allocation bitmap are valid
1 - Second FAT and second allocation bitmap are valid

1 VolumeDirty 0 - Partition is likely in a good state

1 - Partition is likely not in a good state

2 MediaFailure 0 - No bad clusters aside from those already marked "bad"

1 - An I/O operation failed at some point indicating that the entire partition should be scanned for bad clusters at next mount.

3 ClearToZero No real meaning... yet!
4 - 15 n/a Reserved
0x06C 1 BytesPerSectorShift The number of places one would need to shift to translate bytes to sectors or sectors to bytes. Serves as an assist to allow shifting instead of multiplication or division.
0x06D 1 SectorsPerClusterShift Similar to the above, except used to calculate sectors per cluster.
0x06E 1 NumberOfFats Useful when implementing transaction-safe mechanisms

1 - Partition uses only a single FAT and Allocation Bitmaps
2 - Partition uses two FATs and two Allocation Bitmaps

0x06F 1 DriveSelect Similar to the Drive Number field found at offset 0x24 in previous FAT versions. Usually 0x80 for hard disks.
0x070 1 PercentInUse Percent of clusters in the Cluster Heap which are allocated, rounded down to the nearest whole number
0x071 7 Reserved Do not use
0x078 390 BootCode Boot code pointed to by the initial jump instruction at the beginning of the structure
0x1FE 2 BootSignature A series of bytes (0x55, 0xAA) which indicates this is a proper boot sector, as in previous FAT versions.


C Code Examples

Filesystem Header

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];
};

Block bitmap is cluster 2, upcase tbl is cluster 3.

Directory entries

32-byte entries. Entries contain one of a few possible entry types, first byte identifies which type it is. The second byte identifies how many subsequent entries are part of this entry. All bytes after are dependent on the type of entry. Any entry with the highest bit of entrytype set is not used.

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.
        uint8_t pad[2];
        uint32_t flags; // 0x10 == directory, probably identical to fat32
        uint32_t creation, modification, access;
        char pad[12];
} fileEntry;
struct {
        uint8_t entryType = 0xC0;
        uint8_t flags;
        uint8_t unk; // = 0. May be part of next field, but then it'd be in big-endian order which is unlikely.
        uint8_t filenameLengthInBytes;
        uint64_t filesize;
        uint32_t unk;
        uint32_t startCluster; // minus 2! Same as fat12/16/32.
        uint64_t filesize2;
} fileInfoEntry;
struct {
        uint8_t entrytype = 0xC1, entrycount = 0x00;
        uint16_t name[15];
} filenameEntry;


Disk Hexdump Examples

Short filenames

   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  |................|

Longer filenames

   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.............|

Really Long filenames

   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  |................|