ACPI AML decompiler w/ CFG recovery and structured pseudocode
29

Configure Feed

Select the types of activity you want to include in your feed.

Resolve AML NameStrings through lexical namespace lookup

+113 -11
+80
src/nm.c
··· 279 279 return NULL; 280 280 } 281 281 282 + static char *ns_parent(const char *scope) { 283 + if (!scope || strcmp(scope, "\\") == 0 || scope[0] == '\0') { 284 + return NULL; 285 + } 286 + const char *dot = strrchr(scope, '.'); 287 + if (!dot) { 288 + return tobi_xstrdup("\\"); 289 + } 290 + if (dot == scope || (scope[0] == '\\' && dot == scope + 1)) { 291 + return tobi_xstrdup("\\"); 292 + } 293 + return tobi_xstrndup(scope, (size_t)(dot - scope)); 294 + } 295 + 296 + static char *ns_join(const char *scope, const char *name) { 297 + tobi_sb sb; 298 + tobi_sb_init(&sb); 299 + if (!scope || scope[0] == '\0' || strcmp(scope, "\\") == 0) { 300 + tobi_sb_add(&sb, "\\"); 301 + tobi_sb_add(&sb, name); 302 + } else { 303 + tobi_sb_add(&sb, scope); 304 + tobi_sb_ch(&sb, '.'); 305 + tobi_sb_add(&sb, name); 306 + } 307 + return tobi_sb_take(&sb); 308 + } 309 + 310 + static const tobi_ns_ent *ns_follow_alias(const tobi_ns *ns, const tobi_ns_ent *ent) { 311 + const tobi_ns_ent *cur = ent; 312 + for (unsigned i = 0; cur && cur->kind == TOBI_NS_ALIAS && cur->target && i < 16; i++) { 313 + const tobi_ns_ent *next = tobi_ns_find_const(ns, cur->target); 314 + if (!next || next == cur) { 315 + break; 316 + } 317 + cur = next; 318 + } 319 + return cur; 320 + } 321 + 322 + const tobi_ns_ent *tobi_ns_lookup(const tobi_ns *ns, const char *scope, const char *name) { 323 + if (!ns || !name || name[0] == '\0') { 324 + return NULL; 325 + } 326 + if (name[0] == '\\' || name[0] == '^' || strcmp(name, "<null>") == 0) { 327 + char *path = tobi_nm_resolve(scope, name); 328 + const tobi_ns_ent *ent = ns_follow_alias(ns, tobi_ns_find_const(ns, path)); 329 + free(path); 330 + return ent; 331 + } 332 + char *cur = tobi_xstrdup(scope && scope[0] ? scope : "\\"); 333 + for (;;) { 334 + char *candidate = ns_join(cur, name); 335 + const tobi_ns_ent *ent = ns_follow_alias(ns, tobi_ns_find_const(ns, candidate)); 336 + free(candidate); 337 + if (ent) { 338 + free(cur); 339 + return ent; 340 + } 341 + char *parent = ns_parent(cur); 342 + free(cur); 343 + if (!parent) { 344 + break; 345 + } 346 + cur = parent; 347 + } 348 + char *root = ns_join("\\", name); 349 + const tobi_ns_ent *ent = ns_follow_alias(ns, tobi_ns_find_const(ns, root)); 350 + free(root); 351 + return ent; 352 + } 353 + 354 + char *tobi_ns_resolve_path(const tobi_ns *ns, const char *scope, const char *name) { 355 + const tobi_ns_ent *ent = tobi_ns_lookup(ns, scope, name); 356 + if (ent) { 357 + return tobi_xstrdup(ent->path); 358 + } 359 + return tobi_nm_resolve(scope, name); 360 + } 361 + 282 362 tobi_ns_ent *tobi_ns_add(tobi_ns *ns, tobi_ns_kind kind, const char *path, const char *owner, 283 363 size_t off, unsigned args, unsigned flags, int external, 284 364 const char *target, int *duplicate) {
+4
src/nm.h
··· 63 63 64 64 const tobi_ns_ent *tobi_ns_find_method(const tobi_ns *ns, const char *name); 65 65 66 + const tobi_ns_ent *tobi_ns_lookup(const tobi_ns *ns, const char *scope, const char *name); 67 + 68 + char *tobi_ns_resolve_path(const tobi_ns *ns, const char *scope, const char *name); 69 + 66 70 tobi_ns_ent *tobi_ns_add(tobi_ns *ns, tobi_ns_kind kind, const char *path, const char *owner, 67 71 size_t off, unsigned args, unsigned flags, int external, 68 72 const char *target, int *duplicate);
+4 -11
src/p.c
··· 88 88 free(path); 89 89 } 90 90 91 - static int method_find(const parser *p, const char *name) { 92 - const tobi_ns_ent *e = tobi_ns_find_method(p->ns, name); 93 - return e ? (int)e->args : -1; 94 - } 95 - 96 91 static uint8_t table_sum8(const uint8_t *data, size_t len) { 97 92 uint8_t sum = 0; 98 93 for (size_t i = 0; i < len; i++) { ··· 478 473 } 479 474 tobi_ir *n = tobi_ir_new(TOBI_IR_REF, off, tobi_rd_off(rd) - off); 480 475 tobi_ir_set_name(n, name); 481 - char *path = tobi_nm_resolve(p->scope, name); 476 + char *path = tobi_ns_resolve_path(p->ns, p->scope, name); 482 477 tobi_ir_set_path(n, path); 483 478 free(path); 484 479 free(name); ··· 1043 1038 if (!parse_namestring_diag(p, rd, &name)) { 1044 1039 return unknown_node(p, off, 0, 1, "bad namestring expression"); 1045 1040 } 1046 - char *path = tobi_nm_resolve(p->scope, name); 1047 - int argc = method_find(p, path); 1048 - if (argc < 0) { 1049 - argc = method_find(p, name); 1050 - } 1041 + const tobi_ns_ent *ent = tobi_ns_lookup(p->ns, p->scope, name); 1042 + int argc = ent && ent->kind == TOBI_NS_METHOD ? (int)ent->args : -1; 1043 + char *path = ent ? tobi_xstrdup(ent->path) : tobi_nm_resolve(p->scope, name); 1051 1044 tobi_ir *n = tobi_ir_new(argc >= 0 ? TOBI_IR_CALL : TOBI_IR_REF, off, 0); 1052 1045 tobi_ir_set_name(n, name); 1053 1046 tobi_ir_set_path(n, path);
+10
test/t_nm.c
··· 56 56 e = tobi_ns_add(&ns, TOBI_NS_ALIAS, "\\ALX0", "\\", 44, 0, 0, 0, "\\CALX", &dup); 57 57 T_CHECK(e != NULL && dup == 0 && e->target != NULL); 58 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); 59 69 tobi_ns_free(&ns); 60 70 return 0; 61 71 }
+15
test/t_p.c
··· 141 141 T_CHECK(fe != NULL && !fe->external && fe->args == 1); 142 142 tobi_parse_result_free(&r); 143 143 144 + uint8_t scoped_call[] = { 145 + 0x10,0x23,'\\','_','S','B','_', 146 + 0x14,0x08,'C','A','L','0',0x01,0xa4,0x68, 147 + 0x5b,0x82,0x12,'D','E','V','0', 148 + 0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','0',0x0a,0x05 149 + }; 150 + T_CHECK(parse_ok(scoped_call, sizeof(scoped_call), &r)); 151 + use = r.root->child[0]->child[0]->child[1]->child[0]->child[0]; 152 + T_CHECK(use->kind == TOBI_IR_METHOD); 153 + tobi_ir *scall = use->child[0]->child[0]->child[0]; 154 + T_CHECK(scall->kind == TOBI_IR_CALL); 155 + T_CHECK(strcmp(scall->path, "\\_SB_.CAL0") == 0); 156 + T_CHECK(scall->child_len == 1); 157 + tobi_parse_result_free(&r); 158 + 144 159 uint8_t fwd[] = { 145 160 0x14,0x0d,'U','S','E','0',0x00,0xa4,'C','A','L','0',0x0a,0x05, 146 161 0x14,0x08,'C','A','L','0',0x01,0xa4,0x68