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