Skip to content

Commit 86aa5ce

Browse files
committedApr 21, 2025·
uefi: remove duplicated types, use uefi-raw's IpAddress
This does not use core::net::IpAddr in all possible public interfaces. Although there is opportunity for that, this commit only makes `uefi` compatible with the new `uefi-raw` type.
1 parent 5ff3165 commit 86aa5ce

File tree

9 files changed

+92
-184
lines changed

9 files changed

+92
-184
lines changed
 

‎uefi-test-runner/src/proto/network/pxe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

33
use uefi::proto::network::pxe::{BaseCode, DhcpV4Packet, IpFilter, IpFilters, UdpOpFlags};
4-
use uefi::proto::network::IpAddress;
4+
use uefi::proto::network::EfiIpAddr;
55
use uefi::{boot, CStr8};
66

77
pub fn test() {
@@ -27,7 +27,7 @@ pub fn test() {
2727
assert!(base_code.mode().dhcp_ack_received());
2828
let dhcp_ack: &DhcpV4Packet = base_code.mode().dhcp_ack().as_ref();
2929
let server_ip = dhcp_ack.bootp_si_addr;
30-
let server_ip = IpAddress::new_v4(server_ip);
30+
let server_ip = EfiIpAddr::new_v4(server_ip);
3131

3232
const EXAMPLE_FILE_NAME: &[u8] = b"example-file.txt\0";
3333
const EXAMPLE_FILE_CONTENT: &[u8] = b"Hello world!";

‎uefi-test-runner/src/proto/network/snp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

33
use uefi::proto::network::snp::{InterruptStatus, ReceiveFlags, SimpleNetwork};
4-
use uefi::proto::network::MacAddress;
4+
use uefi::proto::network::EfiMacAddr;
55
use uefi::{boot, Status};
66

77
pub fn test() {
@@ -75,7 +75,7 @@ pub fn test() {
7575
\xa9\xe4\
7676
\x04\x01\x02\x03\x04";
7777

78-
let dest_addr = MacAddress([0xffu8; 32]);
78+
let dest_addr = EfiMacAddr([0xffu8; 32]);
7979
assert!(!simple_network
8080
.get_interrupt_status()
8181
.unwrap()

‎uefi/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
defaults to the recommended value of `MemoryType::LOADER_DATA`.
3030
- **Breaking:** Removed duplication in `DevicePathHeader`. Instead of public fields,
3131
there is now a public constructor combined with public getters.
32+
- **Breaking:** Removed type `IpAddress`. Instead, a new alias `EfiIpAddr` is
33+
exported which forwards to the new `IpAddress` type in `uefi-raw`. That type
34+
is tightly integrated with `core::net::{IpAddr, Ipv4Addr, Ipv6Addr}`.
35+
This simplifies working with IP addresses significantly.
36+
- **Breaking:** For consistency, `MacAddress` was renamed to `EfiMacAddr`.
3237
- `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on,
3338
as this is considered a hard internal error where users can't do anything
3439
about it anyway. It will panic instead.

‎uefi/src/proto/device_path/device_path_gen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ use crate::polyfill::maybe_uninit_slice_as_mut_ptr;
1414
use crate::proto::device_path::{
1515
self, DevicePathHeader, DevicePathNode, DeviceSubType, DeviceType, NodeConversionError,
1616
};
17-
use crate::proto::network::IpAddress;
1817
use crate::{guid, Guid};
1918
use bitflags::bitflags;
2019
use core::mem::{size_of, size_of_val};
2120
use core::ptr::addr_of;
2221
use core::{fmt, slice};
2322
use ptr_meta::Pointee;
23+
use uefi_raw::IpAddress;
2424
/// Device path nodes for [`DeviceType::END`].
2525
pub mod end {
2626
use super::*;

‎uefi/src/proto/network/ip4config2.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
use alloc::vec;
88
use alloc::vec::Vec;
99
use core::ffi::c_void;
10-
10+
use core::net::Ipv4Addr;
1111
use uefi::boot::ScopedProtocol;
1212
use uefi::prelude::*;
1313
use uefi::proto::unsafe_protocol;
1414
use uefi::{print, println};
1515
use uefi_raw::protocol::network::ip4_config2::{
1616
Ip4Config2DataType, Ip4Config2InterfaceInfo, Ip4Config2Policy, Ip4Config2Protocol,
1717
};
18-
use uefi_raw::Ipv4Address;
1918

2019
/// IP4 Config2 [`Protocol`]. Configure IPv4 networking.
2120
///
@@ -101,29 +100,19 @@ impl Ip4Config2 {
101100
})
102101
}
103102

104-
fn print_info(info: &Ip4Config2InterfaceInfo) {
105-
println!(
106-
"addr v4: {}.{}.{}.{}",
107-
info.station_addr.0[0],
108-
info.station_addr.0[1],
109-
info.station_addr.0[2],
110-
info.station_addr.0[3],
111-
);
112-
}
113-
114103
/// Bring up network interface. Does nothing in case the network
115104
/// is already set up. Otherwise turns on DHCP and waits until an
116105
/// IPv4 address has been assigned. Reports progress on the
117106
/// console if verbose is set to true. Returns TIMEOUT error in
118107
/// case DHCP configuration does not finish within 30 seconds.
119108
pub fn ifup(&mut self, verbose: bool) -> uefi::Result<()> {
120-
let no_address = Ipv4Address::default();
109+
let no_address = Ipv4Addr::from_bits(0);
121110

122111
let info = self.get_interface_info()?;
123112
if info.station_addr != no_address {
124113
if verbose {
125114
print!("Network is already up: ");
126-
Self::print_info(&info);
115+
println!("addr v4: {}", info.station_addr);
127116
}
128117
return Ok(());
129118
}
@@ -142,7 +131,7 @@ impl Ip4Config2 {
142131
if info.station_addr != no_address {
143132
if verbose {
144133
print!(" OK: ");
145-
Self::print_info(&info);
134+
println!("addr v4: {}", info.station_addr);
146135
}
147136
return Ok(());
148137
}

‎uefi/src/proto/network/mod.rs

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,15 @@
33
//! Network access protocols.
44
//!
55
//! These protocols can be used to interact with network resources.
6+
//!
7+
//! To work with Mac and IP addresses, `uefi` uses with the types:
8+
//! - [`EfiIpAddr`] that is tightly integrated with the [`core::net::IpAddr`]
9+
//! type,
10+
//! - [`EfiMacAddr`]
611
712
pub mod http;
813
pub mod ip4config2;
914
pub mod pxe;
1015
pub mod snp;
1116

12-
pub use uefi_raw::MacAddress;
13-
14-
/// Represents an IPv4/v6 address.
15-
///
16-
/// Corresponds to the `EFI_IP_ADDRESS` type in the C API.
17-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18-
#[repr(C, align(4))]
19-
pub struct IpAddress(pub [u8; 16]);
20-
21-
impl IpAddress {
22-
/// Construct a new IPv4 address.
23-
#[must_use]
24-
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
25-
let mut buffer = [0; 16];
26-
buffer[0] = ip_addr[0];
27-
buffer[1] = ip_addr[1];
28-
buffer[2] = ip_addr[2];
29-
buffer[3] = ip_addr[3];
30-
Self(buffer)
31-
}
32-
33-
/// Construct a new IPv6 address.
34-
#[must_use]
35-
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
36-
Self(ip_addr)
37-
}
38-
39-
/// Construct from a `uefi_raw::IpAddress` union.
40-
///
41-
/// # Safety
42-
///
43-
/// `is_ipv6` must accurately reflect how the union was initialized.
44-
#[must_use]
45-
const unsafe fn from_raw(ip_addr: uefi_raw::IpAddress, is_ipv6: bool) -> Self {
46-
if is_ipv6 {
47-
Self::new_v6(unsafe { ip_addr.v6.0 })
48-
} else {
49-
Self::new_v4(unsafe { ip_addr.v4.0 })
50-
}
51-
}
52-
53-
#[must_use]
54-
const fn as_raw_ptr(&self) -> *const uefi_raw::IpAddress {
55-
// The uefi-raw type is defined differently, but the layout is
56-
// compatible.
57-
self.0.as_ptr().cast()
58-
}
59-
60-
#[must_use]
61-
fn as_raw_ptr_mut(&mut self) -> *mut uefi_raw::IpAddress {
62-
// The uefi-raw type is defined differently, but the layout is
63-
// compatible.
64-
self.0.as_mut_ptr().cast()
65-
}
66-
}
67-
68-
impl From<core::net::Ipv4Addr> for IpAddress {
69-
fn from(t: core::net::Ipv4Addr) -> Self {
70-
Self::new_v4(t.octets())
71-
}
72-
}
73-
74-
impl From<IpAddress> for core::net::Ipv4Addr {
75-
fn from(IpAddress(o): IpAddress) -> Self {
76-
Self::from([o[0], o[1], o[2], o[3]])
77-
}
78-
}
79-
80-
impl From<core::net::Ipv6Addr> for IpAddress {
81-
fn from(t: core::net::Ipv6Addr) -> Self {
82-
Self::new_v6(t.octets())
83-
}
84-
}
85-
86-
impl From<IpAddress> for core::net::Ipv6Addr {
87-
fn from(value: IpAddress) -> Self {
88-
Self::from(value.0)
89-
}
90-
}
91-
92-
impl From<core::net::IpAddr> for IpAddress {
93-
fn from(t: core::net::IpAddr) -> Self {
94-
match t {
95-
core::net::IpAddr::V4(a) => a.into(),
96-
core::net::IpAddr::V6(a) => a.into(),
97-
}
98-
}
99-
}
100-
101-
// NOTE: We cannot impl From<IpAddress> for core::net::IpAddr
102-
// because IpAddress is a raw union, with nothing indicating
103-
// whether it should be considered v4 or v6.
17+
pub use uefi_raw::{IpAddress as EfiIpAddr, MacAddress as EfiMacAddr};

‎uefi/src/proto/network/pxe.rs

Lines changed: 60 additions & 61 deletions
Large diffs are not rendered by default.

‎uefi/src/proto/network/snp.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
//! No interface function must be called until `SimpleNetwork.start` is successfully
1010
//! called first.
1111
12-
use super::{IpAddress, MacAddress};
12+
use super::{EfiIpAddr, EfiMacAddr};
1313
use crate::data_types::Event;
1414
use crate::proto::unsafe_protocol;
1515
use crate::{Result, StatusExt};
1616
use core::ffi::c_void;
17+
use core::net::IpAddr;
1718
use core::ptr;
1819
use core::ptr::NonNull;
1920
use uefi_raw::protocol::network::snp::SimpleNetworkProtocol;
@@ -66,7 +67,7 @@ impl SimpleNetwork {
6667
enable: ReceiveFlags,
6768
disable: ReceiveFlags,
6869
reset_mcast_filter: bool,
69-
mcast_filter: Option<&[MacAddress]>,
70+
mcast_filter: Option<&[EfiMacAddr]>,
7071
) -> Result {
7172
let filter_count = mcast_filter.map(|filters| filters.len()).unwrap_or(0);
7273
let filters = mcast_filter
@@ -87,7 +88,7 @@ impl SimpleNetwork {
8788
}
8889

8990
/// Modify or reset the current station address, if supported.
90-
pub fn station_address(&self, reset: bool, new: Option<&MacAddress>) -> Result {
91+
pub fn station_address(&self, reset: bool, new: Option<&EfiMacAddr>) -> Result {
9192
unsafe {
9293
(self.0.station_address)(
9394
&self.0,
@@ -127,13 +128,13 @@ impl SimpleNetwork {
127128
}
128129

129130
/// Convert a multicast IP address to a multicast HW MAC Address.
130-
pub fn mcast_ip_to_mac(&self, ipv6: bool, ip: IpAddress) -> Result<MacAddress> {
131-
let mut mac_address = MacAddress([0; 32]);
131+
pub fn mcast_ip_to_mac(&self, ipv6: bool, ip: IpAddr) -> Result<EfiMacAddr> {
132+
let mut mac_address = EfiMacAddr([0; 32]);
132133
let status = unsafe {
133134
(self.0.multicast_ip_to_mac)(
134135
&self.0,
135136
Boolean::from(ipv6),
136-
ip.as_raw_ptr(),
137+
EfiIpAddr::from(ip).as_ptr(),
137138
&mut mac_address,
138139
)
139140
};
@@ -191,8 +192,8 @@ impl SimpleNetwork {
191192
&self,
192193
header_size: usize,
193194
buffer: &[u8],
194-
src_addr: Option<MacAddress>,
195-
dest_addr: Option<MacAddress>,
195+
src_addr: Option<EfiMacAddr>,
196+
dest_addr: Option<EfiMacAddr>,
196197
protocol: Option<u16>,
197198
) -> Result {
198199
unsafe {
@@ -216,8 +217,8 @@ impl SimpleNetwork {
216217
&self,
217218
buffer: &mut [u8],
218219
header_size: Option<&mut usize>,
219-
src_addr: Option<&mut MacAddress>,
220-
dest_addr: Option<&mut MacAddress>,
220+
src_addr: Option<&mut EfiMacAddr>,
221+
dest_addr: Option<&mut EfiMacAddr>,
221222
protocol: Option<&mut u16>,
222223
) -> Result<usize> {
223224
let mut buffer_size = buffer.len();

‎xtask/src/device_path/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ fn gen_uefi_code_as_string(groups: &[NodeGroup]) -> Result<String> {
7979
self, DevicePathHeader, DevicePathNode, DeviceSubType, DeviceType,
8080
NodeConversionError,
8181
};
82-
use crate::proto::network::IpAddress;
8382
use crate::mem::memory_map::MemoryType;
8483
use core::mem::{size_of, size_of_val};
8584
use core::ptr::addr_of;
8685
use core::{fmt, slice};
8786
use ptr_meta::Pointee;
87+
use uefi_raw::IpAddress;
8888

8989
#(#packed_modules)*
9090

0 commit comments

Comments
 (0)
Please sign in to comment.