Page 1 of 1

FAT12 Question

Posted: Wed Aug 17, 2005 11:00 pm
by stafe
Hello,

Recently I start a topic about a floppy driver in C ... now the FDC works fine ... i can read every sector from the floppy disk ...

I have also written a function that show me the root dir.

But how I can found out the sector where the File is saved. ( example: if I want to load a file (test.txt -> TEST TXT) how I found out where de File is saved? )

Can somebody help me please ..

Re: FAT12 Question

Posted: Wed Aug 17, 2005 11:00 pm
by smiddy
Here is a copy of the specification: http://www.osdever.net/documents/fatgen ... ?the_id=40

Essentially you need to do the following:

1) read the FAT save to a buffer
2) read in the boot directory into another buffer
3) find the file or director from the root directory entries
4) If found, get the cluster information from the file name entry in root directory
5) Check the FAT for the cluster information for either next cluster or end of file marking
6) convert cluster to sector then to cylinder/head/side, then read each sector
7) if cluster isn't the end of file, go to next cluster in chain do #5 and #6 again, if end of file, then you are done.

Re: FAT12 Question

Posted: Wed Aug 17, 2005 11:00 pm
by stafe
Thank You smiddy ...

I have written a C funcion that shows me the Root-Dir ... every root entry is 32 Bytes ... in a document about FAT12 I've read that I have to add 26 to the 32 Bytes to get the sector of this file:

example (it shows me the first root entry "root" is the root-dir ):


..
..
for( i=0;i<11;i++ )
{
printf( root[32+i] );
}
..
..

if i want to show the sector of the first file...

..
..
printf( root[32+26] );
..
..


... it doesn't work ...

sorry for my extrem bad english
i hope somebody can help me ...

Re: FAT12 Question

Posted: Wed Aug 17, 2005 11:00 pm
by smiddy
Your English is fine, don't sweat that...the more you use the better you'll get.

Now about your question. You are correct, the directory entry is 32 bytes long. The offset from zero to the cluster number is 26. The cluster is what you look up in the fat.

However, I'm not a C person, but I don't beleive your array is correct, or how to print it. Someone else will have to help you on the syntax of C.

Re: FAT12 Question

Posted: Thu Aug 18, 2005 11:00 pm
by stafe
I print it because to see the sector where the file is saved ...

But it doesn't work, because it shows me from every file the self sector ( 3 )

Example:

printf( root[32+26] ); // File1
printf( root[64+26] ); // File2
printf( root[96+26] ); // File3

It prints always the same result. ( 3 )
But I try this same in my bootloader and it works fine ...

Re: FAT12 Question

Posted: Thu Aug 18, 2005 11:00 pm
by smiddy
I assume that root[?] is the entire root directory. From what you have here I assume that you are starting at the second entry root[0 + 26] = the byte of the first entries cluster number, which could be why you're getting the 3 (don't know, I'm guessing). In (FASM) ASM it would be like so:

BTW, it is convention to use the [ code ] C, or ASM code here [ / code ] <- End of code.

Code: Select all

. . .
    MOV DX, WORD [DI + 1Ah]         ; Gets cluster from root directory entry
    MOV WORD [Cluster], DX          ; Save cluster from directory entry
. . .
Since I specify WORD, then a two byte number is placed into the DX register. From what you've written (printf(root[32 + 26]); ) I get the impression that it will just print a byte. You may want to specify an interger and save root[32 + 26] to it then print it, something like this:

Code: Select all

   unsigned int cluster = 0;  // Assumes two byte interger

   cluster = root[32 + 26];  // This I assume will work, I don't know, I'm not a C person

   printf("%u",cluster);  // Print the cluster number
Assuming my fragment works, it shoudl print out the cluster number.

Re: FAT12 Question

Posted: Fri Aug 19, 2005 11:00 pm
by stafe
Thank You smiddy , thank you very much ...

my problem was that i use a unsigned char ... and not a unsigned int ...

now I've written a function likes fopen in c ...
it search for a file and load it into the memory ...

Re: FAT12 Question

Posted: Fri Aug 19, 2005 11:00 pm
by smiddy
Not a problem, glad to have helped. I check this forum nearly daily, but I will be moving across country soon and won't have much time to check the forum. If you have more questions, like how to determine 12-bit cluster information from 16-bit entries and converting LBA to CHS, I may not get to them right away, but I suspect someone else may be able to help you.