ACPI AML decompiler w/ CFG recovery and structured pseudocode
1#include "t.h"
2
3#include "p.h"
4
5static int parse_ok(const uint8_t *b, size_t n, tobi_parse_result *r) {
6 return tobi_parse(b, n, 0, r) && r->root != NULL;
7}
8
9static void fix_checksum(uint8_t *b, size_t n) {
10 unsigned sum = 0;
11 b[9] = 0;
12 for (size_t i = 0; i < n; i++) {
13 sum = (sum + b[i]) & 0xffu;
14 }
15 b[9] = (uint8_t)((256u - sum) & 0xffu);
16}
17
18int t_p(void) {
19 uint8_t m0[] = {0x14,0x0c,'M','T','H','0',0x01,0x70,0x0a,0x2a,0x60,0xa4,0x60};
20 tobi_parse_result r;
21 T_CHECK(parse_ok(m0, sizeof(m0), &r));
22 T_CHECK(r.root->child_len == 1);
23 tobi_ir *m = r.root->child[0];
24 T_CHECK(m->kind == TOBI_IR_METHOD && m->method_args == 1);
25 T_CHECK(m->child_len == 1 && m->child[0]->child_len == 2);
26 T_CHECK(m->child[0]->child[0]->kind == TOBI_IR_STORE);
27 T_CHECK(m->child[0]->child[1]->kind == TOBI_IR_RETURN);
28 tobi_parse_result_free(&r);
29
30 uint8_t table[36 + sizeof(m0)];
31 memset(table, 0, sizeof(table));
32 memcpy(table, "DSDT", 4);
33 uint32_t table_len = (uint32_t)sizeof(table);
34 table[4] = (uint8_t)(table_len & 0xffu);
35 table[5] = (uint8_t)((table_len >> 8) & 0xffu);
36 table[6] = (uint8_t)((table_len >> 16) & 0xffu);
37 table[7] = (uint8_t)((table_len >> 24) & 0xffu);
38 memcpy(table + 36, m0, sizeof(m0));
39 fix_checksum(table, sizeof(table));
40 T_CHECK(parse_ok(table, sizeof(table), &r));
41 T_CHECK(r.meta.is_table && r.meta.aml_off == 36 && r.meta.aml_len == sizeof(m0));
42 T_CHECK(r.meta.checksum_valid && r.meta.checksum_sum == 0);
43 T_CHECK(r.root->child_len == 1 && r.root->child[0]->kind == TOBI_IR_METHOD);
44 T_CHECK(r.root->child[0]->off == 36);
45 T_CHECK(r.diag.len == 0);
46 tobi_parse_result_free(&r);
47
48 uint8_t nested[] = {0x10,0x14,'\\','_','S','B','_',0x5b,0x82,0x0c,'D','E','V','0',0x14,0x06,'M','0','0','0',0x00};
49 T_CHECK(parse_ok(nested, sizeof(nested), &r));
50 T_CHECK(r.root->child[0]->kind == TOBI_IR_SCOPE);
51 T_CHECK(r.root->child[0]->child[0]->child[0]->kind == TOBI_IR_DEVICE);
52 tobi_parse_result_free(&r);
53
54 uint8_t objs[] = {0x08,'B','U','F','0',0x11,0x05,0x0a,0x02,0xaa,0xbb,0x08,'P','K','G','0',0x12,0x06,0x02,0x0a,0x01,0x0a,0x02};
55 T_CHECK(parse_ok(objs, sizeof(objs), &r));
56 T_CHECK(r.root->child_len == 2);
57 T_CHECK(r.root->child[0]->child[0]->kind == TOBI_IR_BUFFER);
58 T_CHECK(r.root->child[1]->child[0]->kind == TOBI_IR_PACKAGE);
59 tobi_parse_result_free(&r);
60
61 uint8_t crs[] = {0x08,'C','R','S','0',0x11,0x05,0x0a,0x02,0x79,0x00};
62 T_CHECK(parse_ok(crs, sizeof(crs), &r));
63 T_CHECK(r.root->child_len == 1);
64 T_CHECK(r.root->child[0]->child[0]->kind == TOBI_IR_BUFFER);
65 T_STR(r.root->child[0]->child[0]->str, "EndTag");
66 tobi_parse_result_free(&r);
67
68 uint8_t fld[] = {0x5b,0x80,'R','E','G','0',0x01,0x0a,0x10,0x0a,0x04,0x5b,0x81,0x0b,'R','E','G','0',0x00,'F','L','D','0',0x08};
69 T_CHECK(parse_ok(fld, sizeof(fld), &r));
70 T_CHECK(r.root->child_len == 2);
71 T_CHECK(r.root->child[1]->kind == TOBI_IR_FIELD);
72 T_CHECK(r.root->child[1]->child_len == 1);
73 T_CHECK(tobi_ns_find_const(&r.ns, "\\REG0") != NULL);
74 const tobi_ns_ent *fe = tobi_ns_find_const(&r.ns, "\\FLD0");
75 T_CHECK(fe != NULL && fe->kind == TOBI_NS_FIELD);
76 T_CHECK(r.diag.len == 0);
77 tobi_parse_result_free(&r);
78
79 uint8_t ifld[] = {0x5b,0x86,0x0f,'I','D','X','0','D','A','T','0',0x00,'I','F','L','D',0x08};
80 T_CHECK(parse_ok(ifld, sizeof(ifld), &r));
81 T_CHECK(r.root->child_len == 1);
82 T_CHECK(r.root->child[0]->kind == TOBI_IR_FIELD);
83 T_CHECK(strcmp(r.root->child[0]->str, "IndexField") == 0);
84 T_CHECK(r.root->child[0]->child_len == 1);
85 T_CHECK(strcmp(r.root->child[0]->child[0]->name, "IFLD") == 0);
86 tobi_parse_result_free(&r);
87
88 uint8_t bfld[] = {0x5b,0x87,0x11,'R','E','G','0','B','N','K','0',0x0a,0x01,0x00,'B','F','L','D',0x08};
89 T_CHECK(parse_ok(bfld, sizeof(bfld), &r));
90 T_CHECK(r.root->child_len == 1);
91 T_CHECK(r.root->child[0]->kind == TOBI_IR_FIELD);
92 T_CHECK(strcmp(r.root->child[0]->str, "BankField") == 0);
93 T_CHECK(r.root->child[0]->child_len == 2);
94 T_CHECK(r.root->child[0]->child[0]->kind == TOBI_IR_INTEGER);
95 T_CHECK(r.root->child[0]->child[1]->kind == TOBI_IR_FIELD_ELEM);
96 tobi_parse_result_free(&r);
97
98 uint8_t call[] = {
99 0x14,0x08,'C','A','L','0',0x01,0xa4,0x68,
100 0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','0',0x0a,0x05
101 };
102 T_CHECK(parse_ok(call, sizeof(call), &r));
103 T_CHECK(r.root->child_len == 2);
104 tobi_ir *use = r.root->child[1];
105 T_CHECK(use->child_len == 1 && use->child[0]->child_len == 1);
106 T_CHECK(use->child[0]->child[0]->kind == TOBI_IR_RETURN);
107 T_CHECK(use->child[0]->child[0]->child_len == 1);
108 T_CHECK(use->child[0]->child[0]->child[0]->kind == TOBI_IR_CALL);
109 T_CHECK(use->child[0]->child[0]->child[0]->child_len == 1);
110 tobi_parse_result_free(&r);
111
112 uint8_t ext_call[] = {
113 0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','X',0x0a,0x05,
114 0x15,'C','A','L','X',0x08,0x01
115 };
116 T_CHECK(parse_ok(ext_call, sizeof(ext_call), &r));
117 T_CHECK(r.root->child_len == 2);
118 use = r.root->child[0];
119 T_CHECK(use->child[0]->child[0]->child[0]->kind == TOBI_IR_CALL);
120 T_CHECK(use->child[0]->child[0]->child[0]->child_len == 1);
121 T_CHECK(r.root->child[1]->kind == TOBI_IR_EXPR);
122 T_CHECK(strcmp(r.root->child[1]->name, "external") == 0);
123 fe = tobi_ns_find_method(&r.ns, "CALX");
124 T_CHECK(fe != NULL && fe->external && fe->args == 1);
125 tobi_parse_result_free(&r);
126
127 uint8_t use_file[] = {0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','0',0x0a,0x05};
128 uint8_t cal_file[] = {0x14,0x08,'C','A','L','0',0x01,0xa4,0x68};
129 tobi_parse_input inputs[] = {
130 {use_file, sizeof(use_file), "use.aml"},
131 {cal_file, sizeof(cal_file), "cal.aml"}
132 };
133 T_CHECK(tobi_parse_multi(inputs, 2, 0, &r));
134 T_CHECK(r.root->child_len == 2);
135 T_CHECK(strcmp(r.root->child[0]->name, "use.aml") == 0);
136 use = r.root->child[0]->child[0];
137 T_CHECK(use->child[0]->child[0]->kind == TOBI_IR_RETURN);
138 T_CHECK(use->child[0]->child[0]->child[0]->kind == TOBI_IR_CALL);
139 T_CHECK(use->child[0]->child[0]->child[0]->child_len == 1);
140 fe = tobi_ns_find_method(&r.ns, "CAL0");
141 T_CHECK(fe != NULL && !fe->external && fe->args == 1);
142 tobi_parse_result_free(&r);
143
144 uint8_t fwd[] = {
145 0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','0',0x0a,0x05,
146 0x14,0x08,'C','A','L','0',0x01,0xa4,0x68
147 };
148 T_CHECK(parse_ok(fwd, sizeof(fwd), &r));
149 T_CHECK(r.root->child_len == 2);
150 use = r.root->child[0];
151 T_CHECK(use->child[0]->child[0]->kind == TOBI_IR_RETURN);
152 T_CHECK(use->child[0]->child[0]->child[0]->kind == TOBI_IR_CALL);
153 T_CHECK(use->child[0]->child[0]->child[0]->child_len == 1);
154 tobi_parse_result_free(&r);
155
156 uint8_t ops[] = {
157 0x14,0x24,'O','P','S','0',0x00,
158 0x5b,0x22,0x0a,0x01,
159 0x8c,'B','U','F','0',0x0a,0x00,'B','Y','T','0',
160 0x70,0x5b,0x30,0x60,
161 0x5b,0x32,0x0a,0x01,0x0c,0x34,0x12,0x00,0x00,0x0a,0x02
162 };
163 T_CHECK(parse_ok(ops, sizeof(ops), &r));
164 T_CHECK(!tobi_diag_has_error(&r.diag));
165 T_CHECK(r.diag.len == 0);
166 tobi_ir *blk = r.root->child[0]->child[0];
167 T_CHECK(blk->child_len == 4);
168 T_CHECK(blk->child[0]->kind == TOBI_IR_EXPR);
169 T_CHECK(strcmp(blk->child[0]->name, "sleep") == 0);
170 T_CHECK(blk->child[1]->kind == TOBI_IR_EXPR);
171 T_CHECK(strcmp(blk->child[1]->name, "create_byte_field") == 0);
172 T_CHECK(blk->child[2]->kind == TOBI_IR_STORE);
173 T_CHECK(blk->child[2]->child[0]->kind == TOBI_IR_INTEGER);
174 T_CHECK(blk->child[3]->kind == TOBI_IR_EXPR);
175 T_CHECK(strcmp(blk->child[3]->name, "fatal") == 0);
176 tobi_parse_result_free(&r);
177
178 uint8_t ext_obj[] = {
179 0x5b,0x88,'D','R','G','0',0x0d,'S','I','G',0x00,0x0d,'O','E','M',0x00,0x0d,'D','A','T',0x00,
180 0x5b,0x23,'M','T','X','0',0x0b,0xff,0xff,
181 0x5b,0x31
182 };
183 T_CHECK(parse_ok(ext_obj, sizeof(ext_obj), &r));
184 T_CHECK(r.diag.len == 0);
185 T_CHECK(r.root->child_len == 3);
186 T_CHECK(r.root->child[0]->kind == TOBI_IR_OPREGION);
187 T_CHECK(strcmp(r.root->child[0]->str, "DataRegion") == 0);
188 T_CHECK(r.root->child[1]->kind == TOBI_IR_EXPR);
189 T_CHECK(strcmp(r.root->child[1]->name, "acquire") == 0);
190 T_CHECK(r.root->child[2]->kind == TOBI_IR_REF);
191 T_CHECK(strcmp(r.root->child[2]->name, "Debug") == 0);
192 tobi_parse_result_free(&r);
193
194 uint8_t more_ext[] = {
195 0x5b,0x20,'T','B','L','0',0x60,
196 0x5b,0x33,
197 0x5b,0x1f,
198 0x0d,'D','S','D','T',0x00,
199 0x0d,'O','E','M',0x00,
200 0x0d,'T','A','B','L','E',0x00,
201 0x0d,'\\',0x00,
202 0x0d,'P','A','R','M',0x00,
203 0x60
204 };
205 T_CHECK(parse_ok(more_ext, sizeof(more_ext), &r));
206 T_CHECK(r.diag.len == 0);
207 T_CHECK(r.root->child_len == 3);
208 T_CHECK(strcmp(r.root->child[0]->name, "load") == 0);
209 T_CHECK(r.root->child[0]->child_len == 2);
210 T_CHECK(strcmp(r.root->child[1]->name, "timer") == 0);
211 T_CHECK(strcmp(r.root->child[2]->name, "load_table") == 0);
212 T_CHECK(r.root->child[2]->child_len == 6);
213 tobi_parse_result_free(&r);
214
215 uint8_t divop[] = {0x78,0x0a,0x07,0x0a,0x02,0x60,0x61};
216 T_CHECK(parse_ok(divop, sizeof(divop), &r));
217 T_CHECK(r.root->child_len == 1);
218 T_CHECK(r.root->child[0]->kind == TOBI_IR_EXPR);
219 T_CHECK(strcmp(r.root->child[0]->name, "div") == 0);
220 T_CHECK(r.root->child[0]->child_len == 4);
221 tobi_parse_result_free(&r);
222
223 uint8_t dup_name[] = {
224 0x08,'D','U','P','0',0x01,
225 0x08,'D','U','P','0',0x00
226 };
227 T_CHECK(tobi_parse(dup_name, sizeof(dup_name), 0, &r));
228 T_CHECK(r.diag.len == 1);
229 T_CHECK(r.diag.items[0].level == TOBI_DIAG_WARN);
230 tobi_parse_result_free(&r);
231 T_CHECK(!tobi_parse(dup_name, sizeof(dup_name), 1, &r));
232 T_CHECK(tobi_diag_has_error(&r.diag));
233 tobi_parse_result_free(&r);
234 return 0;
235}