[boot] Load programs in boot, not kernel
Remove ELF and initrd loading from the kernel. The bootloader now loads the initial programs, as it does with the kernel. Other files that were in the initrd are now on the ESP, and non-program files are just passed as modules.
This commit is contained in:
@@ -21,9 +21,24 @@ namespace kernel {
|
||||
#include "kernel_memory.h"
|
||||
}
|
||||
|
||||
namespace args = kernel::args;
|
||||
|
||||
namespace boot {
|
||||
|
||||
constexpr int max_modules = 10; // Max modules to allocate room for
|
||||
constexpr int max_modules = 5; // Max modules to allocate room for
|
||||
constexpr int max_programs = 5; // Max programs to allocate room for
|
||||
|
||||
struct program_desc
|
||||
{
|
||||
const wchar_t *name;
|
||||
const wchar_t *path;
|
||||
};
|
||||
|
||||
const program_desc program_list[] = {
|
||||
{L"kernel", L"jsix.elf"},
|
||||
{L"null driver", L"nulldrv.elf"},
|
||||
{L"terminal driver", L"terminal.elf"},
|
||||
};
|
||||
|
||||
/// Change a pointer to point to the higher-half linear-offset version
|
||||
/// of the address it points to.
|
||||
@@ -34,19 +49,21 @@ void change_pointer(T *&pointer)
|
||||
}
|
||||
|
||||
/// Allocate space for kernel args. Allocates enough space from pool
|
||||
/// memory for the args header and `max_modules` module headers.
|
||||
kernel::args::header *
|
||||
/// memory for the args header and the module and program headers.
|
||||
args::header *
|
||||
allocate_args_structure(
|
||||
uefi::boot_services *bs,
|
||||
size_t max_modules)
|
||||
size_t max_modules,
|
||||
size_t max_programs)
|
||||
{
|
||||
status_line status(L"Setting up kernel args memory");
|
||||
|
||||
kernel::args::header *args = nullptr;
|
||||
args::header *args = nullptr;
|
||||
|
||||
size_t args_size =
|
||||
sizeof(kernel::args::header) + // The header itself
|
||||
max_modules * sizeof(kernel::args::module); // The module structures
|
||||
sizeof(args::header) + // The header itself
|
||||
max_modules * sizeof(args::module) + // The module structures
|
||||
max_programs * sizeof(args::program); // The program structures
|
||||
|
||||
try_or_raise(
|
||||
bs->allocate_pool(memory::args_type, args_size,
|
||||
@@ -56,47 +73,34 @@ allocate_args_structure(
|
||||
bs->set_mem(args, args_size, 0);
|
||||
|
||||
args->modules =
|
||||
reinterpret_cast<kernel::args::module*>(args + 1);
|
||||
reinterpret_cast<args::module*>(args + 1);
|
||||
args->num_modules = 0;
|
||||
|
||||
args->programs =
|
||||
reinterpret_cast<args::program*>(args->modules + max_modules);
|
||||
args->num_programs = 0;
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
/// Load a file from disk into memory. Also adds an entry to the kernel
|
||||
/// args module headers pointing at the loaded data.
|
||||
/// \arg disk The opened UEFI filesystem to load from
|
||||
/// \arg args The kernel args header to update with module information
|
||||
/// \arg name Name of the module (informational only)
|
||||
/// \arg path Path on `disk` of the file to load
|
||||
/// \arg type Type specifier of this module (eg, initrd or kernel)
|
||||
kernel::args::module *
|
||||
load_module(
|
||||
fs::file &disk,
|
||||
kernel::args::header *args,
|
||||
const wchar_t *name,
|
||||
const wchar_t *path,
|
||||
kernel::args::mod_type type)
|
||||
/// Add a module to the kernel args list
|
||||
inline void
|
||||
add_module(args::header *args, args::mod_type type, buffer &data)
|
||||
{
|
||||
status_line status(L"Loading module", name);
|
||||
|
||||
fs::file file = disk.open(path);
|
||||
kernel::args::module &module = args->modules[args->num_modules++];
|
||||
module.type = type;
|
||||
module.location = file.load(&module.size, memory::module_type);
|
||||
|
||||
console::print(L" Loaded at: 0x%lx, %d bytes\r\n", module.location, module.size);
|
||||
return &module;
|
||||
args::module &m = args->modules[args->num_modules++];
|
||||
m.type = type;
|
||||
m.location = data.data;
|
||||
m.size = data.size;
|
||||
}
|
||||
|
||||
/// The main procedure for the portion of the loader that runs while
|
||||
/// UEFI is still in control of the machine. (ie, while the loader still
|
||||
/// has access to boot services.
|
||||
kernel::args::header *
|
||||
args::header *
|
||||
bootloader_main_uefi(
|
||||
uefi::handle image,
|
||||
uefi::system_table *st,
|
||||
console &con,
|
||||
kernel::entrypoint *kentry)
|
||||
console &con)
|
||||
{
|
||||
error::uefi_handler handler(con);
|
||||
status_line status(L"Performing UEFI pre-boot");
|
||||
@@ -105,27 +109,32 @@ bootloader_main_uefi(
|
||||
uefi::runtime_services *rs = st->runtime_services;
|
||||
memory::init_pointer_fixup(bs, rs);
|
||||
|
||||
kernel::args::header *args =
|
||||
allocate_args_structure(bs, max_modules);
|
||||
args::header *args =
|
||||
allocate_args_structure(bs, max_modules, max_programs);
|
||||
|
||||
args->magic = kernel::args::magic;
|
||||
args->version = kernel::args::version;
|
||||
args->magic = args::magic;
|
||||
args->version = args::version;
|
||||
args->runtime_services = rs;
|
||||
args->acpi_table = hw::find_acpi_table(st);
|
||||
paging::allocate_tables(args, bs);
|
||||
|
||||
memory::mark_pointer_fixup(&args->runtime_services);
|
||||
|
||||
fs::file disk = fs::get_boot_volume(image, bs);
|
||||
load_module(disk, args, L"initrd", L"initrd.img", kernel::args::mod_type::initrd);
|
||||
|
||||
kernel::args::module *kernel =
|
||||
load_module(disk, args, L"kernel", L"jsix.elf", kernel::args::mod_type::kernel);
|
||||
const uefi::memory_type mod_type = memory::module_type;
|
||||
buffer symbols = loader::load_file(disk, L"symbol table", L"symbol_table.dat",
|
||||
memory::module_type);
|
||||
add_module(args, args::mod_type::symbol_table, symbols);
|
||||
|
||||
paging::allocate_tables(args, bs);
|
||||
*kentry = loader::load(kernel->location, kernel->size, args, bs);
|
||||
for (auto &desc : program_list) {
|
||||
buffer buf = loader::load_file(disk, desc.name, desc.path);
|
||||
args::program &program = args->programs[args->num_programs++];
|
||||
loader::load_program(program, desc.name, buf, bs);
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < args->num_modules; ++i) {
|
||||
kernel::args::module &mod = args->modules[i];
|
||||
args::module &mod = args->modules[i];
|
||||
change_pointer(mod.location);
|
||||
}
|
||||
|
||||
@@ -143,9 +152,13 @@ efi_main(uefi::handle image_handle, uefi::system_table *st)
|
||||
error::cpu_assert_handler handler;
|
||||
console con(st->boot_services, st->con_out);
|
||||
|
||||
kernel::entrypoint kentry = nullptr;
|
||||
kernel::args::header *args =
|
||||
bootloader_main_uefi(image_handle, st, con, &kentry);
|
||||
args::header *args =
|
||||
bootloader_main_uefi(image_handle, st, con);
|
||||
|
||||
args::program &kernel = args->programs[0];
|
||||
paging::map_pages(args, kernel.phys_addr, kernel.virt_addr, kernel.size);
|
||||
kernel::entrypoint kentry =
|
||||
reinterpret_cast<kernel::entrypoint>(kernel.entrypoint);
|
||||
|
||||
memory::efi_mem_map map =
|
||||
memory::build_kernel_mem_map(args, st->boot_services);
|
||||
|
||||
Reference in New Issue
Block a user