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
Fixes from PR
1. Make Errors constant
2. Fix UCS-2 conversions: char is UTF-32, and not UTF-8. Thus converting UCS-2
   to and from char can be done using basic u32 to conversions.
3. Use String and str intenally in `os_str`

Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
  • Loading branch information
Ayush1325 committed Dec 1, 2022
commit d84c8e368134add0b44abed8e422ce4acfc32a92
8 changes: 2 additions & 6 deletions 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;
mod error;
pub(crate) mod error;
mod impls;
pub mod prelude;
mod readbuf;
Expand Down Expand Up @@ -2406,11 +2406,7 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
}

fn consume(&mut self, amt: usize) {
if !self.done_first {
self.first.consume(amt)
} else {
self.second.consume(amt)
}
if !self.done_first { self.first.consume(amt) } else { self.second.consume(amt) }
}
}

Expand Down
75 changes: 10 additions & 65 deletions library/std/src/os/uefi/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::io;
use crate::mem::MaybeUninit;
use crate::ptr::NonNull;
use crate::sync::atomic::{AtomicPtr, Ordering};
use r_efi::efi::{Guid, Handle, Status};
use r_efi::efi::{Guid, Handle};
use r_efi::system;

static GLOBAL_SYSTEM_TABLE: AtomicPtr<SystemTable> = AtomicPtr::new(crate::ptr::null_mut());
Expand Down Expand Up @@ -48,35 +48,14 @@ pub fn get_runtime_services() -> Option<NonNull<RuntimeServices>> {
NonNull::new(runtime_services)
}

/// Get the Protocol for current system handle.
/// Note: Some protocols need to be manually freed. It is the callers responsibility to do so.
pub(crate) fn get_current_handle_protocol<T>(protocol_guid: &mut Guid) -> Option<NonNull<T>> {
let system_handle = get_system_handle()?;
get_handle_protocol(system_handle, protocol_guid)
}

pub(crate) fn get_handle_protocol<T>(
handle: NonNull<c_void>,
protocol_guid: &mut Guid,
) -> Option<NonNull<T>> {
let boot_services = get_boot_services()?;
let mut protocol: *mut c_void = crate::ptr::null_mut();

let r = unsafe {
((*boot_services.as_ptr()).handle_protocol)(handle.as_ptr(), protocol_guid, &mut protocol)
};

if r.is_error() { None } else { NonNull::new(protocol.cast()) }
}

pub(crate) fn open_protocol<T>(
handle: NonNull<c_void>,
mut protocol_guid: Guid,
) -> io::Result<NonNull<T>> {
let boot_services = get_boot_services()
.ok_or(io::Error::new(io::ErrorKind::Other, "Failed to get BootServices"))?;
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get BootServices"))?;
let system_handle = get_system_handle()
.ok_or(io::Error::new(io::ErrorKind::Other, "Failed to get System Handle"))?;
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get System Handle"))?;
let mut protocol: MaybeUninit<*mut T> = MaybeUninit::uninit();

let r = unsafe {
Expand All @@ -91,27 +70,10 @@ pub(crate) fn open_protocol<T>(
};

if r.is_error() {
match r {
Status::INVALID_PARAMETER => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER"))
}
Status::UNSUPPORTED => {
Err(io::Error::new(io::ErrorKind::Unsupported, "Handle does not support Protocol"))
}
Status::ACCESS_DENIED => {
Err(io::Error::new(io::ErrorKind::PermissionDenied, "EFI_ACCESS_DENIED"))
}
Status::ALREADY_STARTED => {
Err(io::Error::new(io::ErrorKind::Other, "EFI_ALREADY_STARTED"))
}
_ => Err(io::Error::new(
io::ErrorKind::Uncategorized,
format!("Status: {}", r.as_usize()),
)),
}
Err(super::io::status_to_io_error(r))
} else {
NonNull::new(unsafe { protocol.assume_init() })
.ok_or(io::Error::new(io::ErrorKind::Other, "Null Protocol"))
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Null Protocol"))
}
}

Expand All @@ -132,30 +94,13 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<c_void>>>
)
};

if r.is_error() {
match r {
Status::NOT_FOUND => {
Err(io::Error::new(io::ErrorKind::NotFound, "No handles match the search"))
}
Status::BUFFER_TOO_SMALL => Err(io::Error::new(
io::ErrorKind::InvalidData,
"The BufferSize is too small for the result",
)),
Status::INVALID_PARAMETER => {
Err(io::Error::new(io::ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER"))
}
_ => Err(io::Error::new(
io::ErrorKind::Uncategorized,
format!("Status: {}", r.as_usize()),
)),
}
} else {
Ok(())
}
if r.is_error() { Err(super::io::status_to_io_error(r)) } else { Ok(()) }
}

let boot_services = get_boot_services()
.ok_or(io::Error::new(io::ErrorKind::Other, "Unable to acquire boot services"))?;
let boot_services = get_boot_services().ok_or(io::error::const_io_error!(
io::ErrorKind::Other,
"Unable to acquire boot services"
))?;
let mut buf_len = 0usize;

match inner(&mut guid, boot_services, &mut buf_len, crate::ptr::null_mut()) {
Expand Down
55 changes: 55 additions & 0 deletions library/std/src/os/uefi/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use r_efi::efi::Status;

use crate::io::{self, ErrorKind};

pub(crate) fn status_to_io_error(s: r_efi::efi::Status) -> io::Error {
match s {
Status::INVALID_PARAMETER => {
io::error::const_io_error!(ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER")
}
Status::UNSUPPORTED => {
io::error::const_io_error!(ErrorKind::Unsupported, "EFI_UNSUPPORTED")
}
Status::BAD_BUFFER_SIZE => {
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_BAD_BUFFER_SIZE")
}
Status::INVALID_LANGUAGE => {
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_INVALID_LANGUAGE")
}
Status::CRC_ERROR => io::error::const_io_error!(ErrorKind::InvalidData, "EFI_CRC_ERROR"),
Status::BUFFER_TOO_SMALL => {
io::error::const_io_error!(ErrorKind::FileTooLarge, "EFI_BUFFER_TOO_SMALL")
}
Status::NOT_READY => io::error::const_io_error!(ErrorKind::ResourceBusy, "EFI_NOT_READY"),
Status::WRITE_PROTECTED => {
io::error::const_io_error!(ErrorKind::ReadOnlyFilesystem, "EFI_WRITE_PROTECTED")
}
Status::VOLUME_FULL => {
io::error::const_io_error!(ErrorKind::StorageFull, "EFI_VOLUME_FULL")
}
Status::MEDIA_CHANGED => {
io::error::const_io_error!(ErrorKind::StaleNetworkFileHandle, "EFI_MEDIA_CHANGED")
}
Status::NOT_FOUND => io::error::const_io_error!(ErrorKind::NotFound, "EFI_NOT_FOUND"),
Status::ACCESS_DENIED => {
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_ACCESS_DENIED")
}
Status::SECURITY_VIOLATION => {
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_SECURITY_VIOLATION")
}
Status::NO_RESPONSE => {
io::error::const_io_error!(ErrorKind::HostUnreachable, "EFI_NO_RESPONSE")
}
Status::TIMEOUT => io::error::const_io_error!(ErrorKind::TimedOut, "EFI_TIMEOUT"),
Status::END_OF_FILE => {
io::error::const_io_error!(ErrorKind::UnexpectedEof, "EFI_END_OF_FILE")
}
Status::IP_ADDRESS_CONFLICT => {
io::error::const_io_error!(ErrorKind::AddrInUse, "EFI_IP_ADDRESS_CONFLICT")
}
Status::HTTP_ERROR => {
io::error::const_io_error!(ErrorKind::NetworkUnreachable, "EFI_HTTP_ERROR")
}
_ => io::Error::new(ErrorKind::Uncategorized, format!("Status: {}", s.as_usize())),
}
}
2 changes: 1 addition & 1 deletion library/std/src/os/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

pub mod env;
pub mod ffi;
pub mod io;
pub mod net;
pub mod path;
pub mod raw;
pub mod thread;
11 changes: 7 additions & 4 deletions library/std/src/os/uefi/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ impl TryFrom<NonNull<device_path::Protocol>> for PathBuf {
let mut path = String::new();
for c in ucs2_iter {
let ch = char::from(ucs2::Ucs2Char::from_u16(u16::from(c)).ok_or(
io::Error::new(io::ErrorKind::InvalidData, "Invalid Device Path Text"),
io::error::const_io_error!(
io::ErrorKind::InvalidData,
"Invalid Device Path Text"
),
)?);
path.push(ch);
len += 1;
Expand All @@ -58,7 +61,7 @@ impl TryFrom<NonNull<device_path::Protocol>> for PathBuf {
}
return Ok(PathBuf::from(path));
}
Err(crate::io::Error::new(
Err(crate::io::error::const_io_error!(
crate::io::ErrorKind::InvalidData,
"Failed to Convert to text representation",
))
Expand Down Expand Up @@ -92,7 +95,7 @@ impl TryFrom<&OsStr> for DevicePath {
};
let device_path = match NonNull::new(device_path) {
None => {
return Err(io::Error::new(
return Err(io::error::const_io_error!(
io::ErrorKind::Uncategorized,
"Null DevicePath Returned",
));
Expand All @@ -105,7 +108,7 @@ impl TryFrom<&OsStr> for DevicePath {
.unwrap();
return Ok(DevicePath::new(device_path, layout));
}
Err(crate::io::Error::new(
Err(crate::io::error::const_io_error!(
crate::io::ErrorKind::InvalidData,
"Failed to Convert to text representation",
))
Expand Down
12 changes: 9 additions & 3 deletions library/std/src/os/uefi/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ impl<T> VariableSizeType<T> {
}

pub(crate) fn from_size(size: usize) -> io::Result<Self> {
let layout = Layout::from_size_align(size, Self::ALIGNMENT)
.map_err(|_| io::Error::new(io::ErrorKind::Uncategorized, "Invalid buffer size"))?;
let layout = Layout::from_size_align(size, Self::ALIGNMENT).map_err(|_| {
io::error::const_io_error!(io::ErrorKind::Uncategorized, "Invalid buffer size")
})?;
let inner: NonNull<T> = Global
.allocate(layout)
.map_err(|_| io::Error::new(io::ErrorKind::Uncategorized, "Failed to allocate Buffer"))?
.map_err(|_| {
io::error::const_io_error!(
io::ErrorKind::Uncategorized,
"Failed to allocate Buffer"
)
})?
.cast();
Ok(Self::new(inner, layout))
}
Expand Down
108 changes: 0 additions & 108 deletions library/std/src/os/uefi/thread.rs

This file was deleted.

5 changes: 3 additions & 2 deletions library/std/src/sys/uefi/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::common;
use crate::ffi::OsString;
use crate::fmt;
use crate::num::NonZeroU16;
use crate::os::uefi::{self, ffi::OsStringExt};
use crate::os::uefi::ffi::OsStringExt;
use crate::sys_common::ucs2::Ucs2Units;
use crate::vec;
use core::iter;
Expand All @@ -14,7 +15,7 @@ pub fn args() -> Args {
use r_efi::efi::protocols::loaded_image;

let mut protocol_guid = loaded_image::PROTOCOL_GUID;
match uefi::env::get_current_handle_protocol::<loaded_image::Protocol>(&mut protocol_guid) {
match common::get_current_handle_protocol::<loaded_image::Protocol>(&mut protocol_guid) {
Some(x) => {
let lp_cmd_line = unsafe { (*x.as_ptr()).load_options as *const u16 };
let parsed_args_list =
Expand Down
Loading