VFAT Long File Names
Usage: Stores long filenames (LFN) for Windows
This isn't a file system in itself, but a kind of sub file system, which can be placed over a FAT12, FAT16 or FAT32 file system. The VFAT system is a way of hiding long file names in the directory structure of the FAT file systems.
The filenames are stored using unicode characters which are 16 bit long.
Coexistence with FAT12, FAT16 & FAT32
Depending on the length of the long filename, the system will create a number of invalid 8.3 entries in the Directory Table, these are the LFN (Long Filename) entries. These LFN
entries are stored with the with the last LFN entry topmost, and the first LFN entry just above a valid Directory Entry. So when looked upon from the top and down, the Directory Table looks something like this:
||Without LFN Entries
||With LFN Entries
|n||Normal 1||Normal 1|
|n+1||Normal 2||LFN for Normal 2 - Part 3|
|n+2||Normal 3||LFN for Normal 2 - Part 2|
|n+3||Normal 4||LFN for Normal 2 - Part 1|
|n+4||Normal 5||Normal 2|
|n+5||Normal 6||Normal 3|
The LFN entries have the Volume Name, Hidden, System and the Read-Only flags set. So most programs won't display them because of the Volume Name flag (Volume entries are rarely displayed), and they won't be overwridden because of the Read-Only flag.
The VFAT Directory Format, can coexist on a normal FATxx system. The LFN entries are hidden from normal programs, and available to those who know how to read them. But in addition to the LFN entries the VFAT Directory Format enhances the original structure of the Directory Table as found in FAT16. The Directory Table can still be read by older applications, but all the unused space from the FAT16 format, is now used to store some additional information. This is the format of the VFAT Directory entries:
Structure of the VFAT Directory Entries
|08h||3 bytes||Filename extension|
|0Bh||1 bytes||Flag byte|
|0Ch||1 bytes||NT - Reserved for Windows NT - Should always be 0000h|
|0Dh||1 bytes||Creation Time - Millisecond|
|0Eh||2 bytes||Creation Time - Hour & Minute|
|10h||2 bytes||Created Date|
|12h||2 bytes||Last Accessed Data|
|14h||2 bytes||Starting cluster (High word) on FAT32 file systems, else 0000h|
|1Ah||2 bytes||Starting cluster (Low word)|
|1Ch||4 bytes||File size in bytes|
The flag byte defines a set of flags which is set for directories, volume name, hidden files, system files, etc. These are the flags:
Flags in the flag byte
The A flag is set by a backup program, so that the user/program knows which files that has been backed up. This flag is not used correctly by many user and perhaps also by many operating systems.
This flag shows that the file/directory is important for the system, and shouldn't be manipulated.
This flag tell the system and programs that the file should be hidden for the user. But in a lot of programs this can be overwritten by the user.
The flag is used to prevent programs from not automatically overwriting or deleting this file/directory.
This flag is set, when an entry in the directory table is not pointing to the beginning of a file, but to another directory table. A sub-directory. The sub-directory is placed in the cluster, where the Starting Cluster field points to. The format of this sub-directory table is identical to the root directory table.
When this flag is set, the directory entry is not pointing to a file, but to nothing. The only information used from this entry is the filename (8 bytes) plus the filename extension (3 bytes). These bytes form an 11 bytes long volume label (without any .) There may be only one valid entry on the entire disk with this flag set. And preferably this entry should be among the first 3 entries in the root directory table, if not, then MS-DOS can have trouble displaying the right volume label. This volume name should be the same as the one in the boot sector. The latter one is infact rarely used.
The LFN entries are, as described, placed above the real directory entry, for which they contain the long filename. The first 13 letters are in the first entry, letters 14-26 are in the second LFN entry, letters 27-39 are in the third LFN entry, and so on until the filename has ended. The maximum length of a filename has been limited to 255, even though this scheme has the potential to make them 1664 bytes long, Microsoft - who designed FAT32 - decided not to. The structure of each LFN entry is as follows:
Structure of a LFN Entry
|00h||1 bytes||Ordinal field|
|01h||2 bytes||Unicode Character 1|
|03h||2 bytes||Unicode Character 2|
|05h||2 bytes||Unicode Character 3|
|07h||2 bytes||Unicode Character 4|
|09h||2 bytes||Unicode Character 5|
|0Bh||1 bytes||Flag byte|
|0Ch||1 bytes||Reserved - Always 00h|
|0Eh||2 bytes||Unicode Character 6|
|10h||2 bytes||Unicode Character 7|
|12h||2 bytes||Unicode Character 8|
|14h||2 bytes||Unicode Character 9|
|16h||2 bytes||Unicode Character 10|
|18h||2 bytes||Unicode Character 11|
|1Ah||2 bytes||Always 0000h|
|1Ch||2 bytes||Unicode Character 12|
|1Eh||2 bytes||Unicode Character 13|
Only the cluster word (offset 1Ah) and the flag byte (offset 0Bh), are required for compatibility reasons.
The ordinal field is used to tell the system which number the LFN entry has, in the LFN string. The first LFN entry will have a value of 01h. The second LFN entry, if there is one, will have a value of 02h, and so on it continues. When a LFN entry contains the last character in the name the Last LFN bit is set (bit 6 in the Ordinal Field).
The Ordinal Field
Calculating the Checksum
The Checksum byte in each LFN entry is there to make sure that the LFN entry points to the right file. The checksum value is calculated on the basis of the 8.3 filename, which is present in the valid Directory Table entry. Spaces (ASCII: 20h) are also calculated. The algorithm for calculating the checksum is:
- The ASCII value of the first character is the base sum.
- Rotate the sum bitwise one bit to the right.
- Add the ASCII value of the next character to the sum.
- Repeat step 2 and 3 until all 11 characters has been added.
The Unicode characters in the specification is a 16 bit value, where the lower 8 bits represent the respective ASCII value and the upper 8 bits are clear. The means that the LFN names are just ASCII style names, with extra holes.
The VFAT Directory Format is a very clever way to add long filename support to the older FAT file systems, while still maintaining compatibility. It is however strange why Microsoft (who designed FAT32) didn't implemented native support for long filenames in FAT32 when they created it a few years back.