2021-06-29 16:16:25 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2021-12-31 12:58:05 +03:00
|
|
|
#include <stdnoreturn.h>
|
2022-02-21 01:05:53 +03:00
|
|
|
#include <config.h>
|
2021-06-29 16:16:25 +03:00
|
|
|
#include <protos/multiboot1.h>
|
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/elf.h>
|
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <lib/uri.h>
|
|
|
|
#include <lib/fb.h>
|
|
|
|
#include <lib/term.h>
|
|
|
|
#include <sys/pic.h>
|
|
|
|
#include <sys/cpu.h>
|
2021-09-22 13:33:56 +03:00
|
|
|
#include <sys/idt.h>
|
2021-06-29 16:16:25 +03:00
|
|
|
#include <fs/file.h>
|
|
|
|
#include <mm/vmm.h>
|
|
|
|
#include <mm/pmm.h>
|
|
|
|
#include <drivers/vga_textmode.h>
|
|
|
|
|
2021-12-31 12:58:05 +03:00
|
|
|
noreturn void multiboot1_spinup_32(uint32_t entry_point, uint32_t multiboot1_info);
|
2021-07-06 00:28:04 +03:00
|
|
|
|
2021-11-24 15:23:02 +03:00
|
|
|
bool multiboot1_load(char *config, char *cmdline) {
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *kernel_file;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
|
|
|
|
if (kernel_path == NULL)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: KERNEL_PATH not specified");
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if ((kernel_file = uri_open(kernel_path)) == NULL)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Failed to open kernel with path `%s`. Is the path correct?", kernel_path);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-09-14 08:43:17 +03:00
|
|
|
uint8_t *kernel = freadall(kernel_file, MEMMAP_KERNEL_AND_MODULES);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-10-22 17:37:17 +03:00
|
|
|
size_t kernel_file_size = kernel_file->size;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fclose(kernel_file);
|
|
|
|
|
2021-06-29 16:16:25 +03:00
|
|
|
struct multiboot1_header header = {0};
|
2021-06-30 21:16:10 +03:00
|
|
|
size_t header_offset = 0;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
for (header_offset = 0; header_offset < 8192; header_offset += 4) {
|
2021-06-29 16:16:25 +03:00
|
|
|
uint32_t v;
|
2021-06-30 21:16:10 +03:00
|
|
|
memcpy(&v, kernel + header_offset, 4);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
if (v == MULTIBOOT1_HEADER_MAGIC) {
|
2021-06-30 21:16:10 +03:00
|
|
|
memcpy(&header, kernel + header_offset, sizeof(header));
|
2021-06-29 16:16:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-24 15:23:02 +03:00
|
|
|
if (header.magic != MULTIBOOT1_HEADER_MAGIC) {
|
|
|
|
pmm_free(kernel_file, kernel_file_size);
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-26 01:06:34 +03:00
|
|
|
print("multiboot1: Loading kernel `%s`...\n", kernel_path);
|
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
struct multiboot1_info *multiboot1_info = conv_mem_alloc(sizeof(struct multiboot1_info));
|
|
|
|
|
2021-06-29 16:16:25 +03:00
|
|
|
if (header.magic + header.flags + header.checksum)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Header checksum is invalid");
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-09-14 08:43:17 +03:00
|
|
|
uint32_t entry_point;
|
|
|
|
uint32_t kernel_top;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
if (header.flags & (1 << 16)) {
|
|
|
|
if (header.load_addr > header.header_addr)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Illegal load address");
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
size_t load_size = 0;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
if (header.load_end_addr)
|
|
|
|
load_size = header.load_end_addr - header.load_addr;
|
|
|
|
else
|
2021-10-22 17:37:17 +03:00
|
|
|
load_size = kernel_file_size;
|
2021-06-29 16:51:20 +03:00
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
memmap_alloc_range(header.load_addr, load_size, MEMMAP_KERNEL_AND_MODULES, true, true, false, false);
|
|
|
|
memcpy((void *)(uintptr_t)header.load_addr, kernel + (header_offset
|
|
|
|
- (header.header_addr - header.load_addr)), load_size);
|
|
|
|
|
2021-07-03 02:02:32 +03:00
|
|
|
kernel_top = header.load_addr + load_size;
|
|
|
|
|
2021-06-30 21:16:10 +03:00
|
|
|
if (header.bss_end_addr) {
|
|
|
|
uintptr_t bss_addr = header.load_addr + load_size;
|
|
|
|
if (header.bss_end_addr < bss_addr)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Illegal bss end address");
|
2021-06-30 21:16:10 +03:00
|
|
|
|
|
|
|
uint32_t bss_size = header.bss_end_addr - bss_addr;
|
|
|
|
|
|
|
|
memmap_alloc_range(bss_addr, bss_size, MEMMAP_KERNEL_AND_MODULES, true, true, false, false);
|
|
|
|
memset((void *)bss_addr, 0, bss_size);
|
2021-07-03 02:02:32 +03:00
|
|
|
|
|
|
|
kernel_top = bss_addr + bss_size;
|
2021-06-30 21:16:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
entry_point = header.entry_addr;
|
|
|
|
} else {
|
|
|
|
int bits = elf_bits(kernel);
|
|
|
|
|
|
|
|
switch (bits) {
|
|
|
|
case 32:
|
2021-07-03 02:02:32 +03:00
|
|
|
if (elf32_load(kernel, &entry_point, &kernel_top, MEMMAP_KERNEL_AND_MODULES))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: ELF32 load failure");
|
2021-06-30 21:16:10 +03:00
|
|
|
break;
|
|
|
|
case 64: {
|
2021-07-03 00:58:22 +03:00
|
|
|
uint64_t e, t;
|
2022-03-28 06:13:47 +03:00
|
|
|
if (elf64_load(kernel, &e, &t, NULL, MEMMAP_KERNEL_AND_MODULES, false, true, NULL, NULL, false, NULL, NULL, NULL, NULL))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: ELF64 load failure");
|
2021-06-30 21:16:10 +03:00
|
|
|
entry_point = e;
|
2021-07-03 02:02:32 +03:00
|
|
|
kernel_top = t;
|
2021-06-30 21:16:10 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Invalid ELF file bitness");
|
2021-06-29 16:51:20 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
uint32_t n_modules;
|
|
|
|
|
|
|
|
for (n_modules = 0; ; n_modules++) {
|
|
|
|
if (config_get_value(config, n_modules, "MODULE_PATH") == NULL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_modules) {
|
2021-07-06 04:01:07 +03:00
|
|
|
struct multiboot1_module *mods = conv_mem_alloc(sizeof(*mods) * n_modules);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->mods_count = n_modules;
|
|
|
|
multiboot1_info->mods_addr = (uint32_t)(size_t)mods;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n_modules; i++) {
|
|
|
|
struct multiboot1_module *m = mods + i;
|
|
|
|
|
2022-05-28 06:13:46 +03:00
|
|
|
struct conf_tuple conf_tuple = config_get_tuple(config, i, "MODULE_PATH", "MODULE_STRING");
|
|
|
|
char *module_path = conf_tuple.value1;
|
2021-06-29 16:16:25 +03:00
|
|
|
if (module_path == NULL)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Module disappeared unexpectedly");
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
print("multiboot1: Loading module `%s`...\n", module_path);
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *f;
|
|
|
|
if ((f = uri_open(module_path)) == NULL)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Failed to open module with path `%s`. Is the path correct?", module_path);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2022-05-28 06:13:46 +03:00
|
|
|
char *module_cmdline = conf_tuple.value2;
|
|
|
|
if (module_cmdline == NULL) {
|
|
|
|
module_cmdline = "";
|
|
|
|
}
|
2021-07-07 11:01:08 +03:00
|
|
|
char *lowmem_modstr = conv_mem_alloc(strlen(module_cmdline) + 1);
|
|
|
|
strcpy(lowmem_modstr, module_cmdline);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-07-03 02:45:46 +03:00
|
|
|
void *module_addr = (void *)(uintptr_t)ALIGN_UP(kernel_top, 4096);
|
2022-01-17 00:52:21 +03:00
|
|
|
while (!memmap_alloc_range((uintptr_t)module_addr, f->size, MEMMAP_KERNEL_AND_MODULES,
|
|
|
|
true, false, false, false)) {
|
|
|
|
module_addr += 0x200000;
|
|
|
|
}
|
2021-10-21 02:27:05 +03:00
|
|
|
kernel_top = (uintptr_t)module_addr + f->size;
|
|
|
|
fread(f, module_addr, 0, f->size);
|
2021-07-03 00:58:22 +03:00
|
|
|
|
|
|
|
m->begin = (uint32_t)(size_t)module_addr;
|
2021-10-21 02:27:05 +03:00
|
|
|
m->end = m->begin + f->size;
|
2021-07-07 11:01:08 +03:00
|
|
|
m->cmdline = (uint32_t)(size_t)lowmem_modstr;
|
2021-06-29 16:16:25 +03:00
|
|
|
m->pad = 0;
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2021-06-29 16:16:25 +03:00
|
|
|
if (verbose) {
|
|
|
|
print("multiboot1: Requested module %u:\n", i);
|
|
|
|
print(" Path: %s\n", module_path);
|
2021-07-06 06:17:18 +03:00
|
|
|
print(" String: \"%s\"\n", module_cmdline ?: "");
|
2021-06-29 16:16:25 +03:00
|
|
|
print(" Begin: %x\n", m->begin);
|
|
|
|
print(" End: %x\n", m->end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->flags |= (1 << 3);
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-07 11:01:08 +03:00
|
|
|
char *lowmem_cmdline = conv_mem_alloc(strlen(cmdline) + 1);
|
|
|
|
strcpy(lowmem_cmdline, cmdline);
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->cmdline = (uint32_t)(size_t)lowmem_cmdline;
|
2021-06-29 16:16:25 +03:00
|
|
|
if (cmdline)
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->flags |= (1 << 2);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2022-01-20 07:17:45 +03:00
|
|
|
char *bootload_name = "Limine " LIMINE_VERSION;
|
2021-07-07 11:01:08 +03:00
|
|
|
char *lowmem_bootname = conv_mem_alloc(strlen(bootload_name) + 1);
|
|
|
|
strcpy(lowmem_bootname, bootload_name);
|
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->bootloader_name = (uint32_t)(size_t)lowmem_bootname;
|
|
|
|
multiboot1_info->flags |= (1 << 9);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
term_deinit();
|
|
|
|
|
|
|
|
if (header.flags & (1 << 2)) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t req_width = header.fb_width;
|
|
|
|
size_t req_height = header.fb_height;
|
|
|
|
size_t req_bpp = header.fb_bpp;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
|
|
|
if (header.fb_mode == 0) {
|
|
|
|
char *resolution = config_get_value(config, 0, "RESOLUTION");
|
|
|
|
if (resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, resolution);
|
|
|
|
|
|
|
|
struct fb_info fbinfo;
|
2021-09-14 08:43:17 +03:00
|
|
|
if (!fb_init(&fbinfo, req_width, req_height, req_bpp)) {
|
|
|
|
goto nofb;
|
|
|
|
}
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->fb_addr = (uint64_t)fbinfo.framebuffer_addr;
|
|
|
|
multiboot1_info->fb_width = fbinfo.framebuffer_width;
|
|
|
|
multiboot1_info->fb_height = fbinfo.framebuffer_height;
|
|
|
|
multiboot1_info->fb_bpp = fbinfo.framebuffer_bpp;
|
|
|
|
multiboot1_info->fb_pitch = fbinfo.framebuffer_pitch;
|
|
|
|
multiboot1_info->fb_type = 1;
|
|
|
|
multiboot1_info->fb_red_mask_size = fbinfo.red_mask_size;
|
|
|
|
multiboot1_info->fb_red_mask_shift = fbinfo.red_mask_shift;
|
|
|
|
multiboot1_info->fb_green_mask_size = fbinfo.green_mask_size;
|
|
|
|
multiboot1_info->fb_green_mask_shift = fbinfo.green_mask_shift;
|
|
|
|
multiboot1_info->fb_blue_mask_size = fbinfo.blue_mask_size;
|
|
|
|
multiboot1_info->fb_blue_mask_shift = fbinfo.blue_mask_shift;
|
2021-06-29 16:16:25 +03:00
|
|
|
} else if (header.fb_mode == 1) {
|
2021-09-14 08:43:17 +03:00
|
|
|
nofb:;
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Cannot use text mode with UEFI.");
|
2021-07-15 11:03:47 +03:00
|
|
|
#elif bios == 1
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t rows, cols;
|
2021-06-29 16:16:25 +03:00
|
|
|
init_vga_textmode(&rows, &cols, false);
|
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->fb_addr = 0xb8000;
|
|
|
|
multiboot1_info->fb_width = cols;
|
|
|
|
multiboot1_info->fb_height = rows;
|
|
|
|
multiboot1_info->fb_bpp = 16;
|
|
|
|
multiboot1_info->fb_pitch = 2 * cols;
|
|
|
|
multiboot1_info->fb_type = 2;
|
2021-06-29 16:16:25 +03:00
|
|
|
#endif
|
|
|
|
} else {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Illegal framebuffer type requested");
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->flags |= (1 << 12);
|
2021-07-02 05:34:00 +03:00
|
|
|
} else {
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: Cannot use text mode with UEFI.");
|
2021-07-15 11:03:47 +03:00
|
|
|
#elif bios == 1
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t rows, cols;
|
2021-07-02 05:34:00 +03:00
|
|
|
init_vga_textmode(&rows, &cols, false);
|
|
|
|
#endif
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:03:47 +03:00
|
|
|
#if uefi == 1
|
2021-06-29 16:16:25 +03:00
|
|
|
efi_exit_boot_services();
|
|
|
|
#endif
|
|
|
|
|
2021-07-06 04:01:07 +03:00
|
|
|
size_t mb_mmap_count;
|
|
|
|
struct e820_entry_t *raw_memmap = get_raw_memmap(&mb_mmap_count);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-07-06 04:01:07 +03:00
|
|
|
size_t mb_mmap_len = mb_mmap_count * sizeof(struct multiboot1_mmap_entry);
|
|
|
|
struct multiboot1_mmap_entry *mmap = conv_mem_alloc(mb_mmap_len);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-07-06 04:01:07 +03:00
|
|
|
// Multiboot is bad and passes raw memmap. We do the same to support it.
|
|
|
|
for (size_t i = 0; i < mb_mmap_count; i++) {
|
|
|
|
mmap[i].size = sizeof(struct multiboot1_mmap_entry) - 4;
|
|
|
|
mmap[i].addr = raw_memmap[i].base;
|
|
|
|
mmap[i].len = raw_memmap[i].length;
|
|
|
|
mmap[i].type = raw_memmap[i].type;
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 05:04:41 +03:00
|
|
|
{
|
|
|
|
struct meminfo memory_info = mmap_get_info(mb_mmap_count, raw_memmap);
|
|
|
|
|
|
|
|
// Convert the uppermem and lowermem fields from bytes to
|
|
|
|
// KiB.
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->mem_lower = memory_info.lowermem / 1024;
|
|
|
|
multiboot1_info->mem_upper = memory_info.uppermem / 1024;
|
2021-10-07 05:04:41 +03:00
|
|
|
}
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->mmap_length = mb_mmap_len;
|
|
|
|
multiboot1_info->mmap_addr = ((uint32_t)(size_t)mmap);
|
|
|
|
multiboot1_info->flags |= (1 << 0) | (1 << 6);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-09-22 13:33:56 +03:00
|
|
|
irq_flush_type = IRQ_PIC_ONLY_FLUSH;
|
|
|
|
|
2021-07-06 00:28:04 +03:00
|
|
|
common_spinup(multiboot1_spinup_32, 2,
|
2021-11-25 22:50:41 +03:00
|
|
|
entry_point, (uint32_t)(uintptr_t)multiboot1_info);
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|