Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1746 lines
44 KiB

  1. /*** unasm.c - Unassemble AML file and convert to Intel .ASM file
  2. *
  3. * Copyright (c) 1996,1997 Microsoft Corporation
  4. * Author: Michael Tsang (MikeTs)
  5. * Created: 10/01/97
  6. *
  7. * MODIFICATION HISTORY
  8. */
  9. #include "pch.h"
  10. //
  11. // Local data
  12. //
  13. int giLevel = 0;
  14. /***LP UnAsmFile - Unassemble AML file
  15. *
  16. * ENTRY
  17. * pszAMLName -> AML file name
  18. * pfnPrint -> print function
  19. * pv - print function parameter
  20. *
  21. * EXIT-SUCCESS
  22. * returns ASLERR_NONE
  23. * EXIT-FAILURE
  24. * returns negative error code
  25. */
  26. int LOCAL UnAsmFile(PSZ pszAMLName, PFNPRINT pfnPrint, PVOID pv)
  27. {
  28. int rc = ASLERR_NONE;
  29. PBYTE pb = NULL;
  30. DWORD dwAddr = 0;
  31. int fhAML = 0;
  32. ULONG dwLen = 0;
  33. ENTER((1, "UnAsmFile(AMLName=%s,pfnPrint=%p,pv=%p)\n",
  34. pszAMLName, pfnPrint, pv));
  35. ASSERT(pfnPrint != NULL);
  36. if (gpszAMLFile != NULL)
  37. {
  38. if ((fhAML = _open(gpszAMLFile, _O_BINARY | _O_RDONLY)) == -1)
  39. {
  40. ERROR(("UnAsmFile: failed to open AML file - %s", gpszAMLFile));
  41. rc = ASLERR_OPEN_FILE;
  42. }
  43. else if ((pb = MEMALLOC(sizeof(DESCRIPTION_HEADER))) == NULL)
  44. {
  45. ERROR(("UnAsmFile: failed to allocate description header block"));
  46. rc = ASLERR_OUT_OF_MEM;
  47. }
  48. else if (_read(fhAML, pb, sizeof(DESCRIPTION_HEADER)) !=
  49. sizeof(DESCRIPTION_HEADER))
  50. {
  51. ERROR(("UnAsmFile: failed to read description header block"));
  52. rc = ASLERR_READ_FILE;
  53. }
  54. else if (_lseek(fhAML, 0, SEEK_SET) == -1)
  55. {
  56. ERROR(("UnAsmFile: failed seeking to beginning of AML file"));
  57. rc = ASLERR_SEEK_FILE;
  58. }
  59. else
  60. {
  61. dwLen = ((PDESCRIPTION_HEADER)pb)->Length;
  62. MEMFREE(pb);
  63. if ((pb = MEMALLOC(dwLen)) == NULL)
  64. {
  65. ERROR(("UnAsmFile: failed to allocate AML file buffer"));
  66. rc = ASLERR_OUT_OF_MEM;
  67. }
  68. else if (_read(fhAML, pb, dwLen) != (int)dwLen)
  69. {
  70. ERROR(("UnAsmFile: failed to read AML file"));
  71. rc = ASLERR_OUT_OF_MEM;
  72. }
  73. }
  74. }
  75. #ifdef __UNASM
  76. else
  77. {
  78. DWORD dwTableSig = (gdwfASL & ASLF_DUMP_NONASL)? *((PDWORD)pszAMLName):
  79. *((PDWORD)gpszTabSig);
  80. ASSERT(gpszTabSig != NULL);
  81. if ((pb = GetTableBySig(dwTableSig, &dwAddr)) != NULL)
  82. {
  83. dwLen = ((PDESCRIPTION_HEADER)pb)->Length;
  84. }
  85. else
  86. {
  87. rc = ASLERR_GET_TABLE;
  88. }
  89. }
  90. #endif
  91. if (rc == ASLERR_NONE)
  92. {
  93. rc = UnAsmAML(pszAMLName, dwAddr, pb, pfnPrint, pv);
  94. }
  95. if (pb != NULL)
  96. {
  97. MEMFREE(pb);
  98. }
  99. if (fhAML != 0)
  100. {
  101. _close(fhAML);
  102. }
  103. EXIT((1, "UnAsmFile=%d\n", rc));
  104. return rc;
  105. } //UnAsmFile
  106. /***LP BuildNameSpace - Do a NameSpace building pass
  107. *
  108. * ENTRY
  109. * pszAMLName -> AML file name
  110. * dwAddr - physical address of table
  111. * pb -> AML buffer
  112. *
  113. * EXIT-SUCCESS
  114. * returns ASLERR_NONE
  115. * EXIT-FAILURE
  116. * returns negative error code
  117. */
  118. int LOCAL BuildNameSpace(PSZ pszAMLName, DWORD dwAddr, PBYTE pb)
  119. {
  120. typedef struct _AMLName
  121. {
  122. struct _AMLName *panNext;
  123. PSZ pszAMLName;
  124. DWORD dwAddr;
  125. } AMLNAME, *PAMLNAME;
  126. int rc = ASLERR_NONE;
  127. static PAMLNAME panAMLNames = NULL;
  128. PAMLNAME pan;
  129. ENTER((2, "BuildNameSpace(AMLName=%s,Addr=%x,pb=%p)\n",
  130. pszAMLName, dwAddr, pb));
  131. for (pan = panAMLNames; pan != NULL; pan = pan->panNext)
  132. {
  133. if ((strcmp(pszAMLName, pan->pszAMLName) == 0) &&
  134. (dwAddr == pan->dwAddr))
  135. {
  136. break;
  137. }
  138. }
  139. if (pan == NULL)
  140. {
  141. if ((pan = MEMALLOC(sizeof(AMLNAME))) != NULL)
  142. {
  143. DWORD dwLen = ((PDESCRIPTION_HEADER)pb)->Length;
  144. pan->pszAMLName = pszAMLName;
  145. pan->dwAddr = dwAddr;
  146. pan->panNext = panAMLNames;
  147. panAMLNames = pan;
  148. pb += sizeof(DESCRIPTION_HEADER);
  149. rc = UnAsmScope(&pb, pb + dwLen - sizeof(DESCRIPTION_HEADER), NULL,
  150. NULL);
  151. }
  152. else
  153. {
  154. ERROR(("BuildNameSpace: failed to allocate AMLName entry"));
  155. rc = ASLERR_OUT_OF_MEM;
  156. }
  157. }
  158. EXIT((2, "BuildNameSpace=%d\n", rc));
  159. return rc;
  160. } //BuildNameSpace
  161. /***LP UnAsmAML - Unassemble AML buffer
  162. *
  163. * ENTRY
  164. * pszAMLName -> AML file name
  165. * dwAddr - physical address of table
  166. * pb -> AML buffer
  167. * pfnPrint -> print function
  168. * pv - print function parameter
  169. *
  170. * EXIT-SUCCESS
  171. * returns ASLERR_NONE
  172. * EXIT-FAILURE
  173. * returns negative error code
  174. */
  175. int LOCAL UnAsmAML(PSZ pszAMLName, DWORD dwAddr, PBYTE pb, PFNPRINT pfnPrint,
  176. PVOID pv)
  177. {
  178. int rc = ASLERR_NONE;
  179. PDESCRIPTION_HEADER pdh = (PDESCRIPTION_HEADER)pb;
  180. ENTER((2, "UnAsmAML(AMLName=%s,Addr=%x,pb=%p,pfnPrint=%p,pv=%p)\n",
  181. pszAMLName, dwAddr, pb, pfnPrint, pv));
  182. ASSERT(gpnsNameSpaceRoot != NULL);
  183. gpnsCurrentOwner = NULL;
  184. gpnsCurrentScope = gpnsNameSpaceRoot;
  185. if (ComputeDataChkSum(pb, pdh->Length) != 0)
  186. {
  187. ERROR(("UnAsmAML: failed to verify AML checksum"));
  188. rc = ASLERR_CHECKSUM;
  189. }
  190. else if ((rc = BuildNameSpace(pszAMLName, dwAddr, pb)) == ASLERR_NONE)
  191. {
  192. gpbOpTop = gpbOpBegin = pb;
  193. if ((rc = UnAsmHeader(pszAMLName, pdh, pfnPrint, pv)) == ASLERR_NONE)
  194. {
  195. pb += sizeof(DESCRIPTION_HEADER);
  196. rc = UnAsmScope(&pb, pb + pdh->Length - sizeof(DESCRIPTION_HEADER),
  197. pfnPrint, pv);
  198. if ((rc == ASLERR_NONE) && (pfnPrint != NULL))
  199. {
  200. pfnPrint(pv, "\n");
  201. }
  202. }
  203. }
  204. EXIT((2, "UnAsmAML=%d\n", rc));
  205. return rc;
  206. } //UnAsmAML
  207. /***LP UnAsmHeader - Unassemble table header
  208. *
  209. * ENTRY
  210. * pszAMLName -> AML file name
  211. * pdh -> DESCRIPTION_HEADER
  212. * pfnPrint -> print function
  213. * pv - print function parameter
  214. *
  215. * EXIT-SUCCESS
  216. * returns ASLERR_NONE
  217. * EXIT-FAILURE
  218. * returns negative error code
  219. */
  220. int LOCAL UnAsmHeader(PSZ pszAMLName, PDESCRIPTION_HEADER pdh,
  221. PFNPRINT pfnPrint, PVOID pv)
  222. {
  223. int rc = ASLERR_NONE;
  224. char szSig[sizeof(pdh->Signature) + 1] = {0};
  225. char szOEMID[sizeof(pdh->OEMID) + 1] = {0};
  226. char szOEMTableID[sizeof(pdh->OEMTableID) + 1] = {0};
  227. char szCreatorID[sizeof(pdh->CreatorID) + 1] = {0};
  228. ENTER((2, "UnAsmHeader(AMLName=%s,pdh=%p,pfnPrint=%p,pv=%p)\n",
  229. pszAMLName, pdh, pfnPrint, pv));
  230. if (pfnPrint != NULL)
  231. {
  232. strncpy(szSig, (PSZ)&pdh->Signature, sizeof(pdh->Signature));
  233. strncpy(szOEMID, (PSZ)pdh->OEMID, sizeof(pdh->OEMID));
  234. strncpy(szOEMTableID, (PSZ)pdh->OEMTableID, sizeof(pdh->OEMTableID));
  235. strncpy(szCreatorID, (PSZ)pdh->CreatorID, sizeof(pdh->CreatorID));
  236. if ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))
  237. {
  238. pfnPrint(pv, "; ");
  239. }
  240. pfnPrint(pv, "// CreatorID=%s\tCreatorRev=%x.%x.%d\n",
  241. szCreatorID, pdh->CreatorRev >> 24,
  242. (pdh->CreatorRev >> 16) & 0xff, pdh->CreatorRev & 0xffff);
  243. if ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))
  244. {
  245. pfnPrint(pv, "; ");
  246. }
  247. pfnPrint(pv, "// FileLength=%d\tFileChkSum=0x%x\n\n",
  248. pdh->Length, pdh->Checksum);
  249. if ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))
  250. {
  251. pfnPrint(pv, "; ");
  252. }
  253. pfnPrint(pv, "DefinitionBlock(\"%s\", \"%s\", 0x%02x, \"%s\", \"%s\", 0x%08x)",
  254. pszAMLName, szSig, pdh->Revision, szOEMID, szOEMTableID,
  255. pdh->OEMRevision);
  256. }
  257. EXIT((2, "UnAsmHeader=%d\n", rc));
  258. return rc;
  259. } //UnAsmHeader
  260. /***LP DumpBytes - Dump byte stream in ASM file
  261. *
  262. * ENTRY
  263. * pb -> buffer
  264. * dwLen - length to dump
  265. * pfnPrint -> print function
  266. * pv - print function parameter
  267. *
  268. * EXIT
  269. * None
  270. */
  271. VOID LOCAL DumpBytes(PBYTE pb, DWORD dwLen, PFNPRINT pfnPrint, PVOID pv)
  272. {
  273. int i;
  274. #define MAX_LINE_BYTES 8
  275. ENTER((2, "DumpBytes(pb=%p,Len=%x,pfnPrint=%p,pv=%p)\n",
  276. pb, dwLen, pfnPrint, pv));
  277. while (dwLen > 0)
  278. {
  279. pfnPrint(pv, "\tdb\t0%02xh", *pb);
  280. for (i = 1; i < MAX_LINE_BYTES; ++i)
  281. {
  282. if ((int)dwLen - i > 0)
  283. {
  284. pfnPrint(pv, ", 0%02xh", pb[i]);
  285. }
  286. else
  287. {
  288. pfnPrint(pv, " ");
  289. }
  290. }
  291. pfnPrint(pv, "\t; ");
  292. for (i = 0; (dwLen > 0) && (i < MAX_LINE_BYTES); ++i)
  293. {
  294. pfnPrint(pv, "%c", ((*pb >= ' ') && (*pb <= '~'))? *pb: '.');
  295. dwLen--;
  296. pb++;
  297. }
  298. pfnPrint(pv, "\n");
  299. }
  300. pfnPrint(pv, "\n");
  301. EXIT((2, "DumpBytes!\n"));
  302. } //DumpBytes
  303. /***LP DumpCode - Dump code stream in ASM file
  304. *
  305. * ENTRY
  306. * pbOp -> Opcode pointer
  307. * pfnPrint -> print function
  308. * pv - print function parameter
  309. *
  310. * EXIT
  311. * None
  312. */
  313. VOID LOCAL DumpCode(PBYTE pbOp, PFNPRINT pfnPrint, PVOID pv)
  314. {
  315. ENTER((2, "DumpCode(pbOp=%p,pfnPrint=%p,pv=%p)\n", pbOp, pfnPrint, pv));
  316. if (pfnPrint != NULL)
  317. {
  318. if (gdwfASL & ASLF_GENASM)
  319. {
  320. if (gpbOpBegin != pbOp)
  321. {
  322. pfnPrint(pv, "\n");
  323. pfnPrint(pv, "; %08x:\n", gpbOpBegin - gpbOpTop);
  324. DumpBytes(gpbOpBegin, (DWORD)(pbOp - gpbOpBegin), pfnPrint, pv);
  325. gpbOpBegin = pbOp;
  326. }
  327. }
  328. else
  329. {
  330. pfnPrint(pv, "\n");
  331. }
  332. }
  333. EXIT((2, "DumpCode!\n"));
  334. } //DumpCode
  335. /***LP PrintIndent - Print indent level
  336. *
  337. * ENTRY
  338. * iLevel - indent level
  339. * pfnPrint -> print function
  340. * pv - print function parameter
  341. *
  342. * EXIT
  343. * None
  344. */
  345. VOID LOCAL PrintIndent(int iLevel, PFNPRINT pfnPrint, PVOID pv)
  346. {
  347. int i;
  348. ENTER((3, "PrintIndent(Level=%d,pfnPrint=%p,pv=%p)\n",
  349. iLevel, pfnPrint, pv));
  350. if (pfnPrint != NULL)
  351. {
  352. if ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))
  353. {
  354. pfnPrint(pv, "; ");
  355. }
  356. for (i = 0; i < iLevel; ++i)
  357. {
  358. pfnPrint(pv,
  359. ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))?
  360. "| ": " ");
  361. }
  362. }
  363. EXIT((3, "PrintIndent!\n"));
  364. } //PrintIndent
  365. /***LP FindOpClass - Find opcode class of extended opcode
  366. *
  367. * ENTRY
  368. * bOp - opcode
  369. * pOpTable -> opcode table
  370. *
  371. * EXIT-SUCCESS
  372. * returns opcode class
  373. * EXIT-FAILURE
  374. * returns OPCLASS_INVALID
  375. */
  376. BYTE LOCAL FindOpClass(BYTE bOp, POPMAP pOpTable)
  377. {
  378. BYTE bOpClass = OPCLASS_INVALID;
  379. ENTER((2, "FindOpClass(Op=%x,pOpTable=%x)\n", bOp, pOpTable));
  380. while (pOpTable->bOpClass != 0)
  381. {
  382. if (bOp == pOpTable->bExOp)
  383. {
  384. bOpClass = pOpTable->bOpClass;
  385. break;
  386. }
  387. else
  388. pOpTable++;
  389. }
  390. EXIT((2, "FindOpClass=%x\n", bOpClass));
  391. return bOpClass;
  392. } //FindOpClass
  393. /***LP FindOpTerm - Find opcode in TermTable
  394. *
  395. * ENTRY
  396. * dwOpcode - opcode
  397. *
  398. * EXIT-SUCCESS
  399. * returns TermTable entry pointer
  400. * EXIT-FAILURE
  401. * returns NULL
  402. */
  403. PASLTERM LOCAL FindOpTerm(DWORD dwOpcode)
  404. {
  405. PASLTERM pterm = NULL;
  406. int i;
  407. ENTER((2, "FindOpTerm(Opcode=%x)\n", dwOpcode));
  408. for (i = 0; TermTable[i].pszID != NULL; ++i)
  409. {
  410. if ((TermTable[i].dwOpcode == dwOpcode) &&
  411. (TermTable[i].dwfTermClass &
  412. (TC_CONST_NAME | TC_SHORT_NAME | TC_NAMESPACE_MODIFIER |
  413. TC_DATA_OBJECT | TC_NAMED_OBJECT | TC_OPCODE_TYPE1 |
  414. TC_OPCODE_TYPE2)))
  415. {
  416. break;
  417. }
  418. }
  419. if (TermTable[i].pszID != NULL)
  420. {
  421. pterm = &TermTable[i];
  422. }
  423. EXIT((2, "FindOpTerm=%p (Term=%s)\n", pterm, pterm? pterm->pszID: ""));
  424. return pterm;
  425. } //FindOpTerm
  426. /***LP FindKeywordTerm - Find keyword in TermTable
  427. *
  428. * ENTRY
  429. * cKWGroup - keyword group
  430. * bData - data to match keyword
  431. *
  432. * EXIT-SUCCESS
  433. * returns TermTable entry pointer
  434. * EXIT-FAILURE
  435. * returns NULL
  436. */
  437. PASLTERM LOCAL FindKeywordTerm(char cKWGroup, BYTE bData)
  438. {
  439. PASLTERM pterm = NULL;
  440. int i;
  441. ENTER((2, "FindKeywordTerm(cKWGroup=%c,Data=%x)\n", cKWGroup, bData));
  442. for (i = 0; TermTable[i].pszID != NULL; ++i)
  443. {
  444. if ((TermTable[i].dwfTermClass == TC_KEYWORD) &&
  445. (TermTable[i].pszArgActions[0] == cKWGroup) &&
  446. ((bData & (BYTE)(TermTable[i].dwTermData >> 8)) ==
  447. (BYTE)(TermTable[i].dwTermData & 0xff)))
  448. {
  449. break;
  450. }
  451. }
  452. if (TermTable[i].pszID != NULL)
  453. {
  454. pterm = &TermTable[i];
  455. }
  456. EXIT((2, "FindKeywordTerm=%p (Term=%s)\n", pterm, pterm? pterm->pszID: ""));
  457. return pterm;
  458. } //FindKeywordTerm
  459. /***LP UnAsmScope - Unassemble a scope
  460. *
  461. * ENTRY
  462. * ppbOp -> Opcode pointer
  463. * pbEnd -> end of scope
  464. * pfnPrint -> print function
  465. * pv - print function parameter
  466. *
  467. * EXIT-SUCCESS
  468. * returns ASLERR_NONE
  469. * EXIT-FAILURE
  470. * returns negative error code
  471. */
  472. int LOCAL UnAsmScope(PBYTE *ppbOp, PBYTE pbEnd, PFNPRINT pfnPrint, PVOID pv)
  473. {
  474. int rc = ASLERR_NONE;
  475. ENTER((2, "UnAsmScope(pbOp=%p,pbEnd=%p,pfnPrint=%p,pv=%p)\n",
  476. *ppbOp, pbEnd, pfnPrint, pv));
  477. DumpCode(*ppbOp, pfnPrint, pv);
  478. PrintIndent(giLevel, pfnPrint, pv);
  479. if (pfnPrint != NULL)
  480. {
  481. pfnPrint(pv, "{\n");
  482. }
  483. giLevel++;
  484. while ((rc == ASLERR_NONE) && (*ppbOp < pbEnd))
  485. {
  486. PrintIndent(giLevel, pfnPrint, pv);
  487. rc = UnAsmOpcode(ppbOp, pfnPrint, pv);
  488. if (gpbOpBegin != *ppbOp)
  489. {
  490. DumpCode(*ppbOp, pfnPrint, pv);
  491. }
  492. else if (pfnPrint != NULL)
  493. {
  494. pfnPrint(pv, "\n");
  495. }
  496. }
  497. giLevel--;
  498. PrintIndent(giLevel, pfnPrint, pv);
  499. if (pfnPrint != NULL)
  500. {
  501. pfnPrint(pv, "}");
  502. }
  503. EXIT((2, "UnAsmScope=%d\n", rc));
  504. return rc;
  505. } //UnAsmScope
  506. /***LP UnAsmOpcode - Unassemble an Opcode
  507. *
  508. * ENTRY
  509. * ppbOp -> Opcode pointer
  510. * pfnPrint -> print function
  511. * pv - print function parameter
  512. *
  513. * EXIT-SUCCESS
  514. * returns ASLERR_NONE
  515. * EXIT-FAILURE
  516. * returns negative error code
  517. */
  518. int LOCAL UnAsmOpcode(PBYTE *ppbOp, PFNPRINT pfnPrint, PVOID pv)
  519. {
  520. int rc = ASLERR_NONE;
  521. DWORD dwOpcode;
  522. BYTE bOp;
  523. PASLTERM pterm;
  524. char szUnAsmArgTypes[MAX_ARGS + 1];
  525. PNSOBJ pns;
  526. int i;
  527. ENTER((2, "UnAsmOpcode(pbOp=%p,pfnPrint=%p,pv=%p)\n",
  528. *ppbOp, pfnPrint, pv));
  529. if (**ppbOp == OP_EXT_PREFIX)
  530. {
  531. (*ppbOp)++;
  532. dwOpcode = (((DWORD)**ppbOp) << 8) | OP_EXT_PREFIX;
  533. bOp = FindOpClass(**ppbOp, ExOpClassTable);
  534. }
  535. else
  536. {
  537. dwOpcode = (DWORD)(**ppbOp);
  538. bOp = OpClassTable[**ppbOp];
  539. }
  540. switch (bOp)
  541. {
  542. case OPCLASS_DATA_OBJ:
  543. rc = UnAsmDataObj(ppbOp, pfnPrint, pv);
  544. break;
  545. case OPCLASS_NAME_OBJ:
  546. if (((rc = UnAsmNameObj(ppbOp, pfnPrint, pv, &pns, NSTYPE_UNKNOWN))
  547. == ASLERR_NONE) &&
  548. (pns != NULL) &&
  549. (pns->ObjData.dwDataType == OBJTYPE_METHOD))
  550. {
  551. for (i = 0; i < (int)pns->ObjData.uipDataValue; ++i)
  552. {
  553. szUnAsmArgTypes[i] = 'C';
  554. }
  555. szUnAsmArgTypes[i] = '\0';
  556. rc = UnAsmArgs(szUnAsmArgTypes, NULL, 0, ppbOp, NULL, pfnPrint,
  557. pv);
  558. }
  559. break;
  560. case OPCLASS_ARG_OBJ:
  561. case OPCLASS_LOCAL_OBJ:
  562. case OPCLASS_CODE_OBJ:
  563. case OPCLASS_CONST_OBJ:
  564. if ((pterm = FindOpTerm(dwOpcode)) == NULL)
  565. {
  566. ERROR(("UnAsmOpcode: invalid opcode 0x%x", dwOpcode));
  567. rc = ASLERR_INVALID_OPCODE;
  568. }
  569. else
  570. {
  571. (*ppbOp)++;
  572. rc = UnAsmTermObj(pterm, ppbOp, pfnPrint, pv);
  573. }
  574. break;
  575. default:
  576. ERROR(("UnAsmOpcode: invalid opcode class %d", bOp));
  577. rc = ASLERR_INTERNAL_ERROR;
  578. }
  579. EXIT((2, "UnAsmOpcode=%d\n", rc));
  580. return rc;
  581. } //UnAsmOpcode
  582. /***LP UnAsmDataObj - Unassemble data object
  583. *
  584. * ENTRY
  585. * ppbOp -> opcode pointer
  586. * pfnPrint -> print function
  587. * pv - print function parameter
  588. *
  589. * EXIT-SUCCESS
  590. * returns ASLERR_NONE
  591. * EXIT-FAILURE
  592. * returns negative error code
  593. */
  594. int LOCAL UnAsmDataObj(PBYTE *ppbOp, PFNPRINT pfnPrint, PVOID pv)
  595. {
  596. int rc = ASLERR_NONE;
  597. BYTE bOp = **ppbOp;
  598. ENTER((2, "UnAsmDataObj(pbOp=%p,bOp=%x,pfnPrint=%p,pv=%p)\n",
  599. *ppbOp, bOp, pfnPrint, pv));
  600. (*ppbOp)++;
  601. switch (bOp)
  602. {
  603. case OP_BYTE:
  604. if (pfnPrint != NULL)
  605. {
  606. pfnPrint(pv, "0x%x", **ppbOp);
  607. }
  608. *ppbOp += sizeof(BYTE);
  609. break;
  610. case OP_WORD:
  611. if (pfnPrint != NULL)
  612. {
  613. pfnPrint(pv, "0x%x", *((PWORD)*ppbOp));
  614. }
  615. *ppbOp += sizeof(WORD);
  616. break;
  617. case OP_DWORD:
  618. if (pfnPrint != NULL)
  619. {
  620. pfnPrint(pv, "0x%x", *((PDWORD)*ppbOp));
  621. }
  622. *ppbOp += sizeof(DWORD);
  623. break;
  624. case OP_STRING:
  625. if (pfnPrint != NULL)
  626. {
  627. PSZ psz;
  628. pfnPrint(pv, "\"");
  629. for (psz = (PSZ)*ppbOp; *psz != '\0'; psz++)
  630. {
  631. if (*psz == '\\')
  632. {
  633. pfnPrint(pv, "\\");
  634. }
  635. pfnPrint(pv, "%c", *psz);
  636. }
  637. pfnPrint(pv, "\"");
  638. }
  639. *ppbOp += strlen((PSZ)*ppbOp) + 1;
  640. break;
  641. default:
  642. ERROR(("UnAsmDataObj: unexpected opcode 0x%x", bOp));
  643. rc = ASLERR_INVALID_OPCODE;
  644. }
  645. EXIT((2, "UnAsmDataObj=%d\n", rc));
  646. return rc;
  647. } //UnAsmDataObj
  648. /***LP UnAsmNameObj - Unassemble name object
  649. *
  650. * ENTRY
  651. * ppbOp -> opcode pointer
  652. * pfnPrint -> print function
  653. * pv - print function parameter
  654. * ppns -> to hold object found or created
  655. * c - object type
  656. *
  657. * EXIT-SUCCESS
  658. * returns ASLERR_NONE
  659. * EXIT-FAILURE
  660. * returns negative error code
  661. */
  662. int LOCAL UnAsmNameObj(PBYTE *ppbOp, PFNPRINT pfnPrint, PVOID pv, PNSOBJ *ppns,
  663. char c)
  664. {
  665. int rc = ASLERR_NONE;
  666. char szName[MAX_NSPATH_LEN + 1];
  667. int iLen = 0;
  668. ENTER((2, "UnAsmNameObj(pbOp=%p,pfnPrint=%p,pv=%p,ppns=%p,c=%c)\n",
  669. *ppbOp, pfnPrint, pv, ppns, c));
  670. szName[0] = '\0';
  671. if (**ppbOp == OP_ROOT_PREFIX)
  672. {
  673. szName[iLen] = '\\';
  674. iLen++;
  675. (*ppbOp)++;
  676. rc = ParseNameTail(ppbOp, szName, iLen);
  677. }
  678. else if (**ppbOp == OP_PARENT_PREFIX)
  679. {
  680. szName[iLen] = '^';
  681. iLen++;
  682. (*ppbOp)++;
  683. while ((**ppbOp == OP_PARENT_PREFIX) && (iLen < MAX_NSPATH_LEN))
  684. {
  685. szName[iLen] = '^';
  686. iLen++;
  687. (*ppbOp)++;
  688. }
  689. if (**ppbOp == OP_PARENT_PREFIX)
  690. {
  691. ERROR(("UnAsmNameObj: name too long - \"%s\"", szName));
  692. rc = ASLERR_NAME_TOO_LONG;
  693. }
  694. else
  695. {
  696. rc = ParseNameTail(ppbOp, szName, iLen);
  697. }
  698. }
  699. else
  700. {
  701. rc = ParseNameTail(ppbOp, szName, iLen);
  702. }
  703. if (rc == ASLERR_NONE)
  704. {
  705. PNSOBJ pns = NULL;
  706. if (pfnPrint != NULL)
  707. {
  708. pfnPrint(pv, "%s", szName);
  709. }
  710. if (islower(c) && (gdwfASL & ASLF_UNASM) &&
  711. ((pfnPrint == NULL) ||
  712. (gpnsCurrentScope->ObjData.dwDataType == OBJTYPE_METHOD)))
  713. {
  714. rc = CreateObject(NULL, szName, (char)_toupper(c), &pns);
  715. if ((rc == ASLERR_NSOBJ_EXIST) &&
  716. (gpnsCurrentScope->ObjData.dwDataType == OBJTYPE_METHOD))
  717. {
  718. //
  719. // If we are creating an object inside a method scope, it
  720. // may already exist because we may have unassembled this
  721. // method before. So it is not an error.
  722. //
  723. rc = ASLERR_NONE;
  724. }
  725. }
  726. else if ((rc = GetNameSpaceObj(szName, gpnsCurrentScope, &pns, 0)) ==
  727. ASLERR_NSOBJ_NOT_FOUND)
  728. {
  729. if (c == NSTYPE_SCOPE)
  730. {
  731. rc = CreateScopeObj(szName, &pns);
  732. }
  733. else
  734. {
  735. rc = ASLERR_NONE;
  736. }
  737. }
  738. if (rc == ASLERR_NONE)
  739. {
  740. if ((c == NSTYPE_SCOPE) && (pns != NULL))
  741. {
  742. gpnsCurrentScope = pns;
  743. }
  744. if (ppns != NULL)
  745. {
  746. *ppns = pns;
  747. }
  748. }
  749. }
  750. EXIT((2, "UnAsmNameObj=%d (pns=%p)\n", rc, ppns? *ppns: 0));
  751. return rc;
  752. } //UnAsmNameObj
  753. /***LP ParseNameTail - Parse AML name tail
  754. *
  755. * ENTRY
  756. * ppbOp -> opcode pointer
  757. * pszBuff -> to hold parsed name
  758. * iLen - index to tail of pszBuff
  759. *
  760. * EXIT-SUCCESS
  761. * returns ASLERR_NONE
  762. * EXIT-FAILURE
  763. * returns negative error code
  764. */
  765. int LOCAL ParseNameTail(PBYTE *ppbOp, PSZ pszBuff, int iLen)
  766. {
  767. int rc = ASLERR_NONE;
  768. int icNameSegs = 0;
  769. ENTER((2, "ParseNameTail(pbOp=%x,Name=%s,iLen=%d)\n",
  770. *ppbOp, pszBuff, iLen));
  771. //
  772. // We do not check for invalid NameSeg characters here and assume that
  773. // the compiler does its job not generating it.
  774. //
  775. if (**ppbOp == '\0')
  776. {
  777. //
  778. // There is no NameTail (i.e. either NULL name or name with just
  779. // prefixes.
  780. //
  781. (*ppbOp)++;
  782. }
  783. else if (**ppbOp == OP_MULTI_NAME_PREFIX)
  784. {
  785. (*ppbOp)++;
  786. icNameSegs = (int)**ppbOp;
  787. (*ppbOp)++;
  788. }
  789. else if (**ppbOp == OP_DUAL_NAME_PREFIX)
  790. {
  791. (*ppbOp)++;
  792. icNameSegs = 2;
  793. }
  794. else
  795. icNameSegs = 1;
  796. while ((icNameSegs > 0) && (iLen + sizeof(NAMESEG) < MAX_NSPATH_LEN))
  797. {
  798. strncpy(&pszBuff[iLen], (PSZ)(*ppbOp), sizeof(NAMESEG));
  799. iLen += sizeof(NAMESEG);
  800. *ppbOp += sizeof(NAMESEG);
  801. icNameSegs--;
  802. if ((icNameSegs > 0) && (iLen + 1 < MAX_NSPATH_LEN))
  803. {
  804. pszBuff[iLen] = '.';
  805. iLen++;
  806. }
  807. }
  808. if (icNameSegs > 0)
  809. {
  810. ERROR(("ParseNameTail: name too long - %s", pszBuff));
  811. rc = ASLERR_NAME_TOO_LONG;
  812. }
  813. else
  814. {
  815. pszBuff[iLen] = '\0';
  816. }
  817. EXIT((2, "ParseNameTail=%x (Name=%s)\n", rc, pszBuff));
  818. return rc;
  819. } //ParseNameTail
  820. /***LP UnAsmTermObj - Unassemble term object
  821. *
  822. * ENTRY
  823. * pterm -> term table entry
  824. * ppbOp -> opcode pointer
  825. * pfnPrint -> print function
  826. * pv - print function parameter
  827. *
  828. * EXIT-SUCCESS
  829. * returns ASLERR_NONE
  830. * EXIT-FAILURE
  831. * returns negative error code
  832. */
  833. int LOCAL UnAsmTermObj(PASLTERM pterm, PBYTE *ppbOp, PFNPRINT pfnPrint,
  834. PVOID pv)
  835. {
  836. int rc = ASLERR_NONE;
  837. PBYTE pbEnd = NULL;
  838. PNSOBJ pnsScopeSave = gpnsCurrentScope;
  839. PNSOBJ pns = NULL;
  840. ENTER((2, "UnAsmTermObj(pterm=%p,Term=%s,pbOp=%p,pfnPrint=%p,pv=%p)\n",
  841. pterm, pterm->pszID, *ppbOp, pfnPrint, pv));
  842. if (pfnPrint != NULL)
  843. {
  844. pfnPrint(pv, "%s", pterm->pszID);
  845. }
  846. if (pterm->dwfTerm & TF_PACKAGE_LEN)
  847. {
  848. ParsePackageLen(ppbOp, &pbEnd);
  849. }
  850. if (pterm->pszUnAsmArgTypes != NULL)
  851. {
  852. rc = UnAsmArgs(pterm->pszUnAsmArgTypes, pterm->pszArgActions,
  853. pterm->dwTermData, ppbOp, &pns, pfnPrint, pv);
  854. }
  855. if (rc == ASLERR_NONE)
  856. {
  857. if (pterm->dwfTerm & TF_DATA_LIST)
  858. {
  859. rc = UnAsmDataList(ppbOp, pbEnd, pfnPrint, pv);
  860. }
  861. else if (pterm->dwfTerm & TF_PACKAGE_LIST)
  862. {
  863. rc = UnAsmPkgList(ppbOp, pbEnd, pfnPrint, pv);
  864. }
  865. else if (pterm->dwfTerm & TF_FIELD_LIST)
  866. {
  867. rc = UnAsmFieldList(ppbOp, pbEnd, pfnPrint, pv);
  868. }
  869. else if (pterm->dwfTerm & TF_PACKAGE_LEN)
  870. {
  871. if ((pfnPrint == NULL) && (pterm->lID == ID_METHOD))
  872. {
  873. //
  874. // We are in NameSpace building pass, so don't need to
  875. // go into methods.
  876. //
  877. *ppbOp = pbEnd;
  878. }
  879. else
  880. {
  881. if (pterm->dwfTerm & TF_CHANGE_CHILDSCOPE)
  882. {
  883. ASSERT(pns != NULL);
  884. gpnsCurrentScope = pns;
  885. }
  886. rc = UnAsmScope(ppbOp, pbEnd, pfnPrint, pv);
  887. }
  888. }
  889. }
  890. gpnsCurrentScope = pnsScopeSave;
  891. EXIT((2, "UnAsmTermObj=%d\n", rc));
  892. return rc;
  893. } //UnAsmTermObj
  894. /***LP UnAsmArgs - Unassemble arguments
  895. *
  896. * ENTRY
  897. * pszUnArgTypes -> UnAsm ArgTypes string
  898. * pszArgActions -> Arg Action types
  899. * dwTermData - Term data
  900. * ppbOp -> opcode pointer
  901. * ppns -> to hold created object
  902. * pfnPrint -> print function
  903. * pv - print function parameter
  904. *
  905. * EXIT-SUCCESS
  906. * returns ASLERR_NONE
  907. * EXIT-FAILURE
  908. * returns negative error code
  909. */
  910. int LOCAL UnAsmArgs(PSZ pszUnAsmArgTypes, PSZ pszArgActions, DWORD dwTermData,
  911. PBYTE *ppbOp, PNSOBJ *ppns, PFNPRINT pfnPrint, PVOID pv)
  912. {
  913. int rc = ASLERR_NONE;
  914. static BYTE bArgData = 0;
  915. int iNumArgs, i;
  916. PASLTERM pterm;
  917. ENTER((2, "UnAsmArgs(UnAsmArgTypes=%s,ArgActions=%s,TermData=%x,pbOp=%p,ppns=%p,pfnPrint=%p,pv=%p)\n",
  918. pszUnAsmArgTypes, pszArgActions? pszArgActions: "", dwTermData,
  919. *ppbOp, ppns, pfnPrint, pv));
  920. iNumArgs = strlen(pszUnAsmArgTypes);
  921. if (pfnPrint != NULL)
  922. {
  923. pfnPrint(pv, "(");
  924. }
  925. for (i = 0; i < iNumArgs; ++i)
  926. {
  927. if ((i != 0) && (pfnPrint != NULL))
  928. {
  929. pfnPrint(pv, ", ");
  930. }
  931. switch (pszUnAsmArgTypes[i])
  932. {
  933. case 'N':
  934. ASSERT(pszArgActions != NULL);
  935. rc = UnAsmNameObj(ppbOp, pfnPrint, pv, ppns, pszArgActions[i]);
  936. break;
  937. case 'O':
  938. if ((**ppbOp == OP_BUFFER) || (**ppbOp == OP_PACKAGE) ||
  939. (OpClassTable[**ppbOp] == OPCLASS_CONST_OBJ))
  940. {
  941. pterm = FindOpTerm((DWORD)(**ppbOp));
  942. if(pterm)
  943. {
  944. (*ppbOp)++;
  945. rc = UnAsmTermObj(pterm, ppbOp, pfnPrint, pv);
  946. }
  947. else
  948. {
  949. ASSERT(pterm != NULL);
  950. rc = ASLERR_INVALID_OPCODE;
  951. }
  952. }
  953. else
  954. {
  955. rc = UnAsmDataObj(ppbOp, pfnPrint, pv);
  956. }
  957. break;
  958. case 'C':
  959. rc = UnAsmOpcode(ppbOp, pfnPrint, pv);
  960. break;
  961. case 'B':
  962. if (pfnPrint != NULL)
  963. {
  964. pfnPrint(pv, "0x%x", **ppbOp);
  965. }
  966. *ppbOp += sizeof(BYTE);
  967. break;
  968. case 'E':
  969. case 'e':
  970. case 'K':
  971. case 'k':
  972. if ((pszUnAsmArgTypes[i] == 'K') ||
  973. (pszUnAsmArgTypes[i] == 'E'))
  974. {
  975. bArgData = **ppbOp;
  976. }
  977. if ((pszArgActions != NULL) && (pszArgActions[i] == '!'))
  978. {
  979. if ((gdwfASL & ASLF_UNASM) && (*ppns != NULL))
  980. {
  981. (*ppns)->ObjData.uipDataValue = (DWORD)(**ppbOp & 0x07);
  982. }
  983. if (pfnPrint != NULL)
  984. {
  985. pfnPrint(pv, "0x%x", **ppbOp & 0x07);
  986. }
  987. }
  988. else if (pfnPrint != NULL)
  989. {
  990. pterm = FindKeywordTerm(pszArgActions[i], bArgData);
  991. if (pterm != NULL)
  992. {
  993. pfnPrint(pv, "%s", pterm->pszID);
  994. }
  995. else
  996. {
  997. pfnPrint(pv, "0x%x", bArgData & dwTermData & 0xff);
  998. }
  999. }
  1000. if ((pszUnAsmArgTypes[i] == 'K') ||
  1001. (pszUnAsmArgTypes[i] == 'E'))
  1002. {
  1003. *ppbOp += sizeof(BYTE);
  1004. }
  1005. break;
  1006. case 'W':
  1007. if (pfnPrint != NULL)
  1008. {
  1009. pfnPrint(pv, "0x%x", *((PWORD)*ppbOp));
  1010. }
  1011. *ppbOp += sizeof(WORD);
  1012. break;
  1013. case 'D':
  1014. if (pfnPrint != NULL)
  1015. {
  1016. pfnPrint(pv, "0x%x", *((PDWORD)*ppbOp));
  1017. }
  1018. *ppbOp += sizeof(DWORD);
  1019. break;
  1020. case 'S':
  1021. ASSERT(pszArgActions != NULL);
  1022. rc = UnAsmSuperName(ppbOp, pfnPrint, pv);
  1023. break;
  1024. default:
  1025. ERROR(("UnAsmOpcode: invalid ArgType '%c'", pszUnAsmArgTypes[i]));
  1026. rc = ASLERR_INVALID_ARGTYPE;
  1027. }
  1028. }
  1029. if (pfnPrint != NULL)
  1030. {
  1031. pfnPrint(pv, ")");
  1032. }
  1033. EXIT((2, "UnAsmArgs=%d\n", rc));
  1034. return rc;
  1035. } //UnAsmArgs
  1036. /***LP UnAsmSuperName - Unassemble supername
  1037. *
  1038. * ENTRY
  1039. * ppbOp -> opcode pointer
  1040. * pfnPrint -> print function
  1041. * pv - print function parameter
  1042. *
  1043. * EXIT-SUCCESS
  1044. * returns ASLERR_NONE
  1045. * EXIT-FAILURE
  1046. * returns negative error code
  1047. */
  1048. int LOCAL UnAsmSuperName(PBYTE *ppbOp, PFNPRINT pfnPrint, PVOID pv)
  1049. {
  1050. int rc = ASLERR_NONE;
  1051. ENTER((2, "UnAsmSuperName(pbOp=%p,pfnPrint=%p,pv=%p)\n",
  1052. *ppbOp, pfnPrint, pv));
  1053. if (**ppbOp == 0)
  1054. {
  1055. (*ppbOp)++;
  1056. }
  1057. else if ((**ppbOp == OP_EXT_PREFIX) && (*(*ppbOp + 1) == EXOP_DEBUG))
  1058. {
  1059. if (pfnPrint != NULL)
  1060. {
  1061. pfnPrint(pv, "Debug");
  1062. }
  1063. *ppbOp += 2;
  1064. }
  1065. else if (OpClassTable[**ppbOp] == OPCLASS_NAME_OBJ)
  1066. {
  1067. rc = UnAsmNameObj(ppbOp, pfnPrint, pv, NULL, NSTYPE_UNKNOWN);
  1068. }
  1069. else if ((**ppbOp == OP_INDEX) ||
  1070. (OpClassTable[**ppbOp] == OPCLASS_ARG_OBJ) ||
  1071. (OpClassTable[**ppbOp] == OPCLASS_LOCAL_OBJ))
  1072. {
  1073. rc = UnAsmOpcode(ppbOp, pfnPrint, pv);
  1074. }
  1075. else
  1076. {
  1077. ERROR(("UnAsmSuperName: invalid SuperName - 0x%02x", **ppbOp));
  1078. rc = ASLERR_INVALID_NAME;
  1079. }
  1080. EXIT((2, "UnAsmSuperName=%d\n", rc));
  1081. return rc;
  1082. } //UnAsmSuperName
  1083. /***LP ParsePackageLen - parse package length
  1084. *
  1085. * ENTRY
  1086. * ppbOp -> instruction pointer
  1087. * ppbOpNext -> to hold pointer to next instruction (can be NULL)
  1088. *
  1089. * EXIT
  1090. * returns package length
  1091. */
  1092. DWORD LOCAL ParsePackageLen(PBYTE *ppbOp, PBYTE *ppbOpNext)
  1093. {
  1094. DWORD dwLen;
  1095. BYTE bFollowCnt, i;
  1096. ENTER((2, "ParsePackageLen(pbOp=%x,ppbOpNext=%x)\n", *ppbOp, ppbOpNext));
  1097. if (ppbOpNext != NULL)
  1098. *ppbOpNext = *ppbOp;
  1099. dwLen = (DWORD)(**ppbOp);
  1100. (*ppbOp)++;
  1101. bFollowCnt = (BYTE)((dwLen & 0xc0) >> 6);
  1102. if (bFollowCnt != 0)
  1103. {
  1104. dwLen &= 0x0000000f;
  1105. for (i = 0; i < bFollowCnt; ++i)
  1106. {
  1107. dwLen |= (DWORD)(**ppbOp) << (i*8 + 4);
  1108. (*ppbOp)++;
  1109. }
  1110. }
  1111. if (ppbOpNext != NULL)
  1112. *ppbOpNext += dwLen;
  1113. EXIT((2, "ParsePackageLen=%x (pbOp=%x,pbOpNext=%x)\n",
  1114. dwLen, *ppbOp, ppbOpNext? *ppbOpNext: 0));
  1115. return dwLen;
  1116. } //ParsePackageLen
  1117. /***LP UnAsmDataList - Unassemble data list
  1118. *
  1119. * ENTRY
  1120. * ppbOp -> opcode pointer
  1121. * pbEnd -> end of list
  1122. * pfnPrint -> print function
  1123. * pv - print function parameter
  1124. *
  1125. * EXIT-SUCCESS
  1126. * returns ASLERR_NONE
  1127. * EXIT-FAILURE
  1128. * returns negative error code
  1129. */
  1130. int LOCAL UnAsmDataList(PBYTE *ppbOp, PBYTE pbEnd, PFNPRINT pfnPrint, PVOID pv)
  1131. {
  1132. int rc = ASLERR_NONE;
  1133. int i;
  1134. ENTER((2, "UnAsmDataList(pbOp=%p,pbEnd=%p,pfnPrint=%p,pv=%p)\n",
  1135. *ppbOp, pbEnd, pfnPrint, pv));
  1136. DumpCode(*ppbOp, pfnPrint, pv);
  1137. PrintIndent(giLevel, pfnPrint, pv);
  1138. if (pfnPrint != NULL)
  1139. {
  1140. pfnPrint(pv, "{\n");
  1141. }
  1142. while (*ppbOp < pbEnd)
  1143. {
  1144. if (pfnPrint != NULL)
  1145. {
  1146. if ((gdwfASL & ASLF_GENASM) || !(gdwfASL & ASLF_GENSRC))
  1147. {
  1148. pfnPrint(pv, ";");
  1149. }
  1150. pfnPrint(pv, "\t0x%02x", **ppbOp);
  1151. }
  1152. (*ppbOp)++;
  1153. for (i = 1; (*ppbOp < pbEnd) && (i < 12); ++i)
  1154. {
  1155. if (pfnPrint != NULL)
  1156. {
  1157. pfnPrint(pv, ", 0x%02x", **ppbOp);
  1158. }
  1159. (*ppbOp)++;
  1160. }
  1161. if (pfnPrint != NULL)
  1162. {
  1163. if (*ppbOp < pbEnd)
  1164. {
  1165. pfnPrint(pv, ",");
  1166. }
  1167. pfnPrint(pv, "\n");
  1168. }
  1169. }
  1170. PrintIndent(giLevel, pfnPrint, pv);
  1171. if (pfnPrint != NULL)
  1172. {
  1173. pfnPrint(pv, "}");
  1174. }
  1175. EXIT((2, "UnAsmDataList=%d\n", rc));
  1176. return rc;
  1177. } //UnAsmDataList
  1178. /***LP UnAsmPkgList - Unassemble package list
  1179. *
  1180. * ENTRY
  1181. * ppbOp -> opcode pointer
  1182. * pbEnd -> end of list
  1183. * pfnPrint -> print function
  1184. * pv - print function parameter
  1185. *
  1186. * EXIT-SUCCESS
  1187. * returns ASLERR_NONE
  1188. * EXIT-FAILURE
  1189. * returns negative error code
  1190. */
  1191. int LOCAL UnAsmPkgList(PBYTE *ppbOp, PBYTE pbEnd, PFNPRINT pfnPrint, PVOID pv)
  1192. {
  1193. int rc = ASLERR_NONE;
  1194. PASLTERM pterm;
  1195. ENTER((2, "UnAsmPkgList(pbOp=%p,pbEnd=%p,pfnPrint=%p,pv=%p)\n",
  1196. *ppbOp, pbEnd, pfnPrint, pv));
  1197. DumpCode(*ppbOp, pfnPrint, pv);
  1198. PrintIndent(giLevel, pfnPrint, pv);
  1199. if (pfnPrint != NULL)
  1200. {
  1201. pfnPrint(pv, "{\n");
  1202. }
  1203. giLevel++;
  1204. while ((*ppbOp < pbEnd) && (rc == ASLERR_NONE))
  1205. {
  1206. PrintIndent(giLevel, pfnPrint, pv);
  1207. if ((**ppbOp == OP_BUFFER) || (**ppbOp == OP_PACKAGE) ||
  1208. (OpClassTable[**ppbOp] == OPCLASS_CONST_OBJ))
  1209. {
  1210. pterm = FindOpTerm((DWORD)(**ppbOp));
  1211. ASSERT(pterm != NULL);
  1212. (*ppbOp)++;
  1213. rc = UnAsmTermObj(pterm, ppbOp, pfnPrint, pv);
  1214. }
  1215. else if (OpClassTable[**ppbOp] == OPCLASS_NAME_OBJ)
  1216. {
  1217. rc = UnAsmNameObj(ppbOp, pfnPrint, pv, NULL, NSTYPE_UNKNOWN);
  1218. }
  1219. else
  1220. {
  1221. rc = UnAsmDataObj(ppbOp, pfnPrint, pv);
  1222. }
  1223. if ((*ppbOp < pbEnd) && (rc == ASLERR_NONE) && (pfnPrint != NULL))
  1224. {
  1225. pfnPrint(pv, ",");
  1226. }
  1227. DumpCode(*ppbOp, pfnPrint, pv);
  1228. }
  1229. giLevel--;
  1230. PrintIndent(giLevel, pfnPrint, pv);
  1231. if (pfnPrint != NULL)
  1232. {
  1233. pfnPrint(pv, "}");
  1234. }
  1235. EXIT((2, "UnAsmPkgList=%d\n", rc));
  1236. return rc;
  1237. } //UnAsmPkgList
  1238. /***LP UnAsmFieldList - Unassemble field list
  1239. *
  1240. * ENTRY
  1241. * ppbOp -> opcode pointer
  1242. * pbEnd -> end of list
  1243. * pfnPrint -> print function
  1244. * pv - print function parameter
  1245. *
  1246. * EXIT-SUCCESS
  1247. * returns ASLERR_NONE
  1248. * EXIT-FAILURE
  1249. * returns negative error code
  1250. */
  1251. int LOCAL UnAsmFieldList(PBYTE *ppbOp, PBYTE pbEnd, PFNPRINT pfnPrint, PVOID pv)
  1252. {
  1253. int rc = ASLERR_NONE;
  1254. DWORD dwBitPos = 0;
  1255. ENTER((2, "UnAsmFieldList(pbOp=%p,pbEnd=%p,pfnPrint=%p,pv=%p)\n",
  1256. *ppbOp, pbEnd, pfnPrint, pv));
  1257. DumpCode(*ppbOp, pfnPrint, pv);
  1258. PrintIndent(giLevel, pfnPrint, pv);
  1259. if (pfnPrint != NULL)
  1260. {
  1261. pfnPrint(pv, "{\n");
  1262. }
  1263. giLevel++;
  1264. while ((*ppbOp < pbEnd) && (rc == ASLERR_NONE))
  1265. {
  1266. PrintIndent(giLevel, pfnPrint, pv);
  1267. if (((rc = UnAsmField(ppbOp, pfnPrint, pv, &dwBitPos)) ==
  1268. ASLERR_NONE) &&
  1269. (*ppbOp < pbEnd) && (pfnPrint != NULL))
  1270. {
  1271. pfnPrint(pv, ",");
  1272. }
  1273. DumpCode(*ppbOp, pfnPrint, pv);
  1274. }
  1275. giLevel--;
  1276. PrintIndent(giLevel, pfnPrint, pv);
  1277. if (pfnPrint != NULL)
  1278. {
  1279. pfnPrint(pv, "}");
  1280. }
  1281. EXIT((2, "UnAsmFieldList=%d\n", rc));
  1282. return rc;
  1283. } //UnAsmFieldList
  1284. /***LP UnAsmField - Unassemble field
  1285. *
  1286. * ENTRY
  1287. * ppbOp -> opcode pointer
  1288. * pfnPrint -> print function
  1289. * pv - print function parameter
  1290. * pdwBitPos -> to hold cumulative bit position
  1291. *
  1292. * EXIT-SUCCESS
  1293. * returns ASLERR_NONE
  1294. * EXIT-FAILURE
  1295. * returns negative error code
  1296. */
  1297. int LOCAL UnAsmField(PBYTE *ppbOp, PFNPRINT pfnPrint, PVOID pv,
  1298. PDWORD pdwBitPos)
  1299. {
  1300. int rc = ASLERR_NONE;
  1301. ENTER((2, "UnAsmField(pbOp=%p,pfnPrint=%p,pv=%p,BitPos=%x)\n",
  1302. *ppbOp, pfnPrint, pv, *pdwBitPos));
  1303. if (**ppbOp == 0x01)
  1304. {
  1305. (*ppbOp)++;
  1306. if (pfnPrint != NULL)
  1307. {
  1308. PASLTERM pterm;
  1309. pterm = FindKeywordTerm('A', **ppbOp);
  1310. if(pterm)
  1311. pfnPrint(pv, "AccessAs(%s, 0x%x)",
  1312. pterm->pszID, *(*ppbOp + 1));
  1313. else
  1314. pfnPrint(pv, "FindKeywordTerm('A', **ppbOp); returned NULL");
  1315. }
  1316. *ppbOp += 2;
  1317. }
  1318. else
  1319. {
  1320. char szNameSeg[sizeof(NAMESEG) + 1];
  1321. DWORD dwcbBits;
  1322. if (**ppbOp == 0)
  1323. {
  1324. szNameSeg[0] = '\0';
  1325. (*ppbOp)++;
  1326. }
  1327. else
  1328. {
  1329. strncpy(szNameSeg, (PSZ)*ppbOp, sizeof(NAMESEG));
  1330. szNameSeg[4] = '\0';
  1331. *ppbOp += sizeof(NAMESEG);
  1332. }
  1333. dwcbBits = ParsePackageLen(ppbOp, NULL);
  1334. if (szNameSeg[0] == '\0')
  1335. {
  1336. if (pfnPrint != NULL)
  1337. {
  1338. if ((dwcbBits > 32) && (((*pdwBitPos + dwcbBits) % 8) == 0))
  1339. {
  1340. pfnPrint(pv, "Offset(0x%x)", (*pdwBitPos + dwcbBits)/8);
  1341. }
  1342. else
  1343. {
  1344. pfnPrint(pv, ", %d", dwcbBits);
  1345. }
  1346. }
  1347. }
  1348. else
  1349. {
  1350. if (pfnPrint != NULL)
  1351. {
  1352. pfnPrint(pv, "%s, %d", szNameSeg, dwcbBits);
  1353. }
  1354. if ((gdwfASL & ASLF_UNASM) && (pfnPrint == NULL))
  1355. {
  1356. rc = CreateObject(NULL, szNameSeg, NSTYPE_FIELDUNIT, NULL);
  1357. }
  1358. }
  1359. *pdwBitPos += dwcbBits;
  1360. }
  1361. EXIT((2, "UnAsmField=%d\n", rc));
  1362. return rc;
  1363. } //UnAsmField
  1364. /***LP CreateObject - Create NameSpace object for the term
  1365. *
  1366. * ENTRY
  1367. * ptoken -> TOKEN
  1368. * pszName -> object name
  1369. * c - object type to be created
  1370. * ppns -> to hold object created
  1371. *
  1372. * EXIT-SUCCESS
  1373. * returns ASLERR_NONE
  1374. * EXIT-FAILURE
  1375. * returns negative error code
  1376. */
  1377. int LOCAL CreateObject(PTOKEN ptoken, PSZ pszName, char c, PNSOBJ *ppns)
  1378. {
  1379. int rc = ASLERR_NONE;
  1380. PNSOBJ pns;
  1381. ENTER((2, "CreateObject(ptoken=%p,Name=%s,Type=%c)\n",
  1382. ptoken, pszName, c));
  1383. if (((rc = GetNameSpaceObj(pszName, gpnsCurrentScope, &pns, 0)) ==
  1384. ASLERR_NONE) &&
  1385. (pns->ObjData.dwDataType == OBJTYPE_EXTERNAL) ||
  1386. ((rc = CreateNameSpaceObj(ptoken, pszName, gpnsCurrentScope,
  1387. gpnsCurrentOwner, &pns, NSF_EXIST_ERR)) ==
  1388. ASLERR_NONE))
  1389. {
  1390. #ifndef _UNASM_LIB
  1391. if (!(gdwfASL & ASLF_UNASM))
  1392. {
  1393. ASSERT(gpcodeScope->pnsObj == NULL);
  1394. gpcodeScope->dwfCode |= CF_CREATED_NSOBJ;
  1395. gpcodeScope->pnsObj = pns;
  1396. pns->Context = gpcodeScope;
  1397. }
  1398. #endif //ifndef _UNASM_LIB
  1399. switch (c)
  1400. {
  1401. case NSTYPE_UNKNOWN:
  1402. break;
  1403. case NSTYPE_FIELDUNIT:
  1404. pns->ObjData.dwDataType = OBJTYPE_FIELDUNIT;
  1405. break;
  1406. case NSTYPE_DEVICE:
  1407. pns->ObjData.dwDataType = OBJTYPE_DEVICE;
  1408. break;
  1409. case NSTYPE_EVENT:
  1410. pns->ObjData.dwDataType = OBJTYPE_EVENT;
  1411. break;
  1412. case NSTYPE_METHOD:
  1413. pns->ObjData.dwDataType = OBJTYPE_METHOD;
  1414. break;
  1415. case NSTYPE_MUTEX:
  1416. pns->ObjData.dwDataType = OBJTYPE_MUTEX;
  1417. break;
  1418. case NSTYPE_OPREGION:
  1419. pns->ObjData.dwDataType = OBJTYPE_OPREGION;
  1420. break;
  1421. case NSTYPE_POWERRES:
  1422. pns->ObjData.dwDataType = OBJTYPE_POWERRES;
  1423. break;
  1424. case NSTYPE_PROCESSOR:
  1425. pns->ObjData.dwDataType = OBJTYPE_PROCESSOR;
  1426. break;
  1427. case NSTYPE_THERMALZONE:
  1428. pns->ObjData.dwDataType = OBJTYPE_THERMALZONE;
  1429. break;
  1430. case NSTYPE_OBJALIAS:
  1431. pns->ObjData.dwDataType = OBJTYPE_OBJALIAS;
  1432. break;
  1433. case NSTYPE_BUFFFIELD:
  1434. pns->ObjData.dwDataType = OBJTYPE_BUFFFIELD;
  1435. break;
  1436. default:
  1437. ERROR(("CreateObject: invalid object type %c", c));
  1438. rc = ASLERR_INVALID_OBJTYPE;
  1439. }
  1440. if (ppns != NULL)
  1441. {
  1442. *ppns = pns;
  1443. }
  1444. }
  1445. EXIT((2, "CreateObject=%d\n", rc));
  1446. return rc;
  1447. } //CreateObject
  1448. /***LP CreateScopeObj - Create Scope object
  1449. *
  1450. * ENTRY
  1451. * pszName -> object name
  1452. * ppns -> to hold object created
  1453. *
  1454. * EXIT-SUCCESS
  1455. * returns ASLERR_NONE
  1456. * EXIT-FAILURE
  1457. * returns negative error code
  1458. */
  1459. int LOCAL CreateScopeObj(PSZ pszName, PNSOBJ *ppns)
  1460. {
  1461. int rc = ASLERR_NONE;
  1462. PNSOBJ pnsScope;
  1463. PSZ psz;
  1464. ENTER((2, "CreateScopeObj(Name=%s)\n", pszName));
  1465. ASSERT(ppns != NULL);
  1466. if ((psz = strrchr(pszName, '.')) != NULL)
  1467. {
  1468. *psz = '\0';
  1469. if ((rc = GetNameSpaceObj(pszName, gpnsCurrentScope, &pnsScope, 0)) ==
  1470. ASLERR_NSOBJ_NOT_FOUND)
  1471. {
  1472. rc = CreateScopeObj(pszName, &pnsScope);
  1473. }
  1474. *psz = '.';
  1475. psz++;
  1476. }
  1477. else if (pszName[0] == '\\')
  1478. {
  1479. pnsScope = gpnsNameSpaceRoot;
  1480. psz = &pszName[1];
  1481. }
  1482. else
  1483. {
  1484. pnsScope = gpnsCurrentScope;
  1485. psz = pszName;
  1486. }
  1487. if ((rc == ASLERR_NONE) &&
  1488. ((rc = CreateNameSpaceObj(NULL, psz, pnsScope, NULL, ppns,
  1489. NSF_EXIST_OK)) == ASLERR_NONE))
  1490. {
  1491. (*ppns)->ObjData.dwDataType = OBJTYPE_EXTERNAL;
  1492. }
  1493. EXIT((2, "CreateScopeObj=%d (pns=%p)\n", rc, *ppns));
  1494. return rc;
  1495. } //CreateScopeObj
  1496. /***LP ComputeDataChkSum - Compute checksum of a data buffer
  1497. *
  1498. * ENTRY
  1499. * pb -> data buffer
  1500. * dwLen - size of data buffer
  1501. *
  1502. * EXIT
  1503. * returns the checksum byte
  1504. */
  1505. BYTE LOCAL ComputeDataChkSum(PBYTE pb, DWORD dwLen)
  1506. {
  1507. BYTE bChkSum = 0;
  1508. ENTER((1, "ComputeDataChkSum(pb=%p,Len=%ld)\n", pb, dwLen));
  1509. while (dwLen > 0)
  1510. {
  1511. bChkSum = (BYTE)(bChkSum + *pb);
  1512. pb++;
  1513. dwLen--;
  1514. }
  1515. EXIT((1, "ComputeDataChkSum=%x\n", bChkSum));
  1516. return bChkSum;
  1517. } //ComputeDataChkSum