Skip to content

Commit c811868

Browse files
committed
uefi: use core::net-types in public API + remove duplications
1 parent f2f6abf commit c811868

File tree

9 files changed

+172
-196
lines changed

9 files changed

+172
-196
lines changed

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

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

3-
use uefi::proto::network::IpAddress;
3+
use core::net::{IpAddr, Ipv4Addr};
44
use uefi::proto::network::pxe::{BaseCode, DhcpV4Packet, IpFilter, IpFilters, UdpOpFlags};
55
use uefi::{CStr8, boot};
66

@@ -30,7 +30,7 @@ pub fn test() {
3030
info!("DHCP: Server IP: {:?}", dhcp_ack.bootp_si_addr);
3131
info!("DHCP: Client IP: {:?}", dhcp_ack.bootp_yi_addr);
3232

33-
let server_ip = IpAddress::new_v4(dhcp_ack.bootp_si_addr);
33+
let server_ip = IpAddr::V4(Ipv4Addr::from(dhcp_ack.bootp_si_addr));
3434

3535
const EXAMPLE_FILE_NAME: &[u8] = b"example-file.txt\0";
3636
const EXAMPLE_FILE_CONTENT: &[u8] = b"Hello world!";
@@ -88,7 +88,7 @@ pub fn test() {
8888
let mut read_result = Ok(0);
8989
for i in 0..5 {
9090
read_result = base_code.udp_read(
91-
UdpOpFlags::USE_FILTER,
91+
UdpOpFlags::ANY_SRC_PORT | UdpOpFlags::ANY_SRC_IP | UdpOpFlags::ANY_DEST_PORT | UdpOpFlags::ANY_DEST_IP,
9292
Some(&mut dest_ip),
9393
Some(&mut dest_port),
9494
Some(&mut src_ip),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use smoltcp::wire::{
66
ETHERNET_HEADER_LEN, EthernetFrame, IPV4_HEADER_LEN, Ipv4Packet, UDP_HEADER_LEN, UdpPacket,
77
};
88
use uefi::boot::ScopedProtocol;
9-
use uefi::proto::network::MacAddress;
109
use uefi::proto::network::snp::{InterruptStatus, ReceiveFlags, SimpleNetwork};
1110
use uefi::{Status, boot};
11+
use uefi_raw::MacAddress as EfiMacAddr;
1212
use uefi_raw::protocol::network::snp::NetworkState;
1313

1414
/// The MAC address configured for the interface.
@@ -53,8 +53,8 @@ fn receive(simple_network: &mut SimpleNetwork, buffer: &mut [u8]) -> uefi::Resul
5353
// Wait for a bit to ensure that the previous packet has been processed.
5454
boot::stall(Duration::from_millis(500));
5555

56-
let mut recv_src_mac = MacAddress([0; 32]);
57-
let mut recv_dst_mac = MacAddress([0; 32]);
56+
let mut recv_src_mac = EfiMacAddr([0; 32]);
57+
let mut recv_dst_mac = EfiMacAddr([0; 32]);
5858
let mut recv_ethernet_protocol = 0;
5959

6060
let res = simple_network.receive(

uefi/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
getting stabilized in stable Rust.
3131
- Removed `File::get_boxed_info_in`
3232
- Removed `Directory::read_entry_boxed_in`
33+
- All protocols behind `uefi::net` now only use `core::net` types in their
34+
public API. This especially affects the SNP and PXE protocols. This makes
35+
writing networking code much simpler.
3336

3437
# uefi - 0.35.0 (2025-05-04)
3538

@@ -63,6 +66,15 @@
6366
defaults to the recommended value of `MemoryType::LOADER_DATA`.
6467
- **Breaking:** Removed duplication in `DevicePathHeader`. Instead of public fields,
6568
there is now a public constructor combined with public getters.
69+
- **Breaking:** All public APIs related to networking now use
70+
`core::net::{IpAddr, Ipv4Addr, Ipv6Addr}`, i.e., the types from the standard
71+
library.
72+
- **Breaking:** Removed type `IpAddress`. Instead, a new alias `EfiIpAddr` is
73+
exported which forwards to the new `IpAddress` type in `uefi-raw`. That type
74+
is tightly integrated with `core::net::{IpAddr, Ipv4Addr, Ipv6Addr}` via
75+
various `From/Into` implementations. This simplifies working with IP addresses
76+
significantly.
77+
- **Breaking:** For consistency, `MacAddress` was renamed to `EfiMacAddr`.
6678
- `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on,
6779
as this is considered a hard internal error where users can't do anything
6880
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,12 +14,12 @@ 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::ptr::addr_of;
2120
use core::{fmt, slice};
2221
use ptr_meta::Pointee;
22+
use uefi_raw::IpAddress;
2323
/// Device path nodes for [`DeviceType::END`].
2424
pub mod end {
2525
use super::*;

uefi/src/proto/network/ip4config2.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
use alloc::vec;
88
use alloc::vec::Vec;
99
use core::ffi::c_void;
10+
use core::net::Ipv4Addr;
1011
use core::time::Duration;
1112

1213
use uefi::boot::ScopedProtocol;
1314
use uefi::prelude::*;
1415
use uefi::proto::unsafe_protocol;
1516
use uefi::{print, println};
16-
use uefi_raw::Ipv4Address;
1717
use uefi_raw::protocol::network::ip4_config2::{
1818
Ip4Config2DataType, Ip4Config2InterfaceInfo, Ip4Config2Policy, Ip4Config2Protocol,
1919
};
@@ -102,29 +102,19 @@ impl Ip4Config2 {
102102
})
103103
}
104104

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

123113
let info = self.get_interface_info()?;
124-
if info.station_addr != no_address {
114+
if info.station_addr != no_address.into() {
125115
if verbose {
126116
print!("Network is already up: ");
127-
Self::print_info(&info);
117+
println!("addr v4: {}", info.station_addr);
128118
}
129119
return Ok(());
130120
}
@@ -140,10 +130,10 @@ impl Ip4Config2 {
140130
}
141131
boot::stall(Duration::from_secs(1));
142132
let info = self.get_interface_info()?;
143-
if info.station_addr != no_address {
133+
if info.station_addr != no_address.into() {
144134
if verbose {
145135
print!(" OK: ");
146-
Self::print_info(&info);
136+
println!("addr v4: {}", info.station_addr);
147137
}
148138
return Ok(());
149139
}

uefi/src/proto/network/mod.rs

Lines changed: 12 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,20 @@
33
//! Network access protocols.
44
//!
55
//! These protocols can be used to interact with network resources.
6+
//!
7+
//! All high-level wrappers will accept [`core::net`] types:
8+
//! - [`IpAddr`]
9+
//! - [`Ipv4Addr`]
10+
//! - [`Ipv6Addr`]
11+
//!
12+
//! The only exception is [`uefi_raw::MacAddress`] which doesn't have a
13+
//! corresponding type in the standard library.
14+
//!
15+
//! [`IpAddr`]: core::net::IpAddr
16+
//! [`Ipv4Addr`]: core::net::Ipv4Addr
17+
//! [`Ipv6Addr`]: core::net::Ipv6Addr
618
719
pub mod http;
820
pub mod ip4config2;
921
pub mod pxe;
1022
pub mod snp;
11-
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-
const 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.

0 commit comments

Comments
 (0)