1 /******************************************************************************
3 * Module Name: adisasm - Application-level disassembler routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "aslcompiler.h"
57 #define _COMPONENT ACPI_TOOLS
58 ACPI_MODULE_NAME ("adisasm")
60 /* Local prototypes */
65 ACPI_TABLE_HEADER *Table);
69 ACPI_TABLE_HEADER *Table,
72 /* Stubs for ASL compiler */
74 #ifndef ACPI_ASL_COMPILER
77 ACPI_PARSE_OBJECT *Op,
78 ACPI_WALK_STATE *WalkState)
86 ACPI_WALK_STATE *WalkState)
95 ACPI_NAMESPACE_NODE *Node)
97 return (AE_NOT_IMPLEMENTED);
101 AcpiDsRestartControlMethod (
102 ACPI_WALK_STATE *WalkState,
103 ACPI_OPERAND_OBJECT *ReturnDesc)
109 AcpiDsTerminateControlMethod (
110 ACPI_OPERAND_OBJECT *MethodDesc,
111 ACPI_WALK_STATE *WalkState)
117 AcpiDsCallControlMethod (
118 ACPI_THREAD_STATE *Thread,
119 ACPI_WALK_STATE *WalkState,
120 ACPI_PARSE_OBJECT *Op)
126 AcpiDsMethodDataInitArgs (
127 ACPI_OPERAND_OBJECT **Params,
128 UINT32 MaxParamCount,
129 ACPI_WALK_STATE *WalkState)
135 static ACPI_TABLE_DESC LocalTables[1];
136 ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
139 /*******************************************************************************
141 * FUNCTION: AdInitialize
147 * DESCRIPTION: ACPICA and local initialization
149 ******************************************************************************/
158 /* ACPICA subsystem initialization */
160 Status = AcpiOsInitialize ();
161 if (ACPI_FAILURE (Status))
166 Status = AcpiUtInitGlobals ();
167 if (ACPI_FAILURE (Status))
172 Status = AcpiUtMutexInitialize ();
173 if (ACPI_FAILURE (Status))
178 Status = AcpiNsRootInitialize ();
179 if (ACPI_FAILURE (Status))
184 /* Setup the Table Manager (cheat - there is no RSDT) */
186 AcpiGbl_RootTableList.MaxTableCount = 1;
187 AcpiGbl_RootTableList.CurrentTableCount = 0;
188 AcpiGbl_RootTableList.Tables = LocalTables;
190 AcpiGbl_PreviousOp = NULL;
195 /******************************************************************************
197 * FUNCTION: AdAmlDisassemble
199 * PARAMETERS: Filename - AML input filename
200 * OutToFile - TRUE if output should go to a file
201 * Prefix - Path prefix for output
202 * OutFilename - where the filename is returned
206 * DESCRIPTION: Disassemble an entire ACPI table
208 *****************************************************************************/
218 ACPI_STATUS GlobalStatus = AE_OK;
219 char *DisasmFilename = NULL;
220 char *ExternalFilename;
221 ACPI_EXTERNAL_FILE *ExternalFileList = AcpiGbl_ExternalFileList;
223 ACPI_TABLE_HEADER *Table = NULL;
224 ACPI_TABLE_HEADER *ExternalTable;
225 ACPI_OWNER_ID OwnerId;
229 * Input: AML code from either a file or via GetTables (memory or
234 Status = AcpiDbGetTableFromFile (Filename, &Table, FALSE);
235 if (ACPI_FAILURE (Status))
241 * External filenames separated by commas
242 * Example: iasl -e file1,file2,file3 -d xxx.aml
244 while (ExternalFileList)
246 ExternalFilename = ExternalFileList->Path;
247 if (!strcmp (ExternalFilename, Filename))
249 /* Next external file */
251 ExternalFileList = ExternalFileList->Next;
255 Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable, TRUE);
256 if (ACPI_FAILURE (Status))
258 if (Status == AE_TYPE)
260 ExternalFileList = ExternalFileList->Next;
261 GlobalStatus = AE_TYPE;
268 /* Load external table for symbol resolution */
272 Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
273 if (ACPI_FAILURE (Status))
275 AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
276 AcpiFormatException (Status));
281 * Load namespace from names created within control methods
282 * Set owner id of nodes in external table
284 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
285 AcpiGbl_RootNode, OwnerId);
286 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
289 /* Next external file */
291 ExternalFileList = ExternalFileList->Next;
294 if (ACPI_FAILURE (GlobalStatus))
296 return (GlobalStatus);
299 /* Clear external list generated by Scope in external tables */
301 if (AcpiGbl_ExternalFileList)
303 AcpiDmClearExternalList ();
306 /* Load any externals defined in the optional external ref file */
308 AcpiDmGetExternalsFromFile ();
312 Status = AdGetLocalTables ();
313 if (ACPI_FAILURE (Status))
315 AcpiOsPrintf ("Could not get ACPI tables, %s\n",
316 AcpiFormatException (Status));
320 if (!AcpiGbl_DbOpt_Disasm)
325 /* Obtained the local tables, just disassemble the DSDT */
327 Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
328 if (ACPI_FAILURE (Status))
330 AcpiOsPrintf ("Could not get DSDT, %s\n",
331 AcpiFormatException (Status));
335 AcpiOsPrintf ("\nDisassembly of DSDT\n");
336 Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
340 * Output: ASL code. Redirect to a file if requested
344 /* Create/Open a disassembly output file */
346 DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
349 fprintf (stderr, "Could not generate output filename\n");
354 File = fopen (DisasmFilename, "w+");
357 fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
362 AcpiOsRedirectOutput (File);
365 *OutFilename = DisasmFilename;
367 /* ForceAmlDisassembly means to assume the table contains valid AML */
369 if (!AcpiGbl_ForceAmlDisassembly && !AcpiUtIsAmlTable (Table))
371 AdDisassemblerHeader (Filename, ACPI_IS_DATA_TABLE);
372 AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
374 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength] "
375 "FieldName : FieldValue\n */\n\n");
377 AcpiDmDumpDataTable (Table);
378 fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
383 fprintf (stderr, "Formatted output: %s - %u bytes\n",
384 DisasmFilename, CmGetFileSize (File));
389 /* Always parse the tables, only option is what to display */
391 Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
392 if (ACPI_FAILURE (Status))
394 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
395 AcpiFormatException (Status));
399 if (AslCompilerdebug)
401 AcpiOsPrintf ("/**** Before second load\n");
405 NsSetupNamespaceListing (File);
406 NsDisplayNamespace ();
408 AcpiOsPrintf ("*****/\n");
411 /* Load namespace from names created within control methods */
413 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
414 AcpiGbl_RootNode, OwnerId);
417 * Cross reference the namespace here, in order to
418 * generate External() statements
420 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
421 AcpiGbl_RootNode, OwnerId);
423 if (AslCompilerdebug)
425 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
428 /* Find possible calls to external control methods */
430 AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
433 * If we found any external control methods, we must reparse
434 * the entire tree with the new information (namely, the
435 * number of arguments per method)
437 if (AcpiDmGetExternalMethodCount ())
440 "\nFound %u external control methods, "
441 "reparsing with new information\n",
442 AcpiDmGetExternalMethodCount ());
444 /* Reparse, rebuild namespace */
446 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
447 AcpiGbl_ParseOpRoot = NULL;
448 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
450 AcpiGbl_RootNode = NULL;
451 AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
452 AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
453 AcpiGbl_RootNodeStruct.Type = ACPI_TYPE_DEVICE;
454 AcpiGbl_RootNodeStruct.Parent = NULL;
455 AcpiGbl_RootNodeStruct.Child = NULL;
456 AcpiGbl_RootNodeStruct.Peer = NULL;
457 AcpiGbl_RootNodeStruct.Object = NULL;
458 AcpiGbl_RootNodeStruct.Flags = 0;
460 Status = AcpiNsRootInitialize ();
462 /* New namespace, add the external definitions first */
464 AcpiDmAddExternalsToNamespace ();
466 /* Parse the table again. No need to reload it, however */
468 Status = AdParseTable (Table, NULL, FALSE, FALSE);
469 if (ACPI_FAILURE (Status))
471 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
472 AcpiFormatException (Status));
476 /* Cross reference the namespace again */
478 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
479 AcpiGbl_RootNode, OwnerId);
481 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
482 AcpiGbl_RootNode, OwnerId);
484 if (AslCompilerdebug)
486 AcpiOsPrintf ("/**** After second load and resource conversion\n");
489 NsSetupNamespaceListing (File);
490 NsDisplayNamespace ();
492 AcpiOsPrintf ("*****/\n");
494 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
499 * Now that the namespace is finalized, we can perform namespace
502 * 1) Convert fixed-offset references to resource descriptors
503 * to symbolic references (Note: modifies namespace)
505 AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
507 /* Optional displays */
509 if (AcpiGbl_DbOpt_Disasm)
511 /* This is the real disassembly */
513 AdDisplayTables (Filename, Table);
515 /* Dump hex table if requested (-vt) */
517 AcpiDmDumpDataTable (Table);
519 fprintf (stderr, "Disassembly completed\n");
522 fprintf (stderr, "ASL Output: %s - %u bytes\n",
523 DisasmFilename, CmGetFileSize (File));
528 fprintf (stderr, "%14s %s - %u bytes\n",
529 Gbl_Files[ASL_FILE_MAP_OUTPUT].ShortDescription,
530 Gbl_Files[ASL_FILE_MAP_OUTPUT].Filename,
531 FlGetFileSize (ASL_FILE_MAP_OUTPUT));
538 if (Table && !AcpiGbl_ForceAmlDisassembly &&!AcpiUtIsAmlTable (Table))
545 if (AslCompilerdebug) /* Display final namespace, with transforms */
547 NsSetupNamespaceListing (File);
548 NsDisplayNamespace ();
552 AcpiOsRedirectOutput (stdout);
555 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
556 AcpiGbl_ParseOpRoot = NULL;
561 /******************************************************************************
563 * FUNCTION: AdDisassemblerHeader
565 * PARAMETERS: Filename - Input file for the table
566 * TableType - Either AML or DataTable
570 * DESCRIPTION: Create the disassembler header, including ACPICA signon with
571 * current time and date.
573 *****************************************************************************/
576 AdDisassemblerHeader (
585 /* Header and input table info */
587 AcpiOsPrintf ("/*\n");
588 AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));
590 if (TableType == ACPI_IS_AML_TABLE)
592 if (AcpiGbl_CstyleDisassembly)
595 " * Disassembling to symbolic ASL+ operators\n"
601 " * Disassembling to non-symbolic legacy ASL operators\n"
606 AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
607 AcpiOsPrintf (" *\n");
611 /******************************************************************************
613 * FUNCTION: AdCreateTableHeader
615 * PARAMETERS: Filename - Input file for the table
616 * Table - Pointer to the raw table
620 * DESCRIPTION: Create the ASL table header, including ACPICA signon with
621 * current time and date.
623 *****************************************************************************/
626 AdCreateTableHeader (
628 ACPI_TABLE_HEADER *Table)
635 * Print file header and dump original table header
637 AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
639 AcpiOsPrintf (" * Original Table Header:\n");
640 AcpiOsPrintf (" * Signature \"%4.4s\"\n", Table->Signature);
641 AcpiOsPrintf (" * Length 0x%8.8X (%u)\n", Table->Length, Table->Length);
643 /* Print and validate the revision */
645 AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);
647 switch (Table->Revision)
651 AcpiOsPrintf (" **** Invalid Revision");
656 /* Revision of DSDT controls the ACPI integer width */
658 if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
660 AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
670 /* Print and validate the table checksum */
672 AcpiOsPrintf (" * Checksum 0x%2.2X", Table->Checksum);
674 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
677 AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
678 (UINT8) (Table->Checksum - Checksum));
682 AcpiOsPrintf (" * OEM ID \"%.6s\"\n", Table->OemId);
683 AcpiOsPrintf (" * OEM Table ID \"%.8s\"\n", Table->OemTableId);
684 AcpiOsPrintf (" * OEM Revision 0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
685 AcpiOsPrintf (" * Compiler ID \"%.4s\"\n", Table->AslCompilerId);
686 AcpiOsPrintf (" * Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
687 AcpiOsPrintf (" */\n");
689 /* Create AML output filename based on input filename */
693 NewFilename = FlGenerateFilename (Filename, "aml");
697 NewFilename = UtStringCacheCalloc (9);
700 strncat (NewFilename, Table->Signature, 4);
701 strcat (NewFilename, ".aml");
707 AcpiOsPrintf (" **** Could not generate AML output filename\n");
711 /* Open the ASL definition block */
714 "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
715 NewFilename, Table->Signature, Table->Revision,
716 Table->OemId, Table->OemTableId, Table->OemRevision);
720 /******************************************************************************
722 * FUNCTION: AdDisplayTables
724 * PARAMETERS: Filename - Input file for the table
725 * Table - Pointer to the raw table
729 * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
731 *****************************************************************************/
736 ACPI_TABLE_HEADER *Table)
740 if (!AcpiGbl_ParseOpRoot)
742 return (AE_NOT_EXIST);
745 if (!AcpiGbl_DbOpt_Verbose)
747 AdCreateTableHeader (Filename, Table);
750 AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
751 MpEmitMappingInfo ();
753 if (AcpiGbl_DbOpt_Verbose)
755 AcpiOsPrintf ("\n\nTable Header:\n");
756 AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
757 DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
759 AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
760 AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
761 Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
768 /*******************************************************************************
770 * FUNCTION: AdStoreTable
772 * PARAMETERS: Table - Table header
773 * TableIndex - Where the table index is returned
775 * RETURN: Status and table index.
777 * DESCRIPTION: Add an ACPI table to the global table list
779 ******************************************************************************/
783 ACPI_TABLE_HEADER *Table,
787 ACPI_TABLE_DESC *TableDesc;
790 Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
791 if (ACPI_FAILURE (Status))
796 /* Initialize added table */
798 AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
799 ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
800 Status = AcpiTbValidateTable (TableDesc);
805 /******************************************************************************
807 * FUNCTION: AdGetLocalTables
813 * DESCRIPTION: Get the ACPI tables from either memory or a file
815 *****************************************************************************/
822 ACPI_TABLE_HEADER TableHeader;
823 ACPI_TABLE_HEADER *NewTable;
827 /* Get the DSDT via table override */
829 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
830 AcpiOsTableOverride (&TableHeader, &NewTable);
833 fprintf (stderr, "Could not obtain DSDT\n");
834 return (AE_NO_ACPI_TABLES);
837 AdWriteTable (NewTable, NewTable->Length,
838 ACPI_SIG_DSDT, NewTable->OemTableId);
840 /* Store DSDT in the Table Manager */
842 Status = AdStoreTable (NewTable, &TableIndex);
843 if (ACPI_FAILURE (Status))
845 fprintf (stderr, "Could not store DSDT\n");
846 return (AE_NO_ACPI_TABLES);
853 /******************************************************************************
855 * FUNCTION: AdParseTable
857 * PARAMETERS: Table - Pointer to the raw table
858 * OwnerId - Returned OwnerId of the table
859 * LoadTable - If add table to the global table list
860 * External - If this is an external table
864 * DESCRIPTION: Parse the DSDT.
866 *****************************************************************************/
870 ACPI_TABLE_HEADER *Table,
871 ACPI_OWNER_ID *OwnerId,
875 ACPI_STATUS Status = AE_OK;
876 ACPI_WALK_STATE *WalkState;
884 return (AE_NOT_EXIST);
887 /* Pass 1: Parse everything except control method bodies */
889 fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
891 AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
892 AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
894 /* Create the root object */
896 AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
897 if (!AcpiGbl_ParseOpRoot)
899 return (AE_NO_MEMORY);
902 /* Create and initialize a new walk state */
904 WalkState = AcpiDsCreateWalkState (0,
905 AcpiGbl_ParseOpRoot, NULL, NULL);
908 return (AE_NO_MEMORY);
911 Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
912 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
913 if (ACPI_FAILURE (Status))
918 WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
919 WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
921 Status = AcpiPsParseAml (WalkState);
922 if (ACPI_FAILURE (Status))
927 /* If LoadTable is FALSE, we are parsing the last loaded table */
929 TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
935 Status = AdStoreTable (Table, &TableIndex);
936 if (ACPI_FAILURE (Status))
940 Status = AcpiTbAllocateOwnerId (TableIndex);
941 if (ACPI_FAILURE (Status))
947 Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
948 if (ACPI_FAILURE (Status))
955 fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
957 Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
958 if (ACPI_FAILURE (Status))
963 /* No need to parse control methods of external table */
971 * Pass 3: Parse control methods and link their parse trees
972 * into the main parse tree
975 "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
976 Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
977 fprintf (stderr, "\n");
979 /* Process Resource Templates */
981 AcpiDmFindResources (AcpiGbl_ParseOpRoot);
983 fprintf (stderr, "Parsing completed\n");