Linux FAT16 off-by-one

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mrnoob
Member
Member
Posts: 26
Joined: Thu Sep 18, 2008 11:45 am

Linux FAT16 off-by-one

Post by mrnoob »

hi,
I am working on a fat16 bootloader, but i am coming across a strange off-by-one error. Ill describe the background

I am making an OS for USB sticks, where they have an MBR with a FAT16 partition.
The MBR code for the bootloader works fine, but when i try to lookup STARTUP.SYS the FAT16 loader cannot find it.
I know that the location of the root directory (LBA) is partition_start + reserved_sectors + (number_of_fats * sectors_per_fat). This should be sector 490 in my case, as the partition starts at LBA 1, there is a reserved sector for the fat boot record, there are 2 FATS with 244 sectors each.
However, when i mount the image in linux (mount -o loop -t vfat image mnt) and create a file STARTUP.SYS, and examine the image, i find that the root directory entry has been written to sector 489 instead, which should be the last sector of the second FAT.
This is weird, as I have verified that sector 0 is the MBR, 1 is the FAT boot record, and 2 is the first sector of the first FAT. But 489 is the root directory's start, instead of 490. I assumed this was a mistake in conversions between LBA and CHS, but I examined the image and it isn't. Linux seems to be writing to the sector before the one it should. Is this a bug or have I gone wrong somewhere?

Thanks
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Re: Linux FAT16 off-by-one

Post by ~ »

Maybe it has to do that FAT filesystems take sectors as relative from the start of the partition, and thus is leaving the MBR LBA 0 out, so it is equal to 489 relative+MBR == 490.

That is if I remember correctly.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Linux FAT16 off-by-one

Post by pcmattman »

Have you tried this?

Code: Select all

mount -t vfat -o loop,offset=512 image mnt
mrnoob
Member
Member
Posts: 26
Joined: Thu Sep 18, 2008 11:45 am

Re: Linux FAT16 off-by-one

Post by mrnoob »

@pcmattman aah that works perfectly, thanks a lot :) I guess mount does confuse its offsets if you leave the MBR in after all.
Post Reply