From d60ce4ab7641afd21e729708d5e954513648911e Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Wed, 20 Dec 2023 23:11:47 -0500 Subject: [PATCH 1/2] uefi-raw: Derive Default for address types --- uefi-raw/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/uefi-raw/src/lib.rs b/uefi-raw/src/lib.rs index b464323a3..2b08d38fb 100644 --- a/uefi-raw/src/lib.rs +++ b/uefi-raw/src/lib.rs @@ -59,16 +59,16 @@ pub type PhysicalAddress = u64; pub type VirtualAddress = u64; /// An IPv4 internet protocol address. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)] pub struct Ipv4Address(pub [u8; 4]); /// An IPv6 internet protocol address. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)] pub struct Ipv6Address(pub [u8; 16]); /// A Media Access Control (MAC) address. -#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)] pub struct MacAddress(pub [u8; 32]); From c4b928a5aef6355d5a16e5c3cc970401b5bff0f2 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 22 Dec 2023 18:06:41 -0500 Subject: [PATCH 2/2] uefi-raw: Add IpAddress type For compatibility with typical C code, the type is defined the same way as in edk2; a union rather than a plain byte buffer. --- uefi-raw/CHANGELOG.md | 2 +- uefi-raw/src/lib.rs | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 8c49a40ad..aff264b5b 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -1,7 +1,7 @@ # uefi-raw - [Unreleased] ## Added -- Added `Ipv4Address`, `Ipv6Address`, and `MacAddress` types. +- Added `IpAddress`, `Ipv4Address`, `Ipv6Address`, and `MacAddress` types. - Added `ServiceBindingProtocol`, `Dhcp4Protocol`, `HttpProtocol`, `Ip4Config2Protocol`, `TlsConfigurationProtocol`, and related types. diff --git a/uefi-raw/src/lib.rs b/uefi-raw/src/lib.rs index 2b08d38fb..0340d1068 100644 --- a/uefi-raw/src/lib.rs +++ b/uefi-raw/src/lib.rs @@ -26,6 +26,7 @@ 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}; @@ -68,6 +69,60 @@ pub struct Ipv4Address(pub [u8; 4]); #[repr(transparent)] pub struct Ipv6Address(pub [u8; 16]); +/// An IPv4 or IPv6 internet protocol address. +/// +/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This +/// type is defined in the same way as edk2 for compatibility with C code. Note +/// that this is an untagged union, so there's no way to tell which type of +/// address an `IpAddress` value contains without additional context. +#[derive(Clone, Copy)] +#[repr(C)] +pub union IpAddress { + /// This member serves to align the whole type to a 4 bytes as required by + /// the spec. Note that this is slightly different from `repr(align(4))`, + /// which would prevent placing this type in a packed structure. + pub addr: [u32; 4], + + /// An IPv4 internet protocol address. + pub v4: Ipv4Address, + + /// An IPv6 internet protocol address. + pub v6: Ipv6Address, +} + +impl IpAddress { + /// Construct a new IPv4 address. + #[must_use] + pub const fn new_v4(ip_addr: [u8; 4]) -> Self { + Self { + v4: Ipv4Address(ip_addr), + } + } + + /// Construct a new IPv6 address. + #[must_use] + pub const fn new_v6(ip_addr: [u8; 16]) -> Self { + Self { + v6: Ipv6Address(ip_addr), + } + } +} + +impl Debug for IpAddress { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + // The type is an untagged union, so we don't know whether it contains + // an IPv4 or IPv6 address. It's also not safe to just print the whole + // 16 bytes, since they might not all be initialized. + f.debug_struct("IpAddress").finish() + } +} + +impl Default for IpAddress { + fn default() -> Self { + Self { addr: [0u32; 4] } + } +} + /// A Media Access Control (MAC) address. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)]