2020-04-19 11:14:49 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <protos/linux.h>
|
|
|
|
#include <fs/file.h>
|
2020-12-01 05:19:46 +03:00
|
|
|
#include <lib/libc.h>
|
2020-04-19 11:14:49 +03:00
|
|
|
#include <lib/blib.h>
|
|
|
|
#include <lib/real.h>
|
2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2020-04-19 14:20:26 +03:00
|
|
|
#include <lib/config.h>
|
2020-05-10 01:38:27 +03:00
|
|
|
#include <lib/print.h>
|
2020-11-02 11:20:34 +03:00
|
|
|
#include <lib/uri.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-27 18:39:30 +03:00
|
|
|
#include <mm/mtrr.h>
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2020-05-03 00:38:57 +03:00
|
|
|
#define KERNEL_LOAD_ADDR ((size_t)0x100000)
|
2021-01-27 19:24:56 +03:00
|
|
|
#define KERNEL_HEAP_SIZE ((size_t)0x6000)
|
2020-05-03 00:38:57 +03:00
|
|
|
|
2020-09-25 23:57:57 +03:00
|
|
|
__attribute__((section(".realmode"), used))
|
2021-01-27 18:16:36 +03:00
|
|
|
static void spinup(uint16_t real_mode_code_seg, uint16_t kernel_entry_seg,
|
|
|
|
uint16_t stack_pointer) {
|
2020-09-25 23:57:57 +03:00
|
|
|
asm volatile (
|
|
|
|
"cld\n\t"
|
|
|
|
|
|
|
|
"jmp 0x08:1f\n\t"
|
|
|
|
"1: .code16\n\t"
|
|
|
|
"mov ax, 0x10\n\t"
|
|
|
|
"mov ds, ax\n\t"
|
|
|
|
"mov es, ax\n\t"
|
|
|
|
"mov fs, ax\n\t"
|
|
|
|
"mov gs, ax\n\t"
|
|
|
|
"mov ss, ax\n\t"
|
|
|
|
"mov eax, cr0\n\t"
|
|
|
|
"and al, 0xfe\n\t"
|
|
|
|
"mov cr0, eax\n\t"
|
|
|
|
"jmp 0x0000:1f\n\t"
|
|
|
|
"1:\n\t"
|
|
|
|
"mov ds, bx\n\t"
|
|
|
|
"mov es, bx\n\t"
|
|
|
|
"mov fs, bx\n\t"
|
|
|
|
"mov gs, bx\n\t"
|
|
|
|
"mov ss, bx\n\t"
|
2021-01-27 18:16:36 +03:00
|
|
|
"mov esp, edx\n\t"
|
2020-09-25 23:57:57 +03:00
|
|
|
|
|
|
|
"push cx\n\t"
|
|
|
|
"push 0\n\t"
|
2021-01-27 18:16:36 +03:00
|
|
|
|
2020-09-25 23:57:57 +03:00
|
|
|
"retf\n\t"
|
|
|
|
|
|
|
|
".code32\n\t"
|
|
|
|
:
|
2021-01-27 18:16:36 +03:00
|
|
|
: "b" (real_mode_code_seg), "c" (kernel_entry_seg),
|
|
|
|
"d" (stack_pointer)
|
2020-09-25 23:57:57 +03:00
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
void linux_load(char *config, char *cmdline) {
|
2020-12-31 05:26:19 +03:00
|
|
|
struct file_handle *kernel = ext_mem_alloc(sizeof(struct file_handle));
|
2020-10-17 07:23:11 +03:00
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
char *kernel_path = config_get_value(config, 0, "KERNEL_PATH");
|
|
|
|
if (kernel_path == NULL)
|
2020-10-17 07:23:11 +03:00
|
|
|
panic("KERNEL_PATH not specified");
|
|
|
|
|
2020-11-27 21:33:34 +03:00
|
|
|
if (!uri_open(kernel, kernel_path))
|
2020-10-17 07:23:11 +03:00
|
|
|
panic("Could not open kernel resource");
|
2020-05-06 17:38:45 +03:00
|
|
|
|
2020-04-19 11:14:49 +03:00
|
|
|
uint32_t signature;
|
2020-10-17 07:23:11 +03:00
|
|
|
fread(kernel, &signature, 0x202, sizeof(uint32_t));
|
2020-04-19 11:14:49 +03:00
|
|
|
|
|
|
|
// validate signature
|
|
|
|
if (signature != 0x53726448) {
|
|
|
|
panic("Invalid Linux kernel signature");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t setup_code_size = 0;
|
2020-10-17 07:23:11 +03:00
|
|
|
fread(kernel, &setup_code_size, 0x1f1, 1);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
|
|
|
if (setup_code_size == 0)
|
|
|
|
setup_code_size = 4;
|
|
|
|
|
|
|
|
setup_code_size *= 512;
|
|
|
|
|
|
|
|
size_t real_mode_code_size = 512 + setup_code_size;
|
|
|
|
|
2021-01-27 19:24:56 +03:00
|
|
|
size_t real_mode_and_heap_size = 0x8000 + KERNEL_HEAP_SIZE;
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2021-01-27 19:24:56 +03:00
|
|
|
void *real_mode_code = conv_mem_alloc_aligned(0x10000, 0x1000);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
fread(kernel, real_mode_code, 0, real_mode_code_size);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
|
|
|
uint16_t boot_protocol_ver;
|
|
|
|
boot_protocol_ver = *((uint16_t *)(real_mode_code + 0x206));
|
|
|
|
|
|
|
|
print("linux: Boot protocol: %u.%u\n",
|
|
|
|
boot_protocol_ver >> 8, boot_protocol_ver & 0xff);
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
if (boot_protocol_ver < 0x203) {
|
|
|
|
panic("Linux protocols < 2.03 are not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t heap_end_ptr = real_mode_and_heap_size - 0x200;
|
|
|
|
*((uint16_t *)(real_mode_code + 0x224)) = (uint16_t)heap_end_ptr;
|
|
|
|
|
2021-01-27 19:24:56 +03:00
|
|
|
char *cmdline_reloc = real_mode_code + real_mode_and_heap_size;
|
2021-01-27 18:16:36 +03:00
|
|
|
strcpy(cmdline_reloc, cmdline);
|
|
|
|
|
|
|
|
// vid_mode. 0xffff means "normal"
|
|
|
|
*((uint16_t *)(real_mode_code + 0x1fa)) = 0xffff;
|
|
|
|
|
2020-04-19 11:14:49 +03:00
|
|
|
char *kernel_version;
|
|
|
|
kernel_version = real_mode_code + *((uint16_t *)(real_mode_code + 0x20e)) + 0x200;
|
|
|
|
|
|
|
|
if (kernel_version) {
|
|
|
|
print("linux: Kernel version: %s\n", kernel_version);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set type of loader
|
|
|
|
*((uint8_t *)(real_mode_code + 0x210)) = 0xff;
|
|
|
|
|
|
|
|
uint8_t loadflags;
|
|
|
|
loadflags = *((uint8_t *)(real_mode_code + 0x211));
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
if (!(loadflags & (1 << 0))) {
|
|
|
|
panic("Linux kernels that load at 0x10000 are not supported");
|
|
|
|
}
|
|
|
|
|
2020-04-19 11:14:49 +03:00
|
|
|
loadflags &= ~(1 << 5); // print early messages
|
|
|
|
loadflags |= (1 << 7); // can use heap
|
|
|
|
|
|
|
|
*((uint8_t *)(real_mode_code + 0x211)) = loadflags;
|
|
|
|
|
2020-12-01 05:19:46 +03:00
|
|
|
*((uint32_t *)(real_mode_code + 0x228)) = (uint32_t)cmdline_reloc;
|
2020-04-19 11:14:49 +03:00
|
|
|
|
|
|
|
// load kernel
|
2021-01-27 19:24:56 +03:00
|
|
|
print("linux: Loading kernel...\n");
|
2021-01-27 18:16:36 +03:00
|
|
|
memmap_alloc_range(KERNEL_LOAD_ADDR, kernel->size - real_mode_code_size, 0, true, true);
|
2020-10-17 07:23:11 +03:00
|
|
|
fread(kernel, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, kernel->size - real_mode_code_size);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
uint32_t modules_mem_base = *((uint32_t *)(real_mode_code + 0x22c)) + 1;
|
2021-01-27 19:24:56 +03:00
|
|
|
if (modules_mem_base == 0)
|
|
|
|
modules_mem_base = 0x38000000;
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
size_t size_of_all_modules = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; ; i++) {
|
|
|
|
char *module_path = config_get_value(config, i, "MODULE_PATH");
|
|
|
|
if (module_path == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
struct file_handle module;
|
|
|
|
if (!uri_open(&module, module_path))
|
|
|
|
panic("Could not open `%s`", module_path);
|
|
|
|
|
|
|
|
size_of_all_modules += module.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
modules_mem_base -= size_of_all_modules;
|
|
|
|
modules_mem_base = ALIGN_DOWN(modules_mem_base, 4096);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (memmap_alloc_range(modules_mem_base, size_of_all_modules, 0, true, false))
|
|
|
|
break;
|
|
|
|
modules_mem_base -= 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t _modules_mem_base = modules_mem_base;
|
2020-11-08 00:21:30 +03:00
|
|
|
for (size_t i = 0; ; i++) {
|
2020-11-27 21:33:34 +03:00
|
|
|
char *module_path = config_get_value(config, i, "MODULE_PATH");
|
|
|
|
if (module_path == NULL)
|
2020-11-08 00:21:30 +03:00
|
|
|
break;
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2020-11-08 00:21:30 +03:00
|
|
|
struct file_handle module;
|
2020-11-27 21:33:34 +03:00
|
|
|
if (!uri_open(&module, module_path))
|
|
|
|
panic("Could not open `%s`", module_path);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2021-01-27 19:24:56 +03:00
|
|
|
print("linux: Loading module `%s`...\n", module_path);
|
2020-04-19 11:14:49 +03:00
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
fread(&module, (void *)_modules_mem_base, 0, module.size);
|
2020-11-08 00:21:30 +03:00
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
_modules_mem_base += module.size;
|
2020-11-08 00:21:30 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
if (size_of_all_modules != 0) {
|
|
|
|
*((uint32_t *)(real_mode_code + 0x218)) = (uint32_t)modules_mem_base;
|
|
|
|
*((uint32_t *)(real_mode_code + 0x21c)) = (uint32_t)size_of_all_modules;
|
2020-11-08 00:21:30 +03:00
|
|
|
}
|
2020-04-19 11:14:49 +03:00
|
|
|
|
|
|
|
uint16_t real_mode_code_seg = rm_seg(real_mode_code);
|
|
|
|
uint16_t kernel_entry_seg = real_mode_code_seg + 0x20;
|
|
|
|
|
2020-09-02 10:55:56 +03:00
|
|
|
term_deinit();
|
2020-04-19 14:20:26 +03:00
|
|
|
|
2020-09-27 18:39:30 +03:00
|
|
|
mtrr_restore();
|
|
|
|
|
2021-01-27 18:16:36 +03:00
|
|
|
spinup(real_mode_code_seg, kernel_entry_seg, real_mode_and_heap_size);
|
2020-04-19 11:14:49 +03:00
|
|
|
}
|