FAT32 file system-for TF card

Source: Internet
Author: User
Document directory
  • 2.1 DBR Composition
  • Composition of 2.2 BPB (BiOS parameter block)
1. How is the TF card space allocated?

The following uses a 4 gb tf card as an example to analyze its space distribution using the winhex tool, as shown in:

FAT32 manages directories as files, so there is no independent directory area. All file directory items are in the data area.

2. Start the sector (DBR)

DBR (DOS Boot Record, DOS Boot Record), in the cylindrical 0, head 1, Sector 1, that is, the logical sector 0;
DBR includes:

• A boot program: the DOS boot program locates and loads the DOS system file (Io. sys, msdos. sys ).

• A bpb: BPB is used to describe the disk information of the DOS partition, such as the starting and ending sectors of the partition.

Note: Search for the DBR flag:
• Fat16 DBR: EB 3C 90, no backup DBR
• FAT32 DBR: EB 58 90, which has backup DBR, usually in the 6th sector of the partition
• Ntfs dbr: EB 52 90, which has backup DBR, usually in the last sector of the partition

2.1 DBR Composition

Offset Length/byte Components
00 h 3 Jump command
03 h 8 Operating system vendor ID and version number
08 h 80 The partition parameter table (BpB) contains the required parameters for read/write operations on the partition.
5ah 420 DOS Boot Code, which loads the DOS boot file IO. sys and msdos. sys into the memory
Feh 2 Ending flag "55aa"

Composition of 2.2 BPB (BiOS parameter block)

3. File Allocation Table

 Cluster)

It is the unit of measurement and distribution of disk files in an area of equal size divided into file data areas.
File Allocation Table

It is a special file located on the zero-sector disk. It contains the file size on the disk and the location of the cluster where the file is stored.
First cluster ID of the file

It is stored in the FDT (root directory) registration item, and the subsequent cluster number is stored in the fat.

The fat table identifies the space usage in the partition in the cluster unit (each ID occupies 4 bytes ). As shown in:

The first two clusters of fat are reserved clusters (cluster 0 and cluster 1), which are not assigned to files. Their meanings are as follows:
• Fat16: F8 FF
• FAT32: F8 FF 0f FF

A fat table item value indicates a cluster number occupied by the file and specifies the location of the next cluster number.

The fat table records the usage of each cluster on the disk in sequence. Use 32-bit binary codes for the usage of each cluster. Do not enter zero in the corresponding location of the unallocated cluster; enter a specific value in the corresponding location of the bad cluster; or enter a non-zero value in the corresponding location of the allocated cluster, specifically: If the cluster is the last cluster of the file, enter the value ffffff0fh (0x0fffffff). If the cluster is not the last cluster of the file, the value is the cluster number of the next cluster occupied by the file. In this way, each cluster occupied by the file is a cluster chain and saved in the fat table. The cluster numbers 0000000 H and H are not used. The corresponding DWORD positions (eight bytes starting with the fat table) are used to store the media type numbers of the disk.

The size of the fat table is determined by the number of clusters in the Data zone of the Logical Disk. An integer sector is used.

When a file system is created and formatted, the space allocated to the fat area is cleared, write a specific value to Table 0 and table 1 of fat1 and fat2. The root directory is also created when a file system is created, that is, a cluster space is allocated to the root directory, which is usually cluster 2, therefore, the fat table entry 2 corresponding to cluster 2 will also be written with an end mark, as shown in.

When a cluster has been allocated for use, the value of the fat table item in the corresponding fat table item is the cluster number in the next storage location of the file. If the file ends in the cluster, a file end mark is recorded in its fat table item. For FAT32, the value of the fat table item indicating the end of the file is 0x0fffffff.
If a cluster has a bad sector, the entire cluster will be marked as a bad cluster with the fat table item value 0xffffff7 and will not be used any more. This bad cluster tag will be recorded in the corresponding fat table item.

When creating a file in the file system, if the new file only occupies one cluster, the fat table entry corresponding to the cluster to be allocated will be written to the end mark. If the new file not only occupies one cluster, write the ID of the next cluster allocated to it in the fat table corresponding to each cluster it occupies, write the end mark in the fat representation corresponding to the last cluster.
When a directory is created, only one cluster space is allocated to it, and the ending mark is written in the corresponding fat table item. When the directory is larger than the size of a cluster, a cluster will be allocated to it in the free space, create a fat table chain for the fat table to describe the cluster it occupies.
When operating on files or directories, their corresponding fat table items will be cleared and set to 0 to indicate that their corresponding clusters are not allocated.

4. FDT (file directory table) file directory table

A file directory table (FDT) consists of several 32-byte table items. It records the following content of each file (directory): • file name • Extension • Support for long file names • Start Unit (that is, the start cluster number, which is the most important) • file attributes • Size • creation date and other content when formatting the disk using the format command, a root directory FDT has been created for the entire disk. All files and Their subdirectories in the root directory have a "directory registration item" or "directory item" in the root directory's file directory table (FDT ". Each directory registration entry occupies 32 bytes and is divided into 8 regions, providing information about files or subdirectories. The detailed description is shown in:
• 0 -- 7 bytes: the file name.

• 8-10 bytes: file extension. • 11 bytes: file attributes, defined by binary bits, are retained up to two times, the numbers 0 to 5 are read-only, hidden, system, volume, subdirectory, and archive.
• 12--13: bytes are used only for long file name directory items, and are used to store the file name byte checksum of their corresponding short file name directory items.
• 14--15: The creation time of the byte 24-bit binary file, in which the High 5 bits are hours and the next 6 bits are minutes.
• 16-17 Bytes: The creation date of a 16-bit binary file. The height of the 7-bit file is the year value relative to January 1, 1980. The next 4-bit file is the month, and the last 5 digits are the date within the month.
• 18--19 Bytes: The latest access date of a 16-bit binary file, which is defined as (6 ).
• 16-bit high of the starting cluster number of 20--21 bytes.
• The latest modification time of the 22--23 byte 16-bit binary file, in which the 5-bit height is hour, the 6-bit time is minute, and the second-digit time is second.
• The latest modification date of the 24--25-byte 16-bit binary file, which is defined as (6 ).
• A 16-bit low starting cluster number of 26--27 bytes.
• 28-31 bytes 32-bit file length. The format is little endian, that is, the high byte is located in the high address.


4.1 changes after files are deleted • In the FAT file system, when files are deleted, the operating system finds the corresponding directory file table and changes the first byte to the "E5" flag.
• At the same time, clear the project corresponding to the fat file allocation table for use by other programs
• The file data is not overwritten, but actually exists, but it is invalid for the operating system.
• When new files need to be stored, new data can be directly written to the original data location
The 4.2 Root root directory is no longer a fixed area or a fixed size, but can be considered as part of a data area. Because the root directory has been changed to the root directory file, it adopts the same management method as the sub-directory file. Generally, it is used from 2nd clusters and the size is increased as needed, therefore, the number of files in the root directory is no longer limited to 512.
In principle, FAT32 allows the root directory to be anywhere where data goes, but normally it is located in cluster 2.

4.2.1 it is not easy to locate the root directory in the FAT32 file system and find the location of the first cluster (cluster 2), that is, the starting location of the Data zone, because it is not at the beginning of the file system, but in the Data zone. In front of the data area, both the reserved region and the fat region do not use the fat table for management. Therefore, you can only use the slice address (logical volume address) in the region before the data zone, but not the cluster address.
To locate the data starting point of a FAT32 file system, you can use the relevant parameters of the boot sector to calculate it. 1) offset from the boot sector 0x0e ~ The reserved sector is obtained at the 0x0f byte. 2) Get the number of fat tables from the offset 0x10 bytes. 3) offset from 0x24 ~ 0x27 bytes. 4) use the following formula for calculation:
Number of reserved sectors + number of sectors in each fat Table X number of fat tables = fan ID at the beginning of the Data Area
To calculate the fan area number of other known cluster numbers, you must also find the number of sectors of each cluster at the 0x0 d offset of the boot sector. The formula is as follows:
Start sector ID of a cluster = number of reserved sectors + number of sectors in each fat Table X number of fat tables + (cluster ID-2) X number of sectors in each cluster

4.2.2 The root directory analysis root directory has been created when the file system is created, and its purpose is to store the directory (also called folder) or the directory items of the file. The size of each directory item is 32 bytes.
When the file system is created and no data is stored, there is no content in the root directory. The file system only allocates a cluster space for the root directory (usually Cluster 2 ), write the ending mark to the fat table corresponding to the cluster, indicating that the cluster has been allocated for use. At this time, the space allocated for the root directory does not have any content. If the volume label is set during file system creation, a volume label directory item is created under the root directory, which occupies the first directory position in the root directory.
Both the root directory and sub-directory items have the following basic features:
1) The cluster number of the first cluster allocated to a file or sub-directory is recorded in its directory. Other subsequent clusters are tracked by the fat table chain in the fat table.
2) In addition to recording the subdirectory or the starting cluster number of the file, the directory item also records its name, size (the subdirectory has no size), time value, and other information.
3) in addition to a short file directory, each subdirectory or file also has a long file name directory.
4) The Directory items of short file names occupy 32 bytes. The directory items of long file names occupy 1 or several 32 bytes as needed.
5) for the same subdirectory or file, its long file name directory item is stored before its short file name directory item. If the long file name directory item occupies multiple 32 bytes, it is stored in reverse order before the segment file name directory. 4.3 File Location Information of the root directory is recorded in DBR (2ch: Start cluster number of the disk root directory), by querying the root directory file table FDT, find the corresponding file and directory information, and record the file name, attributes, creation time, modification time information, and the starting cluster number of the file in FDT, the actual file size. With the starting cluster number in the root directory, you can calculate the starting sector number of the cluster: start sector ID of a cluster = number of reserved sectors + number of sectors in each fat Table X number of fat tables + (cluster ID-2) X number of sectors in each cluster
Based on the above formula, we can calculate the root directory's initial fan id = 36 (0x24) + 7566 (0x1d8e) x 2 + (2-2) x 8 = 15168
In DBR, the starting cluster number of the root directory is shown in:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.