mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
Loading files
This commit is contained in:
@@ -1,25 +1,43 @@
|
|||||||
#include <uefi/types.h>
|
#include <uefi/types.h>
|
||||||
|
#include <uefi/protos/file.h>
|
||||||
|
#include <uefi/protos/file_info.h>
|
||||||
#include <uefi/protos/loaded_image.h>
|
#include <uefi/protos/loaded_image.h>
|
||||||
#include <uefi/protos/simple_file_system.h>
|
#include <uefi/protos/simple_file_system.h>
|
||||||
|
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
#include "console.h"
|
#include "console.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
namespace boot {
|
namespace boot {
|
||||||
namespace fs {
|
namespace fs {
|
||||||
|
|
||||||
file::file(uefi::protos::file *f) :
|
file::file(uefi::protos::file *f, uefi::boot_services *bs) :
|
||||||
m_file(f)
|
m_file(f),
|
||||||
|
m_bs(bs)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
file::file(file &&o) :
|
file::file(file &o) :
|
||||||
m_file(o.m_file)
|
m_file(o.m_file),
|
||||||
|
m_bs(o.m_bs)
|
||||||
{
|
{
|
||||||
o.m_file = nullptr;
|
o.m_file = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file::file(file &&o) :
|
||||||
|
m_file(o.m_file),
|
||||||
|
m_bs(o.m_bs)
|
||||||
|
{
|
||||||
|
o.m_file = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
file::~file()
|
||||||
|
{
|
||||||
|
if (m_file)
|
||||||
|
m_file->close();
|
||||||
|
}
|
||||||
|
|
||||||
file
|
file
|
||||||
file::open(const wchar_t *path)
|
file::open(const wchar_t *path)
|
||||||
{
|
{
|
||||||
@@ -29,7 +47,38 @@ file::open(const wchar_t *path)
|
|||||||
m_file->open(&fh, path, uefi::file_mode::read, uefi::file_attr::none),
|
m_file->open(&fh, path, uefi::file_mode::read, uefi::file_attr::none),
|
||||||
L"Could not open relative path to file");
|
L"Could not open relative path to file");
|
||||||
|
|
||||||
return file(fh);
|
return file(fh, m_bs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
file::load(size_t *out_size)
|
||||||
|
{
|
||||||
|
uint8_t buffer[sizeof(uefi::protos::file_info) + 100];
|
||||||
|
size_t size = sizeof(buffer);
|
||||||
|
uefi::guid info_guid = uefi::protos::file_info::guid;
|
||||||
|
|
||||||
|
try_or_raise(
|
||||||
|
m_file->get_info(&info_guid, &size, &buffer),
|
||||||
|
L"Could not get file info");
|
||||||
|
|
||||||
|
uefi::protos::file_info *info =
|
||||||
|
reinterpret_cast<uefi::protos::file_info*>(&buffer);
|
||||||
|
|
||||||
|
size_t pages = memory::bytes_to_pages(info->file_size);
|
||||||
|
void *data = nullptr;
|
||||||
|
try_or_raise(
|
||||||
|
m_bs->allocate_pages(
|
||||||
|
uefi::allocate_type::any_pages,
|
||||||
|
uefi::memory_type::loader_data,
|
||||||
|
pages, &data),
|
||||||
|
L"Could not allocate pages to load file");
|
||||||
|
|
||||||
|
try_or_raise(
|
||||||
|
m_file->read(&size, data),
|
||||||
|
L"Could not read from file");
|
||||||
|
|
||||||
|
*out_size = size;
|
||||||
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
file
|
file
|
||||||
@@ -57,7 +106,7 @@ get_boot_volume(uefi::handle image, uefi::boot_services *bs)
|
|||||||
fs->open_volume(&f),
|
fs->open_volume(&f),
|
||||||
L"Could not open the boot volume");
|
L"Could not open the boot volume");
|
||||||
|
|
||||||
return file(f);
|
return file(f, bs);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fs
|
} // namespace fs
|
||||||
|
|||||||
@@ -10,13 +10,20 @@ namespace fs {
|
|||||||
class file
|
class file
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
file(uefi::protos::file *f);
|
|
||||||
file(file &&o);
|
file(file &&o);
|
||||||
|
file(file &o);
|
||||||
|
~file();
|
||||||
|
|
||||||
file open(const wchar_t *path);
|
file open(const wchar_t *path);
|
||||||
|
void * load(size_t *out_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend file get_boot_volume(uefi::handle, uefi::boot_services*);
|
||||||
|
|
||||||
|
file(uefi::protos::file *f, uefi::boot_services *bs);
|
||||||
|
|
||||||
uefi::protos::file *m_file;
|
uefi::protos::file *m_file;
|
||||||
|
uefi::boot_services *m_bs;
|
||||||
};
|
};
|
||||||
|
|
||||||
file get_boot_volume(uefi::handle image, uefi::boot_services *bs);
|
file get_boot_volume(uefi::handle image, uefi::boot_services *bs);
|
||||||
|
|||||||
@@ -104,19 +104,24 @@ bootloader_main_uefi(uefi::handle image, uefi::system_table *st, console &con)
|
|||||||
args->acpi_table = hw::find_acpi_table(st);
|
args->acpi_table = hw::find_acpi_table(st);
|
||||||
|
|
||||||
fs::file disk = fs::get_boot_volume(image, bs);
|
fs::file disk = fs::get_boot_volume(image, bs);
|
||||||
fs::file kernel = disk.open(L"jsix.elf");
|
|
||||||
|
|
||||||
{
|
{
|
||||||
status_line status(L"Loading modules");
|
status_line status(L"Loading modules");
|
||||||
{
|
{
|
||||||
status_line status(L"Finding boot device");
|
status_line status(L"initrd");
|
||||||
|
|
||||||
|
fs::file file = disk.open(L"initrd.img");
|
||||||
|
kernel::args::module &module = args->modules[args->num_modules++];
|
||||||
|
module.type = kernel::args::type::initrd;
|
||||||
|
module.location = file.load(&module.size);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
status_line status(L"Loading initrd into memory");
|
status_line status(L"kernel");
|
||||||
status_line::warn(L"I can't even");
|
|
||||||
|
|
||||||
kernel::args::module &initrd = args->modules[args->num_modules++];
|
fs::file file = disk.open(L"jsix.elf");
|
||||||
initrd.type = kernel::args::type::initrd;
|
kernel::args::module &module = args->modules[args->num_modules++];
|
||||||
|
module.type = kernel::args::type::kernel;
|
||||||
|
module.location = file.load(&module.size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ init_pointer_fixup(uefi::boot_services *bs, uefi::runtime_services *rs)
|
|||||||
|
|
||||||
// Reserve a page for our replacement PML4, plus some pages for the kernel to use
|
// Reserve a page for our replacement PML4, plus some pages for the kernel to use
|
||||||
// as page tables while it gets started.
|
// as page tables while it gets started.
|
||||||
uintptr_t addr = 0;
|
void *addr = nullptr;
|
||||||
try_or_raise(
|
try_or_raise(
|
||||||
bs->allocate_pages(
|
bs->allocate_pages(
|
||||||
uefi::allocate_type::any_pages,
|
uefi::allocate_type::any_pages,
|
||||||
@@ -97,7 +97,7 @@ init_pointer_fixup(uefi::boot_services *bs, uefi::runtime_services *rs)
|
|||||||
&addr),
|
&addr),
|
||||||
L"Error allocating page table pages.");
|
L"Error allocating page table pages.");
|
||||||
|
|
||||||
new_pml4 = (uint64_t *)addr;
|
new_pml4 = reinterpret_cast<uint64_t*>(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -7,6 +7,12 @@
|
|||||||
namespace boot {
|
namespace boot {
|
||||||
namespace memory {
|
namespace memory {
|
||||||
|
|
||||||
|
constexpr size_t page_size = 0x1000;
|
||||||
|
|
||||||
|
inline constexpr size_t bytes_to_pages(size_t bytes) {
|
||||||
|
return ((bytes - 1) / page_size) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
void init_pointer_fixup(uefi::boot_services *bs, uefi::runtime_services *rs);
|
void init_pointer_fixup(uefi::boot_services *bs, uefi::runtime_services *rs);
|
||||||
void mark_pointer_fixup(void **p);
|
void mark_pointer_fixup(void **p);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user