1 /******************************************************************************
3 * Module Name: adisasm - Application-level disassembler routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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 */
89 ACPI_TABLE_HEADER *Table);
91 /* Stubs for ASL compiler */
93 #ifndef ACPI_ASL_COMPILER
96 ACPI_PARSE_OBJECT *Op,
97 ACPI_WALK_STATE *WalkState)
105 ACPI_WALK_STATE *WalkState)
114 ACPI_NAMESPACE_NODE *Node)
116 return (AE_NOT_IMPLEMENTED);
120 AcpiDsRestartControlMethod (
121 ACPI_WALK_STATE *WalkState,
122 ACPI_OPERAND_OBJECT *ReturnDesc)
128 AcpiDsTerminateControlMethod (
129 ACPI_OPERAND_OBJECT *MethodDesc,
130 ACPI_WALK_STATE *WalkState)
136 AcpiDsCallControlMethod (
137 ACPI_THREAD_STATE *Thread,
138 ACPI_WALK_STATE *WalkState,
139 ACPI_PARSE_OBJECT *Op)
145 AcpiDsMethodDataInitArgs (
146 ACPI_OPERAND_OBJECT **Params,
147 UINT32 MaxParamCount,
148 ACPI_WALK_STATE *WalkState)
154 static ACPI_TABLE_DESC LocalTables[1];
155 static ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
158 /*******************************************************************************
160 * FUNCTION: AdGetFileSize
162 * PARAMETERS: File - Open file handle
166 * DESCRIPTION: Get current file size. Uses seek-to-EOF. File must be open.
168 ******************************************************************************/
178 Offset = ftell (File);
180 fseek (File, 0, SEEK_END);
181 FileSize = (UINT32) ftell (File);
183 /* Restore file pointer */
185 fseek (File, Offset, SEEK_SET);
190 /*******************************************************************************
192 * FUNCTION: AdInitialize
198 * DESCRIPTION: ACPICA and local initialization
200 ******************************************************************************/
209 /* ACPI CA subsystem initialization */
211 Status = AcpiOsInitialize ();
212 if (ACPI_FAILURE (Status))
217 Status = AcpiUtInitGlobals ();
218 if (ACPI_FAILURE (Status))
223 Status = AcpiUtMutexInitialize ();
224 if (ACPI_FAILURE (Status))
229 Status = AcpiNsRootInitialize ();
230 if (ACPI_FAILURE (Status))
235 /* Setup the Table Manager (cheat - there is no RSDT) */
237 AcpiGbl_RootTableList.MaxTableCount = 1;
238 AcpiGbl_RootTableList.CurrentTableCount = 0;
239 AcpiGbl_RootTableList.Tables = LocalTables;
245 /******************************************************************************
247 * FUNCTION: AdAmlDisassemble
249 * PARAMETERS: Filename - AML input filename
250 * OutToFile - TRUE if output should go to a file
251 * Prefix - Path prefix for output
252 * OutFilename - where the filename is returned
253 * GetAllTables - TRUE if all tables are desired
257 * DESCRIPTION: Disassemble an entire ACPI table
259 *****************************************************************************/
267 BOOLEAN GetAllTables)
270 char *DisasmFilename = NULL;
271 char *ExternalFilename;
272 ACPI_EXTERNAL_FILE *ExternalFileList = AcpiGbl_ExternalFileList;
274 ACPI_TABLE_HEADER *Table = NULL;
275 ACPI_TABLE_HEADER *ExternalTable;
276 ACPI_OWNER_ID OwnerId;
280 * Input: AML code from either a file or via GetTables (memory or
285 Status = AcpiDbGetTableFromFile (Filename, &Table);
286 if (ACPI_FAILURE (Status))
292 * External filenames separated by commas
293 * Example: iasl -e file1,file2,file3 -d xxx.aml
295 while (ExternalFileList)
297 ExternalFilename = ExternalFileList->Path;
298 if (!ACPI_STRCMP (ExternalFilename, Filename))
300 /* Next external file */
302 ExternalFileList = ExternalFileList->Next;
306 Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable);
307 if (ACPI_FAILURE (Status))
312 /* Load external table for symbol resolution */
316 Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
317 if (ACPI_FAILURE (Status))
319 AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
320 AcpiFormatException (Status));
325 * Load namespace from names created within control methods
326 * Set owner id of nodes in external table
328 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
329 AcpiGbl_RootNode, OwnerId);
330 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
333 /* Next external file */
335 ExternalFileList = ExternalFileList->Next;
338 /* Clear external list generated by Scope in external tables */
340 if (AcpiGbl_ExternalFileList)
342 AcpiDmClearExternalList ();
347 Status = AdGetLocalTables (Filename, GetAllTables);
348 if (ACPI_FAILURE (Status))
350 AcpiOsPrintf ("Could not get ACPI tables, %s\n",
351 AcpiFormatException (Status));
355 if (!AcpiGbl_DbOpt_disasm)
360 /* Obtained the local tables, just disassemble the DSDT */
362 Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
363 if (ACPI_FAILURE (Status))
365 AcpiOsPrintf ("Could not get DSDT, %s\n",
366 AcpiFormatException (Status));
370 AcpiOsPrintf ("\nDisassembly of DSDT\n");
371 Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
375 * Output: ASL code. Redirect to a file if requested
379 /* Create/Open a disassembly output file */
381 DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
384 fprintf (stderr, "Could not generate output filename\n");
389 File = fopen (DisasmFilename, "w+");
392 fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
397 AcpiOsRedirectOutput (File);
400 *OutFilename = DisasmFilename;
402 if (!AcpiUtIsAmlTable (Table))
404 AdDisassemblerHeader (Filename);
405 AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
407 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength] "
408 "FieldName : FieldValue\n */\n\n");
410 AcpiDmDumpDataTable (Table);
411 fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
413 fprintf (stderr, "Formatted output: %s - %u bytes\n",
414 DisasmFilename, AdGetFileSize (File));
418 /* Always parse the tables, only option is what to display */
420 Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
421 if (ACPI_FAILURE (Status))
423 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
424 AcpiFormatException (Status));
428 if (AslCompilerdebug)
430 AcpiOsPrintf ("/**** Before second load\n");
432 NsSetupNamespaceListing (File);
433 NsDisplayNamespace ();
434 AcpiOsPrintf ("*****/\n");
437 /* Load namespace from names created within control methods */
439 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
440 AcpiGbl_RootNode, OwnerId);
443 * Cross reference the namespace here, in order to
444 * generate External() statements
446 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
447 AcpiGbl_RootNode, OwnerId);
449 if (AslCompilerdebug)
451 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
454 /* Find possible calls to external control methods */
456 AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
459 * If we found any external control methods, we must reparse
460 * the entire tree with the new information (namely, the
461 * number of arguments per method)
463 if (AcpiDmGetExternalMethodCount ())
466 "\nFound %u external control methods, "
467 "reparsing with new information\n",
468 AcpiDmGetExternalMethodCount ());
470 /* Reparse, rebuild namespace. no need to xref namespace */
472 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
473 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
475 AcpiGbl_RootNode = NULL;
476 AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
477 AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
478 AcpiGbl_RootNodeStruct.Type = ACPI_TYPE_DEVICE;
479 AcpiGbl_RootNodeStruct.Parent = NULL;
480 AcpiGbl_RootNodeStruct.Child = NULL;
481 AcpiGbl_RootNodeStruct.Peer = NULL;
482 AcpiGbl_RootNodeStruct.Object = NULL;
483 AcpiGbl_RootNodeStruct.Flags = 0;
485 Status = AcpiNsRootInitialize ();
486 AcpiDmAddExternalsToNamespace ();
488 /* Parse the table again. No need to reload it, however */
490 Status = AdParseTable (Table, NULL, FALSE, FALSE);
491 if (ACPI_FAILURE (Status))
493 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
494 AcpiFormatException (Status));
498 if (AslCompilerdebug)
500 AcpiOsPrintf ("/**** After second load and resource conversion\n");
501 NsSetupNamespaceListing (File);
502 NsDisplayNamespace ();
503 AcpiOsPrintf ("*****/\n");
505 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
510 * Now that the namespace is finalized, we can perform namespace
513 * 1) Convert fixed-offset references to resource descriptors
514 * to symbolic references (Note: modifies namespace)
516 AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
518 /* Optional displays */
520 if (AcpiGbl_DbOpt_disasm)
522 /* This is the real disassembly */
524 AdDisplayTables (Filename, Table);
526 /* Dump hex table if requested (-vt) */
528 AcpiDmDumpDataTable (Table);
530 fprintf (stderr, "Disassembly completed\n");
531 fprintf (stderr, "ASL Output: %s - %u bytes\n",
532 DisasmFilename, AdGetFileSize (File));
538 if (Table && !AcpiUtIsAmlTable (Table))
545 ACPI_FREE (DisasmFilename);
548 if (OutToFile && File)
550 if (AslCompilerdebug) /* Display final namespace, with transforms */
552 NsSetupNamespaceListing (File);
553 NsDisplayNamespace ();
557 AcpiOsRedirectOutput (stdout);
560 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
561 AcpiGbl_ParseOpRoot = NULL;
566 /******************************************************************************
568 * FUNCTION: AdDisassemblerHeader
570 * PARAMETERS: Filename - Input file for the table
574 * DESCRIPTION: Create the disassembler header, including ACPI CA signon with
575 * current time and date.
577 *****************************************************************************/
580 AdDisassemblerHeader (
587 /* Header and input table info */
589 AcpiOsPrintf ("/*\n");
590 AcpiOsPrintf (ACPI_COMMON_HEADER ("AML Disassembler", " * "));
592 AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
593 AcpiOsPrintf (" *\n");
597 /******************************************************************************
599 * FUNCTION: AdCreateTableHeader
601 * PARAMETERS: Filename - Input file for the table
602 * Table - Pointer to the raw table
606 * DESCRIPTION: Create the ASL table header, including ACPI CA signon with
607 * current time and date.
609 *****************************************************************************/
612 AdCreateTableHeader (
614 ACPI_TABLE_HEADER *Table)
621 * Print file header and dump original table header
623 AdDisassemblerHeader (Filename);
625 AcpiOsPrintf (" * Original Table Header:\n");
626 AcpiOsPrintf (" * Signature \"%4.4s\"\n", Table->Signature);
627 AcpiOsPrintf (" * Length 0x%8.8X (%u)\n", Table->Length, Table->Length);
629 /* Print and validate the revision */
631 AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);
633 switch (Table->Revision)
636 AcpiOsPrintf (" **** Invalid Revision");
640 /* Revision of DSDT controls the ACPI integer width */
642 if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
644 AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
653 /* Print and validate the table checksum */
655 AcpiOsPrintf (" * Checksum 0x%2.2X", Table->Checksum);
657 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
660 AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
661 (UINT8) (Table->Checksum - Checksum));
665 AcpiOsPrintf (" * OEM ID \"%.6s\"\n", Table->OemId);
666 AcpiOsPrintf (" * OEM Table ID \"%.8s\"\n", Table->OemTableId);
667 AcpiOsPrintf (" * OEM Revision 0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
668 AcpiOsPrintf (" * Compiler ID \"%.4s\"\n", Table->AslCompilerId);
669 AcpiOsPrintf (" * Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
670 AcpiOsPrintf (" */\n\n");
672 /* Create AML output filename based on input filename */
676 NewFilename = FlGenerateFilename (Filename, "aml");
680 NewFilename = ACPI_ALLOCATE_ZEROED (9);
681 strncat (NewFilename, Table->Signature, 4);
682 strcat (NewFilename, ".aml");
685 /* Open the ASL definition block */
688 "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
689 NewFilename, Table->Signature, Table->Revision,
690 Table->OemId, Table->OemTableId, Table->OemRevision);
692 ACPI_FREE (NewFilename);
696 /******************************************************************************
698 * FUNCTION: AdDisplayTables
700 * PARAMETERS: Filename - Input file for the table
701 * Table - Pointer to the raw table
705 * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
707 *****************************************************************************/
712 ACPI_TABLE_HEADER *Table)
716 if (!AcpiGbl_ParseOpRoot)
718 return (AE_NOT_EXIST);
721 if (!AcpiGbl_DbOpt_verbose)
723 AdCreateTableHeader (Filename, Table);
726 AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
728 if (AcpiGbl_DbOpt_verbose)
730 AcpiOsPrintf ("\n\nTable Header:\n");
731 AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
732 DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
734 AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
735 AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
736 Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
743 /******************************************************************************
745 * FUNCTION: AdGetLocalTables
747 * PARAMETERS: Filename - Not used
748 * GetAllTables - TRUE if all tables are desired
752 * DESCRIPTION: Get the ACPI tables from either memory or a file
754 *****************************************************************************/
759 BOOLEAN GetAllTables)
762 ACPI_TABLE_HEADER TableHeader;
763 ACPI_TABLE_HEADER *NewTable;
771 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_RSDT);
772 AcpiOsTableOverride (&TableHeader, &NewTable);
775 fprintf (stderr, "Could not obtain RSDT\n");
776 return (AE_NO_ACPI_TABLES);
780 AdWriteTable (NewTable, NewTable->Length,
781 ACPI_SIG_RSDT, NewTable->OemTableId);
784 if (ACPI_COMPARE_NAME (NewTable->Signature, ACPI_SIG_RSDT))
786 PointerSize = sizeof (UINT32);
790 PointerSize = sizeof (UINT64);
794 * Determine the number of tables pointed to by the RSDT/XSDT.
795 * This is defined by the ACPI Specification to be the number of
796 * pointers contained within the RSDT/XSDT. The size of the pointers
797 * is architecture-dependent.
799 NumTables = (NewTable->Length - sizeof (ACPI_TABLE_HEADER)) / PointerSize;
800 AcpiOsPrintf ("There are %u tables defined in the %4.4s\n\n",
801 NumTables, NewTable->Signature);
805 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_FADT);
806 AcpiOsTableOverride (&TableHeader, &NewTable);
809 AdWriteTable (NewTable, NewTable->Length,
810 ACPI_SIG_FADT, NewTable->OemTableId);
814 /* Don't bother with FACS, it is usually all zeros */
817 /* Always get the DSDT */
819 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
820 AcpiOsTableOverride (&TableHeader, &NewTable);
823 AdWriteTable (NewTable, NewTable->Length,
824 ACPI_SIG_DSDT, NewTable->OemTableId);
826 /* Store DSDT in the Table Manager */
828 Status = AcpiTbStoreTable (0, NewTable, NewTable->Length,
830 if (ACPI_FAILURE (Status))
832 fprintf (stderr, "Could not store DSDT\n");
833 return (AE_NO_ACPI_TABLES);
838 fprintf (stderr, "Could not obtain DSDT\n");
839 return (AE_NO_ACPI_TABLES);
843 /* TBD: Future implementation */
849 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_SSDT);
853 Status = AcpiOsTableOverride (&TableHeader, &NewTable);
862 /******************************************************************************
864 * FUNCTION: AdParseTable
866 * PARAMETERS: Table - Pointer to the raw table
867 * OwnerId - Returned OwnerId of the table
868 * LoadTable - If add table to the global table list
869 * External - If this is an external table
873 * DESCRIPTION: Parse the DSDT.
875 *****************************************************************************/
879 ACPI_TABLE_HEADER *Table,
880 ACPI_OWNER_ID *OwnerId,
884 ACPI_STATUS Status = AE_OK;
885 ACPI_WALK_STATE *WalkState;
893 return (AE_NOT_EXIST);
896 /* Pass 1: Parse everything except control method bodies */
898 fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
900 AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
901 AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
903 /* Create the root object */
905 AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp ();
906 if (!AcpiGbl_ParseOpRoot)
908 return (AE_NO_MEMORY);
911 /* Create and initialize a new walk state */
913 WalkState = AcpiDsCreateWalkState (0,
914 AcpiGbl_ParseOpRoot, NULL, NULL);
917 return (AE_NO_MEMORY);
920 Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
921 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
922 if (ACPI_FAILURE (Status))
927 WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
928 WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
930 Status = AcpiPsParseAml (WalkState);
931 if (ACPI_FAILURE (Status))
936 /* If LoadTable is FALSE, we are parsing the last loaded table */
938 TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
944 Status = AcpiTbStoreTable ((ACPI_PHYSICAL_ADDRESS) Table, Table,
945 Table->Length, ACPI_TABLE_ORIGIN_ALLOCATED, &TableIndex);
946 if (ACPI_FAILURE (Status))
950 Status = AcpiTbAllocateOwnerId (TableIndex);
951 if (ACPI_FAILURE (Status))
957 Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
958 if (ACPI_FAILURE (Status))
965 fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
967 Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
968 if (ACPI_FAILURE (Status))
973 /* No need to parse control methods of external table */
980 /* Pass 3: Parse control methods and link their parse trees into the main parse tree */
982 fprintf (stderr, "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
983 Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
984 fprintf (stderr, "\n");
986 /* Process Resource Templates */
988 AcpiDmFindResources (AcpiGbl_ParseOpRoot);
990 fprintf (stderr, "Parsing completed\n");