xref: /aosp_15_r20/external/crosvm/acpi_tables/src/rsdp.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2020 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use zerocopy::AsBytes;
6 use zerocopy::FromBytes;
7 use zerocopy::FromZeroes;
8 
9 #[repr(C, packed)]
10 #[derive(Clone, Copy, Default, FromZeroes, FromBytes, AsBytes)]
11 pub struct RSDP {
12     pub signature: [u8; 8],
13     pub checksum: u8,
14     pub oem_id: [u8; 6],
15     pub revision: u8,
16     _rsdt_addr: u32,
17     pub length: u32,
18     pub xsdt_addr: u64,
19     pub extended_checksum: u8,
20     _reserved: [u8; 3],
21 }
22 
23 impl RSDP {
new(oem_id: [u8; 6], xsdt_addr: u64) -> Self24     pub fn new(oem_id: [u8; 6], xsdt_addr: u64) -> Self {
25         let mut rsdp = RSDP {
26             signature: *b"RSD PTR ",
27             checksum: 0,
28             oem_id,
29             revision: 2,
30             _rsdt_addr: 0,
31             length: std::mem::size_of::<RSDP>() as u32,
32             xsdt_addr,
33             extended_checksum: 0,
34             _reserved: [0; 3],
35         };
36 
37         rsdp.checksum = super::generate_checksum(&rsdp.as_bytes()[0..19]);
38         rsdp.extended_checksum = super::generate_checksum(rsdp.as_bytes());
39         rsdp
40     }
41 
len() -> usize42     pub fn len() -> usize {
43         std::mem::size_of::<RSDP>()
44     }
45 }
46 
47 #[cfg(test)]
48 mod tests {
49     use zerocopy::AsBytes;
50 
51     use super::RSDP;
52 
53     #[test]
test_rsdp()54     fn test_rsdp() {
55         let rsdp = RSDP::new(*b"CHYPER", 0xdead_beef);
56         let sum = rsdp
57             .as_bytes()
58             .iter()
59             .fold(0u8, |acc, x| acc.wrapping_add(*x));
60         assert_eq!(sum, 0);
61         let sum: u8 = rsdp
62             .as_bytes()
63             .iter()
64             .fold(0u8, |acc, x| acc.wrapping_add(*x));
65         assert_eq!(sum, 0);
66     }
67 }
68