1 /******************************************************************************
3 * Module Name: adisasm - Application-level disassembler routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
60 #define _COMPONENT ACPI_TOOLS
61 ACPI_MODULE_NAME ("adisasm")
64 * Older versions of Bison won't emit this external in the generated header.
65 * Newer versions do emit the external, so we don't need to do it.
67 #ifndef ASLCOMPILER_ASLCOMPILERPARSE_H
68 extern int AslCompilerdebug;
76 NsSetupNamespaceListing (
80 /* Local prototypes */
85 ACPI_TABLE_HEADER *Table);
87 /* Stubs for ASL compiler */
89 #ifndef ACPI_ASL_COMPILER
92 ACPI_PARSE_OBJECT *Op,
93 ACPI_WALK_STATE *WalkState)
101 ACPI_WALK_STATE *WalkState)
110 ACPI_NAMESPACE_NODE *Node)
112 return (AE_NOT_IMPLEMENTED);
116 AcpiDsRestartControlMethod (
117 ACPI_WALK_STATE *WalkState,
118 ACPI_OPERAND_OBJECT *ReturnDesc)
124 AcpiDsTerminateControlMethod (
125 ACPI_OPERAND_OBJECT *MethodDesc,
126 ACPI_WALK_STATE *WalkState)
132 AcpiDsCallControlMethod (
133 ACPI_THREAD_STATE *Thread,
134 ACPI_WALK_STATE *WalkState,
135 ACPI_PARSE_OBJECT *Op)
141 AcpiDsMethodDataInitArgs (
142 ACPI_OPERAND_OBJECT **Params,
143 UINT32 MaxParamCount,
144 ACPI_WALK_STATE *WalkState)
150 static ACPI_TABLE_DESC LocalTables[1];
151 static ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
154 /*******************************************************************************
156 * FUNCTION: AdInitialize
162 * DESCRIPTION: ACPICA and local initialization
164 ******************************************************************************/
173 /* ACPICA subsystem initialization */
175 Status = AcpiOsInitialize ();
176 if (ACPI_FAILURE (Status))
181 Status = AcpiUtInitGlobals ();
182 if (ACPI_FAILURE (Status))
187 Status = AcpiUtMutexInitialize ();
188 if (ACPI_FAILURE (Status))
193 Status = AcpiNsRootInitialize ();
194 if (ACPI_FAILURE (Status))
199 /* Setup the Table Manager (cheat - there is no RSDT) */
201 AcpiGbl_RootTableList.MaxTableCount = 1;
202 AcpiGbl_RootTableList.CurrentTableCount = 0;
203 AcpiGbl_RootTableList.Tables = LocalTables;
209 /******************************************************************************
211 * FUNCTION: AdAmlDisassemble
213 * PARAMETERS: Filename - AML input filename
214 * OutToFile - TRUE if output should go to a file
215 * Prefix - Path prefix for output
216 * OutFilename - where the filename is returned
220 * DESCRIPTION: Disassemble an entire ACPI table
222 *****************************************************************************/
232 char *DisasmFilename = NULL;
233 char *ExternalFilename;
234 ACPI_EXTERNAL_FILE *ExternalFileList = AcpiGbl_ExternalFileList;
236 ACPI_TABLE_HEADER *Table = NULL;
237 ACPI_TABLE_HEADER *ExternalTable;
238 ACPI_OWNER_ID OwnerId;
242 * Input: AML code from either a file or via GetTables (memory or
247 Status = AcpiDbGetTableFromFile (Filename, &Table);
248 if (ACPI_FAILURE (Status))
254 * External filenames separated by commas
255 * Example: iasl -e file1,file2,file3 -d xxx.aml
257 while (ExternalFileList)
259 ExternalFilename = ExternalFileList->Path;
260 if (!ACPI_STRCMP (ExternalFilename, Filename))
262 /* Next external file */
264 ExternalFileList = ExternalFileList->Next;
268 Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable);
269 if (ACPI_FAILURE (Status))
274 /* Load external table for symbol resolution */
278 Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
279 if (ACPI_FAILURE (Status))
281 AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
282 AcpiFormatException (Status));
287 * Load namespace from names created within control methods
288 * Set owner id of nodes in external table
290 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
291 AcpiGbl_RootNode, OwnerId);
292 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
295 /* Next external file */
297 ExternalFileList = ExternalFileList->Next;
300 /* Clear external list generated by Scope in external tables */
302 if (AcpiGbl_ExternalFileList)
304 AcpiDmClearExternalList ();
307 /* Load any externals defined in the optional external ref file */
309 AcpiDmGetExternalsFromFile ();
313 Status = AdGetLocalTables ();
314 if (ACPI_FAILURE (Status))
316 AcpiOsPrintf ("Could not get ACPI tables, %s\n",
317 AcpiFormatException (Status));
321 if (!AcpiGbl_DbOpt_disasm)
326 /* Obtained the local tables, just disassemble the DSDT */
328 Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
329 if (ACPI_FAILURE (Status))
331 AcpiOsPrintf ("Could not get DSDT, %s\n",
332 AcpiFormatException (Status));
336 AcpiOsPrintf ("\nDisassembly of DSDT\n");
337 Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
341 * Output: ASL code. Redirect to a file if requested
345 /* Create/Open a disassembly output file */
347 DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
350 fprintf (stderr, "Could not generate output filename\n");
355 File = fopen (DisasmFilename, "w+");
358 fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
360 ACPI_FREE (DisasmFilename);
364 AcpiOsRedirectOutput (File);
367 *OutFilename = DisasmFilename;
369 if (!AcpiUtIsAmlTable (Table))
371 AdDisassemblerHeader (Filename);
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",
380 fprintf (stderr, "Formatted output: %s - %u bytes\n",
381 DisasmFilename, CmGetFileSize (File));
385 /* Always parse the tables, only option is what to display */
387 Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
388 if (ACPI_FAILURE (Status))
390 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
391 AcpiFormatException (Status));
395 if (AslCompilerdebug)
397 AcpiOsPrintf ("/**** Before second load\n");
399 NsSetupNamespaceListing (File);
400 NsDisplayNamespace ();
401 AcpiOsPrintf ("*****/\n");
404 /* Load namespace from names created within control methods */
406 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
407 AcpiGbl_RootNode, OwnerId);
410 * Cross reference the namespace here, in order to
411 * generate External() statements
413 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
414 AcpiGbl_RootNode, OwnerId);
416 if (AslCompilerdebug)
418 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
421 /* Find possible calls to external control methods */
423 AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
426 * If we found any external control methods, we must reparse
427 * the entire tree with the new information (namely, the
428 * number of arguments per method)
430 if (AcpiDmGetExternalMethodCount ())
433 "\nFound %u external control methods, "
434 "reparsing with new information\n",
435 AcpiDmGetExternalMethodCount ());
437 /* Reparse, rebuild namespace */
439 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
440 AcpiGbl_ParseOpRoot = NULL;
441 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
443 AcpiGbl_RootNode = NULL;
444 AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
445 AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
446 AcpiGbl_RootNodeStruct.Type = ACPI_TYPE_DEVICE;
447 AcpiGbl_RootNodeStruct.Parent = NULL;
448 AcpiGbl_RootNodeStruct.Child = NULL;
449 AcpiGbl_RootNodeStruct.Peer = NULL;
450 AcpiGbl_RootNodeStruct.Object = NULL;
451 AcpiGbl_RootNodeStruct.Flags = 0;
453 Status = AcpiNsRootInitialize ();
455 /* New namespace, add the external definitions first */
457 AcpiDmAddExternalsToNamespace ();
459 /* Parse the table again. No need to reload it, however */
461 Status = AdParseTable (Table, NULL, FALSE, FALSE);
462 if (ACPI_FAILURE (Status))
464 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
465 AcpiFormatException (Status));
469 /* Cross reference the namespace again */
471 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
472 AcpiGbl_RootNode, OwnerId);
474 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
475 AcpiGbl_RootNode, OwnerId);
477 if (AslCompilerdebug)
479 AcpiOsPrintf ("/**** After second load and resource conversion\n");
480 NsSetupNamespaceListing (File);
481 NsDisplayNamespace ();
482 AcpiOsPrintf ("*****/\n");
484 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
489 * Now that the namespace is finalized, we can perform namespace
492 * 1) Convert fixed-offset references to resource descriptors
493 * to symbolic references (Note: modifies namespace)
495 AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
497 /* Optional displays */
499 if (AcpiGbl_DbOpt_disasm)
501 /* This is the real disassembly */
503 AdDisplayTables (Filename, Table);
505 /* Dump hex table if requested (-vt) */
507 AcpiDmDumpDataTable (Table);
509 fprintf (stderr, "Disassembly completed\n");
510 fprintf (stderr, "ASL Output: %s - %u bytes\n",
511 DisasmFilename, CmGetFileSize (File));
517 if (Table && !AcpiUtIsAmlTable (Table))
522 if (OutToFile && File)
524 if (AslCompilerdebug) /* Display final namespace, with transforms */
526 NsSetupNamespaceListing (File);
527 NsDisplayNamespace ();
531 AcpiOsRedirectOutput (stdout);
534 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
535 AcpiGbl_ParseOpRoot = NULL;
540 /******************************************************************************
542 * FUNCTION: AdDisassemblerHeader
544 * PARAMETERS: Filename - Input file for the table
548 * DESCRIPTION: Create the disassembler header, including ACPICA signon with
549 * current time and date.
551 *****************************************************************************/
554 AdDisassemblerHeader (
561 /* Header and input table info */
563 AcpiOsPrintf ("/*\n");
564 AcpiOsPrintf (ACPI_COMMON_HEADER ("AML Disassembler", " * "));
566 AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
567 AcpiOsPrintf (" *\n");
571 /******************************************************************************
573 * FUNCTION: AdCreateTableHeader
575 * PARAMETERS: Filename - Input file for the table
576 * Table - Pointer to the raw table
580 * DESCRIPTION: Create the ASL table header, including ACPICA signon with
581 * current time and date.
583 *****************************************************************************/
586 AdCreateTableHeader (
588 ACPI_TABLE_HEADER *Table)
595 * Print file header and dump original table header
597 AdDisassemblerHeader (Filename);
599 AcpiOsPrintf (" * Original Table Header:\n");
600 AcpiOsPrintf (" * Signature \"%4.4s\"\n", Table->Signature);
601 AcpiOsPrintf (" * Length 0x%8.8X (%u)\n", Table->Length, Table->Length);
603 /* Print and validate the revision */
605 AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);
607 switch (Table->Revision)
611 AcpiOsPrintf (" **** Invalid Revision");
616 /* Revision of DSDT controls the ACPI integer width */
618 if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
620 AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
630 /* Print and validate the table checksum */
632 AcpiOsPrintf (" * Checksum 0x%2.2X", Table->Checksum);
634 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
637 AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
638 (UINT8) (Table->Checksum - Checksum));
642 AcpiOsPrintf (" * OEM ID \"%.6s\"\n", Table->OemId);
643 AcpiOsPrintf (" * OEM Table ID \"%.8s\"\n", Table->OemTableId);
644 AcpiOsPrintf (" * OEM Revision 0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
645 AcpiOsPrintf (" * Compiler ID \"%.4s\"\n", Table->AslCompilerId);
646 AcpiOsPrintf (" * Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
647 AcpiOsPrintf (" */\n");
649 /* Create AML output filename based on input filename */
653 NewFilename = FlGenerateFilename (Filename, "aml");
657 NewFilename = ACPI_ALLOCATE_ZEROED (9);
660 strncat (NewFilename, Table->Signature, 4);
661 strcat (NewFilename, ".aml");
667 AcpiOsPrintf (" **** Could not generate AML output filename\n");
671 /* Open the ASL definition block */
674 "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
675 NewFilename, Table->Signature, Table->Revision,
676 Table->OemId, Table->OemTableId, Table->OemRevision);
678 ACPI_FREE (NewFilename);
682 /******************************************************************************
684 * FUNCTION: AdDisplayTables
686 * PARAMETERS: Filename - Input file for the table
687 * Table - Pointer to the raw table
691 * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
693 *****************************************************************************/
698 ACPI_TABLE_HEADER *Table)
702 if (!AcpiGbl_ParseOpRoot)
704 return (AE_NOT_EXIST);
707 if (!AcpiGbl_DbOpt_verbose)
709 AdCreateTableHeader (Filename, Table);
712 AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
714 if (AcpiGbl_DbOpt_verbose)
716 AcpiOsPrintf ("\n\nTable Header:\n");
717 AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
718 DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
720 AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
721 AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
722 Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
729 /******************************************************************************
731 * FUNCTION: AdGetLocalTables
737 * DESCRIPTION: Get the ACPI tables from either memory or a file
739 *****************************************************************************/
746 ACPI_TABLE_HEADER TableHeader;
747 ACPI_TABLE_HEADER *NewTable;
751 /* Get the DSDT via table override */
753 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
754 AcpiOsTableOverride (&TableHeader, &NewTable);
757 fprintf (stderr, "Could not obtain DSDT\n");
758 return (AE_NO_ACPI_TABLES);
761 AdWriteTable (NewTable, NewTable->Length,
762 ACPI_SIG_DSDT, NewTable->OemTableId);
764 /* Store DSDT in the Table Manager */
766 Status = AcpiTbStoreTable (0, NewTable, NewTable->Length,
768 if (ACPI_FAILURE (Status))
770 fprintf (stderr, "Could not store DSDT\n");
771 return (AE_NO_ACPI_TABLES);
778 /******************************************************************************
780 * FUNCTION: AdParseTable
782 * PARAMETERS: Table - Pointer to the raw table
783 * OwnerId - Returned OwnerId of the table
784 * LoadTable - If add table to the global table list
785 * External - If this is an external table
789 * DESCRIPTION: Parse the DSDT.
791 *****************************************************************************/
795 ACPI_TABLE_HEADER *Table,
796 ACPI_OWNER_ID *OwnerId,
800 ACPI_STATUS Status = AE_OK;
801 ACPI_WALK_STATE *WalkState;
809 return (AE_NOT_EXIST);
812 /* Pass 1: Parse everything except control method bodies */
814 fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
816 AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
817 AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
819 /* Create the root object */
821 AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp ();
822 if (!AcpiGbl_ParseOpRoot)
824 return (AE_NO_MEMORY);
827 /* Create and initialize a new walk state */
829 WalkState = AcpiDsCreateWalkState (0,
830 AcpiGbl_ParseOpRoot, NULL, NULL);
833 return (AE_NO_MEMORY);
836 Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
837 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
838 if (ACPI_FAILURE (Status))
843 WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
844 WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
846 Status = AcpiPsParseAml (WalkState);
847 if (ACPI_FAILURE (Status))
852 /* If LoadTable is FALSE, we are parsing the last loaded table */
854 TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
860 Status = AcpiTbStoreTable ((ACPI_PHYSICAL_ADDRESS) Table, Table,
861 Table->Length, ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
863 if (ACPI_FAILURE (Status))
867 Status = AcpiTbAllocateOwnerId (TableIndex);
868 if (ACPI_FAILURE (Status))
874 Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
875 if (ACPI_FAILURE (Status))
882 fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
884 Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
885 if (ACPI_FAILURE (Status))
890 /* No need to parse control methods of external table */
898 * Pass 3: Parse control methods and link their parse trees
899 * into the main parse tree
902 "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
903 Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
904 fprintf (stderr, "\n");
906 /* Process Resource Templates */
908 AcpiDmFindResources (AcpiGbl_ParseOpRoot);
910 fprintf (stderr, "Parsing completed\n");