Implement storedevs, prepare to port littlefs

This commit is contained in:
2025-08-16 12:34:36 +02:00
parent c936910199
commit 2b0566c56f
91 changed files with 54963 additions and 37 deletions

39
kernel/storedev/ramsd.c Normal file
View File

@ -0,0 +1,39 @@
#include <stddef.h>
#include <stdint.h>
#include "spinlock/spinlock.h"
#include "errors.h"
#include "dlmalloc/malloc.h"
#include "ramsd.h"
#include "storedev.h"
#include "hal/hal.h"
#include "util/util.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;
}
return E_OK;
}
int32_t ramsd_read(struct StoreDev *sd, uint8_t *const buffer, size_t n, size_t off) {
spinlock_acquire(&sd->spinlock);
hal_memcpy(buffer, sd->sd.ramsd.buffer + off, MIN(n, sd->sd.ramsd.capacity - off));
spinlock_release(&sd->spinlock);
return E_OK;
}
int32_t ramsd_write(struct StoreDev *sd, const uint8_t *const buffer, size_t n, size_t off) {
spinlock_acquire(&sd->spinlock);
hal_memcpy(sd->sd.ramsd.buffer + off, buffer, MIN(n, sd->sd.ramsd.capacity - off));
spinlock_release(&sd->spinlock);
return E_OK;
}
int32_t ramsd_cleanup(struct StoreDev *sd) {
spinlock_acquire(&sd->spinlock);
dlfree(sd->sd.ramsd.buffer);
spinlock_release(&sd->spinlock);
}

20
kernel/storedev/ramsd.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef STOREDEV_RAMSD_H_
#define STOREDEV_RAMSD_H_
struct StoreDev;
typedef struct {
size_t capacity;
uint8_t *buffer;
} RamSd;
typedef struct {
size_t capacity;
} 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);
#endif // STOREDEV_RAMSD_H_

View File

@ -0,0 +1,57 @@
#include <stdint.h>
#include <stddef.h>
#include "storedev.h"
#include "spinlock/spinlock.h"
#include "kprintf.h"
#include "errors.h"
#include "dlmalloc/malloc.h"
#include "ramsd.h"
#include "util/util.h"
StoreDevList STOREDEV_LIST;
void storedev_init(void) {
spinlock_init(&STOREDEV_LIST.spinlock);
STOREDEV_LIST.head = NULL;
LOG("storedev", "init\n");
}
StoreDev *storedev_create(int32_t sdtype, void *extra) {
StoreDev *sd = dlmalloc(sizeof(*sd));
if (sd == NULL) {
return NULL;
}
spinlock_acquire(&STOREDEV_LIST.spinlock);
switch (sdtype) {
case STOREDEV_RAMSD: {
spinlock_init(&sd->spinlock);
sd->sdtype = STOREDEV_RAMSD;
sd->init = &ramsd_init;
sd->cleanup = &ramsd_cleanup;
sd->read = &ramsd_read;
sd->write = &ramsd_write;
int32_t err = sd->init(sd, extra);
if (err != E_OK) {
spinlock_release(&STOREDEV_LIST.spinlock);
return NULL;
}
LL_APPEND(STOREDEV_LIST.head, sd);
} break;
default:
spinlock_release(&STOREDEV_LIST.spinlock);
return NULL;
}
spinlock_release(&STOREDEV_LIST.spinlock);
return sd;
}
int32_t storedev_delete(StoreDev *sd) {
spinlock_acquire(&STOREDEV_LIST.spinlock);
LL_REMOVE(STOREDEV_LIST.head, sd);
int32_t err = sd->cleanup(sd);
spinlock_release(&STOREDEV_LIST.spinlock);
}

View File

@ -0,0 +1,44 @@
#ifndef STOREDEV_STOREDEV_H_
#define STOREDEV_STOREDEV_H_
#include <stdint.h>
#include <stddef.h>
#include "spinlock/spinlock.h"
#include "ramsd.h"
enum {
STOREDEV_RAMSD,
};
static const char *storedev_strings[] = {
"RAMSD",
};
typedef struct StoreDev {
struct StoreDev *next;
int32_t sdtype;
int32_t (*init)(struct StoreDev *sd, void *extra);
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);
union {
RamSd ramsd;
} sd;
SpinLock spinlock;
} StoreDev;
typedef struct {
StoreDev *head;
SpinLock spinlock;
} StoreDevList;
extern StoreDevList STOREDEV_LIST;
void storedev_init(void);
StoreDev *storedev_create(int32_t sdtype, void *extra);
int32_t storedev_delete(StoreDev *sd);
#endif // STOREDEV_STOREDEV_H_