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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Utilities for creating new [`DevicePaths`].
//!
//! This module contains [`DevicePathBuilder`], as well as submodules
//! containing types for building each type of device path node.
//!
//! [`DevicePaths`]: DevicePath

pub use crate::proto::device_path::device_path_gen::build::*;

use crate::polyfill::{maybe_uninit_slice_as_mut_ptr, maybe_uninit_slice_assume_init_ref};
use crate::proto::device_path::{DevicePath, DevicePathNode};
use core::mem::MaybeUninit;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// A builder for [`DevicePaths`].
///
/// The builder can be constructed with either a fixed-length buffer or
/// (if the `alloc` feature is enabled) a `Vec`.
///
/// Nodes are added via the [`push`] method. To construct a node, use one
/// of the structs in these submodules:
/// * [`acpi`]
/// * [`bios_boot_spec`]
/// * [`end`]
/// * [`hardware`]
/// * [`media`]
/// * [`messaging`]
///
/// A node can also be constructed by copying a node from an existing
/// device path.
///
/// To complete a path, call the [`finalize`] method. This adds an
/// [`END_ENTIRE`] node and returns a [`DevicePath`] reference tied to
/// the lifetime of the buffer the builder was constructed with.
///
/// [`DevicePaths`]: DevicePath
/// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
/// [`finalize`]: DevicePathBuilder::finalize
/// [`push`]: DevicePathBuilder::push
///
/// # Examples
///
/// ```
/// use core::mem::MaybeUninit;
/// use uefi::guid;
/// use uefi::proto::device_path::DevicePath;
/// use uefi::proto::device_path::build;
///
/// # fn main() -> Result<(), build::BuildError> {
/// let mut buf = [MaybeUninit::uninit(); 256];
/// let path: &DevicePath = build::DevicePathBuilder::with_buf(&mut buf)
///     .push(&build::acpi::Acpi {
///         hid: 0x41d0_0a03,
///         uid: 0x0000_0000,
///     })?
///     .push(&build::hardware::Pci {
///         function: 0x00,
///         device: 0x1f,
///     })?
///     .push(&build::hardware::Vendor {
///         vendor_guid: guid!("15e39a00-1dd2-1000-8d7f-00a0c92408fc"),
///         vendor_defined_data: &[1, 2, 3, 4, 5, 6],
///     })?
///     .finalize()?;
///
/// assert_eq!(path.node_iter().count(), 3);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct DevicePathBuilder<'a> {
    storage: BuilderStorage<'a>,
}

impl<'a> DevicePathBuilder<'a> {
    /// Create a builder backed by a statically-sized buffer.
    pub fn with_buf(buf: &'a mut [MaybeUninit<u8>]) -> Self {
        Self {
            storage: BuilderStorage::Buf { buf, offset: 0 },
        }
    }

    /// Create a builder backed by a `Vec`.
    ///
    /// The `Vec` is cleared before use.
    #[cfg(feature = "alloc")]
    pub fn with_vec(v: &'a mut Vec<u8>) -> Self {
        v.clear();
        Self {
            storage: BuilderStorage::Vec(v),
        }
    }

    /// Add a node to the device path.
    ///
    /// An error will be returned if an [`END_ENTIRE`] node is passed to
    /// this function, as that node will be added when `finalize` is
    /// called.
    ///
    /// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
    pub fn push(mut self, node: &dyn BuildNode) -> Result<Self, BuildError> {
        let node_size = usize::from(node.size_in_bytes()?);

        match &mut self.storage {
            BuilderStorage::Buf { buf, offset } => {
                node.write_data(
                    buf.get_mut(*offset..*offset + node_size)
                        .ok_or(BuildError::BufferTooSmall)?,
                );
                *offset += node_size;
            }
            #[cfg(feature = "alloc")]
            BuilderStorage::Vec(vec) => {
                let old_size = vec.len();
                vec.reserve(node_size);
                let buf = &mut vec.spare_capacity_mut()[..node_size];
                node.write_data(buf);
                unsafe {
                    vec.set_len(old_size + node_size);
                }
            }
        }

        Ok(self)
    }

    /// Add an [`END_ENTIRE`] node and return the resulting [`DevicePath`].
    ///
    /// This method consumes the builder.
    ///
    /// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
    pub fn finalize(self) -> Result<&'a DevicePath, BuildError> {
        let this = self.push(&end::Entire)?;

        let data: &[u8] = match &this.storage {
            BuilderStorage::Buf { buf, offset } => unsafe {
                maybe_uninit_slice_assume_init_ref(&buf[..*offset])
            },
            #[cfg(feature = "alloc")]
            BuilderStorage::Vec(vec) => vec,
        };

        let ptr: *const () = data.as_ptr().cast();
        Ok(unsafe { &*ptr_meta::from_raw_parts(ptr, data.len()) })
    }
}

#[derive(Debug)]
enum BuilderStorage<'a> {
    Buf {
        buf: &'a mut [MaybeUninit<u8>],
        offset: usize,
    },

    #[cfg(feature = "alloc")]
    Vec(&'a mut Vec<u8>),
}

/// Error type used by [`DevicePathBuilder`].
#[derive(Clone, Copy, Debug)]
pub enum BuildError {
    /// A node was too big to fit in the remaining buffer space.
    BufferTooSmall,

    /// An individual node's length is too big to fit in a [`u16`].
    NodeTooBig,

    /// An [`END_ENTIRE`] node was passed to the builder. Use
    /// [`DevicePathBuilder::finalize`] instead.
    ///
    /// [`END_ENTIRE`]: uefi::proto::device_path::DeviceSubType::END_ENTIRE
    UnexpectedEndEntire,
}

/// Trait for types that can be used to build a node via
/// [`DevicePathBuilder::push`].
///
/// This trait is implemented for all the node types in
/// [`uefi::proto::device_path::build`]. It is also implemented for
/// [`&DevicePathNode`], which allows an existing node to be copied by
/// the builder.
///
/// # Safety
///
/// The methods of this trait are safe to call, but the trait itself is
/// `unsafe` because an incorrect implementation could cause
/// unsafety. In particular, the `write_data` is required to
/// completely initialize all bytes in the output slice.
///
/// [`&DevicePathNode`]: DevicePathNode
pub unsafe trait BuildNode {
    /// Size of the node in bytes, including the standard node
    /// header. Returns [`BuildError::NodeTooBig`] if the node's size
    /// does not fit in a [`u16`].
    fn size_in_bytes(&self) -> Result<u16, BuildError>;

    /// Write out the node data.
    ///
    /// The length of `out` must be equal to the node's `size_in_bytes`.
    ///
    /// The `out` slice will be fully initialized after the call.
    fn write_data(&self, out: &mut [MaybeUninit<u8>]);
}

unsafe impl BuildNode for &DevicePathNode {
    fn size_in_bytes(&self) -> Result<u16, BuildError> {
        Ok(self.header.length)
    }

    fn write_data(&self, out: &mut [MaybeUninit<u8>]) {
        let src: *const u8 = self.as_ffi_ptr().cast();

        let dst: *mut u8 = maybe_uninit_slice_as_mut_ptr(out);
        unsafe {
            dst.copy_from_nonoverlapping(src, out.len());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::guid;
    use crate::proto::device_path::media::{PartitionFormat, PartitionSignature};
    use crate::proto::device_path::messaging::{
        Ipv4AddressOrigin, IscsiLoginOptions, IscsiProtocol, RestServiceAccessMode, RestServiceType,
    };
    use core::{mem, slice};

    fn path_to_bytes(path: &DevicePath) -> &[u8] {
        unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), mem::size_of_val(path)) }
    }

    /// Test building an ACPI ADR node.
    #[test]
    fn test_acpi_adr() -> Result<(), BuildError> {
        assert!(acpi::AdrSlice::new(&[]).is_none());

        let mut v = Vec::new();
        let path = DevicePathBuilder::with_vec(&mut v)
            .push(&acpi::Adr {
                adr: acpi::AdrSlice::new(&[1, 2]).unwrap(),
            })?
            .finalize()?;

        let node: &crate::proto::device_path::acpi::Adr =
            path.node_iter().next().unwrap().try_into().unwrap();
        assert_eq!(node.adr().iter().collect::<Vec<_>>(), [1, 2]);

        let bytes = path_to_bytes(path);
        #[rustfmt::skip]
        assert_eq!(bytes, [
            // ACPI ADR node
            0x02, 0x03, 0x0c, 0x00,
            // Values
            0x01, 0x00, 0x00, 0x00,
            0x02, 0x00, 0x00, 0x00,

            // End-entire node
            0x7f, 0xff, 0x04, 0x00,
        ]);

        Ok(())
    }

    /// Test building an ACPI Expanded node.
    #[test]
    fn test_acpi_expanded() -> Result<(), BuildError> {
        let mut v = Vec::new();
        let path = DevicePathBuilder::with_vec(&mut v)
            .push(&acpi::Expanded {
                hid: 1,
                uid: 2,
                cid: 3,
                hid_str: b"a\0",
                uid_str: b"bc\0",
                cid_str: b"def\0",
            })?
            .finalize()?;

        let node: &crate::proto::device_path::acpi::Expanded =
            path.node_iter().next().unwrap().try_into().unwrap();
        assert_eq!(node.hid(), 1);
        assert_eq!(node.uid(), 2);
        assert_eq!(node.cid(), 3);
        assert_eq!(node.hid_str(), b"a\0");
        assert_eq!(node.uid_str(), b"bc\0");
        assert_eq!(node.cid_str(), b"def\0");

        let bytes = path_to_bytes(path);
        #[rustfmt::skip]
        assert_eq!(bytes, [
            // ACPI Expanded node
            0x02, 0x02, 0x19, 0x00,
            // HID
            0x01, 0x00, 0x00, 0x00,
            // UID
            0x02, 0x00, 0x00, 0x00,
            // CID
            0x03, 0x00, 0x00, 0x00,

            // HID str
            0x61, 0x00,

            // UID str
            0x62, 0x63, 0x00,

            // CID str
            0x64, 0x65, 0x66, 0x00,

            // End-entire node
            0x7f, 0xff, 0x04, 0x00,
        ]);

        Ok(())
    }

    /// Test building a messaging REST Service node.
    #[test]
    fn test_messaging_rest_service() -> Result<(), BuildError> {
        let mut v = Vec::new();
        let vendor_guid = guid!("a1005a90-6591-4596-9bab-1c4249a6d4ff");
        let path = DevicePathBuilder::with_vec(&mut v)
            .push(&messaging::RestService {
                service_type: RestServiceType::REDFISH,
                access_mode: RestServiceAccessMode::IN_BAND,
                vendor_guid_and_data: None,
            })?
            .push(&messaging::RestService {
                service_type: RestServiceType::VENDOR,
                access_mode: RestServiceAccessMode::OUT_OF_BAND,
                vendor_guid_and_data: Some(messaging::RestServiceVendorData {
                    vendor_guid,
                    vendor_defined_data: &[1, 2, 3, 4, 5],
                }),
            })?
            .finalize()?;

        let mut iter = path.node_iter();
        let mut node: &crate::proto::device_path::messaging::RestService =
            iter.next().unwrap().try_into().unwrap();
        assert!(node.vendor_guid_and_data().is_none());
        node = iter.next().unwrap().try_into().unwrap();
        assert_eq!(node.vendor_guid_and_data().unwrap().0, vendor_guid);
        assert_eq!(node.vendor_guid_and_data().unwrap().1, &[1, 2, 3, 4, 5]);

        let bytes = path_to_bytes(path);
        #[rustfmt::skip]
        assert_eq!(bytes, [
            // Messaging REST Service node.
            0x03, 0x21, 0x06, 0x00,
            // Type and access mode
            0x01, 0x01,

            // Messaging REST Service node.  The spec incorrectly says
            // the length is 21+n bytes, it's actually 22+n bytes.
            0x03, 0x21, 0x1b, 0x00,
            // Type and access mode
            0xff, 0x02,
            // Vendor guid
            0x90, 0x5a, 0x00, 0xa1,
            0x91, 0x65, 0x96, 0x45,
            0x9b, 0xab, 0x1c, 0x42,
            0x49, 0xa6, 0xd4, 0xff,
            // Vendor data
            0x01, 0x02, 0x03, 0x04, 0x05,

            // End-entire node
            0x7f, 0xff, 0x04, 0x00,
        ]);

        Ok(())
    }

    /// Test that packed nodes can be passed into the builder.
    #[test]
    fn test_build_with_packed_node() -> Result<(), BuildError> {
        // Build a path with both a statically-sized and DST nodes.
        let mut v = Vec::new();
        let path1 = DevicePathBuilder::with_vec(&mut v)
            .push(&acpi::Acpi {
                hid: 0x41d0_0a03,
                uid: 0x0000_0000,
            })?
            .push(&hardware::Vendor {
                vendor_guid: guid!("15e39a00-1dd2-1000-8d7f-00a0c92408fc"),
                vendor_defined_data: &[1, 2, 3, 4, 5, 6],
            })?
            .finalize()?;

        // Create a second path by copying in the packed nodes from the
        // first path.
        let mut v = Vec::new();
        let mut builder = DevicePathBuilder::with_vec(&mut v);
        for node in path1.node_iter() {
            builder = builder.push(&node)?;
        }
        let path2 = builder.finalize()?;

        // Verify the copied path is identical.
        assert_eq!(path1, path2);

        Ok(())
    }

    /// This test is based on the "Fibre Channel Ex Device Path Example"
    /// from the UEFI Specification.
    #[test]
    fn test_fibre_channel_ex_device_path_example() -> Result<(), BuildError> {
        // Arbitrarily choose this test to use a statically-sized
        // buffer, just to make sure that code path is tested.
        let mut buf = [MaybeUninit::uninit(); 256];
        let path = DevicePathBuilder::with_buf(&mut buf)
            .push(&acpi::Acpi {
                hid: 0x41d0_0a03,
                uid: 0x0000_0000,
            })?
            .push(&hardware::Pci {
                function: 0x00,
                device: 0x1f,
            })?
            .push(&messaging::FibreChannelEx {
                world_wide_name: [0, 1, 2, 3, 4, 5, 6, 7],
                logical_unit_number: [0, 1, 2, 3, 4, 5, 6, 7],
            })?
            .finalize()?;

        let bytes = path_to_bytes(path);
        #[rustfmt::skip]
        assert_eq!(bytes, [
            // ACPI node
            0x02, 0x01, 0x0c, 0x00,
            // HID
            0x03, 0x0a, 0xd0, 0x41,
            // UID
            0x00, 0x00, 0x00, 0x00,

            // PCI node
            0x01, 0x01, 0x06, 0x00,
            // Function
            0x00,
            // Device
            0x1f,

            // Fibre Channel Ex node
            0x03, 0x15,
            // The example in the spec is wrong here; it says 0x14 for
            // the length and leaves out the four-byte reserved field.
            0x18, 0x00,
            // Reserved
            0x00, 0x00, 0x00, 0x00,
            // World wide name
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
            // Logical unit number
            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,

            // End-entire node
            0x7f, 0xff, 0x04, 0x00,
        ]);

        Ok(())
    }

    /// This test is based on the "IPv4 configuration" example from the
    /// UEFI Specification.
    #[test]
    fn test_ipv4_configuration_example() -> Result<(), BuildError> {
        let mut v = Vec::new();
        let path = DevicePathBuilder::with_vec(&mut v)
            .push(&acpi::Acpi {
                hid: 0x41d0_0a03,
                uid: 0x0000_0000,
            })?
            .push(&hardware::Pci {
                function: 0x00,
                device: 0x19,
            })?
            .push(&messaging::MacAddress {
                mac_address: [
                    0x00, 0x13, 0x20, 0xf5, 0xfa, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                ],
                interface_type: 0x01,
            })?
            .push(&messaging::Ipv4 {
                local_ip_address: [192, 168, 0, 1],
                remote_ip_address: [192, 168, 0, 100],
                local_port: 0,
                remote_port: 3260,
                protocol: 6,
                ip_address_origin: Ipv4AddressOrigin::STATIC,
                gateway_ip_address: [0, 0, 0, 0],
                subnet_mask: [0, 0, 0, 0],
            })?
            .push(&messaging::Iscsi {
                protocol: IscsiProtocol::TCP,
                options: IscsiLoginOptions::AUTH_METHOD_NONE,
                logical_unit_number: 0u64.to_le_bytes(),
                target_portal_group_tag: 1,
                iscsi_target_name: b"iqn.1991-05.com.microsoft:iscsitarget-iscsidisk-target\0",
            })?
            .push(&media::HardDrive {
                partition_number: 1,
                partition_start: 0x22,
                partition_size: 0x2710000,
                partition_format: PartitionFormat::GPT,
                partition_signature: PartitionSignature::Guid(guid!(
                    "15e39a00-1dd2-1000-8d7f-00a0c92408fc"
                )),
            })?
            .finalize()?;

        let bytes = path_to_bytes(path);
        #[rustfmt::skip]
        assert_eq!(bytes, [
            // ACPI node
            0x02, 0x01, 0x0c, 0x00,
            // HID
            0x03, 0x0a, 0xd0, 0x41,
            // UID
            0x00, 0x00, 0x00, 0x00,

            // PCI node
            0x01, 0x01, 0x06, 0x00,
            // Function
            0x00,
            // Device
            0x19,

            // MAC address node
            0x03, 0x0b, 0x25, 0x00,
            // MAC address
            0x00, 0x13, 0x20, 0xf5, 0xfa, 0x77, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            // Network interface type
            0x01,

            // IPv4 node
            0x03, 0x0c, 0x1b, 0x00,
            // Local address
            0xc0, 0xa8, 0x00, 0x01,
            // Remote address
            0xc0, 0xa8, 0x00, 0x64,
            // Local port
            0x00, 0x00,
            // Remote port
            0xbc, 0x0c,
            // Protocol
            0x06, 0x00,
            // Static IP
            0x01,
            // Gateway IP
            0x00, 0x00, 0x00, 0x00,
            // Subnet mask
            0x00, 0x00, 0x00, 0x00,

            // iSCSI node
            0x03, 0x13, 0x49, 0x00,
            // Protocol
            0x00, 0x00,
            // Login options
            0x00, 0x08,
            // LUN
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            // Target portal group tag
            0x01, 0x00,
            // Node name
            0x69, 0x71, 0x6e, 0x2e, 0x31, 0x39, 0x39, 0x31,
            0x2d, 0x30, 0x35, 0x2e, 0x63, 0x6f, 0x6d, 0x2e,
            0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
            0x74, 0x3a, 0x69, 0x73, 0x63, 0x73, 0x69, 0x74,
            0x61, 0x72, 0x67, 0x65, 0x74, 0x2d, 0x69, 0x73,
            0x63, 0x73, 0x69, 0x64, 0x69, 0x73, 0x6b, 0x2d,
            0x74, 0x61, 0x72, 0x67, 0x65, 0x74, 0x00,

            // Hard drive node
            0x04, 0x01, 0x2a, 0x00,
            // Partition number
            0x01, 0x00, 0x00, 0x00,
            // Partition start
            0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            // Partition size
            0x00, 0x00, 0x71, 0x02, 0x00, 0x00, 0x00, 0x00,
            // Partition signature
            0x00, 0x9a, 0xe3, 0x15, 0xd2, 0x1d, 0x00, 0x10,
            0x8d, 0x7f, 0x00, 0xa0, 0xc9, 0x24, 0x08, 0xfc,
            // Partition format
            0x02,
            // Signature type
            0x02,

            // End-entire node
            0x7f, 0xff, 0x04, 0x00,
        ]);

        Ok(())
    }
}