|
| 1 | +use super::traits::StructAsBytes; |
| 2 | +use crate::InformationRequestHeaderTag; |
| 3 | +use crate::{HeaderTagFlag, MbiTagType}; |
| 4 | +use alloc::collections::BTreeSet; |
| 5 | +use alloc::vec::Vec; |
| 6 | +use core::fmt::Debug; |
| 7 | +use core::mem::size_of; |
| 8 | + |
| 9 | +/// Helper to build the dynamically sized [`InformationRequestHeaderTag`] |
| 10 | +/// at runtime. The information request tag has a dedicated builder because this way one |
| 11 | +/// can dynamically attach several requests to it. Otherwise, the number of requested tags |
| 12 | +/// must be known at compile time. |
| 13 | +#[derive(Debug)] |
| 14 | +#[cfg(feature = "builder")] |
| 15 | +pub struct InformationRequestHeaderTagBuilder { |
| 16 | + flag: HeaderTagFlag, |
| 17 | + // information requests (irs) |
| 18 | + irs: BTreeSet<MbiTagType>, |
| 19 | +} |
| 20 | + |
| 21 | +#[cfg(feature = "builder")] |
| 22 | +impl InformationRequestHeaderTagBuilder { |
| 23 | + /// New builder. |
| 24 | + pub fn new(flag: HeaderTagFlag) -> Self { |
| 25 | + Self { |
| 26 | + irs: BTreeSet::new(), |
| 27 | + flag, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /// Returns the expected length of the information request tag, |
| 32 | + /// when the `build`-method gets called. |
| 33 | + pub fn expected_len(&self) -> usize { |
| 34 | + let basic_header_size = size_of::<InformationRequestHeaderTag<0>>(); |
| 35 | + let req_tags_size = self.irs.len() * size_of::<MbiTagType>(); |
| 36 | + basic_header_size + req_tags_size |
| 37 | + } |
| 38 | + |
| 39 | + /// Adds an [`MbiTagType`] to the information request. |
| 40 | + pub fn add_ir(mut self, tag: MbiTagType) -> Self { |
| 41 | + self.irs.insert(tag); |
| 42 | + self |
| 43 | + } |
| 44 | + |
| 45 | + /// Adds multiple [`MbiTagType`] to the information request. |
| 46 | + pub fn add_irs(mut self, tags: &[MbiTagType]) -> Self { |
| 47 | + self.irs.extend(tags); |
| 48 | + self |
| 49 | + } |
| 50 | + |
| 51 | + /// Builds the bytes of the dynamically sized information request header. |
| 52 | + pub fn build(self) -> Vec<u8> { |
| 53 | + let expected_len = self.expected_len(); |
| 54 | + let mut data = Vec::with_capacity(expected_len); |
| 55 | + |
| 56 | + let basic_tag = InformationRequestHeaderTag::<0>::new( |
| 57 | + self.flag, |
| 58 | + [], |
| 59 | + // we put the expected length here already, because in the next step we write |
| 60 | + // all the tags into the byte array. We can't know this during compile time, |
| 61 | + // therefore N is 0. |
| 62 | + Some(expected_len as u32), |
| 63 | + ); |
| 64 | + data.extend(basic_tag.struct_as_bytes()); |
| 65 | + #[cfg(debug_assertions)] |
| 66 | + { |
| 67 | + let basic_tag_size = size_of::<InformationRequestHeaderTag<0>>(); |
| 68 | + assert_eq!( |
| 69 | + data.len(), |
| 70 | + basic_tag_size, |
| 71 | + "the vector must be as long as the basic tag!" |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + for tag in &self.irs { |
| 76 | + let bytes: [u8; 4] = (*tag as u32).to_ne_bytes(); |
| 77 | + data.extend(&bytes); |
| 78 | + } |
| 79 | + |
| 80 | + debug_assert_eq!( |
| 81 | + data.len(), |
| 82 | + expected_len, |
| 83 | + "the byte vector must be as long as the expected size of the struct" |
| 84 | + ); |
| 85 | + |
| 86 | + data |
| 87 | + } |
| 88 | +} |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use crate::builder::information_request::InformationRequestHeaderTagBuilder; |
| 92 | + use crate::{HeaderTagFlag, InformationRequestHeaderTag, MbiTagType}; |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_builder() { |
| 96 | + let builder = InformationRequestHeaderTagBuilder::new(HeaderTagFlag::Required) |
| 97 | + .add_ir(MbiTagType::EfiMmap) |
| 98 | + .add_ir(MbiTagType::BootLoaderName) |
| 99 | + .add_ir(MbiTagType::Cmdline); |
| 100 | + // type(u16) + flags(u16) + size(u32) + 3 tags (u32) |
| 101 | + assert_eq!(builder.expected_len(), 2 + 2 + 4 + 3 * 4); |
| 102 | + let tag = builder.build(); |
| 103 | + let tag = unsafe { |
| 104 | + (tag.as_ptr() as *const InformationRequestHeaderTag<3>) |
| 105 | + .as_ref() |
| 106 | + .unwrap() |
| 107 | + }; |
| 108 | + assert_eq!(tag.flags(), HeaderTagFlag::Required); |
| 109 | + // type(u16) + flags(u16) + size(u32) + 3 tags (u32) |
| 110 | + assert_eq!(tag.size(), 2 + 2 + 4 + 3 * 4); |
| 111 | + assert_eq!(tag.dynamic_requests_size(), 3); |
| 112 | + assert!(tag.requests().contains(&MbiTagType::EfiMmap)); |
| 113 | + assert!(tag.requests().contains(&MbiTagType::BootLoaderName)); |
| 114 | + assert!(tag.requests().contains(&MbiTagType::Cmdline)); |
| 115 | + assert_eq!(tag.requests().len(), 3); |
| 116 | + assert!(!tag.requests().contains(&MbiTagType::AcpiV1)); |
| 117 | + println!("{:#?}", tag); |
| 118 | + } |
| 119 | +} |
0 commit comments