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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Code to parse bootorder-related nvram variables.

use guid::Guid;
use std::ffi::CStr;
use thiserror::Error;
use ucs2::Ucs2LeSlice;
use uefi_specs::uefi::boot;
use zerocopy::FromBytes;
use zerocopy_helpers::FromBytesExt;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Data length incorrect or inconsistent with other fields")]
    InvalidLength,
    #[error("Missing null-termination")]
    NullTerminated(#[source] std::ffi::FromBytesUntilNulError),
    #[error("Invalid Ucs2 string")]
    InvalidUcs2(#[source] ucs2::Ucs2ParseError),
    #[error("Invalid UTF-8 string")]
    InvalidUtf8(#[source] std::str::Utf8Error),
    #[error("Device path end structure missing or corrupted")]
    DevicePathEnd,
}

#[derive(Debug, PartialEq)]
pub enum HardwareDevice<'a> {
    MemoryMapped(boot::EfiMemoryMappedDevice),
    Vendor {
        vendor_guid: Guid,
        data: &'a [u8],
    },
    Unknown {
        device_subtype: boot::EfiHardwareDeviceSubType,
        path_data: &'a [u8],
    },
}

#[derive(Debug, PartialEq)]
pub enum AcpiDevice<'a> {
    ExpandedAcpi {
        numeric: boot::EfiExpandedAcpiDevice,
        hidstr: &'a CStr,
        uidstr: &'a CStr,
        cidstr: &'a CStr,
    },
    Unknown {
        device_subtype: boot::EfiAcpiDeviceSubType,
        path_data: &'a [u8],
    },
}

#[derive(Debug, PartialEq)]
pub enum MessagingDevice<'a> {
    Scsi(boot::EfiScsiDevice),
    Unknown {
        device_subtype: boot::EfiMessagingDeviceSubType,
        path_data: &'a [u8],
    },
}

#[derive(Debug, PartialEq)]
pub enum MediaDevice<'a> {
    HardDrive(boot::EfiHardDriveDevice),
    File(&'a Ucs2LeSlice),
    PiwgFirmwareFile(Guid),
    PiwgFirmwareVolume(Guid),
    Unknown {
        device_subtype: boot::EfiMediaDeviceSubType,
        path_data: &'a [u8],
    },
}

#[derive(Debug, PartialEq)]
pub enum EndDevice<'a> {
    Instance,
    Entire,
    Unknown {
        device_subtype: boot::EfiEndDeviceSubType,
        path_data: &'a [u8],
    },
}

#[derive(Debug)]
pub enum EfiDevicePathProtocol<'a> {
    Hardware(HardwareDevice<'a>),
    Acpi(AcpiDevice<'a>),
    Messaging(MessagingDevice<'a>),
    Media(MediaDevice<'a>),
    End(EndDevice<'a>),
    Unknown {
        device_type: boot::EfiDeviceType,
        device_subtype: u8,
        path_data: &'a [u8],
    },
}

impl<'a> EfiDevicePathProtocol<'a> {
    pub fn parse(data: &'a [u8]) -> Result<(Self, &'a [u8]), Error> {
        let (header, path_data) = boot::EfiDevicePathProtocol::read_from_prefix_split(data)
            .ok_or(Error::InvalidLength)?;

        let length = u16::from_le_bytes(header.length) as usize;
        // TODO: Switch to split_at_checked below once stable and remove this check
        if data.len() < length {
            return Err(Error::InvalidLength);
        }

        let (path_data, remaining) = path_data.split_at(
            length
                .checked_sub(size_of::<boot::EfiDevicePathProtocol>())
                .ok_or(Error::InvalidLength)?,
        );

        Ok((
            match header.device_type {
                boot::EfiDeviceType::HARDWARE => EfiDevicePathProtocol::Hardware(
                    match boot::EfiHardwareDeviceSubType(header.sub_type) {
                        boot::EfiHardwareDeviceSubType::MEMORY_MAPPED => {
                            HardwareDevice::MemoryMapped(
                                boot::EfiMemoryMappedDevice::read_from(path_data)
                                    .ok_or(Error::InvalidLength)?,
                            )
                        }
                        boot::EfiHardwareDeviceSubType::VENDOR => {
                            let (vendor_guid, path_data) = Guid::read_from_prefix_split(path_data)
                                .ok_or(Error::InvalidLength)?;
                            HardwareDevice::Vendor {
                                vendor_guid,
                                data: path_data,
                            }
                        }
                        device_subtype => HardwareDevice::Unknown {
                            device_subtype,
                            path_data,
                        },
                    },
                ),
                boot::EfiDeviceType::ACPI => {
                    EfiDevicePathProtocol::Acpi(match boot::EfiAcpiDeviceSubType(header.sub_type) {
                        boot::EfiAcpiDeviceSubType::EXPANDED_ACPI => {
                            // minimum length is numeric representation + 3 null terminators from empty strings
                            if path_data.len() < size_of::<boot::EfiExpandedAcpiDevice>() + 3 {
                                return Err(Error::InvalidLength);
                            }
                            let (numeric, path_data) =
                                boot::EfiExpandedAcpiDevice::read_from_prefix_split(path_data)
                                    .unwrap();
                            let hidstr = CStr::from_bytes_until_nul(path_data)
                                .map_err(Error::NullTerminated)?;
                            let path_data = &path_data[hidstr.to_bytes_with_nul().len()..];
                            let uidstr = CStr::from_bytes_until_nul(path_data)
                                .map_err(Error::NullTerminated)?;
                            let path_data = &path_data[uidstr.to_bytes_with_nul().len()..];
                            let cidstr = CStr::from_bytes_until_nul(path_data)
                                .map_err(Error::NullTerminated)?;
                            if cidstr.to_bytes_with_nul().len() != path_data.len() {
                                return Err(Error::InvalidLength);
                            }
                            AcpiDevice::ExpandedAcpi {
                                numeric,
                                hidstr,
                                uidstr,
                                cidstr,
                            }
                        }
                        device_subtype => AcpiDevice::Unknown {
                            device_subtype,
                            path_data,
                        },
                    })
                }
                boot::EfiDeviceType::MESSAGING => EfiDevicePathProtocol::Messaging(
                    match boot::EfiMessagingDeviceSubType(header.sub_type) {
                        boot::EfiMessagingDeviceSubType::SCSI => MessagingDevice::Scsi(
                            boot::EfiScsiDevice::read_from(path_data)
                                .ok_or(Error::InvalidLength)?,
                        ),
                        device_subtype => MessagingDevice::Unknown {
                            device_subtype,
                            path_data,
                        },
                    },
                ),
                boot::EfiDeviceType::MEDIA => EfiDevicePathProtocol::Media(
                    match boot::EfiMediaDeviceSubType(header.sub_type) {
                        boot::EfiMediaDeviceSubType::HARD_DRIVE => MediaDevice::HardDrive(
                            boot::EfiHardDriveDevice::read_from(path_data)
                                .ok_or(Error::InvalidLength)?,
                        ),
                        boot::EfiMediaDeviceSubType::FILE => {
                            let file_name = Ucs2LeSlice::from_slice_with_nul(path_data)
                                .map_err(Error::InvalidUcs2)?;
                            if file_name.as_bytes().len() != path_data.len() {
                                return Err(Error::InvalidLength);
                            }
                            MediaDevice::File(file_name)
                        }
                        boot::EfiMediaDeviceSubType::PIWG_FIRMWARE_FILE => {
                            MediaDevice::PiwgFirmwareFile(
                                Guid::read_from(path_data).ok_or(Error::InvalidLength)?,
                            )
                        }
                        boot::EfiMediaDeviceSubType::PIWG_FIRMWARE_VOLUME => {
                            MediaDevice::PiwgFirmwareVolume(
                                Guid::read_from(path_data).ok_or(Error::InvalidLength)?,
                            )
                        }
                        device_subtype => MediaDevice::Unknown {
                            device_subtype,
                            path_data,
                        },
                    },
                ),
                boot::EfiDeviceType::END => {
                    EfiDevicePathProtocol::End(match boot::EfiEndDeviceSubType(header.sub_type) {
                        boot::EfiEndDeviceSubType::INSTANCE => EndDevice::Instance,
                        boot::EfiEndDeviceSubType::ENTIRE => EndDevice::Entire,
                        device_subtype => EndDevice::Unknown {
                            device_subtype,
                            path_data,
                        },
                    })
                }
                device_type => EfiDevicePathProtocol::Unknown {
                    device_type,
                    device_subtype: header.sub_type,
                    path_data,
                },
            },
            remaining,
        ))
    }
}

#[derive(Debug)]
pub struct EfiLoadOption<'a> {
    pub attributes: u32,
    pub description: &'a Ucs2LeSlice,
    pub device_paths: Vec<EfiDevicePathProtocol<'a>>,
    pub opt: Option<&'a [u8]>,
}

impl<'a> EfiLoadOption<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self, Error> {
        let (header, data) =
            boot::EfiLoadOption::read_from_prefix_split(data).ok_or(Error::InvalidLength)?;

        let description = Ucs2LeSlice::from_slice_with_nul(data).map_err(Error::InvalidUcs2)?;
        let mut data = &data[description.as_bytes().len()..];

        let mut device_paths = Vec::new();
        loop {
            if data.len() < size_of::<boot::EfiDevicePathProtocol>() {
                return Err(Error::DevicePathEnd);
            }
            let path;
            (path, data) = EfiDevicePathProtocol::parse(data)?;
            match path {
                EfiDevicePathProtocol::End(end) => match end {
                    EndDevice::Instance => continue,
                    EndDevice::Entire => break,
                    _ => return Err(Error::DevicePathEnd),
                },
                path => device_paths.push(path),
            }
        }

        let opt = if !data.is_empty() { Some(data) } else { None };

        Ok(EfiLoadOption {
            attributes: header.attributes,
            description,
            device_paths,
            opt,
        })
    }
}

pub fn parse_boot_order(data: &[u8]) -> Result<impl Iterator<Item = u16> + '_, Error> {
    let boot_order_iter = data.chunks_exact(2);
    if !boot_order_iter.remainder().is_empty() {
        return Err(Error::InvalidLength);
    }
    Ok(boot_order_iter.map(|x| u16::from_le_bytes(x.try_into().unwrap())))
}