FAT16 catalogs

I am working on a low-level application that uses the FAT16 file system structure on a resource-limited microcontroller, which requires me to write my own user access code. I have already studied the use of libraries such as Petit FAT and FatFS, but, unfortunately, I do not think that any of them will satisfy my needs. They have provided useful links to how FAT works.

In one area in which I still have problems, there are entries in a subdirectory.

According to this , a directory entry can point to 1 start cluster. For a data file, this is simply the first data cluster. For a directory, this is the start cluster of a subdirectory (presumably a different entry in the directory).

This works fine if there is only one directory path from the root directory to the base file, but I don’t understand how it allows branching into several files / directories under 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 structure of FAT16,

Immediately after the FAT, there will be a cluster for the first root directory entry containing information for root dir 1 . The first cluster field will contain the cluster address for sub dir 1 , in which the first cluster field will contain the cluster address for file 1 , which indicates the data cluster by the first cluster.

The second root directory entry will then begin in the second cluster after the end of the FAT containing information for root dir 2 . Its first cluster will point to the cluster for sub dir 3 , which will point to the empty cluster as the first cluster (as noted in the FAT).

What am I missing here? I cannot find a way to go from root directory entry to sub dir 2 .

+4
source share
1 answer

First, to eliminate confusion, a single directory entry does not occupy a single cluster. As you can see from your source, one entry in the directory is only 32 bytes, where the cluster size can range from about 4 KB to 64 KB, depending on the size of the drive / version of the FAT used. So what happens after the FAT is actually a table of entries in the directory.

In the subdirectory, you can find the start cluster of your directory entry table in the parent directory entry. They do not have to be packed at the beginning of the disc.

To explain further, consider 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 go to sub dir 2 :

  • Find the root directory entry table that is in the cluster immediately after the FAT;
  • Find the dir 1 entry from the root directory entry table and get its initial cluster;
  • Download the directory entry table for dir 1 from the cluster above;
  • Find the sub dir 2 entry and get its initial cluster;
  • Download the resulting cluster,

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

+3
source

Source: https://habr.com/ru/post/1435443/


All Articles