Page 1 of 1

FAT12 clusters not correctly calculated

Posted: Fri Sep 09, 2011 4:08 pm
by miker00lz
for some reason, my FAT driver is not following cluster chains properly for FAT12 floppies. FAT16 on hard drives works just fine, and according to the wiki here, this code should do it? reading the first cluster given in it's directory entry of any file always returns the correct data, but when i go into the allocation table to get the next cluster it always reads the value as 512 or 6632. wtf?

Code: Select all

uint16 get_clust_val(uint8 part, uint32 cluster) {
    uint16 i;
    if ((fat[part].bpb.totalsects / fat[part].bpb.sectsperclust) < 4085) { //FAT12
        cluster = cluster + (cluster>>1);
        disk_read(fat[part].drive, fat[part].fatloc + cluster, (void far *)FARPTR(_CS, &i), 2);
        if (cluster&1) i = i >> 4;
            else i &= 0x0FFF;
    } else { //FAT16
        disk_read(fat[part].drive, fat[part].fatloc + (cluster<<1), (void far *)FARPTR(_CS, &i), 2);
    }
    return(i);
}

uint32 follow_clust_chain(uint16 part, uint32 first, uint32 count) {
    uint32 cluster, i;

    if (count==0) return(first);
    cluster = first;

    for (i=0; i<count; i++) {
        cluster = (uint32)get_clust_val(part, cluster);
    }
    return(cluster);
}

Re: FAT12 clusters not correctly calculated

Posted: Fri Sep 09, 2011 4:33 pm
by gerryg400
I think the problem is that you're trashing the cluster variable too early. You need a local variable something like this.

Code: Select all

uint16 get_clust_val(uint8 part, uint32 cluster) {
    uint16 i;
    uint16 c;
    if ((fat[part].bpb.totalsects / fat[part].bpb.sectsperclust) < 4085) { //FAT12
        c = cluster + (cluster>>1);
        disk_read(fat[part].drive, fat[part].fatloc + c, (void far *)FARPTR(_CS, &i), 2);
        if (cluster&1) i = i >> 4;
            else i &= 0x0FFF;
    } else { //FAT16
        disk_read(fat[part].drive, fat[part].fatloc + (cluster<<1), (void far *)FARPTR(_CS, &i), 2);
    }
    return(i);
}

Re: FAT12 clusters not correctly calculated

Posted: Fri Sep 09, 2011 4:35 pm
by miker00lz
oh man... i feel stupid. #-o

i think i need a break. what an obvious one. of course, i need to know whether the cluster was odd or even before expanding the value. thanks.

Re: FAT12 clusters not correctly calculated

Posted: Fri Sep 09, 2011 4:54 pm
by gerryg400
miker00lz wrote:oh man... i feel stupid. #-o

i think i need a break. what an obvious one. of course, i need to know whether the cluster was odd or even before expanding the value. thanks.
I wouldn't be too worried about an error like that. Obvious errors in well-structured code are never a problem. It's subtle errors in poorly written code that no code review ever finds.