Author | Message | Time |
---|---|---|
K | Currently working on a homework assignment for one of my Comp. Sci. classes -- the task is to write a program that can read/write FAT12 images. So far I have read the MBR, read the FATables, and read the root directory entry (subdirectories are not so important). I'm running into trouble trying to translate a cluster number from the FATable into an actual byte offset in the file: sometimes it works perfectly, other times I end up begining my read from halfway through where the file started. Here's how I'm translating cluster number into byte offset: v is a struct that contains data read from the MBR; floppy is a pointer to the image loaded in memory. [code] uint8_t ptr = 0; // first compute the sectors number of the cluster ptr += (v.hidden_sector_cnt + v.reserved_sector_cnt); ptr += volinfo.sectors_per_fat * volinfo.fat_cnt; // 32 = size of a Fat12 directory entry (bytes) ptr += (volinfo.root_dir_entries * 32) / volinfo.bytes_per_sector; // subtract two sectors for first two magic entries in the root dir ptr -= 2; // sector_number * bps ptr = (uint8_t*)((int)ptr * volinfo.bytes_per_sector); // actual offset into the file ptr += (int)floppy; // begin copying from ptr + (dirent.start_cluster * bytes_per_cluster) [/code] Any ideas? | April 27, 2005, 8:55 PM |
Adron | Adding by hidden/reserved sector count doesn't sound right. Aren't those just mapped in the fat, a reserved sector can be anywhere on the floppy? | April 28, 2005, 2:29 AM |
K | [quote author=Adron link=topic=11407.msg110233#msg110233 date=1114655350] Adding by hidden/reserved sector count doesn't sound right. Aren't those just mapped in the fat, a reserved sector can be anywhere on the floppy? [/quote] I actually got it to work -- I'm not sure what was wrong, but now it's working 100% of the time. I think the hidden sector is at the beginning of the image; I'm not sure about reserved, but the image we were given doesn't have any so I guess it's ok ;). | April 28, 2005, 5:14 AM |