• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium OS Authors. All rights reserved.
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 std::fs::File;
6 use std::io::{ErrorKind, Read, Result};
7 use std::path::PathBuf;
8 
9 use data_model::DataInit;
10 
11 /// SDT represents for System Description Table. The structure SDT is a
12 /// generic format for creating various ACPI tables like DSDT/FADT/MADT.
13 pub struct SDT {
14     data: Vec<u8>,
15 }
16 
17 pub const HEADER_LEN: u32 = 36;
18 const LENGTH_OFFSET: usize = 4;
19 const CHECKSUM_OFFSET: usize = 9;
20 
21 #[allow(clippy::len_without_is_empty)]
22 impl SDT {
23     /// Set up the ACPI table header at the front of the SDT.
24     /// The arguments correspond to the elements in the ACPI
25     /// table headers.
new( signature: [u8; 4], length: u32, revision: u8, oem_id: [u8; 6], oem_table: [u8; 8], oem_revision: u32, ) -> Self26     pub fn new(
27         signature: [u8; 4],
28         length: u32,
29         revision: u8,
30         oem_id: [u8; 6],
31         oem_table: [u8; 8],
32         oem_revision: u32,
33     ) -> Self {
34         // The length represents for the length of the entire table
35         // which includes this header. And the header is 36 bytes, so
36         // lenght should be >= 36. For the case who gives a number less
37         // than the header len, use the header len directly.
38         let len: u32 = if length < HEADER_LEN {
39             HEADER_LEN
40         } else {
41             length
42         };
43         let mut data = Vec::with_capacity(length as usize);
44         data.extend_from_slice(&signature);
45         data.extend_from_slice(&len.to_le_bytes());
46         data.push(revision);
47         data.push(0); // checksum
48         data.extend_from_slice(&oem_id);
49         data.extend_from_slice(&oem_table);
50         data.extend_from_slice(&oem_revision.to_le_bytes());
51         data.extend_from_slice(b"CROS");
52         data.extend_from_slice(&0u32.to_le_bytes());
53 
54         data.resize(length as usize, 0);
55         let mut sdt = SDT { data };
56 
57         sdt.update_checksum();
58         sdt
59     }
60 
61     /// Set up the ACPI table from file content. Verify file checksum.
from_file(path: &PathBuf) -> Result<Self>62     pub fn from_file(path: &PathBuf) -> Result<Self> {
63         let mut file = File::open(path)?;
64         let mut data = Vec::new();
65         file.read_to_end(&mut data)?;
66         let checksum = super::generate_checksum(data.as_slice());
67         if checksum == 0 {
68             Ok(SDT { data })
69         } else {
70             Err(ErrorKind::InvalidData.into())
71         }
72     }
73 
is_signature(&self, signature: &[u8; 4]) -> bool74     pub fn is_signature(&self, signature: &[u8; 4]) -> bool {
75         self.data[0..4] == *signature
76     }
77 
update_checksum(&mut self)78     fn update_checksum(&mut self) {
79         self.data[CHECKSUM_OFFSET] = 0;
80         let checksum = super::generate_checksum(self.data.as_slice());
81         self.data[CHECKSUM_OFFSET] = checksum;
82     }
83 
as_slice(&self) -> &[u8]84     pub fn as_slice(&self) -> &[u8] {
85         &self.data.as_slice()
86     }
87 
append<T: DataInit>(&mut self, value: T)88     pub fn append<T: DataInit>(&mut self, value: T) {
89         self.data.extend_from_slice(value.as_slice());
90         self.write(LENGTH_OFFSET, self.data.len() as u32);
91     }
92 
append_slice(&mut self, value: &[u8])93     pub fn append_slice(&mut self, value: &[u8]) {
94         self.data.extend_from_slice(value);
95         self.write(LENGTH_OFFSET, self.data.len() as u32);
96     }
97 
98     /// Write a value at the given offset
write<T: DataInit>(&mut self, offset: usize, value: T)99     pub fn write<T: DataInit>(&mut self, offset: usize, value: T) {
100         let value_len = std::mem::size_of::<T>();
101         if (offset + value_len) > self.data.len() {
102             return;
103         }
104 
105         self.data[offset..offset + value_len].copy_from_slice(&value.as_slice());
106         self.update_checksum();
107     }
108 
len(&self) -> usize109     pub fn len(&self) -> usize {
110         self.data.len()
111     }
112 }
113 
114 #[cfg(test)]
115 mod tests {
116     use super::SDT;
117     use std::io::Write;
118     use tempfile::NamedTempFile;
119 
120     #[test]
test_sdt()121     fn test_sdt() {
122         let mut sdt = SDT::new(*b"TEST", 40, 1, *b"CROSVM", *b"TESTTEST", 1);
123         let sum: u8 = sdt
124             .as_slice()
125             .iter()
126             .fold(0u8, |acc, x| acc.wrapping_add(*x));
127         assert_eq!(sum, 0);
128         sdt.write(36, 0x12345678 as u32);
129         let sum: u8 = sdt
130             .as_slice()
131             .iter()
132             .fold(0u8, |acc, x| acc.wrapping_add(*x));
133         assert_eq!(sum, 0);
134     }
135 
136     #[test]
test_sdt_read_write() -> Result<(), std::io::Error>137     fn test_sdt_read_write() -> Result<(), std::io::Error> {
138         let temp_file = NamedTempFile::new()?;
139         let expected_sdt = SDT::new(*b"TEST", 40, 1, *b"CROSVM", *b"TESTTEST", 1);
140 
141         // Write SDT to file.
142         {
143             let mut writer = temp_file.as_file();
144             writer.write_all(&expected_sdt.as_slice())?;
145         }
146 
147         // Read it back and verify.
148         let actual_sdt = SDT::from_file(&temp_file.path().to_path_buf())?;
149         assert!(actual_sdt.is_signature(b"TEST"));
150         assert_eq!(actual_sdt.as_slice(), expected_sdt.as_slice());
151         Ok(())
152     }
153 }
154