1 /******************************************************************************
3 * Module Name: adisasm - Application-level disassembler routines
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/amlcode.h>
46 #include <contrib/dev/acpica/include/acdisasm.h>
47 #include <contrib/dev/acpica/include/acdispat.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
49 #include <contrib/dev/acpica/include/acparser.h>
50 #include <contrib/dev/acpica/include/acapps.h>
53 #define _COMPONENT ACPI_TOOLS
54 ACPI_MODULE_NAME ("adisasm")
56 /* Local prototypes */
59 AdDoExternalFileList (
63 AdDisassembleOneTable (
64 ACPI_TABLE_HEADER *Table,
67 char *DisasmFilename);
71 ACPI_TABLE_HEADER *Table,
73 ACPI_OWNER_ID OwnerId);
76 ACPI_TABLE_DESC LocalTables[1];
77 ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
80 /* Stubs for everything except ASL compiler */
82 #ifndef ACPI_ASL_COMPILER
85 ACPI_PARSE_OBJECT *Op,
86 ACPI_WALK_STATE *WalkState)
94 ACPI_WALK_STATE *WalkState)
101 /*******************************************************************************
103 * FUNCTION: AdInitialize
109 * DESCRIPTION: ACPICA and local initialization
111 ******************************************************************************/
120 /* ACPICA subsystem initialization */
122 Status = AcpiOsInitialize ();
123 if (ACPI_FAILURE (Status))
128 Status = AcpiUtInitGlobals ();
129 if (ACPI_FAILURE (Status))
134 Status = AcpiUtMutexInitialize ();
135 if (ACPI_FAILURE (Status))
140 Status = AcpiNsRootInitialize ();
141 if (ACPI_FAILURE (Status))
146 /* Setup the Table Manager (cheat - there is no RSDT) */
148 AcpiGbl_RootTableList.MaxTableCount = 1;
149 AcpiGbl_RootTableList.CurrentTableCount = 0;
150 AcpiGbl_RootTableList.Tables = LocalTables;
156 /******************************************************************************
158 * FUNCTION: AdAmlDisassemble
160 * PARAMETERS: Filename - AML input filename
161 * OutToFile - TRUE if output should go to a file
162 * Prefix - Path prefix for output
163 * OutFilename - where the filename is returned
167 * DESCRIPTION: Disassembler entry point. Disassemble an entire ACPI table.
169 *****************************************************************************/
179 char *DisasmFilename = NULL;
181 ACPI_TABLE_HEADER *Table = NULL;
182 ACPI_NEW_TABLE_DESC *ListHead = NULL;
186 * Input: AML code from either a file or via GetTables (memory or
191 /* Get the list of all AML tables in the file */
193 Status = AcGetAllTablesFromFile (Filename,
194 ACPI_GET_ALL_TABLES, &ListHead);
195 if (ACPI_FAILURE (Status))
197 AcpiOsPrintf ("Could not get ACPI tables from %s, %s\n",
198 Filename, AcpiFormatException (Status));
202 /* Process any user-specified files for external objects */
204 Status = AdDoExternalFileList (Filename);
205 if (ACPI_FAILURE (Status))
212 Status = AdGetLocalTables ();
213 if (ACPI_FAILURE (Status))
215 AcpiOsPrintf ("Could not get ACPI tables, %s\n",
216 AcpiFormatException (Status));
220 if (!AcpiGbl_DmOpt_Disasm)
225 /* Obtained the local tables, just disassemble the DSDT */
227 Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
228 if (ACPI_FAILURE (Status))
230 AcpiOsPrintf ("Could not get DSDT, %s\n",
231 AcpiFormatException (Status));
235 AcpiOsPrintf ("\nDisassembly of DSDT\n");
236 Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
240 * Output: ASL code. Redirect to a file if requested
244 /* Create/Open a disassembly output file */
246 DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
249 fprintf (stderr, "Could not generate output filename\n");
254 File = fopen (DisasmFilename, "w+");
257 fprintf (stderr, "Could not open output file %s\n",
263 AcpiOsRedirectOutput (File);
266 *OutFilename = DisasmFilename;
268 /* Disassemble all AML tables within the file */
272 Status = AdDisassembleOneTable (ListHead->Table,
273 File, Filename, DisasmFilename);
274 if (ACPI_FAILURE (Status))
279 ListHead = ListHead->Next;
285 !AcpiGbl_ForceAmlDisassembly &&
286 !AcpiUtIsAmlTable (Table))
294 AcpiOsRedirectOutput (stdout);
297 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
298 AcpiGbl_ParseOpRoot = NULL;
303 /******************************************************************************
305 * FUNCTION: AdDisassembleOneTable
307 * PARAMETERS: Table - Raw AML table
308 * File - Pointer for the input file
309 * Filename - AML input filename
310 * DisasmFilename - Output filename
314 * DESCRIPTION: Disassemble a single ACPI table. AML or data table.
316 *****************************************************************************/
319 AdDisassembleOneTable (
320 ACPI_TABLE_HEADER *Table,
323 char *DisasmFilename)
326 ACPI_OWNER_ID OwnerId;
329 /* ForceAmlDisassembly means to assume the table contains valid AML */
331 if (!AcpiGbl_ForceAmlDisassembly && !AcpiUtIsAmlTable (Table))
333 AdDisassemblerHeader (Filename, ACPI_IS_DATA_TABLE);
335 /* This is a "Data Table" (non-AML table) */
337 AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
339 AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength] "
340 "FieldName : FieldValue\n */\n\n");
342 AcpiDmDumpDataTable (Table);
343 fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
348 fprintf (stderr, "Formatted output: %s - %u bytes\n",
349 DisasmFilename, CmGetFileSize (File));
356 * This is an AML table (DSDT or SSDT).
357 * Always parse the tables, only option is what to display
359 Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
360 if (ACPI_FAILURE (Status))
362 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
363 AcpiFormatException (Status));
367 /* Debug output, namespace and parse tree */
369 if (AslCompilerdebug && File)
371 AcpiOsPrintf ("/**** Before second load\n");
373 NsSetupNamespaceListing (File);
374 NsDisplayNamespace ();
376 AcpiOsPrintf ("*****/\n");
379 /* Load namespace from names created within control methods */
381 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
382 AcpiGbl_RootNode, OwnerId);
385 * Cross reference the namespace here, in order to
386 * generate External() statements
388 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
389 AcpiGbl_RootNode, OwnerId);
391 if (AslCompilerdebug)
393 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
396 /* Find possible calls to external control methods */
398 AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
401 * If we found any external control methods, we must reparse
402 * the entire tree with the new information (namely, the
403 * number of arguments per method)
405 if (AcpiDmGetExternalMethodCount ())
407 Status = AdReparseOneTable (Table, File, OwnerId);
408 if (ACPI_FAILURE (Status))
415 * Now that the namespace is finalized, we can perform namespace
418 * 1) Convert fixed-offset references to resource descriptors
419 * to symbolic references (Note: modifies namespace)
421 AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
423 /* Optional displays */
425 if (AcpiGbl_DmOpt_Disasm)
427 /* This is the real disassembly */
429 AdDisplayTables (Filename, Table);
431 /* Dump hex table if requested (-vt) */
433 AcpiDmDumpDataTable (Table);
435 fprintf (stderr, "Disassembly completed\n");
438 fprintf (stderr, "ASL Output: %s - %u bytes\n",
439 DisasmFilename, CmGetFileSize (File));
444 fprintf (stderr, "%14s %s - %u bytes\n",
445 Gbl_Files[ASL_FILE_MAP_OUTPUT].ShortDescription,
446 Gbl_Files[ASL_FILE_MAP_OUTPUT].Filename,
447 FlGetFileSize (ASL_FILE_MAP_OUTPUT));
455 /******************************************************************************
457 * FUNCTION: AdReparseOneTable
459 * PARAMETERS: Table - Raw AML table
460 * File - Pointer for the input file
461 * OwnerId - ID for this table
465 * DESCRIPTION: Reparse a table that has already been loaded. Used to
466 * integrate information about external control methods.
467 * These methods may have been previously parsed incorrectly.
469 *****************************************************************************/
473 ACPI_TABLE_HEADER *Table,
475 ACPI_OWNER_ID OwnerId)
481 "\nFound %u external control methods, "
482 "reparsing with new information\n",
483 AcpiDmGetExternalMethodCount ());
485 /* Reparse, rebuild namespace */
487 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
488 AcpiGbl_ParseOpRoot = NULL;
489 AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
491 AcpiGbl_RootNode = NULL;
492 AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
493 AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
494 AcpiGbl_RootNodeStruct.Type = ACPI_TYPE_DEVICE;
495 AcpiGbl_RootNodeStruct.Parent = NULL;
496 AcpiGbl_RootNodeStruct.Child = NULL;
497 AcpiGbl_RootNodeStruct.Peer = NULL;
498 AcpiGbl_RootNodeStruct.Object = NULL;
499 AcpiGbl_RootNodeStruct.Flags = 0;
501 Status = AcpiNsRootInitialize ();
502 if (ACPI_FAILURE (Status))
507 /* New namespace, add the external definitions first */
509 AcpiDmAddExternalsToNamespace ();
511 /* Parse the table again. No need to reload it, however */
513 Status = AdParseTable (Table, NULL, FALSE, FALSE);
514 if (ACPI_FAILURE (Status))
516 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
517 AcpiFormatException (Status));
521 /* Cross reference the namespace again */
523 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
524 AcpiGbl_RootNode, OwnerId);
526 AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
527 AcpiGbl_RootNode, OwnerId);
529 /* Debug output - namespace and parse tree */
531 if (AslCompilerdebug)
533 AcpiOsPrintf ("/**** After second load and resource conversion\n");
536 NsSetupNamespaceListing (File);
537 NsDisplayNamespace ();
540 AcpiOsPrintf ("*****/\n");
541 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
548 /******************************************************************************
550 * FUNCTION: AdDoExternalFileList
552 * PARAMETERS: Filename - Input file for the table
556 * DESCRIPTION: Process all tables found in the -e external files list
558 *****************************************************************************/
561 AdDoExternalFileList (
564 ACPI_EXTERNAL_FILE *ExternalFileList;
565 char *ExternalFilename;
566 ACPI_NEW_TABLE_DESC *ExternalListHead = NULL;
568 ACPI_STATUS GlobalStatus = AE_OK;
569 ACPI_OWNER_ID OwnerId;
573 * External filenames are specified on the command line like this:
574 * Example: iasl -e file1,file2,file3 -d xxx.aml
576 ExternalFileList = AcpiGbl_ExternalFileList;
578 /* Process each external file */
580 while (ExternalFileList)
582 ExternalFilename = ExternalFileList->Path;
583 if (!strcmp (ExternalFilename, Filename))
585 /* Next external file */
587 ExternalFileList = ExternalFileList->Next;
591 AcpiOsPrintf ("External object resolution file %16s\n",
594 Status = AcGetAllTablesFromFile (
595 ExternalFilename, ACPI_GET_ONLY_AML_TABLES, &ExternalListHead);
596 if (ACPI_FAILURE (Status))
598 if (Status == AE_TYPE)
600 ExternalFileList = ExternalFileList->Next;
601 GlobalStatus = AE_TYPE;
609 /* Load external tables for symbol resolution */
611 while (ExternalListHead)
613 Status = AdParseTable (
614 ExternalListHead->Table, &OwnerId, TRUE, TRUE);
615 if (ACPI_FAILURE (Status))
617 AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
618 AcpiFormatException (Status));
623 * Load namespace from names created within control methods
624 * Set owner id of nodes in external table
626 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
627 AcpiGbl_RootNode, OwnerId);
628 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
630 ExternalListHead = ExternalListHead->Next;
633 /* Next external file */
635 ExternalFileList = ExternalFileList->Next;
638 if (ACPI_FAILURE (GlobalStatus))
640 return (GlobalStatus);
643 /* Clear external list generated by Scope in external tables */
645 if (AcpiGbl_ExternalFileList)
647 AcpiDmClearExternalList ();
650 /* Load any externals defined in the optional external ref file */
652 AcpiDmGetExternalsFromFile ();