2022-08-18 18:32:54 +03:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2022-07-13 08:40:53 +03:00
|
|
|
#include <stdnoreturn.h>
|
2022-06-29 11:27:04 +03:00
|
|
|
#include <protos/multiboot2.h>
|
|
|
|
#include <protos/multiboot.h>
|
2022-02-21 01:05:53 +03:00
|
|
|
#include <config.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/elf.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <lib/config.h>
|
|
|
|
#include <lib/print.h>
|
|
|
|
#include <lib/uri.h>
|
|
|
|
#include <lib/fb.h>
|
|
|
|
#include <lib/term.h>
|
2022-06-29 11:27:04 +03:00
|
|
|
#include <lib/elsewhere.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <sys/pic.h>
|
|
|
|
#include <sys/cpu.h>
|
2021-09-22 13:33:56 +03:00
|
|
|
#include <sys/idt.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <fs/file.h>
|
|
|
|
#include <mm/vmm.h>
|
2021-09-11 07:34:22 +03:00
|
|
|
#include <lib/acpi.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
#include <mm/pmm.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2021-09-10 11:41:01 +03:00
|
|
|
#include <drivers/vga_textmode.h>
|
2023-04-24 14:44:56 +03:00
|
|
|
#include <pxe/pxe.h>
|
2021-09-09 11:40:07 +03:00
|
|
|
|
2022-01-20 07:17:45 +03:00
|
|
|
#define LIMINE_BRAND "Limine " LIMINE_VERSION
|
|
|
|
|
2021-09-12 08:05:59 +03:00
|
|
|
/// Returns the size required to store the multiboot2 info.
|
|
|
|
static size_t get_multiboot2_info_size(
|
2021-09-14 08:43:17 +03:00
|
|
|
char *cmdline,
|
2021-09-13 04:44:22 +03:00
|
|
|
size_t modules_size,
|
2022-06-19 01:30:01 +03:00
|
|
|
uint32_t section_entry_size, uint32_t section_num,
|
2021-10-08 05:26:12 +03:00
|
|
|
uint32_t smbios_tag_size
|
2021-09-12 08:05:59 +03:00
|
|
|
) {
|
|
|
|
return ALIGN_UP(sizeof(struct multiboot2_start_tag), MULTIBOOT_TAG_ALIGN) + // start
|
2022-01-20 07:17:45 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_string) + strlen(cmdline) + 1, MULTIBOOT_TAG_ALIGN) + // cmdline
|
2022-06-30 06:50:31 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_string) + sizeof(LIMINE_BRAND), MULTIBOOT_TAG_ALIGN) + // bootloader brand
|
2021-09-12 08:05:59 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_framebuffer), MULTIBOOT_TAG_ALIGN) + // framebuffer
|
2021-10-05 04:07:52 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_new_acpi) + sizeof(struct rsdp), MULTIBOOT_TAG_ALIGN) + // new ACPI info
|
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_old_acpi) + 20, MULTIBOOT_TAG_ALIGN) + // old ACPI info
|
2022-06-30 06:50:31 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_elf_sections) + section_entry_size * section_num, MULTIBOOT_TAG_ALIGN) + // ELF info
|
2021-09-13 04:44:22 +03:00
|
|
|
ALIGN_UP(modules_size, MULTIBOOT_TAG_ALIGN) + // modules
|
2022-06-30 06:50:31 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_load_base_addr), MULTIBOOT_TAG_ALIGN) + // load base address
|
2021-10-08 05:26:12 +03:00
|
|
|
ALIGN_UP(smbios_tag_size, MULTIBOOT_TAG_ALIGN) + // SMBIOS
|
2021-10-07 05:04:41 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_basic_meminfo), MULTIBOOT_TAG_ALIGN) + // basic memory info
|
2021-09-12 13:08:44 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_mmap) + sizeof(struct multiboot_mmap_entry) * 256, MULTIBOOT_TAG_ALIGN) + // MMAP
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-10-04 03:39:37 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_efi_mmap) + (efi_desc_size * 256), MULTIBOOT_TAG_ALIGN) + // EFI MMAP
|
2021-09-15 00:12:34 +03:00
|
|
|
#if defined (__i386__)
|
2021-10-04 03:39:37 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_efi32), MULTIBOOT_TAG_ALIGN) + // EFI system table 32
|
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_efi32_ih), MULTIBOOT_TAG_ALIGN) + // EFI image handle 32
|
2021-09-15 00:12:34 +03:00
|
|
|
#elif defined (__x86_64__)
|
2021-10-04 03:39:37 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_efi64), MULTIBOOT_TAG_ALIGN) + // EFI system table 64
|
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_efi64_ih), MULTIBOOT_TAG_ALIGN) + // EFI image handle 64
|
2021-09-15 00:12:34 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-04-28 05:59:08 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag_network) + DHCP_ACK_PACKET_LEN, MULTIBOOT_TAG_ALIGN) + // network info
|
2021-09-12 08:05:59 +03:00
|
|
|
ALIGN_UP(sizeof(struct multiboot_tag), MULTIBOOT_TAG_ALIGN); // end
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
2022-08-27 00:30:00 +03:00
|
|
|
#define append_tag(P, TAG) do { \
|
|
|
|
(P) += ALIGN_UP((TAG)->size, MULTIBOOT_TAG_ALIGN); \
|
|
|
|
} while (0)
|
2021-09-12 08:05:59 +03:00
|
|
|
|
2022-07-13 08:40:53 +03:00
|
|
|
noreturn void multiboot2_load(char *config, char* cmdline) {
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *kernel_file;
|
2021-09-09 11:40:07 +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, "multiboot2: KERNEL_PATH not specified");
|
2021-09-09 11:40:07 +03:00
|
|
|
|
2022-09-11 21:45:04 +03:00
|
|
|
print("multiboot2: Loading kernel `%#`...\n", kernel_path);
|
2022-07-13 09:34:30 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
if ((kernel_file = uri_open(kernel_path)) == NULL)
|
2022-09-11 21:45:04 +03:00
|
|
|
panic(true, "multiboot2: Failed to open kernel with path `%#`. Is the path correct?", kernel_path);
|
2021-09-09 11:40:07 +03:00
|
|
|
|
2021-09-14 08:43:17 +03:00
|
|
|
uint8_t *kernel = freadall(kernel_file, MEMMAP_KERNEL_AND_MODULES);
|
2021-10-21 02:27:05 +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-11-24 15:23:02 +03:00
|
|
|
struct multiboot_header *header;
|
|
|
|
|
|
|
|
for (size_t header_offset = 0; header_offset < MULTIBOOT_SEARCH; header_offset += MULTIBOOT_HEADER_ALIGN) {
|
|
|
|
header = (void *)(kernel + header_offset);
|
|
|
|
|
|
|
|
if (header->magic == MULTIBOOT2_HEADER_MAGIC) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header->magic != MULTIBOOT2_HEADER_MAGIC) {
|
2022-07-13 08:40:53 +03:00
|
|
|
panic(true, "multiboot2: Invalid magic");
|
2021-11-24 15:23:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (header->magic + header->architecture + header->checksum + header->header_length) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: Header checksum is invalid");
|
2021-11-24 15:23:02 +03:00
|
|
|
}
|
2021-09-09 11:40:07 +03:00
|
|
|
|
2021-09-14 23:12:22 +03:00
|
|
|
struct multiboot_header_tag_address *addresstag = NULL;
|
2021-09-10 11:41:01 +03:00
|
|
|
struct multiboot_header_tag_framebuffer *fbtag = NULL;
|
2021-10-05 04:07:52 +03:00
|
|
|
|
2022-06-30 06:50:31 +03:00
|
|
|
bool has_reloc_header = false;
|
|
|
|
|
2021-09-11 07:34:22 +03:00
|
|
|
bool is_new_acpi_required = false;
|
2021-10-05 04:07:52 +03:00
|
|
|
bool is_old_acpi_required = false;
|
|
|
|
|
2021-09-14 23:12:22 +03:00
|
|
|
bool is_elf_info_requested = false;
|
|
|
|
|
2022-09-27 06:55:00 +03:00
|
|
|
#if defined (UEFI)
|
2022-09-27 06:46:39 +03:00
|
|
|
bool is_framebuffer_required = false;
|
2022-09-27 06:55:00 +03:00
|
|
|
#endif
|
2022-09-27 06:46:39 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
uint64_t entry_point = 0xffffffff;
|
2021-09-11 07:34:22 +03:00
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
// Iterate through the entries...
|
2021-09-14 08:43:17 +03:00
|
|
|
for (struct multiboot_header_tag *tag = (struct multiboot_header_tag*)(header + 1); // header + 1 to skip the header struct.
|
|
|
|
tag < (struct multiboot_header_tag *)((uintptr_t)header + header->header_length) && tag->type != MULTIBOOT_HEADER_TAG_END;
|
|
|
|
tag = (struct multiboot_header_tag *)((uintptr_t)tag + ALIGN_UP(tag->size, MULTIBOOT_TAG_ALIGN))) {
|
2023-04-12 13:53:24 +03:00
|
|
|
bool is_required = !(tag->flags & MULTIBOOT_HEADER_TAG_OPTIONAL);
|
2021-09-09 11:40:07 +03:00
|
|
|
switch (tag->type) {
|
2021-09-10 11:41:01 +03:00
|
|
|
case MULTIBOOT_HEADER_TAG_INFORMATION_REQUEST: {
|
|
|
|
// Iterate the requests and check if they are supported by or not.
|
2021-09-14 23:12:22 +03:00
|
|
|
struct multiboot_header_tag_information_request *request = (void *)tag;
|
2021-09-14 08:43:17 +03:00
|
|
|
uint32_t size = (request->size - sizeof(struct multiboot_header_tag_information_request)) / sizeof(uint32_t);
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2021-09-10 11:41:01 +03:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
|
|
|
uint32_t r = request->requests[i];
|
|
|
|
|
2021-09-14 08:43:17 +03:00
|
|
|
switch (r) {
|
2021-09-10 11:41:01 +03:00
|
|
|
// We already support the following requests:
|
|
|
|
case MULTIBOOT_TAG_TYPE_CMDLINE:
|
|
|
|
case MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME:
|
|
|
|
case MULTIBOOT_TAG_TYPE_MODULE:
|
|
|
|
case MULTIBOOT_TAG_TYPE_MMAP:
|
2021-10-08 05:26:12 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_SMBIOS:
|
2021-10-07 05:04:41 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_BASIC_MEMINFO:
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-09-15 00:12:34 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_EFI_MMAP:
|
|
|
|
#if defined (__i386__)
|
|
|
|
case MULTIBOOT_TAG_TYPE_EFI32:
|
2021-10-07 05:04:41 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_EFI32_IH:
|
2021-09-15 00:12:34 +03:00
|
|
|
#elif defined (__x86_64__)
|
|
|
|
case MULTIBOOT_TAG_TYPE_EFI64:
|
2021-10-07 05:04:41 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_EFI64_IH:
|
2021-09-15 00:12:34 +03:00
|
|
|
#endif
|
|
|
|
#endif
|
2022-09-27 06:46:39 +03:00
|
|
|
break;
|
2021-09-10 11:41:01 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_FRAMEBUFFER:
|
2022-09-27 06:55:00 +03:00
|
|
|
#if defined (UEFI)
|
2022-09-27 06:46:39 +03:00
|
|
|
is_framebuffer_required = is_required;
|
2022-09-27 06:55:00 +03:00
|
|
|
#endif
|
2021-09-10 11:41:01 +03:00
|
|
|
break;
|
2021-09-14 08:43:17 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_ACPI_NEW:
|
|
|
|
is_new_acpi_required = is_required;
|
|
|
|
break;
|
2021-10-05 04:07:52 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_ACPI_OLD:
|
|
|
|
is_old_acpi_required = is_required;
|
|
|
|
break;
|
2021-09-14 23:12:22 +03:00
|
|
|
case MULTIBOOT_TAG_TYPE_ELF_SECTIONS:
|
|
|
|
is_elf_info_requested = is_required;
|
|
|
|
break;
|
2021-09-14 08:43:17 +03:00
|
|
|
default:
|
2021-09-14 23:12:22 +03:00
|
|
|
if (is_required)
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: Requested tag `%d` which is not supported", r);
|
2021-09-14 08:43:17 +03:00
|
|
|
break;
|
2021-09-10 11:41:01 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-14 08:43:17 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-09-10 11:41:01 +03:00
|
|
|
case MULTIBOOT_HEADER_TAG_FRAMEBUFFER: {
|
2021-09-14 23:12:22 +03:00
|
|
|
fbtag = (void *)tag;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MULTIBOOT_HEADER_TAG_ENTRY_ADDRESS: {
|
|
|
|
struct multiboot_header_tag_entry_address *entrytag = (void *)tag;
|
|
|
|
entry_point = entrytag->entry_addr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MULTIBOOT_HEADER_TAG_ADDRESS: {
|
|
|
|
addresstag = (void *)tag;
|
2021-09-14 08:43:17 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-09-13 04:44:22 +03:00
|
|
|
// We always align the modules ;^)
|
2021-09-15 00:12:34 +03:00
|
|
|
case MULTIBOOT_HEADER_TAG_MODULE_ALIGN:
|
|
|
|
case MULTIBOOT_HEADER_TAG_EFI_BS:
|
|
|
|
break;
|
2021-09-13 04:44:22 +03:00
|
|
|
|
2022-06-30 06:50:31 +03:00
|
|
|
case MULTIBOOT_HEADER_TAG_RELOCATABLE:
|
|
|
|
has_reloc_header = true;
|
|
|
|
break;
|
|
|
|
|
2023-04-12 13:53:24 +03:00
|
|
|
default:
|
|
|
|
if (is_required)
|
|
|
|
panic(true, "multiboot2: Unknown header tag type: %u\n", tag->type);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 11:05:45 +03:00
|
|
|
bool section_hdr_info_valid = false;
|
|
|
|
struct elf_section_hdr_info section_hdr_info = {0};
|
2022-06-29 13:32:50 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
struct elsewhere_range *ranges;
|
|
|
|
uint64_t ranges_count;
|
2022-06-27 11:48:03 +03:00
|
|
|
|
2021-09-14 23:12:22 +03:00
|
|
|
if (addresstag != NULL) {
|
2022-10-30 13:19:02 +03:00
|
|
|
size_t header_offset = (size_t)header - (size_t)kernel;
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-10-30 13:19:02 +03:00
|
|
|
uintptr_t load_src, load_addr;
|
|
|
|
if (addresstag->load_addr != (uint32_t)-1) {
|
|
|
|
if (addresstag->load_addr > addresstag->header_addr) {
|
|
|
|
panic(true, "multiboot2: Illegal load address");
|
|
|
|
}
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-10-30 13:19:02 +03:00
|
|
|
load_src = header_offset - (addresstag->header_addr - addresstag->load_addr);
|
|
|
|
load_addr = addresstag->load_addr;
|
|
|
|
} else {
|
|
|
|
load_src = 0;
|
|
|
|
load_addr = addresstag->header_addr - header_offset;
|
|
|
|
}
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-10-30 13:19:02 +03:00
|
|
|
size_t load_size;
|
|
|
|
if (addresstag->load_end_addr != 0) {
|
|
|
|
load_size = addresstag->load_end_addr - load_addr;
|
|
|
|
} else {
|
|
|
|
load_size = kernel_file_size - load_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bss_size = 0;
|
|
|
|
if (addresstag->bss_end_addr != 0) {
|
|
|
|
uintptr_t bss_addr = load_addr + load_size;
|
|
|
|
if (addresstag->bss_end_addr < bss_addr) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: Illegal bss end address");
|
2022-10-30 13:19:02 +03:00
|
|
|
}
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-06-29 14:04:26 +03:00
|
|
|
bss_size = addresstag->bss_end_addr - bss_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t full_size = load_size + bss_size;
|
|
|
|
|
|
|
|
void *elsewhere = ext_mem_alloc(full_size);
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-10-30 13:19:02 +03:00
|
|
|
memcpy(elsewhere, kernel + load_src, load_size);
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-06-29 14:04:26 +03:00
|
|
|
if (entry_point == 0xffffffff) {
|
|
|
|
panic(true, "multiboot2: Using address tag but entry address tag missing");
|
2021-09-14 23:12:22 +03:00
|
|
|
}
|
2022-06-29 14:04:26 +03:00
|
|
|
|
|
|
|
ranges_count = 1;
|
|
|
|
ranges = ext_mem_alloc(sizeof(struct elsewhere_range));
|
|
|
|
|
|
|
|
ranges->elsewhere = (uintptr_t)elsewhere;
|
2022-10-30 13:19:02 +03:00
|
|
|
ranges->target = load_addr;
|
2022-06-29 14:04:26 +03:00
|
|
|
ranges->length = full_size;
|
2021-09-14 23:12:22 +03:00
|
|
|
} else {
|
2022-06-29 11:27:04 +03:00
|
|
|
uint64_t e;
|
2021-09-14 23:12:22 +03:00
|
|
|
int bits = elf_bits(kernel);
|
|
|
|
|
|
|
|
switch (bits) {
|
|
|
|
case 32:
|
2022-06-29 13:32:50 +03:00
|
|
|
if (!elf32_load_elsewhere(kernel, &e, &ranges, &ranges_count))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: ELF32 load failure");
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-06-29 13:32:50 +03:00
|
|
|
section_hdr_info = elf32_section_hdr_info(kernel);
|
2022-09-11 11:05:45 +03:00
|
|
|
section_hdr_info_valid = true;
|
2021-09-14 23:12:22 +03:00
|
|
|
break;
|
|
|
|
case 64: {
|
2022-06-29 13:32:50 +03:00
|
|
|
if (!elf64_load_elsewhere(kernel, &e, &ranges, &ranges_count))
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: ELF64 load failure");
|
2021-09-14 23:12:22 +03:00
|
|
|
|
2022-06-29 13:32:50 +03:00
|
|
|
section_hdr_info = elf64_section_hdr_info(kernel);
|
2022-09-11 11:05:45 +03:00
|
|
|
section_hdr_info_valid = true;
|
2021-09-14 23:12:22 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: Invalid ELF file bitness");
|
2021-09-14 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry_point == 0xffffffff) {
|
|
|
|
entry_point = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-30 06:50:31 +03:00
|
|
|
// Get the load base address (AKA the lowest target in the ranges)
|
|
|
|
uint64_t load_base_addr = (uint64_t)-1;
|
|
|
|
for (size_t i = 0; i < ranges_count; i++) {
|
|
|
|
if (load_base_addr > ranges[i].target) {
|
|
|
|
load_base_addr = ranges[i].target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:49:59 +03:00
|
|
|
size_t modules_size = 0;
|
|
|
|
size_t n_modules;
|
2021-09-13 04:44:22 +03:00
|
|
|
|
|
|
|
for (n_modules = 0;; n_modules++) {
|
2022-05-28 06:05:11 +03:00
|
|
|
struct conf_tuple conf_tuple = config_get_tuple(config, n_modules, "MODULE_PATH", "MODULE_STRING");
|
|
|
|
if (!conf_tuple.value1) break;
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2022-05-28 06:05:11 +03:00
|
|
|
char *module_cmdline = conf_tuple.value2;
|
|
|
|
if (!module_cmdline) module_cmdline = "";
|
2021-09-13 04:44:22 +03:00
|
|
|
modules_size += sizeof(struct multiboot_tag_module) + strlen(module_cmdline) + 1;
|
|
|
|
}
|
|
|
|
|
2021-10-08 05:26:12 +03:00
|
|
|
struct smbios_entry_point_32* smbios_entry_32 = NULL;
|
|
|
|
struct smbios_entry_point_64* smbios_entry_64 = NULL;
|
|
|
|
|
|
|
|
acpi_get_smbios((void **)&smbios_entry_32, (void **)&smbios_entry_64);
|
|
|
|
|
|
|
|
uint32_t smbios_tag_size = 0;
|
|
|
|
|
|
|
|
if (smbios_entry_32 != NULL)
|
2021-10-08 06:47:04 +03:00
|
|
|
smbios_tag_size += sizeof(struct multiboot_tag_smbios) + smbios_entry_32->length;
|
|
|
|
if (smbios_entry_64 != NULL)
|
|
|
|
smbios_tag_size += sizeof(struct multiboot_tag_smbios) + smbios_entry_64->length;
|
2021-10-08 05:26:12 +03:00
|
|
|
|
|
|
|
size_t mb2_info_size = get_multiboot2_info_size(
|
|
|
|
cmdline,
|
|
|
|
modules_size,
|
2022-09-11 11:05:45 +03:00
|
|
|
section_hdr_info_valid ? section_hdr_info.section_entry_size : 0,
|
|
|
|
section_hdr_info_valid ? section_hdr_info.num : 0,
|
2021-10-08 05:26:12 +03:00
|
|
|
smbios_tag_size
|
|
|
|
);
|
|
|
|
|
2021-09-12 08:05:59 +03:00
|
|
|
size_t info_idx = 0;
|
2022-06-27 11:48:03 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
// Realloc elsewhere ranges to include mb2 info, modules, and elf sections
|
|
|
|
struct elsewhere_range *new_ranges = ext_mem_alloc(sizeof(struct elsewhere_range) *
|
|
|
|
(ranges_count
|
|
|
|
+ 1 /* mb2 info range */
|
|
|
|
+ n_modules
|
2022-09-11 11:05:45 +03:00
|
|
|
+ (section_hdr_info_valid ? section_hdr_info.num : 0)));
|
2022-06-29 11:27:04 +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 11:48:03 +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 11:27:04 +03:00
|
|
|
|
|
|
|
// Elsewhere append mb2 info *after* kernel but *before* modules.
|
2022-06-27 11:48:03 +03:00
|
|
|
uint8_t *mb2_info = ext_mem_alloc(mb2_info_size);
|
|
|
|
uint64_t mb2_info_final_loc = 0x10000;
|
|
|
|
|
2022-07-24 23:28:16 +03:00
|
|
|
if (!elsewhere_append(true /* flexible target */,
|
2022-06-29 11:27:04 +03:00
|
|
|
ranges, &ranges_count,
|
2022-07-24 23:28:16 +03:00
|
|
|
mb2_info, &mb2_info_final_loc, mb2_info_size)) {
|
|
|
|
panic(true, "multiboot2: Cannot allocate mb2 info");
|
|
|
|
}
|
2021-09-12 08:05:59 +03:00
|
|
|
|
2022-01-10 15:37:44 +03:00
|
|
|
struct multiboot2_start_tag *mbi_start = (struct multiboot2_start_tag *)mb2_info;
|
2021-09-12 08:05:59 +03:00
|
|
|
info_idx += sizeof(struct multiboot2_start_tag);
|
2021-09-09 11:40:07 +03:00
|
|
|
|
2022-06-19 00:40:34 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create ELF info tag
|
|
|
|
//////////////////////////////////////////////
|
2022-09-11 11:05:45 +03:00
|
|
|
if (section_hdr_info_valid == false) {
|
2022-06-19 00:40:34 +03:00
|
|
|
if (is_elf_info_requested) {
|
|
|
|
panic(true, "multiboot2: Cannot return ELF file information");
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-11 11:05:45 +03:00
|
|
|
uint32_t size = sizeof(struct multiboot_tag_elf_sections) + section_hdr_info.section_entry_size * section_hdr_info.num;
|
2022-06-19 00:40:34 +03:00
|
|
|
struct multiboot_tag_elf_sections *tag = (struct multiboot_tag_elf_sections*)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_ELF_SECTIONS;
|
|
|
|
tag->size = size;
|
|
|
|
|
2022-09-11 11:05:45 +03:00
|
|
|
tag->num = section_hdr_info.num;
|
|
|
|
tag->entsize = section_hdr_info.section_entry_size;
|
|
|
|
tag->shndx = section_hdr_info.str_section_idx;
|
2022-06-19 00:40:34 +03:00
|
|
|
|
2022-09-11 11:05:45 +03:00
|
|
|
memcpy(tag->sections, kernel + section_hdr_info.section_offset, section_hdr_info.section_entry_size * section_hdr_info.num);
|
2022-06-19 00:40:34 +03:00
|
|
|
|
2023-05-03 04:09:36 +03:00
|
|
|
int bits = elf_bits(kernel);
|
|
|
|
|
2022-09-11 11:05:45 +03:00
|
|
|
for (size_t i = 0; i < section_hdr_info.num; i++) {
|
2023-05-03 04:09:36 +03:00
|
|
|
if (bits == 64) {
|
|
|
|
struct elf64_shdr *shdr = (void *)tag->sections + i * section_hdr_info.section_entry_size;
|
2022-06-19 00:40:34 +03:00
|
|
|
|
2023-05-03 04:09:36 +03:00
|
|
|
if (shdr->sh_addr != 0 || shdr->sh_size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-19 00:40:34 +03:00
|
|
|
|
2023-05-03 04:09:36 +03:00
|
|
|
uint64_t section = (uint64_t)-1; /* no target preference, use top */
|
2022-06-29 11:27:04 +03:00
|
|
|
|
2023-05-03 04:09:36 +03:00
|
|
|
if (!elsewhere_append(true /* flexible target */,
|
|
|
|
ranges, &ranges_count,
|
|
|
|
kernel + shdr->sh_offset, §ion, shdr->sh_size)) {
|
|
|
|
panic(true, "multiboot2: Cannot allocate elf sections");
|
|
|
|
}
|
|
|
|
|
|
|
|
shdr->sh_addr = section;
|
|
|
|
} else {
|
|
|
|
struct elf32_shdr *shdr = (void *)tag->sections + i * section_hdr_info.section_entry_size;
|
2022-06-19 00:40:34 +03:00
|
|
|
|
2023-05-03 04:09:36 +03:00
|
|
|
if (shdr->sh_addr != 0 || shdr->sh_size == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t section = (uint64_t)-1; /* no target preference, use top */
|
|
|
|
|
|
|
|
if (!elsewhere_append(true /* flexible target */,
|
|
|
|
ranges, &ranges_count,
|
|
|
|
kernel + shdr->sh_offset, §ion, shdr->sh_size)) {
|
|
|
|
panic(true, "multiboot2: Cannot allocate elf sections");
|
|
|
|
}
|
|
|
|
|
|
|
|
shdr->sh_addr = section;
|
|
|
|
}
|
2022-06-19 00:40:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
|
2022-06-30 06:50:31 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create load base address tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
if (has_reloc_header) {
|
|
|
|
uint32_t size = sizeof(struct multiboot_tag_load_base_addr);
|
|
|
|
struct multiboot_tag_load_base_addr *tag = (void *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_LOAD_BASE_ADDR;
|
|
|
|
tag->size = size;
|
|
|
|
|
|
|
|
tag->load_base_addr = load_base_addr;
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
|
2021-09-13 04:44:22 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create modules tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
for (size_t i = 0; i < n_modules; i++) {
|
2022-05-28 06:05:11 +03:00
|
|
|
struct conf_tuple conf_tuple = config_get_tuple(config, i, "MODULE_PATH", "MODULE_STRING");
|
|
|
|
char *module_path = conf_tuple.value1;
|
|
|
|
if (!module_path) panic(true, "multiboot2: Module disappeared unexpectedly");
|
2021-09-13 04:44:22 +03:00
|
|
|
|
2022-09-11 21:45:04 +03:00
|
|
|
print("multiboot2: Loading module `%#`...\n", module_path);
|
2021-09-13 04:44:22 +03:00
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
struct file_handle *f;
|
|
|
|
if ((f = uri_open(module_path)) == NULL)
|
2022-09-11 21:45:04 +03:00
|
|
|
panic(true, "multiboot2: Failed to open module with path `%#`. Is the path correct?", module_path);
|
2021-09-13 05:49:59 +03:00
|
|
|
|
|
|
|
// Module commandline can be null, so we guard against that and make the
|
2021-09-13 04:44:22 +03:00
|
|
|
// string "".
|
2022-05-28 06:05:11 +03:00
|
|
|
char *module_cmdline = conf_tuple.value2;
|
|
|
|
if (!module_cmdline) module_cmdline = "";
|
2021-09-13 04:44:22 +03:00
|
|
|
|
2022-06-29 11:27:04 +03:00
|
|
|
void *module_addr = freadall(f, MEMMAP_BOOTLOADER_RECLAIMABLE);
|
|
|
|
uint64_t module_target = (uint64_t)-1;
|
2021-09-13 04:44:22 +03:00
|
|
|
|
2022-07-24 23:28:16 +03:00
|
|
|
if (!elsewhere_append(true /* flexible target */,
|
2022-06-29 11:27:04 +03:00
|
|
|
ranges, &ranges_count,
|
2022-07-24 23:28:16 +03:00
|
|
|
module_addr, &module_target, f->size)) {
|
|
|
|
panic(true, "multiboot2: Cannot allocate module");
|
|
|
|
}
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_module *module_tag = (struct multiboot_tag_module *)(mb2_info + info_idx);
|
2021-09-13 04:44:22 +03:00
|
|
|
|
|
|
|
module_tag->type = MULTIBOOT_TAG_TYPE_MODULE;
|
|
|
|
module_tag->size = sizeof(struct multiboot_tag_module) + strlen(module_cmdline) + 1;
|
2022-06-29 11:27:04 +03:00
|
|
|
module_tag->mod_start = module_target;
|
2021-10-21 02:27:05 +03:00
|
|
|
module_tag->mod_end = module_tag->mod_start + f->size;
|
2021-09-13 04:44:22 +03:00
|
|
|
strcpy(module_tag->cmdline, module_cmdline); // Copy over the command line
|
|
|
|
|
2021-10-21 02:27:05 +03:00
|
|
|
fclose(f);
|
|
|
|
|
2021-09-13 04:44:22 +03:00
|
|
|
if (verbose) {
|
|
|
|
print("multiboot2: Requested module %u:\n", i);
|
|
|
|
print(" Path: %s\n", module_path);
|
|
|
|
print(" String: \"%s\"\n", module_cmdline ?: "");
|
|
|
|
print(" Begin: %x\n", module_tag->mod_start);
|
|
|
|
print(" End: %x\n", module_tag->mod_end);
|
|
|
|
}
|
|
|
|
|
2021-09-13 05:49:59 +03:00
|
|
|
append_tag(info_idx, module_tag);
|
2021-09-13 04:44:22 +03:00
|
|
|
}
|
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create command line tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2022-01-20 07:17:45 +03:00
|
|
|
uint32_t size = sizeof(struct multiboot_tag_string) + strlen(cmdline) + 1;
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_string *tag = (struct multiboot_tag_string *)(mb2_info + info_idx);
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_CMDLINE;
|
|
|
|
tag->size = size;
|
|
|
|
|
|
|
|
strcpy(tag->string, cmdline);
|
2021-09-12 08:05:59 +03:00
|
|
|
append_tag(info_idx, tag);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create bootloader name tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2022-01-20 07:17:45 +03:00
|
|
|
uint32_t size = sizeof(struct multiboot_tag_string) + sizeof(LIMINE_BRAND);
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_string *tag = (struct multiboot_tag_string *)(mb2_info + info_idx);
|
2021-09-09 11:40:07 +03:00
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_BOOT_LOADER_NAME;
|
|
|
|
tag->size = size;
|
|
|
|
|
2022-01-20 07:17:45 +03:00
|
|
|
strcpy(tag->string, LIMINE_BRAND);
|
2021-09-12 08:05:59 +03:00
|
|
|
append_tag(info_idx, tag);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-04 03:39:37 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create EFI image handle tag
|
|
|
|
//////////////////////////////////////////////
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-10-04 03:39:37 +03:00
|
|
|
{
|
|
|
|
#if defined (__i386__)
|
|
|
|
struct multiboot_tag_efi64_ih *tag = (struct multiboot_tag_efi64_ih *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_EFI64_IH;
|
|
|
|
tag->size = sizeof(struct multiboot_tag_efi64_ih);
|
|
|
|
#elif defined (__x86_64__)
|
|
|
|
struct multiboot_tag_efi32_ih *tag = (struct multiboot_tag_efi32_ih *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_EFI32_IH;
|
|
|
|
tag->size = sizeof(struct multiboot_tag_efi32_ih);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tag->pointer = (uintptr_t)efi_image_handle;
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create framebuffer tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2022-06-29 14:25:09 +03:00
|
|
|
struct multiboot_tag_framebuffer *tag = (struct multiboot_tag_framebuffer *)(mb2_info + info_idx);
|
|
|
|
|
2022-06-30 08:25:33 +03:00
|
|
|
tag->common.type = MULTIBOOT_TAG_TYPE_FRAMEBUFFER;
|
|
|
|
tag->common.size = sizeof(struct multiboot_tag_framebuffer);
|
|
|
|
|
2022-11-19 01:59:31 +03:00
|
|
|
term_notready();
|
|
|
|
|
|
|
|
size_t req_width = 0;
|
|
|
|
size_t req_height = 0;
|
|
|
|
size_t req_bpp = 0;
|
2023-04-12 13:50:01 +03:00
|
|
|
#if defined (BIOS)
|
|
|
|
{
|
|
|
|
char *textmode_str = config_get_value(config, 0, "TEXTMODE");
|
|
|
|
bool textmode = textmode_str != NULL && strcmp(textmode_str, "yes") == 0;
|
|
|
|
if (textmode) {
|
|
|
|
goto textmode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2022-06-29 16:20:09 +03:00
|
|
|
if (fbtag) {
|
2022-11-19 01:59:31 +03:00
|
|
|
req_width = fbtag->width;
|
|
|
|
req_height = fbtag->height;
|
|
|
|
req_bpp = fbtag->depth;
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2022-11-19 01:59:31 +03:00
|
|
|
#if defined (UEFI)
|
|
|
|
modeset:;
|
|
|
|
#endif
|
2021-09-10 11:41:01 +03:00
|
|
|
char *resolution = config_get_value(config, 0, "RESOLUTION");
|
|
|
|
if (resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, resolution);
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2022-11-19 01:59:31 +03:00
|
|
|
struct fb_info *fbs;
|
|
|
|
size_t fbs_count;
|
|
|
|
fb_init(&fbs, &fbs_count, req_width, req_height, req_bpp);
|
|
|
|
if (fbs_count == 0) {
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2022-11-19 01:59:31 +03:00
|
|
|
textmode:
|
2022-10-04 03:34:11 +03:00
|
|
|
vga_textmode_init(false);
|
2021-09-14 08:43:17 +03:00
|
|
|
|
|
|
|
tag->common.framebuffer_addr = 0xb8000;
|
2022-11-19 01:59:31 +03:00
|
|
|
tag->common.framebuffer_pitch = 2 * 80;
|
|
|
|
tag->common.framebuffer_width = 80;
|
|
|
|
tag->common.framebuffer_height = 25;
|
2021-09-14 08:43:17 +03:00
|
|
|
tag->common.framebuffer_bpp = 16;
|
|
|
|
tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (UEFI)
|
2022-09-27 06:46:39 +03:00
|
|
|
if (is_framebuffer_required) {
|
|
|
|
panic(true, "multiboot2: Failed to set video mode");
|
|
|
|
} else {
|
|
|
|
goto skip_modeset;
|
|
|
|
}
|
2021-09-15 00:12:34 +03:00
|
|
|
#endif
|
2021-09-14 08:43:17 +03:00
|
|
|
} else {
|
2022-11-19 01:59:31 +03:00
|
|
|
tag->common.framebuffer_addr = fbs[0].framebuffer_addr;
|
|
|
|
tag->common.framebuffer_pitch = fbs[0].framebuffer_pitch;
|
|
|
|
tag->common.framebuffer_width = fbs[0].framebuffer_width;
|
|
|
|
tag->common.framebuffer_height = fbs[0].framebuffer_height;
|
|
|
|
tag->common.framebuffer_bpp = fbs[0].framebuffer_bpp;
|
2021-09-14 08:43:17 +03:00
|
|
|
tag->common.framebuffer_type = MULTIBOOT_FRAMEBUFFER_TYPE_RGB; // We only support RGB for VBE
|
|
|
|
|
2022-11-19 01:59:31 +03:00
|
|
|
tag->framebuffer_red_field_position = fbs[0].red_mask_shift;
|
|
|
|
tag->framebuffer_red_mask_size = fbs[0].red_mask_size;
|
|
|
|
tag->framebuffer_green_field_position = fbs[0].green_mask_shift;
|
|
|
|
tag->framebuffer_green_mask_size = fbs[0].green_mask_size;
|
|
|
|
tag->framebuffer_blue_field_position = fbs[0].blue_mask_shift;
|
|
|
|
tag->framebuffer_blue_mask_size = fbs[0].blue_mask_size;
|
2021-09-14 08:43:17 +03:00
|
|
|
}
|
2021-09-10 11:41:01 +03:00
|
|
|
} else {
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2022-06-30 08:25:33 +03:00
|
|
|
print("multiboot2: Warning: Cannot use text mode with UEFI\n");
|
2022-11-19 01:59:31 +03:00
|
|
|
goto modeset;
|
2022-09-02 03:29:12 +03:00
|
|
|
#elif defined (BIOS)
|
2022-11-19 01:59:31 +03:00
|
|
|
goto textmode;
|
2021-09-10 11:41:01 +03:00
|
|
|
#endif
|
|
|
|
}
|
2022-06-29 14:25:09 +03:00
|
|
|
|
|
|
|
append_tag(info_idx, &tag->common);
|
2022-09-27 06:46:39 +03:00
|
|
|
|
2022-09-27 06:55:00 +03:00
|
|
|
#if defined (UEFI)
|
2022-09-27 06:46:39 +03:00
|
|
|
skip_modeset:;
|
2022-09-27 06:55:00 +03:00
|
|
|
#endif
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 07:34:22 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create new ACPI info tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2021-10-07 05:04:41 +03:00
|
|
|
void *new_rsdp = acpi_get_rsdp_v2();
|
2021-09-11 07:34:22 +03:00
|
|
|
|
|
|
|
if (new_rsdp != NULL) {
|
2021-10-05 04:07:52 +03:00
|
|
|
uint32_t size = sizeof(struct multiboot_tag_new_acpi) + sizeof(struct rsdp); // XSDP is 36 bytes wide
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_new_acpi *tag = (struct multiboot_tag_new_acpi *)(mb2_info + info_idx);
|
2021-09-11 07:34:22 +03:00
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_ACPI_NEW;
|
|
|
|
tag->size = size;
|
|
|
|
|
2021-10-05 04:07:52 +03:00
|
|
|
memcpy(tag->rsdp, new_rsdp, sizeof(struct rsdp));
|
2021-09-12 08:05:59 +03:00
|
|
|
append_tag(info_idx, tag);
|
2021-09-11 07:34:22 +03:00
|
|
|
} else if (is_new_acpi_required) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: XSDP requested but not found");
|
2021-10-05 04:07:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create old ACPI info tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
|
|
|
void *old_rsdp = acpi_get_rsdp_v1();
|
|
|
|
|
|
|
|
if (old_rsdp != NULL) {
|
|
|
|
uint32_t size = sizeof(struct multiboot_tag_old_acpi) + 20; // RSDP is 20 bytes wide
|
|
|
|
struct multiboot_tag_old_acpi *tag = (struct multiboot_tag_old_acpi *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_ACPI_OLD;
|
|
|
|
tag->size = size;
|
|
|
|
|
|
|
|
memcpy(tag->rsdp, old_rsdp, 20);
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
} else if (is_old_acpi_required) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "multiboot2: RSDP requested but not found");
|
2021-09-11 07:34:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 05:26:12 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create SMBIOS tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2021-10-08 06:47:04 +03:00
|
|
|
// NOTE: The multiboot2 specification does not say anything about if both
|
2021-10-21 02:27:05 +03:00
|
|
|
// smbios 32 and 64 bit entry points are present, then we pass both of them + smbios
|
2021-10-08 06:47:04 +03:00
|
|
|
// support for grub2 is unimplemented. So, we are going to assume they expect us to
|
|
|
|
// pass both of them if avaliable. Oh well...
|
2021-10-08 05:26:12 +03:00
|
|
|
if (smbios_entry_32 != NULL) {
|
|
|
|
struct multiboot_tag_smbios *tag = (struct multiboot_tag_smbios *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_SMBIOS;
|
|
|
|
tag->size = sizeof(struct multiboot_tag_smbios);
|
|
|
|
|
|
|
|
tag->major = smbios_entry_32->major_version;
|
|
|
|
tag->minor = smbios_entry_32->minor_version;
|
|
|
|
|
|
|
|
memset(tag->reserved, 0, 6);
|
|
|
|
memcpy(tag->tables, smbios_entry_32, smbios_entry_32->length);
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
2021-10-08 06:47:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (smbios_entry_64 != NULL) {
|
2021-10-08 05:26:12 +03:00
|
|
|
struct multiboot_tag_smbios *tag = (struct multiboot_tag_smbios *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_SMBIOS;
|
|
|
|
tag->size = sizeof(struct multiboot_tag_smbios);
|
|
|
|
|
|
|
|
tag->major = smbios_entry_64->major_version;
|
|
|
|
tag->minor = smbios_entry_64->minor_version;
|
|
|
|
|
|
|
|
memset(tag->reserved, 0, 6);
|
|
|
|
memcpy(tag->tables, smbios_entry_64, smbios_entry_64->length);
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:12:34 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create EFI system table info tag
|
|
|
|
//////////////////////////////////////////////
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-09-15 00:12:34 +03:00
|
|
|
{
|
|
|
|
#if defined (__i386__)
|
|
|
|
uint32_t size = sizeof(struct multiboot_tag_efi32);
|
|
|
|
struct multiboot_tag_efi32 *tag = (void *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_EFI32;
|
|
|
|
#elif defined (__x86_64__)
|
|
|
|
uint32_t size = sizeof(struct multiboot_tag_efi64);
|
|
|
|
struct multiboot_tag_efi64 *tag = (void *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_EFI64;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tag->size = size;
|
|
|
|
tag->pointer = (uintptr_t)gST;
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-07-24 23:32:25 +03:00
|
|
|
// Load relocation stub where it won't get overwritten (hopefully)
|
|
|
|
size_t reloc_stub_size = (size_t)multiboot_reloc_stub_end - (size_t)multiboot_reloc_stub;
|
|
|
|
void *reloc_stub = ext_mem_alloc(reloc_stub_size);
|
|
|
|
memcpy(reloc_stub, multiboot_reloc_stub, reloc_stub_size);
|
|
|
|
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-09-09 11:40:07 +03:00
|
|
|
efi_exit_boot_services();
|
|
|
|
#endif
|
|
|
|
|
2021-10-07 05:04:41 +03:00
|
|
|
size_t mb_mmap_count;
|
2022-08-13 20:54:49 +03:00
|
|
|
struct memmap_entry *raw_memmap = get_raw_memmap(&mb_mmap_count);
|
2021-10-07 05:04:41 +03:00
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
//////////////////////////////////////////////
|
2021-09-11 08:06:51 +03:00
|
|
|
// Create memory map tag
|
2021-09-09 11:40:07 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2021-09-12 13:08:44 +03:00
|
|
|
if (mb_mmap_count > 256) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(false, "multiboot2: too many memory map entries");
|
2021-09-12 13:08:44 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 08:06:51 +03:00
|
|
|
// Create the normal memory map tag.
|
2021-09-09 11:40:07 +03:00
|
|
|
uint32_t mmap_size = sizeof(struct multiboot_tag_mmap) + sizeof(struct multiboot_mmap_entry) * mb_mmap_count;
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_mmap *mmap_tag = (struct multiboot_tag_mmap *)(mb2_info + info_idx);
|
2021-09-13 05:49:59 +03:00
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
mmap_tag->type = MULTIBOOT_TAG_TYPE_MMAP;
|
|
|
|
mmap_tag->entry_size = sizeof(struct multiboot_mmap_entry);
|
|
|
|
mmap_tag->entry_version = 0;
|
|
|
|
mmap_tag->size = mmap_size;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mb_mmap_count; i++) {
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_mmap_entry *entry = &mmap_tag->entries[i];
|
2021-09-09 11:40:07 +03:00
|
|
|
entry->addr = raw_memmap[i].base;
|
|
|
|
entry->len = raw_memmap[i].length;
|
|
|
|
entry->type = raw_memmap[i].type;
|
|
|
|
entry->zero = 0;
|
|
|
|
}
|
2021-09-12 08:05:59 +03:00
|
|
|
|
|
|
|
append_tag(info_idx, mmap_tag);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 05:04:41 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create basic memory info tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
|
|
|
struct meminfo meminfo = mmap_get_info(mb_mmap_count, raw_memmap);
|
|
|
|
struct multiboot_tag_basic_meminfo *tag = (struct multiboot_tag_basic_meminfo *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_BASIC_MEMINFO;
|
|
|
|
tag->size = sizeof(struct multiboot_tag_basic_meminfo);
|
|
|
|
|
|
|
|
// Convert the uppermem and lowermem fields from bytes to
|
|
|
|
// KiB.
|
|
|
|
tag->mem_upper = (uint32_t)(meminfo.uppermem / 1024);
|
|
|
|
tag->mem_lower = (uint32_t)(meminfo.lowermem / 1024);
|
|
|
|
|
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
|
2021-09-11 08:06:51 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create EFI memory map tag
|
|
|
|
//////////////////////////////////////////////
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (UEFI)
|
2021-09-11 08:06:51 +03:00
|
|
|
{
|
2021-09-12 13:08:44 +03:00
|
|
|
if ((efi_mmap_size / efi_desc_size) > 256) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(false, "multiboot2: too many EFI memory map entries");
|
2021-09-12 13:08:44 +03:00
|
|
|
}
|
|
|
|
|
2021-09-11 08:06:51 +03:00
|
|
|
// Create the EFI memory map tag.
|
2022-09-15 23:31:50 +03:00
|
|
|
uint32_t size = sizeof(struct multiboot_tag_efi_mmap) + efi_mmap_size;
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag_efi_mmap *mmap_tag = (struct multiboot_tag_efi_mmap *)(mb2_info + info_idx);
|
2021-09-11 08:06:51 +03:00
|
|
|
|
|
|
|
mmap_tag->type = MULTIBOOT_TAG_TYPE_EFI_MMAP;
|
|
|
|
mmap_tag->descr_vers = efi_desc_ver;
|
|
|
|
mmap_tag->descr_size = efi_desc_size;
|
2021-09-12 08:05:59 +03:00
|
|
|
mmap_tag->size = size;
|
2021-09-11 08:06:51 +03:00
|
|
|
|
|
|
|
// Copy over the EFI memory map.
|
|
|
|
memcpy(mmap_tag->efi_mmap, efi_mmap, efi_mmap_size);
|
2021-09-12 08:05:59 +03:00
|
|
|
append_tag(info_idx, mmap_tag);
|
2021-09-11 08:06:51 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-24 14:44:56 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create network info tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2023-04-28 05:59:08 +03:00
|
|
|
if (cached_dhcp_ack_valid) {
|
2023-04-24 14:44:56 +03:00
|
|
|
struct multiboot_tag_network *tag = (struct multiboot_tag_network *)(mb2_info + info_idx);
|
|
|
|
|
|
|
|
tag->type = MULTIBOOT_TAG_TYPE_NETWORK;
|
2023-04-28 05:59:08 +03:00
|
|
|
tag->size = sizeof(struct multiboot_tag_network) + DHCP_ACK_PACKET_LEN;
|
2023-04-24 14:44:56 +03:00
|
|
|
|
|
|
|
// Copy over the DHCP packet.
|
2023-04-28 05:59:08 +03:00
|
|
|
memcpy(tag->dhcpack, cached_dhcp_packet, DHCP_ACK_PACKET_LEN);
|
2023-04-24 14:44:56 +03:00
|
|
|
append_tag(info_idx, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 11:40:07 +03:00
|
|
|
//////////////////////////////////////////////
|
|
|
|
// Create end tag
|
|
|
|
//////////////////////////////////////////////
|
|
|
|
{
|
2021-09-14 08:43:17 +03:00
|
|
|
struct multiboot_tag *end_tag = (struct multiboot_tag *)(mb2_info + info_idx);
|
2021-09-09 11:40:07 +03:00
|
|
|
end_tag->type = MULTIBOOT_TAG_TYPE_END;
|
|
|
|
end_tag->size = sizeof(struct multiboot_tag);
|
2021-09-12 08:05:59 +03:00
|
|
|
|
|
|
|
append_tag(info_idx, end_tag);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:37:44 +03:00
|
|
|
mbi_start->size = info_idx;
|
|
|
|
mbi_start->reserved = 0x00;
|
|
|
|
|
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)0x36d76289,
|
|
|
|
(uint32_t)mb2_info_final_loc, (uint32_t)entry_point,
|
|
|
|
(uint32_t)(uintptr_t)ranges, (uint32_t)ranges_count);
|
2021-09-09 11:40:07 +03:00
|
|
|
}
|
2022-08-18 18:32:54 +03:00
|
|
|
|
|
|
|
#endif
|