Home | History | Annotate | Line # | Download | only in common
adisasm.c revision 1.1
      1 /******************************************************************************
      2  *
      3  * Module Name: adisasm - Application-level disassembler routines
      4  *
      5  *****************************************************************************/
      6 
      7 /******************************************************************************
      8  *
      9  * 1. Copyright Notice
     10  *
     11  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
     12  * All rights reserved.
     13  *
     14  * 2. License
     15  *
     16  * 2.1. This is your license from Intel Corp. under its intellectual property
     17  * rights.  You may have additional license terms from the party that provided
     18  * you this software, covering your right to use that party's intellectual
     19  * property rights.
     20  *
     21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     22  * copy of the source code appearing in this file ("Covered Code") an
     23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     24  * base code distributed originally by Intel ("Original Intel Code") to copy,
     25  * make derivatives, distribute, use and display any portion of the Covered
     26  * Code in any form, with the right to sublicense such rights; and
     27  *
     28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     29  * license (with the right to sublicense), under only those claims of Intel
     30  * patents that are infringed by the Original Intel Code, to make, use, sell,
     31  * offer to sell, and import the Covered Code and derivative works thereof
     32  * solely to the minimum extent necessary to exercise the above copyright
     33  * license, and in no event shall the patent license extend to any additions
     34  * to or modifications of the Original Intel Code.  No other license or right
     35  * is granted directly or by implication, estoppel or otherwise;
     36  *
     37  * The above copyright and patent license is granted only if the following
     38  * conditions are met:
     39  *
     40  * 3. Conditions
     41  *
     42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     43  * Redistribution of source code of any substantial portion of the Covered
     44  * Code or modification with rights to further distribute source must include
     45  * the above Copyright Notice, the above License, this list of Conditions,
     46  * and the following Disclaimer and Export Compliance provision.  In addition,
     47  * Licensee must cause all Covered Code to which Licensee contributes to
     48  * contain a file documenting the changes Licensee made to create that Covered
     49  * Code and the date of any change.  Licensee must include in that file the
     50  * documentation of any changes made by any predecessor Licensee.  Licensee
     51  * must include a prominent statement that the modification is derived,
     52  * directly or indirectly, from Original Intel Code.
     53  *
     54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     55  * Redistribution of source code of any substantial portion of the Covered
     56  * Code or modification without rights to further distribute source must
     57  * include the following Disclaimer and Export Compliance provision in the
     58  * documentation and/or other materials provided with distribution.  In
     59  * addition, Licensee may not authorize further sublicense of source of any
     60  * portion of the Covered Code, and must include terms to the effect that the
     61  * license from Licensee to its licensee is limited to the intellectual
     62  * property embodied in the software Licensee provides to its licensee, and
     63  * not to intellectual property embodied in modifications its licensee may
     64  * make.
     65  *
     66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     67  * substantial portion of the Covered Code or modification must reproduce the
     68  * above Copyright Notice, and the following Disclaimer and Export Compliance
     69  * provision in the documentation and/or other materials provided with the
     70  * distribution.
     71  *
     72  * 3.4. Intel retains all right, title, and interest in and to the Original
     73  * Intel Code.
     74  *
     75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     76  * Intel shall be used in advertising or otherwise to promote the sale, use or
     77  * other dealings in products derived from or relating to the Covered Code
     78  * without prior written authorization from Intel.
     79  *
     80  * 4. Disclaimer and Export Compliance
     81  *
     82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     83  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
     85  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
     86  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     88  * PARTICULAR PURPOSE.
     89  *
     90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
     96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     97  * LIMITED REMEDY.
     98  *
     99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    100  * software or system incorporating such software without first obtaining any
    101  * required license or other approval from the U. S. Department of Commerce or
    102  * any other agency or department of the United States Government.  In the
    103  * event Licensee exports any such software from the United States or
    104  * re-exports any such software from a foreign destination, Licensee shall
    105  * ensure that the distribution and export/re-export of the software is in
    106  * compliance with all laws, regulations, orders, or other restrictions of the
    107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    108  * any of its subsidiaries will export/re-export any technical data, process,
    109  * software, or service, directly or indirectly, to any country for which the
    110  * United States government or any agency thereof requires an export license,
    111  * other governmental approval, or letter of assurance, without first obtaining
    112  * such license, approval or letter.
    113  *
    114  *****************************************************************************/
    115 
    116 
    117 #include "acpi.h"
    118 #include "accommon.h"
    119 #include "acparser.h"
    120 #include "amlcode.h"
    121 #include "acdebug.h"
    122 #include "acdisasm.h"
    123 #include "acdispat.h"
    124 #include "acnamesp.h"
    125 #include "actables.h"
    126 #include "acapps.h"
    127 
    128 #include <stdio.h>
    129 #include <time.h>
    130 
    131 
    132 #define _COMPONENT          ACPI_TOOLS
    133         ACPI_MODULE_NAME    ("adisasm")
    134 
    135 
    136 extern int                  AslCompilerdebug;
    137 extern char                 *Gbl_ExternalFilename;
    138 
    139 
    140 ACPI_STATUS
    141 LsDisplayNamespace (
    142     void);
    143 
    144 void
    145 LsSetupNsList (
    146     void                    *Handle);
    147 
    148 
    149 /* Local prototypes */
    150 
    151 void
    152 AdCreateTableHeader (
    153     char                    *Filename,
    154     ACPI_TABLE_HEADER       *Table);
    155 
    156 void
    157 AdDisassemblerHeader (
    158     char                    *Filename);
    159 
    160 ACPI_STATUS
    161 AdDeferredParse (
    162     ACPI_PARSE_OBJECT       *Op,
    163     UINT8                   *Aml,
    164     UINT32                  AmlLength);
    165 
    166 ACPI_STATUS
    167 AdParseDeferredOps (
    168     ACPI_PARSE_OBJECT       *Root);
    169 
    170 
    171 /* Stubs for ASL compiler */
    172 
    173 #ifndef ACPI_ASL_COMPILER
    174 BOOLEAN
    175 AcpiDsIsResultUsed (
    176     ACPI_PARSE_OBJECT       *Op,
    177     ACPI_WALK_STATE         *WalkState)
    178 {
    179     return TRUE;
    180 }
    181 
    182 ACPI_STATUS
    183 AcpiDsMethodError (
    184     ACPI_STATUS             Status,
    185     ACPI_WALK_STATE         *WalkState)
    186 {
    187     return (Status);
    188 }
    189 #endif
    190 
    191 ACPI_STATUS
    192 AcpiNsLoadTable (
    193     UINT32                  TableIndex,
    194     ACPI_NAMESPACE_NODE     *Node)
    195 {
    196     return (AE_NOT_IMPLEMENTED);
    197 }
    198 
    199 ACPI_STATUS
    200 AcpiDsRestartControlMethod (
    201     ACPI_WALK_STATE         *WalkState,
    202     ACPI_OPERAND_OBJECT     *ReturnDesc)
    203 {
    204     return (AE_OK);
    205 }
    206 
    207 void
    208 AcpiDsTerminateControlMethod (
    209     ACPI_OPERAND_OBJECT     *MethodDesc,
    210     ACPI_WALK_STATE         *WalkState)
    211 {
    212     return;
    213 }
    214 
    215 ACPI_STATUS
    216 AcpiDsCallControlMethod (
    217     ACPI_THREAD_STATE       *Thread,
    218     ACPI_WALK_STATE         *WalkState,
    219     ACPI_PARSE_OBJECT       *Op)
    220 {
    221     return (AE_OK);
    222 }
    223 
    224 ACPI_STATUS
    225 AcpiDsMethodDataInitArgs (
    226     ACPI_OPERAND_OBJECT     **Params,
    227     UINT32                  MaxParamCount,
    228     ACPI_WALK_STATE         *WalkState)
    229 {
    230     return (AE_OK);
    231 }
    232 
    233 
    234 static ACPI_TABLE_DESC      LocalTables[1];
    235 static ACPI_PARSE_OBJECT    *AcpiGbl_ParseOpRoot;
    236 
    237 
    238 /*******************************************************************************
    239  *
    240  * FUNCTION:    AdInitialize
    241  *
    242  * PARAMETERS:  None
    243  *
    244  * RETURN:      Status
    245  *
    246  * DESCRIPTION: ACPICA and local initialization
    247  *
    248  ******************************************************************************/
    249 
    250 ACPI_STATUS
    251 AdInitialize (
    252     void)
    253 {
    254     ACPI_STATUS             Status;
    255 
    256 
    257     /* ACPI CA subsystem initialization */
    258 
    259     Status = AcpiOsInitialize ();
    260     if (ACPI_FAILURE (Status))
    261     {
    262         return (Status);
    263     }
    264 
    265     Status = AcpiUtInitGlobals ();
    266     if (ACPI_FAILURE (Status))
    267     {
    268         return (Status);
    269     }
    270 
    271     Status = AcpiUtMutexInitialize ();
    272     if (ACPI_FAILURE (Status))
    273     {
    274         return (Status);
    275     }
    276 
    277     Status = AcpiNsRootInitialize ();
    278     if (ACPI_FAILURE (Status))
    279     {
    280         return (Status);
    281     }
    282 
    283     /* Setup the Table Manager (cheat - there is no RSDT) */
    284 
    285     AcpiGbl_RootTableList.MaxTableCount = 1;
    286     AcpiGbl_RootTableList.CurrentTableCount = 0;
    287     AcpiGbl_RootTableList.Tables = LocalTables;
    288 
    289     return (Status);
    290 }
    291 
    292 
    293 /******************************************************************************
    294  *
    295  * FUNCTION:    AdAmlDisassemble
    296  *
    297  * PARAMETERS:  Filename            - AML input filename
    298  *              OutToFile           - TRUE if output should go to a file
    299  *              Prefix              - Path prefix for output
    300  *              OutFilename         - where the filename is returned
    301  *              GetAllTables        - TRUE if all tables are desired
    302  *
    303  * RETURN:      Status
    304  *
    305  * DESCRIPTION: Disassemble an entire ACPI table
    306  *
    307  *****************************************************************************/
    308 
    309 ACPI_STATUS
    310 AdAmlDisassemble (
    311     BOOLEAN                 OutToFile,
    312     char                    *Filename,
    313     char                    *Prefix,
    314     char                    **OutFilename,
    315     BOOLEAN                 GetAllTables)
    316 {
    317     ACPI_STATUS             Status;
    318     char                    *DisasmFilename = NULL;
    319     char                    *ExternalFilename;
    320     FILE                    *File = NULL;
    321     ACPI_TABLE_HEADER       *Table = NULL;
    322     ACPI_TABLE_HEADER       *ExternalTable;
    323     ACPI_OWNER_ID           OwnerId;
    324 
    325 
    326     /*
    327      * Input: AML code from either a file or via GetTables (memory or
    328      * registry)
    329      */
    330     if (Filename)
    331     {
    332         Status = AcpiDbGetTableFromFile (Filename, &Table);
    333         if (ACPI_FAILURE (Status))
    334         {
    335             return Status;
    336         }
    337 
    338         /*
    339          * External filenames separated by commas
    340          * Example: iasl -e file1,file2,file3 -d xxx.aml
    341          */
    342         if (Gbl_ExternalFilename)
    343         {
    344             ExternalFilename = strtok (Gbl_ExternalFilename, ",");
    345 
    346             while (ExternalFilename)
    347             {
    348                 Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable);
    349                 if (ACPI_FAILURE (Status))
    350                 {
    351                     return Status;
    352                 }
    353 
    354                 /* Load external table for symbol resolution */
    355 
    356                 if (ExternalTable)
    357                 {
    358                     Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
    359                     if (ACPI_FAILURE (Status))
    360                     {
    361                         AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
    362                             AcpiFormatException (Status));
    363                         return Status;
    364                     }
    365 
    366                     /*
    367                      * Load namespace from names created within control methods
    368                      * Set owner id of nodes in external table
    369                      */
    370                     AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
    371                         AcpiGbl_RootNode, OwnerId);
    372                     AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
    373                 }
    374 
    375                 /* Next external file name */
    376 
    377                 ExternalFilename = strtok (NULL, ",");
    378             }
    379 
    380             /* Clear external list generated by Scope in external tables */
    381 
    382             AcpiDmClearExternalList ();
    383         }
    384     }
    385     else
    386     {
    387         Status = AdGetLocalTables (Filename, GetAllTables);
    388         if (ACPI_FAILURE (Status))
    389         {
    390             AcpiOsPrintf ("Could not get ACPI tables, %s\n",
    391                 AcpiFormatException (Status));
    392             return Status;
    393         }
    394 
    395         if (!AcpiGbl_DbOpt_disasm)
    396         {
    397             return AE_OK;
    398         }
    399 
    400         /* Obtained the local tables, just disassemble the DSDT */
    401 
    402         Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
    403         if (ACPI_FAILURE (Status))
    404         {
    405             AcpiOsPrintf ("Could not get DSDT, %s\n",
    406                 AcpiFormatException (Status));
    407             return Status;
    408         }
    409 
    410         AcpiOsPrintf ("\nDisassembly of DSDT\n");
    411         Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
    412     }
    413 
    414     /*
    415      * Output:  ASL code. Redirect to a file if requested
    416      */
    417     if (OutToFile)
    418     {
    419         /* Create/Open a disassembly output file */
    420 
    421         DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
    422         if (!OutFilename)
    423         {
    424             fprintf (stderr, "Could not generate output filename\n");
    425             Status = AE_ERROR;
    426             goto Cleanup;
    427         }
    428 
    429         File = fopen (DisasmFilename, "w+");
    430         if (!File)
    431         {
    432             fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
    433             Status = AE_ERROR;
    434             goto Cleanup;
    435         }
    436 
    437         AcpiOsRedirectOutput (File);
    438     }
    439 
    440     *OutFilename = DisasmFilename;
    441 
    442     if (!AcpiUtIsAmlTable (Table))
    443     {
    444         AdDisassemblerHeader (Filename);
    445         AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
    446             Table->Signature);
    447         AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]  FieldName : FieldValue\n */\n\n");
    448 
    449         AcpiDmDumpDataTable (Table);
    450         fprintf (stderr, "Acpi Data Table [%4.4s] decoded, written to \"%s\"\n",
    451             Table->Signature, DisasmFilename);
    452     }
    453     else
    454     {
    455         /* Always parse the tables, only option is what to display */
    456 
    457         Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
    458         if (ACPI_FAILURE (Status))
    459         {
    460             AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
    461                 AcpiFormatException (Status));
    462             goto Cleanup;
    463         }
    464 
    465         if (AslCompilerdebug)
    466         {
    467             AcpiOsPrintf ("/**** Before second load\n");
    468 
    469             LsSetupNsList (File);
    470             LsDisplayNamespace ();
    471             AcpiOsPrintf ("*****/\n");
    472         }
    473 
    474         /*
    475          * Load namespace from names created within control methods
    476          */
    477         AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode, OwnerId);
    478 
    479         /*
    480          * Cross reference the namespace here, in order to generate External() statements
    481          */
    482         AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode, OwnerId);
    483 
    484         if (AslCompilerdebug)
    485         {
    486             AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
    487         }
    488 
    489         /* Find possible calls to external control methods */
    490 
    491         AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
    492 
    493         /* Convert fixed-offset references to resource descriptors to symbolic references */
    494 
    495         AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
    496 
    497         /*
    498          * If we found any external control methods, we must reparse the entire
    499          * tree with the new information (namely, the number of arguments per
    500          * method)
    501          */
    502         if (AcpiDmGetExternalMethodCount ())
    503         {
    504             fprintf (stderr,
    505                 "\nFound %u external control methods, reparsing with new information\n",
    506                 AcpiDmGetExternalMethodCount ());
    507 
    508             /*
    509              * Reparse, rebuild namespace. no need to xref namespace
    510              */
    511             AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
    512             AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
    513 
    514             AcpiGbl_RootNode                    = NULL;
    515             AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
    516             AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
    517             AcpiGbl_RootNodeStruct.Type         = ACPI_TYPE_DEVICE;
    518             AcpiGbl_RootNodeStruct.Parent       = NULL;
    519             AcpiGbl_RootNodeStruct.Child        = NULL;
    520             AcpiGbl_RootNodeStruct.Peer         = NULL;
    521             AcpiGbl_RootNodeStruct.Object       = NULL;
    522             AcpiGbl_RootNodeStruct.Flags        = 0;
    523 
    524             Status = AcpiNsRootInitialize ();
    525             AcpiDmAddExternalsToNamespace ();
    526 
    527             /* Parse table. No need to reload it, however (FALSE) */
    528 
    529             Status = AdParseTable (Table, NULL, FALSE, FALSE);
    530             if (ACPI_FAILURE (Status))
    531             {
    532                 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
    533                     AcpiFormatException (Status));
    534                 goto Cleanup;
    535             }
    536 
    537             if (AslCompilerdebug)
    538             {
    539                 AcpiOsPrintf ("/**** After second load and resource conversion\n");
    540                 LsSetupNsList (File);
    541                 LsDisplayNamespace ();
    542                 AcpiOsPrintf ("*****/\n");
    543 
    544                 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
    545             }
    546         }
    547 
    548         /* Optional displays */
    549 
    550         if (AcpiGbl_DbOpt_disasm)
    551         {
    552             AdDisplayTables (Filename, Table);
    553             fprintf (stderr,
    554                 "Disassembly completed, written to \"%s\"\n",
    555                 DisasmFilename);
    556         }
    557     }
    558 
    559 Cleanup:
    560 
    561     if (Table && !AcpiUtIsAmlTable (Table))
    562     {
    563         ACPI_FREE (Table);
    564     }
    565 
    566     if (DisasmFilename)
    567     {
    568         ACPI_FREE (DisasmFilename);
    569     }
    570 
    571     if (OutToFile && File)
    572     {
    573 
    574 #ifdef ASL_DISASM_DEBUG
    575         LsSetupNsList (File);
    576         LsDisplayNamespace ();
    577 #endif
    578         fclose (File);
    579         AcpiOsRedirectOutput (stdout);
    580     }
    581 
    582     AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
    583     AcpiGbl_ParseOpRoot = NULL;
    584     return (Status);
    585 }
    586 
    587 
    588 /******************************************************************************
    589  *
    590  * FUNCTION:    AdDisassemblerHeader
    591  *
    592  * PARAMETERS:  Filename            - Input file for the table
    593  *
    594  * RETURN:      None
    595  *
    596  * DESCRIPTION: Create the disassembler header, including ACPI CA signon with
    597  *              current time and date.
    598  *
    599  *****************************************************************************/
    600 
    601 void
    602 AdDisassemblerHeader (
    603     char                    *Filename)
    604 {
    605     time_t                  Timer;
    606 
    607     time (&Timer);
    608 
    609     /* Header and input table info */
    610 
    611     AcpiOsPrintf ("/*\n * Intel ACPI Component Architecture\n");
    612     AcpiOsPrintf (" * AML Disassembler version %8.8X\n", ACPI_CA_VERSION);
    613 
    614     AcpiOsPrintf (" *\n * Disassembly of %s, %s", Filename, ctime (&Timer));
    615     AcpiOsPrintf (" *\n");
    616 }
    617 
    618 
    619 /******************************************************************************
    620  *
    621  * FUNCTION:    AdCreateTableHeader
    622  *
    623  * PARAMETERS:  Filename            - Input file for the table
    624  *              Table               - Pointer to the raw table
    625  *
    626  * RETURN:      None
    627  *
    628  * DESCRIPTION: Create the ASL table header, including ACPI CA signon with
    629  *              current time and date.
    630  *
    631  *****************************************************************************/
    632 
    633 void
    634 AdCreateTableHeader (
    635     char                    *Filename,
    636     ACPI_TABLE_HEADER       *Table)
    637 {
    638     char                    *NewFilename;
    639     UINT8                   Checksum;
    640 
    641 
    642     /*
    643      * Print file header and dump original table header
    644      */
    645     AdDisassemblerHeader (Filename);
    646 
    647     AcpiOsPrintf (" *\n * Original Table Header:\n");
    648     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
    649     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
    650 
    651     /* Print and validate the revision */
    652 
    653     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
    654 
    655     switch (Table->Revision)
    656     {
    657     case 0:
    658         AcpiOsPrintf (" **** Invalid Revision");
    659         break;
    660 
    661     case 1:
    662         /* Revision of DSDT controls the ACPI integer width */
    663 
    664         if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
    665         {
    666             AcpiOsPrintf (" **** ACPI 1.0, no 64-bit math support");
    667         }
    668         break;
    669 
    670     default:
    671         break;
    672     }
    673     AcpiOsPrintf ("\n");
    674 
    675     /* Print and validate the table checksum */
    676 
    677     AcpiOsPrintf (" *     Checksum         0x%2.2X",        Table->Checksum);
    678 
    679     Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
    680     if (Checksum)
    681     {
    682         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
    683             (UINT8) (Table->Checksum - Checksum));
    684     }
    685     AcpiOsPrintf ("\n");
    686 
    687     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
    688     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
    689     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
    690     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
    691     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
    692     AcpiOsPrintf (" */\n");
    693 
    694     /* Create AML output filename based on input filename */
    695 
    696     if (Filename)
    697     {
    698         NewFilename = FlGenerateFilename (Filename, "aml");
    699     }
    700     else
    701     {
    702         NewFilename = ACPI_ALLOCATE_ZEROED (9);
    703         strncat (NewFilename, Table->Signature, 4);
    704         strcat (NewFilename, ".aml");
    705     }
    706 
    707     /* Open the ASL definition block */
    708 
    709     AcpiOsPrintf (
    710         "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
    711         NewFilename, Table->Signature, Table->Revision,
    712         Table->OemId, Table->OemTableId, Table->OemRevision);
    713 
    714     ACPI_FREE (NewFilename);
    715 }
    716 
    717 
    718 /******************************************************************************
    719  *
    720  * FUNCTION:    AdDisplayTables
    721  *
    722  * PARAMETERS:  Filename            - Input file for the table
    723  *              Table               - Pointer to the raw table
    724  *
    725  * RETURN:      Status
    726  *
    727  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
    728  *
    729  *****************************************************************************/
    730 
    731 ACPI_STATUS
    732 AdDisplayTables (
    733     char                    *Filename,
    734     ACPI_TABLE_HEADER       *Table)
    735 {
    736 
    737 
    738     if (!AcpiGbl_ParseOpRoot)
    739     {
    740         return AE_NOT_EXIST;
    741     }
    742 
    743     if (!AcpiGbl_DbOpt_verbose)
    744     {
    745         AdCreateTableHeader (Filename, Table);
    746     }
    747 
    748     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
    749 
    750     if (AcpiGbl_DbOpt_verbose)
    751     {
    752         AcpiOsPrintf ("\n\nTable Header:\n");
    753         AcpiUtDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
    754             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
    755 
    756         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
    757         AcpiUtDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)), Table->Length,
    758             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
    759     }
    760 
    761     return AE_OK;
    762 }
    763 
    764 
    765 /******************************************************************************
    766  *
    767  * FUNCTION:    AdDeferredParse
    768  *
    769  * PARAMETERS:  Op                  - Root Op of the deferred opcode
    770  *              Aml                 - Pointer to the raw AML
    771  *              AmlLength           - Length of the AML
    772  *
    773  * RETURN:      Status
    774  *
    775  * DESCRIPTION: Parse one deferred opcode
    776  *              (Methods, operation regions, etc.)
    777  *
    778  *****************************************************************************/
    779 
    780 ACPI_STATUS
    781 AdDeferredParse (
    782     ACPI_PARSE_OBJECT       *Op,
    783     UINT8                   *Aml,
    784     UINT32                  AmlLength)
    785 {
    786     ACPI_WALK_STATE         *WalkState;
    787     ACPI_STATUS             Status;
    788     ACPI_PARSE_OBJECT       *SearchOp;
    789     ACPI_PARSE_OBJECT       *StartOp;
    790     UINT32                  BaseAmlOffset;
    791     ACPI_PARSE_OBJECT       *ExtraOp;
    792 
    793 
    794     ACPI_FUNCTION_TRACE (AdDeferredParse);
    795 
    796 
    797     fprintf (stderr, ".");
    798 
    799     if (!Aml || !AmlLength)
    800     {
    801         return_ACPI_STATUS (AE_OK);
    802     }
    803 
    804     ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Parsing %s [%4.4s]\n",
    805         Op->Common.AmlOpName, (char *) &Op->Named.Name));
    806 
    807     WalkState = AcpiDsCreateWalkState (0, Op, NULL, NULL);
    808     if (!WalkState)
    809     {
    810         return_ACPI_STATUS (AE_NO_MEMORY);
    811     }
    812 
    813     Status = AcpiDsInitAmlWalk (WalkState, Op, NULL, Aml,
    814                     AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
    815     if (ACPI_FAILURE (Status))
    816     {
    817         return_ACPI_STATUS (Status);
    818     }
    819 
    820     /* Parse the method */
    821 
    822     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
    823     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
    824     Status = AcpiPsParseAml (WalkState);
    825 
    826     /*
    827      * We need to update all of the Aml offsets, since the parser thought
    828      * that the method began at offset zero.  In reality, it began somewhere
    829      * within the ACPI table, at the BaseAmlOffset.  Walk the entire tree that
    830      * was just created and update the AmlOffset in each Op
    831      */
    832     BaseAmlOffset = (Op->Common.Value.Arg)->Common.AmlOffset + 1;
    833     StartOp = (Op->Common.Value.Arg)->Common.Next;
    834     SearchOp = StartOp;
    835 
    836     /* Walk the parse tree */
    837 
    838     while (SearchOp)
    839     {
    840         SearchOp->Common.AmlOffset += BaseAmlOffset;
    841         SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
    842     }
    843 
    844     /*
    845      * Link the newly parsed subtree into the main parse tree
    846      */
    847     switch (Op->Common.AmlOpcode)
    848     {
    849     case AML_BUFFER_OP:
    850     case AML_PACKAGE_OP:
    851     case AML_VAR_PACKAGE_OP:
    852 
    853         switch (Op->Common.AmlOpcode)
    854         {
    855         case AML_PACKAGE_OP:
    856             ExtraOp = Op->Common.Value.Arg;
    857             ExtraOp = ExtraOp->Common.Next;
    858             Op->Common.Value.Arg = ExtraOp->Common.Value.Arg;
    859             break;
    860 
    861         case AML_VAR_PACKAGE_OP:
    862         case AML_BUFFER_OP:
    863         default:
    864             ExtraOp = Op->Common.Value.Arg;
    865             Op->Common.Value.Arg = ExtraOp->Common.Value.Arg;
    866             break;
    867         }
    868 
    869         /* Must point all parents to the main tree */
    870 
    871         StartOp = Op;
    872         SearchOp = StartOp;
    873         while (SearchOp)
    874         {
    875             if (SearchOp->Common.Parent == ExtraOp)
    876             {
    877                 SearchOp->Common.Parent = Op;
    878             }
    879             SearchOp = AcpiPsGetDepthNext (StartOp, SearchOp);
    880         }
    881         break;
    882 
    883     default:
    884         break;
    885     }
    886 
    887     return_ACPI_STATUS (AE_OK);
    888 }
    889 
    890 
    891 /******************************************************************************
    892  *
    893  * FUNCTION:    AdParseDeferredOps
    894  *
    895  * PARAMETERS:  Root                - Root of the parse tree
    896  *
    897  * RETURN:      Status
    898  *
    899  * DESCRIPTION: Parse the deferred opcodes (Methods, regions, etc.)
    900  *
    901  *****************************************************************************/
    902 
    903 ACPI_STATUS
    904 AdParseDeferredOps (
    905     ACPI_PARSE_OBJECT       *Root)
    906 {
    907     ACPI_PARSE_OBJECT       *Op = Root;
    908     ACPI_STATUS             Status = AE_OK;
    909     const ACPI_OPCODE_INFO  *OpInfo;
    910 
    911 
    912     ACPI_FUNCTION_NAME (AdParseDeferredOps);
    913     fprintf (stderr, "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
    914 
    915     while (Op)
    916     {
    917         OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
    918         if (!(OpInfo->Flags & AML_DEFER))
    919         {
    920             Op = AcpiPsGetDepthNext (Root, Op);
    921             continue;
    922         }
    923 
    924         switch (Op->Common.AmlOpcode)
    925         {
    926         case AML_METHOD_OP:
    927         case AML_BUFFER_OP:
    928         case AML_PACKAGE_OP:
    929         case AML_VAR_PACKAGE_OP:
    930 
    931             Status = AdDeferredParse (Op, Op->Named.Data, Op->Named.Length);
    932             if (ACPI_FAILURE (Status))
    933             {
    934                 return_ACPI_STATUS (Status);
    935             }
    936             break;
    937 
    938         case AML_REGION_OP:
    939         case AML_CREATE_QWORD_FIELD_OP:
    940         case AML_CREATE_DWORD_FIELD_OP:
    941         case AML_CREATE_WORD_FIELD_OP:
    942         case AML_CREATE_BYTE_FIELD_OP:
    943         case AML_CREATE_BIT_FIELD_OP:
    944         case AML_CREATE_FIELD_OP:
    945         case AML_BANK_FIELD_OP:
    946 
    947             /* Nothing to do in these cases */
    948 
    949             break;
    950 
    951         default:
    952             ACPI_ERROR ((AE_INFO, "Unhandled deferred opcode [%s]",
    953                 Op->Common.AmlOpName));
    954             break;
    955         }
    956 
    957         Op = AcpiPsGetDepthNext (Root, Op);
    958     }
    959 
    960     fprintf (stderr, "\n");
    961     return Status;
    962 }
    963 
    964 
    965 /******************************************************************************
    966  *
    967  * FUNCTION:    AdGetLocalTables
    968  *
    969  * PARAMETERS:  Filename            - Not used
    970  *              GetAllTables        - TRUE if all tables are desired
    971  *
    972  * RETURN:      Status
    973  *
    974  * DESCRIPTION: Get the ACPI tables from either memory or a file
    975  *
    976  *****************************************************************************/
    977 
    978 ACPI_STATUS
    979 AdGetLocalTables (
    980     char                    *Filename,
    981     BOOLEAN                 GetAllTables)
    982 {
    983     ACPI_STATUS             Status;
    984     ACPI_TABLE_HEADER       TableHeader;
    985     ACPI_TABLE_HEADER       *NewTable;
    986     UINT32                  NumTables;
    987     UINT32                  PointerSize;
    988     UINT32                  TableIndex;
    989 
    990 
    991     if (GetAllTables)
    992     {
    993         ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_RSDT);
    994         AcpiOsTableOverride (&TableHeader, &NewTable);
    995         if (!NewTable)
    996         {
    997             fprintf (stderr, "Could not obtain RSDT\n");
    998             return AE_NO_ACPI_TABLES;
    999         }
   1000         else
   1001         {
   1002             AdWriteTable (NewTable, NewTable->Length,
   1003                 ACPI_SIG_RSDT, NewTable->OemTableId);
   1004         }
   1005 
   1006         if (ACPI_COMPARE_NAME (NewTable->Signature, ACPI_SIG_RSDT))
   1007         {
   1008             PointerSize = sizeof (UINT32);
   1009         }
   1010         else
   1011         {
   1012             PointerSize = sizeof (UINT64);
   1013         }
   1014 
   1015         /*
   1016          * Determine the number of tables pointed to by the RSDT/XSDT.
   1017          * This is defined by the ACPI Specification to be the number of
   1018          * pointers contained within the RSDT/XSDT.  The size of the pointers
   1019          * is architecture-dependent.
   1020          */
   1021         NumTables = (NewTable->Length - sizeof (ACPI_TABLE_HEADER)) / PointerSize;
   1022         AcpiOsPrintf ("There are %u tables defined in the %4.4s\n\n",
   1023             NumTables, NewTable->Signature);
   1024 
   1025         /* Get the FADT */
   1026 
   1027         ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_FADT);
   1028         AcpiOsTableOverride (&TableHeader, &NewTable);
   1029         if (NewTable)
   1030         {
   1031             AdWriteTable (NewTable, NewTable->Length,
   1032                 ACPI_SIG_FADT, NewTable->OemTableId);
   1033         }
   1034         AcpiOsPrintf ("\n");
   1035 
   1036         /* Don't bother with FACS, it is usually all zeros */
   1037     }
   1038 
   1039     /* Always get the DSDT */
   1040 
   1041     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
   1042     AcpiOsTableOverride (&TableHeader, &NewTable);
   1043     if (NewTable)
   1044     {
   1045         AdWriteTable (NewTable, NewTable->Length,
   1046             ACPI_SIG_DSDT, NewTable->OemTableId);
   1047 
   1048         /* Store DSDT in the Table Manager */
   1049 
   1050         Status = AcpiTbStoreTable (0, NewTable, NewTable->Length,
   1051                     0, &TableIndex);
   1052         if (ACPI_FAILURE (Status))
   1053         {
   1054             fprintf (stderr, "Could not store DSDT\n");
   1055             return AE_NO_ACPI_TABLES;
   1056         }
   1057     }
   1058     else
   1059     {
   1060         fprintf (stderr, "Could not obtain DSDT\n");
   1061         return AE_NO_ACPI_TABLES;
   1062     }
   1063 
   1064 #if 0
   1065     /* TBD: Future implementation */
   1066 
   1067     AcpiOsPrintf ("\n");
   1068 
   1069     /* Get all SSDTs */
   1070 
   1071     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_SSDT);
   1072     do
   1073     {
   1074         NewTable = NULL;
   1075         Status = AcpiOsTableOverride (&TableHeader, &NewTable);
   1076 
   1077     } while (NewTable);
   1078 #endif
   1079 
   1080     return AE_OK;
   1081 }
   1082 
   1083 
   1084 /******************************************************************************
   1085  *
   1086  * FUNCTION:    AdParseTable
   1087  *
   1088  * PARAMETERS:  Table               - Pointer to the raw table
   1089  *              OwnerId             - Returned OwnerId of the table
   1090  *              LoadTable           - If add table to the global table list
   1091  *              External            - If this is an external table
   1092  *
   1093  * RETURN:      Status
   1094  *
   1095  * DESCRIPTION: Parse the DSDT.
   1096  *
   1097  *****************************************************************************/
   1098 
   1099 ACPI_STATUS
   1100 AdParseTable (
   1101     ACPI_TABLE_HEADER       *Table,
   1102     ACPI_OWNER_ID           *OwnerId,
   1103     BOOLEAN                 LoadTable,
   1104     BOOLEAN                 External)
   1105 {
   1106     ACPI_STATUS             Status = AE_OK;
   1107     ACPI_WALK_STATE         *WalkState;
   1108     UINT8                   *AmlStart;
   1109     UINT32                  AmlLength;
   1110     UINT32                  TableIndex;
   1111 
   1112 
   1113     if (!Table)
   1114     {
   1115         return AE_NOT_EXIST;
   1116     }
   1117 
   1118     /* Pass 1:  Parse everything except control method bodies */
   1119 
   1120     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
   1121 
   1122     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
   1123     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
   1124 
   1125     /* Create the root object */
   1126 
   1127     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp ();
   1128     if (!AcpiGbl_ParseOpRoot)
   1129     {
   1130         return AE_NO_MEMORY;
   1131     }
   1132 
   1133     /* Create and initialize a new walk state */
   1134 
   1135     WalkState = AcpiDsCreateWalkState (0,
   1136                         AcpiGbl_ParseOpRoot, NULL, NULL);
   1137     if (!WalkState)
   1138     {
   1139         return (AE_NO_MEMORY);
   1140     }
   1141 
   1142     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
   1143                 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
   1144     if (ACPI_FAILURE (Status))
   1145     {
   1146         return (Status);
   1147     }
   1148 
   1149     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
   1150     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
   1151 
   1152     Status = AcpiPsParseAml (WalkState);
   1153     if (ACPI_FAILURE (Status))
   1154     {
   1155         return Status;
   1156     }
   1157 
   1158     /* If LoadTable is FALSE, we are parsing the last loaded table */
   1159 
   1160     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
   1161 
   1162     /* Pass 2 */
   1163 
   1164     if (LoadTable)
   1165     {
   1166         Status = AcpiTbStoreTable ((ACPI_PHYSICAL_ADDRESS) Table, Table,
   1167                     Table->Length, ACPI_TABLE_ORIGIN_ALLOCATED, &TableIndex);
   1168         if (ACPI_FAILURE (Status))
   1169         {
   1170             return Status;
   1171         }
   1172         Status = AcpiTbAllocateOwnerId (TableIndex);
   1173         if (ACPI_FAILURE (Status))
   1174         {
   1175             return Status;
   1176         }
   1177         if (OwnerId)
   1178         {
   1179             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
   1180             if (ACPI_FAILURE (Status))
   1181             {
   1182                 return Status;
   1183             }
   1184         }
   1185     }
   1186 
   1187     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
   1188 
   1189     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
   1190     if (ACPI_FAILURE (Status))
   1191     {
   1192         return (Status);
   1193     }
   1194 
   1195     /* No need to parse control methods of external table */
   1196 
   1197     if (External)
   1198     {
   1199         return AE_OK;
   1200     }
   1201 
   1202     /* Pass 3: Parse control methods and link their parse trees into the main parse tree */
   1203 
   1204     Status = AdParseDeferredOps (AcpiGbl_ParseOpRoot);
   1205 
   1206     /* Process Resource Templates */
   1207 
   1208     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
   1209 
   1210     fprintf (stderr, "Parsing completed\n");
   1211     return AE_OK;
   1212 }
   1213 
   1214 
   1215