Working port of Little FS

This commit is contained in:
2025-08-16 20:35:00 +02:00
parent 2b0566c56f
commit 8da890e388
20 changed files with 332 additions and 19 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/limine
/iso_root
/base_root
*.iso
*.img

View File

@ -17,16 +17,17 @@ prepare:
cleanall:
make clean
rm -rf limine
rm -rf littlefs-fuse
clean:
make -C kernel clean
rm -f mop2.iso base.img
base:
dd if=/dev/zero of=base.img bs=1M count=10
mformat -F -i base.img ::
mcopy -i base.img Makefile ::
mdir -i base.img ::
@rm -f base.img
@rm -rf base_root
@mkdir -p base_root
mklittlefs -c base_root -b 4096 -s $$((1<<20)) base.img
iso:
@rm -rf iso_root

View File

@ -10,7 +10,6 @@ CFLAGS += -I. \
-I./std/include \
-I./flanterm/src \
-DPRINTF_INCLUDE_CONFIG_H=1 \
-DLFS_NO_MALLOC \
-DLFS_NO_ASSERT \
-DLFS_NO_DEBUG \
-DLFS_NO_WARN \
@ -41,6 +40,7 @@ SRCFILES := $(wildcard *.c) \
$(wildcard util/*.c) \
$(wildcard fs/kvfs/*.c) \
$(wildcard fs/littlefs/*.c) \
$(wildcard fs/portlfs/*.c) \
$(wildcard baseimg/*.c) \
$(wildcard hal/*.c) \
$(wildcard hal/$(ARCH)/*.c) \

View File

@ -7,9 +7,18 @@
#include "util/util.h"
#include "hal/hal.h"
struct limine_file *baseimg = NULL;
uint64_t baseimg_getaddr(void) {
return (uint64_t)baseimg->address;
}
uint64_t baseimg_getsize(void) {
return baseimg->size;
}
void baseimg_init(void) {
LOG("baseimg", "looking for base image...\n");
struct limine_file *baseimg = NULL;
for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) {
struct limine_file *module = BOOT_INFO.modules->modules[i];
if (hal_strcmp(util_get_filename(module->path), "base.img") == 0) {
@ -23,5 +32,12 @@ void baseimg_init(void) {
hal_hang();
} else {
LOG("baseimg", "base.img found\n");
LOG("baseimg", "addr = %p, size = %lld\n", baseimg->address, baseimg->size);
for (size_t i = 0; i < 30; i++) {
kprintf("%02X ", ((uint8_t *)(baseimg->address))[i]);
if (i > 0 && (i + 1) % 10 == 0) {
kprintf("\n");
}
}
}
}

View File

@ -2,5 +2,7 @@
#define BASEIMG_BASEIMG_H_
void baseimg_init(void);
uint64_t baseimg_getaddr(void);
uint64_t baseimg_getsize(void);
#endif // BASEIMG_BASEIMG_H_

View File

@ -7,5 +7,7 @@
#define E_NOENTRY -3
#define E_OUTOFBOUNDS -4
#define E_UNKNOWN_SDTYPE -5
#define E_TODO -6
#define E_GENERIC_ERROR -7
#endif // ERRORS_H_

View File

@ -32,14 +32,10 @@ int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *co
spinlock_acquire(&vmp->spinlock);
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
if (node == NULL) {
HSHTB_ALLOC(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
if (node == NULL) {
spinlock_release(&vmp->spinlock);
return E_NOMEMORY;
}
}
spinlock_release(&vmp->spinlock);
if (node == NULL) {
return E_NOENTRY;
}
spinlock_acquire(&node->spinlock);
vmp->backingsd->write(vmp->backingsd, buffer, n, off);
@ -72,9 +68,26 @@ int32_t kvfs_cleanup(struct VfsMountPoint *vmp) {
return E_OK;
}
int32_t kvfs_create(struct VfsMountPoint *vmp, const char *path, int32_t type) {
(void)type;
KvfsNode *node = NULL;
spinlock_acquire(&vmp->spinlock);
HSHTB_ALLOC(&vmp->fs.kvfs, nodes, (char *)path, key_, node);
spinlock_release(&vmp->spinlock);
if (node == NULL) {
return E_NOMEMORY;
}
return E_OK;
}
bool kvfs_check(void) {
int32_t ret;
ret = vfs_create("tmpvars", "hello", VFS_CREATE_FILE);
if (ret != E_OK) return false;
char *hello = "WAWAWAWA!!!";
ret = vfs_write("tmpvars", "hello", hello, hal_strlen(hello)+1, 0);
if (ret != E_OK) return false;

View File

@ -1,6 +1,7 @@
#ifndef FS_KVFS_KVFS_H_
#define FS_KVFS_KVFS_H_
#include <stdint.h>
#include <stdbool.h>
struct VfsMountPoint;
@ -22,6 +23,7 @@ typedef struct {
int32_t kvfs_read(struct VfsMountPoint *vmp, const char *key, uint8_t *const buffer, size_t n, size_t off);
int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n, size_t off);
int32_t kvfs_remove(struct VfsMountPoint *vmp, const char *key);
int32_t kvfs_create(struct VfsMountPoint *vmp, const char *path, int32_t type);
int32_t kvfs_cleanup(struct VfsMountPoint *vmp);
bool kvfs_check(void);

121
kernel/fs/portlfs/portlfs.c Normal file
View File

@ -0,0 +1,121 @@
#include <stdint.h>
#include <stdbool.h>
#include "fs/littlefs/lfs.h"
#include "vfs/vfs.h"
#include "errors.h"
#include "kprintf.h"
#include "dlmalloc/malloc.h"
#define CHECK(err) \
do { \
int ok = (err); \
kprintf("ok = %d\n", ok); \
if (ok < 0) goto bad; \
} while(0)
int32_t littlefs_read(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off) {
spinlock_acquire(&vmp->spinlock);
LittleFs *fs = &vmp->fs.littlefs;
lfs_file_t file;
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_RDONLY));
CHECK(lfs_file_seek(&fs->instance, &file, off, LFS_SEEK_SET));
CHECK(lfs_file_read(&fs->instance, &file, buffer + off, n));
CHECK(lfs_file_close(&fs->instance, &file));
spinlock_release(&vmp->spinlock);
return E_OK;
bad:
spinlock_release(&vmp->spinlock);
return E_GENERIC_ERROR;
}
int32_t littlefs_write(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off) {
spinlock_acquire(&vmp->spinlock);
LittleFs *fs = &vmp->fs.littlefs;
lfs_file_t file;
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_WRONLY));
CHECK(lfs_file_seek(&fs->instance, &file, off, LFS_SEEK_SET));
CHECK(lfs_file_write(&fs->instance, &file, buffer, n));
CHECK(lfs_file_close(&fs->instance, &file));
spinlock_release(&vmp->spinlock);
return E_OK;
bad:
spinlock_release(&vmp->spinlock);
return E_GENERIC_ERROR;
}
int32_t littlefs_remove(struct VfsMountPoint *vmp, const char *path) {
spinlock_acquire(&vmp->spinlock);
LittleFs *fs = &vmp->fs.littlefs;
CHECK(lfs_remove(&fs->instance, path));
spinlock_release(&vmp->spinlock);
return E_OK;
bad:
spinlock_release(&vmp->spinlock);
return E_GENERIC_ERROR;
}
int32_t littlefs_cleanup(struct VfsMountPoint *vmp) {
dlfree(vmp->fs.littlefs.instance.cfg);
int32_t err = vmp->backingsd->cleanup(vmp->backingsd);
if (err != E_OK) {
return err;
}
err = lfs_unmount(&vmp->fs.littlefs.instance);
if (err < 0) { return E_GENERIC_ERROR; }
return E_OK;
}
int32_t littlefs_create(struct VfsMountPoint *vmp, const char *path, int32_t type) {
spinlock_acquire(&vmp->spinlock);
LittleFs *fs = &vmp->fs.littlefs;
switch (type) {
case VFS_CREATE_DIR:
CHECK(lfs_mkdir(&fs->instance, path));
break;
case VFS_CREATE_FILE: {
lfs_file_t file;
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_CREAT | LFS_O_WRONLY));
CHECK(lfs_file_close(&fs->instance, &file));
} break;
}
spinlock_release(&vmp->spinlock);
return E_OK;
bad:
spinlock_release(&vmp->spinlock);
return E_GENERIC_ERROR;
}
bool littlefs_check(void) {
return true;
}
int portlfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
VfsMountPoint *vmp = c->context;
vmp->backingsd->read(vmp->backingsd, buffer, size, block * LITTLEFS_BLOCK_SIZE + off);
return 0;
}
int portlfs_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
VfsMountPoint *vmp = c->context;
vmp->backingsd->write(vmp->backingsd, buffer, size, block * LITTLEFS_BLOCK_SIZE + off);
return 0;
}
int portlfs_erase(const struct lfs_config *c, lfs_block_t block) {
(void)c;
(void)block;
return 0;
}
int portlfs_sync(const struct lfs_config *c) {
(void)c;
return 0;
}

View File

@ -0,0 +1,28 @@
#ifndef FS_PORTLFS_PORTLFS_H_
#define FS_PORTLFS_PORTLFS_H_
#include <stdint.h>
#include <stdbool.h>
#include "fs/littlefs/lfs.h"
#define LITTLEFS_BLOCK_SIZE 4096
struct VfsMountPoint;
typedef struct {
lfs_t instance;
} LittleFs;
int32_t littlefs_read(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off);
int32_t littlefs_write(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off);
int32_t littlefs_remove(struct VfsMountPoint *vmp, const char *path);
int32_t littlefs_create(struct VfsMountPoint *vmp, const char *path, int32_t type);
int32_t littlefs_cleanup(struct VfsMountPoint *vmp);
bool littlefs_check(void);
int portlfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
int portlfs_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size);
int portlfs_erase(const struct lfs_config *c, lfs_block_t block);
int portlfs_sync(const struct lfs_config *c);
#endif // FS_PORTLFS_PORTLFS_H_

View File

@ -25,8 +25,8 @@ void kmain(void) {
paging_init();
dlmalloc_check();
storedev_init();
vfs_init();
baseimg_init();
vfs_init();
kprintf(BANNER_TEXT "\n");

View File

@ -0,0 +1,9 @@
#ifndef STD_STDLIB_H_
#define STD_STDLIB_H_
#include <stddef.h>
void *malloc(size_t size);
void free(void *ptr);
#endif // STD_STDLIB_H_

10
kernel/std/stdlib.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdlib.h>
#include "dlmalloc/malloc.h"
void *malloc(size_t size) {
return dlmalloc(size);
}
void free(void *ptr) {
dlfree(ptr);
}

View File

@ -7,13 +7,18 @@
#include "storedev.h"
#include "hal/hal.h"
#include "util/util.h"
#include "kprintf.h"
int32_t ramsd_init(struct StoreDev *sd, void *extra) {
RamSdInitExtra *e = extra;
sd->sd.ramsd.capacity = e->capacity;
sd->sd.ramsd.buffer = dlmalloc(sd->sd.ramsd.capacity);
if (sd->sd.ramsd.buffer == NULL) {
return E_NOMEMORY;
if (e->preallocbuffer != NULL) {
sd->sd.ramsd.buffer = e->preallocbuffer;
} else {
sd->sd.ramsd.buffer = dlmalloc(sd->sd.ramsd.capacity);
if (sd->sd.ramsd.buffer == NULL) {
return E_NOMEMORY;
}
}
return E_OK;
}
@ -36,4 +41,13 @@ int32_t ramsd_cleanup(struct StoreDev *sd) {
spinlock_acquire(&sd->spinlock);
dlfree(sd->sd.ramsd.buffer);
spinlock_release(&sd->spinlock);
return E_OK;
}
size_t ramsd_capacity(struct StoreDev *sd) {
size_t capacity;
spinlock_acquire(&sd->spinlock);
capacity = sd->sd.ramsd.capacity;
spinlock_release(&sd->spinlock);
return capacity;
}

View File

@ -1,6 +1,9 @@
#ifndef STOREDEV_RAMSD_H_
#define STOREDEV_RAMSD_H_
#include <stdint.h>
#include <stddef.h>
struct StoreDev;
typedef struct {
@ -10,11 +13,13 @@ typedef struct {
typedef struct {
size_t capacity;
uint8_t *preallocbuffer;
} RamSdInitExtra;
int32_t ramsd_init(struct StoreDev *sd, void *extra);
int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off);
int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off);
int32_t ramsd_cleanup(struct StoreDev *sd);
size_t ramsd_capacity(struct StoreDev *sd);
#endif // STOREDEV_RAMSD_H_

View File

@ -33,6 +33,7 @@ StoreDev *storedev_create(int32_t sdtype, void *extra) {
sd->cleanup = &ramsd_cleanup;
sd->read = &ramsd_read;
sd->write = &ramsd_write;
sd->capacity = &ramsd_capacity;
int32_t err = sd->init(sd, extra);
if (err != E_OK) {
@ -53,5 +54,10 @@ int32_t storedev_delete(StoreDev *sd) {
spinlock_acquire(&STOREDEV_LIST.spinlock);
LL_REMOVE(STOREDEV_LIST.head, sd);
int32_t err = sd->cleanup(sd);
if (err < 0) {
spinlock_release(&STOREDEV_LIST.spinlock);
return err;
}
spinlock_release(&STOREDEV_LIST.spinlock);
return E_OK;
}

View File

@ -22,6 +22,7 @@ typedef struct StoreDev {
int32_t (*read)(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off);
int32_t (*write)(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off);
int32_t (*cleanup)(struct StoreDev *sd);
size_t (*capacity)(struct StoreDev *sd);
union {
RamSd ramsd;

View File

@ -17,6 +17,7 @@
} \
__tmp->next = (new); \
} else { \
(new)->next = NULL; \
(head) = (new); \
} \
} while(0)

View File

@ -9,7 +9,10 @@
#include "assert.h"
#include "errors.h"
#include "fs/kvfs/kvfs.h"
#include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h"
#include "baseimg/baseimg.h"
#include "dlmalloc/malloc.h"
VfsTable VFS_TABLE;
@ -19,6 +22,41 @@ void vfs_init_kvfs(VfsMountPoint *mp) {
mp->remove = &kvfs_remove;
mp->check = &kvfs_check;
mp->cleanup = &kvfs_cleanup;
mp->create = &kvfs_create;
}
void vfs_init_littlefs(VfsMountPoint *mp) {
struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
hal_memset(cfg, 0, sizeof(*cfg));
cfg->context = mp;
cfg->read = &portlfs_read;
cfg->prog = &portlfs_prog;
cfg->erase = &portlfs_erase;
cfg->sync = &portlfs_sync;
cfg->block_size = LITTLEFS_BLOCK_SIZE;
cfg->block_count = mp->backingsd->capacity(mp->backingsd) / LITTLEFS_BLOCK_SIZE;
cfg->read_size = 64;
cfg->prog_size = 64;
cfg->block_cycles = 16;
cfg->cache_size = 64;
cfg->lookahead_size = 64;
cfg->read_buffer = NULL;
cfg->prog_buffer = NULL;
cfg->lookahead_buffer = NULL;
cfg->name_max = 0;
cfg->file_max = 0;
cfg->attr_max = 0;
int err = lfs_mount(&mp->fs.littlefs.instance, cfg);
if (err < 0) {
ERR("vfs", "Little FS mount failed\n");
}
mp->read = &littlefs_read;
mp->write = &littlefs_write;
mp->remove = &littlefs_remove;
mp->check = &littlefs_check;
mp->cleanup = &littlefs_cleanup;
mp->create = &littlefs_create;
}
int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd) {
@ -39,6 +77,9 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd) {
case VFS_KVFS:
vfs_init_kvfs(mp);
break;
case VFS_LITTLEFS:
vfs_init_littlefs(mp);
break;
default:
return E_UNKNOWN_FSTYPE;
}
@ -82,6 +123,20 @@ int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size
return mp->read(mp, path, buffer, n, off);
}
int32_t vfs_create(char *mountpoint, const char *path, int32_t type) {
VfsMountPoint *mp = NULL;
spinlock_acquire(&VFS_TABLE.spinlock);
HSHTB_GET(&VFS_TABLE, mountpoints, mountpoint, label, mp);
spinlock_release(&VFS_TABLE.spinlock);
if (mp == NULL) {
return E_NOENTRY;
}
return mp->create(mp, path, type);
}
int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n, size_t off) {
VfsMountPoint *mp = NULL;
@ -119,11 +174,21 @@ int32_t tmpvars_init(void) {
return vfs_mount("tmpvars", VFS_KVFS, backingsd);
}
int32_t base_init(void) {
RamSdInitExtra extra = { .capacity = baseimg_getsize(), .preallocbuffer = (uint8_t*)baseimg_getaddr() };
StoreDev *backingsd = storedev_create(STOREDEV_RAMSD, &extra);
if (backingsd == NULL) {
return E_NOMEMORY;
}
return vfs_mount("base", VFS_LITTLEFS, backingsd);
}
void vfs_init(void) {
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
spinlock_init(&VFS_TABLE.spinlock);
tmpvars_init();
base_init();
LOG("vfs", "init done\n");
@ -133,7 +198,12 @@ void vfs_init(void) {
LOG("vfs", "mount point %s: %s, backing device: %s\n",
vmp->label, vfs_strings[vmp->fstype], storedev_strings[vmp->backingsd->sdtype]);
if (vmp->check != NULL) {
LOG("vfs", "check = %s\n", vmp->check() ? "OK" : "FAIL");
bool ok = vmp->check();
if (ok) {
LOG("vfs", "check = %s\n", "OK");
} else {
ERR("vfs", "check = %s\n", "FAIL");
}
} else {
LOG("vfs", "check skipped\n");
}

View File

@ -6,6 +6,7 @@
#include <stdbool.h>
#include "spinlock/spinlock.h"
#include "fs/kvfs/kvfs.h"
#include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h"
#define VFS_MOUNTPOINT_LABEL_MAX 128
@ -13,10 +14,17 @@
enum {
VFS_KVFS,
VFS_LITTLEFS,
};
static const char *vfs_strings[] = {
"KVFS"
"KVFS",
"Little FS",
};
enum {
VFS_CREATE_DIR,
VFS_CREATE_FILE,
};
typedef struct VfsMountPoint {
@ -28,11 +36,13 @@ typedef struct VfsMountPoint {
int32_t (*read)(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off);
int32_t (*write)(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off);
int32_t (*remove)(struct VfsMountPoint *vmp, const char *path);
int32_t (*create)(struct VfsMountPoint *vmp, const char *path, int32_t type);
int32_t (*cleanup)(struct VfsMountPoint *vmp);
bool (*check)(void);
union {
Kvfs kvfs;
LittleFs littlefs;
} fs;
SpinLock spinlock;
} VfsMountPoint;
@ -48,6 +58,7 @@ void vfs_init(void);
int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n, size_t off);
int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n, size_t off);
int32_t vfs_remove(char *mountpoint, const char *path);
int32_t vfs_create(char *mountpoint, const char *path, int32_t type);
int32_t vfs_unmount(char *mountpoint);
#endif // VFS_VFS_H_