forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Minimal Std implementation for UEFI
Implemented modules: 1. alloc 2. os_str 3. env 4. math Tracking Issue: rust-lang#100499 API Change Proposal: rust-lang/libs-team#87 This was originally part of rust-lang#100316. Since that PR was becoming too unwieldy and cluttered, and with suggestion from @dvdhrm, I have extracted a minimal std implementation to this PR. Signed-off-by: Ayush Singh <[email protected]>
- Loading branch information
Showing
24 changed files
with
718 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"rust-analyzer.cargo.target": "x86_64-unknown-uefi" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! UEFI-specific extensions to the primitives in `std::env` module | ||
|
||
use crate::ffi::c_void; | ||
use crate::ptr::NonNull; | ||
use crate::sync::atomic::{AtomicPtr, Ordering}; | ||
use crate::sync::OnceLock; | ||
|
||
// Position 0 = SystemTable | ||
// Position 1 = ImageHandle | ||
static GLOBALS: OnceLock<(AtomicPtr<c_void>, AtomicPtr<c_void>)> = OnceLock::new(); | ||
|
||
/// Initializes the global System Table and Image Handle pointers. | ||
/// | ||
/// The standard library requires access to the UEFI System Table and the Application Image Handle | ||
/// to operate. Those are provided to UEFI Applications via their application entry point. By | ||
/// calling `init_globals()`, those pointers are retained by the standard library for future use. | ||
/// The pointers are never exposed to any entity outside of this application and it is guaranteed | ||
/// that, once the application exited, these pointers are never dereferenced again. | ||
/// | ||
/// Callers are required to ensure the pointers are valid for the entire lifetime of this | ||
/// application. In particular, UEFI Boot Services must not be exited while an application with the | ||
/// standard library is loaded. | ||
/// | ||
/// This function must not be called more than once. | ||
#[unstable(feature = "uefi_std", issue = "100499")] | ||
pub unsafe fn init_globals(handle: NonNull<c_void>, system_table: NonNull<c_void>) { | ||
GLOBALS.set((AtomicPtr::new(system_table.as_ptr()), AtomicPtr::new(handle.as_ptr()))).unwrap() | ||
} | ||
|
||
/// Get the SystemTable Pointer. | ||
/// Note: This function panics if the System Table and Image Handle is Not initialized | ||
#[unstable(feature = "uefi_std", issue = "100499")] | ||
pub fn system_table() -> NonNull<c_void> { | ||
try_system_table().unwrap() | ||
} | ||
|
||
/// Get the SystemHandle Pointer. | ||
/// Note: This function panics if the System Table and Image Handle is Not initialized | ||
#[unstable(feature = "uefi_std", issue = "100499")] | ||
pub fn image_handle() -> NonNull<c_void> { | ||
try_image_handle().unwrap() | ||
} | ||
|
||
/// Get the SystemTable Pointer. | ||
/// This function is mostly intended for places where panic is not an option | ||
pub(crate) fn try_system_table() -> Option<NonNull<crate::ffi::c_void>> { | ||
NonNull::new(GLOBALS.get()?.0.load(Ordering::Acquire)) | ||
} | ||
|
||
/// Get the SystemHandle Pointer. | ||
/// This function is mostly intended for places where panic is not an option | ||
pub(crate) fn try_image_handle() -> Option<NonNull<crate::ffi::c_void>> { | ||
NonNull::new(GLOBALS.get()?.1.load(Ordering::Acquire)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//! Platform-specific extensions to `std` for UEFI. | ||
|
||
#![unstable(feature = "uefi_std", issue = "100499")] | ||
|
||
pub mod env; | ||
#[path = "../windows/ffi.rs"] | ||
pub mod ffi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//! Global Allocator for UEFI. | ||
//! Uses [r-efi-alloc](https://crates.io/crates/r-efi-alloc) | ||
|
||
use crate::alloc::{handle_alloc_error, GlobalAlloc, Layout, System}; | ||
|
||
const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA; | ||
|
||
#[stable(feature = "alloc_system_type", since = "1.28.0")] | ||
unsafe impl GlobalAlloc for System { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
let system_table = match crate::os::uefi::env::try_system_table() { | ||
None => return crate::ptr::null_mut(), | ||
Some(x) => x.as_ptr() as *mut _, | ||
}; | ||
|
||
if layout.size() > 0 { | ||
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) } | ||
} else { | ||
layout.dangling().as_ptr() | ||
} | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
let system_table = match crate::os::uefi::env::try_system_table() { | ||
None => handle_alloc_error(layout), | ||
Some(x) => x.as_ptr() as *mut _, | ||
}; | ||
if layout.size() > 0 { | ||
unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) } | ||
} | ||
} | ||
} |
Oops, something went wrong.