From 8bdbf2605206cec7b21ad9857a7b5652345dc7e1 Mon Sep 17 00:00:00 2001 From: Michael Kubacki Date: Tue, 4 Aug 2026 11:00:09 -0400 Subject: [PATCH] components/patina_adv_logger: Update to use UEFI Services Replaces boot services calls to `StandardBootServices` with calls to the Patina UEFI Services. Signed-off-by: Michael Kubacki --- components/patina_adv_logger/src/component.rs | 117 ++++++++++++++++-- .../patina_adv_logger/src/integration_test.rs | 13 +- 2 files changed, 114 insertions(+), 16 deletions(-) diff --git a/components/patina_adv_logger/src/component.rs b/components/patina_adv_logger/src/component.rs index 7ac0949184..acb86a864e 100644 --- a/components/patina_adv_logger/src/component.rs +++ b/components/patina_adv_logger/src/component.rs @@ -14,11 +14,15 @@ use patina::standard::efi; use patina::{ component::{ component, - service::{Service, perf_timer::ArchTimerFunctionality}, + service::{ + Service, + perf_timer::ArchTimerFunctionality, + uefi_services::protocol::{ProtocolServices, ProtocolServicesExt}, + }, }, error::{EfiError, Result}, peripheral::serial::SerialIO, - uefi::boot_services::{BootServices, StandardBootServices}, + protocol::ProtocolInterface, }; use crate::{logger::AdvancedLogger, protocol::AdvancedLoggerProtocol}; @@ -36,6 +40,15 @@ where adv_logger: &'static AdvancedLogger<'static, S>, } +// SAFETY: `protocol` is the first field of `AdvancedLoggerProtocolInternal` (repr(C)), so a +// pointer to `AdvancedLoggerProtocolInternal` is also a valid pointer to `AdvancedLoggerProtocol`. +unsafe impl ProtocolInterface for AdvancedLoggerProtocolInternal +where + S: SerialIO + Send + 'static, +{ + const PROTOCOL_GUID: patina::BinaryGuid = AdvancedLoggerProtocol::PROTOCOL_GUID; +} + /// The component that will install the Advanced Logger protocol. pub struct AdvancedLoggerComponent where @@ -70,8 +83,9 @@ where let data = unsafe { core::slice::from_raw_parts(buffer, num_bytes) }; let error_level = error_level as u32; - // SAFETY: `this` is null-checked above. The protocol struct is installed by Patina with - // `Box::leak`, so its alignment and validity are guaranteed. + // SAFETY: `this` is null-checked above. The protocol struct is installed through + // `install_protocol`, which leaks it once installation succeeds, so its + // alignment and validity are guaranteed. let internal = unsafe { &*this.cast::>() }; let hw_write = internal.adv_logger.hardware_write_enabled(error_level, None); @@ -83,7 +97,11 @@ where /// /// Installs the Advanced Logger Protocol for use by non-local components. /// - fn entry_point(self, bs: StandardBootServices, timer: Service) -> Result<()> { + fn entry_point( + self, + protocols: Service, + timer: Service, + ) -> Result<()> { let Some(address) = self.adv_logger.get_log_address() else { log::error!("Advanced logger not initialized before component entry point!"); return Err(EfiError::NotStarted); @@ -96,9 +114,8 @@ where adv_logger: self.adv_logger, }; - let protocol = Box::leak(Box::new(protocol)); - if let Err(status) = bs.install_protocol_interface(None, &mut protocol.protocol) { - log::error!("Failed to install Advanced Logger protocol! Status = {status}"); + if let Err(status) = protocols.install_protocol(None, Box::new(protocol)) { + log::error!("Failed to install Advanced Logger protocol! Status = {status:?}"); Err(EfiError::ProtocolError) } else { log::info!("Advanced Logger protocol installed."); @@ -116,7 +133,11 @@ mod tests { writer::AdvancedLogWriter, }; use patina::{ - component::service::{IntoService, Service, perf_timer::ArchTimerFunctionality}, + component::service::{ + IntoService, Service, + perf_timer::ArchTimerFunctionality, + uefi_services::protocol::{Handle, MockProtocolServices, ProtocolError}, + }, debug::log::Format, peripheral::serial::uart::UartNull, }; @@ -142,6 +163,30 @@ mod tests { UartNull {}, ); + // Not touched by `set_log_info_address`, so `get_log_address` always returns `None`. + static UNINITIALIZED_LOGGER: AdvancedLogger = AdvancedLogger::new( + Format::Standard, + &[TargetFilter { target: "", log_level: log::LevelFilter::Trace, hw_filter_override: None }], + log::LevelFilter::Trace, + UartNull {}, + ); + + // Separate from TEST_LOGGER since entry_point's init_timer call can only publish once per logger. + static ENTRY_POINT_SUCCESS_LOGGER: AdvancedLogger = AdvancedLogger::new( + Format::Standard, + &[TargetFilter { target: "", log_level: log::LevelFilter::Trace, hw_filter_override: None }], + log::LevelFilter::Trace, + UartNull {}, + ); + + // Similar to `ENTRY_POINT_SUCCESS_LOGGER`, a failure logger is needed. + static ENTRY_POINT_FAILURE_LOGGER: AdvancedLogger = AdvancedLogger::new( + Format::Standard, + &[TargetFilter { target: "", log_level: log::LevelFilter::Trace, hw_filter_override: None }], + log::LevelFilter::Trace, + UartNull {}, + ); + /// Initializes `TEST_LOGGER` with a newly allocated memory log and a mock timer. fn init_test_logger() { const LOG_LEN: usize = 0x2000; @@ -153,6 +198,16 @@ mod tests { TEST_LOGGER.init_timer(Service::mock(Box::new(MockTimer {}))); } + /// Sets `logger`'s log address, leaving its timer unpublished for `entry_point` to publish. + fn init_log_address(logger: &AdvancedLogger) { + const LOG_LEN: usize = 0x2000; + let log_buff = Box::into_raw(Box::new([0_u8; LOG_LEN])); + let log_address = log_buff as *const u8 as efi::PhysicalAddress; + // SAFETY: This memory was just allocated, so it is valid for LOG_LEN bytes. + unsafe { AdvancedLogWriter::initialize_memory_log(log_address, LOG_LEN as u32) }; + logger.set_log_info_address(log_address); + } + /// Builds a leaked `AdvancedLoggerProtocolInternal` structure and returns a `*const AdvancedLoggerProtocol` /// that can be passed as `this` to `adv_log_write`. fn leak_protocol_this() -> *const AdvancedLoggerProtocol { @@ -199,4 +254,48 @@ mod tests { let status = AdvancedLoggerComponent::::adv_log_write(this, 0, data.as_ptr(), data.len()); assert_eq!(status, efi::Status::SUCCESS); } + + #[test] + fn entry_point_returns_not_started_when_log_address_uninitialized() { + let component = AdvancedLoggerComponent::new(&UNINITIALIZED_LOGGER); + let protocols: Service = Service::mock(Box::new(MockProtocolServices::new())); + let timer: Service = Service::mock(Box::new(MockTimer {})); + + let result = component.entry_point(protocols, timer); + assert_eq!(result, Err(EfiError::NotStarted)); + } + + // Setting the log address writes the shared `DBG_ADV_LOG_BUFFER` static. + #[test] + #[serial(adv_logger_test)] + fn entry_point_installs_protocol_and_succeeds() { + init_log_address(&ENTRY_POINT_SUCCESS_LOGGER); + let component = AdvancedLoggerComponent::new(&ENTRY_POINT_SUCCESS_LOGGER); + + let mut mock_protocols = MockProtocolServices::new(); + mock_protocols + .expect_install_interface() + .once() + .returning(|_, _, _| Ok(Handle::from_raw(core::ptr::dangling_mut::()).unwrap())); + let protocols: Service = Service::mock(Box::new(mock_protocols)); + let timer: Service = Service::mock(Box::new(MockTimer {})); + + let result = component.entry_point(protocols, timer); + assert_eq!(result, Ok(())); + } + + #[test] + #[serial(adv_logger_test)] + fn entry_point_returns_protocol_error_when_install_fails() { + init_log_address(&ENTRY_POINT_FAILURE_LOGGER); + let component = AdvancedLoggerComponent::new(&ENTRY_POINT_FAILURE_LOGGER); + + let mut mock_protocols = MockProtocolServices::new(); + mock_protocols.expect_install_interface().once().returning(|_, _, _| Err(ProtocolError::InvalidParameter)); + let protocols: Service = Service::mock(Box::new(mock_protocols)); + let timer: Service = Service::mock(Box::new(MockTimer {})); + + let result = component.entry_point(protocols, timer); + assert_eq!(result, Err(EfiError::ProtocolError)); + } } diff --git a/components/patina_adv_logger/src/integration_test.rs b/components/patina_adv_logger/src/integration_test.rs index 05fe49ccb5..4a58a6913a 100644 --- a/components/patina_adv_logger/src/integration_test.rs +++ b/components/patina_adv_logger/src/integration_test.rs @@ -10,7 +10,10 @@ //! //! SPDX-License-Identifier: Apache-2.0 //! -use patina::uefi::boot_services::{BootServices, StandardBootServices}; +use patina::component::service::{ + Service, + uefi_services::protocol::{ProtocolServices, ProtocolServicesExt}, +}; use patina_test::{patina_test, u_assert, u_assert_eq}; use patina::{debug::log::DEBUG_INFO, standard::efi}; @@ -19,17 +22,13 @@ use crate::{protocol::AdvancedLoggerProtocol, reader::AdvancedLogReader}; #[cfg_attr(coverage, coverage(off))] #[patina_test] -fn adv_logger_test(bs: StandardBootServices) -> patina_test::error::Result { +fn adv_logger_test(protocols: Service) -> patina_test::error::Result { const DIRECT_STR: &str = "adv_logger_test: Direct log message!!!"; const PROTOCOL_STR: &str = "adv_logger_test: Logged through the protocol!!!\n"; // Get a reference to the advanced logger buffer. The actual transport does // not matter so use the NULL implementation as a stand-in. - // - // SAFETY: The advanced logger protocol should have been installed by the - // component initialization and so should be the AdvancedLoggerProtocol protocol type definition. This search - // and conversion is safe. - let result = unsafe { bs.locate_protocol::(None) }; + let result = protocols.locate_protocol::(); u_assert!(result.is_ok(), "adv_logger_test: Failed to locate the advanced logger protocol."); let protocol = result.unwrap();