Files
mop3/kernel/fs/ramdiskfs.c
kamkow1 b0b69f3e9e
All checks were successful
Build documentation / build-and-deploy (push) Successful in 1m26s
Add sys_exec () and libprocess wrapper, fix ramdisk tar parsing
2026-02-14 21:50:09 +01:00

139 lines
3.1 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>
#include <m/status.h>
#include <sys/debug.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_FILENAME_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) &&
(strncmp (ramdisk_files[i].header->filename, filename, RAMDISK_FILENAME_MAX) == 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 < RAMDISK_FILES_MAX; i++) {
struct ramdisk_tar_header* hdr = (struct ramdisk_tar_header*)addr;
if (hdr->filename[0] == '\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 += 512 + ((size + 511) & ~511);
}
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 (strncmp (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 -ST_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -ST_NOT_FOUND;
desc->size = file->size;
desc->type = FS_FILE;
return ST_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 -ST_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -ST_NOT_FOUND;
if (off + size > file->size)
return -ST_OOB_ERROR;
memcpy (buffer, (void*)((uintptr_t)file->content + off), size);
return ST_OK;
}