1
votes

I created a FAT image with:

[user@localhost]$ dd if=/dev/zero of=floppy.img bs=1024 count=2880
[user@localhost]$ mkdosf -F 12 floppy.img

Then I mount the image and add a new file:

[user@localhost]$ echo "Hello, World!" >> /mnt/hello.txt

On Wikipedia the overview of the file system, data region is at: ReservedSectors + (NumerOfFATs * SectorsPerFAT) + ((NumberOfRootEntries * 32) / BytesPerSector)

The boot sector of floppy.img is the following:

jmp : 0xeb 0x3c
nop : 0x90
OEM : mkfs.fat
Bytes per sectors : 512
Sectors per cluster : 2
Reserved sectors : 1
FAT copies : 2
Root directory entries : 224
Small sectors : 5760
Media type : 0xf0
Sectors per FAT : 9
Sectors per track
Heads : 2
Hidden sectors : 0
Large sectors : 0
Drive number : 0
Signature : 41
Serial number : 1845425665
Volume label : NO NAME    
FS type : FS type : FAT12   
Executable : 0xaa55

In the Root directory table, I search for the hello.txt file, and returns me that the first cluster is at 0x03:

entry 1: HELLO.TXT cluster : 0x03

Now I compute the previous formula and gives me the following offset in sectors:

1 + (2 * 9) + ((224 * 32) / 512) = 33

In bytes should be 33 * 512 = 16896. That should be the start of the data region. To locate the data of the hello.txt file I should add an offset of Cluster * SectorsPerCluster = 6.

The data of the file should be located at sector 39 or at 19968 bytes from the start. But when I examine that sector with hexdump returns nothing:

[user@localhost]$ hexdump -C -s 19968 floppy.img
00004e00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

Reading the hedump, I found that hello.txt is at offset 17920 bytes or at sector 35 from the start.

What am I missing?

1

1 Answers

1
votes

The offset of the data section is correct: ReservedSectors + (NumerOfFATs * SectorsPerFAT) + ((NumberOfRootEntries * 32) / BytesPerSector).

The offset of the file cluster in the data section should be: DataSectionOffset + ((cluster - 2) * SectorsPerCluster). With this new formula now the data of the file hello.txt is at 33 + ((3 - 2) * 2) = 35.

In bytes that offset is 17920:

[user@localhost]$ hexdump -C -s 17920 floppy.img
00004600  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64 21 0a 00 00  |Hello, World!...|