Skip to content

Commit 728dc3b

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 87b00fd commit 728dc3b

File tree

8 files changed

+99
-190
lines changed

8 files changed

+99
-190
lines changed

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

+2-2
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

+2-2
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

+5
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

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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;
17+
use crate::proto::network::EfiIpAddr;
1818
use crate::{guid, Guid};
1919
use bitflags::bitflags;
2020
use core::mem::{size_of, size_of_val};
@@ -2412,7 +2412,7 @@ pub mod messaging {
24122412
pub struct Dns {
24132413
pub(super) header: DevicePathHeader,
24142414
pub(super) address_type: device_path::messaging::DnsAddressType,
2415-
pub(super) addresses: [IpAddress],
2415+
pub(super) addresses: [EfiIpAddr],
24162416
}
24172417

24182418
impl Dns {
@@ -2424,10 +2424,10 @@ pub mod messaging {
24242424

24252425
/// One or more instances of the DNS server address.
24262426
#[must_use]
2427-
pub fn addresses(&self) -> UnalignedSlice<IpAddress> {
2428-
let ptr: *const [IpAddress] = addr_of!(self.addresses);
2427+
pub fn addresses(&self) -> UnalignedSlice<EfiIpAddr> {
2428+
let ptr: *const [EfiIpAddr] = addr_of!(self.addresses);
24292429
let (ptr, len): (*const (), usize) = ptr_meta::to_raw_parts(ptr);
2430-
unsafe { UnalignedSlice::new(ptr.cast::<IpAddress>(), len) }
2430+
unsafe { UnalignedSlice::new(ptr.cast::<EfiIpAddr>(), len) }
24312431
}
24322432
}
24332433

@@ -2438,7 +2438,7 @@ pub mod messaging {
24382438
.field("addresses", {
24392439
let ptr = addr_of!(self.addresses);
24402440
let (ptr, len) = ptr_meta::to_raw_parts(ptr);
2441-
let byte_len = size_of::<IpAddress>() * len;
2441+
let byte_len = size_of::<EfiIpAddr>() * len;
24422442
unsafe { &slice::from_raw_parts(ptr.cast::<u8>(), byte_len) }
24432443
})
24442444
.finish()
@@ -2458,7 +2458,7 @@ pub mod messaging {
24582458
let dst_size = size_of_val(node)
24592459
.checked_sub(static_size)
24602460
.ok_or(NodeConversionError::InvalidLength)?;
2461-
let elem_size = size_of::<IpAddress>();
2461+
let elem_size = size_of::<EfiIpAddr>();
24622462
if dst_size % elem_size != 0 {
24632463
return Err(NodeConversionError::InvalidLength);
24642464
}
@@ -5380,7 +5380,7 @@ pub mod build {
53805380
/// Whether the addresses are IPv4 or IPv6.
53815381
pub address_type: device_path::messaging::DnsAddressType,
53825382
/// One or more instances of the DNS server address.
5383-
pub addresses: &'a [IpAddress],
5383+
pub addresses: &'a [EfiIpAddr],
53845384
}
53855385

53865386
unsafe impl BuildNode for Dns<'_> {

uefi/src/proto/network/ip4config2.rs

+4-15
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

+7-92
Original file line numberDiff line numberDiff line change
@@ -3,101 +3,16 @@
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` works 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;
18+
pub use uefi_raw::MacAddress as EfiMacAddr;

0 commit comments

Comments
 (0)