fat12 simple question

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
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

fat12 simple question

Post by kestaz »

Hi all.

I am trying to write fat12 img reader, but i have problem.

I read http://www.osdev.org/osfaq2/index.php/FAT12%20document FAT12 documentation, but i can't figure out how directories and subdirectories are organized in fat12 file system .. maybe i misted something ?

For example simpe directories tree:

root
/ \
foo bar
|
test.txt

and the Directory structure :

Bytes Meaning
0 - 10 File name with extension
11 Attributes of the file
12 - 21 Reserved Bytes
22 - 23 Indicate the time
24 - 25 Indicate the date
26 - 27 Indicate the entry cluster value
28 - 31 Indicate the size of the file

And can't see any pointers that will show how the directories tree is created ?

How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..


kestaz
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: fat12 simple question

Post by skyking »

kestaz wrote:Hi all.
and the Directory structure :

Bytes Meaning
0 - 10 File name with extension
11 Attributes of the file
12 - 21 Reserved Bytes
22 - 23 Indicate the time
24 - 25 Indicate the date
26 - 27 Indicate the entry cluster value
28 - 31 Indicate the size of the file

And can't see any pointers that will show how the directories tree is created ?

How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..

kestaz
Simple it doesn't. The knowledge is that the directory entry knows what type of file each entry corresponds to (the directory knows it contents, but the contents does not know which directory it belongs to). If the subdirectory bit is set (0x10) the directory entry corresponds to a directory (and the it's size is determined via the FAT).

In your case the root directory contains two entries (foo and bar), foo having subdir bit set. Folowing the entry cluster you find it's contents - clusters containing directory entries, one of them being test.txt.

Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: fat12 simple question

Post by jal »

skyking wrote:Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.
However, totally unimportant for FAT, since it doesn't have hard links. In fact, subdirectories actually have the "." and ".." as part of the directory contents (unlike Unix file systems, which rely on the shell to interpret these), and ".." actually links to the parent directory.


JAL
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: fat12 simple question

Post by jal »

kestaz wrote:How foo subdirectory will know that she belongs to root directory ? How the file test.txt will now that he belongs to foo subdirectory .. and etc ..
Directories and files do not know anything about their location. I thinks there are very little file systems that store this knowledge together with the file. For FAT, all file information is stored in the directory 'file' (as opposed to e.g. the Unix-like file systems where the file information is stored in a global structure called 'i-node'). To build a complete directory tree, you start with the root directory. In the root directory, there are entries that indicate they are subdirectories. Read these subdirectories to discover their contents, etc.


JAL
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Post by kestaz »

no i can't figure out .. how directory entry can point to many list of files ? how it's possible ?
User avatar
hailstorm
Member
Member
Posts: 110
Joined: Wed Nov 02, 2005 12:00 am
Location: The Netherlands

Post by hailstorm »

Skyking allready gave you the answer: Bits 26 and 27 gives you the starting cluster of the directory.
B.t.w. The directory is just a file, but operating systems tend to use the directory for another purpose... :wink:

You have to parse the fat-chain for the directory and read the cluster that belongs to that fat entry.

All files and directories don't have any clue of their parent whatsoever. A file doesn't know to which directory it belongs and this is also true for a subdirectory. Subdirectories however, have 2 entries: dot (.) and dotdot (..). These entries point to itself and its parent respectively. That is, byte 26 and 27 contain the starting cluster.

Hope this helps.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

kestaz wrote:no i can't figure out .. how directory entry can point to many list of files ? how it's possible ?
A directory is the same as a regular file, as far as the FAT file system is concerned. That means that it occupies disk sectors that you can read. A directory contains entries describing files that are 'inside' that directory. A directory is an array of 32-byte structures, each structure describing one file. The attributes inside the structure tell you whether it is a regular file or a directory (or a volume label, but you can ignore that one).

Perhaps you should read and try to understand the official FAT specification that you can find here. It's all in there.


JAL
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: fat12 simple question

Post by skyking »

jal wrote:
skyking wrote:Having the files (and directories) not knowing which directory they belong to is important when implementing hard links since then the files can belong to multiple directories.
However, totally unimportant for FAT, since it doesn't have hard links. In fact, subdirectories actually have the "." and ".." as part of the directory contents (unlike Unix file systems, which rely on the shell to interpret these), and ".." actually links to the parent directory.


JAL
I meant to design a VFS to rely on files to know where they are located is a bad idea for that reason (not that it matters much for FAT12).
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: fat12 simple question

Post by bewing »

jal wrote: "." and ".." ... Unix file systems, which rely on the shell to interpret these ...

JAL
Perhaps the Linuxes use shell interpretation for . and .., but original Unix always used hardlinks.
kestaz
Posts: 11
Joined: Thu Jan 10, 2008 6:09 pm

Post by kestaz »

thanks everybody for replies. Still don't understand ;) So I am wrote some code to make ROOT STRUCTURE dump ..

You can see it at http://www.pastebin.ca/851490

ok i will try to explain what i don't understand

Name: UNTITLED 2 (
Ext: 2 (
starting_sector: 0

Ok everything clear at this point.. I made image of usb flash .. so it was named UNTITLED ..

Name: LABAS TXT !¨π)8)8
Ext: TXT !¨π)8)8
starting_sector: 23

And there's file.. And i see it's points to 23 .. OK .. it's points to FAT

Name: LABAS -wπ)8)8
Attribute: directory
Ext: -wπ)8)8
starting_sector: 22

Ok there's directory .. hmm but what 22 shows ? Where this pointer points ? To the FAT ? TO the ROOT DIRECTORY STRUCTURE ? I still i don't understand ...

OK .. As you said there's ".." , "." but i can't see them ..

I see mane junk in my dump .. How to avoid it ?

You can see my code: http://www.pastebin.ca/851504 hope it helps..

any help will be appropriate
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Post by jal »

kestaz wrote:Ok there's directory .. hmm but what 22 shows ? Where this pointer points ? To the FAT ? TO the ROOT DIRECTORY STRUCTURE ? I still i don't understand ...
Please, PLEASE, PLEASE read the offical FAT specification I linked to somewhere earlier in the thread. It is really all in there. If you don't understand details of it, fine, let's discuss those, but it seems you just want us to provide all the answers that you can easily find yourself!
OK .. As you said there's ".." , "." but i can't see them ..
No, they are not in the root directory, as mentioned.
I see mane junk in my dump .. How to avoid it ?
The strings aren't zero-terminated, so you cannot use %s without specifying a width when printing. And that's something else easily available from the specification. Use %.8s and %.3s instead!
any help will be appropriate
I (and no doubt many others) are more than willing to help, but like I said, please check the documentation first before asking questions, as we are not here to spell it out for you.


JAL
Post Reply