uefi_specs/uefi/
boot.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Definitions related to UEFI boot entries
5
6use guid::Guid;
7use static_assertions::const_assert_eq;
8use zerocopy::FromBytes;
9use zerocopy::Immutable;
10use zerocopy::IntoBytes;
11use zerocopy::KnownLayout;
12
13open_enum::open_enum! {
14    /// From UEFI spec 7.2.1
15    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
16    pub enum EfiMemoryType: u32 {
17        EFI_RESERVED_MEMORY_TYPE = 0,
18        EFI_LOADER_CODE = 1,
19        EFI_LOADER_DATA = 2,
20        EFI_BOOT_SERVICES_CODE = 3,
21        EFI_BOOT_SERVICES_DATA = 4,
22        EFI_RUNTIME_SERVICES_CODE = 5,
23        EFI_RUNTIME_SERVICES_DATA = 6,
24        EFI_CONVENTIONAL_MEMORY = 7,
25        EFI_UNUSABLE_MEMORY = 8,
26        EFI_ACPI_RECLAIM_MEMORY = 9,
27        EFI_ACPI_MEMORY_NVS = 10,
28        EFI_MEMORY_MAPPED_IO = 11,
29        EFI_MEMORY_MAPPED_IOPORT_SPACE = 12,
30        EFI_PAL_CODE = 13,
31        EFI_PERSISTENT_MEMORY = 14,
32        EFI_UNACCEPTED_MEMORY_TYPE = 15,
33        EFI_MAX_MEMORY_TYPE = 16,
34    }
35}
36
37/// From UEFI spec 10.2
38#[repr(C, packed)]
39#[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
40pub struct EfiDevicePathProtocol {
41    pub device_type: EfiDeviceType,
42    pub sub_type: u8,
43    pub length: [u8; 2],
44}
45
46/// From UEFI spec 3.1.3
47#[repr(C, packed)]
48#[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
49pub struct EfiLoadOption {
50    pub attributes: u32,
51    pub file_path_list_length: u16,
52}
53
54open_enum::open_enum! {
55    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
56    pub enum EfiDeviceType: u8 {
57        HARDWARE = 0x01,
58        ACPI = 0x02,
59        MESSAGING = 0x03,
60        MEDIA = 0x04,
61        BIOS_BOOT_SPEC = 0x05,
62        END = 0x7F,
63    }
64}
65
66open_enum::open_enum! {
67    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
68    pub enum EfiEndDeviceSubType: u8 {
69        INSTANCE = 0x01,
70        ENTIRE = 0xFF,
71    }
72}
73
74open_enum::open_enum! {
75    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
76    pub enum EfiHardwareDeviceSubType: u8 {
77        PCI = 1,
78        PCCARD = 2,
79        MEMORY_MAPPED = 3,
80        VENDOR = 4,
81        CONTROLLER = 5,
82        BMC = 6,
83    }
84}
85
86open_enum::open_enum! {
87    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
88    pub enum EfiAcpiDeviceSubType: u8 {
89        ACPI = 1,
90        EXPANDED_ACPI = 2,
91        ADR = 3,
92        NVDIMM = 4,
93    }
94}
95
96open_enum::open_enum! {
97    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
98    pub enum EfiMessagingDeviceSubType: u8 {
99        ATAPI = 1,
100        SCSI = 2,
101        FIBRE_CHANNEL = 3,
102        FIBRE_CHANNEL_EX = 21,
103        IEEE_1394 = 4,
104        USB = 5,
105        SATA = 18,
106        USB_WWID = 16,
107        LOGICAL_UNIT =  17,
108        USB_CLASS = 15,
109        I20_RANDOM_BLOCK_STORAGE_CLASS = 6,
110        MAC_ADDRESS = 11,
111        IPV4 = 12,
112        IPV6 = 13,
113        VLAN = 20,
114        INFINIBAND = 9,
115        UART = 14,
116        SAS = 10,
117        SAS_EX = 22,
118        ISCSI = 19,
119        NVME_NAMESPACE = 23,
120        URI = 24,
121        UFS = 25,
122        SD = 26,
123        BLUETOOTH = 27,
124        WIFI = 28,
125        EMMC = 29,
126        BLUETOOTH_LE = 30,
127        DNS = 31,
128        NVDIMM = 32,
129        REST_SERVICE = 33,
130        NVME_OF = 34,
131    }
132}
133
134open_enum::open_enum! {
135    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
136    pub enum EfiMediaDeviceSubType: u8 {
137        HARD_DRIVE = 0x01,
138        CD_ROM = 0x02,
139        VENDOR = 0x03,
140        FILE = 0x04,
141        MEDIA_PROTOCOL = 0x05,
142        PIWG_FIRMWARE_FILE = 0x06,
143        PIWG_FIRMWARE_VOLUME = 0x07,
144        RELATIVE_OFFSET_RANGE = 0x08,
145        RAM_DISK = 0x09,
146    }
147}
148
149open_enum::open_enum! {
150    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
151    pub enum EfiPartitionFormat: u8 {
152        MBR = 0x01,
153        GUID = 0x02,
154    }
155}
156
157open_enum::open_enum! {
158    #[derive(IntoBytes, FromBytes, Immutable, KnownLayout)]
159    pub enum EfiSignatureType: u8 {
160        NONE = 0x00,
161        MBR = 0x01,
162        GUID = 0x02,
163    }
164}
165
166#[repr(C, packed)]
167#[derive(IntoBytes, FromBytes, Immutable, KnownLayout, Debug, PartialEq)]
168pub struct EfiHardDriveDevice {
169    pub partition_number: u32,
170    pub partition_start: u64,
171    pub partition_size: u64,
172    pub partition_signature: Guid,
173    pub partition_format: EfiPartitionFormat,
174    pub partition_type: EfiSignatureType,
175}
176
177#[repr(C, packed)]
178#[derive(IntoBytes, FromBytes, Immutable, KnownLayout, Debug, PartialEq)]
179pub struct EfiScsiDevice {
180    pub target_id: u16,
181    pub logical_unit_num: u16,
182}
183
184/// From UEFI spec 4.6 — EFI_SYSTEM_TABLE
185///
186/// Minimal layout covering header fields and the pointers needed by
187/// the Linux EFI stub (firmware vendor, configuration table).
188#[repr(C)]
189#[derive(Clone, Copy, Debug, Default, IntoBytes, Immutable, KnownLayout)]
190pub struct EfiSystemTable {
191    // EFI_TABLE_HEADER (UEFI spec 4.2)
192    pub signature: u64,
193    pub revision: u32,
194    pub header_size: u32,
195    pub crc32: u32,
196    pub reserved: u32,
197    // Body
198    pub firmware_vendor: u64,
199    pub firmware_revision: u32,
200    pub _pad0: u32,
201    pub console_in_handle: u64,
202    pub con_in: u64,
203    pub console_out_handle: u64,
204    pub con_out: u64,
205    pub standard_error_handle: u64,
206    pub std_err: u64,
207    pub runtime_services: u64,
208    pub boot_services: u64,
209    pub number_of_table_entries: u64,
210    pub configuration_table: u64,
211}
212
213/// From UEFI spec 4.6
214pub const EFI_SYSTEM_TABLE_SIGNATURE: u64 = 0x5453595320494249; // "IBI SYST"
215/// EFI 2.70 system table revision.
216pub const EFI_2_70_SYSTEM_TABLE_REVISION: u32 = 0x0002_0046;
217
218/// From UEFI spec 7.2 — EFI_MEMORY_DESCRIPTOR
219#[repr(C)]
220#[derive(Clone, Copy, Debug, IntoBytes, Immutable, KnownLayout)]
221pub struct EfiMemoryDescriptor {
222    pub typ: EfiMemoryType,
223    pub _pad: u32,
224    pub physical_start: u64,
225    pub virtual_start: u64,
226    pub number_of_pages: u64,
227    pub attribute: u64,
228}
229
230const_assert_eq!(size_of::<EfiMemoryDescriptor>(), 40);
231
232/// From UEFI spec 7.2
233pub const EFI_MEMORY_DESCRIPTOR_VERSION: u32 = 1;
234
235/// From UEFI spec 7.2 — EFI_MEMORY_WB attribute
236pub const EFI_MEMORY_WB: u64 = 0x8;
237
238/// ACPI 2.0 table GUID for EFI configuration table entries.
239pub const ACPI_20_TABLE_GUID: Guid = guid::guid!("8868e871-e4f1-11d3-bc22-0080c73c8881");
240
241/// EFI RT Properties Table GUID (UEFI spec 4.6).
242pub const EFI_RT_PROPERTIES_TABLE_GUID: Guid = guid::guid!("eb66918a-7eef-402a-842e-931d21c38ae9");
243
244/// From UEFI spec 4.6 — EFI_RT_PROPERTIES_TABLE
245///
246/// Installed in the EFI Configuration Table to tell the OS which runtime
247/// services are supported. Setting `runtime_services_supported` to zero
248/// means no runtime services are backed by real code.
249#[repr(C)]
250#[derive(Clone, Copy, Debug, IntoBytes, Immutable, KnownLayout)]
251pub struct EfiRtPropertiesTable {
252    pub version: u16,
253    pub length: u16,
254    pub runtime_services_supported: u32,
255}
256
257impl EfiRtPropertiesTable {
258    /// A table advertising that no runtime services are supported.
259    pub const NONE_SUPPORTED: Self = Self {
260        version: 1,
261        length: size_of::<Self>() as u16,
262        runtime_services_supported: 0,
263    };
264}
265
266#[repr(C, packed)]
267#[derive(IntoBytes, FromBytes, Immutable, KnownLayout, Debug, PartialEq)]
268pub struct EfiMemoryMappedDevice {
269    pub memory_type: EfiMemoryType,
270    pub start_address: u64,
271    pub end_address: u64,
272}
273
274#[repr(C, packed)]
275#[derive(IntoBytes, FromBytes, Immutable, KnownLayout, Debug, PartialEq)]
276pub struct EfiExpandedAcpiDevice {
277    pub hid: u32,
278    pub cid: u32,
279    pub uid: u32,
280}