mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[bootproto] Create new bootproto lib
This is a rather large commit that is widely focused on cleaning things out of the 'junk drawer' that is src/include. Most notably, several things that were put in there because they needed somewhere where both the kernel, boot, and init could read them have been moved to a new lib, 'bootproto'. - Moved kernel_args.h and init_args.h to bootproto as kernel.h and init.h, respectively. - Moved counted.h and pointer_manipulation.h into util, renaming the latter to util/pointers.h. - Created a new src/include/arch for very arch-dependent definitions, and moved some kernel_memory.h constants like frame size, page table entry count, etc to arch/amd64/memory.h. Also created arch/memory.h which detects platform and includes the former. - Got rid of kernel_memory.h entirely in favor of a new, cog-based approach. The new definitions/memory_layout.csv lists memory regions in descending order from the top of memory, their sizes, and whether they are shared outside the kernel (ie, boot needs to know them). The new header bootproto/memory.h exposes the addresses of the shared regions, while the kernel's memory.h gains the start and size of all the regions. Also renamed the badly-named page-offset area the linear area. - The python build scripts got a few new features: the ability to parse the csv mentioned above in a new memory.py module; the ability to add dependencies to existing source files (The list of files that I had to pull out of the main list just to add them with the dependency on memory.h was getting too large. So I put them back into the sources list, and added the dependency post-hoc.); and the ability to reference 'source_root', 'build_root', and 'module_root' variables in .module files. - Some utility functions that were in the kernel's memory.h got moved to util/pointers.h and util/misc.h, and misc.h's byteswap was renamed byteswap32 to be more specific.
This commit is contained in:
@@ -1,27 +1,26 @@
|
||||
#include <uefi/boot_services.h>
|
||||
#include <uefi/types.h>
|
||||
|
||||
#include <bootproto/init.h>
|
||||
#include <elf/file.h>
|
||||
#include <elf/headers.h>
|
||||
#include <util/pointers.h>
|
||||
|
||||
#include "allocator.h"
|
||||
#include "console.h"
|
||||
#include "elf/file.h"
|
||||
#include "elf/headers.h"
|
||||
#include "error.h"
|
||||
#include "fs.h"
|
||||
#include "init_args.h"
|
||||
#include "loader.h"
|
||||
#include "memory.h"
|
||||
#include "paging.h"
|
||||
#include "pointer_manipulation.h"
|
||||
#include "status.h"
|
||||
|
||||
namespace init = kernel::init;
|
||||
|
||||
namespace boot {
|
||||
namespace loader {
|
||||
|
||||
using memory::alloc_type;
|
||||
|
||||
buffer
|
||||
util::buffer
|
||||
load_file(
|
||||
fs::file &disk,
|
||||
const program_desc &desc)
|
||||
@@ -29,7 +28,7 @@ load_file(
|
||||
status_line status(L"Loading file", desc.path);
|
||||
|
||||
fs::file file = disk.open(desc.path);
|
||||
buffer b = file.load();
|
||||
util::buffer b = file.load();
|
||||
|
||||
//console::print(L" Loaded at: 0x%lx, %d bytes\r\n", b.data, b.size);
|
||||
return b;
|
||||
@@ -37,17 +36,17 @@ load_file(
|
||||
|
||||
|
||||
static void
|
||||
create_module(buffer data, const program_desc &desc, bool loaded)
|
||||
create_module(util::buffer data, const program_desc &desc, bool loaded)
|
||||
{
|
||||
size_t path_len = wstrlen(desc.path);
|
||||
init::module_program *mod = g_alloc.allocate_module<init::module_program>(path_len);
|
||||
mod->mod_type = init::module_type::program;
|
||||
bootproto::module_program *mod = g_alloc.allocate_module<bootproto::module_program>(path_len);
|
||||
mod->mod_type = bootproto::module_type::program;
|
||||
mod->base_address = reinterpret_cast<uintptr_t>(data.pointer);
|
||||
mod->size = data.count;
|
||||
if (loaded)
|
||||
mod->mod_flags = static_cast<init::module_flags>(
|
||||
mod->mod_flags = static_cast<bootproto::module_flags>(
|
||||
static_cast<uint8_t>(mod->mod_flags) |
|
||||
static_cast<uint8_t>(init::module_flags::no_load));
|
||||
static_cast<uint8_t>(bootproto::module_flags::no_load));
|
||||
|
||||
// TODO: support non-ascii path characters and do real utf-16 to utf-8
|
||||
// conversion
|
||||
@@ -56,7 +55,7 @@ create_module(buffer data, const program_desc &desc, bool loaded)
|
||||
mod->filename[path_len] = 0;
|
||||
}
|
||||
|
||||
init::program *
|
||||
bootproto::program *
|
||||
load_program(
|
||||
fs::file &disk,
|
||||
const program_desc &desc,
|
||||
@@ -64,7 +63,7 @@ load_program(
|
||||
{
|
||||
status_line status(L"Loading program", desc.name);
|
||||
|
||||
buffer data = load_file(disk, desc);
|
||||
util::buffer data = load_file(disk, desc);
|
||||
|
||||
if (add_module)
|
||||
create_module(data, desc, true);
|
||||
@@ -79,20 +78,20 @@ load_program(
|
||||
++num_sections;
|
||||
}
|
||||
|
||||
init::program_section *sections = new init::program_section [num_sections];
|
||||
bootproto::program_section *sections = new bootproto::program_section [num_sections];
|
||||
|
||||
size_t next_section = 0;
|
||||
for (auto &seg : program.programs()) {
|
||||
if (seg.type != elf::segment_type::load)
|
||||
continue;
|
||||
|
||||
init::program_section §ion = sections[next_section++];
|
||||
bootproto::program_section §ion = sections[next_section++];
|
||||
|
||||
size_t page_count = memory::bytes_to_pages(seg.mem_size);
|
||||
|
||||
if (seg.mem_size > seg.file_size) {
|
||||
void *pages = g_alloc.allocate_pages(page_count, alloc_type::program, true);
|
||||
void *source = offset_ptr<void>(data.pointer, seg.offset);
|
||||
void *source = util::offset_pointer(data.pointer, seg.offset);
|
||||
g_alloc.copy(pages, source, seg.file_size);
|
||||
section.phys_addr = reinterpret_cast<uintptr_t>(pages);
|
||||
} else {
|
||||
@@ -101,10 +100,10 @@ load_program(
|
||||
|
||||
section.virt_addr = seg.vaddr;
|
||||
section.size = seg.mem_size;
|
||||
section.type = static_cast<init::section_flags>(seg.flags);
|
||||
section.type = static_cast<bootproto::section_flags>(seg.flags);
|
||||
}
|
||||
|
||||
init::program *prog = new init::program;
|
||||
bootproto::program *prog = new bootproto::program;
|
||||
prog->sections = { .pointer = sections, .count = num_sections };
|
||||
prog->phys_base = program.base();
|
||||
prog->entrypoint = program.entrypoint();
|
||||
@@ -118,25 +117,25 @@ load_module(
|
||||
{
|
||||
status_line status(L"Loading module", desc.name);
|
||||
|
||||
buffer data = load_file(disk, desc);
|
||||
util::buffer data = load_file(disk, desc);
|
||||
create_module(data, desc, false);
|
||||
}
|
||||
|
||||
void
|
||||
verify_kernel_header(init::program &program)
|
||||
verify_kernel_header(bootproto::program &program)
|
||||
{
|
||||
status_line status(L"Verifying kernel header");
|
||||
|
||||
const init::header *header =
|
||||
reinterpret_cast<const init::header *>(program.sections[0].phys_addr);
|
||||
const bootproto::header *header =
|
||||
reinterpret_cast<const bootproto::header *>(program.sections[0].phys_addr);
|
||||
|
||||
if (header->magic != init::header_magic)
|
||||
if (header->magic != bootproto::header_magic)
|
||||
error::raise(uefi::status::load_error, L"Bad kernel magic number");
|
||||
|
||||
if (header->length < sizeof(init::header))
|
||||
if (header->length < sizeof(bootproto::header))
|
||||
error::raise(uefi::status::load_error, L"Bad kernel header length");
|
||||
|
||||
if (header->version < init::min_header_version)
|
||||
if (header->version < bootproto::min_header_version)
|
||||
error::raise(uefi::status::unsupported, L"Kernel header version not supported");
|
||||
|
||||
console::print(L" Loaded kernel vserion: %d.%d.%d %x\r\n",
|
||||
|
||||
Reference in New Issue
Block a user