Skip to content
Open
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
17 changes: 13 additions & 4 deletions src/backends/efi_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ pub use crate::util::{inner_u32, inner_u64};
#[cfg(not(target_os = "uefi"))]
compile_error!("`efi_rng` backend can be enabled only for UEFI targets!");

#[inline]
fn is_uefi_error(status: r_efi::efi::Status) -> bool {
// Match EDK2 `EFI_ERROR(s)` (`s >= MAX_BIT`) and `Error::from_uefi_code`
// rather than `r_efi::Status::is_error()`, which masks the top two bits
// (`0xc0`) and classifies `0xC0..` high-bit statuses as non-errors.
const UEFI_ERROR_FLAG: usize = 1 << (usize::BITS - 1);
status.as_usize() & UEFI_ERROR_FLAG != 0
}

#[cold]
#[inline(never)]
fn init() -> Result<NonNull<rng::Protocol>, Error> {
Expand All @@ -39,7 +48,7 @@ fn init() -> Result<NonNull<rng::Protocol>, Error> {
)
};

if ret.is_error() {
if is_uefi_error(ret) {
return Err(Error::from_uefi_code(ret.as_usize()));
}

Expand All @@ -62,7 +71,7 @@ fn init() -> Result<NonNull<rng::Protocol>, Error> {
)
};

let protocol = if ret.is_error() {
let protocol = if is_uefi_error(ret) {
continue;
} else {
let protocol = unsafe { protocol.assume_init() };
Expand All @@ -81,7 +90,7 @@ fn init() -> Result<NonNull<rng::Protocol>, Error> {
)
};

if ret.is_error() {
if is_uefi_error(ret) {
continue;
}

Expand Down Expand Up @@ -109,7 +118,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
)
};

if ret.is_error() {
if is_uefi_error(ret) {
Err(Error::from_uefi_code(ret.as_usize()))
} else {
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/backends/esp_idf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::{ffi::c_void, mem::MaybeUninit};
pub use crate::util::{inner_u32, inner_u64};

unsafe extern "C" {
fn esp_fill_random(buf: *mut c_void, len: usize) -> u32;
fn esp_fill_random(buf: *mut c_void, len: usize);
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/backends/wasi_p1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
match ret {
0 => Ok(()),
// WASI functions should return positive error codes which are smaller than `MAX_ERROR_CODE`
code if code <= MAX_ERROR_CODE => Err(Error::from_neg_error_code(-code)),
code @ 1..=MAX_ERROR_CODE => Err(Error::from_neg_error_code(-code)),
_ => Err(Error::UNEXPECTED),
}
}
Loading