limine/src/main.c

177 lines
4.5 KiB
C
Raw Normal View History

2019-05-15 07:08:56 +03:00
asm (
".section .entry\n\t"
2019-05-15 07:08:56 +03:00
"xor dh, dh\n\t"
"push edx\n\t"
2020-04-29 17:53:05 +03:00
// Zero out .bss
"xor al, al\n\t"
"lea edi, bss_begin\n\t"
"lea ecx, bss_end\n\t"
"lea edx, bss_begin\n\t"
"sub ecx, edx\n\t"
"rep stosb\n\t"
2019-05-15 07:08:56 +03:00
"call main\n\t"
);
#include <drivers/vga_textmode.h>
#include <lib/real.h>
2020-01-22 07:02:12 +03:00
#include <lib/blib.h>
#include <lib/libc.h>
2020-04-15 14:21:44 +03:00
#include <lib/part.h>
2020-01-22 09:13:19 +03:00
#include <lib/config.h>
2020-05-03 00:38:57 +03:00
#include <lib/e820.h>
2020-06-05 18:51:33 +03:00
#include <lib/memmap.h>
2020-05-10 01:38:27 +03:00
#include <lib/print.h>
2020-04-14 06:20:55 +03:00
#include <fs/file.h>
2020-03-25 03:04:18 +03:00
#include <lib/elf.h>
#include <protos/stivale.h>
2020-04-19 11:14:49 +03:00
#include <protos/linux.h>
2020-05-06 18:00:41 +03:00
#include <protos/chainload.h>
2020-01-21 13:42:17 +03:00
2020-04-24 19:26:08 +03:00
static char *cmdline;
#define CMDLINE_MAX 1024
2020-04-21 19:27:05 +03:00
static char config_entry_name[1024];
2020-04-21 19:27:05 +03:00
void boot_menu(void) {
text_disable_cursor();
int selected_entry = 0;
refresh:
text_clear();
print("qloader2\n\n");
print("Select an entry:\n\n");
2020-04-29 17:53:05 +03:00
int max_entries;
2020-04-21 19:27:05 +03:00
for (max_entries = 0; ; max_entries++) {
if (config_get_entry_name(config_entry_name, max_entries, 1024) == -1)
2020-04-21 19:27:05 +03:00
break;
if (max_entries == selected_entry)
print(" -> %s\n", config_entry_name);
2020-04-21 19:27:05 +03:00
else
print(" %s\n", config_entry_name);
2020-04-21 19:27:05 +03:00
}
2020-04-24 19:26:08 +03:00
if (max_entries == 0)
panic("Config contains no entries.");
2020-04-21 19:27:05 +03:00
print("\nArrows to choose, enter to select, 'e' to edit command line.");
for (;;) {
int c = getchar();
switch (c) {
case GETCHAR_CURSOR_UP:
if (--selected_entry == -1)
selected_entry = max_entries - 1;
goto refresh;
case GETCHAR_CURSOR_DOWN:
if (++selected_entry == max_entries)
selected_entry = 0;
goto refresh;
case '\r':
config_set_entry(selected_entry);
text_enable_cursor();
2020-04-24 19:26:08 +03:00
if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
2020-05-06 17:38:45 +03:00
if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
cmdline[0] = '\0';
}
2020-04-24 19:26:08 +03:00
}
2020-04-21 19:27:05 +03:00
text_clear();
return;
case 'e':
config_set_entry(selected_entry);
text_enable_cursor();
2020-04-24 19:26:08 +03:00
if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
2020-05-06 17:38:45 +03:00
if (!config_get_value(cmdline, 0, CMDLINE_MAX, "CMDLINE")) {
cmdline[0] = '\0';
}
2020-04-24 19:26:08 +03:00
}
2020-04-21 19:27:05 +03:00
print("\n\n> ");
2020-04-24 19:26:08 +03:00
gets(cmdline, cmdline, CMDLINE_MAX);
2020-04-21 19:27:05 +03:00
text_clear();
return;
}
}
}
2019-05-15 07:08:56 +03:00
void main(int boot_drive) {
// Initial prompt.
init_vga_textmode();
2020-01-25 04:05:19 +03:00
2020-04-21 19:27:05 +03:00
print("qloader2\n\n");
2020-04-24 19:26:08 +03:00
2020-04-15 14:21:44 +03:00
print("Boot drive: %x\n", boot_drive);
2020-01-21 13:42:17 +03:00
2020-04-24 19:26:08 +03:00
cmdline = balloc(CMDLINE_MAX);
2020-04-15 14:21:44 +03:00
// Look for config file.
print("Searching for config file...\n");
struct part parts[4];
for (int i = 0; ; i++) {
if (i == 4) {
panic("Config file not found.");
}
print("Checking partition %d...\n", i);
int ret = get_part(&parts[i], boot_drive, i);
if (ret) {
2020-04-15 14:21:44 +03:00
print("Partition not found.\n");
} else {
2020-04-15 14:21:44 +03:00
print("Partition found.\n");
if (!init_config(boot_drive, i)) {
print("Config file found and loaded.\n");
break;
2020-01-22 09:13:19 +03:00
}
}
}
2020-05-06 17:38:45 +03:00
int timeout; {
char buf[32];
if (!config_get_value(buf, 0, 32, "TIMEOUT")) {
timeout = 5;
} else {
timeout = (int)strtoui(buf);
}
2020-01-22 09:13:19 +03:00
}
2020-04-21 19:27:05 +03:00
print("\n");
2020-03-28 04:52:59 +03:00
for (int i = timeout; i; i--) {
2020-04-21 19:27:05 +03:00
print("\rBooting in %d (press any key for boot menu, command line edit)...", i);
2020-01-25 05:06:56 +03:00
if (pit_sleep_and_quit_on_keypress(18)) {
2020-04-21 19:27:05 +03:00
boot_menu();
goto got_entry;
2020-01-25 05:06:56 +03:00
}
}
2020-04-15 14:21:44 +03:00
print("\n\n");
2020-01-25 05:06:56 +03:00
2020-04-21 19:27:05 +03:00
if (config_set_entry(0) == -1) {
panic("Invalid config entry.");
}
2020-04-24 19:26:08 +03:00
if (!config_get_value(cmdline, 0, CMDLINE_MAX, "KERNEL_CMDLINE")) {
cmdline[0] = '\0';
}
2020-04-22 18:47:25 +03:00
2020-04-21 19:27:05 +03:00
got_entry:
2020-05-03 00:38:57 +03:00
init_e820();
2020-06-05 18:51:33 +03:00
init_memmap();
2020-05-03 00:38:57 +03:00
2020-05-06 17:38:45 +03:00
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")) {
2020-05-06 17:38:45 +03:00
stivale_load(cmdline, boot_drive);
2020-04-19 11:14:49 +03:00
} else if (!strcmp(proto, "linux")) {
2020-05-06 17:38:45 +03:00
linux_load(cmdline, boot_drive);
2020-05-06 18:00:41 +03:00
} else if (!strcmp(proto, "chainload")) {
chainload();
} else {
2020-04-24 19:26:08 +03:00
panic("Invalid protocol specified");
}
2020-04-15 14:21:44 +03:00
}