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

uefi-raw: unified boolean type #1307

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion uefi-raw/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# uefi-raw - [Unreleased]

## Added
- `Boolean` type

# uefi-raw - 0.8.0 (2024-09-09)

## Added

- Added `PAGE_SIZE` constant.

- Added a new `unstable` feature
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed


# uefi-raw - 0.7.0 (2024-08-20)

Expand Down
4 changes: 4 additions & 0 deletions uefi-raw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ bitflags.workspace = true
ptr_meta.workspace = true
uguid.workspace = true

[features]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no error type in uefi-raw now, so I think the features section can be dropped

default = []
unstable = []

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
65 changes: 63 additions & 2 deletions uefi-raw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ pub mod time;

mod status;

use core::ffi::c_void;
use core::fmt::{self, Debug, Formatter};
pub use status::Status;
pub use uguid::{guid, Guid};

use core::ffi::c_void;
use core::fmt::{self, Debug, Formatter};

/// Handle to an event structure.
pub type Event = *mut c_void;

Expand Down Expand Up @@ -65,6 +66,43 @@ pub type PhysicalAddress = u64;
/// of target platform.
pub type VirtualAddress = u64;

/// ABI-compatible UEFI boolean.
///
/// Opaque 1-byte value holding either `0` for FALSE or a `1` for TRUE. This
/// type can be converted from and to `bool` via corresponding [`From`]
/// implementations.
#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq, Hash)]
#[repr(transparent)]
pub struct Boolean(pub u8);

impl Boolean {
/// [`Boolean`] representing `true`.
pub const TRUE: Self = Self(1);

/// [`Boolean`] representing `false`.
pub const FALSE: Self = Self(0);
}

impl From<bool> for Boolean {
fn from(value: bool) -> Self {
match value {
true => Self(1),
false => Self(0),
}
}
}

impl From<Boolean> for bool {
#[allow(clippy::match_like_matches_macro)]
fn from(value: Boolean) -> Self {
// We handle it as in C: Any bit-pattern != 0 equals true
match value.0 {
0 => false,
_ => true,
}
}
}

/// An IPv4 internet protocol address.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
Expand Down Expand Up @@ -133,3 +171,26 @@ impl Default for IpAddress {
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct MacAddress(pub [u8; 32]);

#[cfg(test)]
mod tests {
use super::*;

#[test]
/// Test the properties promised in [0]. This also applies for the other
/// architectures.
///
/// [0] https://github.com/tianocore/edk2/blob/b0f43dd3fdec2363e3548ec31eb455dc1c4ac761/MdePkg/Include/X64/ProcessorBind.h#L192
fn test_boolean_abi() {
assert_eq!(size_of::<Boolean>(), 1);
assert_eq!(Boolean::from(true).0, 1);
assert_eq!(Boolean::from(false).0, 0);
assert_eq!(Boolean::TRUE.0, 1);
assert_eq!(Boolean::FALSE.0, 0);
assert_eq!(bool::from(Boolean(0b0)), false);
assert_eq!(bool::from(Boolean(0b1)), true);
// We do it a C: Every bit pattern not 0 is equal to true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// We do it a C: Every bit pattern not 0 is equal to true
// We do it as in C: Every bit pattern not 0 is equal to true

assert_eq!(bool::from(Boolean(0b11111110)), true);
assert_eq!(bool::from(Boolean(0b11111111)), true);
}
}
14 changes: 7 additions & 7 deletions uefi-raw/src/protocol/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{guid, Guid, Status};
use crate::{guid, Boolean, Guid, Status};
use core::ffi::c_void;

/// Logical block address.
Expand All @@ -9,11 +9,11 @@ pub type Lba = u64;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BlockIoMedia {
pub media_id: u32,
pub removable_media: bool,
pub media_present: bool,
pub logical_partition: bool,
pub read_only: bool,
pub write_caching: bool,
pub removable_media: Boolean,
pub media_present: Boolean,
pub logical_partition: Boolean,
pub read_only: Boolean,
pub write_caching: Boolean,
pub block_size: u32,
pub io_align: u32,
pub last_block: Lba,
Expand All @@ -31,7 +31,7 @@ pub struct BlockIoMedia {
pub struct BlockIoProtocol {
pub revision: u64,
pub media: *const BlockIoMedia,
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: bool) -> Status,
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: Boolean) -> Status,
pub read_blocks: unsafe extern "efiapi" fn(
this: *const Self,
media_id: u32,
Expand Down
18 changes: 9 additions & 9 deletions uefi-raw/src/protocol/console.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod serial;

use crate::{guid, Char16, Event, Guid, PhysicalAddress, Status};
use crate::{guid, Boolean, Char16, Event, Guid, PhysicalAddress, Status};
use bitflags::bitflags;
use core::ptr;

Expand Down Expand Up @@ -42,7 +42,7 @@ pub struct AbsolutePointerState {
#[derive(Debug)]
#[repr(C)]
pub struct AbsolutePointerProtocol {
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: u8) -> Status,
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: Boolean) -> Status,
pub get_state:
unsafe extern "efiapi" fn(this: *const Self, state: *mut AbsolutePointerState) -> Status,
pub wait_for_input: Event,
Expand All @@ -63,7 +63,7 @@ pub struct InputKey {
#[derive(Debug)]
#[repr(C)]
pub struct SimpleTextInputProtocol {
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: bool) -> Status,
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: Boolean) -> Status,
pub read_key_stroke: unsafe extern "efiapi" fn(this: *mut Self, key: *mut InputKey) -> Status,
pub wait_for_key: Event,
}
Expand All @@ -80,13 +80,13 @@ pub struct SimpleTextOutputMode {
pub attribute: i32,
pub cursor_column: i32,
pub cursor_row: i32,
pub cursor_visible: bool,
pub cursor_visible: Boolean,
}

#[derive(Debug)]
#[repr(C)]
pub struct SimpleTextOutputProtocol {
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended: bool) -> Status,
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended: Boolean) -> Status,
pub output_string: unsafe extern "efiapi" fn(this: *mut Self, string: *const Char16) -> Status,
pub test_string: unsafe extern "efiapi" fn(this: *mut Self, string: *const Char16) -> Status,
pub query_mode: unsafe extern "efiapi" fn(
Expand All @@ -100,7 +100,7 @@ pub struct SimpleTextOutputProtocol {
pub clear_screen: unsafe extern "efiapi" fn(this: *mut Self) -> Status,
pub set_cursor_position:
unsafe extern "efiapi" fn(this: *mut Self, column: usize, row: usize) -> Status,
pub enable_cursor: unsafe extern "efiapi" fn(this: *mut Self, visible: bool) -> Status,
pub enable_cursor: unsafe extern "efiapi" fn(this: *mut Self, visible: Boolean) -> Status,
pub mode: *mut SimpleTextOutputMode,
}

Expand All @@ -124,16 +124,16 @@ pub struct SimplePointerState {
pub relative_movement_x: i32,
pub relative_movement_y: i32,
pub relative_movement_z: i32,
pub left_button: u8,
pub right_button: u8,
pub left_button: Boolean,
pub right_button: Boolean,
}

#[derive(Debug)]
#[repr(C)]
pub struct SimplePointerProtocol {
pub reset: unsafe extern "efiapi" fn(
this: *mut SimplePointerProtocol,
extended_verification: bool,
extended_verification: Boolean,
) -> Status,
pub get_state: unsafe extern "efiapi" fn(
this: *mut SimplePointerProtocol,
Expand Down
10 changes: 5 additions & 5 deletions uefi-raw/src/protocol/device_path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{guid, Char16, Guid};
use crate::{guid, Boolean, Char16, Guid};

/// Device path protocol.
///
Expand All @@ -25,13 +25,13 @@ impl DevicePathProtocol {
pub struct DevicePathToTextProtocol {
pub convert_device_node_to_text: unsafe extern "efiapi" fn(
device_node: *const DevicePathProtocol,
display_only: bool,
allow_shortcuts: bool,
display_only: Boolean,
allow_shortcuts: Boolean,
) -> *const Char16,
pub convert_device_path_to_text: unsafe extern "efiapi" fn(
device_path: *const DevicePathProtocol,
display_only: bool,
allow_shortcuts: bool,
display_only: Boolean,
allow_shortcuts: Boolean,
) -> *const Char16,
}

Expand Down
4 changes: 2 additions & 2 deletions uefi-raw/src/protocol/file_system.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::time::Time;
use crate::{guid, Char16, Event, Guid, Status};
use crate::{guid, Boolean, Char16, Event, Guid, Status};
use bitflags::bitflags;
use core::ffi::c_void;

Expand Down Expand Up @@ -151,7 +151,7 @@ impl FileInfo {
#[repr(C)]
pub struct FileSystemInfo {
pub size: u64,
pub read_only: u8,
pub read_only: Boolean,
pub volume_size: u64,
pub free_space: u64,
pub block_size: u32,
Expand Down
6 changes: 3 additions & 3 deletions uefi-raw/src/protocol/media.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::protocol::device_path::DevicePathProtocol;
use crate::{guid, Guid, Status};
use crate::{guid, Boolean, Guid, Status};
use core::ffi::c_void;

#[derive(Debug)]
Expand All @@ -8,7 +8,7 @@ pub struct LoadFileProtocol {
pub load_file: unsafe extern "efiapi" fn(
this: *mut LoadFileProtocol,
file_path: *const DevicePathProtocol,
boot_policy: bool,
boot_policy: Boolean,
buffer_size: *mut usize,
buffer: *mut c_void,
) -> Status,
Expand All @@ -24,7 +24,7 @@ pub struct LoadFile2Protocol {
pub load_file: unsafe extern "efiapi" fn(
this: *mut LoadFile2Protocol,
file_path: *const DevicePathProtocol,
boot_policy: bool,
boot_policy: Boolean,
buffer_size: *mut usize,
buffer: *mut c_void,
) -> Status,
Expand Down
4 changes: 2 additions & 2 deletions uefi-raw/src/protocol/network/dhcp4.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{guid, Char8, Event, Guid, Ipv4Address, MacAddress, Status};
use crate::{guid, Boolean, Char8, Event, Guid, Ipv4Address, MacAddress, Status};
use core::ffi::c_void;

newtype_enum! {
Expand Down Expand Up @@ -148,7 +148,7 @@ pub struct Dhcp4Protocol {
pub start: unsafe extern "efiapi" fn(this: *mut Self, completion_event: Event) -> Status,
pub renew_rebind: unsafe extern "efiapi" fn(
this: *mut Self,
rebind_request: bool,
rebind_request: Boolean,
completion_event: Event,
) -> Status,
pub release: unsafe extern "efiapi" fn(this: *mut Self) -> Status,
Expand Down
6 changes: 3 additions & 3 deletions uefi-raw/src/protocol/network/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{guid, Char16, Char8, Event, Guid, Ipv4Address, Ipv6Address, Status};
use crate::{guid, Boolean, Char16, Char8, Event, Guid, Ipv4Address, Ipv6Address, Status};
use core::ffi::c_void;
use core::fmt::{self, Debug, Formatter};

Expand All @@ -7,7 +7,7 @@ use core::fmt::{self, Debug, Formatter};
pub struct HttpConfigData {
pub http_version: HttpVersion,
pub time_out_millisec: u32,
pub local_addr_is_ipv6: bool,
pub local_addr_is_ipv6: Boolean,
pub access_point: HttpAccessPoint,
}

Expand All @@ -22,7 +22,7 @@ newtype_enum! {
#[derive(Debug)]
#[repr(C)]
pub struct HttpV4AccessPoint {
pub use_default_addr: bool,
pub use_default_addr: Boolean,
pub local_address: Ipv4Address,
pub local_subnet: Ipv4Address,
pub local_port: u16,
Expand Down
6 changes: 3 additions & 3 deletions uefi-raw/src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::protocol::device_path::DevicePathProtocol;
use crate::table::Header;
use crate::{Char16, Event, Guid, Handle, PhysicalAddress, Status, VirtualAddress};
use crate::{Boolean, Char16, Event, Guid, Handle, PhysicalAddress, Status, VirtualAddress};
use bitflags::bitflags;
use core::ffi::c_void;
use core::ops::RangeInclusive;
Expand Down Expand Up @@ -103,7 +103,7 @@ pub struct BootServices {

// Image services
pub load_image: unsafe extern "efiapi" fn(
boot_policy: u8,
boot_policy: Boolean,
parent_image_handle: Handle,
device_path: *const DevicePathProtocol,
source_buffer: *const u8,
Expand Down Expand Up @@ -140,7 +140,7 @@ pub struct BootServices {
controller: Handle,
driver_image: Handle,
remaining_device_path: *const DevicePathProtocol,
recursive: bool,
recursive: Boolean,
) -> Status,
pub disconnect_controller: unsafe extern "efiapi" fn(
controller: Handle,
Expand Down
4 changes: 2 additions & 2 deletions uefi-raw/src/table/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::capsule::CapsuleHeader;
use crate::table::boot::MemoryDescriptor;
use crate::table::Header;
use crate::time::Time;
use crate::{guid, Char16, Guid, PhysicalAddress, Status};
use crate::{guid, Boolean, Char16, Guid, PhysicalAddress, Status};
use bitflags::bitflags;
use core::ffi::c_void;

Expand Down Expand Up @@ -115,7 +115,7 @@ pub struct TimeCapabilities {

/// Whether a time set operation clears the device's time below the
/// "resolution" reporting level. False for normal PC-AT CMOS RTC devices.
pub sets_to_zero: bool,
pub sets_to_zero: Boolean,
}

bitflags! {
Expand Down
4 changes: 2 additions & 2 deletions uefi-test-runner/src/proto/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use uefi::proto::BootPolicy;
use uefi::{boot, Guid, Handle};
use uefi_raw::protocol::device_path::DevicePathProtocol;
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
use uefi_raw::Status;
use uefi_raw::{Boolean, Status};

unsafe extern "efiapi" fn raw_load_file(
this: *mut LoadFile2Protocol,
_file_path: *const DevicePathProtocol,
_boot_policy: bool,
_boot_policy: Boolean,
buffer_size: *mut usize,
buffer: *mut c_void,
) -> Status {
Expand Down
2 changes: 1 addition & 1 deletion uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ details of the deprecated items that were removed in this release.
- **Breaking:** `FileSystem` no longer has a lifetime parameter, and the
deprecated conversion from `uefi::table::boot::ScopedProtocol` has been
removed.
- **Breaking:** Removed `BootPolicyError` as `BootPolicy`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **Breaking:** Removed `BootPolicyError` as `BootPolicy`
- **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no longer fallible.

- Fixed `boot::open_protocol` to properly handle a null interface pointer.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit: per your changes in 4d1906e, two blank lines between each version entry

# uefi - 0.32.0 (2024-09-09)

See [Deprecating SystemTable/BootServices/RuntimeServices][funcmigrate] for
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ pub fn connect_controller(
.map(|dp| dp.as_ffi_ptr())
.unwrap_or(ptr::null())
.cast(),
recursive,
recursive.into(),
)
}
.to_result_with_err(|_| ())
Expand Down
Loading