FAT Driver

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
Therx

FAT Driver

Post by Therx »

Ok after rewriting my whole OS I'm stuck on the FAT driver again. It works with files up to 512 but after that I need to read the FAT for the next cluster. My problem is that no matter what code I use it returns 0 for 198(start cluster of my test file) then 2548 for 0; then 0 again. Maybe I'm reading it in wrong? Below is my code: the read function works and 0 is sector 0; 512 sector 1 etc. after a read the location increases by the size specified (3rd argument)

Code: Select all

   void *fat;
   seek(substream, (boot->num_reserved - 1) * 512);
   fat = malloc(boot->spf * boot->bps);
   for(i = 0; i < boot->spf; i++)
   {
      read(substream, (unsigned char *)(fat + (i * boot->bps)), 512);
   }

Code: Select all

short nextclust(int index, void *fat)
{
   short tmp;
   tmp = *(short *)(fat + index + (index / 2));
   if(index & 1) {
     tmp >>= 4;
   }
   return tmp & 0xfff;
}
Thanks in advance
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:FAT Driver

Post by Pype.Clicker »

did you checked the FAT was read correctly (i suggest you implement a small HEXDUMP feature and check your memory image of the FAT is what you expected)? what kind of FAT is it ? 12 , 16 or 32. If 12, did you check the bytes were read correctly ?

from a 16-bits word, you could have

let's consider the fat as a list of 3-bytes entries, given cluster nr X, the entry will be X/4.

Code: Select all

struct fat_entry { unsigned short data[3]};
struct fat_entry FAT[fat_size];

[X X X y] [y y Z Z] [Z u u u]
entry=&(FAT[index/4])
X.index % 4 = 0 --> val = entry[0]&0xfff;
y.index % 4 = 1 --> val = (entry[0]>>12) | ((entry[1]<<4)&0xfff);
etc.

now it looks like your code does something very similar ... maybe i could need some more coffee ...

However, i'm wondering if using "unsigned short" rather than "short" couldn't help you ... i remember it's hard to know whether x>>N will be sign-extended or not in C with signed numbers ... maybe it just doesn't care because of that "&0xFFF" operation. I think it could be wiser and cleaner not to do bit operations on signed numbers anyway ...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:FAT Driver

Post by Pype.Clicker »

oh, just another thing ... did you checked your clusters were 512bytes long ? i can't remember what is the cluster size on 1.44MB disks, but i wonder if it wasn't 1KB ...
Peter_Vigren

Re:FAT Driver

Post by Peter_Vigren »

Pype.Clicker wrote: oh, just another thing ... did you checked your clusters were 512bytes long ? i can't remember what is the cluster size on 1.44MB disks, but i wonder if it wasn't 1KB ...
The cluster size is 512 bytes.

There are 2880 sectors on a 1.44 Mb floppy. 2880 fits into a 12-bit number so you didn't have to make the clusters bigger to adress them all...
Therx

Re:FAT Driver

Post by Therx »

Ok changed the short variable tmp to unsigned short. Now it returns a value but it is incorrect because when I read from the returned sector I get rubbish and if I defrag the disk the clusters for a file will be next to each other but they're not :'(
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:FAT Driver

Post by Pype.Clicker »

you should really check your read sectors function and check it behaves as it should.
Therx

Re:FAT Driver

Post by Therx »

I know it works because it loads the Boot sector table fine, the root directory fine the first sector of each file fine.
Post Reply