Files
mop3/kernel/fs/ramdiskfs.c
kamkow1 4ad1519e06
All checks were successful
Build documentation / build-and-deploy (push) Successful in 27s
Virtual filesystem and ramdiskfs
2026-02-11 21:36:50 +01:00

139 lines
3.0 KiB
C

#include <fs/path.h>
#include <fs/ramdiskfs.h>
#include <fs/vfs.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <m/fs_desc_buffer.h>
struct ramdisk_tar_header {
char filename[100];
uint8_t mode[8];
uint8_t uid[8];
uint8_t gid[8];
uint8_t size[12];
uint8_t mtime[12];
uint8_t checksum[8];
uint8_t type_flag;
} PACKED;
struct ramdisk_tar_file {
struct ramdisk_tar_header* header;
uint8_t* content;
size_t size;
};
#define RAMDISK_FILES_MAX 128
#define RAMDISK_PATH "/boot/mop3dist.tar"
static struct ramdisk_tar_file ramdisk_files[RAMDISK_FILES_MAX];
static struct ramdisk_tar_file* ramdisk_get_file (const char* filename) {
for (size_t i = 0; i < RAMDISK_FILES_MAX; i++) {
if ((ramdisk_files[i].header != NULL) &&
(memcmp (ramdisk_files[i].header->filename, filename, strlen (filename)) == 0))
return &ramdisk_files[i];
}
return NULL;
}
static size_t ramdisk_tar_get_size (uint8_t* in) {
size_t size = 0;
size_t j;
size_t count = 1;
for (j = 11; j > 0; j--, count *= 8)
size += ((in[j - 1] - '0') * count);
return size;
}
static size_t ramdisk_tar_parse (uint8_t* addr) {
size_t i;
for (i = 0;; i++) {
struct ramdisk_tar_header* hdr = (struct ramdisk_tar_header*)addr;
if (hdr->filename[i] == '\0')
break;
size_t size = ramdisk_tar_get_size (hdr->size);
ramdisk_files[i].header = hdr;
ramdisk_files[i].content = (uint8_t*)((uintptr_t)hdr + 512);
ramdisk_files[i].size = ramdisk_tar_get_size ((uint8_t*)hdr->size);
addr += ((size / 512) + 1) * 512;
if (size % 512)
addr += 512;
}
return i;
}
bool ramdiskfs_mount (struct vfs_mountpoint* mountpoint) {
(void)mountpoint;
struct limine_module_response* module = limine_module_request.response;
uint8_t* rd_addr = NULL;
for (size_t i = 0; i < module->module_count; i++) {
struct limine_file* file = module->modules[i];
if (memcmp (file->path, RAMDISK_PATH, strlen (RAMDISK_PATH)) == 0) {
rd_addr = file->address;
}
}
if (rd_addr == NULL)
return false;
ramdisk_tar_parse (rd_addr);
return true;
}
int ramdiskfs_describe (struct vfs_mountpoint* mountpoint, const char* path,
struct fs_desc_buffer* desc) {
(void)mountpoint;
const char* filename = path_basename (path);
if (filename == NULL)
return -VFS_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -VFS_NOT_FOUND;
desc->size = file->size;
desc->type = FS_FILE;
return VFS_OK;
}
int ramdiskfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer,
size_t off, size_t size) {
(void)mountpoint;
const char* filename = path_basename (path);
if (filename == NULL)
return -VFS_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -VFS_NOT_FOUND;
if (off + size > file->size)
return -VFS_OOB_ERROR;
memcpy (buffer, (void*)((uintptr_t)file->content + off), size);
return VFS_OK;
}