Make config more encapsulated

This commit is contained in:
mintsuki 2020-03-30 21:24:36 +02:00
parent 84ac96435e
commit babc5269eb
3 changed files with 42 additions and 19 deletions

View File

@ -1,25 +1,51 @@
#include <stddef.h> #include <stddef.h>
#include <lib/config.h> #include <lib/config.h>
#include <lib/libc.h> #include <lib/libc.h>
#include <lib/blib.h>
#include <fs/echfs.h>
#define SEPARATOR '\n' #define SEPARATOR '\n'
#define CONFIG_NAME "qloader2.cfg"
#define MAX_CONFIG_SIZE 4096
char *config_get_value(char *buf, size_t limit, const char *config, const char *key) { static char *config_addr;
if (!limit || !buf)
int init_config(int drive, int part) {
struct echfs_file_handle f;
if (echfs_open(&f, drive, part, CONFIG_NAME)) {
return -1;
}
if (f.dir_entry.size >= 4096) {
print("Config file is too big!\n");
for (;;);
}
config_addr = balloc(MAX_CONFIG_SIZE);
memset(config_addr, 0, MAX_CONFIG_SIZE);
echfs_read(&f, config_addr, 0, f.dir_entry.size);
return 0;
}
char *config_get_value(char *buf, size_t limit, const char *key) {
if (!limit || !buf || !key)
return NULL; return NULL;
size_t key_len = strlen(key); size_t key_len = strlen(key);
for (size_t i = 0; config[i]; i++) { for (size_t i = 0; config_addr[i]; i++) {
if (!strncmp(&config[i], key, key_len) && config[i + key_len] == '=') { if (!strncmp(&config_addr[i], key, key_len) && config_addr[i + key_len] == '=') {
if (i && config[i - 1] != SEPARATOR) if (i && config_addr[i - 1] != SEPARATOR)
continue; continue;
i += key_len + 1; i += key_len + 1;
size_t j; size_t j;
for (j = 0; config[i + j] != SEPARATOR && config[i + j]; j++) { for (j = 0; config_addr[i + j] != SEPARATOR && config_addr[i + j]; j++) {
if (j == limit - 1) if (j == limit - 1)
break; break;
buf[j] = config[i + j]; buf[j] = config_addr[i + j];
} }
buf[j] = 0; buf[j] = 0;
return buf; return buf;

View File

@ -3,6 +3,7 @@
#include <stddef.h> #include <stddef.h>
char *config_get_value(char *buf, size_t limit, const char *config, const char *key); int init_config(int drive, int part);
char *config_get_value(char *buf, size_t limit, const char *key);
#endif #endif

View File

@ -16,8 +16,6 @@ asm (
#include <lib/elf.h> #include <lib/elf.h>
#include <protos/stivale.h> #include <protos/stivale.h>
#define CONFIG_NAME "qloader2.cfg"
extern symbol bss_begin; extern symbol bss_begin;
extern symbol bss_end; extern symbol bss_end;
@ -38,7 +36,6 @@ void main(int boot_drive) {
print("qLoader 2\n\n"); print("qLoader 2\n\n");
print("=> Boot drive: %x\n", boot_drive); print("=> Boot drive: %x\n", boot_drive);
void *config_addr = balloc(4096);
// Enumerate partitions. // Enumerate partitions.
struct mbr_part parts[4]; struct mbr_part parts[4];
@ -50,8 +47,7 @@ void main(int boot_drive) {
} else { } else {
print(" Found!\n"); print(" Found!\n");
if (!config_loaded) { if (!config_loaded) {
if (!echfs_open(&f, boot_drive, i, CONFIG_NAME)) { if (!init_config(boot_drive, i)) {
echfs_read(&f, config_addr, 0, f.dir_entry.size);
config_loaded = 1; config_loaded = 1;
print(" Config file found and loaded!\n"); print(" Config file found and loaded!\n");
} }
@ -64,22 +60,22 @@ void main(int boot_drive) {
if (config_loaded) { if (config_loaded) {
char buf[32]; char buf[32];
if (!config_get_value(buf, 32, config_addr, "KERNEL_DRIVE")) { if (!config_get_value(buf, 32, "KERNEL_DRIVE")) {
print("KERNEL_DRIVE not specified, using boot drive (%x)", boot_drive); print("KERNEL_DRIVE not specified, using boot drive (%x)", boot_drive);
drive = boot_drive; drive = boot_drive;
} else { } else {
drive = (int)strtoui(buf); drive = (int)strtoui(buf);
} }
if (!config_get_value(buf, 64, config_addr, "TIMEOUT")) { if (!config_get_value(buf, 64, "TIMEOUT")) {
timeout = 5; timeout = 5;
} else { } else {
timeout = (int)strtoui(buf); timeout = (int)strtoui(buf);
} }
config_get_value(buf, 32, config_addr, "KERNEL_PARTITION"); config_get_value(buf, 32, "KERNEL_PARTITION");
part = (int)strtoui(buf); part = (int)strtoui(buf);
config_get_value(path, 128, config_addr, "KERNEL_PATH"); config_get_value(path, 128, "KERNEL_PATH");
config_get_value(cmdline, 128, config_addr, "KERNEL_CMDLINE"); config_get_value(cmdline, 128, "KERNEL_CMDLINE");
config_get_value(proto, 64, config_addr, "KERNEL_PROTO"); config_get_value(proto, 64, "KERNEL_PROTO");
} else { } else {
print(" !! NO CONFIG FILE FOUND ON BOOT DRIVE !!"); print(" !! NO CONFIG FILE FOUND ON BOOT DRIVE !!");
for (;;); for (;;);