ACPI AML decompiler w/ CFG recovery and structured pseudocode
1#include "t.h"
2
3#include "nm.h"
4
5static char *parse(uint8_t *b, size_t n) {
6 tobi_rd r;
7 tobi_rd_init(&r, b, n, 0);
8 char *s = NULL;
9 if (!tobi_nm_namestring(&r, &s)) {
10 return NULL;
11 }
12 return s;
13}
14
15int t_nm(void) {
16 uint8_t simple[] = {'A','B','C','0'};
17 uint8_t root[] = {'\\','_','S','B','_'};
18 uint8_t parent[] = {'^','^','P','C','I','0'};
19 uint8_t dual[] = {0x2e,'A','A','A','0','B','B','B','0'};
20 uint8_t multi[] = {0x2f,3,'A','A','A','0','B','B','B','0','C','C','C','0'};
21 char *s = parse(simple, sizeof(simple));
22 T_CHECK(s && strcmp(s, "ABC0") == 0);
23 free(s);
24 s = parse(root, sizeof(root));
25 T_CHECK(s && strcmp(s, "\\_SB_") == 0);
26 free(s);
27 s = parse(parent, sizeof(parent));
28 T_CHECK(s && strcmp(s, "^^PCI0") == 0);
29 free(s);
30 s = parse(dual, sizeof(dual));
31 T_CHECK(s && strcmp(s, "AAA0.BBB0") == 0);
32 free(s);
33 s = parse(multi, sizeof(multi));
34 T_CHECK(s && strcmp(s, "AAA0.BBB0.CCC0") == 0);
35 free(s);
36 uint8_t bad[] = {'1','B','C','D'};
37 T_CHECK(parse(bad, sizeof(bad)) == NULL);
38 uint8_t trunc[] = {0x2f,2,'A','A','A','0','B'};
39 T_CHECK(parse(trunc, sizeof(trunc)) == NULL);
40 s = tobi_nm_resolve("\\_SB_.PCI0", "^DEV0");
41 T_CHECK(strcmp(s, "\\_SB_.DEV0") == 0);
42 free(s);
43
44 tobi_ns ns;
45 tobi_ns_init(&ns);
46 int dup = 0;
47 tobi_ns_ent *e = tobi_ns_add(&ns, TOBI_NS_METHOD, "\\CALX", "\\", 4, 1, 0x08, 1, NULL, &dup);
48 T_CHECK(e != NULL && dup == 0);
49 T_CHECK(e->external && e->args == 1);
50 T_CHECK(tobi_ns_find_method(&ns, "CALX") == e);
51 e = tobi_ns_add(&ns, TOBI_NS_METHOD, "\\CALX", "\\", 20, 2, 0x02, 0, NULL, &dup);
52 T_CHECK(e != NULL && dup == 0);
53 T_CHECK(!e->external && e->off == 20 && e->args == 2);
54 (void)tobi_ns_add(&ns, TOBI_NS_METHOD, "\\CALX", "\\", 40, 2, 0x02, 0, NULL, &dup);
55 T_CHECK(dup == 1);
56 e = tobi_ns_add(&ns, TOBI_NS_ALIAS, "\\ALX0", "\\", 44, 0, 0, 0, "\\CALX", &dup);
57 T_CHECK(e != NULL && dup == 0 && e->target != NULL);
58 T_CHECK(strcmp(e->target, "\\CALX") == 0);
59 (void)tobi_ns_add(&ns, TOBI_NS_METHOD, "\\_SB_.MTH0", "\\_SB_", 52, 3, 0, 0, NULL, &dup);
60 (void)tobi_ns_add(&ns, TOBI_NS_METHOD, "\\MTH0", "\\", 56, 1, 0, 0, NULL, &dup);
61 const tobi_ns_ent *found = tobi_ns_lookup(&ns, "\\_SB_.PCI0.DEV0", "MTH0");
62 T_CHECK(found != NULL && strcmp(found->path, "\\_SB_.MTH0") == 0 && found->args == 3);
63 (void)tobi_ns_add(&ns, TOBI_NS_ALIAS, "\\_SB_.PCI0.DEV0.AMTH", "\\_SB_.PCI0.DEV0", 60, 0, 0, 0, "\\_SB_.MTH0", &dup);
64 found = tobi_ns_lookup(&ns, "\\_SB_.PCI0.DEV0", "AMTH");
65 T_CHECK(found != NULL && strcmp(found->path, "\\_SB_.MTH0") == 0);
66 char *rp = tobi_ns_resolve_path(&ns, "\\_SB_.PCI0.DEV0", "^MTH0");
67 T_CHECK(strcmp(rp, "\\_SB_.PCI0.MTH0") == 0);
68 free(rp);
69 tobi_ns_free(&ns);
70 return 0;
71}