virtfw-libefi 0.2.1

library to read + write efi data structures
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
//! efi device paths

extern crate alloc;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use core::fmt;
use core::mem::size_of;
use core::net::{Ipv4Addr, Ipv6Addr};

use uguid::Guid;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes};

use crate::nocasestr::NoCaseString;

pub const TYPE_HW: u8 = 1;
pub const TYPE_ACPI: u8 = 2;
pub const TYPE_MSG: u8 = 3;
pub const TYPE_MEDIA: u8 = 4;
pub const TYPE_BBS: u8 = 5;
pub const TYPE_END: u8 = 0x7f;

pub const TYPE_HW_PCI: u8 = 1;

pub const TYPE_ACPI_DP: u8 = 1;
pub const TYPE_ACPI_DPX: u8 = 2;
pub const TYPE_ACPI_ADR: u8 = 3;

pub const TYPE_MSG_SCSI: u8 = 2;
pub const TYPE_MSG_USB: u8 = 5;
pub const TYPE_MSG_VENDOR: u8 = 0x0a;
pub const TYPE_MSG_MAC: u8 = 0x0b;
pub const TYPE_MSG_IPV4: u8 = 0x0c;
pub const TYPE_MSG_IPV6: u8 = 0x0d;
pub const TYPE_MSG_UART: u8 = 0x0e;
pub const TYPE_MSG_USB_CLASS: u8 = 0x0f;
pub const TYPE_MSG_SATA: u8 = 0x12;
pub const TYPE_MSG_URI: u8 = 0x18;

pub const TYPE_MEDIA_HD: u8 = 1;
pub const TYPE_MEDIA_CD: u8 = 2;
pub const TYPE_MEDIA_VENDOR: u8 = 3;
pub const TYPE_MEDIA_FILEPATH: u8 = 4;
pub const TYPE_MEDIA_PROTOCOL: u8 = 5;
pub const TYPE_MEDIA_FW_FILE: u8 = 6;
pub const TYPE_MEDIA_FW_VOL: u8 = 7;

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathHeader {
    pub devtype: u8,
    pub subtype: u8,
    pub length: u16,
}

impl DevPathHeader {
    pub fn new(devtype: u8, subtype: u8, length: usize) -> Self {
        DevPathHeader {
            devtype,
            subtype,
            length: (size_of::<DevPathHeader>() + length) as u16,
        }
    }

    pub fn new_hw_pci() -> Self {
        DevPathHeader::new(TYPE_HW, TYPE_HW_PCI, size_of::<DevPathHwPci>())
    }

    pub fn new_acpi_dp() -> Self {
        DevPathHeader::new(TYPE_ACPI, TYPE_ACPI_DP, size_of::<DevPathAcpiDp>())
    }

    pub fn new_acpi_adr(elems: usize) -> Self {
        DevPathHeader::new(TYPE_ACPI, TYPE_ACPI_ADR, elems * size_of::<u32>())
    }

    pub fn new_msg_scsi() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_SCSI, size_of::<DevPathMsgScsi>())
    }

    pub fn new_msg_usb() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_USB, size_of::<DevPathMsgUsb>())
    }

    pub fn new_msg_vendor() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_VENDOR, size_of::<Guid>())
    }

    pub fn new_msg_mac() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_MAC, size_of::<DevPathMsgMac>())
    }

    pub fn new_msg_ipv4() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_IPV4, 23)
    }

    pub fn new_msg_ipv6() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_IPV6, 56)
    }

    pub fn new_msg_uart() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_UART, size_of::<DevPathMsgUart>())
    }

    pub fn new_msg_sata() -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_SATA, size_of::<DevPathMsgSata>())
    }

    pub fn new_msg_usb_class() -> Self {
        DevPathHeader::new(
            TYPE_MSG,
            TYPE_MSG_USB_CLASS,
            size_of::<DevPathMsgUsbClass>(),
        )
    }

    pub fn new_msg_uri(bytes: usize) -> Self {
        DevPathHeader::new(TYPE_MSG, TYPE_MSG_URI, bytes)
    }

    pub fn new_media_hd() -> Self {
        DevPathHeader::new(TYPE_MEDIA, TYPE_MEDIA_HD, 38)
    }

    pub fn new_media_vendor() -> Self {
        DevPathHeader::new(TYPE_MEDIA, TYPE_MEDIA_VENDOR, size_of::<Guid>())
    }

    pub fn new_media_fw_file() -> Self {
        DevPathHeader::new(TYPE_MEDIA, TYPE_MEDIA_FW_FILE, size_of::<Guid>())
    }

    pub fn new_media_fw_vol() -> Self {
        DevPathHeader::new(TYPE_MEDIA, TYPE_MEDIA_FW_VOL, size_of::<Guid>())
    }

    pub fn new_media_filepath(bytes: usize) -> Self {
        DevPathHeader::new(TYPE_MEDIA, TYPE_MEDIA_FILEPATH, bytes)
    }

    pub fn new_end(subtype: u8) -> Self {
        DevPathHeader::new(TYPE_END, subtype, 0)
    }
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathHwPci {
    pub function: u8,
    pub device: u8,
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathAcpiDp {
    pub hid: u32,
    pub uid: u32,
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgScsi {
    pub target: u16,
    pub lun: u16,
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgUsb {
    pub port: u8,
    pub interface: u8,
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgMac {
    pub mac: [u8; 32],
    pub iftype: u8,
}

#[derive(Debug, PartialEq)]
pub struct DevPathMsgIPv4 {
    pub local_addr: Ipv4Addr,
    pub remote_addr: Ipv4Addr,
    pub local_port: u16,
    pub remote_port: u16,
    pub protocol: u16,
    pub static_ip: bool,
    pub gw_addr: Ipv4Addr,
    pub net_mask: Ipv4Addr,
}

#[derive(Debug, PartialEq)]
pub struct DevPathMsgIPv6 {
    pub local_addr: Ipv6Addr,
    pub remote_addr: Ipv6Addr,
    pub local_port: u16,
    pub remote_port: u16,
    pub protocol: u16,
    pub addr_origin: u8,
    pub prefix_len: u8,
    pub gw_addr: Ipv6Addr,
}

#[repr(C, packed)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgUart {
    pub reserved: u32,
    pub baudrate: u64,
    pub databits: u8,
    pub parity: u8,
    pub stopbits: u8,
}

#[repr(C)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgSata {
    pub port: u16,
    pub mul_port: u16,
    pub lun: u16,
}

#[repr(C, packed)]
#[derive(Debug, PartialEq, FromBytes, IntoBytes, Immutable)]
pub struct DevPathMsgUsbClass {
    pub vendor: u16,
    pub product: u16,
    pub class: u8,
    pub subclass: u8,
    pub protocol: u8,
}

#[derive(Debug, PartialEq)]
pub struct DevPathMediaHd {
    pub nr: u32,
    pub start: u64,
    pub size: u64,
    pub signature: Guid,
    pub mbrtype: u8, // use enum ?
    pub sigtype: u8, // use enum ?
}

#[derive(Debug, PartialEq)]
pub enum DevPathNode {
    HwPci(DevPathHwPci),

    AcpiDp(DevPathAcpiDp),
    AcpiAdr(Vec<u32>),

    MsgScsi(DevPathMsgScsi),
    MsgUsb(DevPathMsgUsb),
    MsgVendor(Guid),
    MsgMac(DevPathMsgMac),
    MsgIPv4(DevPathMsgIPv4),
    MsgIPv6(DevPathMsgIPv6),
    MsgUart(DevPathMsgUart),
    MsgSata(DevPathMsgSata),
    MsgUsbClass(DevPathMsgUsbClass),
    MsgUri(Option<String>),

    MediaHd(DevPathMediaHd),
    MediaVendor(Guid),
    MediaFilePath(NoCaseString),
    MediaFwFile(Guid),
    MediaFwVol(Guid),

    End {
        subtype: u8,
    },

    // generic catch for unimplemented types
    Other {
        devtype: u8,
        subtype: u8,
        blob: Vec<u8>,
    },
}

impl fmt::Display for DevPathNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DevPathNode::HwPci(pci) => {
                write!(f, "Pci({:02x}.{:x})", pci.device, pci.function)
            }

            DevPathNode::AcpiDp(acpi) => match (acpi.hid, acpi.uid) {
                (0xa0341d0, _) => {
                    write!(f, "PciRoot()")
                }
                (h, u) => {
                    write!(f, "Acpi(hid=0x{:x},uid=0x{:x})", h, u)
                }
            },

            DevPathNode::AcpiAdr(adr) => {
                let s: Vec<String> = adr.iter().map(|a| format!("0x{:x}", a)).collect();
                write!(f, "Acpi(adr={})", s.join(","))
            }

            DevPathNode::MsgScsi(scsi) => {
                write!(f, "Scsi(target={},lun={})", scsi.target, scsi.lun)
            }

            DevPathNode::MsgUsb(usb) => {
                write!(f, "Usb(port={},if={})", usb.port, usb.interface)
            }

            DevPathNode::MsgVendor(guid) => {
                write!(f, "Vendor({})", guid)
            }

            DevPathNode::MsgMac(_) => {
                write!(f, "MAC()")
            }

            DevPathNode::MsgIPv4(ipv4) => {
                if ipv4.static_ip {
                    write!(f, "IPv4(static)")
                } else {
                    write!(f, "IPv4(dhcp)")
                }
            }

            DevPathNode::MsgIPv6(ipv6) => match ipv6.addr_origin {
                0 => write!(f, "IPv6(static)"),
                1 => write!(f, "IPv6(stateless)"),
                2 => write!(f, "IPv6(stateful)"),
                _ => write!(f, "IPv6(invalid)"),
            },

            DevPathNode::MsgUart(_) => {
                write!(f, "UART()")
            }

            DevPathNode::MsgSata(sata) => {
                write!(f, "SATA(port={})", sata.port)
            }

            DevPathNode::MsgUsbClass(_) => {
                write!(f, "UsbClass()")
            }
            DevPathNode::MsgUri(uri) => match &uri {
                None => write!(f, "URI()"),
                Some(u) => write!(f, "URI(\"{}\")", u),
            },

            DevPathNode::MediaHd(part) => {
                write!(f, "HD({})", part.nr)
            }

            DevPathNode::MediaVendor(guid) => {
                write!(f, "Vendor({})", guid)
            }

            DevPathNode::MediaFilePath(path) => {
                write!(f, "File(\"{}\")", path)
            }

            DevPathNode::MediaFwFile(filename) => {
                write!(f, "FwFile({})", filename)
            }

            DevPathNode::MediaFwVol(volume) => {
                write!(f, "FwVol({})", volume)
            }

            DevPathNode::End { subtype: _ } => {
                write!(f, "End()")
            }

            DevPathNode::Other {
                devtype,
                subtype,
                blob,
            } => {
                write!(
                    f,
                    "Other(type=0x{:02x}.0x{:02x},len={})",
                    devtype,
                    subtype,
                    blob.len()
                )
            }
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct DevPath {
    pub nodes: Vec<DevPathNode>,
}

impl DevPath {
    pub fn new() -> DevPath {
        let nodes = Vec::new();
        DevPath { nodes }
    }
    pub fn push(&mut self, node: DevPathNode) {
        self.nodes.push(node);
    }
}

impl Default for DevPath {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for DevPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s: Vec<String> = self.nodes.iter().map(|a| format!("{}", a)).collect();
        write!(f, "{}", s.join("/"))
    }
}

#[test]
fn test_struct_sizes() {
    use core::mem::size_of;

    assert_eq!(size_of::<Guid>(), 16);

    assert_eq!(size_of::<DevPathHwPci>(), 2);
    assert_eq!(size_of::<DevPathAcpiDp>(), 8);

    assert_eq!(size_of::<DevPathMsgScsi>(), 4);
    assert_eq!(size_of::<DevPathMsgUsb>(), 2);
    assert_eq!(size_of::<DevPathMsgMac>(), 33);
    assert_eq!(size_of::<DevPathMsgSata>(), 6);
    assert_eq!(size_of::<DevPathMsgUart>(), 15);
    assert_eq!(size_of::<DevPathMsgUsbClass>(), 7);
}