huge work on FAT32, initial support for writing

This commit is contained in:
2021-02-14 21:35:47 +01:00
parent 6aa951190e
commit da12992ae8
14 changed files with 609 additions and 91 deletions

View File

@@ -6,7 +6,23 @@
struct TestDevice {
uint8_t buf[4096];
size_t size;
uint8_t* buf;
TestDevice(size_t size) : size(size) {
buf = (uint8_t*) malloc(size);
memset(buf, 0, size);
}
~TestDevice() {
free(buf);
}
void toFile(const std::string& name) {
FILE* f = fopen(name.c_str(), "w+");
fwrite(buf, size, 1, f);
fclose(f);
}
/** read a 512 byte block into dst */
bool readSingleBlock(LBA512 addr, uint8_t* dst) {
@@ -14,7 +30,7 @@ struct TestDevice {
return true;
}
bool writeSingleBlock(LBA512 addr, uint8_t* src) {
bool writeSingleBlock(LBA512 addr, const uint8_t* src) {
memcpy(buf+addr*512, src, 512);
return true;
}

View File

@@ -5,12 +5,12 @@
#include "../AccessHelper.h"
/*
TEST (TestAccessHelper, read) {
// read varying numbers of bytes at arbitrary locations
TestDevice dev;
TestDevice dev(4096);
AccessHelper<TestDevice> ah(dev);
for (size_t i = 0; i < sizeof(dev.buf); ++i) {dev.buf[i] = i;}
@@ -33,14 +33,14 @@ TEST (TestAccessHelper, read) {
}
}
*/
TEST (TestAccessHelper, write) {
// write varying numbers of bytes at arbitrary locations
const uint8_t MAGIC = 0xFF;
TestDevice dev;
TestDevice dev(4096);
AccessHelper<TestDevice> ah(dev);
// src data

View File

@@ -0,0 +1,50 @@
#include <gtest/gtest.h>
#include "Helper.h"
#include "../AccessHelper.h"
#include "../fat32/FS.h"
// dd if=/dev/zero of=/tmp/ram/orig.fat32 bs=8192 count=4096
// mkfs.vfat -F 32 -R 32 -s 8 orig.fat32
// hexdump -v -C -n 512 orig.fat32
TEST(TestCreate, structure) {
FAT32::FSHeader header;
ASSERT_EQ(0x0B, (uint8_t*)&header.bytesPerSector - (uint8_t*)&header);
ASSERT_EQ(0x0D, (uint8_t*)&header.sectorsPerCluster - (uint8_t*)&header);
ASSERT_EQ(0x0E, (uint8_t*)&header.numReservedSectors - (uint8_t*)&header);
ASSERT_EQ(0x10, (uint8_t*)&header.numberOfFATs - (uint8_t*)&header);
ASSERT_EQ(0x15, (uint8_t*)&header.mediaDescriptor - (uint8_t*)&header);
ASSERT_EQ(0x20, (uint8_t*)&header.sectorsInPartition - (uint8_t*)&header);
ASSERT_EQ(0x24, (uint8_t*)&header.sectorsPerFAT - (uint8_t*)&header);
ASSERT_EQ(0x2C, (uint8_t*)&header.rootDirFirstCluster - (uint8_t*)&header);
}
TEST (TestCreate, write) {
using BlockDev = AccessHelper<TestDevice>;
size_t size = 32*1024*1024;
TestDevice dev(size);
BlockDev bDev(dev);
FAT32::FS<BlockDev> fs(bDev, 0);
fs.setup(size);
const char* test = "This is a test";
FAT32::FS<BlockDev>::File2 f1 = fs.create2("test1.txt");
f1.write(strlen(test), (const uint8_t*)test, [](int percent) {});
FAT32::FS<BlockDev>::File2 f2 = fs.create2("test2.txt");
f2.write(strlen(test), (const uint8_t*)test, [](int percent) {});
dev.toFile("/tmp/ram/1.fat32");
// mount -t vfat 1.fat32 /mnt/fat/ && ls -l /mnt/fat/ && cat /mnt/fat/test1.txt && umount /mnt/fat
}