From 2d4db6b7b89addfd0ef4cf4e5cceeb3d9554e1b0 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 25 Jun 2023 11:58:07 -0400 Subject: [PATCH] uefi: Use uefi_raw's SystemTable to implement SystemTable --- uefi/src/table/system.rs | 76 +++++++++------------------------------- 1 file changed, 16 insertions(+), 60 deletions(-) diff --git a/uefi/src/table/system.rs b/uefi/src/table/system.rs index 6ce549e7e..f3d859ce3 100644 --- a/uefi/src/table/system.rs +++ b/uefi/src/table/system.rs @@ -2,14 +2,14 @@ use core::ffi::c_void; use core::fmt::{Debug, Formatter}; use core::marker::PhantomData; use core::ptr::NonNull; -use core::{ptr, slice}; +use core::slice; use crate::proto::console::text; -use crate::{CStr16, Char16, Handle, Result, Status, StatusExt}; +use crate::{CStr16, Result, Status, StatusExt}; use super::boot::{BootServices, MemoryDescriptor, MemoryMap, MemoryType}; use super::runtime::{ResetType, RuntimeServices}; -use super::{cfg, Header, Revision}; +use super::{cfg, Revision}; /// Marker trait used to provide different views of the UEFI System Table. pub trait SystemTableView {} @@ -47,7 +47,7 @@ impl SystemTableView for Runtime {} /// will be provided to replace it. #[repr(transparent)] pub struct SystemTable { - table: *const SystemTableImpl, + table: *const uefi_raw::table::system::SystemTable, _marker: PhantomData, } @@ -56,13 +56,13 @@ impl SystemTable { /// Return the firmware vendor string #[must_use] pub fn firmware_vendor(&self) -> &CStr16 { - unsafe { CStr16::from_ptr((*self.table).fw_vendor) } + unsafe { CStr16::from_ptr((*self.table).firmware_vendor.cast()) } } /// Return the firmware revision #[must_use] pub const fn firmware_revision(&self) -> u32 { - unsafe { (*self.table).fw_revision } + unsafe { (*self.table).firmware_revision } } /// Returns the revision of this table, which is defined to be @@ -80,9 +80,10 @@ impl SystemTable { unsafe { let table = &*self.table; table - .cfg_table + .configuration_table + .cast::() .as_ref() - .map(|ptr| slice::from_raw_parts(ptr, table.nr_cfg)) + .map(|ptr| slice::from_raw_parts(ptr, table.number_of_configuration_table_entries)) .unwrap_or(&[]) } } @@ -119,7 +120,7 @@ impl SystemTable { impl SystemTable { /// Returns the standard input protocol. pub fn stdin(&mut self) -> &mut text::Input { - unsafe { &mut *(*self.table).stdin } + unsafe { &mut *(*self.table).stdin.cast() } } /// Returns the standard output protocol. @@ -135,13 +136,13 @@ impl SystemTable { /// Access runtime services #[must_use] pub const fn runtime_services(&self) -> &RuntimeServices { - unsafe { &*(*self.table).runtime } + unsafe { &*(*self.table).runtime_services.cast_const().cast() } } /// Access boot services #[must_use] pub const fn boot_services(&self) -> &BootServices { - unsafe { &*(*self.table).boot } + unsafe { &*(*self.table).boot_services.cast_const().cast() } } /// Get the size in bytes of the buffer to allocate for storing the memory @@ -299,7 +300,7 @@ impl SystemTable { /// "Calling Conventions" chapter of the UEFI specification for details. #[must_use] pub const unsafe fn runtime_services(&self) -> &RuntimeServices { - &*(*self.table).runtime + &*(*self.table).runtime_services.cast_const().cast() } /// Changes the runtime addressing mode of EFI firmware from physical to virtual. @@ -325,11 +326,11 @@ impl SystemTable { let entry_size = core::mem::size_of::(); let entry_version = MemoryDescriptor::VERSION; let map_ptr = map.as_mut_ptr(); - (*(*self.table).runtime) + self.runtime_services() .set_virtual_address_map(map_size, entry_size, entry_version, map_ptr) .to_result_with_val(|| { - let new_table_ref = - &mut *(new_system_table_virtual_addr as usize as *mut SystemTableImpl); + let new_table_ref = &mut *(new_system_table_virtual_addr as usize + as *mut uefi_raw::table::system::SystemTable); Self { table: new_table_ref, _marker: PhantomData, @@ -345,51 +346,6 @@ impl SystemTable { } } -/// The actual UEFI system table -#[repr(C)] -struct SystemTableImpl { - header: Header, - /// Null-terminated string representing the firmware's vendor. - fw_vendor: *const Char16, - fw_revision: u32, - stdin_handle: Handle, - stdin: *mut text::Input, - stdout_handle: Handle, - stdout: *mut text::Output, - stderr_handle: Handle, - stderr: *mut text::Output, - /// Runtime services table. - runtime: *const RuntimeServices, - /// Boot services table. - boot: *const BootServices, - /// Number of entries in the configuration table. - nr_cfg: usize, - /// Pointer to beginning of the array. - cfg_table: *const cfg::ConfigTableEntry, -} - impl super::Table for SystemTable { const SIGNATURE: u64 = 0x5453_5953_2049_4249; } - -impl Debug for SystemTableImpl { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - f.debug_struct("UefiSystemTable") - .field("header", &self.header) - .field("fw_vendor", &(unsafe { CStr16::from_ptr(self.fw_vendor) })) - .field("fw_revision", &self.fw_revision) - .field("stdin_handle", &self.stdin_handle) - .field("stdin", &self.stdin) - .field("stdout_handle", &self.stdout_handle) - .field("stdout", &self.stdout) - .field("stderr_handle", &self.stderr_handle) - .field("stderr", &self.stderr) - .field("runtime", &self.runtime) - // a little bit of extra work needed to call debug-fmt on the BootServices - // instead of printing the raw pointer - .field("boot", &(unsafe { ptr::read(self.boot) })) - .field("nf_cfg", &self.nr_cfg) - .field("cfg_table", &self.cfg_table) - .finish() - } -}