From 389b687985f4671f3eea8ad0e51e8097ec864292 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Wed, 6 May 2020 16:38:45 +0200 Subject: [PATCH] Change how config works a bit --- CONFIG.md | 16 +++++++++----- src/main.c | 52 +++++++++++++++++--------------------------- src/protos/linux.c | 37 ++++++++++++++++++++++++++++--- src/protos/linux.h | 2 +- src/protos/stivale.c | 30 ++++++++++++++++++++++++- src/protos/stivale.h | 2 +- test/qloader2.cfg | 5 +++-- 7 files changed, 99 insertions(+), 45 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 1ceb0e3c..78d89a04 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -23,20 +23,26 @@ Some *local assignments* are shared between entries using any *protocol*, while * `TIMEOUT` - Specifies the timeout in seconds before the first *entry* is automatically booted. *Locally assignable (non protocol specific)* keys are: -* `KERNEL_DRIVE` - The BIOS drive (in decimal) where the kernel resides (if unspecified, boot drive is assumed). -* `KERNEL_PARTITION` - The index (in decimal) of the partition containing the kernel. -* `KERNEL_PATH` - The path of the kernel in said partition, forward slashes to delimit directories. -* `KERNEL_PROTO` - The boot protocol that will be used to boot the kernel. Valid protocols are: `linux`, `stivale`. -* `KERNEL_CMDLINE` - The command line string to be passed to the kernel. +* `PROTOCOL` - The boot protocol that will be used to boot the kernel. Valid protocols are: `linux`, `stivale`. +* `KERNEL_PROTO` - Alias of `PROTOCOL`. +* `CMDLINE` - The command line string to be passed to the kernel. Can be omitted. +* `KERNEL_CMDLINE` - Alias of `CMDLINE`. *Locally assignable (protocol specific)* keys are: * Linux protocol: + * `KERNEL_DRIVE` - The BIOS drive (in decimal) where the kernel resides (if unspecified, boot drive is assumed). + * `KERNEL_PARTITION` - The index (in decimal) of the partition containing the kernel. + * `KERNEL_PATH` - The path of the kernel in said partition, forward slashes to delimit directories. * `INITRD_PARTITION` - Partition index of the initial ramdisk. * `INITRD_PATH` - The path to the initial ramdisk. * stivale protocol: + * `KERNEL_DRIVE` - The BIOS drive (in decimal) where the kernel resides (if unspecified, boot drive is assumed). + * `KERNEL_PARTITION` - The index (in decimal) of the partition containing the kernel. + * `KERNEL_PATH` - The path of the kernel in said partition, forward slashes to delimit directories. * `MODULE_PARTITION` - Partition index of a module. * `MODULE_PATH` - The path to a module. * `MODULE_STRING` - A string to be passed to a module. + Note that one can define these 3 variable multiple times to specify multiple modules. The entries will be matched in order. E.g.: the 1st partition entry will be matched to the 1st path and the 1st string entry that appear, and so on. diff --git a/src/main.c b/src/main.c index 3a0ff8ed..f6553dd4 100644 --- a/src/main.c +++ b/src/main.c @@ -71,7 +71,9 @@ refresh: config_set_entry(selected_entry); text_enable_cursor(); if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) { - cmdline[0] = '\0'; + if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) { + cmdline[0] = '\0'; + } } text_clear(); return; @@ -79,7 +81,9 @@ refresh: config_set_entry(selected_entry); text_enable_cursor(); if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) { - cmdline[0] = '\0'; + if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) { + cmdline[0] = '\0'; + } } print("\n\n> "); gets(cmdline, cmdline, CMDLINE_MAX); @@ -90,8 +94,6 @@ refresh: } void main(int boot_drive) { - struct file_handle f; - // Initial prompt. init_vga_textmode(); @@ -121,14 +123,13 @@ void main(int boot_drive) { } } - int drive, part; - char path[128], proto[64], buf[32]; - - int timeout; - if (!config_get_value(buf, 0, 64, "TIMEOUT")) { - timeout = 5; - } else { - timeout = (int)strtoui(buf); + int timeout; { + char buf[32]; + if (!config_get_value(buf, 0, 32, "TIMEOUT")) { + timeout = 5; + } else { + timeout = (int)strtoui(buf); + } } print("\n"); @@ -152,30 +153,17 @@ void main(int boot_drive) { got_entry: init_e820(); - if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) { - drive = boot_drive; - } else { - drive = (int)strtoui(buf); - } - if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) { - panic("KERNEL_PARTITION not specified"); - } - part = (int)strtoui(buf); - if (!config_get_value(path, 0, 128, "KERNEL_PATH")) { - panic("KERNEL_PATH not specified"); - } - if (!config_get_value(proto, 0, 64, "KERNEL_PROTO")) { - panic("KERNEL_PROTO not specified"); - } - - if (fopen(&f, drive, part, path)) { - panic("Could not open kernel file"); + char proto[32]; + if (!config_get_value(proto, 0, 32, "KERNEL_PROTO")) { + if (!config_get_value(proto, 0, 32, "PROTOCOL")) { + panic("PROTOCOL not specified"); + } } if (!strcmp(proto, "stivale")) { - stivale_load(&f, cmdline); + stivale_load(cmdline, boot_drive); } else if (!strcmp(proto, "linux")) { - linux_load(&f, cmdline); + linux_load(cmdline, boot_drive); } else { panic("Invalid protocol specified"); } diff --git a/src/protos/linux.c b/src/protos/linux.c index 61443f08..e55713f3 100644 --- a/src/protos/linux.c +++ b/src/protos/linux.c @@ -10,7 +10,35 @@ #define KERNEL_LOAD_ADDR ((size_t)0x100000) #define INITRD_LOAD_ADDR ((size_t)0x1000000) -void linux_load(struct file_handle *fd, char *cmdline) { +void linux_load(char *cmdline, int boot_drive) { + int kernel_drive; { + char buf[32]; + if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) { + kernel_drive = boot_drive; + } else { + kernel_drive = (int)strtoui(buf); + } + } + + int kernel_part; { + char buf[32]; + if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) { + panic("KERNEL_PARTITION not specified"); + } else { + kernel_part = (int)strtoui(buf); + } + } + + char *kernel_path = balloc(128); + if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) { + panic("KERNEL_PATH not specified"); + } + + struct file_handle *fd = balloc(sizeof(struct file_handle)); + if (fopen(fd, kernel_drive, kernel_part, kernel_path)) { + panic("Could not open kernel file"); + } + uint32_t signature; fread(fd, &signature, 0x202, sizeof(uint32_t)); @@ -82,8 +110,11 @@ void linux_load(struct file_handle *fd, char *cmdline) { int initrd_part; { char buf[32]; - config_get_value(buf, 0, 32, "INITRD_PARTITION"); - initrd_part = (int)strtoui(buf); + if (!config_get_value(buf, 0, 32, "INITRD_PARTITION")) { + initrd_part = fd->partition; + } else { + initrd_part = (int)strtoui(buf); + } } struct file_handle initrd; diff --git a/src/protos/linux.h b/src/protos/linux.h index 3e41f041..bf09bf34 100644 --- a/src/protos/linux.h +++ b/src/protos/linux.h @@ -3,6 +3,6 @@ #include -void linux_load(struct file_handle *fd, char *cmdline); +void linux_load(char *cmdline, int boot_drive); #endif diff --git a/src/protos/stivale.c b/src/protos/stivale.c index 7c245a01..5e1609ac 100644 --- a/src/protos/stivale.c +++ b/src/protos/stivale.c @@ -46,7 +46,35 @@ struct stivale_struct { struct stivale_struct stivale_struct = {0}; -void stivale_load(struct file_handle *fd, char *cmdline) { +void stivale_load(char *cmdline, int boot_drive) { + int kernel_drive; { + char buf[32]; + if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) { + kernel_drive = boot_drive; + } else { + kernel_drive = (int)strtoui(buf); + } + } + + int kernel_part; { + char buf[32]; + if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) { + panic("KERNEL_PARTITION not specified"); + } else { + kernel_part = (int)strtoui(buf); + } + } + + char *kernel_path = balloc(128); + if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) { + panic("KERNEL_PATH not specified"); + } + + struct file_handle *fd = balloc(sizeof(struct file_handle)); + if (fopen(fd, kernel_drive, kernel_part, kernel_path)) { + panic("Could not open kernel file"); + } + struct stivale_header stivale_hdr; int bits = elf_bits(fd); diff --git a/src/protos/stivale.h b/src/protos/stivale.h index 0c7fd40d..5c808917 100644 --- a/src/protos/stivale.h +++ b/src/protos/stivale.h @@ -3,6 +3,6 @@ #include -void stivale_load(struct file_handle *fd, char *cmdline); +void stivale_load(char *cmdline, int boot_drive); #endif diff --git a/test/qloader2.cfg b/test/qloader2.cfg index 2410e05e..abfb8d8c 100644 --- a/test/qloader2.cfg +++ b/test/qloader2.cfg @@ -2,10 +2,11 @@ TIMEOUT=3 :Test kernel +PROTOCOL=stivale + KERNEL_PARTITION=1 KERNEL_PATH=/boot/test.elf -KERNEL_PROTO=stivale -KERNEL_CMDLINE=none +KERNEL_CMDLINE=something MODULE_PARTITION=1 MODULE_PATH=qloader2.cfg