Operating system practice - preliminaries - FAT12 file system

Reference: http://www.disc.ua.es/~gil/FAT12Description.pdf

floppy disk

The file organization format of floppy disk is generally FAT12, and the organization unit is divided into:

  • Partition: for example, C disk and D disk
  • Cluster: the file allocates storage space in clusters. A cluster contains one or more sectors
  • Sectors: the smallest unit of data on a disk

Each floppy disk has 2 sides * 80 cylinders * 18 sectors (physical sector number starts from 1) * 512 bytes = 1.44MB.

The logical sectors of the disk are numbered from 0 and read one cylinder at a time. For example, 0-17 corresponds to 0-cylinder, 18-35 corresponds to 1-cylinder. It is also said that the odd and even different faces of each sector are to be determined.

FAT12 organizational format

logical sector Occupied sector content Disk CHS parameters (head / cylinder / sector)
0 1(512B) Bootloader 0/0/1
1 9(4608B) FAT file distribution table 1 0/0/2 ~0/0/10
10 9(4608B) FAT file allocation table 2, redundant data 0/0/11 ~1/0/1
19 14(9728B) Catalog file entry Length is not fixed, specified in the first sector
33 end File data area
  • Boot sector: 0
  • FAT1 table: sector 1-9
  • FAT2 table: sector 10-18, redundant structure of FAT1
  • Root directory area: the length is not fixed, which is determined by the number of directory entries in BPB information. Each directory entry occupies 32 bytes (at least 64 bytes when long filename is supported)
  • Data area: non fixed length

boot sector

The first sector of the floppy disk is the boot sector, which stores the file system metadata (type, total number of logical sectors, number of sectors contained in each cluster, relationship between tracks and sectors, etc.). The 512B is divided into three parts in order:

  • BPB data structure: size 62 bytes, save file system metadata
  • Other 448 bytes: can be used to store boot code, data and other fill characters
  • Boot sector flag: last 2 bytes, 0xAA55

The 512 bytes of the boot sector are distributed as follows:

Name Start byte length content reference value
BS_jmpBOOT 0 3 Jump instruction jmp _bootsect
nop (short jump 2 bytes, plus 1 byte nop)
BS_OEMName 3 8 OEM string, must be 8 characters, not enough space "MSWIN4.1"
BPB_BytesPerSec 11 2 Bytes per sector 0x200
BPB_SecPerClus 13 1 Sectors Per Cluster 0x1
BPB_ResvdSecCnt 14 2 Number of reserved sectors (for Boot) 0x1
BPB_NumFATs 16 1 How many FAT tables are there 0x2
BPB_RootEntCnt 17 2 Maximum number of entries in root 0xE0
BPB_TotSec16 19 2 Total logical sectors 0xB40
BPB_Media 21 1 Media descriptor 0xF0
BPB_FATSz16 22 2 Sectors per FAT table 0x9
BPB_SecPerTrk 24 2 Sectors per track 0x12
BPB_NumHeads 26 2 Number of heads (number of faces) 0x2
BPB_HiddSec 28 4 Hidden Sector 0
BPB_TotSec32 32 4 If BPb? Tosec16 = 0, the number of sectors is given here 0
BS_DrvNum 36 1 Drive letter for INT 13H, starting at 0 0
BS_Reserved1 37 1 Reserved, not used 0
BS_BootSig 38 1 Extended boot flag, fixed 0x29
BS_VolID 39 4 Volume serial number 0
BS_VolLab 43 11 Volume label, must be 11 characters, not enough for space filling "hello world"
BS_FileSysType 54 8 File system type, must be 8 characters, insufficient space "FAT12 "
Guide code and other contents 62 448 Boot code and data,, not enough space. Jump from 0 bytes
End flag 0xAA55 510 2 The 510th byte is 0x55 and the 511st byte is 0xAA 0xAA55

FAT table

FAT1 and FAT2 tables are exactly the same, with 9 sectors each. Fat table is a bitmap, and every 12 bits form a FAT Entry to represent a cluster.

Physical sector code = 33 + FAT sector code - 2

FAT item No. starts from 0, and No. 0 and No. 1 are reserved. FAT item No. 2 starts from each cluster in the data area, pointing to No. 33 of the physical sector (No. 0-32 data is not played), but No. 2 usually puts the volume label after partition, generally starting from No. 3, pointing to No. 34 of the physical sector (address 0x4400). Up to 4096 clusters (9 sectors * 512 bytes * 8bit / 12bit) are supported.

When the FAT item is not 0, it means that the corresponding sector has been used by the file, and its value represents the next cluster number of the file (logical sector number, 0-2880). The meaning of the FAT item value:

  • 0x0ff0 ~ 0x0ff6: reserved
  • 0x0ff7: bad cluster, track or cylinder is damaged and cannot be used. It is automatically filled by the system when the format disk is used
  • 0x0ff8 ~ 0x0fff means the end of file content, until this cluster
  • Other values: the next cluster number of the current file

Root directory area

General entry

The maximum file name is 8 characters, and the maximum suffix is 3 characters. Only ASCII is supported.

Offset (in bytes) Length (in bytes) Description
0 8 Filename (but see notes below about the first byte in this field)
8 3 Extension
11 1 Attributes (see details below)
12 2 Reserved
14 2 Creation Time
16 2 Creation Date
18 2 Last Access Date
20 2 Ignore in FAT12
22 2 Last Write Time
24 2 Last Write Date
26 2 First Logical Cluster
28 4 File Size (in bytes)

The file directory entries occupy 14 sectors, each of which occupies 32 bytes. The maximum supported number of root directories is: 14 sectors * 512 bytes / 32 bytes = 224.

The start sector number of the root directory area is 19, which is composed of several directory entries. The maximum number of entries is BPb [rootentcnt. Because the size of the root directory area depends on BPb [rootentcnt], the length is not fixed.

Some directory entries are as follows:

Name deviation length describe
DIR_Name 0 0xB File name 8 bytes, extension 3 bytes
DIR_Attr 0xB 1 File attribute
DIR_WrtTime 0x16 2 Last modified
DIR_WrtDate 0x18 2 Last modified date
DIR_FstClus 0x1A 2 Start cluster number corresponding to this entry
DIR_FileSize 0x1C 4 file size

Support LFN entries with long filenames

Long File Name
The maximum number of FAT12 early filenames is 8 characters and the maximum number of suffixes is 3. Each directory entry is now 32 bytes. In order to support long filenames, a 32 byte LFN entry is added. LFN entries are unlimited before normal entries.

LFN Reference here.
LFN entry structure

Byte range describe
0 Serial number and assignment status
1 - 10 Filename character (Unicode)
11 File attribute
12 Retain
13 Checksum
14 - 25 Filename character (Unicode)
26 - 27 Retain
28 - 31 Filename character (Unicode)

Difference between creating directory and creating file

  • In the root directory, no matter the file or directory is created, at least one entry will be allocated in the FAT table, and one entry will be allocated in the root directory area.
  • When a subdirectory is created, at least one entry is assigned in the FAT table and one entry is assigned in the current subdirectory area.

Data area

Each file occupies at least one sector and forms a cluster chain through FAT table.

Create and operate FAT12 under Linux

Create FAT12 file system

Create an empty file

dd if=/dev/zero of=floppy.img bs=512 count=2880

Format bit FAT12

mkfs.vfat -F 12 floppy.img

View content

Check the binary contents of the floppy disk through the hexdump -C floppy.img | less command:

00000000  eb 3c 90 6d 6b 66 73 2e  66 61 74 00 02 01 01 00  |.<.mkfs.fat.....|
00000010  02 e0 00 40 0b f0 09 00  12 00 02 00 00 00 00 00  |...@............|
00000020  00 00 00 00 00 00 29 18  47 11 5a 4e 4f 20 4e 41  |......).G.ZNO NA|
00000030  4d 45 20 20 20 20 46 41  54 31 32 20 20 20 0e 1f  |ME    FAT12   ..|
00000040  be 5b 7c ac 22 c0 74 0b  56 b4 0e bb 07 00 cd 10  |.[|.".t.V.......|
00000050  5e eb f0 32 e4 cd 16 cd  19 eb fe 54 68 69 73 20  |^..2.......This |
00000060  69 73 20 6e 6f 74 20 61  20 62 6f 6f 74 61 62 6c  |is not a bootabl|
00000070  65 20 64 69 73 6b 2e 20  20 50 6c 65 61 73 65 20  |e disk.  Please |
00000080  69 6e 73 65 72 74 20 61  20 62 6f 6f 74 61 62 6c  |insert a bootabl|
00000090  65 20 66 6c 6f 70 70 79  20 61 6e 64 0d 0a 70 72  |e floppy and..pr|
000000a0  65 73 73 20 61 6e 79 20  6b 65 79 20 74 6f 20 74  |ess any key to t|
000000b0  72 79 20 61 67 61 69 6e  20 2e 2e 2e 20 0d 0a 00  |ry again ... ...|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001400  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00168000
Name Start byte length content
BS_jmpBOOT 0 3
BS_OEMName 3 8 "mkfs.fat"
BPB_BytesPerSec 11 2 0x0200
BPB_SecPerClus 13 1 0x01
BPB_ResvdSecCnt 14 2 Reserved sectors (for Boot) 0x0001
BPB_NumFATs 16 1 How many FAT forms are there? 0x02
BPB_RootEntCnt 17 2 Maximum number of files in root 0x00E0
BPB_TotSec16 19 2 Total logical sectors 0x0B40
BPB_Media 21 1 Media descriptor 0xF0
BPB_FATSz16 22 2 Sectors per FAT table 0x0009
BPB_SecPerTrk 24 2 Sectors per track 0x0012
BPB_NumHeads 26 2 Number of heads (number of faces) 0x0002
BPB_HiddSec 28 4 Number of hidden sectors 0x00000000
BPB_TotSec32 32 4 If BPb ﹣ tosec16 = 0, the number of sectors 0x00000000 is given here
BS_DrvNum 36 1 Drive letter of INT 13H, starting from 0 0x00
BS_Reserved1 37 1 Reserved, not used 0x00
BS_BootSig 38 1 Extended boot flag, fixed 0x29
BS_VolID 39 4 Volume serial number 0x5a114718
BS_VolLab 43 11 Volume label, must be 11 characters, not enough space to fill in "NO NAME"
BS_FileSysType 54 8 File system type, must be 8 characters, insufficient space to fill in "FAT12"
Guide code and other contents 62 448 Boot code and data,, not enough space. Jump from 0 bytes
End flag 0xAA55 510 2 0xaa55

Mount and write to file

command

mount floppy.img /mnt
cp a.txt /mnt
umount /mnt
hexdump -C floppy.img | less

View file system

Root directory area

The root directory area starts from sector 19 (19 * 512Byte, 0x2600). There are 224 entries, accounting for 14 sectors.

You can see that the first file a.txt corresponds to 64 bytes of the directory area, indicating that LFN long file names are supported (the following example is provided), and the root directory entry currently has LFN entries. The properties of the file you just created are:

  • Filename + suffix: 41 61 00 2e 00 74 00 78 00 74 00
  • Property: 0f
  • The sequence number of the first logical cluster: the 5th and 6th bytes from the bottom of the basic directory entry, i.e. 0x0003, indicates that the first sector of the file is in the 3rd logical sector, corresponding to 33 + 3-2 = 34 of the physical sector. Go to the FAT table to find the third, fff, which means that the file has only one sector
  • File length: the last four bytes of the basic directory entry. d321 bytes
  • Last written date: the 7th and 8th bytes from the bottom of the basic directory entry, i.e. 0x502c, 20524
  • Last write time: the 9th and 10th bytes from the bottom of the basic directory entry, i.e. 0x1ef97929

FAT table

FAT1 table starts from the first sector (512Byte, 0x200), removes the first two entries (24bit in total), and gets 00 f0 ff, which is sorted as 000 and fff according to the bit stream. So entry 3 is fff, which means there is only one sector in this file.

Data area

One boot sector + 18 FAT sectors + 14 root sectors, 33 sectors before the data area. Data area starting address = 34 * 512B = 0x4400.

Binary of FAT12 file

000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001400  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002600  41 61 00 2e 00 74 00 78  00 74 00 0f 00 5d 00 00  |Aa...t.x.t...]..|
00002610  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00002620  41 20 20 20 20 20 20 20  54 58 54 20 00 1e f9 1e  |A       TXT ....|
00002630  2c 50 2c 50 00 00 f9 1e  2c 50 03 00 d3 00 00 00  |,P,P....,P......|
00002640  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004400  74 6f 74 61 6c 20 31 34  34 38 0a 64 72 77 78 72  |total 1448.drwxr|
00004410  2d 78 72 2d 78 20 32 20  72 6f 6f 74 20 72 6f 6f  |-xr-x 2 root roo|
00004420  74 20 20 20 20 34 30 39  36 20 4a 61 6e 20 31 31  |t    4096 Jan 11|
00004430  20 32 32 3a 35 35 20 2e  2f 0a 64 72 77 78 72 2d  | 22:55 ./.drwxr-|
00004440  78 72 2d 78 20 35 20 72  6f 6f 74 20 72 6f 6f 74  |xr-x 5 root root|
00004450  20 20 20 20 34 30 39 36  20 4a 61 6e 20 31 31 20  |    4096 Jan 11 |
00004460  32 32 3a 34 31 20 2e 2e  2f 0a 2d 72 77 2d 72 2d  |22:41 ../.-rw-r-|
00004470  2d 72 2d 2d 20 31 20 72  6f 6f 74 20 72 6f 6f 74  |-r-- 1 root root|
00004480  20 20 20 20 20 20 20 30  20 4a 61 6e 20 31 31 20  |       0 Jan 11 |
00004490  32 32 3a 35 35 20 61 2e  74 78 74 0a 2d 72 77 2d  |22:55 a.txt.-rw-|
000044a0  72 2d 2d 72 2d 2d 20 31  20 72 6f 6f 74 20 72 6f  |r--r-- 1 root ro|
000044b0  6f 74 20 31 34 37 34 35  36 30 20 4a 61 6e 20 31  |ot 1474560 Jan 1|
000044c0  31 20 32 32 3a 35 35 20  66 6c 6f 70 70 79 2e 69  |1 22:55 floppy.i|
000044d0  6d 67 0a 00 00 00 00 00  00 00 00 00 00 00 00 00  |mg..............|
000044e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00168000

Create a file and directory larger than 512B

Files larger than one sector

  • File size: 0x000002a, i.e. 522B
  • The first sector corresponds to FAT logical serial number: 0004, and the corresponding data is 05 f0 ff, that is, 005. This means that this file has more than one sector and the logical number of the next sector is 005, which just happens to contain fff, so this file occupies two sectors

Catalog

  • The first sector corresponds to FAT logical serial number: 0006, and the corresponding data is fff. Therefore, this directory occupies a sector, which is used to place subdirectory entries (similar to root directory entries)
  • Go to physical sector 0x4a00 (33 + 6-2 = 37), and you can see that two directories have been created:... And
00000200  f0 ff ff 00 f0 ff 05 f0  ff ff 0f 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001400  f0 ff ff 00 f0 ff 05 f0  ff ff 0f 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002600  41 61 00 2e 00 74 00 78  00 74 00 0f 00 5d 00 00  |Aa...t.x.t...]..|
00002610  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00002620  41 20 20 20 20 20 20 20  54 58 54 20 00 1e f9 1e  |A       TXT ....|
00002630  2c 50 2c 50 00 00 f9 1e  2c 50 03 00 d3 00 00 00  |,P,P....,P......|
00002640  41 62 00 2e 00 74 00 78  00 74 00 0f 00 1d 00 00  |Ab...t.x.t......|
00002650  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00002660  42 20 20 20 20 20 20 20  54 58 54 20 00 2d f1 22  |B       TXT .-."|
00002670  2c 50 2c 50 00 00 f1 22  2c 50 04 00 0a 02 00 00  |,P,P...",P......|
00002680  41 64 00 69 00 72 00 00  00 ff ff 0f 00 fe ff ff  |Ad.i.r..........|
00002690  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
000026a0  44 49 52 20 20 20 20 20  20 20 20 10 00 a6 8f 2a  |DIR        ....*|
000026b0  2c 50 2c 50 00 00 8f 2a  2c 50 06 00 00 00 00 00  |,P,P...*,P......|
000026c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
...
00004800  6c 6f 70 70 79 2e 69 6d  67 0a 00 00 00 00 00 00  |loppy.img.......|
00004810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004a00  2e 20 20 20 20 20 20 20  20 20 20 10 00 a6 8f 2a  |.          ....*|
00004a10  2c 50 2c 50 00 00 8f 2a  2c 50 06 00 00 00 00 00  |,P,P...*,P......|
00004a20  2e 2e 20 20 20 20 20 20  20 20 20 10 00 a6 8f 2a  |..         ....*|
00004a30  2c 50 2c 50 00 00 8f 2a  2c 50 00 00 00 00 00 00  |,P,P...*,P......|
00004a40  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00168000

Then create abcdefghijklmn.txt and / dir/c.txt

Long file name

You can see the file with long file name, which takes up 3 entries (two LFN s, one basic entry):
Directory area:

00002600  41 61 00 2e 00 74 00 78  00 74 00 0f 00 5d 00 00  |Aa...t.x.t...]..|
00002610  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00002620  41 20 20 20 20 20 20 20  54 58 54 20 00 1e f9 1e  |A       TXT ....|
00002630  2c 50 2c 50 00 00 f9 1e  2c 50 03 00 d3 00 00 00  |,P,P....,P......|
00002640  41 62 00 2e 00 74 00 78  00 74 00 0f 00 1d 00 00  |Ab...t.x.t......|
00002650  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00002660  42 20 20 20 20 20 20 20  54 58 54 20 00 2d f1 22  |B       TXT .-."|
00002670  2c 50 2c 50 00 00 f1 22  2c 50 04 00 0a 02 00 00  |,P,P...",P......|
00002680  41 64 00 69 00 72 00 00  00 ff ff 0f 00 fe ff ff  |Ad.i.r..........|
00002690  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
000026a0  44 49 52 20 20 20 20 20  20 20 20 10 00 01 a8 2c  |DIR        ....,|
000026b0  2c 50 2c 50 00 00 a8 2c  2c 50 06 00 00 00 00 00  |,P,P...,,P......|
000026c0  42 6e 00 6f 00 70 00 71  00 2e 00 0f 00 27 74 00  |Bn.o.p.q.....'t.|
000026d0  78 00 74 00 00 00 ff ff  ff ff 00 00 ff ff ff ff  |x.t.............|
000026e0  01 61 00 62 00 63 00 64  00 65 00 0f 00 27 66 00  |.a.b.c.d.e...'f.|
000026f0  67 00 68 00 69 00 6a 00  6b 00 00 00 6c 00 6d 00  |g.h.i.j.k...l.m.|
00002700  41 42 43 44 45 46 7e 31  54 58 54 20 00 8a 94 2c  |ABCDEF~1TXT ...,|
00002710  2c 50 2c 50 00 00 94 2c  2c 50 07 00 0a 02 00 00  |,P,P...,,P......|
00002720  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

Create files in subdirectories

The file attributes are written in the data area of the subdirectory instead of the root area.

Data area corresponding to dir Directory:

00004a00  2e 20 20 20 20 20 20 20  20 20 20 10 00 a6 8f 2a  |.          ....*|
00004a10  2c 50 2c 50 00 00 8f 2a  2c 50 06 00 00 00 00 00  |,P,P...*,P......|
00004a20  2e 2e 20 20 20 20 20 20  20 20 20 10 00 a6 8f 2a  |..         ....*|
00004a30  2c 50 2c 50 00 00 8f 2a  2c 50 00 00 00 00 00 00  |,P,P...*,P......|
00004a40  41 63 00 2e 00 74 00 78  00 74 00 0f 00 dc 00 00  |Ac...t.x.t......|
00004a50  ff ff ff ff ff ff ff ff  ff ff 00 00 ff ff ff ff  |................|
00004a60  43 20 20 20 20 20 20 20  54 58 54 20 00 01 a8 2c  |C       TXT ...,|
00004a70  2c 50 2c 50 00 00 a8 2c  2c 50 09 00 09 00 00 00  |,P,P...,,P......|
00004a80  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

You can see that c.txt points to FAT table 9, and its value is fff, so the file only occupies one sector:

00000200  f0 ff ff 00 f0 ff 05 f0  ff ff 8f 00 ff ff ff 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

c. Data area of TXT:

00005000  61 61 61 61 61 61 61 61  0a 00 00 00 00 00 00 00  |aaaaaaaa........|
00005010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
Published 291 original articles, praised 134, visited 910000+
His message board follow

Tags: Attribute less ascii Linux

Posted on Sun, 12 Jan 2020 03:52:41 -0500 by rane500