From e79c21c2e251a885eb938c988b85a826dad091ed Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 21 Mar 2025 18:08:26 -0400 Subject: [PATCH 1/2] uefi-raw: Increase MSRV to 1.77 This will allow the core::net types to be used. --- uefi-raw/CHANGELOG.md | 1 + uefi-raw/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index bc3f286a1..750f87ab3 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -1,6 +1,7 @@ # uefi-raw - [Unreleased] ## Added +- MSRV increased to 1.77. - Added `Boolean` type - Added `protocol::network::pxe` module. - Added conversions between `MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses. diff --git a/uefi-raw/Cargo.toml b/uefi-raw/Cargo.toml index e872eaad9..6bb736118 100644 --- a/uefi-raw/Cargo.toml +++ b/uefi-raw/Cargo.toml @@ -16,7 +16,7 @@ license.workspace = true repository.workspace = true # uefi-raw is much less likely to need the latest bleeding-edge features. # Hence, it is okay to not use the workspace MSRV. -rust-version = "1.70" +rust-version = "1.77" [dependencies] bitflags.workspace = true From dd6750f4d3f5eb88899ef6efb07260cbc096c6dd Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Fri, 21 Mar 2025 16:06:46 -0400 Subject: [PATCH 2/2] uefi-raw: Add conversions to/from core::net IP address types --- uefi-raw/CHANGELOG.md | 2 ++ uefi-raw/src/lib.rs | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 750f87ab3..590735292 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -5,6 +5,8 @@ - Added `Boolean` type - Added `protocol::network::pxe` module. - Added conversions between `MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses. +- Implemented `From` conversions between the `core::net` and `uefi_raw` IP + address types. - Added `DiskInfoProtocol`. - Added `ExtScsiPassThruProtocol`. diff --git a/uefi-raw/src/lib.rs b/uefi-raw/src/lib.rs index e092ef410..7f8050b56 100644 --- a/uefi-raw/src/lib.rs +++ b/uefi-raw/src/lib.rs @@ -111,11 +111,35 @@ impl From for bool { #[repr(transparent)] pub struct Ipv4Address(pub [u8; 4]); +impl From for Ipv4Address { + fn from(ip: core::net::Ipv4Addr) -> Self { + Self(ip.octets()) + } +} + +impl From for core::net::Ipv4Addr { + fn from(ip: Ipv4Address) -> Self { + Self::from(ip.0) + } +} + /// An IPv6 internet protocol address. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)] pub struct Ipv6Address(pub [u8; 16]); +impl From for Ipv6Address { + fn from(ip: core::net::Ipv6Addr) -> Self { + Self(ip.octets()) + } +} + +impl From for core::net::Ipv6Addr { + fn from(ip: Ipv6Address) -> Self { + Self::from(ip.0) + } +} + /// An IPv4 or IPv6 internet protocol address. /// /// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This @@ -170,6 +194,19 @@ impl Default for IpAddress { } } +impl From for IpAddress { + fn from(t: core::net::IpAddr) -> Self { + match t { + core::net::IpAddr::V4(ip) => Self { + v4: Ipv4Address::from(ip), + }, + core::net::IpAddr::V6(ip) => Self { + v6: Ipv6Address::from(ip), + }, + } + } +} + /// A Media Access Control (MAC) address. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] #[repr(transparent)] @@ -198,6 +235,11 @@ impl From for [u8; 6] { mod tests { use super::*; + const TEST_IPV4: [u8; 4] = [91, 92, 93, 94]; + const TEST_IPV6: [u8; 16] = [ + 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + ]; + #[test] /// Test the properties promised in [0]. This also applies for the other /// architectures. @@ -215,4 +257,34 @@ mod tests { assert!(bool::from(Boolean(0b11111110))); assert!(bool::from(Boolean(0b11111111))); } + + /// Test round-trip conversion between `Ipv4Address` and `core::net::Ipv4Addr`. + #[test] + fn test_ip_addr4_conversion() { + let uefi_addr = Ipv4Address(TEST_IPV4); + let core_addr = core::net::Ipv4Addr::from(uefi_addr); + assert_eq!(uefi_addr, Ipv4Address::from(core_addr)); + } + + /// Test round-trip conversion between `Ipv6Address` and `core::net::Ipv6Addr`. + #[test] + fn test_ip_addr6_conversion() { + let uefi_addr = Ipv6Address(TEST_IPV6); + let core_addr = core::net::Ipv6Addr::from(uefi_addr); + assert_eq!(uefi_addr, Ipv6Address::from(core_addr)); + } + + /// Test conversion from `core::net::IpAddr` to `IpvAddress`. + /// + /// Note that conversion in the other direction is not possible. + #[test] + fn test_ip_addr_conversion() { + let core_addr = core::net::IpAddr::V4(core::net::Ipv4Addr::from(TEST_IPV4)); + let uefi_addr = IpAddress::from(core_addr); + assert_eq!(unsafe { uefi_addr.v4.0 }, TEST_IPV4); + + let core_addr = core::net::IpAddr::V6(core::net::Ipv6Addr::from(TEST_IPV6)); + let uefi_addr = IpAddress::from(core_addr); + assert_eq!(unsafe { uefi_addr.v6.0 }, TEST_IPV6); + } }