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>
|
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>
|
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>
|
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-03-02 12:23:43 +03:00
|
|
|
#if defined (uefi)
|
2021-03-04 09:18:00 +03:00
|
|
|
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
|
2021-04-11 00:08:32 +03:00
|
|
|
// Invalid return address of 0 to end stacktraces here
|
|
|
|
asm volatile (
|
|
|
|
"push 0\n\t"
|
|
|
|
"jmp efi_entry_point\n\t"
|
|
|
|
:: "D" (ImageHandle), "S" (SystemTable)
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((noreturn))
|
|
|
|
void efi_entry_point(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-03-05 08:10:58 +03:00
|
|
|
init_memmap();
|
|
|
|
|
2021-04-11 00:08:32 +03:00
|
|
|
term_vbe(0, 0);
|
|
|
|
early_term = true;
|
2021-03-10 07:08:37 +03:00
|
|
|
|
2021-03-14 08:13:19 +03:00
|
|
|
print("Limine " LIMINE_VERSION "\n\n");
|
2021-03-10 07:08:37 +03:00
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
disk_create_index();
|
2021-03-04 00:38:28 +03:00
|
|
|
|
|
|
|
EFI_GUID loaded_img_prot_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
|
|
|
|
EFI_LOADED_IMAGE_PROTOCOL *loaded_image = NULL;
|
|
|
|
|
|
|
|
uefi_call_wrapper(gBS->HandleProtocol, 3, ImageHandle, &loaded_img_prot_guid,
|
|
|
|
&loaded_image);
|
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle);
|
2021-03-04 11:15:10 +03:00
|
|
|
if (boot_volume == NULL) {
|
2021-03-04 03:42:25 +03:00
|
|
|
panic("Can't determine boot disk");
|
|
|
|
}
|
2021-03-04 00:38:28 +03:00
|
|
|
|
2021-04-11 00:08:32 +03:00
|
|
|
stage3_common();
|
2021-03-02 12:23:43 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-03-04 00:38:28 +03:00
|
|
|
#if defined (bios)
|
2021-03-02 01:38:55 +03:00
|
|
|
__attribute__((section(".stage3_build_id")))
|
|
|
|
uint64_t stage3_build_id = BUILD_ID;
|
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:22:08 +03:00
|
|
|
bool got_config = false;
|
2021-03-04 11:15:10 +03:00
|
|
|
volume_iterate_parts(boot_volume,
|
|
|
|
if (!init_config_disk(_PART)) {
|
|
|
|
print("Config file found and loaded.\n");
|
2021-03-11 02:23:44 +03:00
|
|
|
boot_volume = _PART;
|
2021-03-04 11:22:08 +03:00
|
|
|
got_config = true;
|
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-03-04 11:22:08 +03:00
|
|
|
if (!got_config)
|
|
|
|
panic("Config file not found.");
|
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
print("Boot drive: %x\n", boot_volume->drive);
|
|
|
|
print("Boot partition: %d\n", boot_volume->partition);
|
2021-03-04 11:22:08 +03:00
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
char *cmdline;
|
|
|
|
char *config = menu(&cmdline);
|
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) {
|
2020-11-01 12:31:35 +03:00
|
|
|
panic("PROTOCOL not specified");
|
2020-04-25 21:33:26 +03:00
|
|
|
}
|
2020-01-22 03:55:40 +03:00
|
|
|
|
2021-03-13 16:26:27 +03:00
|
|
|
if (!strcmp(proto, "stivale")) {
|
2020-11-17 01:31:03 +03:00
|
|
|
stivale_load(config, cmdline);
|
2020-09-02 11:31:39 +03:00
|
|
|
} else if (!strcmp(proto, "stivale2")) {
|
2021-03-14 08:13:19 +03:00
|
|
|
#if defined (bios)
|
|
|
|
void *efi_system_table = NULL;
|
|
|
|
#elif defined (uefi)
|
|
|
|
void *efi_system_table = gST;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
stivale2_load(config, cmdline, boot_volume->pxe, efi_system_table);
|
2020-09-02 11:31:39 +03:00
|
|
|
} else if (!strcmp(proto, "linux")) {
|
2020-11-17 01:31:03 +03:00
|
|
|
linux_load(config, cmdline);
|
2020-09-02 11:31:39 +03:00
|
|
|
} else if (!strcmp(proto, "chainload")) {
|
2021-03-13 16:26:27 +03:00
|
|
|
#if defined (bios)
|
2020-11-17 01:31:03 +03:00
|
|
|
chainload(config);
|
2021-03-13 16:26:27 +03:00
|
|
|
#elif defined (uefi)
|
|
|
|
panic("UEFI Limine does not support the chainload boot protocol");
|
2021-03-04 00:38:28 +03:00
|
|
|
#endif
|
2020-03-25 23:05:14 +03:00
|
|
|
}
|
2021-02-25 13:28:14 +03:00
|
|
|
|
2021-03-04 00:38:28 +03:00
|
|
|
panic("Invalid protocol specified");
|
2020-04-15 14:21:44 +03:00
|
|
|
}
|