Skip to content

Commit

Permalink
Update test-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop committed May 2, 2024
1 parent 9121ad1 commit 515a508
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 181 deletions.
48 changes: 21 additions & 27 deletions uefi-test-runner/src/bin/shell_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ use uefi::proto::device_path::build::{self, DevicePathBuilder};
use uefi::proto::device_path::{DevicePath, DeviceSubType, DeviceType, LoadedImageDevicePath};
use uefi::proto::loaded_image::LoadedImage;
use uefi::table::boot::LoadImageSource;
use uefi::{boot, Result};

/// Get the device path of the shell app. This is the same as the
/// currently-loaded image's device path, but with the file path part changed.
fn get_shell_app_device_path<'a>(
boot_services: &BootServices,
storage: &'a mut Vec<u8>,
) -> &'a DevicePath {
let loaded_image_device_path = boot_services
.open_protocol_exclusive::<LoadedImageDevicePath>(boot_services.image_handle())
.expect("failed to open LoadedImageDevicePath protocol");
fn get_shell_app_device_path(storage: &mut Vec<u8>) -> &DevicePath {
let loaded_image_device_path =
boot::open_protocol_exclusive::<LoadedImageDevicePath>(boot::image_handle())
.expect("failed to open LoadedImageDevicePath protocol");

let mut builder = DevicePathBuilder::with_vec(storage);
for node in loaded_image_device_path.node_iter() {
Expand All @@ -44,29 +42,25 @@ fn get_shell_app_device_path<'a>(
builder.finalize().unwrap()
}

#[entry]
fn efi_main(image: Handle, mut st: SystemTable<Boot>) -> Status {
uefi::helpers::init(&mut st).unwrap();
let boot_services = st.boot_services();
fn main() -> Result {
uefi::helpers::init_v2().unwrap();

let mut storage = Vec::new();
let shell_image_path = get_shell_app_device_path(boot_services, &mut storage);
let shell_image_path = get_shell_app_device_path(&mut storage);

// Load the shell app.
let shell_image_handle = boot_services
.load_image(
image,
LoadImageSource::FromDevicePath {
device_path: shell_image_path,
from_boot_manager: false,
},
)
.expect("failed to load shell app");
let shell_image_handle = boot::load_image(
boot::image_handle(),
LoadImageSource::FromDevicePath {
device_path: shell_image_path,
from_boot_manager: false,
},
)
.expect("failed to load shell app");

// Set the command line passed to the shell app so that it will run the
// test-runner app. This automatically turns off the five-second delay.
let mut shell_loaded_image = boot_services
.open_protocol_exclusive::<LoadedImage>(shell_image_handle)
let mut shell_loaded_image = boot::open_protocol_exclusive::<LoadedImage>(shell_image_handle)
.expect("failed to open LoadedImage protocol");
let load_options = cstr16!(r"shell.efi test_runner.efi arg1 arg2");
unsafe {
Expand All @@ -77,9 +71,9 @@ fn efi_main(image: Handle, mut st: SystemTable<Boot>) -> Status {
}

info!("launching the shell app");
boot_services
.start_image(shell_image_handle)
.expect("failed to launch the shell app");
boot::start_image(shell_image_handle).expect("failed to launch the shell app");

Status::SUCCESS
Ok(())
}

uefi::set_main!(main);
25 changes: 11 additions & 14 deletions uefi-test-runner/src/boot/memory.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
use uefi::table::boot::{AllocateType, BootServices, MemoryType};
use uefi::boot;
use uefi::table::boot::{AllocateType, MemoryType};

use alloc::vec::Vec;

pub fn test(bt: &BootServices) {
pub fn test() {
info!("Testing memory functions");

allocate_pages(bt);
allocate_pages();
vec_alloc();
alloc_alignment();

memory_map(bt);
memory_map();
}

fn allocate_pages(bt: &BootServices) {
fn allocate_pages() {
info!("Allocating some pages of memory");

let ty = AllocateType::AnyPages;
let mem_ty = MemoryType::LOADER_DATA;
let pgs = bt
.allocate_pages(ty, mem_ty, 1)
.expect("Failed to allocate a page of memory");
let pgs = boot::allocate_pages(ty, mem_ty, 1).expect("Failed to allocate a page of memory");

assert_eq!(pgs % 4096, 0, "Page pointer is not page-aligned");

Expand All @@ -31,7 +30,7 @@ fn allocate_pages(bt: &BootServices) {
buf[4095] = 0x23;

// Clean up to avoid memory leaks.
unsafe { bt.free_pages(pgs, 1) }.unwrap();
unsafe { boot::free_pages(pgs, 1) }.unwrap();
}

// Simple test to ensure our custom allocator works with the `alloc` crate.
Expand Down Expand Up @@ -60,21 +59,19 @@ fn alloc_alignment() {
assert_eq!(value.as_ptr() as usize % 0x100, 0, "Wrong alignment");
}

fn memory_map(bt: &BootServices) {
fn memory_map() {
info!("Testing memory map functions");

// Get the memory descriptor size and an estimate of the memory map size
let sizes = bt.memory_map_size();
let sizes = boot::memory_map_size();

// 2 extra descriptors should be enough.
let buf_sz = sizes.map_size + 2 * sizes.entry_size;

// We will use vectors for convenience.
let mut buffer = vec![0_u8; buf_sz];

let mut memory_map = bt
.memory_map(&mut buffer)
.expect("Failed to retrieve UEFI memory map");
let mut memory_map = boot::memory_map(&mut buffer).expect("Failed to retrieve UEFI memory map");

memory_map.sort();

Expand Down
Loading

0 comments on commit 515a508

Please sign in to comment.