FAT12 hell
Posted: Sun Nov 07, 2004 7:22 am
Hi all!
I'm currently writing a FAT12 driver, which is giving me some problems. I'm a bit confused about how to read the FAT entries, and finding the next cluster in chain. The weird problem is that a small file ( 1 or 2 clusters ) starting at an even cluster works just fine, but big files jump over all the place. I'm thinking about it, but I can't get the math straigt. As I've read over and over, odd clusters shift 4 to the right, even clusters mask with 0x0FFF. But what I don't get, if I use this method, some bytes will be missing. Because we use ?r the first 12 bits of a word, or the upper 12 bits. I.e. every FAT entry starts at bit 0 or bit 4 of a word. But as far as I can imagine, FAT entries can also start at bit 8 or 12 right? Or am I thinking to complicated?
My cat function:
I'm currently writing a FAT12 driver, which is giving me some problems. I'm a bit confused about how to read the FAT entries, and finding the next cluster in chain. The weird problem is that a small file ( 1 or 2 clusters ) starting at an even cluster works just fine, but big files jump over all the place. I'm thinking about it, but I can't get the math straigt. As I've read over and over, odd clusters shift 4 to the right, even clusters mask with 0x0FFF. But what I don't get, if I use this method, some bytes will be missing. Because we use ?r the first 12 bits of a word, or the upper 12 bits. I.e. every FAT entry starts at bit 0 or bit 4 of a word. But as far as I can imagine, FAT entries can also start at bit 8 or 12 right? Or am I thinking to complicated?
My cat function:
Code: Select all
... some BPB reading here...
int cluster = (block[(c*32)+26] ) + ( block[(c*32)+27] * 0xFF ) ; // Starting cluster from dir entry
int next_cluster=0, fat_offset=0;
BYTE FAT[0x1200];
fd_read_block(1,FAT, 9); // read the FAt, I know this is not the right way to go ;)
while(cluster < 0x0FF8) {
double fat_entry = (cluster * 3)/2;
fat_offset = FAT[(int)fat_entry] + (FAT[(int)fat_entry + 1] * 0xFF);
if(floor(fat_entry) == fat_entry){
next_cluster = fat_offset >> 4;
} else {
next_cluster = (fat_offset & 0x0FFF);
}
show_sector(cluster); // This function displays the contect of cluster x
cluster = next_cluster;
}