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.
44 #include "aslcompiler.h"
57 #define _COMPONENT ACPI_TOOLS
58 ACPI_MODULE_NAME ("adisasm")
60 /* Local prototypes */
65 ACPI_TABLE_HEADER *Table);
67 /* Stubs for ASL compiler */
69 #ifndef ACPI_ASL_COMPILER
72 ACPI_PARSE_OBJECT *Op,
73 ACPI_WALK_STATE *WalkState)
81 ACPI_WALK_STATE *WalkState)
90 ACPI_NAMESPACE_NODE *Node)
92 return (AE_NOT_IMPLEMENTED);
96 AcpiDsRestartControlMethod (
97 ACPI_WALK_STATE *WalkState,
98 ACPI_OPERAND_OBJECT *ReturnDesc)
104 AcpiDsTerminateControlMethod (
105 ACPI_OPERAND_OBJECT *MethodDesc,
106 ACPI_WALK_STATE *WalkState)
112 AcpiDsCallControlMethod (
113 ACPI_THREAD_STATE *Thread,
114 ACPI_WALK_STATE *WalkState,
115 ACPI_PARSE_OBJECT *Op)
121 AcpiDsMethodDataInitArgs (
122 ACPI_OPERAND_OBJECT **Params,
123 UINT32 MaxParamCount,
124 ACPI_WALK_STATE *WalkState)
130 static ACPI_TABLE_DESC LocalTables[1];
131 static ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
134 /*******************************************************************************
136 * FUNCTION: AdInitialize
142 * DESCRIPTION: ACPICA and local initialization
144 ******************************************************************************/
153 /* ACPICA subsystem initialization */
155 Status = AcpiOsInitialize ();
156 if (ACPI_FAILURE (Status))
161 Status = AcpiUtInitGlobals ();
162 if (ACPI_FAILURE (Status))
167 Status = AcpiUtMutexInitialize ();
168 if (ACPI_FAILURE (Status))
173 Status = AcpiNsRootInitialize ();
174 if (ACPI_FAILURE (Status))
179 /* Setup the Table Manager (cheat - there is no RSDT) */
181 AcpiGbl_RootTableList.MaxTableCount = 1;
182 AcpiGbl_RootTableList.CurrentTableCount = 0;
183 AcpiGbl_RootTableList.Tables = LocalTables;
189 /******************************************************************************
191 * FUNCTION: AdAmlDisassemble
193 * PARAMETERS: Filename - AML input filename
194 * OutToFile - TRUE if output should go to a file
195 * Prefix - Path prefix for output
196 * OutFilename - where the filename is returned
200 * DESCRIPTION: Disassemble an entire ACPI table
202 *****************************************************************************/
212 char *DisasmFilename = NULL;
213 char *ExternalFilename;
214 ACPI_EXTERNAL_FILE *ExternalFileList = AcpiGbl_ExternalFileList;
216 ACPI_TABLE_HEADER *Table = NULL;
217 ACPI_TABLE_HEADER *ExternalTable;
218 ACPI_OWNER_ID OwnerId;
222 * Input: AML code from either a file or via GetTables (memory or
227 Status = AcpiDbGetTableFromFile (Filename, &Table);
228 if (ACPI_FAILURE (Status))
234 * External filenames separated by commas
235 * Example: iasl -e file1,file2,file3 -d xxx.aml
237 while (ExternalFileList)
239 ExternalFilename = ExternalFileList->Path;
240 if (!ACPI_STRCMP (ExternalFilename, Filename))
242 /* Next external file */
244 ExternalFileList = ExternalFileList->Next;
248 Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable);
249 if (ACPI_FAILURE (Status))
254 /* Load external table for symbol resolution */
258 Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
259 if (ACPI_FAILURE (Status))
261 AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
262 AcpiFormatException (Status));
267 * Load namespace from names created within control methods
268 * Set owner id of nodes in external table
270 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
271 AcpiGbl_RootNode, OwnerId);
272 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
275 /* Next external file */
277 ExternalFileList = ExternalFileList->Next;
280 /* Clear external list generated by Scope in external tables */
282 if (AcpiGbl_ExternalFileList)
284 AcpiDmClearExternalList ();
287 /* Load any externals defined in the optional external ref file */
289 AcpiDmGetExternalsFromFile ();
293 Status = AdGetLocalTables ();
294 if (ACPI_FAILURE (Status))
296 AcpiOsPrintf ("Could not get ACPI tables, %s\n",
297 AcpiFormatException (Status));
301 if (!AcpiGbl_DbOpt_disasm)
306 /* Obtained the local tables, just disassemble the DSDT */
308 Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
309 if (ACPI_FAILURE (Status))
311 AcpiOsPrintf ("Could not get DSDT, %s\n",
312 AcpiFormatException (Status));
316 AcpiOsPrintf ("\nDisassembly of DSDT\n");
317 Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
321 * Output: ASL code. Redirect to a file if requested
325 /* Create/Open a disassembly output file */
327 DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
330 fprintf (stderr, "Could not generate output filename\n");
335 File = fopen (DisasmFilename, "w+");
338 fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
340 ACPI_FREE (DisasmFilename);
344 AcpiOsRedirectOutput (File);
347 *OutFilename = DisasmFilename;
349 if (!AcpiUtIsAmlTable (Table))
351 AdDisassemblerHeader (Filename);
352 AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
354 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength] "
355 "FieldName : FieldValue\n */\n\n");
357 AcpiDmDumpDataTable (Table);
358 fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
360 fprintf (stderr, "Formatted output: %s - %u bytes\n",
361 DisasmFilename, CmGetFileSize (File));
365 /* Always parse the tables, only option is what to display */
367 Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
368 if (ACPI_FAILURE (Status))
370 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
371 AcpiFormatException (Status));
375 if (AslCompilerdebug)
377 AcpiOsPrintf ("/**** Before second load\n");
379 NsSetupNamespaceListing (File);
380 NsDisplayNamespace ();
381 AcpiOsPrintf ("*****/\n");
384 /* Load namespace from names created within control methods */
386 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
387 AcpiGbl_RootNode, OwnerId);
390 * Cross reference the namespace here, in order to
391 * generate External() statements
393 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
394 AcpiGbl_RootNode, OwnerId);
396 if (AslCompilerdebug)
398 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
401 /* Find possible calls to external control methods */
403 AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
406 * If we found any external control methods, we must reparse
407 * the entire tree with the new information (namely, the
408 * number of arguments per method)
410 if (AcpiDmGetExternalMethodCount ())
413 "\nFound %u external control methods, "
414 "reparsing with new information\n",
415 AcpiDmGetExternalMethodCount ());
417 /* Reparse, rebuild namespace */
419 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
420 AcpiGbl_ParseOpRoot = NULL;
421 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
423 AcpiGbl_RootNode = NULL;
424 AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
425 AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
426 AcpiGbl_RootNodeStruct.Type = ACPI_TYPE_DEVICE;
427 AcpiGbl_RootNodeStruct.Parent = NULL;
428 AcpiGbl_RootNodeStruct.Child = NULL;
429 AcpiGbl_RootNodeStruct.Peer = NULL;
430 AcpiGbl_RootNodeStruct.Object = NULL;
431 AcpiGbl_RootNodeStruct.Flags = 0;
433 Status = AcpiNsRootInitialize ();
435 /* New namespace, add the external definitions first */
437 AcpiDmAddExternalsToNamespace ();
439 /* Parse the table again. No need to reload it, however */
441 Status = AdParseTable (Table, NULL, FALSE, FALSE);
442 if (ACPI_FAILURE (Status))
444 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
445 AcpiFormatException (Status));
449 /* Cross reference the namespace again */
451 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
452 AcpiGbl_RootNode, OwnerId);
454 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
455 AcpiGbl_RootNode, OwnerId);
457 if (AslCompilerdebug)
459 AcpiOsPrintf ("/**** After second load and resource conversion\n");
460 NsSetupNamespaceListing (File);
461 NsDisplayNamespace ();
462 AcpiOsPrintf ("*****/\n");
464 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
469 * Now that the namespace is finalized, we can perform namespace
472 * 1) Convert fixed-offset references to resource descriptors
473 * to symbolic references (Note: modifies namespace)
475 AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
477 /* Optional displays */
479 if (AcpiGbl_DbOpt_disasm)
481 /* This is the real disassembly */
483 AdDisplayTables (Filename, Table);
485 /* Dump hex table if requested (-vt) */
487 AcpiDmDumpDataTable (Table);
489 fprintf (stderr, "Disassembly completed\n");
490 fprintf (stderr, "ASL Output: %s - %u bytes\n",
491 DisasmFilename, CmGetFileSize (File));
497 if (Table && !AcpiUtIsAmlTable (Table))
502 if (OutToFile && File)
504 if (AslCompilerdebug) /* Display final namespace, with transforms */
506 NsSetupNamespaceListing (File);
507 NsDisplayNamespace ();
511 AcpiOsRedirectOutput (stdout);
514 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
515 AcpiGbl_ParseOpRoot = NULL;
520 /******************************************************************************
522 * FUNCTION: AdDisassemblerHeader
524 * PARAMETERS: Filename - Input file for the table
528 * DESCRIPTION: Create the disassembler header, including ACPICA signon with
529 * current time and date.
531 *****************************************************************************/
534 AdDisassemblerHeader (
541 /* Header and input table info */
543 AcpiOsPrintf ("/*\n");
544 AcpiOsPrintf (ACPI_COMMON_HEADER ("AML Disassembler", " * "));
546 AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
547 AcpiOsPrintf (" *\n");
551 /******************************************************************************
553 * FUNCTION: AdCreateTableHeader
555 * PARAMETERS: Filename - Input file for the table
556 * Table - Pointer to the raw table
560 * DESCRIPTION: Create the ASL table header, including ACPICA signon with
561 * current time and date.
563 *****************************************************************************/
566 AdCreateTableHeader (
568 ACPI_TABLE_HEADER *Table)
575 * Print file header and dump original table header
577 AdDisassemblerHeader (Filename);
579 AcpiOsPrintf (" * Original Table Header:\n");
580 AcpiOsPrintf (" * Signature \"%4.4s\"\n", Table->Signature);
581 AcpiOsPrintf (" * Length 0x%8.8X (%u)\n", Table->Length, Table->Length);
583 /* Print and validate the revision */
585 AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);
587 switch (Table->Revision)
591 AcpiOsPrintf (" **** Invalid Revision");
596 /* Revision of DSDT controls the ACPI integer width */
598 if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
600 AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
610 /* Print and validate the table checksum */
612 AcpiOsPrintf (" * Checksum 0x%2.2X", Table->Checksum);
614 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
617 AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
618 (UINT8) (Table->Checksum - Checksum));
622 AcpiOsPrintf (" * OEM ID \"%.6s\"\n", Table->OemId);
623 AcpiOsPrintf (" * OEM Table ID \"%.8s\"\n", Table->OemTableId);
624 AcpiOsPrintf (" * OEM Revision 0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
625 AcpiOsPrintf (" * Compiler ID \"%.4s\"\n", Table->AslCompilerId);
626 AcpiOsPrintf (" * Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
627 AcpiOsPrintf (" */\n");
629 /* Create AML output filename based on input filename */
633 NewFilename = FlGenerateFilename (Filename, "aml");
637 NewFilename = UtStringCacheCalloc (9);
640 strncat (NewFilename, Table->Signature, 4);
641 strcat (NewFilename, ".aml");
647 AcpiOsPrintf (" **** Could not generate AML output filename\n");
651 /* Open the ASL definition block */
654 "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
655 NewFilename, Table->Signature, Table->Revision,
656 Table->OemId, Table->OemTableId, Table->OemRevision);
660 /******************************************************************************
662 * FUNCTION: AdDisplayTables
664 * PARAMETERS: Filename - Input file for the table
665 * Table - Pointer to the raw table
669 * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
671 *****************************************************************************/
676 ACPI_TABLE_HEADER *Table)
680 if (!AcpiGbl_ParseOpRoot)
682 return (AE_NOT_EXIST);
685 if (!AcpiGbl_DbOpt_verbose)
687 AdCreateTableHeader (Filename, Table);
690 AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
692 if (AcpiGbl_DbOpt_verbose)
694 AcpiOsPrintf ("\n\nTable Header:\n");
695 AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
696 DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
698 AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
699 AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
700 Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
707 /******************************************************************************
709 * FUNCTION: AdGetLocalTables
715 * DESCRIPTION: Get the ACPI tables from either memory or a file
717 *****************************************************************************/
724 ACPI_TABLE_HEADER TableHeader;
725 ACPI_TABLE_HEADER *NewTable;
729 /* Get the DSDT via table override */
731 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
732 AcpiOsTableOverride (&TableHeader, &NewTable);
735 fprintf (stderr, "Could not obtain DSDT\n");
736 return (AE_NO_ACPI_TABLES);
739 AdWriteTable (NewTable, NewTable->Length,
740 ACPI_SIG_DSDT, NewTable->OemTableId);
742 /* Store DSDT in the Table Manager */
744 Status = AcpiTbStoreTable (0, NewTable, NewTable->Length,
746 if (ACPI_FAILURE (Status))
748 fprintf (stderr, "Could not store DSDT\n");
749 return (AE_NO_ACPI_TABLES);
756 /******************************************************************************
758 * FUNCTION: AdParseTable
760 * PARAMETERS: Table - Pointer to the raw table
761 * OwnerId - Returned OwnerId of the table
762 * LoadTable - If add table to the global table list
763 * External - If this is an external table
767 * DESCRIPTION: Parse the DSDT.
769 *****************************************************************************/
773 ACPI_TABLE_HEADER *Table,
774 ACPI_OWNER_ID *OwnerId,
778 ACPI_STATUS Status = AE_OK;
779 ACPI_WALK_STATE *WalkState;
787 return (AE_NOT_EXIST);
790 /* Pass 1: Parse everything except control method bodies */
792 fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
794 AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
795 AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
797 /* Create the root object */
799 AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp ();
800 if (!AcpiGbl_ParseOpRoot)
802 return (AE_NO_MEMORY);
805 /* Create and initialize a new walk state */
807 WalkState = AcpiDsCreateWalkState (0,
808 AcpiGbl_ParseOpRoot, NULL, NULL);
811 return (AE_NO_MEMORY);
814 Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
815 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
816 if (ACPI_FAILURE (Status))
821 WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
822 WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
824 Status = AcpiPsParseAml (WalkState);
825 if (ACPI_FAILURE (Status))
830 /* If LoadTable is FALSE, we are parsing the last loaded table */
832 TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
838 Status = AcpiTbStoreTable ((ACPI_PHYSICAL_ADDRESS) Table, Table,
839 Table->Length, ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
841 if (ACPI_FAILURE (Status))
845 Status = AcpiTbAllocateOwnerId (TableIndex);
846 if (ACPI_FAILURE (Status))
852 Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
853 if (ACPI_FAILURE (Status))
860 fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
862 Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
863 if (ACPI_FAILURE (Status))
868 /* No need to parse control methods of external table */
876 * Pass 3: Parse control methods and link their parse trees
877 * into the main parse tree
880 "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
881 Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
882 fprintf (stderr, "\n");
884 /* Process Resource Templates */
886 AcpiDmFindResources (AcpiGbl_ParseOpRoot);
888 fprintf (stderr, "Parsing completed\n");