Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rust std support for x86_64-unknown-uefi #100316

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f872477
Add UEFI module in std/sys
Ayush1325 Jun 15, 2022
2be029d
Improve Handling of Pointers
Ayush1325 Jul 2, 2022
ff55531
Add OsStringExt and OsStrExt traits
Ayush1325 Jul 3, 2022
b988418
Graceful abort
Ayush1325 Jul 4, 2022
2800931
Re-export r-efi in std::os::uefi::raw
Ayush1325 Jul 5, 2022
868e3e1
Implement args
Ayush1325 Jul 7, 2022
6769cb5
Implement SystemTime
Ayush1325 Jul 8, 2022
a14d62e
Implment fs
Ayush1325 Jul 10, 2022
289c445
Implement TCP
Ayush1325 Jul 16, 2022
645647d
Add UEFI support for running tests
Ayush1325 Jul 18, 2022
3e891b3
Implement Environment Variables
Ayush1325 Jul 20, 2022
5689d37
Implement append for files
Ayush1325 Jul 23, 2022
372f6e1
Allow Running remote-test-server for UEFI
Ayush1325 Jul 23, 2022
65849ae
Remove r_efi reexports from std
Ayush1325 Jul 25, 2022
ebc5ca2
Various fixes
Ayush1325 Jul 25, 2022
d5e78bf
Add vectored read/write for TcpStream
Ayush1325 Jul 26, 2022
368ab5a
Overhaul File I/O
Ayush1325 Jul 27, 2022
ce43ad6
Fix/Ignore tests
Ayush1325 Jul 27, 2022
72641b8
Implement more of fs
Ayush1325 Jul 29, 2022
95e8f68
Improve TCP
Ayush1325 Jul 30, 2022
aa980a6
Rebase to latest master
Ayush1325 Aug 2, 2022
3d9a83b
Add UEFI Std docs
Ayush1325 Aug 5, 2022
767650c
Refactor UCS-2 stuff
Ayush1325 Aug 6, 2022
4f32221
Improve process
Ayush1325 Aug 7, 2022
0bb8203
Add x86_64-uefi ci
Ayush1325 Aug 9, 2022
e85df7a
Add nothread and staticlink for remote-test-server
Ayush1325 Aug 9, 2022
1f52c23
Fix some tidy errors
Ayush1325 Aug 9, 2022
d84c8e3
Fixes from PR
Ayush1325 Aug 9, 2022
3bf5104
Update compiler_builtins
Ayush1325 Aug 10, 2022
176361a
Improve Envirnoment Variables
Ayush1325 Aug 11, 2022
88c615c
Implement hashmap_random_keys()
Ayush1325 Aug 11, 2022
8cd315c
Improve UEFI sys documentation
Ayush1325 Aug 13, 2022
4bc53e3
Fixes from PR
Ayush1325 Aug 13, 2022
c257c00
Initial implementation of Instant using TSC
Ayush1325 Aug 18, 2022
7262c22
Overhaul Dealing with UEFI ZSTs
Ayush1325 Aug 19, 2022
36c628f
Refactor `std::os::uefi`
Ayush1325 Aug 19, 2022
73852ec
Inline more functions
Ayush1325 Aug 19, 2022
d610cde
Implement `std::fs::File::set_times`
Ayush1325 Aug 20, 2022
3702334
Improve Environment variables
Ayush1325 Aug 22, 2022
c4320c1
Fix CI for x86_64-uefi
Ayush1325 Aug 23, 2022
944bf59
Update r-efi
Ayush1325 Aug 23, 2022
c83cbd2
Improve sys::process
Ayush1325 Aug 26, 2022
a95933b
Fix Env vars
Ayush1325 Aug 27, 2022
3df71e6
Apply fixes suggested in PR
Ayush1325 Aug 28, 2022
545899b
Improve Pipe Protocol
Ayush1325 Sep 13, 2022
945049c
Revert formatting changes to tests
Ayush1325 Sep 14, 2022
1baa38f
Rebase on master
Ayush1325 Sep 19, 2022
a9c1bb8
Implement io for UEFI
Ayush1325 Sep 19, 2022
939e11e
Improve std::uefi::env APIs
Ayush1325 Sep 19, 2022
67329ff
Fixes from PR
Ayush1325 Sep 20, 2022
2345867
Improve handling of Globals
Ayush1325 Oct 2, 2022
d343822
Improve UEFI fs
Ayush1325 Oct 2, 2022
dae6b19
Remove ignore-uefi from tests
Ayush1325 Oct 2, 2022
195633a
Improve TCP
Ayush1325 Oct 5, 2022
4f7ff72
Fixes from PR
Ayush1325 Oct 5, 2022
77283ae
Use compiler generated entry point for UEFI
Ayush1325 Oct 6, 2022
f89fceb
Fixes after rebase
Ayush1325 Oct 14, 2022
000228b
Implement UEFI argument parsing
Ayush1325 Dec 1, 2022
d48d07d
Use r-efi-alloc for allocator
Ayush1325 Dec 6, 2022
824f067
Fixes suggested in PR
Ayush1325 Dec 19, 2022
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
Prev Previous commit
Next Next commit
Apply fixes suggested in PR
1. Limit scope to crate
2. Make std::io::error private again
3. Fix atomic ordering
4. Remove unnecessary inlines
5. Remove r-efi re-exports: Returning void pointer in place of SystemTable
   pointer now. It can be cast to SystemTable pointer of user's choice.
6. Remove std::path::Prefix::UefiDevice
7. Fix CI
8. Mimic Windows OsStrExt and OsStringExt
9. move parse_lp_cmd_line to sys_common: This allows sharing parse_lp_cmd_line
   between Windows and UEFI.
10. No longer storing path in File struct.
11. Removed Windows panic_abort instructions since they did not work for UEFI
12. Overhaul VariableBox
  a. No longer implementing Deref, DerefMut, AsRef and AsRefMut for
     VariableBox. More about why this should be avoided can be found here:
     rust-lang/unsafe-code-guidelines#256
  b. Only accessing contents of VariableBox using pointers

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
  • Loading branch information
Ayush1325 committed Dec 1, 2022
commit 3df71e60bc750e572cb01502646813babfb8f901
2 changes: 1 addition & 1 deletion library/std/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pub(crate) use error::const_io_error;
mod buffered;
pub(crate) mod copy;
mod cursor;
pub(crate) mod error;
mod error;
mod impls;
pub mod prelude;
mod readbuf;
Expand Down
51 changes: 22 additions & 29 deletions library/std/src/os/uefi/env.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
//! UEFI-specific extensions to the primitives in `std::env` module

use super::raw::{BootServices, RuntimeServices, SystemTable};
use crate::ffi::c_void;
use crate::ptr::NonNull;
use crate::sync::atomic::{AtomicPtr, Ordering};

static GLOBAL_SYSTEM_TABLE: AtomicPtr<SystemTable> = AtomicPtr::new(crate::ptr::null_mut());
static GLOBAL_SYSTEM_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());
static GLOBAL_SYSTEM_TABLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());
static GLOBAL_IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());

/// Initializes Global Atomic Pointers to SystemTable and Handle.
/// Should only be called once in the program execution under normal circumstances.
/// The caller should ensure that the pointers are valid.
/// 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 fn init_globals(handle: NonNull<c_void>, system_table: NonNull<SystemTable>) {
GLOBAL_SYSTEM_TABLE.store(system_table.as_ptr(), Ordering::SeqCst);
GLOBAL_SYSTEM_HANDLE.store(handle.as_ptr(), Ordering::SeqCst);
pub unsafe fn init_globals(handle: NonNull<c_void>, system_table: NonNull<c_void>) {
GLOBAL_SYSTEM_TABLE.store(system_table.as_ptr(), Ordering::Release);
GLOBAL_IMAGE_HANDLE.store(handle.as_ptr(), Ordering::Release);
}

/// Get the SystemTable Pointer.
#[unstable(feature = "uefi_std", issue = "100499")]
pub fn get_system_table() -> Option<NonNull<SystemTable>> {
NonNull::new(GLOBAL_SYSTEM_TABLE.load(Ordering::SeqCst))
pub fn system_table() -> Option<NonNull<c_void>> {
NonNull::new(GLOBAL_SYSTEM_TABLE.load(Ordering::Acquire))
}

/// Get the SystemHandle Pointer.
#[unstable(feature = "uefi_std", issue = "100499")]
pub fn get_system_handle() -> Option<NonNull<c_void>> {
NonNull::new(GLOBAL_SYSTEM_HANDLE.load(Ordering::SeqCst))
}

/// Get the BootServices Pointer.
#[unstable(feature = "uefi_std", issue = "100499")]
pub fn get_boot_services() -> Option<NonNull<BootServices>> {
let system_table = get_system_table()?;
let boot_services = unsafe { (*system_table.as_ptr()).boot_services };
NonNull::new(boot_services)
}

/// Get the RuntimeServices Pointer.
#[unstable(feature = "uefi_std", issue = "100499")]
pub fn get_runtime_services() -> Option<NonNull<RuntimeServices>> {
let system_table = get_system_table()?;
let runtime_services = unsafe { (*system_table.as_ptr()).runtime_services };
NonNull::new(runtime_services)
pub fn image_handle() -> Option<NonNull<c_void>> {
NonNull::new(GLOBAL_IMAGE_HANDLE.load(Ordering::Acquire))
}
75 changes: 37 additions & 38 deletions library/std/src/os/uefi/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ use crate::sealed::Sealed;
use crate::sys_common::ucs2;

#[unstable(feature = "uefi_std", issue = "100499")]
pub trait OsStrExt: Sealed {
/// This function does not do any allocation
fn to_ucs2<'a>(&'a self) -> ucs2::EncodeUcs2<'a>;
pub use ucs2::EncodeUcs2;

/// Creates a UCS-2 Vec which can be passed for FFI.
/// Note: This function will replace `NULL` and other characters which are not valid in UEFI
/// strings with `std::sys_common::ucs::Ucs2Char::REPLACEMENT_CHARACTER`
fn to_ffi_string(&self) -> Vec<u16> {
let mut v: Vec<u16> = self
.to_ucs2()
.map(|x| match x {
Ok(c) => c,
Err(_) => ucs2::Ucs2Char::REPLACEMENT_CHARACTER,
})
.map(u16::from)
.collect();
v.push(0);
v.shrink_to_fit();
v
}
#[unstable(feature = "uefi_std", issue = "100499")]
pub trait OsStrExt: Sealed {
/// Re-encodes an `OsStr` as a wide character sequence, i.e., UCS-2
///
/// # Examples
///
/// ```
/// use std::ffi::OsString;
/// use std::os::uefi::ffi::*;
///
/// // UTF-2 encoding for "Unicode".
/// let source = [0x0055, 0x006E, 0x0069, 0x0063, 0x006F, 0x0064, 0x0065];
///
/// let string = OsString::from_wide(&source[..]);
///
/// let result: Vec<u16> = string.encode_wide().collect();
/// assert_eq!(&source[..], &result[..]);
/// ```
fn encode_wide<'a>(&'a self) -> EncodeUcs2<'a>;
}

impl OsStrExt for OsStr {
fn to_ucs2<'a>(&'a self) -> ucs2::EncodeUcs2<'a> {
// This conversion should never fail since the underlying OsStr is UTF-8 encoded
fn encode_wide<'a>(&'a self) -> EncodeUcs2<'a> {
// SAFETY: Calling unwrap on `self.to_str` is safe since the underlying OsStr is UTF-8
// encoded
ucs2::EncodeUcs2::from_str(self.to_str().unwrap())
}
}
Expand All @@ -37,26 +39,23 @@ pub trait OsStringExt: Sealed
where
Self: Sized,
{
fn from_ucs2_lossy(ucs: &[u16]) -> Self;

// For Null terminated UCS-2 Strings.
#[inline]
fn from_ucs2_null_termintated(ucs: &[u16]) -> Self {
Self::from_ucs2_lossy(&ucs[..(ucs.len() - 1)])
}

// Create OsString from an FFI obtained pointer.
// Len is the number of elemented in the string, not number of bytes.
// Note: This string is assumed to be null terminated
#[inline]
unsafe fn from_ffi(ucs: *mut u16, len: usize) -> Self {
let s = crate::slice::from_raw_parts(ucs, len);
Self::from_ucs2_null_termintated(s)
}
/// Creates an `OsString` from a UCS-2 slice of 16-bit code units.
/// # Examples
///
/// ```
/// use std::ffi::OsString;
/// use std::os::uefi::ffi::*;
///
/// // UTF-16 encoding for "Unicode".
/// let source = [0x0055, 0x006E, 0x0069, 0x0063, 0x006F, 0x0064, 0x0065];
///
/// let string = OsString::from_wide(&source[..]);
/// ```
fn from_wide(ucs: &[u16]) -> Self;
}

impl OsStringExt for OsString {
fn from_ucs2_lossy(ucs: &[u16]) -> Self {
fn from_wide(ucs: &[u16]) -> Self {
// Min capacity(in case of all ASCII) is `ucs.len()`
let mut buf = String::with_capacity(ucs.len());

Expand Down
1 change: 0 additions & 1 deletion library/std/src/os/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

pub mod env;
pub mod ffi;
pub mod raw;
3 changes: 0 additions & 3 deletions library/std/src/os/uefi/raw.rs

This file was deleted.

8 changes: 1 addition & 7 deletions library/std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,6 @@ pub enum Prefix<'a> {
/// Prefix `C:` for the given disk drive.
#[stable(feature = "rust1", since = "1.0.0")]
Disk(#[stable(feature = "rust1", since = "1.0.0")] u8),

/// UEFI Device Prefix. e.g., `PciRoot(0x0)/Pci(0x1,0x1)/Ata(0x0)/CDROM(0x0)`
/// Sometimes also represented like Windows Disks, but this is more general
#[unstable(feature = "uefi_std", issue = "100499")]
UefiDevice(&'a OsStr),
}

impl<'a> Prefix<'a> {
Expand All @@ -209,7 +204,6 @@ impl<'a> Prefix<'a> {
UNC(x, y) => 2 + os_str_len(x) + if os_str_len(y) > 0 { 1 + os_str_len(y) } else { 0 },
DeviceNS(x) => 4 + os_str_len(x),
Disk(_) => 2,
UefiDevice(x) => os_str_len(x),
}
}

Expand Down Expand Up @@ -2165,7 +2159,7 @@ impl Path {
!self.is_absolute()
}

pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
fn prefix(&self) -> Option<Prefix<'_>> {
self.components().prefix
}

Expand Down
37 changes: 15 additions & 22 deletions library/std/src/sys/uefi/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//! Takes a lot of inspiration from Windows allocator for Alignment > 8.

use crate::alloc::{GlobalAlloc, Layout, System};
use crate::os::uefi;
use crate::ptr;
use crate::sys::uefi::common;

pub(crate) const POOL_ALIGNMENT: usize = 8;
// FIXME: Maybe allow chaing the MEMORY_TYPE. However, since allocation is done even before main,
// there will be a few allocations with the default MEMORY_TYPE.

const MEMORY_TYPE: u32 = r_efi::efi::LOADER_DATA;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
Expand All @@ -16,46 +16,39 @@ unsafe impl GlobalAlloc for System {
let align = layout.align();
let size = layout.size();

// Return NULL pointer if `layout.size == 0`
if size == 0 {
return core::ptr::null_mut();
}

// Return NULL pointer if boot_services pointer cannot be obtained. The only time this
// should happen is if SystemTable has not been initialized
let boot_services = match uefi::env::get_boot_services() {
let boot_services = match common::get_boot_services() {
Some(x) => x,
None => return core::ptr::null_mut(),
None => return ptr::null_mut(),
};

let allocate_pool_ptr = unsafe { (*boot_services.as_ptr()).allocate_pool };

let mut ptr: *mut crate::ffi::c_void = crate::ptr::null_mut();
let mut ptr: *mut crate::ffi::c_void = ptr::null_mut();
let aligned_size = align_size(size, align);

let r = (allocate_pool_ptr)(MEMORY_TYPE, aligned_size, &mut ptr);

if r.is_error() || ptr.is_null() {
return crate::ptr::null_mut();
return ptr::null_mut();
}

unsafe { align_ptr(ptr.cast(), align) }
}

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
if layout.size() != 0 {
let boot_services = match uefi::env::get_boot_services() {
Some(x) => x,
None => return,
};
let boot_services = match common::get_boot_services() {
Some(x) => x,
None => return,
};

let free_pool_ptr = unsafe { (*boot_services.as_ptr()).free_pool };
let free_pool_ptr = unsafe { (*boot_services.as_ptr()).free_pool };

let ptr = unsafe { unalign_ptr(ptr, layout.align()) };
let r = (free_pool_ptr)(ptr.cast());
let ptr = unsafe { unalign_ptr(ptr, layout.align()) };
let r = (free_pool_ptr)(ptr.cast());

assert!(!r.is_error());
}
assert!(!r.is_error());
}
}

Expand Down
Loading