Skip to content

Commit 235bc84

Browse files
authored
Merge pull request #556 from phip1611/read-entry-return-code
documentation and code improvements for Status, Error, and read()
2 parents b342da6 + d295e8d commit 235bc84

File tree

8 files changed

+50
-15
lines changed

8 files changed

+50
-15
lines changed

uefi/src/data_types/strs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl CStr16 {
337337
}
338338

339339
/// Writes each [`Char16`] as a [`char`] (4 bytes long in Rust language) into the buffer.
340-
/// It is up the the implementer of [`core::fmt::Write`] to convert the char to a string
340+
/// It is up to the implementer of [`core::fmt::Write`] to convert the char to a string
341341
/// with proper encoding/charset. For example, in the case of [`alloc::string::String`]
342342
/// all Rust chars (UTF-32) get converted to UTF-8.
343343
///

uefi/src/proto/console/text/output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'boot> Output<'boot> {
110110
(self.query_mode)(self, index, &mut columns, &mut rows).into_with_val(|| (columns, rows))
111111
}
112112

113-
/// Returns the the current text mode.
113+
/// Returns the current text mode.
114114
pub fn current_mode(&self) -> Result<Option<OutputMode>> {
115115
match self.data.mode {
116116
-1 => Ok(None),

uefi/src/proto/media/file/dir.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ impl Directory {
5050
FileInfo::assert_aligned(buffer);
5151

5252
// Read the directory entry into the aligned storage
53-
self.0.read(buffer).map(|size| {
54-
if size != 0 {
55-
unsafe { Some(FileInfo::from_uefi(buffer.as_mut_ptr().cast::<c_void>())) }
56-
} else {
53+
self.0.read(buffer).map(|read_bytes| {
54+
// 0 read bytes signals that the last directory entry was read
55+
let last_directory_entry_read = read_bytes == 0;
56+
if last_directory_entry_read {
5757
None
58+
} else {
59+
unsafe { Some(FileInfo::from_uefi(buffer.as_mut_ptr().cast::<c_void>())) }
5860
}
5961
})
6062
}

uefi/src/proto/media/file/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub trait File: Sized {
182182
fn is_directory(&self) -> Result<bool>;
183183
}
184184

185-
// Internal File helper methods to access the funciton pointer table.
185+
// Internal File helper methods to access the function pointer table.
186186
trait FileInternal: File {
187187
fn imp(&mut self) -> &mut FileImpl {
188188
unsafe { &mut *self.handle().0 }
@@ -286,6 +286,19 @@ pub(super) struct FileImpl {
286286
) -> Status,
287287
close: extern "efiapi" fn(this: &mut FileImpl) -> Status,
288288
delete: extern "efiapi" fn(this: &mut FileImpl) -> Status,
289+
/// # Read from Regular Files
290+
/// If `self` is not a directory, the function reads the requested number of bytes from the file
291+
/// at the file’s current position and returns them in `buffer`. If the read goes beyond the end
292+
/// of the file, the read length is truncated to the end of the file. The file’s current
293+
/// position is increased by the number of bytes returned.
294+
///
295+
/// # Read from Directory
296+
/// If `self` is a directory, the function reads the directory entry at the file’s current
297+
/// position and returns the entry in `buffer`. If the `buffer` is not large enough to hold the
298+
/// current directory entry, then `EFI_BUFFER_TOO_SMALL` is returned and the current file
299+
/// position is not updated. `buffer_size` is set to be the size of the buffer needed to read
300+
/// the entry. On success, the current position is updated to the next directory entry. If there
301+
/// are no more directory entries, the read returns a zero-length buffer.
289302
read: unsafe extern "efiapi" fn(
290303
this: &mut FileImpl,
291304
buffer_size: &mut usize,

uefi/src/proto/media/file/regular.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl RegularFile {
2121
Self(handle)
2222
}
2323

24-
/// Read data from file
24+
/// Read data from file.
2525
///
2626
/// Try to read as much as possible into `buffer`. Returns the number of bytes that were
2727
/// actually read.
@@ -38,10 +38,15 @@ impl RegularFile {
3838
/// and the required buffer size is provided as output.
3939
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Option<usize>> {
4040
let mut buffer_size = buffer.len();
41-
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) }.into_with(
41+
let status =
42+
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) };
43+
44+
status.into_with(
4245
|| buffer_size,
4346
|s| {
4447
if s == Status::BUFFER_TOO_SMALL {
48+
// `buffer_size` was updated to the required buffer size by the underlying read
49+
// function.
4550
Some(buffer_size)
4651
} else {
4752
None

uefi/src/result/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub use self::status::Status;
1414
/// Almost all UEFI operations provide a status code as an output which
1515
/// indicates either success, a warning, or an error. This type alias maps
1616
/// [`Status::SUCCESS`] to the `Ok` variant (with optional `Output` data), and
17-
/// maps both warning and error statuses to the `Err` variant (with optional
18-
/// `ErrData`).
17+
/// maps both warning and error statuses to the `Err` variant of type [`Error`],
18+
/// which may carry optional inner `ErrData`.
1919
///
2020
/// Warnings are treated as errors by default because they generally indicate
2121
/// an abnormal situation.

uefi/src/result/status.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ newtype_enum! {
1212
/// enum, as injecting an unknown value in a Rust enum is undefined behaviour.
1313
///
1414
/// For lack of a better option, we therefore model them as a newtype of usize.
15+
///
16+
/// For a convenient integration into the Rust ecosystem, there are multiple
17+
/// factory methods to convert a Status into a [`uefi::Result`]:
18+
/// - [`Status::into_with`]
19+
/// - [`Status::into_with_val`]
20+
/// - [`Status::into_with_err`]
1521
#[must_use]
1622
pub enum Status: usize => {
1723
/// The operation completed successfully.
@@ -122,7 +128,10 @@ impl Status {
122128
self.0 & ERROR_BIT != 0
123129
}
124130

125-
/// Converts this status code into a result with a given value.
131+
/// Converts this status code into a [`uefi::Result`] with a given `Ok` value.
132+
///
133+
/// If the status does not indicate success, the status representing the specific error
134+
/// code is embedded into the `Err` variant of type [`uefi::Error`].
126135
#[inline]
127136
pub fn into_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
128137
if self.is_success() {
@@ -132,7 +141,10 @@ impl Status {
132141
}
133142
}
134143

135-
/// Converts this status code into a result with a given error payload
144+
/// Converts this status code into a [`uefi::Result`] with a given `Err` payload.
145+
///
146+
/// If the status does not indicate success, the status representing the specific error
147+
/// code is embedded into the `Err` variant of type [`uefi::Error`].
136148
#[inline]
137149
pub fn into_with_err<ErrData: Debug>(
138150
self,
@@ -145,7 +157,10 @@ impl Status {
145157
}
146158
}
147159

148-
/// Convert this status code into a result with a given value and error payload
160+
/// Convert this status code into a result with a given `Ok` value and `Err` payload.
161+
///
162+
/// If the status does not indicate success, the status representing the specific error
163+
/// code is embedded into the `Err` variant of type [`uefi::Error`].
149164
#[inline]
150165
pub fn into_with<T, ErrData: Debug>(
151166
self,

xtask/src/device_path/node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl Node {
165165
}
166166

167167
// It's not trivial to nicely format the DST data since
168-
// the the slice might be unaligned. Treat it as a byte
168+
// the slice might be unaligned. Treat it as a byte
169169
// slice instead.
170170
quote!({
171171
let ptr = addr_of!(#field_val);

0 commit comments

Comments
 (0)