uefi-raw 0.14.0

Raw UEFI types and bindings for protocols, boot, and runtime services. This can serve as base for an UEFI firmware implementation or a high-level wrapper to access UEFI functionality from an UEFI image.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// SPDX-License-Identifier: MIT OR Apache-2.0

//! UEFI network types.
//!
//! The main exports of this module are:
//! - [`MacAddress`]
//! - [`IpAddress`]
//! - [`Ipv4Address`]
//! - [`Ipv6Address`]

use core::fmt::{self, Debug, Display, Formatter};

/// An IPv4 internet protocol address.
///
/// # Conversions and Relation to [`core::net`]
///
/// The following [`From`] implementations exist:
///   - `[u8; 4]` -> [`Ipv4Address`]
///   - [`core::net::Ipv4Addr`] -> [`Ipv4Address`]
///   - [`core::net::IpAddr`] -> [`Ipv4Address`]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Ipv4Address(pub [u8; 4]);

impl Ipv4Address {
    /// Returns the octets of the IP address.
    #[must_use]
    pub const fn octets(self) -> [u8; 4] {
        self.0
    }
}

impl From<core::net::Ipv4Addr> for Ipv4Address {
    fn from(ip: core::net::Ipv4Addr) -> Self {
        Self(ip.octets())
    }
}

impl From<Ipv4Address> for core::net::Ipv4Addr {
    fn from(ip: Ipv4Address) -> Self {
        Self::from(ip.0)
    }
}

impl From<[u8; 4]> for Ipv4Address {
    fn from(octets: [u8; 4]) -> Self {
        Self(octets)
    }
}

impl Display for Ipv4Address {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let ip = core::net::Ipv4Addr::from(*self);
        write!(f, "{}", ip)
    }
}

/// An IPv6 internet protocol address.
///
/// # Conversions and Relation to [`core::net`]
///
/// The following [`From`] implementations exist:
///   - `[u8; 16]` -> [`Ipv6Address`]
///   - [`core::net::Ipv6Addr`] -> [`Ipv6Address`]
///   - [`core::net::IpAddr`] -> [`Ipv6Address`]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Ipv6Address(pub [u8; 16]);

impl Ipv6Address {
    /// Returns the octets of the IP address.
    #[must_use]
    pub const fn octets(self) -> [u8; 16] {
        self.0
    }
}

impl From<core::net::Ipv6Addr> for Ipv6Address {
    fn from(ip: core::net::Ipv6Addr) -> Self {
        Self(ip.octets())
    }
}

impl From<Ipv6Address> for core::net::Ipv6Addr {
    fn from(ip: Ipv6Address) -> Self {
        Self::from(ip.0)
    }
}

impl From<[u8; 16]> for Ipv6Address {
    fn from(octets: [u8; 16]) -> Self {
        Self(octets)
    }
}

impl Display for Ipv6Address {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let ip = core::net::Ipv6Addr::from(*self);
        write!(f, "{}", ip)
    }
}

/// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI.
///
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
/// type is defined in the same way as edk2 for compatibility with C code. Note
/// that this is an untagged union, so there's no way to tell which type of
/// address an `IpAddress` value contains without additional context.
///
/// # Conversions and Relation to [`core::net`]
///
/// The following [`From`] implementations exist:
///   - `[u8; 4]` -> [`IpAddress`]
///   - `[u8; 16]` -> [`IpAddress`]
///   - [`core::net::Ipv4Addr`] -> [`IpAddress`]
///   - [`core::net::Ipv6Addr`] -> [`IpAddress`]
///   - [`core::net::IpAddr`] -> [`IpAddress`]
#[derive(Clone, Copy)]
#[repr(C)]
pub union IpAddress {
    /// This member serves to align the whole type to a 4 bytes as required by
    /// the spec. Note that this is slightly different from `repr(align(4))`,
    /// which would prevent placing this type in a packed structure.
    pub addr: [u32; 4],

    /// An IPv4 internet protocol address.
    pub v4: Ipv4Address,

    /// An IPv6 internet protocol address.
    pub v6: Ipv6Address,
}

impl IpAddress {
    /// Zeroed variant where all bytes are guaranteed to be initialized to zero.
    pub const ZERO: Self = Self { addr: [0; 4] };

    /// Construct a new IPv4 address.
    ///
    /// The type won't know that it is an IPv6 address and additional context
    /// is needed.
    ///
    /// # Safety
    /// The constructor only initializes the bytes needed for IPv4 addresses.
    #[must_use]
    pub const fn new_v4(octets: [u8; 4]) -> Self {
        Self {
            v4: Ipv4Address(octets),
        }
    }

    /// Construct a new IPv6 address.
    ///
    /// The type won't know that it is an IPv6 address and additional context
    /// is needed.
    #[must_use]
    pub const fn new_v6(octets: [u8; 16]) -> Self {
        Self {
            v6: Ipv6Address(octets),
        }
    }

    /// Transforms this EFI type to the Rust standard library's type
    /// [`core::net::IpAddr`].
    ///
    /// # Arguments
    /// - `is_ipv6`: Whether the internal data should be interpreted as IPv6 or
    ///   IPv4 address.
    ///
    /// # Safety
    /// Callers must ensure that the `v4` field is valid if `is_ipv6` is false,
    /// and that the `v6` field is valid if `is_ipv6` is true
    #[must_use]
    pub unsafe fn into_core_addr(self, is_ipv6: bool) -> core::net::IpAddr {
        if is_ipv6 {
            // SAFETY: Caller assumes that the underlying data is initialized.
            core::net::IpAddr::V6(core::net::Ipv6Addr::from(unsafe { self.v6.octets() }))
        } else {
            // SAFETY: Caller assumes that the underlying data is initialized.
            core::net::IpAddr::V4(core::net::Ipv4Addr::from(unsafe { self.v4.octets() }))
        }
    }
}

impl Debug for IpAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // The type is an untagged union, so we don't know whether it contains
        // an IPv4 or IPv6 address. It's also not safe to just print the whole
        // 16 bytes, since they might not all be initialized.
        f.debug_struct("IpAddress").finish()
    }
}

impl Default for IpAddress {
    fn default() -> Self {
        Self::ZERO
    }
}

impl From<core::net::IpAddr> for IpAddress {
    fn from(t: core::net::IpAddr) -> Self {
        match t {
            core::net::IpAddr::V4(ip) => Self::new_v4(ip.octets()),
            core::net::IpAddr::V6(ip) => Self::new_v6(ip.octets()),
        }
    }
}

impl From<core::net::Ipv4Addr> for IpAddress {
    fn from(value: core::net::Ipv4Addr) -> Self {
        Self::new_v4(value.octets())
    }
}

impl From<core::net::Ipv6Addr> for IpAddress {
    fn from(value: core::net::Ipv6Addr) -> Self {
        Self::new_v6(value.octets())
    }
}

impl From<[u8; 4]> for IpAddress {
    fn from(octets: [u8; 4]) -> Self {
        Self::new_v4(octets)
    }
}

impl From<[u8; 16]> for IpAddress {
    fn from(octets: [u8; 16]) -> Self {
        Self::new_v6(octets)
    }
}

/// UEFI Media Access Control (MAC) address.
///
/// UEFI supports multiple network protocols and hardware types, not just
/// Ethernet. Some of them may use MAC addresses longer than 6 bytes. To be
/// protocol-agnostic and future-proof, the UEFI spec chooses a maximum size
/// that can hold any supported media access control address.
///
/// In most cases, this is just a typical `[u8; 6]` Ethernet style MAC
/// address with the rest of the bytes being zero.
///
/// # Conversions and Relation to [`core::net`]
///
/// There is no matching type in [`core::net`] but the following [`From`]
/// implementations exist:
///   - `[u8; 6]` <-> [`MacAddress`]
///   - `[u8; 32]` -> [`MacAddress`]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct MacAddress(pub [u8; 32]);

impl MacAddress {
    /// Returns the octets of the MAC address.
    #[must_use]
    pub const fn octets(self) -> [u8; 32] {
        self.0
    }

    /// Interpret the MAC address as normal 6-byte MAC address, as used in
    /// Ethernet.
    #[must_use]
    pub fn into_ethernet_addr(self) -> [u8; 6] {
        let mut buffer = [0; 6];
        buffer.copy_from_slice(&self.octets()[6..]);
        buffer
    }
}

// Normal Ethernet MAC address.
impl From<[u8; 6]> for MacAddress {
    fn from(octets: [u8; 6]) -> Self {
        let mut buffer = [0; 32];
        buffer[..6].copy_from_slice(&octets);
        Self(buffer)
    }
}

// Normal Ethernet MAC address.
impl From<MacAddress> for [u8; 6] {
    fn from(MacAddress(o): MacAddress) -> Self {
        [o[0], o[1], o[2], o[3], o[4], o[5]]
    }
}

// UEFI MAC addresses.
impl From<[u8; 32]> for MacAddress {
    fn from(octets: [u8; 32]) -> Self {
        Self(octets)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const TEST_IPV4: [u8; 4] = [91, 92, 93, 94];
    const TEST_IPV6: [u8; 16] = [
        101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
    ];

    /// Test round-trip conversion between `Ipv4Address` and `core::net::Ipv4Addr`.
    #[test]
    fn test_ip_addr4_conversion() {
        let uefi_addr = Ipv4Address(TEST_IPV4);
        let core_addr = core::net::Ipv4Addr::from(uefi_addr);
        assert_eq!(uefi_addr, Ipv4Address::from(core_addr));
    }

    /// Test round-trip conversion between `Ipv6Address` and `core::net::Ipv6Addr`.
    #[test]
    fn test_ip_addr6_conversion() {
        let uefi_addr = Ipv6Address(TEST_IPV6);
        let core_addr = core::net::Ipv6Addr::from(uefi_addr);
        assert_eq!(uefi_addr, Ipv6Address::from(core_addr));
    }

    /// Test conversion from `core::net::IpAddr` to `IpvAddress`.
    ///
    /// Note that conversion in the other direction is not possible.
    #[test]
    fn test_ip_addr_conversion() {
        let core_addr = core::net::IpAddr::V4(core::net::Ipv4Addr::from(TEST_IPV4));
        let uefi_addr = IpAddress::from(core_addr);
        assert_eq!(unsafe { uefi_addr.v4.0 }, TEST_IPV4);

        let core_addr = core::net::IpAddr::V6(core::net::Ipv6Addr::from(TEST_IPV6));
        let uefi_addr = IpAddress::from(core_addr);
        assert_eq!(unsafe { uefi_addr.v6.0 }, TEST_IPV6);
    }

    // Ensure that our IpAddress type can be put into a packed struct,
    // even when it is normally 4 byte aligned.
    #[test]
    fn test_efi_ip_address_abi() {
        #[repr(C, packed)]
        struct PackedHelper<T>(T);

        assert_eq!(align_of::<IpAddress>(), 4);
        assert_eq!(size_of::<IpAddress>(), 16);

        assert_eq!(align_of::<PackedHelper<IpAddress>>(), 1);
        assert_eq!(size_of::<PackedHelper<IpAddress>>(), 16);
    }

    /// Tests the From-impls from the documentation.
    #[test]
    fn test_promised_from_impls() {
        // octets -> Ipv4Address
        {
            let octets = [0_u8, 1, 2, 3];
            assert_eq!(Ipv4Address::from(octets), Ipv4Address(octets));
            let uefi_addr = IpAddress::from(octets);
            assert_eq!(&octets, &unsafe { uefi_addr.v4.octets() });
        }
        // octets -> Ipv6Address
        {
            let octets = [0_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
            assert_eq!(Ipv6Address::from(octets), Ipv6Address(octets));
            let uefi_addr = IpAddress::from(octets);
            assert_eq!(&octets, &unsafe { uefi_addr.v6.octets() });
        }
        // StdIpv4Addr -> Ipv4Address
        {
            let octets = [7, 5, 3, 1];
            let core_ipv4_addr = core::net::Ipv4Addr::from(octets);
            assert_eq!(Ipv4Address::from(core_ipv4_addr).octets(), octets);
            assert_eq!(
                unsafe { IpAddress::from(core_ipv4_addr).v4.octets() },
                octets
            );
        }
        // StdIpv6Addr -> Ipv6Address
        {
            let octets = [7, 5, 3, 1, 6, 3, 8, 5, 2, 5, 2, 7, 3, 5, 2, 6];
            let core_ipv6_addr = core::net::Ipv6Addr::from(octets);
            assert_eq!(Ipv6Address::from(core_ipv6_addr).octets(), octets);
            assert_eq!(
                unsafe { IpAddress::from(core_ipv6_addr).v6.octets() },
                octets
            );
        }
        // StdIpAddr -> IpAddress
        {
            let octets = [8, 8, 2, 6];
            let core_ip_addr = core::net::IpAddr::from(octets);
            assert_eq!(unsafe { IpAddress::from(core_ip_addr).v4.octets() }, octets);
        }
        // octets <-> MacAddress
        {
            let octets = [8, 8, 2, 6, 6, 7];
            let uefi_mac_addr = MacAddress::from(octets);
            assert_eq!(uefi_mac_addr.octets()[0..6], octets);
            let octets2: [u8; 6] = uefi_mac_addr.into();
            assert_eq!(octets2, octets)
        }
        // octets -> MacAddress
        {
            let octets = [
                8_u8, 8, 2, 6, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 0, 0, 0,
                0, 0, 0, 0, 42,
            ];
            let uefi_mac_addr = MacAddress::from(octets);
            assert_eq!(uefi_mac_addr.octets(), octets);
        }
    }

    /// Tests the intended usage of net types in high-level APIs and how they
    /// map to lower level UEFI types.
    ///
    /// TL;DR: High-level interfaces use core::net types whereas lower-level
    /// interfaces use types from this module.
    #[test]
    fn test_uefi_flow() {
        fn efi_retrieve_efi_ip_addr(addr: *mut IpAddress, is_ipv6: bool) {
            let addr = unsafe { addr.as_mut().unwrap() };
            // SAFETY: Alignment is guaranteed and memory is initialized.
            unsafe {
                addr.v4.0[0] = 42;
                addr.v4.0[1] = 42;
                addr.v4.0[2] = 42;
                addr.v4.0[3] = 42;
            }
            if is_ipv6 {
                unsafe {
                    addr.v6.0[14] = 42;
                    addr.v6.0[15] = 42;
                }
            }
        }

        fn high_level_retrieve_ip(is_ipv6: bool) -> core::net::IpAddr {
            let mut efi_ip_addr = IpAddress::ZERO;
            efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6);
            unsafe { efi_ip_addr.into_core_addr(is_ipv6) }
        }

        let ipv4_addr = high_level_retrieve_ip(false);
        let ipv4_addr: core::net::Ipv4Addr = match ipv4_addr {
            core::net::IpAddr::V4(ipv4_addr) => ipv4_addr,
            core::net::IpAddr::V6(_) => panic!("should not happen"),
        };
        assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]);

        let ipv6_addr = high_level_retrieve_ip(true);
        let ipv6_addr: core::net::Ipv6Addr = match ipv6_addr {
            core::net::IpAddr::V6(ipv6_addr) => ipv6_addr,
            core::net::IpAddr::V4(_) => panic!("should not happen"),
        };
        let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42];
        assert_eq!(ipv6_addr.octets(), expected);
    }
}