2021-12-31 10:58:05 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdnoreturn.h>
|
2020-09-02 09:55:56 +02:00
|
|
|
#include <lib/term.h>
|
2019-05-31 05:47:13 +02:00
|
|
|
#include <lib/real.h>
|
2022-08-26 23:44:47 +02:00
|
|
|
#include <lib/misc.h>
|
2020-03-25 21:05:14 +01:00
|
|
|
#include <lib/libc.h>
|
2020-04-15 13:21:44 +02:00
|
|
|
#include <lib/part.h>
|
2020-01-22 07:13:19 +01:00
|
|
|
#include <lib/config.h>
|
2020-11-15 17:56:10 +01:00
|
|
|
#include <lib/trace.h>
|
2020-09-18 20:02:10 +02:00
|
|
|
#include <sys/e820.h>
|
2020-10-15 11:35:49 +02:00
|
|
|
#include <sys/a20.h>
|
2021-09-21 17:28:32 +02:00
|
|
|
#include <sys/idt.h>
|
2021-11-25 21:51:41 +01:00
|
|
|
#include <sys/gdt.h>
|
2020-05-10 00:38:27 +02:00
|
|
|
#include <lib/print.h>
|
2020-04-14 05:20:55 +02:00
|
|
|
#include <fs/file.h>
|
2020-03-25 01:04:18 +01:00
|
|
|
#include <lib/elf.h>
|
2020-09-20 12:03:44 +02:00
|
|
|
#include <mm/pmm.h>
|
2020-06-05 20:27:52 +02:00
|
|
|
#include <menu.h>
|
2020-11-05 01:37:45 +01:00
|
|
|
#include <pxe/pxe.h>
|
|
|
|
#include <pxe/tftp.h>
|
2021-03-04 01:42:25 +01:00
|
|
|
#include <drivers/disk.h>
|
2021-09-21 11:11:52 +02:00
|
|
|
#include <sys/lapic.h>
|
2024-04-20 01:32:34 +02:00
|
|
|
#include <lib/getchar.h>
|
2023-09-13 09:35:30 -05:00
|
|
|
#include <sys/cpu.h>
|
2020-04-21 18:27:05 +02:00
|
|
|
|
2021-03-11 00:23:44 +01:00
|
|
|
void stage3_common(void);
|
2021-03-03 22:38:28 +01:00
|
|
|
|
2022-09-02 02:29:12 +02:00
|
|
|
#if defined (UEFI)
|
2023-09-11 04:27:02 -05:00
|
|
|
extern symbol __slide;
|
2024-03-25 20:13:53 +01:00
|
|
|
extern symbol __image_size;
|
|
|
|
extern symbol _start;
|
2022-08-25 21:50:37 +02:00
|
|
|
|
2021-12-31 10:58:05 +01:00
|
|
|
noreturn void uefi_entry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
|
2021-03-02 10:23:43 +01:00
|
|
|
gST = SystemTable;
|
|
|
|
gBS = SystemTable->BootServices;
|
|
|
|
gRT = SystemTable->RuntimeServices;
|
2021-03-05 04:20:58 +01:00
|
|
|
efi_image_handle = ImageHandle;
|
2021-03-02 10:23:43 +01:00
|
|
|
|
2021-05-06 04:31:05 +02:00
|
|
|
EFI_STATUS status;
|
|
|
|
|
2024-03-30 05:46:06 +01:00
|
|
|
const char *deferred_error = NULL;
|
|
|
|
|
2024-03-25 20:13:53 +01:00
|
|
|
#if defined (__x86_64__)
|
|
|
|
if ((uintptr_t)__slide >= 0x100000000) {
|
|
|
|
size_t image_size_pages = ALIGN_UP((size_t)__image_size, 4096) / 4096;
|
|
|
|
size_t new_base;
|
2024-03-30 05:46:06 +01:00
|
|
|
for (new_base = 0x1000; new_base + (size_t)__image_size < 0x100000000; new_base += 0x1000) {
|
2024-03-25 20:13:53 +01:00
|
|
|
EFI_PHYSICAL_ADDRESS _new_base = (EFI_PHYSICAL_ADDRESS)new_base;
|
|
|
|
status = gBS->AllocatePages(AllocateAddress, EfiLoaderData, image_size_pages, &_new_base);
|
|
|
|
if (status == 0) {
|
|
|
|
goto new_base_gotten;
|
|
|
|
}
|
|
|
|
}
|
2024-03-30 05:46:06 +01:00
|
|
|
deferred_error = "Limine does not support being loaded above 4GiB and no alternative loading spot found";
|
|
|
|
goto defer_error;
|
2024-03-25 20:13:53 +01:00
|
|
|
new_base_gotten:
|
|
|
|
memcpy((void *)new_base, __slide, (size_t)__image_size);
|
|
|
|
__attribute__((ms_abi))
|
|
|
|
void (*new_entry_point)(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable);
|
|
|
|
new_entry_point = (void *)(new_base + ((uintptr_t)_start - (uintptr_t)__slide));
|
|
|
|
new_entry_point(ImageHandle, SystemTable);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2024-03-30 05:46:06 +01:00
|
|
|
defer_error:
|
2024-03-30 05:59:25 +01:00
|
|
|
#endif
|
|
|
|
|
2022-10-06 05:29:13 +02:00
|
|
|
gST->ConOut->EnableCursor(gST->ConOut, false);
|
|
|
|
|
2022-11-18 23:59:31 +01:00
|
|
|
init_memmap();
|
|
|
|
|
2022-09-17 10:40:14 +02:00
|
|
|
term_fallback();
|
|
|
|
|
2021-09-21 18:56:20 +02:00
|
|
|
status = gBS->SetWatchdogTimer(0, 0x10000, 0, NULL);
|
|
|
|
if (status) {
|
|
|
|
print("WARNING: Failed to disable watchdog timer!\n");
|
|
|
|
}
|
|
|
|
|
2024-03-30 05:46:06 +01:00
|
|
|
if (deferred_error != NULL) {
|
|
|
|
panic(false, "%s", deferred_error);
|
|
|
|
}
|
|
|
|
|
2022-08-18 17:32:54 +02:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
2021-11-25 21:51:41 +01:00
|
|
|
init_gdt();
|
2022-08-18 17:32:54 +02:00
|
|
|
#endif
|
2021-11-25 21:51:41 +01:00
|
|
|
|
2021-03-04 09:15:10 +01:00
|
|
|
disk_create_index();
|
2021-03-03 22:38:28 +01:00
|
|
|
|
2021-09-02 02:11:15 +02:00
|
|
|
boot_volume = NULL;
|
|
|
|
|
2021-05-06 04:31:05 +02:00
|
|
|
EFI_HANDLE current_handle = ImageHandle;
|
2022-09-18 10:39:39 +02:00
|
|
|
for (size_t j = 0; j < 25; j++) {
|
2021-09-02 02:11:15 +02:00
|
|
|
if (current_handle == NULL) {
|
2022-09-18 10:39:39 +02:00
|
|
|
could_not_match:
|
2021-09-02 02:11:15 +02: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 01:27:05 +02:00
|
|
|
struct file_handle *f;
|
2021-09-02 02:11:15 +02:00
|
|
|
|
2022-09-01 14:02:53 +02:00
|
|
|
bool old_cif = case_insensitive_fopen;
|
|
|
|
case_insensitive_fopen = true;
|
2021-10-21 01:27:05 +02:00
|
|
|
if ((f = fopen(volume_index[i], "/limine.cfg")) == NULL
|
2022-10-06 06:29:11 +02:00
|
|
|
&& (f = fopen(volume_index[i], "/limine/limine.cfg")) == NULL
|
2021-10-21 01:27:05 +02:00
|
|
|
&& (f = fopen(volume_index[i], "/boot/limine.cfg")) == NULL
|
2022-10-06 06:29:11 +02:00
|
|
|
&& (f = fopen(volume_index[i], "/boot/limine/limine.cfg")) == NULL
|
2021-10-21 01:27:05 +02:00
|
|
|
&& (f = fopen(volume_index[i], "/EFI/BOOT/limine.cfg")) == NULL) {
|
2022-09-01 14:02:53 +02:00
|
|
|
case_insensitive_fopen = old_cif;
|
2021-09-02 02:11:15 +02:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-01 14:02:53 +02:00
|
|
|
case_insensitive_fopen = old_cif;
|
2021-09-02 02:11:15 +02:00
|
|
|
|
2021-10-21 01:27:05 +02:00
|
|
|
fclose(f);
|
|
|
|
|
2021-09-02 02:11:15 +02:00
|
|
|
if (volume_index[i]->backing_dev != NULL) {
|
|
|
|
boot_volume = volume_index[i]->backing_dev;
|
|
|
|
} else {
|
|
|
|
boot_volume = volume_index[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-09-17 10:40:14 +02:00
|
|
|
if (boot_volume != NULL) {
|
2021-09-02 02:11:15 +02:00
|
|
|
stage3_common();
|
2022-09-17 10:40:14 +02:00
|
|
|
}
|
2021-09-02 02:11:15 +02:00
|
|
|
|
2021-12-11 19:58:00 +01:00
|
|
|
panic(false, "No volume contained a Limine configuration file");
|
2021-09-02 02:11:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 04:31:05 +02:00
|
|
|
EFI_GUID loaded_img_prot_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL;
|
2021-03-03 22:38:28 +01:00
|
|
|
|
2021-08-25 22:03:00 +02:00
|
|
|
status = gBS->HandleProtocol(current_handle, &loaded_img_prot_guid,
|
|
|
|
(void **)&loaded_image);
|
2021-03-03 22:38:28 +01:00
|
|
|
|
2021-05-06 04:31:05 +02:00
|
|
|
if (status) {
|
2022-10-06 05:58:39 +02:00
|
|
|
goto could_not_match;
|
2021-05-06 04:31:05 +02:00
|
|
|
}
|
2021-03-03 22:38:28 +01:00
|
|
|
|
2021-05-06 04:31:05 +02:00
|
|
|
boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle);
|
2021-04-11 01:26:50 +02:00
|
|
|
|
2022-09-17 10:40:14 +02:00
|
|
|
if (boot_volume != NULL) {
|
2021-05-06 04:31:05 +02:00
|
|
|
stage3_common();
|
2022-09-17 10:40:14 +02:00
|
|
|
}
|
2021-05-06 04:31:05 +02:00
|
|
|
|
|
|
|
current_handle = loaded_image->ParentHandle;
|
|
|
|
}
|
2022-09-18 10:39:39 +02:00
|
|
|
|
|
|
|
goto could_not_match;
|
2021-03-02 10:23:43 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-31 10:58:05 +01:00
|
|
|
noreturn void stage3_common(void) {
|
2022-08-18 17:32:54 +02:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
2021-09-21 18:40:23 +02:00
|
|
|
init_flush_irqs();
|
|
|
|
init_io_apics();
|
2022-08-18 17:32:54 +02:00
|
|
|
#endif
|
2021-09-21 18:40:23 +02:00
|
|
|
|
2023-09-13 09:35:30 -05:00
|
|
|
#if defined (__riscv)
|
|
|
|
#if defined (UEFI)
|
|
|
|
RISCV_EFI_BOOT_PROTOCOL *rv_proto = get_riscv_boot_protocol();
|
|
|
|
if (rv_proto == NULL || rv_proto->GetBootHartId(rv_proto, &bsp_hartid) != EFI_SUCCESS) {
|
|
|
|
panic(false, "failed to get BSP's hartid");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#error riscv: only UEFI is supported
|
|
|
|
#endif
|
|
|
|
init_riscv();
|
|
|
|
#endif
|
|
|
|
|
2023-02-16 03:54:10 +01:00
|
|
|
term_notready();
|
|
|
|
|
2021-12-11 15:34:30 +01:00
|
|
|
menu(true);
|
2020-04-15 13:21:44 +02:00
|
|
|
}
|