Skip to content

Commit f8d6fca

Browse files
authored
Merge pull request #1981 from rust-osdev/improvements3
uefi: streamline + fix usize_from_u32()
2 parents 4be8221 + d10507a commit f8d6fca

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

uefi/src/proto/ata/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! ATA Protocols.
44
55
use crate::mem::{AlignedBuffer, AlignmentError};
6-
use crate::util::usize_from_u32;
76
use core::alloc::LayoutError;
87
use core::marker::PhantomData;
98
use core::ptr;
@@ -64,8 +63,8 @@ impl<'a> AtaRequestBuilder<'a> {
6463
protocol: AtaPassThruCommandProtocol,
6564
) -> Result<Self, LayoutError> {
6665
// status block has alignment requirements!
67-
let mut asb =
68-
AlignedBuffer::from_size_align(size_of::<AtaStatusBlock>(), usize_from_u32(io_align))?;
66+
let io_align_usize = usize::try_from(io_align).expect("I/O alignment should fit in usize");
67+
let mut asb = AlignedBuffer::from_size_align(size_of::<AtaStatusBlock>(), io_align_usize)?;
6968
Ok(Self {
7069
req: AtaRequest {
7170
io_align,

uefi/src/proto/loaded_image.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ impl LoadedImage {
7171
/// [`&CStr16`]: `CStr16`
7272
/// [`load_options_as_bytes`]: `Self::load_options_as_bytes`
7373
pub fn load_options_as_cstr16(&self) -> Result<&CStr16, LoadOptionsError> {
74-
let load_options_size = usize_from_u32(self.0.load_options_size);
74+
let load_options_size = usize::try_from(self.0.load_options_size)
75+
.expect("load options size should fit in usize");
7576

7677
if self.0.load_options.is_null() {
7778
Err(LoadOptionsError::NotSet)

uefi/src/proto/tcg/v2.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use super::{AlgorithmId, EventType, HashAlgorithm, PcrIndex, v1};
1616
use crate::data_types::{Align, PhysicalAddress, UnalignedSlice};
1717
use crate::proto::unsafe_protocol;
18-
use crate::util::{ptr_write_unaligned_and_add, usize_from_u32};
18+
use crate::util::ptr_write_unaligned_and_add;
1919
use crate::{Error, Result, Status, StatusExt};
2020
use core::fmt::{self, Debug, Formatter};
2121
use core::marker::PhantomData;
@@ -279,7 +279,8 @@ impl<'a> EventLogHeader<'a> {
279279
let version_major = *event.get(21)?;
280280
let version_errata = *event.get(22)?;
281281
let uintn_size = *event.get(23)?;
282-
let number_of_algorithms = usize_from_u32(u32_le_from_bytes_at_offset(event, 24)?);
282+
let number_of_algorithms = usize::try_from(u32_le_from_bytes_at_offset(event, 24)?)
283+
.expect("algorithm count should fit in usize");
283284
let vendor_info_size_byte_offset =
284285
28 + (number_of_algorithms * size_of::<AlgorithmDigestSize>());
285286
let vendor_info_size = usize::from(*event.get(vendor_info_size_byte_offset)?);
@@ -462,7 +463,8 @@ impl<'a> PcrEvent<'a> {
462463

463464
let digests = unsafe { slice::from_raw_parts(digests_ptr, digests_byte_size) };
464465
let event_size_ptr = unsafe { digests_ptr.add(digests_byte_size) };
465-
let event_size = usize_from_u32(unsafe { event_size_ptr.cast::<u32>().read_unaligned() });
466+
let event_size = usize::try_from(unsafe { event_size_ptr.cast::<u32>().read_unaligned() })
467+
.expect("event size should fit in usize");
466468
let event_data_ptr = unsafe { event_size_ptr.add(4) };
467469
let event_data = unsafe { slice::from_raw_parts(event_data_ptr, event_size) };
468470

uefi/src/util.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ pub const unsafe fn ptr_write_unaligned_and_add<T>(ptr: &mut *mut u8, val: T) {
1111
}
1212
}
1313

14-
/// Convert from a `u32` to a `usize`. Panic if the input does fit. On typical
15-
/// targets `usize` is at least as big as `u32`, so this should never panic
16-
/// except on unusual targets.
14+
/// Converts a `u32` to `usize`.
1715
///
18-
/// Comparison to alternatives:
19-
/// * `val as usize` doesn't check that `val` actually fits in a `usize`.
20-
/// * `usize::try_from(val).unwrap()` doesn't work in a const context.
16+
/// Panics if `val` does not fit in `usize`.
17+
///
18+
/// On targets where `usize` is at least 32 bits wide, this never panics.
19+
///
20+
/// Unlike `as`, this does not silently truncate on narrow `usize` targets.
21+
/// Unlike `usize::try_from(...).unwrap()`, this works in `const` contexts.
2122
pub const fn usize_from_u32(val: u32) -> usize {
22-
// This is essentially the same as `usize::try_from(val).unwrap()`, but
23-
// works in a `const` context on stable.
24-
if size_of::<usize>() < size_of::<u32>() && val < (usize::MAX as u32) {
25-
panic!("value does not fit in a usize");
26-
} else {
27-
val as usize
23+
if size_of::<usize>() < size_of::<u32>() && val > usize::MAX as u32 {
24+
panic!("value does not fit in usize");
2825
}
26+
27+
val as usize
2928
}
3029

3130
/// Get the raw pointer from `opt`, defaulting to `null_mut`.

0 commit comments

Comments
 (0)