FAT12 clusters not correctly calculated

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
miker00lz
Member
Member
Posts: 144
Joined: Wed Dec 08, 2010 3:16 am
Location: St. Louis, MO USA

FAT12 clusters not correctly calculated

Post 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);
}
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: FAT12 clusters not correctly calculated

Post 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);
}
If a trainstation is where trains stop, what is a workstation ?
User avatar
miker00lz
Member
Member
Posts: 144
Joined: Wed Dec 08, 2010 3:16 am
Location: St. Louis, MO USA

Re: FAT12 clusters not correctly calculated

Post 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.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: FAT12 clusters not correctly calculated

Post 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.
If a trainstation is where trains stop, what is a workstation ?
Post Reply