1use bitfield_struct::bitfield;
7use core::mem::size_of;
8use guid::Guid;
9use zerocopy::FromBytes;
10use zerocopy::Immutable;
11use zerocopy::IntoBytes;
12use zerocopy::KnownLayout;
13
14fn align_8(x: usize) -> usize {
15 (x + 7) & !7
16}
17
18#[derive(Debug)]
20pub struct Blob {
21 data: Vec<u8>,
22 count: u32,
23}
24
25impl Blob {
26 pub fn new() -> Self {
28 let mut blob = Self {
29 data: Vec::new(),
30 count: 0,
31 };
32 blob.add(&StructureCount {
33 total_structure_count: 0,
34 total_config_blob_size: 0,
35 });
36 blob
37 }
38
39 pub fn add<T: BlobStructure>(&mut self, data: &T) -> &mut Self {
41 self.add_raw(T::STRUCTURE_TYPE, data.as_bytes())
42 }
43
44 pub fn add_cstring(&mut self, structure_type: BlobStructureType, data: &[u8]) -> &mut Self {
53 if !data.is_empty() {
54 self.add_raw_inner(structure_type, data, !data.ends_with(&[0]))
55 } else {
56 self
57 }
58 }
59
60 pub fn add_raw(&mut self, structure_type: BlobStructureType, data: &[u8]) -> &mut Self {
63 self.add_raw_inner(structure_type, data, false)
64 }
65
66 fn add_raw_inner(
67 &mut self,
68 structure_type: BlobStructureType,
69 data: &[u8],
70 add_null_term: bool,
71 ) -> &mut Self {
72 let aligned_data_len = align_8(data.len() + add_null_term as usize);
74 self.data.extend_from_slice(
75 Header {
76 structure_type: structure_type as u32,
77 length: (size_of::<Header>() + aligned_data_len) as u32,
78 }
79 .as_bytes(),
80 );
81 self.data.extend_from_slice(data);
82 if add_null_term {
83 self.data.push(0);
84 }
85 self.data
87 .extend_from_slice(&[0; 7][..aligned_data_len - (data.len() + add_null_term as usize)]);
88 self.count += 1;
89 self
90 }
91
92 pub fn complete(mut self) -> Vec<u8> {
95 let total_config_blob_size = self.data.len() as u32;
96 self.data[size_of::<Header>()..size_of::<Header>() + size_of::<StructureCount>()]
97 .copy_from_slice(
98 StructureCount {
99 total_structure_count: self.count,
100 total_config_blob_size,
101 }
102 .as_bytes(),
103 );
104 self.data
105 }
106}
107
108impl Default for Blob {
109 fn default() -> Self {
110 Self::new()
111 }
112}
113
114pub trait BlobStructure: IntoBytes + FromBytes + Immutable + KnownLayout {
115 const STRUCTURE_TYPE: BlobStructureType;
116}
117
118macro_rules! blobtypes {
119 {
120 $($name:ident,)*
121 } => {
122 $(
123 impl BlobStructure for $name {
124 const STRUCTURE_TYPE: BlobStructureType = BlobStructureType::$name;
125 }
126 )*
127 }
128}
129
130blobtypes! {
131 StructureCount,
132 BiosInformation,
133 Entropy,
134 BiosGuid,
135 Smbios31ProcessorInformation,
136 Flags,
137 ProcessorInformation,
138 MmioRanges,
139 NvdimmCount,
140 VpciInstanceFilter,
141 Gic,
142}
143
144#[repr(u32)]
146pub enum BlobStructureType {
147 StructureCount = 0x00,
148 BiosInformation = 0x01,
149 Srat = 0x02,
150 MemoryMap = 0x03,
151 Entropy = 0x04,
152 BiosGuid = 0x05,
153 SmbiosSystemSerialNumber = 0x06,
154 SmbiosBaseSerialNumber = 0x07,
155 SmbiosChassisSerialNumber = 0x08,
156 SmbiosChassisAssetTag = 0x09,
157 SmbiosBiosLockString = 0x0A,
158 Smbios31ProcessorInformation = 0x0B,
159 SmbiosSocketDesignation = 0x0C,
160 SmbiosProcessorManufacturer = 0x0D,
161 SmbiosProcessorVersion = 0x0E,
162 SmbiosProcessorSerialNumber = 0x0F,
163 SmbiosProcessorAssetTag = 0x10,
164 SmbiosProcessorPartNumber = 0x11,
165 Flags = 0x12,
166 ProcessorInformation = 0x13,
167 MmioRanges = 0x14,
168 Aarch64Mpidr = 0x15,
169 AcpiTable = 0x16,
170 NvdimmCount = 0x17,
171 Madt = 0x18,
172 VpciInstanceFilter = 0x19,
173 SmbiosSystemManufacturer = 0x1A,
174 SmbiosSystemProductName = 0x1B,
175 SmbiosSystemVersion = 0x1C,
176 SmbiosSystemSkuNumber = 0x1D,
177 SmbiosSystemFamily = 0x1E,
178 SmbiosMemoryDeviceSerialNumber = 0x1F,
179 Slit = 0x20,
180 Aspt = 0x21,
181 Pptt = 0x22,
182 Gic = 0x23,
183 Mcfg = 0x24,
184 Ssdt = 0x25,
185 Hmat = 0x26,
186 Iort = 0x27,
187}
188
189#[repr(C)]
204#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
205pub struct Header {
206 pub structure_type: u32,
207 pub length: u32,
208}
209
210#[repr(C)]
217#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
218pub struct StructureCount {
219 pub total_structure_count: u32,
220 pub total_config_blob_size: u32,
221}
222
223#[repr(C)]
224#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
225pub struct BiosInformation {
226 pub bios_size_pages: u32,
227 pub flags: u32,
232}
233
234pub const VM_MEMORY_RANGE_FLAG_PLATFORM_RESERVED: u32 = 0x1;
247pub const VM_MEMORY_RANGE_FLAG_PERSISTENT: u32 = 0x2;
248pub const VM_MEMORY_RANGE_FLAG_SPECIFIC_PURPOSE: u32 = 0x4;
249
250#[repr(C)]
251#[derive(Debug, IntoBytes, Immutable, KnownLayout, FromBytes)]
252pub struct MemoryRangeV5 {
253 pub base_address: u64,
254 pub length: u64,
255 pub flags: u32,
256 pub reserved: u32,
257}
258
259#[repr(C)]
260#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
261pub struct Entropy(pub [u8; 64]);
262
263impl Default for Entropy {
264 fn default() -> Self {
265 Entropy([0; 64])
266 }
267}
268
269#[repr(C)]
270#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
271pub struct BiosGuid(pub Guid);
272
273#[repr(C)]
274#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
275pub struct Smbios31ProcessorInformation {
276 pub processor_id: u64,
277 pub external_clock: u16,
278 pub max_speed: u16,
279 pub current_speed: u16,
280 pub processor_characteristics: u16,
281 pub processor_family2: u16,
282 pub processor_type: u8,
283 pub voltage: u8,
284 pub status: u8,
285 pub processor_upgrade: u8,
286 pub reserved: u16,
287}
288
289#[bitfield(u64, debug = false)]
290#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
291pub struct Flags {
292 pub serial_controllers_enabled: bool,
293 pub pause_after_boot_failure: bool,
294 pub pxe_ip_v6: bool,
295 pub debugger_enabled: bool,
296 pub load_oemp_table: bool,
297 pub tpm_enabled: bool,
298 pub hibernate_enabled: bool,
299
300 #[bits(2)]
301 pub console: ConsolePort,
302
303 pub memory_attributes_table_enabled: bool,
304 pub virtual_battery_enabled: bool,
305 pub sgx_memory_enabled: bool,
306 pub is_vmbfs_boot: bool,
307 pub measure_additional_pcrs: bool,
308 pub disable_frontpage: bool,
309 pub default_boot_always_attempt: bool,
310 pub low_power_s0_idle_enabled: bool,
311 pub vpci_boot_enabled: bool,
312 pub proc_idle_enabled: bool,
313 pub disable_sha384_pcr: bool,
314 pub media_present_enabled_by_default: bool,
315
316 #[bits(2)]
317 pub memory_protection: MemoryProtection,
318
319 pub enable_imc_when_isolated: bool,
320 pub watchdog_enabled: bool,
321 pub tpm_locality_regs_enabled: bool,
322 pub dhcp6_link_layer_address: bool,
323 pub cxl_memory_enabled: bool,
324 pub mtrrs_initialized_at_load: bool,
325 pub hv_sint_enabled: bool,
326
327 #[bits(34)]
328 _reserved: u64,
329}
330
331#[derive(Clone, Copy)]
332pub enum ConsolePort {
333 Default = 0b00,
334 Com1 = 0b01,
335 Com2 = 0b10,
336 None = 0b11,
337}
338
339impl ConsolePort {
340 const fn from_bits(bits: u64) -> Self {
341 match bits {
342 0b00 => Self::Default,
343 0b01 => Self::Com1,
344 0b10 => Self::Com2,
345 0b11 => Self::None,
346 _ => unreachable!(),
347 }
348 }
349
350 const fn into_bits(self) -> u64 {
351 self as u64
352 }
353}
354
355pub enum MemoryProtection {
356 Disabled = 0b00,
357 Default = 0b01,
358 Strict = 0b10,
359 Relaxed = 0b11,
360}
361
362impl MemoryProtection {
363 const fn from_bits(bits: u64) -> Self {
364 match bits {
365 0b00 => Self::Disabled,
366 0b01 => Self::Default,
367 0b10 => Self::Strict,
368 0b11 => Self::Relaxed,
369 _ => unreachable!(),
370 }
371 }
372
373 const fn into_bits(self) -> u64 {
374 self as u64
375 }
376}
377
378#[repr(C)]
379#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
380pub struct ProcessorInformation {
381 pub max_processor_count: u32,
382 pub processor_count: u32,
383 pub processors_per_virtual_socket: u32,
384 pub threads_per_processor: u32,
385}
386
387#[repr(C)]
388#[derive(IntoBytes, Immutable, KnownLayout, FromBytes, Copy, Clone)]
389pub struct Mmio {
390 pub mmio_page_number_start: u64,
391 pub mmio_size_in_pages: u64,
392}
393
394#[repr(C)]
395#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
396pub struct MmioRanges(pub [Mmio; 2]);
397
398#[repr(C)]
399#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
400pub struct NvdimmCount {
401 pub count: u16,
402 pub padding: [u16; 3],
403}
404
405#[repr(C)]
406#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
407pub struct VpciInstanceFilter {
408 pub instance_guid: Guid,
409}
410
411#[repr(C)]
412#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
413pub struct Gic {
414 pub gic_distributor_base: u64,
415 pub gic_redistributors_base: u64,
416}
417
418#[cfg(test)]
419mod tests {
420 use super::*;
421
422 fn read<T>(bytes: &[u8]) -> T
423 where
424 T: FromBytes + Immutable + KnownLayout,
425 {
426 T::read_from_prefix(bytes)
427 .expect("byte slice should always be big enough")
428 .0
429 }
430
431 fn add_one_dynamic(length: usize) {
432 let padded_length = align_8(length);
433 let madt = vec![0xCC; length];
434
435 let data = {
436 let mut blob = Blob::new();
437 blob.add_raw(BlobStructureType::Madt, &madt);
438 blob.complete()
439 };
440
441 assert_eq!(data.len() % 8, 0);
442
443 let header: Header = read(&data[..]);
444 let structure: StructureCount = read(&data[size_of::<Header>()..]);
445
446 let header_exp = Header {
447 structure_type: 0x00,
448 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
449 };
450 let structure_exp = StructureCount {
451 total_structure_count: 2,
452 total_config_blob_size: (2 * size_of::<Header>()
453 + size_of::<StructureCount>()
454 + padded_length) as u32,
455 };
456
457 assert_eq!(header.structure_type, header_exp.structure_type);
458 assert_eq!(header.length, header_exp.length);
459 assert_eq!(
460 structure.total_structure_count,
461 structure_exp.total_structure_count
462 );
463 assert_eq!(
464 structure.total_config_blob_size,
465 structure_exp.total_config_blob_size
466 );
467
468 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
469 let structure = &data[2 * size_of::<Header>() + size_of::<StructureCount>()..][..length];
470
471 let header_exp = Header {
472 structure_type: 0x18,
473 length: (size_of::<Header>() + padded_length) as u32,
474 };
475 let structure_exp = &madt[..];
476
477 assert_eq!(header.structure_type, header_exp.structure_type);
478 assert_eq!(header.length, header_exp.length);
479 assert_eq!(structure, structure_exp);
480 }
481
482 #[test]
483 fn add_none() {
484 let data = {
485 let blob = Blob::new();
486 blob.complete()
487 };
488
489 assert_eq!(data.len() % 8, 0);
490
491 let header: Header = read(&data[..]);
492 let structure: StructureCount = read(&data[size_of::<Header>()..]);
493
494 let header_exp = Header {
495 structure_type: 0x00,
496 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
497 };
498 let structure_exp = StructureCount {
499 total_structure_count: 1,
500 total_config_blob_size: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
501 };
502
503 assert_eq!(header.structure_type, header_exp.structure_type);
504 assert_eq!(header.length, header_exp.length);
505 assert_eq!(
506 structure.total_structure_count,
507 structure_exp.total_structure_count
508 );
509 assert_eq!(
510 structure.total_config_blob_size,
511 structure_exp.total_config_blob_size
512 );
513 }
514
515 #[test]
516 fn add_one_fixed() {
517 let biosinfo = BiosInformation {
518 bios_size_pages: 12345678,
519 flags: 1 << 31,
520 };
521
522 let data = {
523 let mut blob = Blob::new();
524 blob.add(&BiosInformation {
525 bios_size_pages: 12345678,
526 flags: 1 << 31,
527 });
528 blob.complete()
529 };
530
531 assert_eq!(data.len() % 8, 0);
532
533 let header: Header = read(&data[..]);
534 let structure: StructureCount = read(&data[size_of::<Header>()..]);
535
536 let header_exp = Header {
537 structure_type: 0x00,
538 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
539 };
540 let structure_exp = StructureCount {
541 total_structure_count: 2,
542 total_config_blob_size: (2 * size_of::<Header>()
543 + size_of::<StructureCount>()
544 + size_of::<BiosInformation>()) as u32,
545 };
546
547 assert_eq!(header.structure_type, header_exp.structure_type);
548 assert_eq!(header.length, header_exp.length);
549 assert_eq!(
550 structure.total_structure_count,
551 structure_exp.total_structure_count
552 );
553 assert_eq!(
554 structure.total_config_blob_size,
555 structure_exp.total_config_blob_size
556 );
557
558 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
559 let structure: BiosInformation =
560 read(&data[2 * size_of::<Header>() + size_of::<StructureCount>()..]);
561
562 let header_exp = Header {
563 structure_type: 0x01,
564 length: (size_of::<Header>() + size_of::<BiosInformation>()) as u32,
565 };
566
567 assert_eq!(header.structure_type, header_exp.structure_type);
568 assert_eq!(header.length, header_exp.length);
569 assert_eq!(structure.bios_size_pages, biosinfo.bios_size_pages);
570 assert_eq!(structure.flags, biosinfo.flags);
571 }
572
573 #[test]
574 fn add_one_dynamic_misaligned() {
575 add_one_dynamic(43);
576 }
577
578 #[test]
579 fn add_one_dynamic_aligned() {
580 add_one_dynamic(40);
581 }
582
583 #[test]
584 fn add_two() {
585 const LENGTH: usize = 93;
586 const PADDED_LENGTH: usize = 96;
587 let madt = vec![0xCC; LENGTH];
588 let procinfo = ProcessorInformation {
589 max_processor_count: 4,
590 processor_count: 3,
591 processors_per_virtual_socket: 2,
592 threads_per_processor: 1,
593 };
594
595 let data = {
596 let mut blob = Blob::new();
597 blob.add_raw(BlobStructureType::Madt, &madt).add(&procinfo);
598 blob.complete()
599 };
600
601 assert_eq!(data.len() % 8, 0);
602
603 let header: Header = read(&data[..]);
604 let structure: StructureCount = read(&data[size_of::<Header>()..]);
605
606 let header_exp = Header {
607 structure_type: 0x00,
608 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
609 };
610 let structure_exp = StructureCount {
611 total_structure_count: 3,
612 total_config_blob_size: (3 * size_of::<Header>()
613 + size_of::<StructureCount>()
614 + PADDED_LENGTH
615 + size_of::<ProcessorInformation>()) as u32,
616 };
617
618 assert_eq!(header.structure_type, header_exp.structure_type);
619 assert_eq!(header.length, header_exp.length);
620 assert_eq!(
621 structure.total_structure_count,
622 structure_exp.total_structure_count
623 );
624 assert_eq!(
625 structure.total_config_blob_size,
626 structure_exp.total_config_blob_size
627 );
628
629 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
630 let structure = &data[2 * size_of::<Header>() + size_of::<StructureCount>()..][..LENGTH];
631 let padding = &data[2 * size_of::<Header>() + size_of::<StructureCount>() + LENGTH..]
632 [..PADDED_LENGTH - LENGTH];
633
634 let header_exp = Header {
635 structure_type: 0x18,
636 length: (size_of::<Header>() + PADDED_LENGTH) as u32,
637 };
638 let structure_exp = &madt[..];
639
640 assert_eq!(header.structure_type, header_exp.structure_type);
641 assert_eq!(header.length, header_exp.length);
642 assert_eq!(structure.as_bytes(), structure_exp.as_bytes());
643 assert_eq!(padding, &[0; PADDED_LENGTH - LENGTH]);
644
645 let header: Header =
646 read(&data[2 * size_of::<Header>() + size_of::<StructureCount>() + PADDED_LENGTH..]);
647 let structure: ProcessorInformation =
648 read(&data[3 * size_of::<Header>() + size_of::<StructureCount>() + PADDED_LENGTH..]);
649
650 let header_exp = Header {
651 structure_type: 0x13,
652 length: (size_of::<Header>() + size_of::<ProcessorInformation>()) as u32,
653 };
654
655 assert_eq!(header.structure_type, header_exp.structure_type);
656 assert_eq!(header.length, header_exp.length);
657 assert_eq!(structure.max_processor_count, procinfo.max_processor_count);
658 assert_eq!(structure.processor_count, procinfo.processor_count);
659 assert_eq!(
660 structure.processors_per_virtual_socket,
661 procinfo.processors_per_virtual_socket
662 );
663 assert_eq!(
664 structure.threads_per_processor,
665 procinfo.threads_per_processor
666 );
667 }
668}