I am working on a small OS kernel of my own. I am having a trouble accessing the "right" cluster for a given subdirectory on a FAT16 harddisk.
I got so far the grips on reading the OEM parameter block, and extracting the root directory info. But when I try to access a directory in the root (drive C) I get a wrong offset for the sector on the disk.
I am using the following code, which first calculates the absolute cluster from the current cluster number, then adds the root directory sectors (32 in my case), then adds the LBA of the start of the disk (I am using a partitioned drive, starting at cluster 0, head 1, sector 1 = LBA 63). then it adds the first data sector (amounts to 513, as I am using FAT16 with 2 tables, 256 sector each, and one reserved sector).
Code: Select all
uint32_t tmp = (pfile->current_cluster-2)*mount_info.sectors_per_cluster; //get absolute cluster number
tmp += (mount_info.num_root_entries*32)/mount_info.bytes_per_sector; //skip root directory
tmp += mount_info.lba; //add partition starting lba
tmp += mount_info.first_data_sector; //add first data sector
LBA_to_CHS(tmp, &cylinder, &head, §or); //convert into CHS
My question is: are the above calculations right? in other words, how can I correctly convert a relative cluster number into an absolute CHS to read it from the disk? remember I need to add the offset into the partition..
Thanx