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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use crate::{AcpiError, AcpiHandler};
use core::{fmt, mem, mem::MaybeUninit, str};
#[repr(C, packed)]
pub struct ExtendedField<T: Copy, const MIN_REVISION: u8>(MaybeUninit<T>);
impl<T: Copy, const MIN_REVISION: u8> ExtendedField<T, MIN_REVISION> {
pub unsafe fn access(&self, revision: u8) -> Option<T> {
if revision >= MIN_REVISION {
Some(unsafe { self.0.assume_init() })
} else {
None
}
}
}
#[derive(Clone, Copy)]
#[repr(C, packed)]
pub struct SdtHeader {
pub signature: Signature,
pub length: u32,
pub revision: u8,
pub checksum: u8,
pub oem_id: [u8; 6],
pub oem_table_id: [u8; 8],
pub oem_revision: u32,
pub creator_id: u32,
pub creator_revision: u32,
}
impl SdtHeader {
pub fn validate(&self, signature: Signature) -> Result<(), AcpiError> {
if self.signature != signature {
return Err(AcpiError::SdtInvalidSignature(signature));
}
if str::from_utf8(&self.oem_id).is_err() {
return Err(AcpiError::SdtInvalidOemId(signature));
}
if str::from_utf8(&self.oem_table_id).is_err() {
return Err(AcpiError::SdtInvalidTableId(signature));
}
let self_ptr = self as *const SdtHeader as *const u8;
let mut sum: u8 = 0;
for i in 0..self.length {
sum = sum.wrapping_add(unsafe { *(self_ptr.offset(i as isize)) } as u8);
}
if sum > 0 {
return Err(AcpiError::SdtInvalidChecksum(signature));
}
Ok(())
}
pub fn oem_id<'a>(&'a self) -> &'a str {
str::from_utf8(&self.oem_id).unwrap()
}
pub fn oem_table_id<'a>(&'a self) -> &'a str {
str::from_utf8(&self.oem_table_id).unwrap()
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Signature([u8; 4]);
impl Signature {
pub const RSDT: Signature = Signature(*b"RSDT");
pub const XSDT: Signature = Signature(*b"XSDT");
pub const FADT: Signature = Signature(*b"FACP");
pub const HPET: Signature = Signature(*b"HPET");
pub const MADT: Signature = Signature(*b"APIC");
pub const MCFG: Signature = Signature(*b"MCFG");
pub const SSDT: Signature = Signature(*b"SSDT");
pub fn as_str(&self) -> &str {
str::from_utf8(&self.0).unwrap()
}
}
impl fmt::Display for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl fmt::Debug for Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"{}\"", self.as_str())
}
}
pub(crate) fn peek_at_sdt_header<H>(handler: &H, physical_address: usize) -> SdtHeader
where
H: AcpiHandler,
{
let mapping =
unsafe { handler.map_physical_region::<SdtHeader>(physical_address, mem::size_of::<SdtHeader>()) };
(*mapping).clone()
}