From 91f7aa4009794518daedc73ba949f316b3c3e6f6 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Tue, 6 Jun 2023 23:22:08 +0200 Subject: [PATCH] feature (drivers): created infrastructure to config drivers to load on startup --- .gitignore | 2 +- build.sh | 4 +++- ramdisk/drivers.json | 5 +++++ ramdisk/noxos.json | 7 +------ src/boot/kmain.c | 4 ---- src/drivers/driver.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 ramdisk/drivers.json diff --git a/.gitignore b/.gitignore index b9a6d43..f8446b6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ build cmake-build-debug noxos.log ramdisk/shell.elf -ramdisk/*.nxkm \ No newline at end of file +*.nxkm \ No newline at end of file diff --git a/build.sh b/build.sh index 587e83d..0c8bdae 100755 --- a/build.sh +++ b/build.sh @@ -56,7 +56,9 @@ libnxdrv_build() { cd ../drivers/test bash build.sh cd ../../kernel - cp ../drivers/build/test.nxkm ramdisk/test.nxkm + + mkdir -pv ramdisk/drv/ + cp ../drivers/build/test.nxkm ramdisk/drv/test.nxkm } shell_build() { diff --git a/ramdisk/drivers.json b/ramdisk/drivers.json new file mode 100644 index 0000000..0756151 --- /dev/null +++ b/ramdisk/drivers.json @@ -0,0 +1,5 @@ +{ + "BOOT": [ + "/initrd/drv/test.nxkm" + ] +} \ No newline at end of file diff --git a/ramdisk/noxos.json b/ramdisk/noxos.json index bda6cf1..d172870 100644 --- a/ramdisk/noxos.json +++ b/ramdisk/noxos.json @@ -1,9 +1,4 @@ { "PS2_ACPI_VALIDATION": true, - "LOG_GRAPHICAL": true, - - "drivers": [ - "/initrd/drv/test.nxkm", - "/initrd/drv/nx_std_fat32.nxkm" - ] + "LOG_GRAPHICAL": true } \ No newline at end of file diff --git a/src/boot/kmain.c b/src/boot/kmain.c index 4d98fa0..ff142c0 100644 --- a/src/boot/kmain.c +++ b/src/boot/kmain.c @@ -84,10 +84,6 @@ void kmain(boot_info_T boot_info) { // // syscall_perform(SYSCALL_PROCESS_SIGNAL, pid, PROCESS_SIGNAL_START, 0, 0); - uint64_t drv_id, fd; - syscall_perform(SYSCALL_NX_FS_OPEN, (uint64_t)"/initrd/test.nxkm", 0, (uint64_t)&fd, 0); - syscall_perform(SYSCALL_NX_DRV_REGISTER, fd, (uint64_t)&drv_id, 0, 0); - driver_T* resolved = driver_lookup_pci_device(0x8086, 0x2922); DEBUG("resolved: 0x%x", resolved); diff --git a/src/drivers/driver.c b/src/drivers/driver.c index 9bd124c..6728289 100644 --- a/src/drivers/driver.c +++ b/src/drivers/driver.c @@ -2,9 +2,11 @@ #include #include +#include #include #include #include +#include #include driver_manager_T g_driver_manager; @@ -15,6 +17,47 @@ void driver_manager_init() { g_driver_manager.lookup_table_usb = hashmap_create(DRIVER_MANAGER_HASHMAP_SIZE); g_driver_manager.lookup_table_fs_gpt = hashmap_create(DRIVER_MANAGER_HASHMAP_SIZE); g_driver_manager.lookup_table_fs_mbr = hashmap_create(DRIVER_MANAGER_HASHMAP_SIZE); + + vfs_node_T* node = vfs_resolve_path(&g_root_fs, "/initrd/drivers.json"); + if (node == NULL || node->type != VFS_NODE_FILE) { + log(LOG_WARNING, "No drivers config file found. !!! THIS WILL VERY LIKELY CAUSE THE OS TO NOT WORK!!!"); + return; + } + + char* buffer = memory_allocate(node->size + 1); + vfs_file_read(node, 0, node->size, buffer); + buffer[node->size] = 0; + + json_T* json = json_from_string(buffer); + + json_node_T* child = json->root_node->childs_start; + while (child != NULL) { + if (string_compare("BOOT", child->string)) { + if (&child->childs_start[0] == NULL || child->childs_start[0].type != JSON_NODE_ARRAY) { + log(LOG_WARNING, "sysconfig -> failed to parse field 'BOOT' (expected array)"); + } else { + log(LOG_INFO, "Loading drivers for level 'BOOT':"); + json_node_T* array_child = child->childs_start[0].childs_start; + while (array_child != NULL) { + uint64_t drv_id, fd; + log(LOG_INFO, " > %s", array_child->string); + syscall_perform(SYSCALL_NX_FS_OPEN, (uint64_t)array_child->string, 0, (uint64_t)&fd, 0); + syscall_perform(SYSCALL_NX_DRV_REGISTER, fd, (uint64_t)&drv_id, 0, 0); + syscall_perform(SYSCALL_NX_FS_CLOSE, fd, 0, 0, 0); + array_child = array_child->next; + } + } + } else { + log(LOG_WARNING, "sysconfig -> skipping field '%s' (unknown identifier)", child->string); + } + + child = child->next; + } + + + json_destruct(json); + + memory_free(buffer); } driver_manager_chunk_T* driver_manager_chunk_alloc(driver_manager_chunk_T* prev) {