FAT Library

I’m using a Logomatic V2 board and I’ve been writing my own firmware for some time now.

I’ve been using the Roland Riegel Library but it does not have an append option when you download it. I’ve used another firmware which does implement the append fuction however it only appends to the first cluster in the file.

When it leaves the first cluster and writes to the next, the subsequent write starts over again in the first cluster.

I’m looking for help in developing a check system for the write function to know which cluster it should be writing into.

If anyone has had success in playing around with the clusters in FAT libraries I would definately appreciate some help.

The current append function looks like this;

struct fat16_file_struct* open_file_in_dir_append(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name)

{

struct fat16_file_struct* result;

struct fat16_dir_entry_struct file_entry;

if(!find_file_in_dir(fs, dd, name, &file_entry))

return 0;

result = fat16_open_file(fs, &file_entry);

result->pos = result->dir_entry.file_size; // Find position in cluster

result->pos_cluster = result->dir_entry.cluster; // Doesn’t work??? but my hopes were to find the new cluster.

}

timpower:
result->pos = result->dir_entry.file_size; // Find position in cluster

result->pos_cluster = result->dir_entry.cluster; // Doesn’t work??? but my hopes were to find the new cluster.

What is stored in the directory entry is the first allocated cluster. In order to find the last cluster you must walk the cluster chain. This isn’t very hard, just tedious.

Use the first cluster number as an index into the FAT. You will find another cluster number at this index. If it is the last cluster in the chain it will have a value of 0xfff8 or greater. (FAT16) Otherwise it points to the next cluster in the chain. Repeat.

Then you get to mess with the fiddly details. Compute the offset within the cluster (file_size%CLUSTERSIZE). If this doesn’t fall on a sector boundary then you should read the last sector so you don’t lose that data when you start writing.