简体   繁体   中英

how FAT let me know what to load next cluster hint by file's first cluster?

I am reading brokenthorn's OS Dev tutorial

http://www.brokenthorn.com/Resources/OSDev6.html

I stuck at this for several days.;

to load "stage2.sys",

search root directory "stage2 sys".

root directory contains file name and starting cluster address.(ie 0x002)

load 0x002th cluster and lookup FAT for what cluster to load next.

FAT consist of 12bit array and

array size is 4077

for now, I don't know how to FAT let me know what to load next hint by file's first cluster.

the question is this.

  1. are there any missing or wrong point?

  2. how FAT let me know what to load next cluster hint by file's first cluster?

FAT consist of just 12bit array.. how?

thanks.

The FAT cluster list is simply an array, with the entries in the array being the next index in the file's chain of clusters. The file entry in the directory tells you the location of the first cluster (in the example, cluster 0x002). To find the next cluster, you look at index 0x002 in the FAT array:

next_cluster = FAT[0x002];

For example, if FAT[0x002] held 0x014, then that's the cluster that would hold the next set of data for the file, then you'd look in FAT[0x014] for the 3rd cluster, etc.

If you're dealing with a 12 bit FAT then that means you have a bit of complexity in indexing the array. I think the indexing goes something like

// find the 16-bit word that contains the 12-bit FAT entry
uint16_t tmp = ((uint16_t*)(((char*)FAT) + index + (index / 2)));

// keep either the high 12-bits or low 12-bits depending on if the 
// index is even or odd.
next_cluster = (tmp >> ((index % 2) ? 4 : 0)) & 0x0fff;

Of course, this is based on an in-memory FAT. If the FAT is being read from media you'd need to convert the appropriately to a sector to read from (and an offset into the sector).

The directory entry for a file contains the first cluster number. You can use that to get the actual cluster from the "data" portion of the disk, plus the next cluster number from the FAT.

An entry in the FAT contains the cluster number of the next cluster in a file.

So you would have a directory entry which might indicate the first cluster number is 22. You would then go and get the data for cluster 22 and also look up the FAT entry for cluster number 22 - let's say that held 76.

So, the next cluster in the file is number 76 and you follow the same process: get the data and also look up the next cluster number in FAT entry 76.

Special values for the next cluster number indicated that the chain was finished and no more were available. I think (from memory), this was 0xFFF for FAT12 but it may have been a range of high values.

The FAT is basically a linked list. If the beginning of the file is in cluster 2, then to find the next cluster, you look at entry 2 in the FAT. That will contain the cluster number of the next cluster for that file. You follow those through until you get to one that contains a number larger than (going from memory) 0ff8h, which marks the end of the file.

As an aside, unless you're really set on working with a 12-bit FAT, 16-bit FATs are considerably easier to deal with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM