FAT32: support directory entries larger than 1 sector

There was already a support for partitions with bigger cluster sizes but there was also a bug where the directory loading code would only look  at the first sector of each cluster and could incorrectly report some files as non-existing.
This commit is contained in:
KrekBuk 2020-05-02 15:47:29 +02:00
parent 6175e7280b
commit b842720a70
2 changed files with 73 additions and 39 deletions

View File

@ -62,3 +62,35 @@ fat32-test: all
rm -rf test_image loopback_dev
./qloader2-install src/qloader2.bin test.img 2048
qemu-system-x86_64 -hda test.img -debugcon stdio
fat32-test-big-clusters: all
$(MAKE) -C test
rm -rf test.img test_image/
mkdir test_image
# Setup partitions
dd if=/dev/zero bs=1M count=0 seek=512 of=test.img
parted -s test.img mklabel gpt
parted -s test.img mkpart primary 2048s 6143s
parted -s test.img mkpart primary 6144s 1042432s
sudo losetup -Pf --show test.img > loopback_dev
sudo partprobe `cat loopback_dev`
sudo mkfs.fat -s 8 -F 32 `cat loopback_dev`p2
sudo mount `cat loopback_dev`p2 test_image
sudo mkdir test_image/boot
# Copy some random files to fill up the fats to span multiple clusters.
# This will make the test more credible.
sudo find ./src -type f -exec cp -n {} test_image/ \;
sudo find ./src -type f -exec cp -n {} test_image/boot/ \;
# Copy the actual important files
sudo cp test/test.elf test_image/boot/
sudo cp test/qloader2.cfg test_image/
sync
sudo umount test_image/
sudo losetup -d `cat loopback_dev`
rm -rf test_image loopback_dev
./qloader2-install src/qloader2.bin test.img 2048
qemu-system-x86_64 -hda test.img -debugcon stdio

View File

@ -128,8 +128,9 @@ static int fat32_open_in(struct fat32_context* context, struct fat32_directory_e
bool has_lfn = false;
do {
for (size_t sector_in_cluster = 0; sector_in_cluster < context->sectors_per_cluster; sector_in_cluster++) {
struct fat32_directory_entry directory_entries[FAT32_SECTOR_SIZE / sizeof(struct fat32_directory_entry)];
error = fat32_load_fat_cluster_to_memory(context, current_cluster_number, directory_entries, 0, sizeof(directory_entries));
error = fat32_load_fat_cluster_to_memory(context, current_cluster_number, directory_entries, 0 * FAT32_SECTOR_SIZE, sizeof(directory_entries));
if (error != 0) {
return error;
@ -181,6 +182,7 @@ static int fat32_open_in(struct fat32_context* context, struct fat32_directory_e
has_lfn = false;
}
}
}
error = fat32_read_cluster_from_map(context, current_cluster_number, &current_cluster_number);