diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index a71d409e0..d15903ccf 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -3,6 +3,7 @@ ## Added - 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. # uefi-raw - 0.10.0 (2025-02-07) diff --git a/uefi-raw/src/lib.rs b/uefi-raw/src/lib.rs index e9ae22e8f..e092ef410 100644 --- a/uefi-raw/src/lib.rs +++ b/uefi-raw/src/lib.rs @@ -175,6 +175,25 @@ impl Default for IpAddress { #[repr(transparent)] pub struct MacAddress(pub [u8; 32]); +impl From<[u8; 6]> for MacAddress { + fn from(octets: [u8; 6]) -> Self { + let mut buffer = [0; 32]; + buffer[0] = octets[0]; + buffer[1] = octets[1]; + buffer[2] = octets[2]; + buffer[3] = octets[3]; + buffer[4] = octets[4]; + buffer[5] = octets[5]; + Self(buffer) + } +} + +impl From for [u8; 6] { + fn from(MacAddress(o): MacAddress) -> Self { + [o[0], o[1], o[2], o[3], o[4], o[5]] + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/uefi/CHANGELOG.md b/uefi/CHANGELOG.md index 99e032ff1..fde676be6 100644 --- a/uefi/CHANGELOG.md +++ b/uefi/CHANGELOG.md @@ -2,6 +2,8 @@ ## Added - Added `boot::signal_event`. +- Added conversions between `proto::network::IpAddress` and `core::net` types. +- Added conversions between `proto::network::MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses. ## Changed - **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no diff --git a/uefi/src/proto/network/mod.rs b/uefi/src/proto/network/mod.rs index 7b8e9544c..ffce22303 100644 --- a/uefi/src/proto/network/mod.rs +++ b/uefi/src/proto/network/mod.rs @@ -34,3 +34,40 @@ impl IpAddress { Self(ip_addr) } } + +impl From for IpAddress { + fn from(t: core::net::Ipv4Addr) -> Self { + Self::new_v4(t.octets()) + } +} + +impl From for core::net::Ipv4Addr { + fn from(IpAddress(o): IpAddress) -> Self { + Self::from([o[0], o[1], o[2], o[3]]) + } +} + +impl From for IpAddress { + fn from(t: core::net::Ipv6Addr) -> Self { + Self::new_v6(t.octets()) + } +} + +impl From for core::net::Ipv6Addr { + fn from(value: IpAddress) -> Self { + Self::from(value.0) + } +} + +impl From for IpAddress { + fn from(t: core::net::IpAddr) -> Self { + match t { + core::net::IpAddr::V4(a) => a.into(), + core::net::IpAddr::V6(a) => a.into(), + } + } +} + +// NOTE: We cannot impl From for core::net::IpAddr +// because IpAddress is a raw union, with nothing indicating +// whether it should be considered v4 or v6.