1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// The tag that the bootloader passes will depend on the ACPI version the hardware supports.
/// For ACPI Version 1.0, a `RsdpV1Tag` will be provided, which can be accessed from
/// `BootInformation` using the `rsdp_v1_tag` function. For subsequent versions of ACPI, a
/// `RsdpV2Tag` will be provided, which can be accessed with `rsdp_v2_tag`.
///
/// Even though the bootloader should give the address of the real RSDP/XSDT, the checksum and
/// signature should be manually verified.

use core::str;

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct RsdpV1Tag {
    typ: u32,
    size: u32,
    signature: [u8; 8],
    checksum: u8,
    oem_id: [u8; 6],
    revision: u8,
    rsdt_address: u32,  // This is the PHYSICAL address of the RSDT
}

impl RsdpV1Tag {
    pub fn signature<'a>(&'a self) -> Option<&'a str> {
        str::from_utf8(&self.signature).ok()
    }

    pub fn checksum(&self) -> u8 {
        self.checksum
    }

    pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
        str::from_utf8(&self.oem_id).ok()
    }

    pub fn revision(&self) -> u8 {
        self.revision
    }

    /// Get the physical address of the RSDT.
    pub fn rsdt_address(&self) -> usize {
        self.rsdt_address as usize
    }
}

#[derive(Clone, Copy, Debug)]
#[repr(C, packed)]
pub struct RsdpV2Tag {
    typ: u32,
    size: u32,
    signature: [u8; 8],
    checksum: u8,
    oem_id: [u8; 6],
    revision: u8,
    _rsdt_address: u32,
    length: u32,
    xsdt_address: u64,  // This is the PHYSICAL address of the XSDT
    ext_checksum: u8,
    _reserved: [u8; 3],
}

impl RsdpV2Tag {
    pub fn signature<'a>(&'a self) -> Option<&'a str> {
        str::from_utf8(&self.signature).ok()
    }

    pub fn checksum(&self) -> u8 {
        self.checksum
    }

    pub fn oem_id<'a>(&'a self) -> Option<&'a str> {
        str::from_utf8(&self.oem_id).ok()
    }

    pub fn revision(&self) -> u8 {
        self.revision
    }

    /// Get the physical address of the XSDT. On x86, this is truncated from 64-bit to 32-bit.
    pub fn xsdt_address(&self) -> usize {
        self.xsdt_address as usize
    }

    pub fn ext_checksum(&self) -> u8 {
        self.ext_checksum
    }
}