rulimine/stage23/entry.s3.c

145 lines
3.7 KiB
C
Raw Normal View History

2020-09-02 10:55:56 +03:00
#include <lib/term.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-11-15 19:56:10 +03:00
#include <lib/trace.h>
2020-09-18 21:02:10 +03:00
#include <sys/e820.h>
#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>
#include <mm/mtrr.h>
#include <protos/stivale.h>
#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>
#include <drivers/disk.h>
2020-04-21 19:27:05 +03:00
2021-03-04 00:38:28 +03:00
void stage3_common(void);
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-03-02 12:23:43 +03:00
gST = SystemTable;
gBS = SystemTable->BootServices;
gRT = SystemTable->RuntimeServices;
2021-03-04 09:18:00 +03:00
print("Limine " LIMINE_VERSION "\n\n", print);
2021-03-04 00:38:28 +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-04 11:15:10 +03:00
struct volume *boot_volume = disk_volume_from_efi_handle(loaded_image->DeviceHandle);
if (boot_volume == NULL) {
panic("Can't determine boot disk");
}
2021-03-04 00:38:28 +03:00
2021-03-04 11:15:10 +03:00
if (boot_volume->backing_dev != NULL) {
boot_volume = boot_volume->backing_dev;
int part_cnt = 0;
for (size_t i = 0; ; i++) {
if (part_cnt > boot_volume->max_partition)
break;
struct volume *volume = volume_get_by_coord(boot_volume->drive, i);
if (volume == NULL)
continue;
part_cnt++;
if (!init_config_disk(volume)) {
print("Config file found and loaded.\n");
boot_partition = i;
boot_drive = boot_volume->drive;
goto config_loaded;
}
}
panic("Config file not found.");
} else {
struct volume *volume = volume_get_by_coord(boot_volume->drive, -1);
if (volume == NULL)
panic("Config file not found.");
if (!init_config_disk(volume)) {
print("Config file found and loaded.\n");
2021-03-04 11:15:10 +03:00
boot_partition = -1;
boot_drive = boot_volume->drive;
goto config_loaded;
}
2021-03-04 11:15:10 +03:00
2021-03-04 00:38:28 +03:00
panic("Config file not found.");
}
2021-03-02 21:11:34 +03:00
2021-03-04 11:15:10 +03:00
config_loaded:
print("Boot drive: %x\n", boot_drive);
print("Boot partition: %d\n", boot_partition);
stage3_common();
2021-03-02 12:23:43 +03:00
}
#endif
2021-03-04 00:38:28 +03:00
#if defined (bios)
__attribute__((section(".stage3_build_id")))
uint64_t stage3_build_id = BUILD_ID;
__attribute__((noreturn))
__attribute__((section(".stage3_entry")))
2021-02-26 03:30:27 +03:00
void stage3_entry(int boot_from) {
2021-03-04 11:15:10 +03:00
(void)boot_from;
mtrr_save();
2021-03-04 11:15:10 +03:00
struct volume *boot_volume = volume_get_by_coord(boot_drive, -1);
volume_iterate_parts(boot_volume,
if (!init_config_disk(_PART)) {
print("Config file found and loaded.\n");
boot_partition = _PARTNO;
boot_drive = _PART->drive;
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 00:38:28 +03:00
stage3_common();
}
#endif
__attribute__((noreturn))
void stage3_common(void) {
char *cmdline;
char *config = menu(&cmdline);
2020-09-02 10:55:56 +03:00
char *proto = config_get_value(config, 0, "PROTOCOL");
if (proto == NULL) {
panic("PROTOCOL not specified");
}
2021-03-04 00:38:28 +03:00
if (0) {
} else if (!strcmp(proto, "stivale")) {
stivale_load(config, cmdline);
} else if (!strcmp(proto, "stivale2")) {
2021-02-25 06:06:14 +03:00
stivale2_load(config, cmdline, booted_from_pxe);
2021-03-04 00:38:28 +03:00
#if defined (bios)
} else if (!strcmp(proto, "linux")) {
linux_load(config, cmdline);
} else if (!strcmp(proto, "chainload")) {
chainload(config);
2021-03-04 00:38:28 +03:00
#endif
}
2021-03-04 00:38:28 +03:00
panic("Invalid protocol specified");
2020-04-15 14:21:44 +03:00
}