In the last week’s part 1 of my FAT and SD tutorial, we got as far as reading the file entries in root directory, and peeking into a file with hex editor. Now we’ll cover the file allocation table itself to enable reading longer files, and adapt the code into a small footprint FAT16 library!
File allocation table in FAT16
In the previous part, we learned that the data on a FAT-formatted disk is stored in clusters. In our test image, the cluster size was 32 sectors, i.e. 16 kiB (16 384 bytes). Let’s imagine a newly formatted disk with 100 free clusters, with the clusters numbered from 2 (the first cluster at the beginning of data area) to 101 (the very last cluster). Now let’s copy some files there:
|File operation||File allocation|
|Copy HAMLET.TXT (193 082 bytes) to disk||Clusters 2-13 allocated for the file (196 608 bytes)|
|Copy README.TXT (353 bytes) to disk||Cluster 14 allocated for the file|
|Create SUBDIR on disk||Cluster 15 allocated for the directory file entries|
|Create 1.TXT in SUBDIR||Cluster 16 allocated for the file, a new file entry written to SUBDIR (beginning of cluster 15)|