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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! `aml` is a pure-Rust AML (ACPI Machine Language) parser, used for parsing the DSDT and
//! SSDT tables from ACPI. This crate can be used by kernels to gather information about the
//! hardware, and invoke control methods (this is not yet supported) to query and change the state
//! of devices in a hardware-independent way.
//!
//! ### Using the library
//! To use the library, you will mostly interact with the `AmlContext` type. You should create an
//! instance of this type using `AmlContext::new()`, and then pass it tables containing AML
//! (probably from the `acpi` crate), which you've mapped into the virtual address space. This will
//! parse the table, populating the namespace with objects encoded by the AML. After this, you may
//! unmap the memory the table was mapped into - all the information needed will be extracted and
//! allocated on the heap.
//!
//! You can then access specific objects by name like so: e.g.
//! ```ignore
//! let my_aml_value = aml_context.lookup(&AmlName::from_str("\\_SB.PCI0.S08._ADR").unwrap());
//! ```
//!
//! ### About the parser
//! The parser is written using a set of custom parser combinators - the code can be confusing on
//! first reading, but provides an extensible and type-safe way to write parsers. For an easy
//! introduction to parser combinators and the foundations used for this library, I suggest reading
//! [Bodil's fantastic blog post](https://bodil.lol/parser-combinators/).
//!
//! The actual combinators can be found in `parser.rs`. Various tricks are used to provide a nice
//! API and work around limitations in the type system, such as the concrete types like
//! `MapWithContext`, and the `make_parser_concrete` hack macro.
//!
//! The actual parsers are then grouped into categories based loosely on the AML grammar sections in
//! the ACPI spec. Most are written in terms of combinators, but some have to be written in a more
//! imperitive style, either because they're clearer, or because we haven't yet found good
//! combinator patterns to express the parse.

#![no_std]
#![feature(decl_macro, type_ascription, box_syntax)]

extern crate alloc;

#[cfg(test)]
extern crate std;

#[cfg(test)]
mod test_utils;

pub(crate) mod misc;
pub(crate) mod name_object;
pub(crate) mod namespace;
pub(crate) mod opcode;
pub(crate) mod parser;
pub(crate) mod pkg_length;
pub(crate) mod term_object;
pub(crate) mod type1;
pub(crate) mod type2;
pub mod value;

pub use crate::{
    namespace::{AmlHandle, AmlName, Namespace},
    value::AmlValue,
};

use alloc::string::String;
use log::error;
use misc::{ArgNum, LocalNum};
use parser::Parser;
use pkg_length::PkgLength;
use term_object::term_list;
use value::Args;

/// AML has a `RevisionOp` operator that returns the "AML interpreter revision". It's not clear
/// what this is actually used for, but this is ours.
pub const AML_INTERPRETER_REVISION: u64 = 0;

#[derive(Debug)]
pub struct AmlContext {
    pub namespace: Namespace,
    current_scope: AmlName,

    /*
     * AML local variables. These are used when we invoke a control method. A `None` value
     * represents a null AML object.
     */
    local_0: Option<AmlValue>,
    local_1: Option<AmlValue>,
    local_2: Option<AmlValue>,
    local_3: Option<AmlValue>,
    local_4: Option<AmlValue>,
    local_5: Option<AmlValue>,
    local_6: Option<AmlValue>,
    local_7: Option<AmlValue>,

    /// If we're currently invoking a control method, this stores the arguments that were passed to
    /// it. It's `None` if we aren't invoking a method.
    current_args: Option<Args>,
}

impl AmlContext {
    pub fn new() -> AmlContext {
        AmlContext {
            namespace: Namespace::new(),
            current_scope: AmlName::root(),
            local_0: None,
            local_1: None,
            local_2: None,
            local_3: None,
            local_4: None,
            local_5: None,
            local_6: None,
            local_7: None,
            current_args: None,
        }
    }

    pub fn parse_table(&mut self, stream: &[u8]) -> Result<(), AmlError> {
        if stream.len() == 0 {
            return Err(AmlError::UnexpectedEndOfStream);
        }

        let table_length = PkgLength::from_raw_length(stream, stream.len() as u32) as PkgLength;
        match term_object::term_list(table_length).parse(stream, self) {
            Ok(_) => Ok(()),
            Err((_, _, err)) => {
                error!("Failed to parse AML stream. Err = {:?}", err);
                Err(err)
            }
        }
    }

    /// Invoke a method referred to by its path in the namespace, with the given arguments.
    pub fn invoke_method(&mut self, path: &AmlName, args: Args) -> Result<AmlValue, AmlError> {
        if let AmlValue::Method { flags, code } = self.namespace.get_by_path(path)?.clone() {
            /*
             * First, set up the state we expect to enter the method with, but clearing local
             * variables to "null" and setting the arguments.
             */
            self.current_scope = path.clone();
            self.current_args = Some(args);
            self.local_0 = None;
            self.local_1 = None;
            self.local_2 = None;
            self.local_3 = None;
            self.local_4 = None;
            self.local_5 = None;
            self.local_6 = None;
            self.local_7 = None;

            let return_value =
                match term_list(PkgLength::from_raw_length(&code, code.len() as u32)).parse(&code, self) {
                    // If the method doesn't return a value, we implicitly return `0`
                    Ok(_) => Ok(AmlValue::Integer(0)),
                    Err((_, _, AmlError::Return(result))) => Ok(result),
                    Err((_, _, err)) => {
                        error!("Failed to execute control method: {:?}", err);
                        Err(err)
                    }
                };

            /*
             * Now clear the state.
             */
            self.current_args = None;
            self.local_0 = None;
            self.local_1 = None;
            self.local_2 = None;
            self.local_3 = None;
            self.local_4 = None;
            self.local_5 = None;
            self.local_6 = None;
            self.local_7 = None;

            return_value
        } else {
            Err(AmlError::IncompatibleValueConversion)
        }
    }

    pub(crate) fn current_arg(&self, arg: ArgNum) -> Result<&AmlValue, AmlError> {
        self.current_args.as_ref().ok_or(AmlError::InvalidArgumentAccess(0xff))?.arg(arg)
    }

    /// Get the current value of a local by its local number.
    ///
    /// ### Panics
    /// Panics if an invalid local number is passed (valid local numbers are `0..=7`)
    pub(crate) fn local(&self, local: LocalNum) -> Result<&AmlValue, AmlError> {
        match local {
            0 => self.local_0.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            1 => self.local_1.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            2 => self.local_2.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            3 => self.local_3.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            4 => self.local_4.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            5 => self.local_5.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            6 => self.local_6.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            7 => self.local_7.as_ref().ok_or(AmlError::InvalidLocalAccess(local)),
            _ => panic!("Invalid local number: {}", local),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AmlError {
    /*
     * Errors produced parsing the AML stream.
     */
    UnexpectedEndOfStream,
    UnexpectedByte(u8),
    InvalidNameSeg([u8; 4]),
    InvalidFieldFlags,
    IncompatibleValueConversion,
    UnterminatedStringConstant,
    InvalidStringConstant,
    InvalidRegionSpace(u8),
    /// Emitted by a parser when it's clear that the stream doesn't encode the object parsed by
    /// that parser (e.g. the wrong opcode starts the stream). This is handled specially by some
    /// parsers such as `or` and `choice!`.
    WrongParser,

    /*
     * Errors produced manipulating AML names.
     */
    /// Produced when trying to normalize a path that does not point to a valid level of the
    /// namespace. E.g. `\_SB.^^PCI0` goes above the root of the namespace.
    InvalidNormalizedName(String),
    RootHasNoParent,

    /*
     * Errors produced working with the namespace.
     */
    /// Produced when a path is given that does not point to an object in the AML namespace.
    ObjectDoesNotExist(String),
    HandleDoesNotExist(AmlHandle),
    /// Produced when two values with the same name are added to the namespace.
    NameCollision(AmlName),

    /*
     * Errors produced executing control methods.
     */
    /// Produced when a method accesses an argument it does not have (e.g. a method that takes 2
    /// arguments accesses `Arg4`). The inner value is the number of the argument accessed. If any
    /// arguments are accessed when a method is not being executed, this error is produced with an
    /// argument number of `0xff`.
    InvalidArgumentAccess(ArgNum),
    InvalidLocalAccess(LocalNum),
    /// This is not a real error, but is used to propagate return values from within the deep
    /// parsing call-stack. It should only be emitted when parsing a `DefReturn`. We use the
    /// error system here because the way errors are propagated matches how we want to handle
    /// return values.
    Return(AmlValue),
}