multiboot2 0.27.0

Convenient and safe parsing of Multiboot2 Boot Information (MBI) structures and the contained information tags. Usable in `no_std` environments, such as a kernel. The default `builder` feature also allows the construction of the corresponding structures.
Documentation
//! Module for [`ApmTag`].

use crate::{TagHeader, TagType};
use multiboot2_common::{MaybeDynSized, Tag};

/// The Advanced Power Management (APM) tag.
#[derive(Debug)]
#[repr(C, align(8))]
pub struct ApmTag {
    header: TagHeader,
    version: u16,
    cseg: u16,
    offset: u32,
    cset_16: u16,
    dseg: u16,
    flags: u16,
    cseg_len: u16,
    cseg_16_len: u16,
    dseg_len: u16,
}

impl ApmTag {
    /// Creates a new tag.
    #[expect(clippy::too_many_arguments)]
    #[must_use]
    pub fn new(
        version: u16,
        cseg: u16,
        offset: u32,
        cset_16: u16,
        dset: u16,
        flags: u16,
        cseg_len: u16,
        cseg_16_len: u16,
        dseg_len: u16,
    ) -> Self {
        Self {
            header: TagHeader::new(Self::ID, Self::BASE_SIZE as u32),
            version,
            cseg,
            offset,
            cset_16,
            dseg: dset,
            flags,
            cseg_len,
            cseg_16_len,
            dseg_len,
        }
    }

    /// The version number of the APM BIOS.
    #[must_use]
    pub const fn version(&self) -> u16 {
        self.version
    }

    /// Contains the 16-bit code segment (CS) address for the APM entry point.
    #[must_use]
    pub const fn cseg(&self) -> u16 {
        self.cseg
    }

    /// Represents the offset address within the code segment (`cseg`) for the
    /// APM entry point.
    #[must_use]
    pub const fn offset(&self) -> u32 {
        self.offset
    }

    /// Contains the 16-bit code segment (CS) address used for 16-bit protected
    /// mode APM functions.
    #[must_use]
    pub const fn cset_16(&self) -> u16 {
        self.cset_16
    }

    /// Holds the 16-bit data segment (DS) address used by the APM BIOS for
    /// data operations.
    #[must_use]
    pub const fn dseg(&self) -> u16 {
        self.dseg
    }

    /// Indicates the status and characteristics of the APM connection, such as
    /// if APM is present and its capabilities.
    #[must_use]
    pub const fn flags(&self) -> u16 {
        self.flags
    }

    /// Indicates the length, in bytes, of the 32-bit code segment (`cseg`).
    #[must_use]
    pub const fn cseg_len(&self) -> u16 {
        self.cseg_len
    }

    /// Provides the length, in bytes, of the 16-bit code segment (`cseg_16`)
    /// used for APM functions.
    #[must_use]
    pub const fn cseg_16_len(&self) -> u16 {
        self.cseg_16_len
    }

    /// Indicates the length, in bytes, of the data segment (`dseg`) used by
    /// the APM BIOS.
    #[must_use]
    pub const fn dseg_len(&self) -> u16 {
        self.dseg_len
    }
}

// SAFETY: The tag is repr(C) with the header as first field, any bit
// pattern is valid, and `BASE_SIZE`/`dst_len` match the ABI.
unsafe impl MaybeDynSized for ApmTag {
    type Header = TagHeader;

    // Spec size (28), excluding the trailing padding that `size_of::<Self>()`
    // (32) would add. Payload: eight `u16` fields plus the `u32` offset.
    const BASE_SIZE: usize = size_of::<TagHeader>() + size_of::<[u16; 8]>() + size_of::<u32>();
}

impl Tag for ApmTag {
    type IDType = TagType;

    const ID: TagType = TagType::Apm;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reported_size_matches_spec() {
        // The Multiboot2 spec mandates size 28 (excluding padding), not the
        // 32 bytes that `size_of::<ApmTag>()` would report.
        let tag = ApmTag::new(1, 2, 3, 4, 5, 6, 7, 8, 9);
        assert_eq!(tag.header.size, 28);
    }

    /// Representative test for all stack-constructed sized tags: the bytes
    /// exposed for a tag built via its constructor must cover exactly the
    /// reported tag size and exclude the implicit trailing padding of the
    /// Rust type layout, which is uninitialized memory that must never be
    /// read (verified by Miri).
    #[test]
    fn as_bytes_covers_exactly_the_reported_tag_size() {
        let tag = ApmTag::new(1, 2, 3, 4, 5, 6, 7, 8, 9);
        let bytes = tag.as_bytes();
        assert_eq!(bytes.len(), 28);
        assert!(bytes.len() < size_of::<ApmTag>());
        // Reading every single byte must be defined behavior.
        let byte_sum = bytes.iter().map(|&byte| byte as u64).sum::<u64>();
        assert!(byte_sum > 0);
    }
}