2021-12-31 12:58:05 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdnoreturn.h>
|
2021-03-02 01:38:55 +03:00
|
|
|
#include <lib/term.h>
|
|
|
|
#include <lib/real.h>
|
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/part.h>
|
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/trace.h>
|
|
|
|
#include <sys/e820.h>
|
|
|
|
#include <sys/a20.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <fs/file.h>
|
|
|
|
#include <lib/elf.h>
|
|
|
|
#include <mm/pmm.h>
|
|
|
|
#include <protos/stivale.h>
|
|
|
|
#include <protos/stivale2.h>
|
|
|
|
#include <protos/linux.h>
|
|
|
|
#include <protos/chainload.h>
|
|
|
|
#include <menu.h>
|
|
|
|
#include <pxe/pxe.h>
|
|
|
|
#include <pxe/tftp.h>
|
2021-03-04 11:15:10 +03:00
|
|
|
#include <drivers/disk.h>
|
2021-03-13 05:21:01 +03:00
|
|
|
#include <sys/idt.h>
|
2021-11-21 03:18:39 +03:00
|
|
|
#include <sys/cpu.h>
|
2021-03-02 01:38:55 +03:00
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
struct volume *boot_volume;
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if bios == 1
|
2021-03-02 12:23:43 +03:00
|
|
|
|
2021-03-02 01:38:55 +03:00
|
|
|
bool stage3_loaded = false;
|
2021-03-21 11:44:07 +03:00
|
|
|
static bool stage3_found = false;
|
2021-03-02 01:38:55 +03:00
|
|
|
|
|
|
|
extern symbol stage3_addr;
|
|
|
|
extern symbol limine_sys_size;
|
2021-11-08 11:42:41 +03:00
|
|
|
extern symbol build_id_s2;
|
|
|
|
extern symbol build_id_s3;
|
2021-03-02 01:38:55 +03:00
|
|
|
|
|
|
|
static bool stage3_init(struct volume *part) {
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *stage3;
|
2021-03-02 01:38:55 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if ((stage3 = fopen(part, "/limine.sys")) == NULL
|
|
|
|
&& (stage3 = fopen(part, "/boot/limine.sys")) == NULL) {
|
2021-03-02 01:38:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-21 11:44:07 +03:00
|
|
|
stage3_found = true;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if (stage3->size != (size_t)limine_sys_size) {
|
2021-03-02 01:38:55 +03:00
|
|
|
print("limine.sys size incorrect.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fread(stage3, stage3_addr,
|
2021-03-02 01:38:55 +03:00
|
|
|
(uintptr_t)stage3_addr - 0x8000,
|
2021-10-21 02:27:05 +03:00
|
|
|
stage3->size - ((uintptr_t)stage3_addr - 0x8000));
|
|
|
|
|
|
|
|
fclose(stage3);
|
2021-03-02 01:38:55 +03:00
|
|
|
|
2021-11-08 11:42:41 +03:00
|
|
|
if (memcmp(build_id_s2 + 16, build_id_s3 + 16, 20) != 0) {
|
2021-03-02 01:38:55 +03:00
|
|
|
print("limine.sys build ID mismatch.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stage3_loaded = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
enum {
|
2022-02-09 14:45:55 +03:00
|
|
|
BOOTED_FROM_HDD = 0,
|
|
|
|
BOOTED_FROM_PXE = 1,
|
|
|
|
BOOTED_FROM_CD = 2
|
2021-03-11 02:23:44 +03:00
|
|
|
};
|
2021-03-02 01:38:55 +03:00
|
|
|
|
2021-12-31 12:58:05 +03:00
|
|
|
noreturn void entry(uint8_t boot_drive, int boot_from) {
|
2021-09-23 01:17:15 +03:00
|
|
|
// XXX DO NOT MOVE A20 ENABLE CALL
|
|
|
|
if (!a20_enable())
|
2021-12-12 20:25:59 +03:00
|
|
|
panic(false, "Could not enable A20 line");
|
2021-09-23 01:17:15 +03:00
|
|
|
|
2021-04-15 03:21:38 +03:00
|
|
|
init_e820();
|
|
|
|
init_memmap();
|
|
|
|
|
2021-03-13 05:21:01 +03:00
|
|
|
init_idt();
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
disk_create_index();
|
|
|
|
|
2021-03-11 02:23:44 +03:00
|
|
|
if (boot_from == BOOTED_FROM_HDD || boot_from == BOOTED_FROM_CD) {
|
2021-06-12 14:13:19 +03:00
|
|
|
boot_volume = volume_get_by_bios_drive(boot_drive);
|
2021-03-13 11:08:01 +03:00
|
|
|
} else if (boot_from == BOOTED_FROM_PXE) {
|
|
|
|
pxe_init();
|
|
|
|
boot_volume = pxe_bind_volume();
|
2021-03-11 02:23:44 +03:00
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
|
2022-02-09 14:45:55 +03:00
|
|
|
if (boot_volume == NULL) {
|
|
|
|
panic(false, "Could not determine boot drive\n");
|
|
|
|
}
|
|
|
|
|
2021-03-04 11:15:10 +03:00
|
|
|
volume_iterate_parts(boot_volume,
|
|
|
|
if (stage3_init(_PART)) {
|
2021-03-02 01:38:55 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-03-04 11:15:10 +03:00
|
|
|
);
|
|
|
|
|
2021-11-20 23:47:51 +03:00
|
|
|
if (!stage3_found) {
|
2021-03-21 11:44:07 +03:00
|
|
|
print("\n"
|
|
|
|
"!! Stage 3 file not found!\n"
|
|
|
|
"!! Have you copied limine.sys to the root or /boot directories of\n"
|
|
|
|
"!! one of the partitions on the boot device?\n\n");
|
2021-11-20 23:47:51 +03:00
|
|
|
}
|
2021-03-21 11:44:07 +03:00
|
|
|
|
2021-11-20 23:47:51 +03:00
|
|
|
if (!stage3_loaded) {
|
2021-12-12 20:25:59 +03:00
|
|
|
panic(false, "Failed to load stage 3.");
|
2021-11-20 23:47:51 +03:00
|
|
|
}
|
2021-03-02 01:38:55 +03:00
|
|
|
|
2021-12-12 20:25:59 +03:00
|
|
|
stage3_common();
|
2021-03-02 01:38:55 +03:00
|
|
|
}
|
2021-03-02 12:23:43 +03:00
|
|
|
|
|
|
|
#endif
|