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