2022-03-12 21:40:49 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <protos/stivale.h>
|
|
|
|
#include <protos/stivale2.h>
|
|
|
|
#include <lib/elf.h>
|
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <lib/acpi.h>
|
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/time.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <lib/real.h>
|
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/gterm.h>
|
|
|
|
#include <lib/uri.h>
|
|
|
|
#include <sys/smp.h>
|
|
|
|
#include <sys/cpu.h>
|
|
|
|
#include <sys/gdt.h>
|
|
|
|
#include <lib/fb.h>
|
|
|
|
#include <lib/term.h>
|
|
|
|
#include <sys/pic.h>
|
|
|
|
#include <sys/lapic.h>
|
|
|
|
#include <fs/file.h>
|
|
|
|
#include <mm/pmm.h>
|
|
|
|
#include <stivale2.h>
|
|
|
|
#include <pxe/tftp.h>
|
|
|
|
#include <drivers/edid.h>
|
|
|
|
#include <drivers/vga_textmode.h>
|
|
|
|
#include <lib/rand.h>
|
|
|
|
#define LIMINE_NO_POINTERS
|
|
|
|
#include <protos/limine.h>
|
|
|
|
#include <limine.h>
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
#define MAX_REQUESTS 128
|
2022-03-15 23:34:47 +03:00
|
|
|
#define MAX_MEMMAP 256
|
2022-03-13 07:35:29 +03:00
|
|
|
|
|
|
|
static uint64_t physical_base, virtual_base, slide, direct_map_offset;
|
|
|
|
static size_t requests_count;
|
2022-04-14 21:34:59 +03:00
|
|
|
static void **requests;
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
static uint64_t reported_addr(void *addr) {
|
|
|
|
return (uint64_t)(uintptr_t)addr + direct_map_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct limine_file get_file(struct file_handle *file, char *cmdline) {
|
|
|
|
struct limine_file ret = {0};
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-18 01:46:48 +03:00
|
|
|
if (file->pxe) {
|
2022-04-02 11:28:07 +03:00
|
|
|
ret.media_type = LIMINE_MEDIA_TYPE_TFTP;
|
|
|
|
|
2022-03-22 12:26:41 +03:00
|
|
|
ret.tftp_ip = file->pxe_ip;
|
|
|
|
ret.tftp_port = file->pxe_port;
|
2022-03-26 13:59:27 +03:00
|
|
|
} else {
|
|
|
|
struct volume *vol = file->vol;
|
2022-03-18 01:46:48 +03:00
|
|
|
|
2022-04-02 11:28:07 +03:00
|
|
|
if (vol->is_optical) {
|
|
|
|
ret.media_type = LIMINE_MEDIA_TYPE_OPTICAL;
|
|
|
|
}
|
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
ret.partition_index = vol->partition;
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
ret.mbr_disk_id = mbr_get_id(vol);
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
if (vol->guid_valid) {
|
|
|
|
memcpy(&ret.part_uuid, &vol->guid, sizeof(struct limine_uuid));
|
|
|
|
}
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
if (vol->part_guid_valid) {
|
|
|
|
memcpy(&ret.gpt_part_uuid, &vol->part_guid, sizeof(struct limine_uuid));
|
|
|
|
}
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
struct guid gpt_disk_uuid;
|
|
|
|
if (gpt_get_guid(&gpt_disk_uuid, vol->backing_dev ?: vol) == true) {
|
|
|
|
memcpy(&ret.gpt_disk_uuid, &gpt_disk_uuid, sizeof(struct limine_uuid));
|
|
|
|
}
|
2022-03-16 08:49:41 +03:00
|
|
|
}
|
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
char *path = ext_mem_alloc(strlen(file->path) + 1);
|
|
|
|
strcpy(path, file->path);
|
|
|
|
ret.path = reported_addr(path);
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-31 10:03:06 +03:00
|
|
|
ret.address = reported_addr(freadall(file, MEMMAP_KERNEL_AND_MODULES));
|
|
|
|
ret.size = file->size;
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
ret.cmdline = reported_addr(cmdline);
|
|
|
|
|
|
|
|
return ret;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
2022-03-13 07:35:29 +03:00
|
|
|
|
|
|
|
static void *_get_request(uint64_t id[4]) {
|
|
|
|
for (size_t i = 0; i < requests_count; i++) {
|
|
|
|
uint64_t *p = requests[i];
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
if (p[2] != id[2]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[3] != id[3]) {
|
|
|
|
continue;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
2022-03-13 07:35:29 +03:00
|
|
|
|
|
|
|
return p;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
return NULL;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
#define get_request(REQ) _get_request((uint64_t[4])REQ)
|
|
|
|
|
2022-03-12 21:40:49 +03:00
|
|
|
#define FEAT_START do {
|
|
|
|
#define FEAT_END } while (0);
|
|
|
|
|
2022-03-21 07:15:10 +03:00
|
|
|
#if defined (__i386__)
|
|
|
|
extern symbol stivale2_term_write_entry;
|
|
|
|
extern void *stivale2_rt_stack;
|
|
|
|
extern uint64_t stivale2_term_callback_ptr;
|
2022-04-02 11:05:14 +03:00
|
|
|
extern uint64_t stivale2_term_write_ptr;
|
2022-04-01 11:00:36 +03:00
|
|
|
void stivale2_term_callback(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t);
|
2022-03-21 07:15:10 +03:00
|
|
|
#endif
|
|
|
|
|
2022-04-02 11:05:14 +03:00
|
|
|
static void term_write_shim(uint64_t context, uint64_t buf, uint64_t count) {
|
|
|
|
(void)context;
|
|
|
|
term_write(buf, count);
|
|
|
|
}
|
|
|
|
|
2022-03-12 21:40:49 +03:00
|
|
|
bool limine_load(char *config, char *cmdline) {
|
|
|
|
uint32_t eax, ebx, ecx, edx;
|
|
|
|
|
|
|
|
char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
|
|
|
|
if (kernel_path == NULL)
|
|
|
|
panic(true, "limine: KERNEL_PATH not specified");
|
|
|
|
|
|
|
|
struct file_handle *kernel_file;
|
|
|
|
if ((kernel_file = uri_open(kernel_path)) == NULL)
|
|
|
|
panic(true, "limine: Failed to open kernel with path `%s`. Is the path correct?", kernel_path);
|
|
|
|
|
|
|
|
uint8_t *kernel = freadall(kernel_file, MEMMAP_BOOTLOADER_RECLAIMABLE);
|
|
|
|
|
|
|
|
char *kaslr_s = config_get_value(config, 0, "KASLR");
|
|
|
|
bool kaslr = true;
|
|
|
|
if (kaslr_s != NULL && strcmp(kaslr_s, "no") == 0)
|
|
|
|
kaslr = false;
|
|
|
|
|
|
|
|
int bits = elf_bits(kernel);
|
|
|
|
|
2022-04-02 11:10:38 +03:00
|
|
|
if (bits == -1 || bits == 32) {
|
|
|
|
printv("limine: Kernel in unrecognised format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ELF loading
|
2022-03-12 21:40:49 +03:00
|
|
|
uint64_t entry_point = 0;
|
|
|
|
struct elf_range *ranges;
|
|
|
|
uint64_t ranges_count;
|
|
|
|
|
2022-03-26 07:03:02 +03:00
|
|
|
uint64_t image_size;
|
2022-03-28 06:13:47 +03:00
|
|
|
bool is_reloc;
|
2022-03-26 07:03:02 +03:00
|
|
|
|
2022-04-02 11:10:38 +03:00
|
|
|
if (elf64_load(kernel, &entry_point, NULL, &slide,
|
|
|
|
MEMMAP_KERNEL_AND_MODULES, kaslr, false,
|
|
|
|
&ranges, &ranges_count,
|
|
|
|
true, &physical_base, &virtual_base, &image_size,
|
|
|
|
&is_reloc)) {
|
|
|
|
return false;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-28 06:13:47 +03:00
|
|
|
kaslr = is_reloc;
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
// Load requests
|
2022-04-14 21:34:59 +03:00
|
|
|
if (elf64_load_section(kernel, &requests, ".limine_reqs", 0, slide) == 0) {
|
|
|
|
for (size_t i = 0; ; i++) {
|
|
|
|
if (requests[i] == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
requests[i] -= virtual_base;
|
|
|
|
requests[i] += physical_base;
|
|
|
|
requests_count++;
|
2022-03-13 07:35:29 +03:00
|
|
|
}
|
2022-04-14 21:34:59 +03:00
|
|
|
} else {
|
|
|
|
requests = ext_mem_alloc(MAX_REQUESTS * sizeof(void *));
|
|
|
|
requests_count = 0;
|
|
|
|
uint64_t common_magic[2] = { LIMINE_COMMON_MAGIC };
|
|
|
|
for (size_t i = 0; i < ALIGN_DOWN(image_size, 8); i += 8) {
|
|
|
|
uint64_t *p = (void *)(uintptr_t)physical_base + i;
|
|
|
|
|
|
|
|
if (p[0] != common_magic[0]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p[1] != common_magic[1]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requests_count == MAX_REQUESTS) {
|
|
|
|
panic(true, "limine: Maximum requests exceeded");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a conflict
|
|
|
|
if (_get_request(p) != NULL) {
|
|
|
|
panic(true, "limine: Conflict detected for request ID %X %X", p[2], p[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
requests[requests_count++] = p;
|
2022-03-13 23:20:24 +03:00
|
|
|
}
|
2022-03-13 07:35:29 +03:00
|
|
|
}
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
if (requests_count == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
// Check if 64 bit CPU
|
|
|
|
if (!cpuid(0x80000001, 0, &eax, &ebx, &ecx, &edx) || !(edx & (1 << 29))) {
|
|
|
|
panic(true, "limine: This CPU does not support 64-bit mode.");
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
print("limine: Loading kernel `%s`...\n", kernel_path);
|
|
|
|
|
|
|
|
printv("limine: Physical base: %X\n", physical_base);
|
|
|
|
printv("limine: Virtual base: %X\n", virtual_base);
|
|
|
|
printv("limine: Slide: %X\n", slide);
|
|
|
|
printv("limine: ELF entry point: %X\n", entry_point);
|
|
|
|
printv("limine: Requests count: %u\n", requests_count);
|
2022-03-12 21:40:49 +03:00
|
|
|
|
|
|
|
// 5 level paging feature & HHDM slide
|
|
|
|
bool want_5lv;
|
|
|
|
FEAT_START
|
|
|
|
// Check if 5-level paging is available
|
|
|
|
bool level5pg = false;
|
|
|
|
if (cpuid(0x00000007, 0, &eax, &ebx, &ecx, &edx) && (ecx & (1 << 16))) {
|
|
|
|
printv("limine: CPU has 5-level paging support\n");
|
|
|
|
level5pg = true;
|
|
|
|
}
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
struct limine_5_level_paging_request *lv5pg_request = get_request(LIMINE_5_LEVEL_PAGING_REQUEST);
|
|
|
|
want_5lv = lv5pg_request != NULL && level5pg;
|
2022-03-12 21:40:49 +03:00
|
|
|
|
|
|
|
direct_map_offset = want_5lv ? 0xff00000000000000 : 0xffff800000000000;
|
|
|
|
|
|
|
|
if (kaslr) {
|
|
|
|
direct_map_offset += (rand64() & ~((uint64_t)0x40000000 - 1)) & 0xfffffffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want_5lv) {
|
|
|
|
void *lv5pg_response = ext_mem_alloc(sizeof(struct limine_5_level_paging_response));
|
2022-03-13 07:35:29 +03:00
|
|
|
lv5pg_request->response = reported_addr(lv5pg_response);
|
|
|
|
}
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
struct limine_file *kf = ext_mem_alloc(sizeof(struct limine_file));
|
|
|
|
*kf = get_file(kernel_file, cmdline);
|
|
|
|
fclose(kernel_file);
|
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
// Entry point feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_entry_point_request *entrypoint_request = get_request(LIMINE_ENTRY_POINT_REQUEST);
|
|
|
|
if (entrypoint_request == NULL) {
|
|
|
|
break;
|
2022-03-12 21:40:49 +03:00
|
|
|
}
|
2022-03-13 07:35:29 +03:00
|
|
|
|
|
|
|
entry_point = entrypoint_request->entry;
|
|
|
|
|
2022-04-02 11:14:52 +03:00
|
|
|
printv("limine: Entry point at %X\n", entry_point);
|
2022-03-13 07:35:29 +03:00
|
|
|
|
|
|
|
struct limine_entry_point_response *entrypoint_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_entry_point_response));
|
|
|
|
|
|
|
|
entrypoint_request->response = reported_addr(entrypoint_response);
|
2022-03-12 21:40:49 +03:00
|
|
|
FEAT_END
|
|
|
|
|
2022-03-18 05:08:47 +03:00
|
|
|
// Bootloader info feature
|
2022-03-12 21:40:49 +03:00
|
|
|
FEAT_START
|
2022-03-18 05:08:47 +03:00
|
|
|
struct limine_bootloader_info_request *bootloader_info_request = get_request(LIMINE_BOOTLOADER_INFO_REQUEST);
|
|
|
|
if (bootloader_info_request == NULL) {
|
2022-03-12 21:40:49 +03:00
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
2022-03-18 05:08:47 +03:00
|
|
|
struct limine_bootloader_info_response *bootloader_info_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_bootloader_info_response));
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-18 05:08:47 +03:00
|
|
|
bootloader_info_response->name = reported_addr("Limine");
|
|
|
|
bootloader_info_response->version = reported_addr(LIMINE_VERSION);
|
2022-03-12 21:40:49 +03:00
|
|
|
|
2022-03-18 05:08:47 +03:00
|
|
|
bootloader_info_request->response = reported_addr(bootloader_info_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-20 21:01:24 +03:00
|
|
|
// Kernel address feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_kernel_address_request *kernel_address_request = get_request(LIMINE_KERNEL_ADDRESS_REQUEST);
|
|
|
|
if (kernel_address_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_kernel_address_response *kernel_address_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_kernel_address_response));
|
|
|
|
|
|
|
|
kernel_address_response->physical_base = physical_base;
|
|
|
|
kernel_address_response->virtual_base = virtual_base;
|
|
|
|
|
|
|
|
kernel_address_request->response = reported_addr(kernel_address_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-18 05:08:47 +03:00
|
|
|
// HHDM feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_hhdm_request *hhdm_request = get_request(LIMINE_HHDM_REQUEST);
|
|
|
|
if (hhdm_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_hhdm_response *hhdm_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_hhdm_response));
|
|
|
|
|
2022-03-24 04:18:33 +03:00
|
|
|
hhdm_response->offset = direct_map_offset;
|
2022-03-18 05:08:47 +03:00
|
|
|
|
|
|
|
hhdm_request->response = reported_addr(hhdm_response);
|
2022-03-12 21:40:49 +03:00
|
|
|
FEAT_END
|
|
|
|
|
2022-03-17 21:21:35 +03:00
|
|
|
// RSDP feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_rsdp_request *rsdp_request = get_request(LIMINE_RSDP_REQUEST);
|
|
|
|
if (rsdp_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_rsdp_response *rsdp_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_rsdp_response));
|
|
|
|
|
|
|
|
void *rsdp = acpi_get_rsdp();
|
|
|
|
if (rsdp) {
|
|
|
|
rsdp_response->address = reported_addr(rsdp);
|
|
|
|
}
|
|
|
|
|
|
|
|
rsdp_request->response = reported_addr(rsdp_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-17 21:59:03 +03:00
|
|
|
// SMBIOS feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_smbios_request *smbios_request = get_request(LIMINE_SMBIOS_REQUEST);
|
|
|
|
if (smbios_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_smbios_response *smbios_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_smbios_response));
|
|
|
|
|
|
|
|
void *smbios_entry_32 = NULL, *smbios_entry_64 = NULL;
|
|
|
|
acpi_get_smbios(&smbios_entry_32, &smbios_entry_64);
|
|
|
|
|
|
|
|
if (smbios_entry_32) {
|
|
|
|
smbios_response->entry_32 = reported_addr(smbios_entry_32);
|
|
|
|
}
|
|
|
|
if (smbios_entry_64) {
|
|
|
|
smbios_response->entry_64 = reported_addr(smbios_entry_64);
|
|
|
|
}
|
|
|
|
|
|
|
|
smbios_request->response = reported_addr(smbios_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-20 10:05:12 +03:00
|
|
|
|
|
|
|
#if uefi == 1
|
|
|
|
// EFI system table feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_efi_system_table_request *est_request = get_request(LIMINE_EFI_SYSTEM_TABLE_REQUEST);
|
|
|
|
if (est_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_efi_system_table_response *est_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_efi_system_table_response));
|
|
|
|
|
|
|
|
est_response->address = reported_addr(gST);
|
|
|
|
|
|
|
|
est_request->response = reported_addr(est_response);
|
|
|
|
FEAT_END
|
|
|
|
#endif
|
|
|
|
|
2022-03-28 09:00:40 +03:00
|
|
|
// Stack size
|
2022-04-17 08:56:41 +03:00
|
|
|
uint64_t stack_size = 65536;
|
2022-03-28 09:00:40 +03:00
|
|
|
FEAT_START
|
|
|
|
struct limine_stack_size_request *stack_size_request = get_request(LIMINE_STACK_SIZE_REQUEST);
|
|
|
|
if (stack_size_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_stack_size_response *stack_size_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_stack_size_response));
|
|
|
|
|
2022-05-07 18:11:11 +03:00
|
|
|
if (stack_size_request->stack_size > stack_size) {
|
2022-05-05 14:51:31 +03:00
|
|
|
stack_size = stack_size_request->stack_size;
|
|
|
|
}
|
2022-03-28 09:00:40 +03:00
|
|
|
|
|
|
|
stack_size_request->response = reported_addr(stack_size_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
// Kernel file
|
|
|
|
FEAT_START
|
|
|
|
struct limine_kernel_file_request *kernel_file_request = get_request(LIMINE_KERNEL_FILE_REQUEST);
|
|
|
|
if (kernel_file_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_kernel_file_response *kernel_file_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_kernel_file_response));
|
|
|
|
|
|
|
|
kernel_file_response->kernel_file = reported_addr(kf);
|
|
|
|
|
|
|
|
kernel_file_request->response = reported_addr(kernel_file_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-16 08:49:41 +03:00
|
|
|
// Modules
|
|
|
|
FEAT_START
|
|
|
|
struct limine_module_request *module_request = get_request(LIMINE_MODULE_REQUEST);
|
|
|
|
if (module_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t module_count;
|
|
|
|
for (module_count = 0; ; module_count++) {
|
|
|
|
char *module_file = config_get_value(config, module_count, "MODULE_PATH");
|
|
|
|
if (module_file == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-26 13:59:27 +03:00
|
|
|
if (module_count == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:49:41 +03:00
|
|
|
struct limine_module_response *module_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_module_response));
|
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
struct limine_file *modules = ext_mem_alloc(module_count * sizeof(struct limine_file));
|
2022-03-20 21:13:18 +03:00
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
for (size_t i = 0; i < module_count; i++) {
|
2022-03-16 08:49:41 +03:00
|
|
|
struct conf_tuple conf_tuple =
|
2022-03-26 11:27:09 +03:00
|
|
|
config_get_tuple(config, i,
|
2022-03-16 22:33:59 +03:00
|
|
|
"MODULE_PATH", "MODULE_CMDLINE");
|
2022-03-16 08:49:41 +03:00
|
|
|
|
2022-03-20 21:13:18 +03:00
|
|
|
char *module_path = conf_tuple.value1;
|
|
|
|
char *module_cmdline = conf_tuple.value2;
|
|
|
|
|
|
|
|
if (module_cmdline == NULL) {
|
|
|
|
module_cmdline = "";
|
2022-03-16 08:49:41 +03:00
|
|
|
}
|
|
|
|
|
2022-03-20 21:22:38 +03:00
|
|
|
print("limine: Loading module `%s`...\n", module_path);
|
2022-03-16 08:49:41 +03:00
|
|
|
|
|
|
|
struct file_handle *f;
|
2022-03-20 21:13:18 +03:00
|
|
|
if ((f = uri_open(module_path)) == NULL)
|
2022-03-16 08:49:41 +03:00
|
|
|
panic(true, "limine: Failed to open module with path `%s`. Is the path correct?", module_path);
|
|
|
|
|
2022-03-26 11:27:09 +03:00
|
|
|
struct limine_file *l = &modules[i];
|
|
|
|
*l = get_file(f, module_cmdline);
|
2022-03-16 08:49:41 +03:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2022-03-20 22:28:44 +03:00
|
|
|
uint64_t *modules_list = ext_mem_alloc(module_count * sizeof(uint64_t));
|
|
|
|
for (size_t i = 0; i < module_count; i++) {
|
|
|
|
modules_list[i] = reported_addr(&modules[i]);
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:23:47 +03:00
|
|
|
module_response->module_count = module_count;
|
2022-03-20 22:28:44 +03:00
|
|
|
module_response->modules = reported_addr(modules_list);
|
2022-03-16 08:49:41 +03:00
|
|
|
|
|
|
|
module_request->response = reported_addr(module_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-14 14:05:59 +03:00
|
|
|
size_t req_width = 0, req_height = 0, req_bpp = 0;
|
|
|
|
|
|
|
|
char *resolution = config_get_value(config, 0, "RESOLUTION");
|
|
|
|
if (resolution != NULL) {
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, resolution);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fb_info fb;
|
|
|
|
|
2022-03-21 07:15:10 +03:00
|
|
|
// Terminal feature
|
2022-04-01 11:00:36 +03:00
|
|
|
uint64_t *term_fb_ptr = NULL;
|
2022-03-21 07:15:10 +03:00
|
|
|
FEAT_START
|
|
|
|
struct limine_terminal_request *terminal_request = get_request(LIMINE_TERMINAL_REQUEST);
|
|
|
|
if (terminal_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_terminal_response *terminal_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_terminal_response));
|
|
|
|
|
2022-04-01 11:00:36 +03:00
|
|
|
struct limine_terminal *terminal = ext_mem_alloc(sizeof(struct limine_terminal));
|
|
|
|
|
2022-03-21 07:15:10 +03:00
|
|
|
quiet = false;
|
|
|
|
serial = false;
|
|
|
|
|
|
|
|
term_vbe(req_width, req_height);
|
|
|
|
|
|
|
|
if (current_video_mode < 0) {
|
|
|
|
panic(true, "limine: Failed to initialise terminal");
|
|
|
|
}
|
|
|
|
|
|
|
|
fb = fbinfo;
|
|
|
|
|
2022-04-17 08:51:28 +03:00
|
|
|
if (terminal_request->callback != 0) {
|
2022-03-21 07:15:10 +03:00
|
|
|
#if defined (__i386__)
|
2022-04-17 08:51:28 +03:00
|
|
|
term_callback = stivale2_term_callback;
|
|
|
|
stivale2_term_callback_ptr = terminal_request->callback;
|
2022-03-21 07:15:10 +03:00
|
|
|
#elif defined (__x86_64__)
|
2022-04-17 08:51:28 +03:00
|
|
|
term_callback = (void *)terminal_request->callback;
|
2022-03-21 07:15:10 +03:00
|
|
|
#endif
|
2022-04-17 08:51:28 +03:00
|
|
|
}
|
2022-03-21 07:15:10 +03:00
|
|
|
|
2022-04-01 11:00:36 +03:00
|
|
|
term_arg = reported_addr(terminal);
|
|
|
|
|
2022-03-21 07:15:10 +03:00
|
|
|
#if defined (__i386__)
|
|
|
|
if (stivale2_rt_stack == NULL) {
|
2022-03-26 13:59:27 +03:00
|
|
|
stivale2_rt_stack = ext_mem_alloc(16384) + 16384;
|
2022-03-21 07:15:10 +03:00
|
|
|
}
|
|
|
|
|
2022-04-02 11:05:14 +03:00
|
|
|
stivale2_term_write_ptr = (uintptr_t)term_write_shim;
|
|
|
|
terminal_response->write = (uintptr_t)(void *)stivale2_term_write_entry;
|
2022-03-21 07:15:10 +03:00
|
|
|
#elif defined (__x86_64__)
|
2022-04-02 11:05:14 +03:00
|
|
|
terminal_response->write = (uintptr_t)term_write_shim;
|
2022-03-21 07:15:10 +03:00
|
|
|
#endif
|
|
|
|
|
2022-04-01 11:00:36 +03:00
|
|
|
term_fb_ptr = &terminal->framebuffer;
|
|
|
|
|
|
|
|
terminal->columns = term_cols;
|
|
|
|
terminal->rows = term_rows;
|
|
|
|
|
|
|
|
uint64_t *term_list = ext_mem_alloc(1 * sizeof(uint64_t));
|
|
|
|
term_list[0] = reported_addr(terminal);
|
|
|
|
|
|
|
|
terminal_response->terminal_count = 1;
|
|
|
|
terminal_response->terminals = reported_addr(term_list);
|
2022-03-21 07:15:10 +03:00
|
|
|
|
|
|
|
terminal_request->response = reported_addr(terminal_response);
|
|
|
|
|
|
|
|
goto skip_fb_init;
|
|
|
|
FEAT_END
|
|
|
|
|
|
|
|
// Framebuffer feature
|
|
|
|
FEAT_START
|
|
|
|
term_deinit();
|
|
|
|
|
2022-03-14 14:05:59 +03:00
|
|
|
if (!fb_init(&fb, req_width, req_height, req_bpp)) {
|
|
|
|
panic(true, "limine: Could not acquire framebuffer");
|
|
|
|
}
|
|
|
|
|
2022-03-21 07:15:10 +03:00
|
|
|
skip_fb_init:;
|
2022-04-15 16:13:40 +03:00
|
|
|
memmap_alloc_range(fb.framebuffer_addr,
|
|
|
|
(uint64_t)fb.framebuffer_pitch * fb.framebuffer_height,
|
|
|
|
MEMMAP_FRAMEBUFFER, false, false, false, true);
|
|
|
|
|
2022-05-13 20:14:10 +03:00
|
|
|
// For now we only support 1 framebuffer
|
|
|
|
struct limine_framebuffer *fbp = ext_mem_alloc(sizeof(struct limine_framebuffer));
|
|
|
|
|
|
|
|
if (term_fb_ptr != NULL) {
|
|
|
|
*term_fb_ptr = reported_addr(fbp);
|
|
|
|
}
|
|
|
|
|
2022-03-14 14:05:59 +03:00
|
|
|
struct limine_framebuffer_request *framebuffer_request = get_request(LIMINE_FRAMEBUFFER_REQUEST);
|
|
|
|
if (framebuffer_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_framebuffer_response *framebuffer_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_framebuffer_response));
|
|
|
|
|
2022-03-18 04:45:37 +03:00
|
|
|
struct edid_info_struct *edid_info = get_edid_info();
|
|
|
|
if (edid_info != NULL) {
|
2022-03-20 21:11:22 +03:00
|
|
|
fbp->edid_size = sizeof(struct edid_info_struct);
|
|
|
|
fbp->edid = reported_addr(edid_info);
|
2022-03-18 04:45:37 +03:00
|
|
|
}
|
|
|
|
|
2022-03-20 21:11:22 +03:00
|
|
|
fbp->memory_model = LIMINE_FRAMEBUFFER_RGB;
|
|
|
|
fbp->address = reported_addr((void *)(uintptr_t)fb.framebuffer_addr);
|
|
|
|
fbp->width = fb.framebuffer_width;
|
|
|
|
fbp->height = fb.framebuffer_height;
|
|
|
|
fbp->bpp = fb.framebuffer_bpp;
|
|
|
|
fbp->pitch = fb.framebuffer_pitch;
|
|
|
|
fbp->red_mask_size = fb.red_mask_size;
|
|
|
|
fbp->red_mask_shift = fb.red_mask_shift;
|
|
|
|
fbp->green_mask_size = fb.green_mask_size;
|
|
|
|
fbp->green_mask_shift = fb.green_mask_shift;
|
|
|
|
fbp->blue_mask_size = fb.blue_mask_size;
|
|
|
|
fbp->blue_mask_shift = fb.blue_mask_shift;
|
2022-03-14 14:05:59 +03:00
|
|
|
|
2022-03-20 22:28:44 +03:00
|
|
|
uint64_t *fb_list = ext_mem_alloc(1 * sizeof(uint64_t));
|
|
|
|
fb_list[0] = reported_addr(fbp);
|
|
|
|
|
2022-03-20 23:23:47 +03:00
|
|
|
framebuffer_response->framebuffer_count = 1;
|
|
|
|
framebuffer_response->framebuffers = reported_addr(fb_list);
|
2022-03-20 22:28:44 +03:00
|
|
|
|
2022-03-14 14:05:59 +03:00
|
|
|
framebuffer_request->response = reported_addr(framebuffer_response);
|
2022-03-20 10:19:32 +03:00
|
|
|
FEAT_END
|
|
|
|
|
|
|
|
// Boot time feature
|
|
|
|
FEAT_START
|
|
|
|
struct limine_boot_time_request *boot_time_request = get_request(LIMINE_BOOT_TIME_REQUEST);
|
|
|
|
if (boot_time_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_boot_time_response *boot_time_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_boot_time_response));
|
|
|
|
|
|
|
|
boot_time_response->boot_time = time();
|
|
|
|
|
|
|
|
boot_time_request->response = reported_addr(boot_time_response);
|
2022-03-12 21:40:49 +03:00
|
|
|
FEAT_END
|
|
|
|
|
|
|
|
// Wrap-up stuff before memmap close
|
|
|
|
struct gdtr *local_gdt = ext_mem_alloc(sizeof(struct gdtr));
|
|
|
|
local_gdt->limit = gdt.limit;
|
|
|
|
uint64_t local_gdt_base = (uint64_t)gdt.ptr;
|
|
|
|
local_gdt_base += direct_map_offset;
|
|
|
|
local_gdt->ptr = local_gdt_base;
|
|
|
|
#if defined (__i386__)
|
|
|
|
local_gdt->ptr_hi = local_gdt_base >> 32;
|
|
|
|
#endif
|
|
|
|
|
2022-03-28 09:00:40 +03:00
|
|
|
void *stack = ext_mem_alloc(stack_size) + stack_size;
|
2022-03-12 21:40:49 +03:00
|
|
|
|
|
|
|
pagemap_t pagemap = {0};
|
|
|
|
pagemap = stivale_build_pagemap(want_5lv, true, ranges, ranges_count, true,
|
|
|
|
physical_base, virtual_base, direct_map_offset);
|
|
|
|
|
2022-03-17 01:23:04 +03:00
|
|
|
#if uefi == 1
|
|
|
|
efi_exit_boot_services();
|
|
|
|
#endif
|
|
|
|
|
2022-03-18 03:17:56 +03:00
|
|
|
// SMP
|
|
|
|
FEAT_START
|
|
|
|
struct limine_smp_request *smp_request = get_request(LIMINE_SMP_REQUEST);
|
|
|
|
if (smp_request == NULL) {
|
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
2022-03-20 21:12:55 +03:00
|
|
|
struct limine_smp_info *smp_array;
|
2022-03-18 03:17:56 +03:00
|
|
|
struct smp_information *smp_info;
|
|
|
|
size_t cpu_count;
|
|
|
|
uint32_t bsp_lapic_id;
|
2022-03-20 21:12:55 +03:00
|
|
|
smp_info = init_smp(0, (void **)&smp_array,
|
2022-03-18 03:17:56 +03:00
|
|
|
&cpu_count, &bsp_lapic_id,
|
|
|
|
true, want_5lv,
|
2022-03-18 03:47:04 +03:00
|
|
|
pagemap, smp_request->flags & LIMINE_SMP_X2APIC, true,
|
2022-03-31 14:33:18 +03:00
|
|
|
direct_map_offset, true);
|
2022-03-18 03:17:56 +03:00
|
|
|
|
|
|
|
if (smp_info == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < cpu_count; i++) {
|
2022-03-28 09:00:40 +03:00
|
|
|
void *cpu_stack = ext_mem_alloc(stack_size) + stack_size;
|
|
|
|
smp_info[i].stack_addr = reported_addr(cpu_stack + stack_size);
|
2022-03-18 03:17:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct limine_smp_response *smp_response =
|
|
|
|
ext_mem_alloc(sizeof(struct limine_smp_response));
|
|
|
|
|
|
|
|
smp_response->flags |= (smp_request->flags & LIMINE_SMP_X2APIC) && x2apic_check();
|
|
|
|
smp_response->bsp_lapic_id = bsp_lapic_id;
|
2022-03-20 22:28:44 +03:00
|
|
|
|
|
|
|
uint64_t *smp_list = ext_mem_alloc(cpu_count * sizeof(uint64_t));
|
|
|
|
for (size_t i = 0; i < cpu_count; i++) {
|
|
|
|
smp_list[i] = reported_addr(&smp_array[i]);
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:23:47 +03:00
|
|
|
smp_response->cpu_count = cpu_count;
|
2022-03-20 22:28:44 +03:00
|
|
|
smp_response->cpus = reported_addr(smp_list);
|
2022-03-18 03:17:56 +03:00
|
|
|
|
|
|
|
smp_request->response = reported_addr(smp_response);
|
|
|
|
FEAT_END
|
|
|
|
|
2022-03-12 21:40:49 +03:00
|
|
|
// Memmap
|
|
|
|
FEAT_START
|
2022-03-13 07:35:29 +03:00
|
|
|
struct limine_memmap_request *memmap_request = get_request(LIMINE_MEMMAP_REQUEST);
|
2022-03-12 23:41:36 +03:00
|
|
|
struct limine_memmap_response *memmap_response;
|
2022-03-20 21:13:18 +03:00
|
|
|
struct limine_memmap_entry *_memmap;
|
2022-03-20 22:28:44 +03:00
|
|
|
uint64_t *memmap_list;
|
2022-03-12 23:41:36 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
if (memmap_request != NULL) {
|
2022-03-12 23:41:36 +03:00
|
|
|
memmap_response = ext_mem_alloc(sizeof(struct limine_memmap_response));
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap = ext_mem_alloc(sizeof(struct limine_memmap_entry) * MAX_MEMMAP);
|
2022-03-20 22:28:44 +03:00
|
|
|
memmap_list = ext_mem_alloc(MAX_MEMMAP * sizeof(uint64_t));
|
2022-03-12 23:41:36 +03:00
|
|
|
}
|
|
|
|
|
2022-03-12 21:40:49 +03:00
|
|
|
size_t mmap_entries;
|
|
|
|
struct e820_entry_t *mmap = get_memmap(&mmap_entries);
|
2022-03-12 23:41:36 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
if (memmap_request == NULL) {
|
2022-03-12 23:41:36 +03:00
|
|
|
break; // next feature
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:34:47 +03:00
|
|
|
if (mmap_entries > MAX_MEMMAP) {
|
|
|
|
panic(false, "limine: Too many memmap entries");
|
|
|
|
}
|
|
|
|
|
2022-03-12 23:41:36 +03:00
|
|
|
for (size_t i = 0; i < mmap_entries; i++) {
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].base = mmap[i].base;
|
|
|
|
_memmap[i].length = mmap[i].length;
|
2022-03-15 23:34:47 +03:00
|
|
|
|
2022-03-12 23:41:36 +03:00
|
|
|
switch (mmap[i].type) {
|
|
|
|
case MEMMAP_USABLE:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_USABLE;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_ACPI_RECLAIMABLE:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_ACPI_RECLAIMABLE;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_ACPI_NVS:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_ACPI_NVS;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_BAD_MEMORY:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_BAD_MEMORY;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_BOOTLOADER_RECLAIMABLE:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_BOOTLOADER_RECLAIMABLE;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_KERNEL_AND_MODULES:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_KERNEL_AND_MODULES;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
case MEMMAP_FRAMEBUFFER:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_FRAMEBUFFER;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case MEMMAP_RESERVED:
|
2022-03-20 21:13:18 +03:00
|
|
|
_memmap[i].type = LIMINE_MEMMAP_RESERVED;
|
2022-03-12 23:41:36 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 22:28:44 +03:00
|
|
|
for (size_t i = 0; i < mmap_entries; i++) {
|
|
|
|
memmap_list[i] = reported_addr(&_memmap[i]);
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:23:47 +03:00
|
|
|
memmap_response->entry_count = mmap_entries;
|
2022-03-20 22:28:44 +03:00
|
|
|
memmap_response->entries = reported_addr(memmap_list);
|
2022-03-12 23:41:36 +03:00
|
|
|
|
2022-03-13 07:35:29 +03:00
|
|
|
memmap_request->response = reported_addr(memmap_response);
|
2022-03-12 21:40:49 +03:00
|
|
|
FEAT_END
|
|
|
|
|
2022-04-17 08:51:28 +03:00
|
|
|
// Clear terminal for kernels that will use the Limine terminal
|
2022-03-21 07:15:10 +03:00
|
|
|
term_write((uint64_t)(uintptr_t)("\e[2J\e[H"), 7);
|
|
|
|
|
|
|
|
term_runtime = true;
|
|
|
|
|
2022-03-12 21:40:49 +03:00
|
|
|
stivale_spinup(64, want_5lv, &pagemap, entry_point, 0,
|
2022-03-31 14:33:18 +03:00
|
|
|
reported_addr(stack), true, true, (uintptr_t)local_gdt);
|
2022-03-12 21:40:49 +03:00
|
|
|
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|