fat_io_lib finally works, implement virtual partition devices, manage devices via string keys
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m35s

This commit is contained in:
2026-03-01 00:00:27 +01:00
parent baa13fb695
commit 0533c2705d
37 changed files with 619 additions and 139 deletions

View File

@@ -1,4 +1,5 @@
#include <desc.h>
#include <devices.h>
#include <fs/fat1.h>
#include <fs/fatfs.h>
#include <fs/fatfs_ctx.h>
@@ -13,13 +14,15 @@
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
uint32_t sector_count) {
struct vfs_volume* volume = ctx->udata;
struct device* back_device = volume->back_device;
if (sector_count == 0)
return 0;
size_t sector_size;
size_t phys_sector, phys_sector_count;
int ret;
@@ -29,7 +32,7 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
return 0;
}
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
@@ -38,33 +41,97 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
ret = device_op (back_device, XDRV_READ, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
return 0;
}
spin_unlock (&back_device->lock);
if (ret < 0)
return -1;
return 0;
return 0;
return 1;
}
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
(void)proc, (void)rctx;
static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
uint32_t sector_count) {
struct vfs_volume* volume = ctx->udata;
struct device* back_device = volume->back_device;
if (sector_count == 0)
return 0;
size_t sector_size;
size_t phys_sector, phys_sector_count;
int ret;
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
}
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
&phys_sector_count);
ret = device_op (back_device, XDRV_WRITE, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
}
spin_unlock (&back_device->lock);
if (ret < 0)
return 0;
return 1;
}
int fatfs_mount (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = malloc (sizeof (*fatfs_ctx));
int r;
if (fatfs_ctx == NULL)
return -ST_OOM_ERROR;
memset (fatfs_ctx, 0, sizeof (*fatfs_ctx));
fl_attach_media (fatfs_ctx, &fat1_diskio_read, NULL);
fatfs_ctx->udata = volume;
volume->udata = fatfs_ctx;
fl_init (fatfs_ctx);
fl_attach_media (fatfs_ctx, &fat1_diskio_read, &fat1_diskio_write);
return ST_OK;
}
int fatfs16_format (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
int r = fatfs_format_fat16 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat16");
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs32_format (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
int r = fatfs_format_fat32 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat32");
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
@@ -114,6 +181,23 @@ int fatfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, si
return ST_OK;
}
int fatfs_write (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+");
if (file == NULL)
return -ST_NOT_FOUND;
fl_fseek (fatfs_ctx, file, off, SEEK_SET);
fl_fwrite (fatfs_ctx, buffer, 1, size, file);
fl_fclose (fatfs_ctx, file);
return ST_OK;
}
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num) {
struct fatfs_ctx* fatfs_ctx = volume->udata;