Overview and Purpose

This document describes the on-media FAT file system format.
This document is written to help guide development of FAT implementations that are compatible with those provided by Microsoft.

这个文档描述了FAT文件系统的格式。
这个文档是微软为了引导FAT实现兼容性开发而编写的。

This document does not describe all algorithms contained in the Microsoft FAT file system driver implementation neither does it describe all algorithms contained in associated utilities (Microsoft format and chkdsk utilities).
However, it is expected that the reader will refer to the Microsoft Corporation FAT file system reference source code for additional clarification as/when needed.

该文档不会描述包含在Microsoft FAT 文件系统驱动中的所有算法。也没有描述包含在相关工具中的所有算法(Microsoft format(格式化工具) and chkdsk(磁盘检查工具))。
不过若读者需要可以参考微软公司的FAT文件系统的源代码reference以更加获得更清晰地理解。

There are three variants of the FAT on-disk format, namely:

FAT 在磁盘上的格式有以下三种变体,分别叫做:

  • FAT12
  • FAT16
  • FAT32

Data structures comprising all three variants are described here.
由三种变体组成的数据结构都会在本文进行描述。

Also provided are descriptions of specific algorithms that will be useful to the engineer implementing a FAT driver for reading and/or writing to media.
It is expected that the FAT Test Specification document will be used by the reader to validate interoperability and correctness of the resultant FAT implementation.

本文还将描述一些特定的算法,这将对工程师实现FAT驱动在存储媒介上的读写操作有用。

Section 1: Definitions and Notations

1.1 Definitions

  • byte

    A string of binary digits operated upon as a unit.

    是一串二进制数,这作为一个操作单元。

  • bad (defective) sector

    A sector whose contents cannot be read or one that cannot be written.

    一个扇区的内容不能读或写,那么该扇区称为坏扇区或无效扇区。

  • file

    A named stream of bytes representing a collection of information.

    被称作比特流,它表达了一个信息的集合。

  • sector

    A unit of data that can be accessed independently of other units on the media

    是一个数据单元能够独立于媒介上其他数据单元而访问。

  • cluster

    A unit of allocation comprising a set of logically contiguous sectors. Each cluster within the volume is referred to by a cluster number “N”. All allocation for a file must be an integral multiple of a cluster.

    逻辑上连续的扇区集合构成了一个分配单元叫做一个簇。卷上的每个簇都会被一个簇号N所引用到。一个文件的所有分配情况必须是簇的整数倍。

  • partition

    An extent of sectors within a volume.

    volume内的扇区范围

  • volume

    A logically contiguous sector address space as specified in the relevant standard for recording.

    通过某个有关标准的记录信息,可确定一个逻辑上连续的扇区的地址空间,叫做volume

1.2 Notation

Numerical Notation

Numbers in decimal notation are represented by decimal digits, namely 0 to 9.

数字在十进制下通过一个或多个十进制数字表示,记做0~9。

Numbers in hexadecimal notation are represented as a sequence of one or more hexadecimal digits namely 0 to 9 and A to F, prefixed by “0x”.

数字在十六进制下的符号被表达为一个或多个十六进制下数字,通常记做09和AF,并且通过前缀0x修饰。

Zero represents a single bit with the value 0.

零表达了一个简单的比特位0

Arithmetic Notation

The notation ip(x) shall mean the integer part of x.

符号ip(x)的意思是取x的整数部分。

The notation ceil(x) shall mean the minimum integer that is greater than x.

符号ceil(x)的意思是取比x大的最小整数值。

The notation rem(x,y) shall mean the remainder of the integer division of x by y.

符号rem(x,y)的意思是x被y整数除法的余数。

Section 2: Volume Structure

A FAT file system volume is composed of four basic regions, which are laid out in this order on the volume:

一个FAT文件系统卷是有四个基本区域所构成,这四个区域通过以下方式放置:

  • 0 – Reserved Region

    保留区域

  • 1 – FAT Region

    FAT区域

  • 2 – Root Directory Region (doesn’t exist on FAT32 volumes)

    根目录区域(FAT32卷中不存在)

  • 3 – File and Directory Data Region

    文件和目录数据区

The below figure illustrates the four regions in a volume formatted FAT:

以下图表说明了四个区域在FAT卷中的区域分布:

All of the FAT file systems were originally developed for the IBM PC machine architecture.
Hence, on disk data structures for the FAT format are all “little endian.”

整个FAT文件系统最初都是为IBM的PC机的架构而开发的。
因此,FAT格式在磁盘上的数据结构是小端存储。

To illustrate, below is an instance of a 32-bit FAT entry stored on disk as a series of four 8-bit bytes—the first being byte[0] and the last being byte[3]. The 32 bits numbered 00 through 31 are stored as follows (00 being the least significant bit):

为了进一步解释说明,以下是一个32位的FAT条目实例,它通过一系列的4个8bit字节存储在磁盘上,第一个字节是byte[0],最后一个是byte[3]。32位数通过00到31位进行编号,其存储结构如下,00是最低有效位:

1
2
3
4
5
6
7
8
9
10
11
byte[3] 3 3 2 2 2 2 2 2
1 0 9 8 7 6 5 4

byte[2] 2 2 2 2 1 1 1 1
3 2 1 0 9 8 7 6

byte[1] 1 1 1 1 1 1 0 0
5 4 3 2 1 0 9 8

byte[0] 0 0 0 0 0 0 0 0
7 6 5 4 3 2 1 0

Section 3: Boot Sector and BPB

The BPB (BIOS Parameter Block) is located in the first sector of the volume in the Reserved Region.
This sector is sometimes called the “boot sector” or the “0 th sector”.
The important fact to note is that this sector is simply the first sector of the volume.
All FAT volumes must have a BPB in the boot sector. The BPB has evolved over the years as follows:
BPB(BIOS参数块)位于卷的保留区域中的第一个扇区。
这个扇区通常叫做引导扇区或0扇区。
一个很重要的注意点就是该扇区仅仅只是卷的第一个扇区。
所有的FAT卷都必须有个BPB在引导扇区中。
BPB的进化史如下:

  • The FAT implementation in MS-DOS 1.x did not include a BPB (introduced in MS-DOS 2.x)

    在MS-DOS 1.x中FAT的实现不包含BPB(它在MS-DOS 2.x中被引入)

  • The BPB in MS-DOS 3.x was modified to allow > 64K sectors

    在MS-DOS 3.x 中 BPB被修改为允许大于64K个扇区。

  • For FAT32, the BPB differs from that for FAT16/FAT12 beginning at offset 36

    对于FAT32文件系统,BPB 与 FAT16/FAT12 的 BPB 不同,它从偏移量 36 开始。

The BPB in the boot sector of a FAT volume must always have all of the BPB fields for either the FAT12/FAT16 or FAT32 BPB type.
This ensures maximum compatibility of the FAT volume and will also ensure that all FAT file system drivers understand and support the volume correctly.

BPB在FAT卷的引导扇区上必须总是包含要么FAT12/FAT16要么FAT32的所有的BPB字段。
这确保了 FAT 卷的最大兼容性,也将确保所有 FAT 文件系统驱动程序正确识别和支持该卷。

NOTE:

In the following description, all the fields whose names start with BPB_ are part of the BPB.

All the fields whose names start with BS_ are part of the boot sector and not really part of the BPB.

注意:

在以下描述中,所有以BPB_开头命名的字段都是BPB的一部分。

所有以BS_开头命名的字段是引导扇区的一部分但不是BPB的真正部分。

The fields comprising the BPB are described further in this section.

在这一节,将更进一步描述这些构成BPB的字段。

3.1 BPB structure common to FAT12, FAT16, and FAT32 implementations

The below table describes the fields in the BPB that are common to all FAT variants.

下列表格描述了FAT变体的共同BPB字段。

BS_jmpBoot

Offset: 0 byte

Size: 3 bytes

Jump instruction to boot code. This field has two
allowed forms:

跳转指令到引导代码。这个字段有以下两种形式

jmpBoot[0] = 0xEB, jmpBoot[1] = 0x??, jmpBoot[2] = 0x90

and

jmpBoot[0] = 0xE9, jmpBoot[1] = 0x??, jmpBoot[2] = 0x??

0x?? indicates that any 8-bit value is allowed in
that byte.

0x?? 表示在那个字节中的任意允许的8位数。

What this forms is a three-byte Intel x86
unconditional branch (jump) instruction that jumps
to the start of the operating system bootstrap code.

这形成了intel x86的三字节无条件分支(jump)指令去跳转到操作系统引导代码的起始位置。

This code typically occupies the rest of sector 0 of
the volume following the BPB and possibly other
sectors. Either of these forms is acceptable.
JmpBoot[0] = 0xEB is the more frequently used
format.

此代码通常占据 BPB 之后的卷的其余扇区 0 以及可能的其他扇区。这些形式中的任何一种都是可以接受的。JmpBoot[0] = 0xEB 是使用最频繁的格式。

BS_OEMName

Offset: 3 byte

Size: 8 bytes

OEM Name Identifier.
Can be set by a FAT implementation to any desired value.
Typically this is some indication of what system formatted the volume.

OEM 名称标识。
可以通过FAT的实现设置为任何想要的值。
通常这被标识为是什么系统格式化了该卷。

BPB_BytsPerSec

Offset: 11 byte

Size: 2 bytes

Count of bytes per sector.
This value may take on only the following values: 512, 1024, 2048 or 4096.

每个扇区的比特数。
这个值通常只接受以下值:512, 1024, 2048 or 4096

BPB_SecPerClus

Offset: 13 byte

Size: 1 bytes

Number of sectors per allocation unit. This value must be a power of 2 that is greater than 0. The legal values are 1, 2, 4, 8, 16, 32, 64, and 128.

每个分配单元的扇区数。这个值必须是大于0的2的整数次方。
合法值如下,1,2,4,8,16,32,64和128。

BPB_RsvdSecCnt

Offset: 14 byte

Size: 2 bytes

Number of reserved sectors in the reserved region of the volume starting at the first sector of the volume.
This field is used to align the start of the data area to integral multiples of the cluster size with respect to the start of the partition/media.
This field must not be 0 and can be any non-zero value.
This field should typically be used to align the start of the data area (cluster #2) to the desired alignment unit, typically cluster size.

从卷的第一个扇区开始的保留区域中的保留扇区数。

这个字段用于对齐数据域的起始位置到相对于分区或存储媒介的簇大小的整数倍。

该字段不能是0,必须是非0数。

该字段通常应该被用于对齐数据域的起点(簇 #2)到期望的对齐单元。通常是簇的大小。

BPB_NumFATs

Offset: 16 byte

Size: 1 bytes

The count of file allocation tables (FATs) on the volume. A value of 2 is recommended although a value of 1 is acceptable.

卷上文件分配表的数目。通常推荐是2,即使1也是可以用的。

BPB_RootEntCnt

Offset: 17 byte

Size: 2 bytes

For FAT12 and FAT16 volumes, this field contains the count of 32-byte directory entries in the root directory.
For FAT32 volumes, this field must be set to 0. For FAT12 and FAT16 volumes, this value should always specify a count that when multiplied by 32 results in an even multiple of BPB_BytsPerSec.
For maximum compatibility, FAT16 volumes should use the value 512.

在FAT12和FAT16卷中,这个字段包含了32个字节的根目录入口。
对于FAT32卷,这个字段必须设置为0.对于FAT12和FAT16卷,该字段的值应该总是一个特殊的值,使其被32相乘后的结果是BPB_BytsPerSec的偶数倍。
出于最大兼容性考虑,FAT16卷应该使用512.

BPB_TotSec16

Offset: 19 byte

Size: 2 bytes

This field is the old 16-bit total count of sectors on the volume. This count includes the count of all
sectors in all four regions of the volume.
This field can be 0; if it is 0, then BPB_TotSec32
must be non-zero. For FAT32 volumes, this field
must be 0.
For FAT12 and FAT16 volumes, this field contains
the sector count, and BPB_TotSec32 is 0 if the
total sector count “fits” (is less than 0x10000).

BPB_Media

Offset: 21 byte

Size: 1 bytes

The legal values for this field are 0xF0, 0xF8, 0xF9,
0xFA, 0xFB, 0xFC, 0xFD, 0xFE, and 0xFF.
0xF8 is the standard value for “fixed” (non-
removable) media. For removable media, 0xF0 is
frequently used.

BPB_FATSz16

Offset: 22 byte

Size: 2 bytes

This field is the FAT12/FAT16 16-bit count of
sectors occupied by one FAT. On FAT32 volumes
this field must be 0, and BPB_FATSz32 contains
the FAT size count.

BPB_SecPerTrk

Offset: 24 byte

Size: 2 bytes

Sectors per track for interrupt 0x13.
This field is only relevant for media that have a
geometry (volume is broken down into tracks by
multiple heads and cylinders) and are visible on
interrupt 0x13.

BPB_NumHeads

Offset: 26 byte

Size: 2 bytes

Number of heads for interrupt 0x13. This field is
relevant as discussed earlier for BPB_SecPerTrk.
This field contains the one based “count of heads”.
For example, on a 1.44 MB 3.5-inch floppy drive
this value is 2.

BPB_HiddSec

Offset: 28 byte

Size: 4 bytes

Count of hidden sectors preceding the partition that
contains this FAT volume. This field is generally
only relevant for media visible on interrupt 0x13.
This field must always be zero on media that are
not partitioned.
NOTE: Attempting to utilize this field to align the
start of data area is incorrect.

BPB_TotSec32

Offset: 32 byte

Size: 4 bytes

This field is the new 32-bit total count of sectors on
the volume. This count includes the count of all
sectors in all four regions of the volume.
This field can be 0; if it is 0, then BPB_TotSec16
must be non-zero. For FAT12/FAT16 volumes, this
field contains the sector count if BPB_TotSec16 is
0 (count is greater than or equal to 0x10000).
For FAT32 volumes, this field must be non-zero.