Skip to content

Commit a836560

Browse files
committed
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 cede821 commit a836560

File tree

9 files changed

+100
-186
lines changed

9 files changed

+100
-186
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,6 +1,6 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use uefi::proto::network::IpAddress;
3+
use uefi::proto::network::EfiIpAddr;
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 = EfiIpAddr::new_v4(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!";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ 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;
9+
use uefi::proto::network::EfiMacAddr;
1010
use uefi::proto::network::snp::{InterruptStatus, ReceiveFlags, SimpleNetwork};
1111
use uefi::{Status, boot};
1212
use uefi_raw::protocol::network::snp::NetworkState;
@@ -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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@
5858
defaults to the recommended value of `MemoryType::LOADER_DATA`.
5959
- **Breaking:** Removed duplication in `DevicePathHeader`. Instead of public fields,
6060
there is now a public constructor combined with public getters.
61+
- **Breaking:** All public APIs related to networking now use
62+
`core::net::{IpAddr, Ipv4Addr, Ipv6Addr}`, i.e., the types from the standard
63+
library.
64+
- **Breaking:** Removed type `IpAddress`. Instead, a new alias `EfiIpAddr` is
65+
exported which forwards to the new `IpAddress` type in `uefi-raw`. That type
66+
is tightly integrated with `core::net::{IpAddr, Ipv4Addr, Ipv6Addr}` via
67+
various `From/Into` implementations. This simplifies working with IP addresses
68+
significantly.
69+
- **Breaking:** For consistency, `MacAddress` was renamed to `EfiMacAddr`.
6170
- `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on,
6271
as this is considered a hard internal error where users can't do anything
6372
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: 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-
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.
17+
pub use uefi_raw::{IpAddress as EfiIpAddr, MacAddress as EfiMacAddr};

0 commit comments

Comments
 (0)