From 758d681005337cde00903642a488e1433612e4a2 Mon Sep 17 00:00:00 2001 From: Jozef Nagy Date: Sun, 26 Jan 2025 18:05:21 +0100 Subject: [PATCH] Added VFS read and started UEFI SFS driver --- boot/common/fs/uefi_sfs.c | 36 ++++++++++++++++++ boot/common/init.c | 3 ++ boot/common/mm/mman.c | 2 +- boot/common/vfs/vfs.c | 75 +++++++++++++++++++++++++++++++++++--- boot/include/fs/uefi_sfs.h | 27 ++++++++++++++ boot/include/vfs/vfs.h | 11 ++++-- 6 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 boot/common/fs/uefi_sfs.c create mode 100644 boot/include/fs/uefi_sfs.h diff --git a/boot/common/fs/uefi_sfs.c b/boot/common/fs/uefi_sfs.c new file mode 100644 index 0000000..1b17978 --- /dev/null +++ b/boot/common/fs/uefi_sfs.c @@ -0,0 +1,36 @@ +/*********************************************************************************/ +/* Module Name: uefi_sfs.c */ +/* Project: AurixOS */ +/* */ +/* Copyright (c) 2024-2025 Jozef Nagy */ +/* */ +/* This source is subject to the MIT License. */ +/* See License.txt in the root of this repository. */ +/* All other rights reserved. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/*********************************************************************************/ + +#ifndef AXBOOT_UEFI + +#include +#include +#include +#include + +struct sfs_fsdata { +}; + +uint8_t sfs_read(char *filename, char *buffer, struct device *dev, void *fsdata) +{ + (void)dev; + (void)fsdata; +} + +#endif \ No newline at end of file diff --git a/boot/common/init.c b/boot/common/init.c index 8c50d47..72280f5 100644 --- a/boot/common/init.c +++ b/boot/common/init.c @@ -17,7 +17,10 @@ /* SOFTWARE. */ /*********************************************************************************/ +#include + void axboot_init() { + vfs_init(); while (1); } \ No newline at end of file diff --git a/boot/common/mm/mman.c b/boot/common/mm/mman.c index c2a4d6e..47d952d 100644 --- a/boot/common/mm/mman.c +++ b/boot/common/mm/mman.c @@ -24,7 +24,7 @@ #include // NOTE: If any allocations fail, try increasing this number. -#define MAX_ALLOCATIONS 256 +#define MAX_ALLOCATIONS 64 struct alloc_header allocation_list[MAX_ALLOCATIONS] = {0}; diff --git a/boot/common/vfs/vfs.c b/boot/common/vfs/vfs.c index 46b52bd..38cc383 100644 --- a/boot/common/vfs/vfs.c +++ b/boot/common/vfs/vfs.c @@ -17,22 +17,87 @@ /* SOFTWARE. */ /*********************************************************************************/ +/* VFS originally written by Levente Kurusa, stripped down for AxBoot */ +/* https://github.com/levex/osdev */ + +#include +#include #include +#include +#include #include #include +#define MAX_MOUNTS 32 + +struct vfs_mount **mountpoints = NULL; +uint8_t last_mount = 0; + +size_t str_backspace(char *str, char c) +{ + size_t i = strlen(str) - 1; + + while (--i) { + if (str[i] == c) { + str[i+1] = 0; + return 1; + } + } + return 0; +} + +/* Returns UINT8_MAX incase an error happens */ +uint8_t find_mntpoint_from_filename(char *filename, uint32_t *s_off) +{ + char *orig = (char *)mem_alloc(strlen(filename) + 1); + if (!orig) { + debug("find_mntpoint_from_filename(): Failed to allocate memory for filename!\n"); + return UINT8_MAX; + } + + memset(orig, 0, strlen(filename) + 1); + memcpy(orig, filename, strlen(filename) + 1); + + if (orig[strlen(orig)] == '/') + str_backspace(orig, '/'); + + // TODO: Check if there's a way to stay in this loop forever + while (1) { + for (int i = 0; i < MAX_MOUNTS; i++) { + if (!mountpoints[i]) + break; + + if (strcmp(mountpoints[i]->mnt, orig) == 0) { + /* Adjust the orig to make it relative to the partition */ + *s_off = (strlen(orig) - 1); + mem_free(orig); + return i; + } + } + + if (strcmp(orig, "/") == 0) + break; + str_backspace(orig, '/'); + } + return 0; +} + void vfs_init(void) { + mountpoints = (struct vfs_mount **)mem_alloc(sizeof(struct vfs_mount) * MAX_MOUNTS); + if (!mountpoints) { + debug("vfs_init(): Failed to allocate memory for VFS!\n"); + // TODO: Panic and halt + } } int vfs_read(char *filename, char *buf, size_t len) - { - (void)filename; - (void)buf; - (void)len; + uint32_t s_off = 0; + int i = find_mntpoint_from_filename(filename, &s_off); + filename += s_off; - return 0; + return mountpoints[i]->drive->fs->read(filename, buf, mountpoints[i]->drive, mountpoints[i]->drive->fs->fs_data); } int vfs_write(char *filename, char *buf, size_t len) diff --git a/boot/include/fs/uefi_sfs.h b/boot/include/fs/uefi_sfs.h new file mode 100644 index 0000000..8d22f7b --- /dev/null +++ b/boot/include/fs/uefi_sfs.h @@ -0,0 +1,27 @@ +/*********************************************************************************/ +/* Module Name: uefi_sfs.c */ +/* Project: AurixOS */ +/* */ +/* Copyright (c) 2024-2025 Jozef Nagy */ +/* */ +/* This source is subject to the MIT License. */ +/* See License.txt in the root of this repository. */ +/* All other rights reserved. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */ +/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */ +/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */ +/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */ +/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */ +/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ +/* SOFTWARE. */ +/*********************************************************************************/ + +#ifndef _FS_UEFI_SFS_H +#define _FS_UEFI_SFS_H + +#ifndef AXBOOT_UEFI + +#endif + +#endif /* _FS_UEFI_SFS_H */ diff --git a/boot/include/vfs/vfs.h b/boot/include/vfs/vfs.h index 3a921a2..dc6f8c9 100644 --- a/boot/include/vfs/vfs.h +++ b/boot/include/vfs/vfs.h @@ -26,12 +26,17 @@ struct vfs_drive; struct vfs_filesystem { - int (*read_file)(struct vfs_drive *drive, char *filename, char *buf, size_t len); - int (*write_file)(struct vfs_drive *drive, char *filename, char *buf, size_t len); + char *fsname; + + uint8_t (*read)(char *, char *, struct vfs_drive *, void *); + uint8_t (*write)(char *, char *, size_t, struct vfs_drive *, void *); + uint8_t (*mount)(struct vfs_drive *, void *); + + uint8_t *fs_data; }; struct vfs_mount { - char *mountpoint; + char *mnt; struct vfs_drive *drive; };