git clone https://garage44.eu/jeroen/pinnacle.git.. /aml/src/asm.rs
//! AML written: a few functions that each give the bytes of one construct,
//! so tests (and `cargo xtask laptop`'s battery and lid, an SSDT QEMU is
//! given) are written in Rust rather than compiled from ASL by a tool
//! from outside.
//!
//! ```text
//! table(b"SSDT", &[scope("\\_SB", &[device("BAT0", &[
//! name("_HID", &eisa("PNP0C0A")),
//! method("_STA", 0, &[ret(&int(0x1f))]),
//! ])])].concat())
//! ```
use alloc::vec;
use alloc::vec::Vec;
/// A table: its header (revision 2: 64-bit integers) and `body`, the
/// checksum set.
pub fn table(signature: &[u8; 4], body: &[u8]) -> Vec<u8> {
let mut t = Vec::with_capacity(36 + body.len());
t.extend_from_slice(signature);
t.extend_from_slice(&((36 + body.len()) as u32).to_le_bytes());
t.push(2);
t.push(0);
t.extend_from_slice(b"PINNAC");
t.extend_from_slice(b"PINNACLE");
t.extend_from_slice(&1u32.to_le_bytes());
t.extend_from_slice(b"PNCL");
t.extend_from_slice(&1u32.to_le_bytes());
t.extend_from_slice(body);
let sum = t.iter().fold(0u8, |s, &b| s.wrapping_add(b));
t[9] = 0u8.wrapping_sub(sum);
t
}
/// `v` encoded as a PkgLength's value, in `n` bytes.
fn encode_length(v: usize, n: usize) -> Vec<u8> {
if n == 1 {
return vec![v as u8];
}
let mut out = vec![((n - 1) << 6) as u8 | (v & 0x0f) as u8];
for i in 0..n - 1 {
out.push((v >> (4 + 8 * i)) as u8);
}
out
}
fn fits(v: usize, n: usize) -> bool {
if n == 1 {
v < 0x40
} else {
v < 1 << (4 + 8 * (n - 1))
}
}
/// A PkgLength for `content` bytes after it (its own bytes counted).
fn pkg_length(content: usize) -> Vec<u8> {
let n = (1..=4)
.find(|&n| fits(content + n, n))
.expect("too long for a PkgLength");
encode_length(content + n, n)
}
/// A PkgLength holding `v` itself, as a field list's widths do.
fn raw_length(v: usize) -> Vec<u8> {
let n = (1..=4).find(|&n| fits(v, n)).expect("too long for a PkgLength");
encode_length(v, n)
}
/// `op`, a PkgLength, then `content`.
pub fn package_of(op: &[u8], content: &[u8]) -> Vec<u8> {
let mut out = op.to_vec();
out.extend(pkg_length(content.len()));
out.extend_from_slice(content);
out
}
/// A name as written in ASL (`\_SB.PCI0`, `^LID`, `BAT0`).
pub fn path(text: &str) -> Vec<u8> {
let mut out = Vec::new();
let mut rest = text;
if let Some(r) = rest.strip_prefix('\\') {
out.push(b'\\');
rest = r;
}
while let Some(r) = rest.strip_prefix('^') {
out.push(b'^');
rest = r;
}
let segs: Vec<[u8; 4]> = rest
.split('.')
.filter(|s| !s.is_empty())
.map(|s| {
let mut seg = [b'_'; 4];
seg[..s.len().min(4)].copy_from_slice(&s.as_bytes()[..s.len().min(4)]);
seg
})
.collect();
match segs.len() {
0 => out.push(0),
1 => {}
2 => out.push(0x2e),
n => {
out.push(0x2f);
out.push(n as u8);
}
}
for s in segs {
out.extend_from_slice(&s);
}
out
}
pub fn int(v: u64) -> Vec<u8> {
match v {
0 => vec![0x00],
1 => vec![0x01],
v if v <= 0xff => vec![0x0a, v as u8],
v if v <= 0xffff => [&[0x0b][..], &(v as u16).to_le_bytes()].concat(),
v if v <= 0xffff_ffff => [&[0x0c][..], &(v as u32).to_le_bytes()].concat(),
v => [&[0x0e][..], &v.to_le_bytes()].concat(),
}
}
pub fn string(s: &str) -> Vec<u8> {
[&[0x0d][..], s.as_bytes(), &[0]].concat()
}
/// An EISA ID compressed as `_HID` holds it (`PNP0C0A`).
pub fn eisa(id: &str) -> Vec<u8> {
let b = id.as_bytes();
let c = |i: usize| u32::from(b[i] - 0x40) & 0x1f;
let hex = |i: usize| char::from(b[i]).to_digit(16).unwrap_or(0);
let v = c(0) << 26 | c(1) << 21 | c(2) << 16 | hex(3) << 12 | hex(4) << 8 | hex(5) << 4 | hex(6);
[&[0x0c][..], &v.swap_bytes().to_le_bytes()].concat()
}
pub fn buffer(bytes: &[u8]) -> Vec<u8> {
package_of(&[0x11], &[int(bytes.len() as u64), bytes.to_vec()].concat())
}
pub fn package(items: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0x12], &[vec![items.len() as u8], items.concat()].concat())
}
pub fn name(n: &str, value: &[u8]) -> Vec<u8> {
[&[0x08][..], &path(n), value].concat()
}
pub fn scope(n: &str, body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0x10], &[path(n), body.concat()].concat())
}
pub fn device(n: &str, body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0x5b, 0x82], &[path(n), body.concat()].concat())
}
pub fn method(n: &str, args: u8, body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0x14], &[path(n), vec![args & 7], body.concat()].concat())
}
/// `OperationRegion(name, space, offset, length)`.
pub fn region(n: &str, space: u8, offset: u64, len: u64) -> Vec<u8> {
[&[0x5b, 0x80][..], &path(n), &[space], &int(offset), &int(len)].concat()
}
/// `Field(region, flags) { name, bits, … }`; a name of `""` is reserved
/// bits.
pub fn field(region: &str, flags: u8, units: &[(&str, u32)]) -> Vec<u8> {
let mut list = vec![];
for &(n, bits) in units {
if n.is_empty() {
list.push(0);
} else {
let mut seg = [b'_'; 4];
seg[..n.len().min(4)].copy_from_slice(&n.as_bytes()[..n.len().min(4)]);
list.extend_from_slice(&seg);
}
// A field's width is a PkgLength's value, its own bytes not
// counted.
list.extend(raw_length(bits as usize));
}
package_of(&[0x5b, 0x81], &[path(region), vec![flags], list].concat())
}
pub fn ret(v: &[u8]) -> Vec<u8> {
[&[0xa4][..], v].concat()
}
pub fn store(v: &[u8], target: &[u8]) -> Vec<u8> {
[&[0x70][..], v, target].concat()
}
pub fn local(n: u8) -> Vec<u8> {
vec![0x60 + n]
}
pub fn arg(n: u8) -> Vec<u8> {
vec![0x68 + n]
}
pub fn if_(predicate: &[u8], body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0xa0], &[predicate.to_vec(), body.concat()].concat())
}
pub fn else_(body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0xa1], &body.concat())
}
pub fn while_(predicate: &[u8], body: &[Vec<u8>]) -> Vec<u8> {
package_of(&[0xa2], &[predicate.to_vec(), body.concat()].concat())
}
/// A two-operand operation with a target (`Add`, `Subtract`, `And` …):
/// its opcode.
pub fn op2(op: u8, a: &[u8], b: &[u8], target: &[u8]) -> Vec<u8> {
[&[op][..], a, b, target].concat()
}
pub const ADD: u8 = 0x72;
pub const SUBTRACT: u8 = 0x74;
pub const MULTIPLY: u8 = 0x77;
pub const SHIFT_LEFT: u8 = 0x79;
pub const AND: u8 = 0x7b;
pub const OR: u8 = 0x7d;
/// A comparison (`LEqual` 0x93, `LGreater` 0x94, `LLess` 0x95).
pub fn compare(op: u8, a: &[u8], b: &[u8]) -> Vec<u8> {
[&[op][..], a, b].concat()
}
pub fn index(source: &[u8], i: &[u8]) -> Vec<u8> {
[&[0x88][..], source, i, &[0]].concat()
}
pub fn deref(r: &[u8]) -> Vec<u8> {
[&[0x83][..], r].concat()
}
pub fn increment(target: &[u8]) -> Vec<u8> {
[&[0x75][..], target].concat()
}
pub fn notify(target: &[u8], value: &[u8]) -> Vec<u8> {
[&[0x86][..], target, value].concat()
}
/// A call: the method's name, then its arguments.
pub fn call(n: &str, args: &[Vec<u8>]) -> Vec<u8> {
[path(n), args.concat()].concat()
}