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
326 #[bits(35)]
327 _reserved: u64,
328}
329
330#[derive(Clone, Copy)]
331pub enum ConsolePort {
332 Default = 0b00,
333 Com1 = 0b01,
334 Com2 = 0b10,
335 None = 0b11,
336}
337
338impl ConsolePort {
339 const fn from_bits(bits: u64) -> Self {
340 match bits {
341 0b00 => Self::Default,
342 0b01 => Self::Com1,
343 0b10 => Self::Com2,
344 0b11 => Self::None,
345 _ => unreachable!(),
346 }
347 }
348
349 const fn into_bits(self) -> u64 {
350 self as u64
351 }
352}
353
354pub enum MemoryProtection {
355 Disabled = 0b00,
356 Default = 0b01,
357 Strict = 0b10,
358 Relaxed = 0b11,
359}
360
361impl MemoryProtection {
362 const fn from_bits(bits: u64) -> Self {
363 match bits {
364 0b00 => Self::Disabled,
365 0b01 => Self::Default,
366 0b10 => Self::Strict,
367 0b11 => Self::Relaxed,
368 _ => unreachable!(),
369 }
370 }
371
372 const fn into_bits(self) -> u64 {
373 self as u64
374 }
375}
376
377#[repr(C)]
378#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
379pub struct ProcessorInformation {
380 pub max_processor_count: u32,
381 pub processor_count: u32,
382 pub processors_per_virtual_socket: u32,
383 pub threads_per_processor: u32,
384}
385
386#[repr(C)]
387#[derive(IntoBytes, Immutable, KnownLayout, FromBytes, Copy, Clone)]
388pub struct Mmio {
389 pub mmio_page_number_start: u64,
390 pub mmio_size_in_pages: u64,
391}
392
393#[repr(C)]
394#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
395pub struct MmioRanges(pub [Mmio; 2]);
396
397#[repr(C)]
398#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
399pub struct NvdimmCount {
400 pub count: u16,
401 pub padding: [u16; 3],
402}
403
404#[repr(C)]
405#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
406pub struct VpciInstanceFilter {
407 pub instance_guid: Guid,
408}
409
410#[repr(C)]
411#[derive(IntoBytes, Immutable, KnownLayout, FromBytes)]
412pub struct Gic {
413 pub gic_distributor_base: u64,
414 pub gic_redistributors_base: u64,
415}
416
417#[cfg(test)]
418mod tests {
419 use super::*;
420
421 fn read<T>(bytes: &[u8]) -> T
422 where
423 T: FromBytes + Immutable + KnownLayout,
424 {
425 T::read_from_prefix(bytes)
426 .expect("byte slice should always be big enough")
427 .0
428 }
429
430 fn add_one_dynamic(length: usize) {
431 let padded_length = align_8(length);
432 let madt = vec![0xCC; length];
433
434 let data = {
435 let mut blob = Blob::new();
436 blob.add_raw(BlobStructureType::Madt, &madt);
437 blob.complete()
438 };
439
440 assert_eq!(data.len() % 8, 0);
441
442 let header: Header = read(&data[..]);
443 let structure: StructureCount = read(&data[size_of::<Header>()..]);
444
445 let header_exp = Header {
446 structure_type: 0x00,
447 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
448 };
449 let structure_exp = StructureCount {
450 total_structure_count: 2,
451 total_config_blob_size: (2 * size_of::<Header>()
452 + size_of::<StructureCount>()
453 + padded_length) as u32,
454 };
455
456 assert_eq!(header.structure_type, header_exp.structure_type);
457 assert_eq!(header.length, header_exp.length);
458 assert_eq!(
459 structure.total_structure_count,
460 structure_exp.total_structure_count
461 );
462 assert_eq!(
463 structure.total_config_blob_size,
464 structure_exp.total_config_blob_size
465 );
466
467 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
468 let structure = &data[2 * size_of::<Header>() + size_of::<StructureCount>()..][..length];
469
470 let header_exp = Header {
471 structure_type: 0x18,
472 length: (size_of::<Header>() + padded_length) as u32,
473 };
474 let structure_exp = &madt[..];
475
476 assert_eq!(header.structure_type, header_exp.structure_type);
477 assert_eq!(header.length, header_exp.length);
478 assert_eq!(structure, structure_exp);
479 }
480
481 #[test]
482 fn add_none() {
483 let data = {
484 let blob = Blob::new();
485 blob.complete()
486 };
487
488 assert_eq!(data.len() % 8, 0);
489
490 let header: Header = read(&data[..]);
491 let structure: StructureCount = read(&data[size_of::<Header>()..]);
492
493 let header_exp = Header {
494 structure_type: 0x00,
495 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
496 };
497 let structure_exp = StructureCount {
498 total_structure_count: 1,
499 total_config_blob_size: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
500 };
501
502 assert_eq!(header.structure_type, header_exp.structure_type);
503 assert_eq!(header.length, header_exp.length);
504 assert_eq!(
505 structure.total_structure_count,
506 structure_exp.total_structure_count
507 );
508 assert_eq!(
509 structure.total_config_blob_size,
510 structure_exp.total_config_blob_size
511 );
512 }
513
514 #[test]
515 fn add_one_fixed() {
516 let biosinfo = BiosInformation {
517 bios_size_pages: 12345678,
518 flags: 1 << 31,
519 };
520
521 let data = {
522 let mut blob = Blob::new();
523 blob.add(&BiosInformation {
524 bios_size_pages: 12345678,
525 flags: 1 << 31,
526 });
527 blob.complete()
528 };
529
530 assert_eq!(data.len() % 8, 0);
531
532 let header: Header = read(&data[..]);
533 let structure: StructureCount = read(&data[size_of::<Header>()..]);
534
535 let header_exp = Header {
536 structure_type: 0x00,
537 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
538 };
539 let structure_exp = StructureCount {
540 total_structure_count: 2,
541 total_config_blob_size: (2 * size_of::<Header>()
542 + size_of::<StructureCount>()
543 + size_of::<BiosInformation>()) as u32,
544 };
545
546 assert_eq!(header.structure_type, header_exp.structure_type);
547 assert_eq!(header.length, header_exp.length);
548 assert_eq!(
549 structure.total_structure_count,
550 structure_exp.total_structure_count
551 );
552 assert_eq!(
553 structure.total_config_blob_size,
554 structure_exp.total_config_blob_size
555 );
556
557 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
558 let structure: BiosInformation =
559 read(&data[2 * size_of::<Header>() + size_of::<StructureCount>()..]);
560
561 let header_exp = Header {
562 structure_type: 0x01,
563 length: (size_of::<Header>() + size_of::<BiosInformation>()) as u32,
564 };
565
566 assert_eq!(header.structure_type, header_exp.structure_type);
567 assert_eq!(header.length, header_exp.length);
568 assert_eq!(structure.bios_size_pages, biosinfo.bios_size_pages);
569 assert_eq!(structure.flags, biosinfo.flags);
570 }
571
572 #[test]
573 fn add_one_dynamic_misaligned() {
574 add_one_dynamic(43);
575 }
576
577 #[test]
578 fn add_one_dynamic_aligned() {
579 add_one_dynamic(40);
580 }
581
582 #[test]
583 fn add_two() {
584 const LENGTH: usize = 93;
585 const PADDED_LENGTH: usize = 96;
586 let madt = vec![0xCC; LENGTH];
587 let procinfo = ProcessorInformation {
588 max_processor_count: 4,
589 processor_count: 3,
590 processors_per_virtual_socket: 2,
591 threads_per_processor: 1,
592 };
593
594 let data = {
595 let mut blob = Blob::new();
596 blob.add_raw(BlobStructureType::Madt, &madt).add(&procinfo);
597 blob.complete()
598 };
599
600 assert_eq!(data.len() % 8, 0);
601
602 let header: Header = read(&data[..]);
603 let structure: StructureCount = read(&data[size_of::<Header>()..]);
604
605 let header_exp = Header {
606 structure_type: 0x00,
607 length: (size_of::<Header>() + size_of::<StructureCount>()) as u32,
608 };
609 let structure_exp = StructureCount {
610 total_structure_count: 3,
611 total_config_blob_size: (3 * size_of::<Header>()
612 + size_of::<StructureCount>()
613 + PADDED_LENGTH
614 + size_of::<ProcessorInformation>()) as u32,
615 };
616
617 assert_eq!(header.structure_type, header_exp.structure_type);
618 assert_eq!(header.length, header_exp.length);
619 assert_eq!(
620 structure.total_structure_count,
621 structure_exp.total_structure_count
622 );
623 assert_eq!(
624 structure.total_config_blob_size,
625 structure_exp.total_config_blob_size
626 );
627
628 let header: Header = read(&data[size_of::<Header>() + size_of::<StructureCount>()..]);
629 let structure = &data[2 * size_of::<Header>() + size_of::<StructureCount>()..][..LENGTH];
630 let padding = &data[2 * size_of::<Header>() + size_of::<StructureCount>() + LENGTH..]
631 [..PADDED_LENGTH - LENGTH];
632
633 let header_exp = Header {
634 structure_type: 0x18,
635 length: (size_of::<Header>() + PADDED_LENGTH) as u32,
636 };
637 let structure_exp = &madt[..];
638
639 assert_eq!(header.structure_type, header_exp.structure_type);
640 assert_eq!(header.length, header_exp.length);
641 assert_eq!(structure.as_bytes(), structure_exp.as_bytes());
642 assert_eq!(padding, &[0; PADDED_LENGTH - LENGTH]);
643
644 let header: Header =
645 read(&data[2 * size_of::<Header>() + size_of::<StructureCount>() + PADDED_LENGTH..]);
646 let structure: ProcessorInformation =
647 read(&data[3 * size_of::<Header>() + size_of::<StructureCount>() + PADDED_LENGTH..]);
648
649 let header_exp = Header {
650 structure_type: 0x13,
651 length: (size_of::<Header>() + size_of::<ProcessorInformation>()) as u32,
652 };
653
654 assert_eq!(header.structure_type, header_exp.structure_type);
655 assert_eq!(header.length, header_exp.length);
656 assert_eq!(structure.max_processor_count, procinfo.max_processor_count);
657 assert_eq!(structure.processor_count, procinfo.processor_count);
658 assert_eq!(
659 structure.processors_per_virtual_socket,
660 procinfo.processors_per_virtual_socket
661 );
662 assert_eq!(
663 structure.threads_per_processor,
664 procinfo.threads_per_processor
665 );
666 }
667}