Kaynağa Gözat

multiboot2: introduce new TagHeader type

This is a preparation for following refactorings.
Philipp Schuster 8 ay önce
ebeveyn
işleme
ec05bf5663

+ 2 - 0
multiboot2/Changelog.md

@@ -5,6 +5,8 @@
 - updated dependencies
 - MSRV is 1.75
 - doc fixes
+- Introduced new `TagHeader` type as replacement for the `Tag` type that will
+  be changed in the next step.
 
 ## 0.20.2 (2024-05-26)
 

+ 4 - 5
multiboot2/src/boot_loader_name.rs

@@ -1,6 +1,6 @@
 //! Module for [`BootLoaderNameTag`].
 
-use crate::tag::StringError;
+use crate::tag::{StringError, TagHeader};
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::{Debug, Formatter};
 use core::mem::size_of;
@@ -13,8 +13,7 @@ const METADATA_SIZE: usize = size_of::<TagTypeId>() + size_of::<u32>();
 #[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct BootLoaderNameTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     /// Null-terminated UTF-8 string
     name: [u8],
 }
@@ -55,8 +54,8 @@ impl BootLoaderNameTag {
 impl Debug for BootLoaderNameTag {
     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("BootLoaderNameTag")
-            .field("typ", &{ self.typ })
-            .field("size", &{ self.size })
+            .field("typ", &self.header.typ)
+            .field("size", &self.header.size)
             .field("name", &self.name())
             .finish()
     }

+ 4 - 6
multiboot2/src/command_line.rs

@@ -1,8 +1,7 @@
 //! Module for [`CommandLineTag`].
 
+use crate::tag::{StringError, TagHeader};
 use crate::{Tag, TagTrait, TagType, TagTypeId};
-
-use crate::tag::StringError;
 use core::fmt::{Debug, Formatter};
 use core::mem;
 use core::str;
@@ -18,8 +17,7 @@ pub(crate) const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + mem::size_
 #[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct CommandLineTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     /// Null-terminated UTF-8 string
     cmdline: [u8],
 }
@@ -63,8 +61,8 @@ impl CommandLineTag {
 impl Debug for CommandLineTag {
     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("CommandLineTag")
-            .field("typ", &{ self.typ })
-            .field("size", &{ self.size })
+            .field("typ", &self.header.typ)
+            .field("size", &self.header.size)
             .field("cmdline", &self.cmdline())
             .finish()
     }

+ 11 - 21
multiboot2/src/efi.rs

@@ -6,7 +6,7 @@
 //! - [`EFIImageHandle64Tag`]
 //! - [`EFIBootServicesNotExitedTag`]
 
-use crate::TagTypeId;
+use crate::tag::TagHeader;
 use crate::{Tag, TagTrait, TagType};
 use core::mem::size_of;
 
@@ -14,8 +14,7 @@ use core::mem::size_of;
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFISdt32Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     pointer: u32,
 }
 
@@ -23,8 +22,7 @@ impl EFISdt32Tag {
     /// Create a new tag to pass the EFI32 System Table pointer.
     pub fn new(pointer: u32) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             pointer,
         }
     }
@@ -45,8 +43,7 @@ impl TagTrait for EFISdt32Tag {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFISdt64Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     pointer: u64,
 }
 
@@ -54,8 +51,7 @@ impl EFISdt64Tag {
     /// Create a new tag to pass the EFI64 System Table pointer.
     pub fn new(pointer: u64) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             pointer,
         }
     }
@@ -77,8 +73,7 @@ impl TagTrait for EFISdt64Tag {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFIImageHandle32Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     pointer: u32,
 }
 
@@ -86,8 +81,7 @@ impl EFIImageHandle32Tag {
     #[cfg(feature = "builder")]
     pub fn new(pointer: u32) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             pointer,
         }
     }
@@ -109,8 +103,7 @@ impl TagTrait for EFIImageHandle32Tag {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFIImageHandle64Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     pointer: u64,
 }
 
@@ -118,8 +111,7 @@ impl EFIImageHandle64Tag {
     #[cfg(feature = "builder")]
     pub fn new(pointer: u64) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             pointer,
         }
     }
@@ -140,8 +132,7 @@ impl TagTrait for EFIImageHandle64Tag {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFIBootServicesNotExitedTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
 }
 
 impl EFIBootServicesNotExitedTag {
@@ -155,8 +146,7 @@ impl EFIBootServicesNotExitedTag {
 impl Default for EFIBootServicesNotExitedTag {
     fn default() -> Self {
         Self {
-            typ: TagType::EfiBs.into(),
-            size: core::mem::size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
         }
     }
 }

+ 16 - 17
multiboot2/src/framebuffer.rs

@@ -1,5 +1,6 @@
 //! Module for [`FramebufferTag`].
 
+use crate::tag::TagHeader;
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::Debug;
 use core::mem::size_of;
@@ -51,8 +52,7 @@ const METADATA_SIZE: usize = size_of::<TagTypeId>()
 #[derive(ptr_meta::Pointee, Eq)]
 #[repr(C)]
 pub struct FramebufferTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
 
     /// Contains framebuffer physical address.
     ///
@@ -185,13 +185,13 @@ impl TagTrait for FramebufferTag {
 impl Debug for FramebufferTag {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("FramebufferTag")
-            .field("typ", &{ self.typ })
-            .field("size", &{ self.size })
+            .field("typ", &self.header.typ)
+            .field("size", &self.header.size)
             .field("buffer_type", &self.buffer_type())
-            .field("address", &{ self.address })
-            .field("pitch", &{ self.pitch })
-            .field("width", &{ self.width })
-            .field("height", &{ self.height })
+            .field("address", &self.address)
+            .field("pitch", &self.pitch)
+            .field("width", &self.width)
+            .field("height", &self.height)
             .field("bpp", &self.bpp)
             .finish()
     }
@@ -199,15 +199,14 @@ impl Debug for FramebufferTag {
 
 impl PartialEq for FramebufferTag {
     fn eq(&self, other: &Self) -> bool {
-        ({ self.typ } == { other.typ }
-            && { self.size } == { other.size }
-            && { self.address } == { other.address }
-            && { self.pitch } == { other.pitch }
-            && { self.width } == { other.width }
-            && { self.height } == { other.height }
-            && { self.bpp } == { other.bpp }
-            && { self.type_no } == { other.type_no }
-            && self.buffer == other.buffer)
+        self.header == other.header
+            && self.address == { other.address }
+            && self.pitch == { other.pitch }
+            && self.width == { other.width }
+            && self.height == { other.height }
+            && self.bpp == { other.bpp }
+            && self.type_no == { other.type_no }
+            && self.buffer == other.buffer
     }
 }
 

+ 4 - 5
multiboot2/src/image_load_addr.rs

@@ -1,6 +1,7 @@
 //! Module for [`ImageLoadPhysAddrTag`].
 
-use crate::{Tag, TagTrait, TagType, TagTypeId};
+use crate::tag::TagHeader;
+use crate::{Tag, TagTrait, TagType};
 #[cfg(feature = "builder")]
 use core::mem::size_of;
 
@@ -10,8 +11,7 @@ use core::mem::size_of;
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct ImageLoadPhysAddrTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     load_base_addr: u32,
 }
 
@@ -19,8 +19,7 @@ impl ImageLoadPhysAddrTag {
     #[cfg(feature = "builder")]
     pub fn new(load_base_addr: u32) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             load_base_addr,
         }
     }

+ 7 - 10
multiboot2/src/memory_map.rs

@@ -5,6 +5,7 @@ pub use uefi_raw::table::boot::MemoryAttribute as EFIMemoryAttribute;
 pub use uefi_raw::table::boot::MemoryDescriptor as EFIMemoryDesc;
 pub use uefi_raw::table::boot::MemoryType as EFIMemoryAreaType;
 
+use crate::tag::TagHeader;
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::{Debug, Formatter};
 use core::marker::PhantomData;
@@ -27,8 +28,7 @@ const METADATA_SIZE: usize = mem::size_of::<TagTypeId>() + 3 * mem::size_of::<u3
 #[derive(ptr_meta::Pointee, Debug, PartialEq, Eq)]
 #[repr(C)]
 pub struct MemoryMapTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     entry_size: u32,
     entry_version: u32,
     areas: [MemoryArea],
@@ -246,8 +246,7 @@ impl PartialEq<MemoryAreaTypeId> for MemoryAreaType {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct BasicMemoryInfoTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     memory_lower: u32,
     memory_upper: u32,
 }
@@ -255,8 +254,7 @@ pub struct BasicMemoryInfoTag {
 impl BasicMemoryInfoTag {
     pub fn new(memory_lower: u32, memory_upper: u32) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: mem::size_of::<BasicMemoryInfoTag>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             memory_lower,
             memory_upper,
         }
@@ -287,8 +285,7 @@ impl AsBytes for EFIMemoryDesc {}
 #[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct EFIMemoryMapTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     /// Most likely a little more than the size of a [`EFIMemoryDesc`].
     /// This is always the reference, and `size_of` never.
     /// See <https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059>.
@@ -380,8 +377,8 @@ impl EFIMemoryMapTag {
 impl Debug for EFIMemoryMapTag {
     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("EFIMemoryMapTag")
-            .field("typ", &self.typ)
-            .field("size", &self.size)
+            .field("typ", &self.header.typ)
+            .field("size", &self.header.size)
             .field("desc_size", &self.desc_size)
             .field("buf", &self.memory_map.as_ptr())
             .field("buf_len", &self.memory_map.len())

+ 4 - 5
multiboot2/src/module.rs

@@ -1,6 +1,6 @@
 //! Module for [`ModuleTag`].
 
-use crate::tag::{StringError, TagIter};
+use crate::tag::{StringError, TagHeader, TagIter};
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::{Debug, Formatter};
 use core::mem::size_of;
@@ -15,8 +15,7 @@ const METADATA_SIZE: usize = size_of::<TagTypeId>() + 3 * size_of::<u32>();
 #[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct ModuleTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     mod_start: u32,
     mod_end: u32,
     /// Null-terminated UTF-8 string
@@ -81,8 +80,8 @@ impl TagTrait for ModuleTag {
 impl Debug for ModuleTag {
     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("ModuleTag")
-            .field("type", &{ self.typ })
-            .field("size", &{ self.size })
+            .field("type", &self.header.typ)
+            .field("size", &self.header.size)
             // Trick to print as hex.
             .field("mod_start", &self.mod_start)
             .field("mod_end", &self.mod_end)

+ 6 - 9
multiboot2/src/rsdp.rs

@@ -12,7 +12,8 @@
 //! signature should be manually verified.
 //!
 
-use crate::{Tag, TagTrait, TagType, TagTypeId};
+use crate::tag::TagHeader;
+use crate::{Tag, TagTrait, TagType};
 #[cfg(feature = "builder")]
 use core::mem::size_of;
 use core::slice;
@@ -25,8 +26,7 @@ const RSDPV1_LENGTH: usize = 20;
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct RsdpV1Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     signature: [u8; 8],
     checksum: u8,
     oem_id: [u8; 6],
@@ -44,8 +44,7 @@ impl RsdpV1Tag {
         rsdt_address: u32,
     ) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             signature,
             checksum,
             oem_id,
@@ -97,8 +96,7 @@ impl TagTrait for RsdpV1Tag {
 #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct RsdpV2Tag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     signature: [u8; 8],
     checksum: u8,
     oem_id: [u8; 6],
@@ -125,8 +123,7 @@ impl RsdpV2Tag {
         ext_checksum: u8,
     ) -> Self {
         Self {
-            typ: Self::ID.into(),
-            size: size_of::<Self>().try_into().unwrap(),
+            header: TagHeader::new(Self::ID, size_of::<Self>().try_into().unwrap()),
             signature,
             checksum,
             oem_id,

+ 7 - 7
multiboot2/src/smbios.rs

@@ -2,6 +2,7 @@
 
 #[cfg(feature = "builder")]
 use crate::builder::BoxedDst;
+use crate::tag::TagHeader;
 use crate::{Tag, TagTrait, TagType, TagTypeId};
 use core::fmt::Debug;
 
@@ -13,8 +14,7 @@ const METADATA_SIZE: usize = core::mem::size_of::<TagTypeId>()
 #[derive(ptr_meta::Pointee, PartialEq, Eq, PartialOrd, Ord, Hash)]
 #[repr(C)]
 pub struct SmbiosTag {
-    typ: TagTypeId,
-    size: u32,
+    header: TagHeader,
     pub major: u8,
     pub minor: u8,
     _reserved: [u8; 6],
@@ -42,10 +42,10 @@ impl TagTrait for SmbiosTag {
 impl Debug for SmbiosTag {
     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
         f.debug_struct("BootLoaderNameTag")
-            .field("typ", &{ self.typ })
-            .field("size", &{ self.size })
-            .field("major", &{ self.major })
-            .field("minor", &{ self.minor })
+            .field("typ", &self.header.typ)
+            .field("size", &self.header.size)
+            .field("major", &self.major)
+            .field("minor", &self.minor)
             .finish()
     }
 }
@@ -76,7 +76,7 @@ mod tests {
         let tag = get_bytes();
         let tag = unsafe { &*tag.as_ptr().cast::<Tag>() };
         let tag = tag.cast_tag::<SmbiosTag>();
-        assert_eq!({ tag.typ }, TagType::Smbios);
+        assert_eq!(tag.header.typ, TagType::Smbios);
         assert_eq!(tag.major, 3);
         assert_eq!(tag.minor, 0);
         assert_eq!(tag.tables, [0xabu8; 24]);

+ 23 - 0
multiboot2/src/tag.rs

@@ -34,6 +34,29 @@ impl core::error::Error for StringError {
     }
 }
 
+/// The common header that all tags have in common. This type is ABI compatible.
+///
+/// Not to be confused with Multiboot header tags, which are something
+/// different.
+#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
+#[repr(C)]
+pub struct TagHeader {
+    pub typ: TagTypeId, /* u32 */
+    pub size: u32,
+    // Followed by additional, tag specific fields.
+}
+
+impl TagHeader {
+    /// Creates a new header.
+    #[cfg(feature = "builder")]
+    pub fn new(typ: impl Into<TagTypeId>, size: u32) -> Self {
+        Self {
+            typ: typ.into(),
+            size,
+        }
+    }
+}
+
 /// Common base structure for all tags that can be passed via the Multiboot2
 /// Information Structure (MBI) to a Multiboot2 payload/program/kernel.
 ///