multiboot2/
tag.rs

1//! Module for the base tag definition [`TagHeader`].
2
3use crate::TagTypeId;
4use core::fmt::Debug;
5use core::mem;
6use multiboot2_common::Header;
7
8/// The common header that all tags have in common. This type is ABI compatible.
9///
10/// Not to be confused with Multiboot header tags, which are something
11/// different.
12///
13/// It is the sized counterpart of `GenericTag`, an internal type.
14#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15#[repr(C, align(8))] // Alignment also propagates to all tag types using this.
16pub struct TagHeader {
17    /// The ABI-compatible [`TagType`].
18    ///
19    /// [`TagType`]: crate::TagType
20    pub typ: TagTypeId, /* u32 */
21    /// The total size of the tag including the header.
22    pub size: u32,
23    // Followed by optional additional tag specific fields.
24}
25
26impl TagHeader {
27    /// Creates a new header.
28    pub fn new(typ: impl Into<TagTypeId>, size: u32) -> Self {
29        Self {
30            typ: typ.into(),
31            size,
32        }
33    }
34}
35
36impl Header for TagHeader {
37    fn payload_len(&self) -> usize {
38        assert!(self.size as usize >= mem::size_of::<Self>());
39        self.size as usize - mem::size_of::<Self>()
40    }
41
42    fn set_size(&mut self, total_size: usize) {
43        self.size = total_size as u32
44    }
45}