From 0b6fbe304e14f104a9e8cf43a09bf60709d44207 Mon Sep 17 00:00:00 2001 From: Minep Date: Sun, 18 Jun 2023 21:27:23 +0100 Subject: [PATCH] feat: dynamic boot medium probing and mounting fix: null pointer exception in usr/sh fix: parameter passing by uwrapper --- lunaix-os/config/make-os | 4 +- lunaix-os/includes/lunaix/fs/iso9660.h | 15 +++---- lunaix-os/includes/lunaix/fs/probe_boot.h | 9 ++++ lunaix-os/kernel/block/block.c | 5 ++- lunaix-os/kernel/fs/probe_boot.c | 50 +++++++++++++++++++++++ lunaix-os/kernel/loader/exec.c | 6 +-- lunaix-os/kernel/proc0.c | 7 ++-- lunaix-os/makefile.ker | 2 +- lunaix-os/uprog/sh.c | 19 +++++++-- lunaix-os/usr/uwrap.S | 4 +- 10 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 lunaix-os/includes/lunaix/fs/probe_boot.h create mode 100644 lunaix-os/kernel/fs/probe_boot.c diff --git a/lunaix-os/config/make-os b/lunaix-os/config/make-os index 7ea5c01..5cb285c 100644 --- a/lunaix-os/config/make-os +++ b/lunaix-os/config/make-os @@ -1,6 +1,8 @@ OS_ARCH := x86 OS_NAME := lunaix +OS_ID := LunaixOS +OS_VER := dev20230618 OS_BIN := $(OS_NAME).bin OS_ISO := $(OS_NAME).iso -USR_LIB := liblxusrt.a \ No newline at end of file +USR_LIB := liblxusrt.a diff --git a/lunaix-os/includes/lunaix/fs/iso9660.h b/lunaix-os/includes/lunaix/fs/iso9660.h index 3392af3..96e7338 100644 --- a/lunaix-os/includes/lunaix/fs/iso9660.h +++ b/lunaix-os/includes/lunaix/fs/iso9660.h @@ -21,11 +21,11 @@ #define ISO_SIGNATURE_HI 0x31 // Volume Types -#define ISO_VOLBOOT 0 // Boot Record -#define ISO_VOLPRIM 1 // Primary -#define ISO_VOLSUPP 2 // Supplementary -#define ISO_VOLPART 3 // Partition -#define ISO_VOLTERM 255 // Volume descriptor set terminator +#define ISO_VOLBOOT 0 // Boot Record +#define ISO_VOLPRIM 1 // Primary +#define ISO_VOLSUPP 2 // Supplementary +#define ISO_VOLPART 3 // Partition +#define ISO_VOLTERM 255 // Volume descriptor set terminator #define ISO_FHIDDEN 0x1 // a hidden file #define ISO_FDIR 0x2 // a directory file @@ -36,6 +36,7 @@ #define ISO9660_BLKSZ 2048 #define ISO9660_IDLEN 256 +#define ISO9660_READ_OFF (ISO9660_BLKSZ * 16) // NOTES: // Each Descriptor sized 1 logical block (2048 bytes in common cases) @@ -155,8 +156,8 @@ struct iso_drecord iso_bbo32_t data_size; struct iso_datetime2 PACKED mktime; // Time the record is made, see 9.1.5 u8_t flags; - u8_t fu_sz; // size of file unit (FU) - u8_t gap_sz; // size of gap if FU is interleaved. + u8_t fu_sz; // size of file unit (FU) + u8_t gap_sz; // size of gap if FU is interleaved. iso_bbo16_t vol_seq; struct iso_var_mdu name; } PACKED; diff --git a/lunaix-os/includes/lunaix/fs/probe_boot.h b/lunaix-os/includes/lunaix/fs/probe_boot.h new file mode 100644 index 0000000..4eb8e06 --- /dev/null +++ b/lunaix-os/includes/lunaix/fs/probe_boot.h @@ -0,0 +1,9 @@ +#ifndef __LUNAIX_PROBE_BOOT_H +#define __LUNAIX_PROBE_BOOT_H + +#include + +struct device* +probe_boot_medium(); + +#endif /* __LUNAIX_PROBE_BOOT_H */ diff --git a/lunaix-os/kernel/block/block.c b/lunaix-os/kernel/block/block.c index 1cade0c..f699169 100644 --- a/lunaix-os/kernel/block/block.c +++ b/lunaix-os/kernel/block/block.c @@ -24,6 +24,7 @@ LOG_MODULE("BLOCK") static struct cake_pile* lbd_pile; static struct block_dev** dev_registry; static struct twifs_node* blk_sysroot; +static struct device* blk_parent_dev; int free_slot = 0; @@ -41,6 +42,7 @@ block_init() dev_registry = vcalloc(sizeof(struct block_dev*), MAX_DEV); free_slot = 0; blk_sysroot = twifs_dir_node(NULL, "block"); + blk_parent_dev = device_addcat(NULL, "block"); } int @@ -314,7 +316,8 @@ __block_register(struct block_dev* bdev) return 0; } - struct device* dev = device_addvol(NULL, bdev, "sd%c", 'a' + free_slot); + struct device* dev = + device_addvol(blk_parent_dev, bdev, "sd%c", 'a' + free_slot); dev->write = __block_write; dev->write_page = __block_write_page; dev->read = __block_read; diff --git a/lunaix-os/kernel/fs/probe_boot.c b/lunaix-os/kernel/fs/probe_boot.c new file mode 100644 index 0000000..69af61e --- /dev/null +++ b/lunaix-os/kernel/fs/probe_boot.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +LOG_MODULE("PROBE") + +#define LUNAIX_ID 0x414e554cUL // "LUNA" + +struct device* +probe_boot_medium() +{ + struct device* block_cat = device_getbyname(NULL, "block", 5); + if (!block_cat) { + return NULL; + } + + struct iso_vol_primary* volp = valloc(ISO9660_BLKSZ); + + struct device *pos, *n; + llist_for_each(pos, n, &block_cat->children, siblings) + { + int errno = + pos->read(pos, (void*)volp, ISO9660_READ_OFF, ISO9660_BLKSZ); + if (errno < 0) { + kprintf(KWARN "can not probe %x:%s (%d)\n", + pos->dev_id, + pos->name.value, + errno); + pos = NULL; + goto done; + } + + if (*(u32_t*)volp->header.std_id != ISO_SIGNATURE_LO) { + continue; + } + + if (*(u32_t*)volp->sys_id == LUNAIX_ID) { + kprintf(KINFO "[%x:%s] %s\n", + pos->dev_id, + pos->name.value, + (char*)volp->vol_id); + break; + } + } + +done: + vfree(volp); + return pos; +} \ No newline at end of file diff --git a/lunaix-os/kernel/loader/exec.c b/lunaix-os/kernel/loader/exec.c index e5e64fe..6a18ffc 100644 --- a/lunaix-os/kernel/loader/exec.c +++ b/lunaix-os/kernel/loader/exec.c @@ -26,11 +26,11 @@ exec_str_size(const char** str_arr, size_t* length) sz += strlen(chr); len++; - chr = *(str_arr + sz); + chr = *(str_arr++); } *length = len; - return sz + 1; + return sz + sizeof(char*); } void @@ -116,7 +116,7 @@ exec_load(struct ld_param* param, memcpy(arg_start + sz_argv, (void*)envp, sz_envp); ptr_t* ustack = (ptr_t*)USTACK_TOP; - struct usr_exec_param* exec_param = mapped; + struct usr_exec_param* exec_param = (struct usr_exec_param*)mapped; ustack[-1] = (ptr_t)mapped; param->info.stack_top = &ustack[-1]; diff --git a/lunaix-os/kernel/proc0.c b/lunaix-os/kernel/proc0.c index abd0ac0..c38ad2b 100644 --- a/lunaix-os/kernel/proc0.c +++ b/lunaix-os/kernel/proc0.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -48,14 +49,14 @@ mount_bootmedium() { struct v_dnode* dnode; int errno = 0; - if ((errno = vfs_walk_proc("/dev/sdb", &dnode, NULL, 0))) { + struct device* dev = probe_boot_medium(); + if (!dev) { kprintf(KERROR "fail to acquire device. (%d)", errno); return 0; } - struct device* dev = (struct device*)dnode->inode->data; if ((errno = vfs_mount("/mnt/lunaix-os", "iso9660", dev, 0))) { - kprintf(KERROR "fail to boot medium. (%d)", errno); + kprintf(KERROR "fail to mount boot medium. (%d)", errno); return 0; } diff --git a/lunaix-os/makefile.ker b/lunaix-os/makefile.ker index 8411140..05dcc13 100644 --- a/lunaix-os/makefile.ker +++ b/lunaix-os/makefile.ker @@ -31,7 +31,7 @@ $(BUILD_DIR)/$(OS_ISO): $(BIN_DIR)/$(OS_BIN) @./config-grub.sh ${OS_NAME} $(ISO_GRUB_DIR)/grub.cfg @cp $(BIN_DIR)/$(OS_BIN) $(ISO_BOOT_DIR) @cp -r $(USR_DIR) $(ISO_DIR) - @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR) + @grub-mkrescue -o $(BUILD_DIR)/$(OS_ISO) $(ISO_DIR) -- -volid "$(OS_ID) $(OS_VER)" -system_id "$(OS_NAME)" all: $(BUILD_DIR)/$(OS_ISO) diff --git a/lunaix-os/uprog/sh.c b/lunaix-os/uprog/sh.c index 4e0c6f6..10aed0b 100644 --- a/lunaix-os/uprog/sh.c +++ b/lunaix-os/uprog/sh.c @@ -60,7 +60,11 @@ parse_cmdline(char* line, char** cmd, char** arg_part) l++; } *cmd = line; - *arg_part = strltrim_safe(line + l); + if (c) { + *arg_part = strltrim_safe(line + l); + } else { + *arg_part = NULL; + } } void @@ -134,25 +138,34 @@ sh_loop() // stdout (by default, unless user did smth) is the tty we are currently at ioctl(stdout, TIOCSPGRP, getpgid()); + char* argv[] = { 0, 0 }; + while (1) { getcwd(pwd, 512); printf("[\033[2m%s\033[39;49m]$ ", pwd); size_t sz = read(stdin, buf, 511); + if (sz < 0) { printf("fail to read user input (%d)\n", geterrno()); return; } + buf[sz] = '\0'; - parse_cmdline(buf, &cmd, &argpart); + + // currently, this shell only support single argument + parse_cmdline(buf, &cmd, &argv[0]); + if (cmd[0] == 0) { printf("\n"); goto cont; } + // cmd=="exit" if (*(unsigned int*)cmd == 0x74697865U) { break; } - sh_exec(cmd, NULL); + + sh_exec(cmd, (const char**)&argv); cont: printf("\n"); } diff --git a/lunaix-os/usr/uwrap.S b/lunaix-os/usr/uwrap.S index 5708463..c9e2bbc 100644 --- a/lunaix-os/usr/uwrap.S +++ b/lunaix-os/usr/uwrap.S @@ -9,13 +9,15 @@ .section .text .global _u_start _u_start: + movl (%esp), %eax + pushl %eax call usr_pre_init jnz 1f popl %eax - pushl (%eax) // argc pushl 4(%eax) // argv + pushl (%eax) // argc xorl %eax, %eax call main -- 2.27.0