2021-06-29 16:16:25 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <protos/multiboot1.h>
|
2022-06-29 11:27:04 +03:00
|
|
|
#include <protos/multiboot.h>
|
|
|
|
#include <config.h>
|
2021-06-29 16:16:25 +03:00
|
|
|
#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>
|
2022-06-29 10:34:14 +03:00
|
|
|
#include <lib/elsewhere.h>
|
2021-06-29 16:16:25 +03:00
|
|
|
#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>
|
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
#define LIMINE_BRAND "Limine " LIMINE_VERSION
|
|
|
|
|
|
|
|
// Returns the size required to store the multiboot info.
|
|
|
|
static size_t get_multiboot1_info_size(
|
|
|
|
char *cmdline,
|
|
|
|
size_t modules_count, size_t modules_cmdlines_size,
|
|
|
|
uint32_t section_entry_size, uint32_t section_num
|
|
|
|
) {
|
|
|
|
return ALIGN_UP(sizeof(struct multiboot1_info), 16) + // base structure
|
|
|
|
ALIGN_UP(strlen(cmdline) + 1, 16) + // cmdline
|
|
|
|
ALIGN_UP(sizeof(LIMINE_BRAND), 16) + // bootloader brand
|
|
|
|
ALIGN_UP(sizeof(section_entry_size * section_num), 16) + // ELF info
|
|
|
|
ALIGN_UP(sizeof(struct multiboot1_module) * modules_count, 16) + // modules count
|
|
|
|
ALIGN_UP(modules_cmdlines_size, 16) + // modules command lines
|
|
|
|
ALIGN_UP(sizeof(struct multiboot1_mmap_entry) * 256, 16); // memory map
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *mb1_info_alloc(void **mb1_info_raw, size_t size) {
|
|
|
|
void *ret = *mb1_info_raw;
|
|
|
|
*mb1_info_raw += ALIGN_UP(size, 16);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-06-22 22:11:34 +03:00
|
|
|
uint32_t v = *(uint32_t *)(kernel+header_offset);
|
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-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
|
|
|
|
2022-06-19 02:46:38 +03:00
|
|
|
struct elf_section_hdr_info *section_hdr_info = NULL;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
uint64_t entry_point;
|
|
|
|
struct elsewhere_range *ranges;
|
|
|
|
uint64_t ranges_count;
|
2022-06-27 12:17:47 +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
|
|
|
|
2022-06-28 12:41:15 +03:00
|
|
|
size_t load_size;
|
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
|
|
|
|
2022-06-28 12:41:15 +03:00
|
|
|
uint32_t bss_size = 0;
|
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
|
|
|
|
2022-06-28 12:41:15 +03:00
|
|
|
bss_size = header.bss_end_addr - bss_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t full_size = load_size + bss_size;
|
2021-06-30 21:16:10 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
void *elsewhere = ext_mem_alloc(full_size);
|
2022-06-28 12:41:15 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
memcpy(elsewhere, kernel + (header_offset
|
2022-06-28 12:41:15 +03:00
|
|
|
- (header.header_addr - header.load_addr)), load_size);
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
entry_point = header.entry_addr;
|
2022-06-28 12:41:15 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
ranges_count = 1;
|
|
|
|
ranges = ext_mem_alloc(sizeof(struct elsewhere_range));
|
2022-06-28 12:41:15 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
ranges->elsewhere = (uintptr_t)elsewhere;
|
2022-06-29 10:34:14 +03:00
|
|
|
ranges->target = header.load_addr;
|
|
|
|
ranges->length = full_size;
|
2021-06-30 21:16:10 +03:00
|
|
|
} else {
|
|
|
|
int bits = elf_bits(kernel);
|
|
|
|
|
2022-06-19 02:46:38 +03:00
|
|
|
switch (bits) {
|
|
|
|
case 32:
|
2022-06-29 10:34:14 +03:00
|
|
|
if (!elf32_load_elsewhere(kernel, &entry_point, &ranges, &ranges_count))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: ELF32 load failure");
|
2022-06-28 12:41:15 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
section_hdr_info = elf32_section_hdr_info(kernel);
|
2021-06-30 21:16:10 +03:00
|
|
|
break;
|
|
|
|
case 64: {
|
2022-06-29 10:34:14 +03:00
|
|
|
if (!elf64_load_elsewhere(kernel, &entry_point, &ranges, &ranges_count))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot1: ELF64 load failure");
|
2021-06-30 21:16:10 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
section_hdr_info = elf64_section_hdr_info(kernel);
|
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
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
size_t n_modules;
|
|
|
|
size_t modules_cmdlines_size = 0;
|
|
|
|
|
|
|
|
for (n_modules = 0;; n_modules++) {
|
|
|
|
struct conf_tuple conf_tuple = config_get_tuple(config, n_modules, "MODULE_PATH", "MODULE_STRING");
|
|
|
|
if (!conf_tuple.value1) break;
|
|
|
|
|
|
|
|
char *module_cmdline = conf_tuple.value2;
|
|
|
|
if (!module_cmdline) module_cmdline = "";
|
|
|
|
modules_cmdlines_size += ALIGN_UP(strlen(module_cmdline) + 1, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mb1_info_size = get_multiboot1_info_size(
|
|
|
|
cmdline,
|
|
|
|
n_modules,
|
|
|
|
modules_cmdlines_size,
|
|
|
|
section_hdr_info ? section_hdr_info->section_entry_size : 0,
|
|
|
|
section_hdr_info ? section_hdr_info->num : 0
|
|
|
|
);
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
// Realloc elsewhere ranges to include mb1 info, modules, and elf sections
|
|
|
|
struct elsewhere_range *new_ranges = ext_mem_alloc(sizeof(struct elsewhere_range) *
|
|
|
|
(ranges_count
|
|
|
|
+ 1 /* mb1 info range */
|
2022-06-29 11:27:04 +03:00
|
|
|
+ n_modules
|
|
|
|
+ (section_hdr_info ? section_hdr_info->num : 0)));
|
2022-06-29 10:34:14 +03:00
|
|
|
|
|
|
|
memcpy(new_ranges, ranges, sizeof(struct elsewhere_range) * ranges_count);
|
|
|
|
pmm_free(ranges, sizeof(struct elsewhere_range) * ranges_count);
|
|
|
|
ranges = new_ranges;
|
|
|
|
|
2022-06-27 12:17:47 +03:00
|
|
|
// GRUB allocates boot info at 0x10000, *except* if the kernel happens
|
|
|
|
// to overlap this region, then it gets moved to right after the
|
|
|
|
// kernel, or whichever PHDR happens to sit at 0x10000.
|
|
|
|
// Allocate it wherever, then move it to where GRUB puts it
|
|
|
|
// afterwards.
|
2022-06-29 10:34:14 +03:00
|
|
|
|
|
|
|
// Elsewhere append mb1 info *after* kernel but *before* modules.
|
2022-06-29 08:26:06 +03:00
|
|
|
void *mb1_info_raw = ext_mem_alloc(mb1_info_size);
|
2022-06-27 12:17:47 +03:00
|
|
|
uint64_t mb1_info_final_loc = 0x10000;
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
elsewhere_append(true /* flexible target */,
|
|
|
|
ranges, &ranges_count,
|
|
|
|
mb1_info_raw, &mb1_info_final_loc, mb1_info_size);
|
2022-06-27 12:17:47 +03:00
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
size_t mb1_info_slide = (size_t)mb1_info_raw - mb1_info_final_loc;
|
|
|
|
|
|
|
|
struct multiboot1_info *multiboot1_info =
|
|
|
|
mb1_info_alloc(&mb1_info_raw, sizeof(struct multiboot1_info));
|
|
|
|
|
2022-06-19 02:46:38 +03:00
|
|
|
if (section_hdr_info != NULL) {
|
|
|
|
multiboot1_info->elf_sect.num = section_hdr_info->num;
|
|
|
|
multiboot1_info->elf_sect.size = section_hdr_info->section_entry_size;
|
|
|
|
multiboot1_info->elf_sect.shndx = section_hdr_info->str_section_idx;
|
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
void *sections = mb1_info_alloc(&mb1_info_raw,
|
|
|
|
section_hdr_info->section_entry_size * section_hdr_info->num);
|
2022-06-19 02:46:38 +03:00
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
multiboot1_info->elf_sect.addr = (uintptr_t)sections - mb1_info_slide;
|
2022-06-19 02:46:38 +03:00
|
|
|
|
|
|
|
memcpy(sections, kernel + section_hdr_info->section_offset, section_hdr_info->section_entry_size * section_hdr_info->num);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < section_hdr_info->num; i++) {
|
2022-06-19 21:16:37 +03:00
|
|
|
struct elf64_shdr *shdr = (void *)sections + i * section_hdr_info->section_entry_size;
|
2022-06-19 02:46:38 +03:00
|
|
|
|
|
|
|
if (shdr->sh_addr != 0 || shdr->sh_size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
uint64_t section = (uint64_t)-1; /* no target preference, use top */
|
|
|
|
|
|
|
|
elsewhere_append(true /* flexible target */,
|
|
|
|
ranges, &ranges_count,
|
|
|
|
kernel + shdr->sh_offset, §ion, shdr->sh_size);
|
2022-06-19 02:46:38 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
shdr->sh_addr = section;
|
2022-06-19 02:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
multiboot1_info->flags |= (1 << 5);
|
|
|
|
}
|
|
|
|
|
2021-06-29 16:16:25 +03:00
|
|
|
if (n_modules) {
|
2022-06-29 08:26:06 +03:00
|
|
|
struct multiboot1_module *mods =
|
|
|
|
mb1_info_alloc(&mb1_info_raw, sizeof(struct multiboot1_module) * n_modules);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->mods_count = n_modules;
|
2022-06-29 08:26:06 +03:00
|
|
|
multiboot1_info->mods_addr = (size_t)mods - mb1_info_slide;
|
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 = "";
|
|
|
|
}
|
2022-06-29 08:26:06 +03:00
|
|
|
char *lowmem_modstr = mb1_info_alloc(&mb1_info_raw, strlen(module_cmdline) + 1);
|
2021-07-07 11:01:08 +03:00
|
|
|
strcpy(lowmem_modstr, module_cmdline);
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
void *module_addr = freadall(f, MEMMAP_BOOTLOADER_RECLAIMABLE);
|
2022-06-29 10:34:14 +03:00
|
|
|
uint64_t module_target = (uint64_t)-1; /* no target preference, use top */
|
2022-06-19 02:46:38 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
elsewhere_append(true /* flexible target */,
|
|
|
|
ranges, &ranges_count,
|
|
|
|
module_addr, &module_target, f->size);
|
2021-07-03 00:58:22 +03:00
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
m->begin = module_target;
|
2021-10-21 02:27:05 +03:00
|
|
|
m->end = m->begin + f->size;
|
2022-06-29 08:26:06 +03:00
|
|
|
m->cmdline = (uint32_t)(size_t)lowmem_modstr - mb1_info_slide;
|
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
|
|
|
}
|
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
char *lowmem_cmdline = mb1_info_alloc(&mb1_info_raw, strlen(cmdline) + 1);
|
2021-07-07 11:01:08 +03:00
|
|
|
strcpy(lowmem_cmdline, cmdline);
|
2022-06-29 08:26:06 +03:00
|
|
|
multiboot1_info->cmdline = (uint32_t)(size_t)lowmem_cmdline - mb1_info_slide;
|
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-06-29 08:26:06 +03:00
|
|
|
char *bootload_name = LIMINE_BRAND;
|
|
|
|
char *lowmem_bootname = mb1_info_alloc(&mb1_info_raw, strlen(bootload_name) + 1);
|
2021-07-07 11:01:08 +03:00
|
|
|
strcpy(lowmem_bootname, bootload_name);
|
|
|
|
|
2022-06-29 08:26:06 +03:00
|
|
|
multiboot1_info->bootloader_name = (uint32_t)(size_t)lowmem_bootname - mb1_info_slide;
|
2021-11-25 22:50:41 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
// Load relocation stub where it won't get overwritten (hopefully)
|
2022-06-27 12:17:47 +03:00
|
|
|
size_t reloc_stub_size = (size_t)multiboot_reloc_stub_end - (size_t)multiboot_reloc_stub;
|
2022-06-29 10:34:14 +03:00
|
|
|
void *reloc_stub = ext_mem_alloc(reloc_stub_size);
|
2022-06-27 12:17:47 +03:00
|
|
|
memcpy(reloc_stub, multiboot_reloc_stub, reloc_stub_size);
|
|
|
|
|
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);
|
2022-06-29 08:26:06 +03:00
|
|
|
struct multiboot1_mmap_entry *mmap = mb1_info_alloc(&mb1_info_raw, 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
|
|
|
}
|
|
|
|
|
2022-06-29 10:34:14 +03:00
|
|
|
struct meminfo memory_info = mmap_get_info(mb_mmap_count, raw_memmap);
|
|
|
|
|
|
|
|
// Convert the uppermem and lowermem fields from bytes to
|
|
|
|
// KiB.
|
|
|
|
multiboot1_info->mem_lower = memory_info.lowermem / 1024;
|
|
|
|
multiboot1_info->mem_upper = memory_info.uppermem / 1024;
|
2021-06-29 16:16:25 +03:00
|
|
|
|
2021-11-25 22:50:41 +03:00
|
|
|
multiboot1_info->mmap_length = mb_mmap_len;
|
2022-06-29 08:26:06 +03:00
|
|
|
multiboot1_info->mmap_addr = (uint32_t)(size_t)mmap - mb1_info_slide;
|
2021-11-25 22:50:41 +03:00
|
|
|
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;
|
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
common_spinup(multiboot_spinup_32, 6,
|
|
|
|
(uint32_t)(uintptr_t)reloc_stub, (uint32_t)0x2badb002,
|
|
|
|
(uint32_t)mb1_info_final_loc, (uint32_t)entry_point,
|
2022-06-29 10:34:14 +03:00
|
|
|
(uint32_t)(uintptr_t)ranges, (uint32_t)ranges_count);
|
2021-06-29 16:16:25 +03:00
|
|
|
}
|