2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2019-05-31 06:47:13 +03:00
|
|
|
#include <lib/real.h>
|
2020-01-22 07:02:12 +03:00
|
|
|
#include <lib/blib.h>
|
2020-03-25 23:05:14 +03:00
|
|
|
#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-11-15 19:56:10 +03:00
|
|
|
#include <lib/trace.h>
|
2020-09-18 21:02:10 +03:00
|
|
|
#include <sys/e820.h>
|
2020-10-15 12:35:49 +03:00
|
|
|
#include <sys/a20.h>
|
2021-09-21 18:28:32 +03:00
|
|
|
#include <sys/idt.h>
|
2021-11-25 23:51:41 +03:00
|
|
|
#include <sys/gdt.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>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-03-25 23:05:14 +03:00
|
|
|
#include <protos/stivale.h>
|
2020-08-11 18:43:39 +03:00
|
|
|
#include <protos/stivale2.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>
|
2021-06-29 16:16:25 +03:00
|
|
|
#include <protos/multiboot1.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <protos/multiboot2.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
#include <menu.h>
|
2020-11-05 03:37:45 +03:00
|
|
|
#include <pxe/pxe.h>
|
|
|
|
#include <pxe/tftp.h>
|
2021-03-04 03:42:25 +03:00
|
|
|
#include <drivers/disk.h>
|
2021-09-21 12:11:52 +03:00
|
|
|
#include <sys/lapic.h>
|
2021-11-29 00:30:05 +03:00
|
|
|
#include <lib/readline.h>
|
2020-04-21 19:27:05 +03:00
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
void stage3_common(void);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-05-20 02:16:39 +03:00
|
|
|
__attribute__((naked))
|
2021-05-29 19:07:28 +03:00
|
|
|
EFI_STATUS efi_main(
|
|
|
|
__attribute__((unused)) EFI_HANDLE ImageHandle,
|
|
|
|
__attribute__((unused)) EFI_SYSTEM_TABLE *SystemTable) {
|
2021-05-06 05:31:05 +03:00
|
|
|
// Invalid return address of 0 to end stacktraces here
|
2021-07-20 14:35:43 +03:00
|
|
|
#if defined (__x86_64__)
|
2021-05-20 02:16:39 +03:00
|
|
|
asm (
|
2021-05-29 19:07:28 +03:00
|
|
|
"xorl %eax, %eax\n\t"
|
2021-07-20 14:35:43 +03:00
|
|
|
"movq %rax, (%rsp)\n\t"
|
2021-05-06 05:31:05 +03:00
|
|
|
"jmp uefi_entry\n\t"
|
|
|
|
);
|
2021-07-20 14:35:43 +03:00
|
|
|
#elif defined (__i386__)
|
|
|
|
asm (
|
|
|
|
"xorl %eax, %eax\n\t"
|
|
|
|
"movl %eax, (%esp)\n\t"
|
|
|
|
"jmp uefi_entry\n\t"
|
|
|
|
);
|
|
|
|
#endif
|
2021-05-06 05:31:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noreturn))
|
|
|
|
void uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
|
2021-03-02 12:23:43 +03:00
|
|
|
gST = SystemTable;
|
|
|
|
gBS = SystemTable->BootServices;
|
|
|
|
gRT = SystemTable->RuntimeServices;
|
2021-03-05 06:20:58 +03:00
|
|
|
efi_image_handle = ImageHandle;
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2021-09-21 19:56:20 +03:00
|
|
|
status = gBS->SetWatchdogTimer(0, 0x10000, 0, NULL);
|
|
|
|
if (status) {
|
2021-10-09 14:32:57 +03:00
|
|
|
term_vbe(0, 0);
|
|
|
|
early_term = true;
|
2021-09-21 19:56:20 +03:00
|
|
|
print("WARNING: Failed to disable watchdog timer!\n");
|
|
|
|
}
|
|
|
|
|
2021-11-20 23:47:51 +03:00
|
|
|
term_notready();
|
|
|
|
|
2021-03-05 08:10:58 +03:00
|
|
|
init_memmap();
|
|
|
|
|
2021-11-25 23:51:41 +03:00
|
|
|
init_gdt();
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
disk_create_index();
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-09-02 03:11:15 +03:00
|
|
|
boot_volume = NULL;
|
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
EFI_HANDLE current_handle = ImageHandle;
|
|
|
|
for (;;) {
|
2021-09-02 03:11:15 +03:00
|
|
|
if (current_handle == NULL) {
|
2021-10-09 14:32:57 +03:00
|
|
|
term_vbe(0, 0);
|
|
|
|
early_term = true;
|
|
|
|
|
2021-09-02 03:11:15 +03:00
|
|
|
print("WARNING: Could not meaningfully match the boot device handle with a volume.\n");
|
|
|
|
print(" Using the first volume containing a Limine configuration!\n");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < volume_index_i; i++) {
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *f;
|
2021-09-02 03:11:15 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if ((f = fopen(volume_index[i], "/limine.cfg")) == NULL
|
|
|
|
&& (f = fopen(volume_index[i], "/boot/limine.cfg")) == NULL
|
|
|
|
&& (f = fopen(volume_index[i], "/EFI/BOOT/limine.cfg")) == NULL) {
|
2021-09-02 03:11:15 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2021-09-02 03:11:15 +03:00
|
|
|
if (volume_index[i]->backing_dev != NULL) {
|
|
|
|
boot_volume = volume_index[i]->backing_dev;
|
|
|
|
} else {
|
|
|
|
boot_volume = volume_index[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (boot_volume != NULL)
|
|
|
|
stage3_common();
|
|
|
|
|
|
|
|
panic("No volume contained a Limine configuration file");
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
EFI_GUID loaded_img_prot_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL;
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-08-25 23:03:00 +03:00
|
|
|
status = gBS->HandleProtocol(current_handle, &loaded_img_prot_guid,
|
|
|
|
(void **)&loaded_image);
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
if (status) {
|
2021-05-20 02:16:39 +03:00
|
|
|
panic("HandleProtocol failure (%x)", status);
|
2021-05-06 05:31:05 +03:00
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle);
|
2021-04-11 02:26:50 +03:00
|
|
|
|
2021-05-06 05:31:05 +03:00
|
|
|
if (boot_volume != NULL)
|
|
|
|
stage3_common();
|
|
|
|
|
|
|
|
current_handle = loaded_image->ParentHandle;
|
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if bios == 1
|
2021-02-25 13:28:14 +03:00
|
|
|
__attribute__((section(".stage3_entry")))
|
2021-03-04 11:22:08 +03:00
|
|
|
#endif
|
|
|
|
__attribute__((noreturn))
|
2021-03-11 02:23:44 +03:00
|
|
|
void stage3_common(void) {
|
2021-03-04 11:15:10 +03:00
|
|
|
volume_iterate_parts(boot_volume,
|
|
|
|
if (!init_config_disk(_PART)) {
|
2021-03-11 02:23:44 +03:00
|
|
|
boot_volume = _PART;
|
2021-02-26 03:30:27 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
);
|
2021-02-26 03:30:27 +03:00
|
|
|
|
2021-11-20 23:47:51 +03:00
|
|
|
char *quiet_str = config_get_value(NULL, 0, "QUIET");
|
|
|
|
quiet = quiet_str != NULL && strcmp(quiet_str, "yes") == 0;
|
|
|
|
|
2021-05-11 07:46:42 +03:00
|
|
|
char *verbose_str = config_get_value(NULL, 0, "VERBOSE");
|
|
|
|
verbose = verbose_str != NULL && strcmp(verbose_str, "yes") == 0;
|
|
|
|
|
2021-07-09 15:40:44 +03:00
|
|
|
char *randomise_mem_str = config_get_value(NULL, 0, "RANDOMISE_MEMORY");
|
|
|
|
if (randomise_mem_str == NULL)
|
|
|
|
randomise_mem_str = config_get_value(NULL, 0, "RANDOMIZE_MEMORY");
|
|
|
|
bool randomise_mem = randomise_mem_str != NULL && strcmp(randomise_mem_str, "yes") == 0;
|
|
|
|
if (randomise_mem)
|
|
|
|
pmm_randomise_memory();
|
|
|
|
|
2021-09-21 19:40:23 +03:00
|
|
|
init_flush_irqs();
|
|
|
|
init_io_apics();
|
|
|
|
|
2021-05-11 07:46:42 +03:00
|
|
|
if (verbose) {
|
2021-10-11 22:08:32 +03:00
|
|
|
print("Boot drive: %d\n", boot_volume->index);
|
2021-05-11 07:46:42 +03:00
|
|
|
print("Boot partition: %d\n", boot_volume->partition);
|
|
|
|
}
|
2021-03-04 11:22:08 +03:00
|
|
|
|
2021-11-29 00:30:05 +03:00
|
|
|
bool disable_timeout = false;
|
|
|
|
|
|
|
|
menu_again:;
|
2020-11-17 01:31:03 +03:00
|
|
|
char *cmdline;
|
2021-11-29 00:30:05 +03:00
|
|
|
char *config = menu(&cmdline, disable_timeout);
|
2020-09-02 10:55:56 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *proto = config_get_value(config, 0, "PROTOCOL");
|
|
|
|
if (proto == NULL) {
|
2021-11-24 15:23:02 +03:00
|
|
|
printv("PROTOCOL not specified, using autodetection...\n");
|
|
|
|
autodetect:
|
|
|
|
stivale2_load(config, cmdline);
|
|
|
|
stivale_load(config, cmdline);
|
|
|
|
multiboot2_load(config, cmdline);
|
|
|
|
multiboot1_load(config, cmdline);
|
|
|
|
linux_load(config, cmdline);
|
|
|
|
panic("Kernel protocol autodetection failed");
|
2020-04-25 21:33:26 +03:00
|
|
|
}
|
2020-01-22 03:55:40 +03:00
|
|
|
|
2021-11-24 15:23:02 +03:00
|
|
|
bool ret = true;
|
|
|
|
|
2021-10-31 21:09:21 +03:00
|
|
|
if (!strcmp(proto, "stivale1") || !strcmp(proto, "stivale")) {
|
2021-11-24 15:23:02 +03:00
|
|
|
ret = stivale_load(config, cmdline);
|
2020-09-02 11:31:39 +03:00
|
|
|
} else if (!strcmp(proto, "stivale2")) {
|
2021-11-24 15:23:02 +03:00
|
|
|
ret = stivale2_load(config, cmdline);
|
2020-09-02 11:31:39 +03:00
|
|
|
} else if (!strcmp(proto, "linux")) {
|
2021-11-24 15:23:02 +03:00
|
|
|
ret = linux_load(config, cmdline);
|
2021-06-29 17:04:12 +03:00
|
|
|
} else if (!strcmp(proto, "multiboot1") || !strcmp(proto, "multiboot")) {
|
2021-11-24 15:23:02 +03:00
|
|
|
ret = multiboot1_load(config, cmdline);
|
2021-09-09 11:40:07 +03:00
|
|
|
} else if (!strcmp(proto, "multiboot2")) {
|
2021-11-24 15:23:02 +03:00
|
|
|
ret = multiboot2_load(config, cmdline);
|
|
|
|
} else if (!strcmp(proto, "chainload")) {
|
|
|
|
chainload(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
print("WARNING: Unsupported protocol specified: %s.\n", proto);
|
|
|
|
} else {
|
|
|
|
print("WARNING: Incorrect protocol specified for kernel.\n");
|
2020-03-25 23:05:14 +03:00
|
|
|
}
|
2021-02-25 13:28:14 +03:00
|
|
|
|
2021-11-29 00:30:05 +03:00
|
|
|
print(" Press A to attempt autodetection or any other key to return to menu.\n");
|
|
|
|
|
|
|
|
int c = getchar();
|
|
|
|
if (c == 'a' || c == 'A') {
|
|
|
|
goto autodetect;
|
|
|
|
} else {
|
|
|
|
disable_timeout = true;
|
|
|
|
goto menu_again;
|
|
|
|
}
|
2020-04-15 14:21:44 +03:00
|
|
|
}
|