Question

I'm working on a low-level application which uses a FAT16 file system structure on a resource-constrained microcontroller which necessitates that I write my own custom access code. I've already looked into using libraries like Petit FAT and FatFS but unfortunately I don't think either of these will suite my needs. They have served as useful references for how FAT works, though.

The one area that I'm still having troubles with is sub-directory entries.

According to this, a directory entry is able to point to 1 start cluster. For a data file, this is simply the first data cluster. For a directory, this is the starting cluster of the sub-directory (presumably another directory entry).

This works fine if there's only 1 directory path from a root directory down to the base file, but I don't understand how this allows you to branch out to multiple files/directories underneath any given directory.

ex. directory structure:

- root dir 1
    - sub dir 1
        - file 1
    - sub dir 2
        - file 2
- root dir 2
    - sub dir 3

Based on my understanding of the FAT16 structure,

Immediately following the FATs will be the cluster for the first root directory entry, containing information for root dir 1. The first cluster field would then contain the cluster address for sub dir 1, who's first cluster field would contain the cluster address for file 1, who's first cluster points to a data cluster.

The second root directory entry would then start at the second cluster after then end of the FATs containing information for root dir 2. Its first cluster would point to the cluster for sub dir 3, who's first cluster would point to an empty cluster (as marked in the FATs).

What am I missing here? I can't figure out a way to navigate from a root directory entry down to sub dir 2.

Était-ce utile?

La solution

First, to clear the confusion, one directory entry does not occupy a single cluster. As you can see from your source, a single directory entry is just 32 bytes, where the size of cluster can be something like between 4KB to 64KB, depends on the disk size/version of FAT you're using. So, what comes after FAT is actually a table of directory entries.

For subdirectory, you can find the starting cluster of its directory entry table in the parent directory entry. They do not necessarily be packed to the beginning of the disk.

To explain further, let's look at a slightly modified version of your original example:

\ (root)
    - dir 1
        - sub dir 1
            - file 1
        - sub dir 2
            - file 2
    - dir 2
        - sub dir 3

To traverse to sub dir 2:

  1. Locate the root directory entry table, which contains in the cluster right after the FAT;
  2. Look for the entry dir 1 from the root directory entry table, and get its starting cluster;
  3. Load the directory entry table for dir 1 from the cluster you've obtained above;
  4. Look for the entry sub dir 2 and get its starting cluster;
  5. Load the cluster obtained above;

Then you should now have the directory entry table for sub dir 2.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top