Linux FAT16 off-by-one
Posted: Wed Aug 12, 2009 4:53 pm
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
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