summary refs log tree commit diff
path: root/acpi_tables/src/sdt.rs
blob: 96f4d0fde22988636f07630bd01f1f2912786f4f (plain) (blame)
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
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::fs::File;
use std::io::{ErrorKind, Read, Result};
use std::path::PathBuf;

use data_model::DataInit;

/// SDT represents for System Description Table. The structure SDT is a
/// generic format for creating various ACPI tables like DSDT/FADT/MADT.
pub struct SDT {
    data: Vec<u8>,
}

pub const HEADER_LEN: u32 = 36;
const LENGTH_OFFSET: usize = 4;
const CHECKSUM_OFFSET: usize = 9;

#[allow(clippy::len_without_is_empty)]
impl SDT {
    /// Set up the ACPI table header at the front of the SDT.
    /// The arguments correspond to the elements in the ACPI
    /// table headers.
    pub fn new(
        signature: [u8; 4],
        length: u32,
        revision: u8,
        oem_id: [u8; 6],
        oem_table: [u8; 8],
        oem_revision: u32,
    ) -> Self {
        // The length represents for the length of the entire table
        // which includes this header. And the header is 36 bytes, so
        // lenght should be >= 36. For the case who gives a number less
        // than the header len, use the header len directly.
        let len: u32 = if length < HEADER_LEN {
            HEADER_LEN
        } else {
            length
        };
        let mut data = Vec::with_capacity(length as usize);
        data.extend_from_slice(&signature);
        data.extend_from_slice(&len.to_le_bytes());
        data.push(revision);
        data.push(0); // checksum
        data.extend_from_slice(&oem_id);
        data.extend_from_slice(&oem_table);
        data.extend_from_slice(&oem_revision.to_le_bytes());
        data.extend_from_slice(b"CROS");
        data.extend_from_slice(&0u32.to_le_bytes());

        data.resize(length as usize, 0);
        let mut sdt = SDT { data };

        sdt.update_checksum();
        sdt
    }

    /// Set up the ACPI table from file content. Verify file checksum.
    pub fn from_file(path: &PathBuf) -> Result<Self> {
        let mut file = File::open(path)?;
        let mut data = Vec::new();
        file.read_to_end(&mut data)?;
        let checksum = super::generate_checksum(data.as_slice());
        if checksum == 0 {
            Ok(SDT { data })
        } else {
            Err(ErrorKind::InvalidData.into())
        }
    }

    pub fn is_signature(&self, signature: &[u8; 4]) -> bool {
        self.data[0..4] == *signature
    }

    fn update_checksum(&mut self) {
        self.data[CHECKSUM_OFFSET] = 0;
        let checksum = super::generate_checksum(self.data.as_slice());
        self.data[CHECKSUM_OFFSET] = checksum;
    }

    pub fn as_slice(&self) -> &[u8] {
        &self.data.as_slice()
    }

    pub fn append<T: DataInit>(&mut self, value: T) {
        self.data.extend_from_slice(value.as_slice());
        self.write(LENGTH_OFFSET, self.data.len() as u32);
    }

    pub fn append_slice(&mut self, value: &[u8]) {
        self.data.extend_from_slice(value);
        self.write(LENGTH_OFFSET, self.data.len() as u32);
    }

    /// Write a value at the given offset
    pub fn write<T: DataInit>(&mut self, offset: usize, value: T) {
        let value_len = std::mem::size_of::<T>();
        if (offset + value_len) > self.data.len() {
            return;
        }

        self.data[offset..offset + value_len].copy_from_slice(&value.as_slice());
        self.update_checksum();
    }

    pub fn len(&self) -> usize {
        self.data.len()
    }
}

#[cfg(test)]
mod tests {
    use super::SDT;

    #[test]
    fn test_sdt() {
        let mut sdt = SDT::new(*b"TEST", 40, 1, *b"CROSVM", *b"TESTTEST", 1);
        let sum: u8 = sdt
            .as_slice()
            .iter()
            .fold(0u8, |acc, x| acc.wrapping_add(*x));
        assert_eq!(sum, 0);
        sdt.write(36, 0x12345678 as u32);
        let sum: u8 = sdt
            .as_slice()
            .iter()
            .fold(0u8, |acc, x| acc.wrapping_add(*x));
        assert_eq!(sum, 0);
    }
}