First, FAT12
FAT12 is the file system that started using the DOS era, and is still used on floppy disks, and the FAT12 floppy disk is formatted with two heads, 80 cylinders per head (track), 18 sectors per cylinder, and 512 byte space per sector. So the total space for the standard floppy disk is:
2 * *18 * 512=1474560b=1440k=1.44m
The following is a diagram of the FAT12 structure:
1. Boot Sector
The secret of the operating system's understanding of the FAT12 format disk is the logic 0 sector 512B. If the contents of the last two bytes of this 512 byte are 55 and AA (0xaa55 low byte in front, high byte behind), the BIOS will read this sector to 0:7C00H-0:7DFFH at boot, and then jump to 0:7c00h to continue executing the instruction. The operating system uses this to achieve the purpose of booting the system, and this disk is called the boot disk.
The operating system identifies the FAT12 file system because a specific data structure is stored in the logical 0 sector (that is, the boot sector), which has a fixed format that is automatically generated when the operating system formats the disk, as shown in the following table:
Name |
Start byte |
Length |
Content |
Reference values |
Bs_jmpboot |
0 |
3 |
A short jump instruction |
JMP Short Label_start Nop |
Bs_oemname |
3 |
8 |
Manufacturer name |
' Zgh ' |
Bpb_bytespersec |
11 |
2 |
Number of bytes per sector (Bytes/sector) |
0x200 |
Bpb_secperclus |
13 |
1 |
Number of sectors per cluster (Sector/cluster) |
0x1 |
Bpb_resvdseccnt |
14 |
2 |
How many sectors the boot record occupies |
Ox1 |
Bpb_numfats |
16 |
1 |
How many fat tables are there? |
0x2 |
Bpb_rootentcnt |
17 |
2 |
Maximum number of root zone files |
0xE0 |
Bpb_totsec16 |
19 |
2 |
Total Sectors |
0xB40 |
Bpb_media |
21st |
1 |
Media descriptors |
0xF0 |
Bpb_fatsz16 |
22 |
2 |
Number of sectors per fat table |
0x9 |
Bpb_secpertrk |
24 |
2 |
Number of sectors per track (Sector/track) |
0x12 |
Bpb_numheads |
26 |
2 |
Number of heads (number of faces) |
0x2 |
Bpb_hiddsec |
28 |
4 |
Number of hidden sectors |
0 |
Bpb_totsec32 |
32 |
4 |
If bpb_totsec16=0, the number of sectors is given here |
0 |
Bs_drvnum |
36 |
1 |
The drive letter of the INT 13H |
0 |
Bs_reserved1 |
37 |
1 |
Reserved, not used |
0 |
Bs_bootsig |
38 |
1 |
Extended boot tag (29h) |
0x29 |
Bs_volid |
39 |
4 |
Volume Serial Number |
0 |
Bs_vollab |
43 |
11 |
Volume label |
' Zgh ' |
Bs_filesystype |
54 |
8 |
File system type |
' FAT12 ' |
Boot code and other content |
62 |
448 |
Boot code and other data |
Boot code (0 padding for remaining space) |
End Flag 0xaa55 |
510 |
2 |
The No. 510 byte is 0x55, and the No. 511 byte is 0xAA |
0xaa55 |
Here we describe the meanings of some of these variables:
Bs_jmpboot: is a jump instruction, the jump instruction at offset 0 must be a valid executable based on x86 CPU instruction, such as: JMP start, this can generate 3-byte long instructions, (plus the short jump instruction of the key word length is 2 bytes), pointing to the operating system boot code section. The jump instructions in the Windows and MS-DOS-generated FAT12 boot sector are short jumps, such as: jmp Label_start, and then add a nop empty instruction to keep the length of 3 bytes.
Bpb_bytspersec: The number of bytes per sector, the type is double-byte long, the number of bytes per sector on the standard partition is typically 512B, and the format of FAT12 is set to (0x200h).
Bpb_secperclus: Number of sectors per cluster, offset 13, type is byte, cluster is the smallest unit of data storage, in FAT12 format is generally 1, that is, 1 sectors per cluster (512 bytes).
Bpb_rsvdseccnt:boot records How many sectors are occupied, that is, the boot sector before FAT1, typically, the boot sector occupies 1 sectors.
Bpb_numfats: The total number of fat tables, by default This field has a value of 2, that is, there are two fat tables, FAT1 and FAT2 the same content, when the FAT1 table error, you can use FAT2 to restore the file allocation table.
BPB_ROOTENTCNT: The maximum number of root files, default is 224, each directory entry occupies 32B of space, so the root directory size is: 224*32/512=14, which occupies 14 sectors.
BPB_TOTSEC16: Total Sectors =0xb40=2880
BPB_FATSZ16: The number of sectors per fat occupied =0x9=9, that is, FAT1 occupies 1-9 logical sectors, FAT2 occupies 10-18 logical sectors.
BPB_SECPERTRK: The number of sectors per track is =0x12=18, which is the standard FAT12 file system, with a sector count of 18 per track.
Bpb_numheads: The number of heads =0x2=2, the disk consists of 2 heads, that is, the number of polygons is 2. 2. Fat table
FAT1 and FAT2 are two identical fat tables, with each fat occupying 9 sectors. Where FAT1 occupies 1-9 sectors, FAT2 occupies 10-18 sectors. See below for a detailed description of the 4. 3. root directory area
The starting sector area code for the root area is 19, which consists of several directory entries (directory Entry), with entries of up to bpb_rootentcnt, because the size of the root area is dependent on bpb_rootentcnt, so the length is not fixed.
In this FAT12, because the bpb_rootentcnt=0xe0=14*16+0=244, that is, the entry is up to 244, and because each entry occupies 32 bytes, so 244*32/512=14, that is, the root directory area of 14 sectors, that is 19-32.
Each entry in the root area occupies 32 bytes, and its format is as follows:
This defines the name of the file, the attributes, the time and date of the last write, the starting cluster number of the file, and the file size.
Let's use examples to understand these things,
1, first create a virtual floppy disk, where we use WinImage, the specific download address in my download resources.
Open WinImage:
Select File-"new"
After creating a virtual floppy disk, you need to add files to it and we need to write the following files in advance.
River. TXT, content is Riverriverriver
FLOWER. TXT, the content is flowerflower.........flower, at least 100 flower, so that the data space is greater than 512 bytes, so that the file will occupy two contiguous sectors.
TREE. TXT, content is Treetreetree
Add a house directory, and then add two text files under directory \house:
CAT. TXT, content is Catcatcat
DOG. TXT, content is Dogdogdog
Select Image-Join, add river.txt,flower.txt,tree.txt three files in turn
Image-"Create Folder House"
Add House Catalog
Double-click House, go into House's folder, and add cat.txt,dog.txt two files
Add file completion, and then save the note, where the save type is: Virtual floppy image (*.VFD), I don't know what the difference is between this type and IMG, but I know that this type gets the right result. Oh. The file name is floopy, so we create a virtual floppy disk floopy.vfd
Then use UltraEdit to open floopy.vfd, because the root zone is starting from the 19th sector, each sector is 512 bytes, so its first byte is at offset 19*512=9278=0x2600, OK, Now let's go to 0x2600 to see what directory entry is.
River. The values of the txt:
Here, we only need to care about RIVER.TXT Dir_fstclus, that is, the beginning of the file cluster number, because of the bpb_secperclus=1 in this FAT12, so a cluster is a sector, dir_fstclus= 2, meaning that the file's starting sector code in the data area is 2. It should be noted here that the cluster number of the first cluster in the data area is 2, not 0 or 1, so the data for the file starts at the first cluster in the data area, that is, the first sector.
So where is the first sector of the data area?
First calculate the number of sectors that the root zone occupies:
Rootdirsectors = ((bpb_rootentcnt*32) + (bpb_bytspersec-1))/bpb_bytspersec.
The reason the numerator is added (bpb_bytspersec-1) is to ensure that the formula is still true when the root area cannot fill the integer sector.
In this example, because of bpb_rootentcnt=224, the calculated root area occupies a sector of 14. So
The sector area code starting with the data area = The sector area code +14=19+14=33 at the beginning of the root area.
Now let's jump into the 33rd sector offset is 512*33=16896=0x00004200, let's look at the content here:
Sure enough, it was riverriverriver. 4. Fat table
In this case, since RIVER.TXT is less than 512 bytes, we do not need the fat table to find the RIVER.TXT content in the data area, but for files larger than 512 bytes, it is not so easy to use a fat table to find all the data area sectors that the file occupies.
Let's jump into the contents of the FAT1, the FAT1 start sector area code is 1, so the offset is 1*512=512=0x200.
A bunch of symbols that don't understand, like a lot of f. It's not complicated, it's kind of like a bitmap, where every 12 bits becomes a fat item (Fat Entry), which represents a cluster in a data area. The No. 0 and 1th fat items are never used, and the 2nd FAT entries begin to represent each cluster in the data area, that is, the 2nd Fat entries represent the first cluster in the data area, and so on. As I said earlier, the cluster number of the first cluster in the data area is 2, echoing here.
It is important to note that because each fat item occupies 12 bits and contains one byte and another byte in general, it feels particularly awkward. This is the case, assuming that the 3 consecutive bytes are as shown in the figure:
Typically, the value of a FAT item represents the next cluster number of the file, but if the value is greater than or equal to 0XFF8, the current cluster is already the last cluster of the file. If the value is 0XFF7, it indicates that it is a bad cluster.
The starting cluster number for file RIVER.TXT is 2, and the corresponding FAT table has a value of 0xFFF, indicating that the cluster is already the last.
File FLOWER.TXT the start of the cluster number is 3, the corresponding FAT table value is 0x004, indicating that the file is not finished, the next cluster number is 0x004, and then we look at the Fat table in the 4th cluster corresponding to the fat value of 0xFFF, that is the last cluster, the file FLOWER.TXT occupy the 3rd , 4 clusters.
If you want to make the file content in a discontinuous sector, there is a way to do this is to first add the file to the floppy disk drive, and then in the process of adding the same file, to overwrite. Of course, the size of the file must be greater than 512 bytes.