From 891f1010bbdc1351bda8d2a6139094a14bdfd5e1 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Sat, 24 Feb 2024 14:47:25 -0600 Subject: kernel: string: add a strcpy function Add an implmentation of strcpy. This function is used in several places in the kernel to copy strings between memory addresses. Signed-off-by: Danny Holman --- Makefile | 1 + arch/i386/boot/boot.s | 16 ++++++---------- arch/i386/kernel/timer.c | 1 - include/kernel/string.h | 1 + kernel/init.c | 25 +++++++++++++++++++++---- kernel/string.c | 8 ++++++++ 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 5db0d5e..a0ce753 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ KERNEL_OBJS=$(KERNEL_ARCH_OBJS) \ kernel/init.o \ kernel/string.o \ kernel/io.o \ + kernel/panic.o \ kernel/mem.o \ OBJS=$(ARCHDIR)/boot/crti.o \ diff --git a/arch/i386/boot/boot.s b/arch/i386/boot/boot.s index 4b9106d..2d17f6a 100644 --- a/arch/i386/boot/boot.s +++ b/arch/i386/boot/boot.s @@ -76,25 +76,21 @@ _start: 1: hlt jmp 1b -.global load_page_dir -.type load_page_dir, @function -load_page_dir: +.global enable_paging +.type enable_paging, @function +enable_paging: pushl %ebp movl %esp, %ebp movl 8(%esp), %eax movl %eax, %cr3 - movl %ebp, %esp - popl %ebp - ret - -.global enable_paging -.type enable_paging, @function -enable_paging: movl %cr0, %eax orl $0x80000001, %eax movl %eax, %cr0 + + movl $0, %eax + popl %ebp ret .global flush_gdt diff --git a/arch/i386/kernel/timer.c b/arch/i386/kernel/timer.c index 4aafed1..e93c291 100644 --- a/arch/i386/kernel/timer.c +++ b/arch/i386/kernel/timer.c @@ -1,7 +1,6 @@ #include #include #include -#include static uint32_t num_ticks = 0; diff --git a/include/kernel/string.h b/include/kernel/string.h index f9a8eed..76bf610 100644 --- a/include/kernel/string.h +++ b/include/kernel/string.h @@ -10,6 +10,7 @@ void* memset(void *str, int c, size_t n); int strncmp(const char *str1, const char *str2, size_t n); int strcmp(const char *str1, const char *str2); size_t strlen(const char *str); +char* strncpy(char* __restrict dest, const char* __restrict src, size_t n); char* strcpy(char* __restrict dest, const char* __restrict src); char* strcat(char* __restrict dest, const char* __restrict src); char* strtok(char* __restrict str, const char* __restrict delim); diff --git a/kernel/init.c b/kernel/init.c index 195ce92..86567a4 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -1,8 +1,25 @@ -#include #include +#include +#include #include -void kernel_main(void) { - tty_init(); - kprintf("Hello world\n"); +void jump_userspace(void); + +char rootfs[1024]; + +int start_init(int argc, char* argv[]) { + return 0; +} + +void process_cmd(char *cmdline) { + char *token = strtok(cmdline, " "); + while (token != NULL) { + if (strncmp(token, "root=", 5) == 0) + strcpy(rootfs, &token[6]); + } +} + +void kernel_main(char *cmdline) { + kprintf("Box Kernel version %s\n", VERSION); + serial_init(); } diff --git a/kernel/string.c b/kernel/string.c index 35dc16e..fa0fd3f 100644 --- a/kernel/string.c +++ b/kernel/string.c @@ -31,6 +31,14 @@ void* memcpy(void* __restrict dest, const void* __restrict src, size_t n) { return dest; } +char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) { + for (int i = 0; i < n; i++) { + if (src[i] == '\0') + break; + dest[i] = src[i]; + } +} + char* strcpy(char* __restrict dest, const char* __restrict src) { if (strlen(src) > strlen(dest)) return NULL; -- cgit v1.2.3