Comprehensive Analysis of the fat12 File System in Chapter 4 of "writing an operating system by yourself"

Source: Internet
Author: User
Tags time and date

I. fat12

Fat12 is a file system used in the DOS era. It is still used on a floppy disk until now. After the fat12 floppy disk is formatted, it has two heads, each head has 80 cylinder (track), each cylinder has 18 sectors, and each sector has 512 bytes of space. Therefore, the total space of the standard floppy disk is:

2*80*18*512 = 1474560b = 1440 K = 1.44 m

The structure of fat12 is as follows:


1. Boot Sector

The reason why the operating system recognizes a disk in the fat12 format lies in the fact that the logical 0-sector 512b. If the content of the last two bytes of the 512 bytes is 55 and AA (0xaa55 low-byte front and high-byte back), the BIOS will read this sector to 0 at startup: 7c00h-0: 7dffh, and then jump to 0: 7c00h to continue executing the command, the operating system uses this to boot the system, and this disk is called a boot disk.

The Operating System ID fat12 file system stores a specific data structure in the logic 0-sector (that is, the boot sector), which has a fixed format, the disk is automatically generated when the operating system formats the disk. The specific data structure is shown in the following table:

 

Name

Start byte

Length

Content

Reference Value

Bs_jmpboot

0

3

A short jump command

JMP short label_start

NOP

Bs_oemname

3

8

Vendor name

'Zgh'

Bpb_bytespersec

11

2

Bytes per slice (Bytes/sector)

Zero X 200

Bpb_secperclus

13

1

Number of sectors per cluster (Sector/cluster)

0x1

Bpb_resvdseccnt

14

2

Number of sectors occupied by the Boot Record

Ox1

Bpb_numfats

16

1

Total number of fat tables

0x2

Bpb_rootentcnt

17

2

Maximum number of files in the root directory

0xe0

Bpb_totsec16

19

2

Total slice count

0xb40

Bpb_media

21

1

Media Descriptor

0xf0

Bpb_fatsz16

22

2

Number of sectors occupied by each fat table

0x9

Bpb_secpertrk

24

2

Number of sectors per track (Sector/track)

0x12

Bpb_numheads

26

2

Number of cores (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

Drive letter of INT 13 H

0

Bs_reserved1

37

1

Reserved, unused

0

Bs_bootsig

38

1

Extended boot tag (29 H)

0x29

Bs_volid

39

4

Volume serial number

0

Bs_vollab

43

11

Scale

'Zgh'

Bs_file1_ype

54

8

File System Type

'Fat12'

Guide code and other content

62

448

Guide code and other data

Guide code (fill the remaining space with 0)

End flag 0xaa55

510

2

510th Bytes: 0x55, 511st Bytes: 0xaa

0xaa55

The following describes the meanings of some variables:

Bs_jmpboot: A jump command. The jump command at offset 0 must be a valid and executable x86-based CPU command, such as JMP start, which can generate a 3-byte long command, (the length of the Short jump command with the keyword short is 2 bytes), pointing to the Operating System Boot Code section. The jump command in the fat12 boot sector generated by windows and MS-DOS is short jump, such as: JMP
Short label_start, and then add a NOP empty command to keep the length of 3 bytes.

Bpb_bytspersec: the number of bytes per slice. The type is dual-byte length. The number of bytes per slice in the standard partition is generally 512 B, and the fat12 format is set to 200 (0 x H ).

Bpb_secperclus: number of sectors per cluster. The offset is 13. The type is byte. The cluster is the smallest unit of data storage. Generally, the value is 1 in the fat12 format, that is, each cluster has only one sector (512 bytes ).

Bpb_rsvdseccnt: number of sectors occupied by the Boot Record, that is, the boot sector before fat1. Generally, the boot sector occupies 1 sector.

Bpb_numfats: Total number of fat tables. By default, the value of this field is 2, that is, two fat tables have the same content as fat1 and fat2, when an error occurs in the fat1 table, you can use fat2 to restore the file allocation table.

Bpb_rootentcnt: Maximum number of root directory files. The default value is 224. Each directory entry occupies 32B space. Therefore, the root directory size is 224*32/512 = 14, which occupies 14 sectors.

Bpb_totsec16: Total number of sectors = 0xb40 = 2880

Bpb_fatsz16: the number of sectors occupied by each fat is 0x9 = 9, that is, fat1 occupies 1-9 logical sectors, and fat2 occupies 10-18 logical sectors.

Bpb_secpertrk: number of sectors per track = 0x12 = 18, that is, in the standard fat12 file system, the number of sectors per track is 18.

Bpb_numheads: The number of magnetic heads = 0x2 = 2. the disk contains two heads, that is, the number of faces is 2.

2. Fat table

Fat1 and fat2 are two identical fat tables, each of which occupies nine sectors. Fat1 occupies 1-9 sectors and fat2 occupies 10-18 sectors. For details, see the following 4.

3. root directory

The START sector number of the root directory is 19, which consists of several directory entries, which can contain up to bpb_rootentcnt, because the size of the root directory is dependent on bpb_rootentcnt, the length is not fixed.

In fat12, because bpb_rootentcnt = 0xe0 = 14*16 + 0 = 244, the maximum number of entries is 244, and each entry occupies 32 bytes, therefore, 244*32/512 = 14 means that the root directory occupies 14 sectors, that is, 19-32.

Each entry in the root directory occupies 32 bytes. Its format is as follows:

The file name, attributes, the last write time and date, the number of start clusters of the file, and the file size are defined here.

The following describes the content through examples,

1. First, create a virtual floppy disk. Here we use winimage, which is specific to my download resources.

Open winimage:

Select File> New

After creating a new virtual floppy disk, you need to add files to it. We need to write the following files in advance:

River. txt, with the content riverriverriver

Flower. txt with the content of flowerflower ......... Flower, which requires at least 100 flower, so that the data space is greater than 512 bytes, so that the file will occupy two consecutive sectors.

Tree. txt, with the content treetreetree

Add another house directory and add two text files under the directory \ house:

Cat. txt with the content catcatcatcat

Dog. txt. The content is dogdogdog

Select "image-" and add the following files: River. txt, flower. txt, and tree. txt.

Image-create a folder house

Add house directory

Double-click house to go to the house folder and add cat. txt and dog. txt files.

After the file is added, save the file. Note that the storage type is: Virtual floppy disk image (*. VFD), I don't know the difference between this type and IMG, but I know that the result of this type is correct. Haha! The file name is Floopy, so we have created a virtual Floopy. VFD

Then use ultraedit to open Floopy. VFD. Because the root directory zone starts from 19th sectors and each sector has 512 bytes, the first byte is at the offset of 19*512 = 9278 = 0x2600, now let's locate 0x2600 to see what the directory entry is?

River. txt values:

Here, we only need to care about river. dir_fstclus of txt, that is, the start cluster number of the file. Because bpb_secperclus in fat12 is 1, a cluster is a sector, where dir_fstclus = 2, this indicates that the start sector of the file in the Data zone is 2. Note that the number of the first cluster in the Data zone is 2, instead of 0 or 1. Therefore, the data in this file starts from the first cluster in the Data zone, that is, the first sector.

Where is the first sector in the Data zone?

First, calculate the number of sectors occupied by the root directory:

Rootdirsectors = (bpb_rootentcnt * 32) + (BPB_BytsPerSec-1)/bpb_bytspersec.

The reason why the numerator needs to add (BPB_BytsPerSec-1) is to ensure that this formula is still true when the root directory cannot fill the integer sector.

In this example, because bpb_rootentcnt = 224, 14 sectors are occupied in the root directory. So

The fan area number starting from the data area = the fan area number starting from the root directory area + 14 = 19 + 14 = 33.

Now let's jump to the 33rd sector offset of 512*33 = 16896 = 0x00004200. Let's take a look at the content here:

It was riverriver.

4. Fat table

Here, because of river. TXT is smaller than 512 bytes, so we can find River in the data area without the fat table. TXT content, but for a file larger than 512 bytes, it is not so simple, you need to use the fat table to find all the Data Partition sectors occupied by the file.

Next let's jump to fat1. The first fan area of fat1 is 1, so the offset is 1*512 = 512 = 0x200.

A bunch of unreadable symbols, like a lot of F. In fact, it is not complicated. It is a bit like a bitmap. Each 12 bits are a fat entry, representing a cluster in a data zone. 0th and 1st fat items are never used. 2nd fat items start to represent each cluster in the Data zone. That is to say, 2nd fat items indicate the first cluster in the Data zone, and so on. As mentioned above, the number of the first cluster in the Data zone is 2, which is similar to that in this case.

Note that each fat item occupies 12 bits and contains one byte and another byte, so it is especially awkward. The specific situation is as follows:

Generally, the value of the fat item indicates the next cluster number of the file. However, if the value is greater than or equal to 0xff8, it indicates that the current cluster is the last cluster of the file. If the value is 0xff7, it indicates it is a bad cluster.

The START cluster number of the file river. txt is 2, and the value in the corresponding fat table is 0 xfff, indicating that the cluster is the last one.

File flower. the START cluster number of txt is 3, and the value in the corresponding fat table is 0x004, indicating that the file is not over yet, And the next cluster number is 0x004, then, let's look at the fat value corresponding to the 4th clusters in the fat table as 0 xfff, which indicates that this is the last cluster, then the file flower. TXT occupies 3rd and 4 clusters.

If you want to split the file content into discontinuous slices, you can add the file to the floppy disk driver first, and then add the same file to overwrite it. Of course, the file size must be greater than 512 bytes.

"Write the operating system yourself" read Sense http://blog.csdn.net/zgh1988/article/details/7059936

Comprehensive analysis of the first chapter of "self-writing Operating System" http://blog.csdn.net/zgh1988/article/details/7060032

Comprehensive Analysis of the "self-writing Operating System" Chapter 2 http://blog.csdn.net/zgh1988/article/details/7062065

Comprehensive Analysis of the "hands-on writing Operating System" Chapter 3 1 http://blog.csdn.net/zgh1988/article/details/7098981

Comprehensive Analysis of "self-writing Operating System" -- "real mode -- protection mode -- real mode" http://write.blog.csdn.net/postedit/7256254

Comprehensive Analysis of the "hands-on writing the Operating System" -- stack segment of the work mode http://blog.csdn.net/zgh1988/article/details/7256254

A comprehensive analysis of the "write your own operating system" --- privileged-Level Jump rules between different privileged-level code segments http://blog.csdn.net/zgh1988/article/details/7262901

Comprehensive Analysis of "self-writing Operating System" -- paging mechanism http://blog.csdn.net/zgh1988/article/details/7270748

A comprehensive analysis of "writing the operating system by yourself" -- interrupt mechanism http://blog.csdn.net/zgh1988/article/details/7276259


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.