acpi/dsdt/
objects.rs

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use super::helpers::*;

pub trait DsdtObject {
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>);

    fn to_bytes(&self) -> Vec<u8> {
        let mut byte_stream = Vec::new();
        self.append_to_vec(&mut byte_stream);
        byte_stream
    }
}

pub struct NamedObject {
    name: Vec<u8>,
    object: Vec<u8>,
}

impl NamedObject {
    pub fn new(name: &[u8], object: &impl DsdtObject) -> Self {
        let encoded_name = encode_name(name);
        assert!(!encoded_name.is_empty());
        NamedObject {
            name: encoded_name,
            object: object.to_bytes(),
        }
    }
}

impl DsdtObject for NamedObject {
    // A named object consists of the identifier (0x8) followed by the 4-byte name
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        byte_stream.push(8);
        byte_stream.extend_from_slice(&self.name);
        byte_stream.extend_from_slice(&self.object);
    }
}

pub struct GenericObject<T: AsRef<[u8]>>(pub T);

impl<T> DsdtObject for GenericObject<T>
where
    T: AsRef<[u8]>,
{
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        let buffer = self.0.as_ref();
        byte_stream.extend_from_slice(buffer);
    }
}

pub struct NamedInteger {
    data: NamedObject,
}

impl NamedInteger {
    pub fn new(name: &[u8], value: u64) -> Self {
        Self {
            data: NamedObject::new(name, &GenericObject(encode_integer(value))),
        }
    }
}

impl DsdtObject for NamedInteger {
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        self.data.append_to_vec(byte_stream);
    }
}

pub struct NamedString {
    data: NamedObject,
}

impl NamedString {
    pub fn new(name: &[u8], value: &[u8]) -> Self {
        Self {
            data: NamedObject::new(name, &GenericObject(encode_string(value))),
        }
    }
}

impl DsdtObject for NamedString {
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        self.data.append_to_vec(byte_stream);
    }
}

pub struct StructuredPackage<T: AsRef<[u8]>> {
    pub elem_count: u8,
    pub elem_data: T,
}

impl<T> DsdtObject for StructuredPackage<T>
where
    T: AsRef<[u8]>,
{
    // A package consists of the identifier (0x12), followed by the length (including itself),
    // the number of elements (depends on what package contents represent) and the content.
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        let buffer = self.elem_data.as_ref();
        byte_stream.push(0x12);
        byte_stream.extend_from_slice(&encode_package_len(buffer.len() + 1));
        byte_stream.push(self.elem_count);
        byte_stream.extend_from_slice(buffer);
    }
}

pub struct Package<T: AsRef<[u8]>>(pub T);

impl<T> DsdtObject for Package<T>
where
    T: AsRef<[u8]>,
{
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        let buffer = self.0.as_ref();
        StructuredPackage {
            elem_count: buffer.len() as u8,
            elem_data: buffer,
        }
        .append_to_vec(byte_stream);
    }
}

pub struct Buffer<T: AsRef<[u8]>>(pub T);

impl<T> DsdtObject for Buffer<T>
where
    T: AsRef<[u8]>,
{
    // A buffer consists of the identifier (0x11), followed by the length (including itself), followed by the size of
    // the buffer in bytes and then the content.
    fn append_to_vec(&self, byte_stream: &mut Vec<u8>) {
        let buffer = self.0.as_ref();
        let encoded_len = encode_integer(buffer.len().try_into().unwrap());
        byte_stream.push(0x11);
        byte_stream.extend_from_slice(&encode_package_len(buffer.len() + encoded_len.len()));
        byte_stream.extend_from_slice(&encoded_len);
        byte_stream.extend_from_slice(buffer);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dsdt::tests::verify_expected_bytes;

    #[test]
    fn verify_package() {
        let package = Package(vec![1, 2, 3, 4]);
        let bytes = package.to_bytes();
        verify_expected_bytes(&bytes, &[0x12, 6, 4, 1, 2, 3, 4]);
    }

    #[test]
    fn verify_large_package() {
        let package = Package(vec![0; 0xff]);
        let bytes = package.to_bytes();
        assert_eq!(bytes.len(), 0xff + 4);
        verify_expected_bytes(&bytes[..5], &[0x12, (1 << 6) | 2, 0x10, 0xff, 0]);
        assert_eq!(bytes[0xff + 3], 0);
    }

    #[test]
    fn verify_named_object() {
        let package = Package(vec![0]);
        let nobj = NamedObject::new(b"FOO", &package);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(&bytes, &[8, b'F', b'O', b'O', b'_', 0x12, 3, 1, 0]);
    }

    #[test]
    fn verify_named_integers() {
        let nobj = NamedInteger::new(b"FOO", 0);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(&bytes, &[8, b'F', b'O', b'O', b'_', 0]);

        let nobj = NamedInteger::new(b"FOO", 1);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(&bytes, &[8, b'F', b'O', b'O', b'_', 1]);

        let nobj = NamedInteger::new(b"FOO", 2);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(&bytes, &[8, b'F', b'O', b'O', b'_', 0xa, 2]);

        let nobj = NamedInteger::new(b"FOO", 0x100);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(&bytes, &[8, b'F', b'O', b'O', b'_', 0xb, 0x00, 0x01]);

        let nobj = NamedInteger::new(b"FOO", 0x10000);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(
            &bytes,
            &[8, b'F', b'O', b'O', b'_', 0xc, 0x00, 0x00, 0x01, 0x00],
        );

        let nobj = NamedInteger::new(b"FOO", 0x100000000);
        let bytes = nobj.to_bytes();
        verify_expected_bytes(
            &bytes,
            &[
                8, b'F', b'O', b'O', b'_', 0xe, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            ],
        );
    }

    #[test]
    fn verify_named_string() {
        let nobj = NamedString::new(b"FOO", b"hello");
        let bytes = nobj.to_bytes();
        verify_expected_bytes(
            &bytes,
            &[
                8, b'F', b'O', b'O', b'_', 0xd, b'h', b'e', b'l', b'l', b'o', 0,
            ],
        );
    }
}