Skip to content

Commit 400c905

Browse files
Enhance Guid::from_values and Guid::fmt (#280)
1 parent 58ae6a4 commit 400c905

File tree

6 files changed

+75
-123
lines changed

6 files changed

+75
-123
lines changed

src/data_types/guid.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,31 @@ pub struct Guid {
2626

2727
impl Guid {
2828
/// Creates a new GUID from its canonical representation
29-
//
30-
// FIXME: An unwieldy array of bytes must be used for the node ID until one
31-
// can assert that an u64 has its high 16-bits cleared in a const fn.
32-
// Once that is done, we can take an u64 to be even closer to the
33-
// canonical UUID/GUID format.
34-
//
3529
pub const fn from_values(
3630
time_low: u32,
3731
time_mid: u16,
3832
time_high_and_version: u16,
3933
clock_seq_and_variant: u16,
40-
node: [u8; 6],
34+
node: u64,
4135
) -> Self {
36+
assert!(node.leading_zeros() >= 16, "node must be a 48-bit integer");
37+
// intentional shadowing
38+
let node = node.to_be_bytes();
39+
4240
Guid {
4341
a: time_low,
4442
b: time_mid,
4543
c: time_high_and_version,
4644
d: [
4745
(clock_seq_and_variant / 0x100) as u8,
4846
(clock_seq_and_variant % 0x100) as u8,
49-
node[0],
50-
node[1],
47+
// first two elements of node are ignored, we only want the low 48 bits
5148
node[2],
5249
node[3],
5350
node[4],
5451
node[5],
52+
node[6],
53+
node[7],
5554
],
5655
}
5756
}
@@ -60,18 +59,17 @@ impl Guid {
6059
impl fmt::Display for Guid {
6160
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
6261
let d = {
63-
let (low, high) = (u16::from(self.d[0]), u16::from(self.d[1]));
64-
65-
(low << 8) | high
62+
let mut buf = [0u8; 2];
63+
buf[..].copy_from_slice(&self.d[0..2]);
64+
u16::from_be_bytes(buf)
6665
};
6766

68-
// Extract and reverse byte order.
69-
let e = self.d[2..8].iter().enumerate().fold(0, |acc, (i, &elem)| {
70-
acc | {
71-
let shift = (5 - i) * 8;
72-
u64::from(elem) << shift
73-
}
74-
});
67+
let e = {
68+
let mut buf = [0u8; 8];
69+
// first two elements of node are ignored, we only want the low 48 bits
70+
buf[2..].copy_from_slice(&self.d[2..8]);
71+
u64::from_be_bytes(buf)
72+
};
7573

7674
write!(
7775
fmt,
@@ -107,3 +105,32 @@ pub unsafe trait Identify {
107105
}
108106

109107
pub use uefi_macros::unsafe_guid;
108+
109+
#[cfg(test)]
110+
mod tests {
111+
use uefi::unsafe_guid;
112+
extern crate alloc;
113+
use super::*;
114+
115+
#[test]
116+
fn test_guid_display() {
117+
assert_eq!(
118+
alloc::format!(
119+
"{}",
120+
Guid::from_values(0x12345678, 0x9abc, 0xdef0, 0x1234, 0x56789abcdef0)
121+
),
122+
"12345678-9abc-def0-1234-56789abcdef0"
123+
);
124+
}
125+
126+
#[test]
127+
fn test_unsafe_guid() {
128+
#[unsafe_guid("12345678-9abc-def0-1234-56789abcdef0")]
129+
struct X;
130+
131+
assert_eq!(
132+
X::GUID,
133+
Guid::from_values(0x12345678, 0x9abc, 0xdef0, 0x1234, 0x56789abcdef0)
134+
);
135+
}
136+
}

src/proto/media/partition.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ newtype_enum! {
5959
0x0000,
6060
0x0000,
6161
0x0000,
62-
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
62+
0x000000000000,
6363
),
6464

6565
/// EFI System Partition.
@@ -68,7 +68,7 @@ newtype_enum! {
6868
0xf81f,
6969
0x11d2,
7070
0xba4b,
71-
[0x00, 0xa0, 0xc9, 0x3e, 0xc9, 0x3b],
71+
0x00a0c93ec93b,
7272
),
7373

7474
/// Partition containing a legacy MBR.
@@ -77,7 +77,7 @@ newtype_enum! {
7777
0x33e7,
7878
0x11d3,
7979
0x9d69,
80-
[0x00, 0x08, 0xc7, 0x81, 0xf3, 0x9f],
80+
0x0008c781f39f,
8181
),
8282
}
8383
}

src/table/cfg.rs

+21-85
Original file line numberDiff line numberDiff line change
@@ -26,54 +26,25 @@ pub struct ConfigTableEntry {
2626
/// Whether this is a physical or virtual address depends on the table.
2727
pub address: *const c_void,
2828
}
29-
3029
/// Entry pointing to the old ACPI 1 RSDP.
31-
pub const ACPI_GUID: Guid = Guid::from_values(
32-
0xeb9d2d30,
33-
0x2d88,
34-
0x11d3,
35-
0x9a16,
36-
[0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d],
37-
);
30+
pub const ACPI_GUID: Guid = Guid::from_values(0xeb9d2d30, 0x2d88, 0x11d3, 0x9a16, 0x0090273fc14d);
3831

3932
///Entry pointing to the ACPI 2 RSDP.
40-
pub const ACPI2_GUID: Guid = Guid::from_values(
41-
0x8868e871,
42-
0xe4f1,
43-
0x11d3,
44-
0xbc22,
45-
[0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81],
46-
);
33+
pub const ACPI2_GUID: Guid = Guid::from_values(0x8868e871, 0xe4f1, 0x11d3, 0xbc22, 0x0080c73c8881);
4734

4835
/// Entry pointing to the SMBIOS 1.0 table.
49-
pub const SMBIOS_GUID: Guid = Guid::from_values(
50-
0xeb9d2d31,
51-
0x2d88,
52-
0x11d3,
53-
0x9a16,
54-
[0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d],
55-
);
36+
pub const SMBIOS_GUID: Guid = Guid::from_values(0xeb9d2d31, 0x2d88, 0x11d3, 0x9a16, 0x0090273fc14d);
5637

5738
/// Entry pointing to the SMBIOS 3.0 table.
58-
pub const SMBIOS3_GUID: Guid = Guid::from_values(
59-
0xf2fd1544,
60-
0x9794,
61-
0x4a2c,
62-
0x992e,
63-
[0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94],
64-
);
39+
pub const SMBIOS3_GUID: Guid =
40+
Guid::from_values(0xf2fd1544, 0x9794, 0x4a2c, 0x992e, 0xe5bbcf20e394);
6541

6642
/// GUID of the UEFI properties table.
6743
///
6844
/// The properties table is used to provide additional info
6945
/// about the UEFI implementation.
70-
pub const PROPERTIES_TABLE_GUID: Guid = Guid::from_values(
71-
0x880aaca3,
72-
0x4adc,
73-
0x4a04,
74-
0x9079,
75-
[0xb7, 0x47, 0x34, 0x08, 0x25, 0xe5],
76-
);
46+
pub const PROPERTIES_TABLE_GUID: Guid =
47+
Guid::from_values(0x880aaca3, 0x4adc, 0x4a04, 0x9079, 0xb747340825e5);
7748

7849
/// This table contains additional information about the UEFI implementation.
7950
#[repr(C)]
@@ -102,65 +73,30 @@ bitflags! {
10273
/// Hand-off Blocks are used to pass data from the early pre-UEFI environment to the UEFI drivers.
10374
///
10475
/// Most OS loaders or applications should not mess with this.
105-
pub const HAND_OFF_BLOCK_LIST_GUID: Guid = Guid::from_values(
106-
0x7739f24c,
107-
0x93d7,
108-
0x11d4,
109-
0x9a3a,
110-
[0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d],
111-
);
76+
pub const HAND_OFF_BLOCK_LIST_GUID: Guid =
77+
Guid::from_values(0x7739f24c, 0x93d7, 0x11d4, 0x9a3a, 0x0090273fc14d);
11278

11379
/// Table used in the early boot environment to record memory ranges.
114-
pub const MEMORY_TYPE_INFORMATION_GUID: Guid = Guid::from_values(
115-
0x4c19049f,
116-
0x4137,
117-
0x4dd3,
118-
0x9c10,
119-
[0x8b, 0x97, 0xa8, 0x3f, 0xfd, 0xfa],
120-
);
80+
pub const MEMORY_TYPE_INFORMATION_GUID: Guid =
81+
Guid::from_values(0x4c19049f, 0x4137, 0x4dd3, 0x9c10, 0x8b97a83ffdfa);
12182

12283
/// Used to identify Hand-off Blocks which store
12384
/// status codes reported during the pre-UEFI environment.
124-
pub const MEMORY_STATUS_CODE_RECORD_GUID: Guid = Guid::from_values(
125-
0x60cc026,
126-
0x4c0d,
127-
0x4dda,
128-
0x8f41,
129-
[0x59, 0x5f, 0xef, 0x00, 0xa5, 0x02],
130-
);
85+
pub const MEMORY_STATUS_CODE_RECORD_GUID: Guid =
86+
Guid::from_values(0x60cc026, 0x4c0d, 0x4dda, 0x8f41, 0x595fef00a502);
13187

13288
/// Table which provides Driver eXecution Environment services.
133-
pub const DXE_SERVICES_GUID: Guid = Guid::from_values(
134-
0x5ad34ba,
135-
0x6f02,
136-
0x4214,
137-
0x952e,
138-
[0x4d, 0xa0, 0x39, 0x8e, 0x2b, 0xb9],
139-
);
89+
pub const DXE_SERVICES_GUID: Guid =
90+
Guid::from_values(0x5ad34ba, 0x6f02, 0x4214, 0x952e, 0x4da0398e2bb9);
14091

14192
/// LZMA-compressed filesystem.
142-
pub const LZMA_COMPRESS_GUID: Guid = Guid::from_values(
143-
0xee4e5898,
144-
0x3914,
145-
0x4259,
146-
0x9d6e,
147-
[0xdc, 0x7b, 0xd7, 0x94, 0x03, 0xcf],
148-
);
93+
pub const LZMA_COMPRESS_GUID: Guid =
94+
Guid::from_values(0xee4e5898, 0x3914, 0x4259, 0x9d6e, 0xdc7bd79403cf);
14995

15096
/// A custom compressed filesystem used by the Tiano UEFI implementation.
151-
pub const TIANO_COMPRESS_GUID: Guid = Guid::from_values(
152-
0xa31280ad,
153-
0x481e,
154-
0x41b6,
155-
0x95e8,
156-
[0x12, 0x7f, 0x4c, 0x98, 0x47, 0x79],
157-
);
97+
pub const TIANO_COMPRESS_GUID: Guid =
98+
Guid::from_values(0xa31280ad, 0x481e, 0x41b6, 0x95e8, 0x127f4c984779);
15899

159100
/// Pointer to the debug image info table.
160-
pub const DEBUG_IMAGE_INFO_GUID: Guid = Guid::from_values(
161-
0x49152e77,
162-
0x1ada,
163-
0x4764,
164-
0xb7a2,
165-
[0x7a, 0xfe, 0xfe, 0xd9, 0x5e, 0x8b],
166-
);
101+
pub const DEBUG_IMAGE_INFO_GUID: Guid =
102+
Guid::from_values(0x49152e77, 0x1ada, 0x4764, 0xb7a2, 0x7afefed95e8b);

src/table/runtime.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use core::fmt::{Debug, Formatter};
1414
use core::mem;
1515
use core::mem::MaybeUninit;
1616
use core::{fmt, ptr};
17-
1817
/// Contains pointers to all of the runtime services.
1918
///
2019
/// This table, and the function pointers it contains are valid
@@ -503,7 +502,7 @@ newtype_enum! {
503502
0x93ca,
504503
0x11d2,
505504
0xaa0d,
506-
[0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c],
505+
0x00e098032b8c,
507506
),
508507

509508
/// Used to access EFI signature database variables.
@@ -512,7 +511,7 @@ newtype_enum! {
512511
0x3d3a,
513512
0x4596,
514513
0xa3bc,
515-
[0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f],
514+
0xdad00e67656f,
516515
),
517516
}
518517
}

uefi-macros/src/lib.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,7 @@ pub fn unsafe_guid(args: TokenStream, input: TokenStream) -> TokenStream {
7373
let time_mid = next_guid_int(16) as u16;
7474
let time_high_and_version = next_guid_int(16) as u16;
7575
let clock_seq_and_variant = next_guid_int(16) as u16;
76-
let node_64 = next_guid_int(48);
77-
78-
// Convert the node ID to an array of bytes to comply with Guid::from_values expectations
79-
let node = [
80-
(node_64 >> 40) as u8,
81-
((node_64 >> 32) % 0x100) as u8,
82-
((node_64 >> 24) % 0x100) as u8,
83-
((node_64 >> 16) % 0x100) as u8,
84-
((node_64 >> 8) % 0x100) as u8,
85-
(node_64 % 0x100) as u8,
86-
];
76+
let node = next_guid_int(48);
8777

8878
// At this point, we know everything we need to implement Identify
8979
let ident = type_definition.ident.clone();
@@ -97,7 +87,7 @@ pub fn unsafe_guid(args: TokenStream, input: TokenStream) -> TokenStream {
9787
#time_mid,
9888
#time_high_and_version,
9989
#clock_seq_and_variant,
100-
[#(#node),*],
90+
#node,
10191
);
10292
}
10393
});

uefi-test-runner/src/runtime/vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn test_variables(rt: &RuntimeServices) {
1515
0xe187,
1616
0x497e,
1717
0xae77,
18-
[0x5b, 0xd8, 0xb0, 0xe0, 0x97, 0x03],
18+
0x5bd8b0e09703,
1919
));
2020

2121
info!("Testing set_variable");

0 commit comments

Comments
 (0)