|
| 1 | +use crate::TagType; |
| 2 | +#[cfg(feature = "builder")] |
| 3 | +use crate::builder::boxed_dst_tag; |
| 4 | +#[cfg(feature = "builder")] |
| 5 | +use crate::builder::traits::StructAsBytes; |
| 6 | + |
| 7 | +use core::convert::TryInto; |
| 8 | +use core::fmt::Debug; |
| 9 | + |
| 10 | +#[cfg(feature = "builder")] |
| 11 | +use alloc::boxed::Box; |
| 12 | + |
| 13 | +#[cfg(test)] |
| 14 | +const METADATA_SIZE: usize = core::mem::size_of::<TagType>() |
| 15 | + + core::mem::size_of::<u32>() + core::mem::size_of::<u8>() * 8; |
| 16 | + |
| 17 | + |
| 18 | +/// This tag contains a copy of SMBIOS tables as well as their version. |
| 19 | +#[repr(C, packed)] |
| 20 | +pub struct SmbiosTag { |
| 21 | + typ: TagType, |
| 22 | + size: u32, |
| 23 | + pub major: u8, |
| 24 | + pub minor: u8, |
| 25 | + _reserved: [u8; 6], |
| 26 | + pub tables: [u8], |
| 27 | +} |
| 28 | + |
| 29 | +impl SmbiosTag { |
| 30 | + #[cfg(feature = "builder")] |
| 31 | + pub fn new(major: u8, minor: u8, tables: &[u8]) -> Box<Self> { |
| 32 | + let mut bytes = [major, minor].to_vec(); |
| 33 | + bytes.extend(tables); |
| 34 | + let tag = boxed_dst_tag( |
| 35 | + TagType::Smbios, bytes.as_slice(), |
| 36 | + ); |
| 37 | + unsafe { Box::from_raw(Box::into_raw(tag) as *mut Self) } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(feature = "builder")] |
| 42 | +impl StructAsBytes for SmbiosTag { |
| 43 | + fn byte_size(&self) -> usize { |
| 44 | + self.size.try_into().unwrap() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl Debug for SmbiosTag { |
| 49 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 50 | + f.debug_struct("BootLoaderNameTag") |
| 51 | + .field("typ", &{self.typ}) |
| 52 | + .field("size", &{self.size}) |
| 53 | + .field("major", &{self.major}) |
| 54 | + .field("minor", &{self.minor}) |
| 55 | + .finish() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use core::ptr::slice_from_raw_parts; |
| 62 | + |
| 63 | + use crate::{TagType, smbios::METADATA_SIZE}; |
| 64 | + |
| 65 | + /// Returns the tag structure in bytes in native endian format. |
| 66 | + fn get_bytes() -> std::vec::Vec<u8> { |
| 67 | + let tables = [0xabu8; 24]; |
| 68 | + // size is: 4 bytes for tag + 4 bytes for size + 1 byte for major and minor |
| 69 | + // + 6 bytes reserved + the actual tables |
| 70 | + let size = (4 + 4 + 1 + 1 + 6 + tables.len()) as u32; |
| 71 | + let mut bytes = [ |
| 72 | + ((TagType::Smbios as u32).to_ne_bytes()), size.to_ne_bytes() |
| 73 | + ].concat(); |
| 74 | + bytes.push(3); |
| 75 | + bytes.push(0); |
| 76 | + bytes.extend([0; 6]); |
| 77 | + bytes.extend(tables); |
| 78 | + bytes |
| 79 | + } |
| 80 | + |
| 81 | + /// Tests to parse a string with a terminating null byte from the tag (as the spec defines). |
| 82 | + #[test] |
| 83 | + fn test_parse() { |
| 84 | + let tag = get_bytes(); |
| 85 | + let tag = unsafe { |
| 86 | + let ptr = tag.as_ptr() as *const (); |
| 87 | + (slice_from_raw_parts( |
| 88 | + ptr, tag.len() - METADATA_SIZE |
| 89 | + ) as *const super::SmbiosTag) |
| 90 | + .as_ref() |
| 91 | + .unwrap() |
| 92 | + }; |
| 93 | + assert_eq!({ tag.typ }, TagType::Smbios); |
| 94 | + assert_eq!(tag.major, 3); |
| 95 | + assert_eq!(tag.minor, 0); |
| 96 | + assert_eq!(tag.tables, [0xabu8; 24]); |
| 97 | + } |
| 98 | +} |
0 commit comments