Page 1 of 1

Linux FAT16 off-by-one

Posted: Wed Aug 12, 2009 4:53 pm
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

Re: Linux FAT16 off-by-one

Posted: Wed Aug 12, 2009 5:01 pm
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.

Re: Linux FAT16 off-by-one

Posted: Wed Aug 12, 2009 5:02 pm
by pcmattman
Have you tried this?

Code: Select all

mount -t vfat -o loop,offset=512 image mnt

Re: Linux FAT16 off-by-one

Posted: Wed Aug 12, 2009 5:08 pm
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.