lanzaboote/rust/lanzaboote/src/main.rs

123 lines
3.1 KiB
Rust
Raw Normal View History

2022-11-21 05:44:04 -05:00
#![no_main]
#![no_std]
#![feature(abi_efiapi)]
2022-11-22 18:25:52 -05:00
#![feature(negative_impls)]
2022-11-21 05:44:04 -05:00
extern crate alloc;
2022-11-21 10:22:44 -05:00
2022-11-22 18:25:52 -05:00
mod linux_loader;
2022-11-22 10:18:12 -05:00
mod pe_section;
2022-11-22 10:24:09 -05:00
mod uefi_helpers;
2022-11-22 10:18:12 -05:00
use log::{debug, info};
use uefi::{
prelude::*,
proto::{
console::text::Output,
2022-11-22 10:18:12 -05:00
loaded_image::LoadedImage,
media::file::{File, FileAttribute, FileMode},
},
};
2022-11-21 10:22:44 -05:00
use crate::{
linux_loader::InitrdLoader,
pe_section::pe_section,
uefi_helpers::{booted_image_cmdline, booted_image_file, read_all},
};
2022-11-22 10:18:12 -05:00
2022-11-21 10:22:44 -05:00
fn print_logo(output: &mut Output) {
output.clear().unwrap();
output
.output_string(cstr16!(
"
_ _ _ \r
| | | | | | \r
| | __ _ _ __ ______ _| |__ ___ ___ | |_ \r
| |/ _` | '_ \\|_ / _` | '_ \\ / _ \\ / _ \\| __|\r
| | (_| | | | |/ / (_| | |_) | (_) | (_) | |_ \r
|_|\\__,_|_| |_/___\\__,_|_.__/ \\___/ \\___/ \\__|\r
2022-11-22 10:18:12 -05:00
\r
2022-11-21 10:22:44 -05:00
"
))
.unwrap();
}
2022-11-21 05:44:04 -05:00
#[entry]
fn main(handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
2022-11-21 05:44:04 -05:00
uefi_services::init(&mut system_table).unwrap();
2022-11-21 10:22:44 -05:00
print_logo(system_table.stdout());
2022-11-21 09:36:39 -05:00
2022-11-22 10:18:12 -05:00
{
let image_data =
read_all(&mut booted_image_file(system_table.boot_services()).unwrap()).unwrap();
2022-11-22 10:18:12 -05:00
if let Some(data) = pe_section(&image_data, ".osrel") {
info!("osrel = {}", core::str::from_utf8(data).unwrap_or("???"))
}
}
2022-11-22 05:50:05 -05:00
let mut file_system = system_table
.boot_services()
.get_image_file_system(handle)
.unwrap();
let mut root = file_system.open_volume().unwrap();
let mut file = root
2022-11-21 19:39:05 -05:00
.open(cstr16!("linux.efi"), FileMode::Read, FileAttribute::empty())
.unwrap()
.into_regular_file()
.unwrap();
2022-11-22 18:25:52 -05:00
let initrd = root
.open(cstr16!("initrd"), FileMode::Read, FileAttribute::empty())
.unwrap()
.into_regular_file()
.unwrap();
// We need to manually drop those to be able to touch the system_table again.
drop(root);
drop(file_system);
debug!("Opened file");
let kernel_cmdline = booted_image_cmdline(system_table.boot_services()).unwrap();
2022-11-21 19:39:05 -05:00
let kernel_data = read_all(&mut file).unwrap();
let kernel_handle = system_table
.boot_services()
2022-11-21 19:39:05 -05:00
.load_image(
handle,
uefi::table::boot::LoadImageSource::FromBuffer {
buffer: &kernel_data,
2022-11-21 19:39:05 -05:00
file_path: None,
},
)
.unwrap();
let mut kernel_image = system_table
.boot_services()
.open_protocol_exclusive::<LoadedImage>(kernel_handle)
.unwrap();
unsafe {
kernel_image.set_load_options(
kernel_cmdline.as_ptr() as *const u8,
u32::try_from(kernel_cmdline.len()).unwrap(),
);
}
let mut initrd_loader =
InitrdLoader::new(&system_table.boot_services(), handle, initrd).unwrap();
let status = system_table
.boot_services()
.start_image(kernel_handle)
.status();
initrd_loader
.uninstall(&system_table.boot_services())
.unwrap();
2022-11-22 18:25:52 -05:00
status
2022-11-21 05:44:04 -05:00
}