From 6d93e1f4eba0449a9896a323bb49754bd7ca5376 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 20 Jan 2025 22:20:41 -0500 Subject: [PATCH 1/3] relicensing: Rewrite PR "allocator: Use appropriate memory types" The allocator code has already changed a fair amount since that PR landed, so only docstrings needed to be rewritten. This covers these commits: 9dbdfe9486b648d3f8f28e2381a5fbebeae2b5e9 e6803d70b716dd3c069d011b4f0848b74451b953 --- uefi/src/allocator.rs | 15 ++++++++------- uefi/src/proto/loaded_image.rs | 14 ++------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/uefi/src/allocator.rs b/uefi/src/allocator.rs index 3969a3a31..e29d9fdae 100644 --- a/uefi/src/allocator.rs +++ b/uefi/src/allocator.rs @@ -7,13 +7,12 @@ //! services are not active, `alloc` will return a null pointer, and `dealloc` //! will panic. -use core::alloc::{GlobalAlloc, Layout}; -use core::ptr::{self, NonNull}; -use core::sync::atomic::{AtomicU32, Ordering}; - use crate::boot; use crate::mem::memory_map::MemoryType; use crate::proto::loaded_image::LoadedImage; +use core::alloc::{GlobalAlloc, Layout}; +use core::ptr::{self, NonNull}; +use core::sync::atomic::{AtomicU32, Ordering}; /// Get the memory type to use for allocation. /// @@ -48,9 +47,11 @@ fn get_memory_type() -> MemoryType { pub struct Allocator; unsafe impl GlobalAlloc for Allocator { - /// Allocate memory using [`boot::allocate_pool`]. The allocation is - /// of type [`MemoryType::LOADER_DATA`] for UEFI applications, [`MemoryType::BOOT_SERVICES_DATA`] - /// for UEFI boot drivers and [`MemoryType::RUNTIME_SERVICES_DATA`] for UEFI runtime drivers. + /// Allocate memory using [`boot::allocate_pool`]. The allocation's [memory + /// type] matches the current image's [data type]. + /// + /// [memory type]: MemoryType + /// [data type]: LoadedImage::data_type unsafe fn alloc(&self, layout: Layout) -> *mut u8 { if !boot::are_boot_services_active() { return ptr::null_mut(); diff --git a/uefi/src/proto/loaded_image.rs b/uefi/src/proto/loaded_image.rs index cc2ef5cb4..baff6846b 100644 --- a/uefi/src/proto/loaded_image.rs +++ b/uefi/src/proto/loaded_image.rs @@ -180,23 +180,13 @@ impl LoadedImage { (self.0.image_base, self.0.image_size) } - /// Get the memory type of the image's code sections. - /// - /// Normally the returned value is one of: - /// - `MemoryType::LOADER_CODE` for UEFI applications - /// - `MemoryType::BOOT_SERVICES_CODE` for UEFI boot drivers - /// - `MemoryType::RUNTIME_SERVICES_CODE` for UEFI runtime drivers + /// Returns the memory type that the image's code sections were loaded as. #[must_use] pub const fn code_type(&self) -> MemoryType { self.0.image_code_type } - /// Get the memory type of the image's data sections. - /// - /// Normally the returned value is one of: - /// - `MemoryType::LOADER_DATA` for UEFI applications - /// - `MemoryType::BOOT_SERVICES_DATA` for UEFI boot drivers - /// - `MemoryType::RUNTIME_SERVICES_DATA` for UEFI runtime drivers + /// Returns the memory type that the image's data sections were loaded as. #[must_use] pub const fn data_type(&self) -> MemoryType { self.0.image_data_type From 83e99f2abbe053fc1dcf4c3b4e9bf734835a8abb Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Tue, 21 Jan 2025 01:32:38 -0500 Subject: [PATCH 2/3] relicensing: Rewrite PR "uefi: Fix wrong install_configuration_table() signature" The signature fix itself is trivial, just adding a `&`. The test changes had already been partially rewritten during the freestanding table function refactor. Now alter constants and variable names, remove a use of `as`, and free the memory at the end. The PR originally included a cleanup commit ("uefi-test-runner: Fix buffer alignment on ia32") that ended up being cherry-picked and merged in a separate PR: https://github.com/rust-osdev/uefi-rs/pull/844. Rewrite that fix here as well. This covers these commits: d7697c3ab5b385e2c56fba1ec231ed2af6ba9382 9609873a1c849ed1c1ac46585c63efffb9edae28 --- uefi-test-runner/src/boot/misc.rs | 26 ++++++++++++++------------ uefi-test-runner/src/proto/media.rs | 7 ++++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/uefi-test-runner/src/boot/misc.rs b/uefi-test-runner/src/boot/misc.rs index 196ea9dbe..7514fa5ea 100644 --- a/uefi-test-runner/src/boot/misc.rs +++ b/uefi-test-runner/src/boot/misc.rs @@ -184,29 +184,31 @@ fn test_uninstall_protocol_interface() { fn test_install_configuration_table() { // Get the current number of entries. - let count = system::with_config_table(|t| t.len()); + let initial_table_count = system::with_config_table(|t| t.len()); // Create the entry data. - let config = boot::allocate_pool(MemoryType::RUNTIME_SERVICES_DATA, 1) - .unwrap() - .as_ptr(); - unsafe { config.write(42) }; + let config: NonNull = boot::allocate_pool(MemoryType::RUNTIME_SERVICES_DATA, 1).unwrap(); + unsafe { config.write(123u8) }; // Install the table. - const ID: Guid = guid!("4bec53c4-5fc1-48a1-ab12-df214907d29f"); + const TABLE_GUID: Guid = guid!("4bec53c4-5fc1-48a1-ab12-df214907d29f"); unsafe { - boot::install_configuration_table(&ID, config.cast()).unwrap(); + boot::install_configuration_table(&TABLE_GUID, config.as_ptr().cast()).unwrap(); } // Verify the installation. - assert_eq!(count + 1, system::with_config_table(|t| t.len())); + assert_eq!( + initial_table_count + 1, + system::with_config_table(|t| t.len()) + ); system::with_config_table(|t| { - let config_entry = t.iter().find(|ct| ct.guid == ID).unwrap(); - assert_eq!(unsafe { *(config_entry.address as *const u8) }, 42); + let config_entry = t.iter().find(|ct| ct.guid == TABLE_GUID).unwrap(); + assert_eq!(unsafe { *config_entry.address.cast::() }, 123); }); - // Uninstall the table. + // Uninstall the table and free the memory. unsafe { - boot::install_configuration_table(&ID, ptr::null()).unwrap(); + boot::install_configuration_table(&TABLE_GUID, ptr::null()).unwrap(); + boot::free_pool(config).unwrap(); } } diff --git a/uefi-test-runner/src/proto/media.rs b/uefi-test-runner/src/proto/media.rs index 839ad372b..85863a787 100644 --- a/uefi-test-runner/src/proto/media.rs +++ b/uefi-test-runner/src/proto/media.rs @@ -15,6 +15,9 @@ use uefi::proto::media::fs::SimpleFileSystem; use uefi::proto::media::partition::{MbrOsType, PartitionInfo}; use uefi::runtime::{Daylight, Time, TimeParams}; +#[repr(align(8))] +struct AlignedBuf([u8; 256]); + /// Test directory entry iteration. fn test_existing_dir(directory: &mut Directory) { info!("Testing existing directory"); @@ -31,11 +34,9 @@ fn test_existing_dir(directory: &mut Directory) { let dir = RefCell::new(dir); assert_eq!(FileInfo::alignment(), 8); - #[repr(align(8))] - struct Buf([u8; 200]); // Backing memory to read the file info data into. - let mut stack_buf = Buf([0; 200]); + let mut stack_buf = AlignedBuf([0; 256]); // The file names that the test read from the directory. let entry_names = RefCell::new(vec![]); From 90def73e1b91229f2061c60ae4eed9fc9e4d7a6b Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Tue, 21 Jan 2025 11:12:51 -0500 Subject: [PATCH 3/3] relicensing: Rewrite PR "loaded_image: Add set_unload" This covers these commits: d4e88a1de5201d08df59977dbb1c6973501caf0c --- uefi/src/proto/loaded_image.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/uefi/src/proto/loaded_image.rs b/uefi/src/proto/loaded_image.rs index baff6846b..ace4c7dba 100644 --- a/uefi/src/proto/loaded_image.rs +++ b/uefi/src/proto/loaded_image.rs @@ -134,25 +134,21 @@ impl LoadedImage { self.0.image_size = size; } - /// Set the callback handler to unload the image. - /// - /// Drivers that wish to support unloading have to register their unload handler - /// using this protocol. It is responsible for cleaning up any resources the - /// image is using before returning. Unloading a driver is done with - /// [`boot::unload_image`]. + /// Registers a cleanup function that is called when [`boot::unload_image`] + /// is called. /// /// # Safety /// - /// Only the driver that this [`LoadedImage`] is attached to should register an - /// unload handler. + /// The registered function must reside in memory that is not freed until + /// after the image is unloaded. /// /// [`boot::unload_image`]: crate::boot::unload_image pub unsafe fn set_unload( &mut self, unload: extern "efiapi" fn(image_handle: Handle) -> Status, ) { - type RawFn = unsafe extern "efiapi" fn(image_handle: uefi_raw::Handle) -> uefi_raw::Status; - let unload: RawFn = mem::transmute(unload); + let unload: unsafe extern "efiapi" fn(image_handle: uefi_raw::Handle) -> uefi_raw::Status = + mem::transmute(unload); self.0.unload = Some(unload); }