Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 108 additions & 9 deletions components/patina_adv_logger/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<S> ProtocolInterface for AdvancedLoggerProtocolInternal<S>
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<S>
where
Expand Down Expand Up @@ -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::<AdvancedLoggerProtocolInternal<S>>() };

let hw_write = internal.adv_logger.hardware_write_enabled(error_level, None);
Expand All @@ -83,7 +97,11 @@ where
///
/// Installs the Advanced Logger Protocol for use by non-local components.
///
fn entry_point(self, bs: StandardBootServices, timer: Service<dyn ArchTimerFunctionality>) -> Result<()> {
fn entry_point(
self,
protocols: Service<dyn ProtocolServices>,
timer: Service<dyn ArchTimerFunctionality>,
) -> 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);
Expand All @@ -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.");
Expand All @@ -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,
};
Expand All @@ -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<UartNull> = 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<UartNull> = 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<UartNull> = 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;
Expand All @@ -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<UartNull>) {
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 {
Expand Down Expand Up @@ -199,4 +254,48 @@ mod tests {
let status = AdvancedLoggerComponent::<UartNull>::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<dyn ProtocolServices> = Service::mock(Box::new(MockProtocolServices::new()));
let timer: Service<dyn ArchTimerFunctionality> = 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::<core::ffi::c_void>()).unwrap()));
let protocols: Service<dyn ProtocolServices> = Service::mock(Box::new(mock_protocols));
let timer: Service<dyn ArchTimerFunctionality> = 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<dyn ProtocolServices> = Service::mock(Box::new(mock_protocols));
let timer: Service<dyn ArchTimerFunctionality> = Service::mock(Box::new(MockTimer {}));

let result = component.entry_point(protocols, timer);
assert_eq!(result, Err(EfiError::ProtocolError));
}
}
13 changes: 6 additions & 7 deletions components/patina_adv_logger/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<dyn ProtocolServices>) -> 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::<AdvancedLoggerProtocol>(None) };
let result = protocols.locate_protocol::<AdvancedLoggerProtocol>();

u_assert!(result.is_ok(), "adv_logger_test: Failed to locate the advanced logger protocol.");
let protocol = result.unwrap();
Expand Down
Loading