1118613Snjl/******************************************************************************
2118613Snjl *
3118613Snjl * Module Name: adisasm - Application-level disassembler routines
4118613Snjl *
5118613Snjl *****************************************************************************/
6118613Snjl
7316303Sjkim/******************************************************************************
8316303Sjkim *
9316303Sjkim * 1. Copyright Notice
10316303Sjkim *
11316303Sjkim * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12118613Snjl * All rights reserved.
13118613Snjl *
14316303Sjkim * 2. License
15316303Sjkim *
16316303Sjkim * 2.1. This is your license from Intel Corp. under its intellectual property
17316303Sjkim * rights. You may have additional license terms from the party that provided
18316303Sjkim * you this software, covering your right to use that party's intellectual
19316303Sjkim * property rights.
20316303Sjkim *
21316303Sjkim * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22316303Sjkim * copy of the source code appearing in this file ("Covered Code") an
23316303Sjkim * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24316303Sjkim * base code distributed originally by Intel ("Original Intel Code") to copy,
25316303Sjkim * make derivatives, distribute, use and display any portion of the Covered
26316303Sjkim * Code in any form, with the right to sublicense such rights; and
27316303Sjkim *
28316303Sjkim * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29316303Sjkim * license (with the right to sublicense), under only those claims of Intel
30316303Sjkim * patents that are infringed by the Original Intel Code, to make, use, sell,
31316303Sjkim * offer to sell, and import the Covered Code and derivative works thereof
32316303Sjkim * solely to the minimum extent necessary to exercise the above copyright
33316303Sjkim * license, and in no event shall the patent license extend to any additions
34316303Sjkim * to or modifications of the Original Intel Code. No other license or right
35316303Sjkim * is granted directly or by implication, estoppel or otherwise;
36316303Sjkim *
37316303Sjkim * The above copyright and patent license is granted only if the following
38316303Sjkim * conditions are met:
39316303Sjkim *
40316303Sjkim * 3. Conditions
41316303Sjkim *
42316303Sjkim * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43316303Sjkim * Redistribution of source code of any substantial portion of the Covered
44316303Sjkim * Code or modification with rights to further distribute source must include
45316303Sjkim * the above Copyright Notice, the above License, this list of Conditions,
46316303Sjkim * and the following Disclaimer and Export Compliance provision. In addition,
47316303Sjkim * Licensee must cause all Covered Code to which Licensee contributes to
48316303Sjkim * contain a file documenting the changes Licensee made to create that Covered
49316303Sjkim * Code and the date of any change. Licensee must include in that file the
50316303Sjkim * documentation of any changes made by any predecessor Licensee. Licensee
51316303Sjkim * must include a prominent statement that the modification is derived,
52316303Sjkim * directly or indirectly, from Original Intel Code.
53316303Sjkim *
54316303Sjkim * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55316303Sjkim * Redistribution of source code of any substantial portion of the Covered
56316303Sjkim * Code or modification without rights to further distribute source must
57316303Sjkim * include the following Disclaimer and Export Compliance provision in the
58316303Sjkim * documentation and/or other materials provided with distribution. In
59316303Sjkim * addition, Licensee may not authorize further sublicense of source of any
60316303Sjkim * portion of the Covered Code, and must include terms to the effect that the
61316303Sjkim * license from Licensee to its licensee is limited to the intellectual
62316303Sjkim * property embodied in the software Licensee provides to its licensee, and
63316303Sjkim * not to intellectual property embodied in modifications its licensee may
64316303Sjkim * make.
65316303Sjkim *
66316303Sjkim * 3.3. Redistribution of Executable. Redistribution in executable form of any
67316303Sjkim * substantial portion of the Covered Code or modification must reproduce the
68316303Sjkim * above Copyright Notice, and the following Disclaimer and Export Compliance
69316303Sjkim * provision in the documentation and/or other materials provided with the
70316303Sjkim * distribution.
71316303Sjkim *
72316303Sjkim * 3.4. Intel retains all right, title, and interest in and to the Original
73316303Sjkim * Intel Code.
74316303Sjkim *
75316303Sjkim * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76316303Sjkim * Intel shall be used in advertising or otherwise to promote the sale, use or
77316303Sjkim * other dealings in products derived from or relating to the Covered Code
78316303Sjkim * without prior written authorization from Intel.
79316303Sjkim *
80316303Sjkim * 4. Disclaimer and Export Compliance
81316303Sjkim *
82316303Sjkim * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83316303Sjkim * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84316303Sjkim * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85316303Sjkim * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86316303Sjkim * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88316303Sjkim * PARTICULAR PURPOSE.
89316303Sjkim *
90316303Sjkim * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91316303Sjkim * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92316303Sjkim * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93316303Sjkim * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94316303Sjkim * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95316303Sjkim * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96316303Sjkim * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97316303Sjkim * LIMITED REMEDY.
98316303Sjkim *
99316303Sjkim * 4.3. Licensee shall not export, either directly or indirectly, any of this
100316303Sjkim * software or system incorporating such software without first obtaining any
101316303Sjkim * required license or other approval from the U. S. Department of Commerce or
102316303Sjkim * any other agency or department of the United States Government. In the
103316303Sjkim * event Licensee exports any such software from the United States or
104316303Sjkim * re-exports any such software from a foreign destination, Licensee shall
105316303Sjkim * ensure that the distribution and export/re-export of the software is in
106316303Sjkim * compliance with all laws, regulations, orders, or other restrictions of the
107316303Sjkim * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108316303Sjkim * any of its subsidiaries will export/re-export any technical data, process,
109316303Sjkim * software, or service, directly or indirectly, to any country for which the
110316303Sjkim * United States government or any agency thereof requires an export license,
111316303Sjkim * other governmental approval, or letter of assurance, without first obtaining
112316303Sjkim * such license, approval or letter.
113316303Sjkim *
114316303Sjkim *****************************************************************************
115316303Sjkim *
116316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
117316303Sjkim * following license:
118316303Sjkim *
119217365Sjkim * Redistribution and use in source and binary forms, with or without
120217365Sjkim * modification, are permitted provided that the following conditions
121217365Sjkim * are met:
122217365Sjkim * 1. Redistributions of source code must retain the above copyright
123217365Sjkim *    notice, this list of conditions, and the following disclaimer,
124217365Sjkim *    without modification.
125217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
127217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
128217365Sjkim *    including a substantially similar Disclaimer requirement for further
129217365Sjkim *    binary redistribution.
130217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
131217365Sjkim *    of any contributors may be used to endorse or promote products derived
132217365Sjkim *    from this software without specific prior written permission.
133118613Snjl *
134316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135316303Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136316303Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137316303Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138316303Sjkim * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139316303Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140316303Sjkim * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141316303Sjkim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142316303Sjkim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143316303Sjkim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144316303Sjkim * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145316303Sjkim *
146316303Sjkim * Alternatively, you may choose to be licensed under the terms of the
147217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
148217365Sjkim * Software Foundation.
149118613Snjl *
150316303Sjkim *****************************************************************************/
151118613Snjl
152272444Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
153193529Sjkim#include <contrib/dev/acpica/include/amlcode.h>
154193529Sjkim#include <contrib/dev/acpica/include/acdisasm.h>
155193529Sjkim#include <contrib/dev/acpica/include/acdispat.h>
156193529Sjkim#include <contrib/dev/acpica/include/acnamesp.h>
157298714Sjkim#include <contrib/dev/acpica/include/acparser.h>
158193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
159118613Snjl
160118613Snjl
161118613Snjl#define _COMPONENT          ACPI_TOOLS
162118613Snjl        ACPI_MODULE_NAME    ("adisasm")
163118613Snjl
164167802Sjkim/* Local prototypes */
165167802Sjkim
166298714Sjkimstatic ACPI_STATUS
167298714SjkimAdDoExternalFileList (
168298714Sjkim    char                    *Filename);
169298714Sjkim
170298714Sjkimstatic ACPI_STATUS
171298714SjkimAdDisassembleOneTable (
172298714Sjkim    ACPI_TABLE_HEADER       *Table,
173298714Sjkim    FILE                    *File,
174167802Sjkim    char                    *Filename,
175298714Sjkim    char                    *DisasmFilename);
176167802Sjkim
177281396Sjkimstatic ACPI_STATUS
178298714SjkimAdReparseOneTable (
179281396Sjkim    ACPI_TABLE_HEADER       *Table,
180298714Sjkim    FILE                    *File,
181298714Sjkim    ACPI_OWNER_ID           OwnerId);
182281396Sjkim
183167802Sjkim
184298714SjkimACPI_TABLE_DESC             LocalTables[1];
185298714SjkimACPI_PARSE_OBJECT           *AcpiGbl_ParseOpRoot;
186298714Sjkim
187298714Sjkim
188298714Sjkim/* Stubs for everything except ASL compiler */
189298714Sjkim
190151937Sjkim#ifndef ACPI_ASL_COMPILER
191118613SnjlBOOLEAN
192118613SnjlAcpiDsIsResultUsed (
193118613Snjl    ACPI_PARSE_OBJECT       *Op,
194118613Snjl    ACPI_WALK_STATE         *WalkState)
195118613Snjl{
196298714Sjkim    return (TRUE);
197118613Snjl}
198167802Sjkim
199167802SjkimACPI_STATUS
200167802SjkimAcpiDsMethodError (
201167802Sjkim    ACPI_STATUS             Status,
202167802Sjkim    ACPI_WALK_STATE         *WalkState)
203167802Sjkim{
204167802Sjkim    return (Status);
205167802Sjkim}
206118613Snjl#endif
207118613Snjl
208167802Sjkim
209118613Snjl/*******************************************************************************
210118613Snjl *
211118613Snjl * FUNCTION:    AdInitialize
212118613Snjl *
213198237Sjkim * PARAMETERS:  None
214118613Snjl *
215118613Snjl * RETURN:      Status
216118613Snjl *
217198237Sjkim * DESCRIPTION: ACPICA and local initialization
218118613Snjl *
219118613Snjl ******************************************************************************/
220118613Snjl
221118613SnjlACPI_STATUS
222118613SnjlAdInitialize (
223118613Snjl    void)
224118613Snjl{
225118613Snjl    ACPI_STATUS             Status;
226118613Snjl
227118613Snjl
228272444Sjkim    /* ACPICA subsystem initialization */
229118613Snjl
230167802Sjkim    Status = AcpiOsInitialize ();
231193529Sjkim    if (ACPI_FAILURE (Status))
232193529Sjkim    {
233316303Sjkim        fprintf (stderr, "Could not initialize ACPICA subsystem: %s\n",
234316303Sjkim            AcpiFormatException (Status));
235316303Sjkim
236193529Sjkim        return (Status);
237193529Sjkim    }
238167802Sjkim
239193529Sjkim    Status = AcpiUtInitGlobals ();
240193529Sjkim    if (ACPI_FAILURE (Status))
241193529Sjkim    {
242316303Sjkim        fprintf (stderr, "Could not initialize ACPICA globals: %s\n",
243316303Sjkim            AcpiFormatException (Status));
244316303Sjkim
245193529Sjkim        return (Status);
246193529Sjkim    }
247193529Sjkim
248118613Snjl    Status = AcpiUtMutexInitialize ();
249118613Snjl    if (ACPI_FAILURE (Status))
250118613Snjl    {
251316303Sjkim        fprintf (stderr, "Could not initialize ACPICA mutex objects: %s\n",
252316303Sjkim            AcpiFormatException (Status));
253316303Sjkim
254193529Sjkim        return (Status);
255118613Snjl    }
256118613Snjl
257118613Snjl    Status = AcpiNsRootInitialize ();
258167802Sjkim    if (ACPI_FAILURE (Status))
259167802Sjkim    {
260316303Sjkim        fprintf (stderr, "Could not initialize ACPICA namespace: %s\n",
261316303Sjkim            AcpiFormatException (Status));
262316303Sjkim
263193529Sjkim        return (Status);
264167802Sjkim    }
265167802Sjkim
266167802Sjkim    /* Setup the Table Manager (cheat - there is no RSDT) */
267167802Sjkim
268207344Sjkim    AcpiGbl_RootTableList.MaxTableCount = 1;
269207344Sjkim    AcpiGbl_RootTableList.CurrentTableCount = 0;
270167802Sjkim    AcpiGbl_RootTableList.Tables = LocalTables;
271167802Sjkim
272316303Sjkim    return (AE_OK);
273118613Snjl}
274118613Snjl
275118613Snjl
276118613Snjl/******************************************************************************
277118613Snjl *
278118613Snjl * FUNCTION:    AdAmlDisassemble
279118613Snjl *
280198237Sjkim * PARAMETERS:  Filename            - AML input filename
281198237Sjkim *              OutToFile           - TRUE if output should go to a file
282198237Sjkim *              Prefix              - Path prefix for output
283198237Sjkim *              OutFilename         - where the filename is returned
284118613Snjl *
285118613Snjl * RETURN:      Status
286118613Snjl *
287298714Sjkim * DESCRIPTION: Disassembler entry point. Disassemble an entire ACPI table.
288118613Snjl *
289118613Snjl *****************************************************************************/
290118613Snjl
291118613SnjlACPI_STATUS
292118613SnjlAdAmlDisassemble (
293118613Snjl    BOOLEAN                 OutToFile,
294118613Snjl    char                    *Filename,
295118613Snjl    char                    *Prefix,
296272444Sjkim    char                    **OutFilename)
297118613Snjl{
298118613Snjl    ACPI_STATUS             Status;
299118613Snjl    char                    *DisasmFilename = NULL;
300118613Snjl    FILE                    *File = NULL;
301193529Sjkim    ACPI_TABLE_HEADER       *Table = NULL;
302298714Sjkim    ACPI_NEW_TABLE_DESC     *ListHead = NULL;
303118613Snjl
304118613Snjl
305118613Snjl    /*
306198237Sjkim     * Input: AML code from either a file or via GetTables (memory or
307198237Sjkim     * registry)
308118613Snjl     */
309118613Snjl    if (Filename)
310118613Snjl    {
311298714Sjkim        /* Get the list of all AML tables in the file */
312298714Sjkim
313298714Sjkim        Status = AcGetAllTablesFromFile (Filename,
314298714Sjkim            ACPI_GET_ALL_TABLES, &ListHead);
315118613Snjl        if (ACPI_FAILURE (Status))
316118613Snjl        {
317298714Sjkim            AcpiOsPrintf ("Could not get ACPI tables from %s, %s\n",
318298714Sjkim                Filename, AcpiFormatException (Status));
319241973Sjkim            return (Status);
320118613Snjl        }
321167802Sjkim
322298714Sjkim        /* Process any user-specified files for external objects */
323193529Sjkim
324298714Sjkim        Status = AdDoExternalFileList (Filename);
325298714Sjkim        if (ACPI_FAILURE (Status))
326283092Sjkim        {
327298714Sjkim            return (Status);
328283092Sjkim        }
329118613Snjl    }
330118613Snjl    else
331118613Snjl    {
332272444Sjkim        Status = AdGetLocalTables ();
333118613Snjl        if (ACPI_FAILURE (Status))
334118613Snjl        {
335118613Snjl            AcpiOsPrintf ("Could not get ACPI tables, %s\n",
336118613Snjl                AcpiFormatException (Status));
337241973Sjkim            return (Status);
338118613Snjl        }
339118613Snjl
340287168Sjkim        if (!AcpiGbl_DmOpt_Disasm)
341118613Snjl        {
342241973Sjkim            return (AE_OK);
343118613Snjl        }
344118613Snjl
345167802Sjkim        /* Obtained the local tables, just disassemble the DSDT */
346118613Snjl
347167802Sjkim        Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
348167802Sjkim        if (ACPI_FAILURE (Status))
349167802Sjkim        {
350167802Sjkim            AcpiOsPrintf ("Could not get DSDT, %s\n",
351167802Sjkim                AcpiFormatException (Status));
352241973Sjkim            return (Status);
353167802Sjkim        }
354167802Sjkim
355118613Snjl        AcpiOsPrintf ("\nDisassembly of DSDT\n");
356167802Sjkim        Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
357118613Snjl    }
358118613Snjl
359118613Snjl    /*
360246849Sjkim     * Output: ASL code. Redirect to a file if requested
361118613Snjl     */
362118613Snjl    if (OutToFile)
363118613Snjl    {
364118613Snjl        /* Create/Open a disassembly output file */
365118613Snjl
366118613Snjl        DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
367272444Sjkim        if (!DisasmFilename)
368118613Snjl        {
369118613Snjl            fprintf (stderr, "Could not generate output filename\n");
370123315Snjl            Status = AE_ERROR;
371123315Snjl            goto Cleanup;
372118613Snjl        }
373118613Snjl
374118613Snjl        File = fopen (DisasmFilename, "w+");
375118613Snjl        if (!File)
376118613Snjl        {
377298714Sjkim            fprintf (stderr, "Could not open output file %s\n",
378298714Sjkim                DisasmFilename);
379123315Snjl            Status = AE_ERROR;
380123315Snjl            goto Cleanup;
381118613Snjl        }
382118613Snjl
383118613Snjl        AcpiOsRedirectOutput (File);
384118613Snjl    }
385118613Snjl
386118613Snjl    *OutFilename = DisasmFilename;
387118613Snjl
388298714Sjkim    /* Disassemble all AML tables within the file */
389281396Sjkim
390298714Sjkim    while (ListHead)
391167802Sjkim    {
392298714Sjkim        Status = AdDisassembleOneTable (ListHead->Table,
393298714Sjkim            File, Filename, DisasmFilename);
394167802Sjkim        if (ACPI_FAILURE (Status))
395167802Sjkim        {
396298714Sjkim            break;
397167802Sjkim        }
398128212Snjl
399298714Sjkim        ListHead = ListHead->Next;
400118613Snjl    }
401118613Snjl
402118613SnjlCleanup:
403193529Sjkim
404298714Sjkim    if (Table &&
405298714Sjkim        !AcpiGbl_ForceAmlDisassembly &&
406298714Sjkim        !AcpiUtIsAmlTable (Table))
407193529Sjkim    {
408193529Sjkim        ACPI_FREE (Table);
409193529Sjkim    }
410193529Sjkim
411322877Sjkim    AcDeleteTableList (ListHead);
412322877Sjkim
413278970Sjkim    if (File)
414118613Snjl    {
415118613Snjl        fclose (File);
416118613Snjl        AcpiOsRedirectOutput (stdout);
417118613Snjl    }
418118613Snjl
419167802Sjkim    AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
420193529Sjkim    AcpiGbl_ParseOpRoot = NULL;
421193529Sjkim    return (Status);
422118613Snjl}
423118613Snjl
424118613Snjl
425118613Snjl/******************************************************************************
426118613Snjl *
427298714Sjkim * FUNCTION:    AdDisassembleOneTable
428167802Sjkim *
429298714Sjkim * PARAMETERS:  Table               - Raw AML table
430298714Sjkim *              File                - Pointer for the input file
431298714Sjkim *              Filename            - AML input filename
432298714Sjkim *              DisasmFilename      - Output filename
433167802Sjkim *
434298714Sjkim * RETURN:      Status
435167802Sjkim *
436298714Sjkim * DESCRIPTION: Disassemble a single ACPI table. AML or data table.
437167802Sjkim *
438167802Sjkim *****************************************************************************/
439167802Sjkim
440298714Sjkimstatic ACPI_STATUS
441298714SjkimAdDisassembleOneTable (
442298714Sjkim    ACPI_TABLE_HEADER       *Table,
443298714Sjkim    FILE                    *File,
444281396Sjkim    char                    *Filename,
445298714Sjkim    char                    *DisasmFilename)
446167802Sjkim{
447298714Sjkim    ACPI_STATUS             Status;
448298714Sjkim    ACPI_OWNER_ID           OwnerId;
449167802Sjkim
450278970Sjkim
451316303Sjkim#ifdef ACPI_ASL_COMPILER
452316303Sjkim
453316303Sjkim    /*
454316303Sjkim     * For ASL-/ASL+ converter: replace the temporary "XXXX"
455316303Sjkim     * table signature with the original. This "XXXX" makes
456316303Sjkim     * it harder for the AML interpreter to run the badaml
457316303Sjkim     * (.xxx) file produced from the converter in case if
458316303Sjkim     * it fails to get deleted.
459316303Sjkim     */
460327557Sjkim    if (AcpiGbl_CaptureComments)
461316303Sjkim    {
462327557Sjkim        strncpy (Table->Signature, AcpiGbl_TableSig, ACPI_NAME_SIZE);
463316303Sjkim    }
464316303Sjkim#endif
465316303Sjkim
466298714Sjkim    /* ForceAmlDisassembly means to assume the table contains valid AML */
467167802Sjkim
468298714Sjkim    if (!AcpiGbl_ForceAmlDisassembly && !AcpiUtIsAmlTable (Table))
469298714Sjkim    {
470298714Sjkim        AdDisassemblerHeader (Filename, ACPI_IS_DATA_TABLE);
471167802Sjkim
472298714Sjkim        /* This is a "Data Table" (non-AML table) */
473167802Sjkim
474298714Sjkim        AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
475298714Sjkim            Table->Signature);
476298714Sjkim        AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]  "
477298714Sjkim            "FieldName : FieldValue\n */\n\n");
478298714Sjkim
479298714Sjkim        AcpiDmDumpDataTable (Table);
480298714Sjkim        fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
481298714Sjkim            Table->Signature);
482298714Sjkim
483298714Sjkim        if (File)
484281396Sjkim        {
485298714Sjkim            fprintf (stderr, "Formatted output:  %s - %u bytes\n",
486298714Sjkim                DisasmFilename, CmGetFileSize (File));
487281396Sjkim        }
488298714Sjkim
489298714Sjkim        return (AE_OK);
490278970Sjkim    }
491278970Sjkim
492298714Sjkim    /*
493298714Sjkim     * This is an AML table (DSDT or SSDT).
494298714Sjkim     * Always parse the tables, only option is what to display
495298714Sjkim     */
496298714Sjkim    Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
497298714Sjkim    if (ACPI_FAILURE (Status))
498298714Sjkim    {
499298714Sjkim        AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
500298714Sjkim            AcpiFormatException (Status));
501298714Sjkim        return (Status);
502298714Sjkim    }
503167802Sjkim
504298714Sjkim    /* Debug output, namespace and parse tree */
505167802Sjkim
506298714Sjkim    if (AslCompilerdebug && File)
507298714Sjkim    {
508298714Sjkim        AcpiOsPrintf ("/**** Before second load\n");
509118613Snjl
510298714Sjkim        NsSetupNamespaceListing (File);
511298714Sjkim        NsDisplayNamespace ();
512118613Snjl
513298714Sjkim        AcpiOsPrintf ("*****/\n");
514298714Sjkim    }
515118613Snjl
516298714Sjkim    /* Load namespace from names created within control methods */
517298714Sjkim
518298714Sjkim    AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
519298714Sjkim        AcpiGbl_RootNode, OwnerId);
520298714Sjkim
521193529Sjkim    /*
522298714Sjkim     * Cross reference the namespace here, in order to
523298714Sjkim     * generate External() statements
524193529Sjkim     */
525298714Sjkim    AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
526298714Sjkim        AcpiGbl_RootNode, OwnerId);
527118613Snjl
528298714Sjkim    if (AslCompilerdebug)
529298714Sjkim    {
530298714Sjkim        AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
531298714Sjkim    }
532193529Sjkim
533298714Sjkim    /* Find possible calls to external control methods */
534193529Sjkim
535298714Sjkim    AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
536193529Sjkim
537298714Sjkim    /*
538298714Sjkim     * If we found any external control methods, we must reparse
539298714Sjkim     * the entire tree with the new information (namely, the
540298714Sjkim     * number of arguments per method)
541298714Sjkim     */
542322877Sjkim    if (AcpiDmGetUnresolvedExternalMethodCount ())
543193529Sjkim    {
544298714Sjkim        Status = AdReparseOneTable (Table, File, OwnerId);
545298714Sjkim        if (ACPI_FAILURE (Status))
546193529Sjkim        {
547298714Sjkim            return (Status);
548193529Sjkim        }
549193529Sjkim    }
550193529Sjkim
551298714Sjkim    /*
552298714Sjkim     * Now that the namespace is finalized, we can perform namespace
553298714Sjkim     * transforms.
554298714Sjkim     *
555298714Sjkim     * 1) Convert fixed-offset references to resource descriptors
556298714Sjkim     *    to symbolic references (Note: modifies namespace)
557298714Sjkim     */
558322877Sjkim    AcpiDmConvertParseObjects (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
559193529Sjkim
560298714Sjkim    /* Optional displays */
561193529Sjkim
562298714Sjkim    if (AcpiGbl_DmOpt_Disasm)
563193529Sjkim    {
564298714Sjkim        /* This is the real disassembly */
565193529Sjkim
566298714Sjkim        AdDisplayTables (Filename, Table);
567118613Snjl
568298714Sjkim        /* Dump hex table if requested (-vt) */
569167802Sjkim
570298714Sjkim        AcpiDmDumpDataTable (Table);
571298714Sjkim
572298714Sjkim        fprintf (stderr, "Disassembly completed\n");
573298714Sjkim        if (File)
574272444Sjkim        {
575298714Sjkim            fprintf (stderr, "ASL Output:    %s - %u bytes\n",
576298714Sjkim                DisasmFilename, CmGetFileSize (File));
577272444Sjkim        }
578167802Sjkim
579298714Sjkim        if (Gbl_MapfileFlag)
580298714Sjkim        {
581298714Sjkim            fprintf (stderr, "%14s %s - %u bytes\n",
582298714Sjkim                Gbl_Files[ASL_FILE_MAP_OUTPUT].ShortDescription,
583298714Sjkim                Gbl_Files[ASL_FILE_MAP_OUTPUT].Filename,
584298714Sjkim                FlGetFileSize (ASL_FILE_MAP_OUTPUT));
585298714Sjkim        }
586272444Sjkim    }
587272444Sjkim
588298714Sjkim    return (AE_OK);
589118613Snjl}
590118613Snjl
591118613Snjl
592118613Snjl/******************************************************************************
593118613Snjl *
594298714Sjkim * FUNCTION:    AdReparseOneTable
595118613Snjl *
596298714Sjkim * PARAMETERS:  Table               - Raw AML table
597298714Sjkim *              File                - Pointer for the input file
598298714Sjkim *              OwnerId             - ID for this table
599118613Snjl *
600118613Snjl * RETURN:      Status
601118613Snjl *
602298714Sjkim * DESCRIPTION: Reparse a table that has already been loaded. Used to
603298714Sjkim *              integrate information about external control methods.
604298714Sjkim *              These methods may have been previously parsed incorrectly.
605118613Snjl *
606118613Snjl *****************************************************************************/
607118613Snjl
608298714Sjkimstatic ACPI_STATUS
609298714SjkimAdReparseOneTable (
610298714Sjkim    ACPI_TABLE_HEADER       *Table,
611298714Sjkim    FILE                    *File,
612298714Sjkim    ACPI_OWNER_ID           OwnerId)
613118613Snjl{
614298714Sjkim    ACPI_STATUS             Status;
615316303Sjkim    ACPI_COMMENT_ADDR_NODE  *AddrListHead;
616118613Snjl
617118613Snjl
618298714Sjkim    fprintf (stderr,
619298714Sjkim        "\nFound %u external control methods, "
620298714Sjkim        "reparsing with new information\n",
621322877Sjkim        AcpiDmGetUnresolvedExternalMethodCount ());
622118613Snjl
623298714Sjkim    /* Reparse, rebuild namespace */
624118613Snjl
625298714Sjkim    AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
626298714Sjkim    AcpiGbl_ParseOpRoot = NULL;
627298714Sjkim    AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
628118613Snjl
629298714Sjkim    AcpiGbl_RootNode                    = NULL;
630298714Sjkim    AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
631298714Sjkim    AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
632298714Sjkim    AcpiGbl_RootNodeStruct.Type         = ACPI_TYPE_DEVICE;
633298714Sjkim    AcpiGbl_RootNodeStruct.Parent       = NULL;
634298714Sjkim    AcpiGbl_RootNodeStruct.Child        = NULL;
635298714Sjkim    AcpiGbl_RootNodeStruct.Peer         = NULL;
636298714Sjkim    AcpiGbl_RootNodeStruct.Object       = NULL;
637298714Sjkim    AcpiGbl_RootNodeStruct.Flags        = 0;
638298714Sjkim
639298714Sjkim    Status = AcpiNsRootInitialize ();
640298714Sjkim    if (ACPI_FAILURE (Status))
641118613Snjl    {
642298714Sjkim        return (Status);
643118613Snjl    }
644118613Snjl
645298714Sjkim    /* New namespace, add the external definitions first */
646118613Snjl
647322877Sjkim    AcpiDmAddExternalListToNamespace ();
648118613Snjl
649316303Sjkim    /* For -ca option: clear the list of comment addresses. */
650316303Sjkim
651316303Sjkim    while (AcpiGbl_CommentAddrListHead)
652316303Sjkim    {
653316303Sjkim        AddrListHead= AcpiGbl_CommentAddrListHead;
654316303Sjkim        AcpiGbl_CommentAddrListHead = AcpiGbl_CommentAddrListHead->Next;
655316303Sjkim        AcpiOsFree(AddrListHead);
656316303Sjkim    }
657316303Sjkim
658298714Sjkim    /* Parse the table again. No need to reload it, however */
659281396Sjkim
660298714Sjkim    Status = AdParseTable (Table, NULL, FALSE, FALSE);
661281396Sjkim    if (ACPI_FAILURE (Status))
662281396Sjkim    {
663298714Sjkim        AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
664298714Sjkim            AcpiFormatException (Status));
665281396Sjkim        return (Status);
666281396Sjkim    }
667281396Sjkim
668298714Sjkim    /* Cross reference the namespace again */
669281396Sjkim
670298714Sjkim    AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
671298714Sjkim        AcpiGbl_RootNode, OwnerId);
672281396Sjkim
673298714Sjkim    AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
674298714Sjkim        AcpiGbl_RootNode, OwnerId);
675281396Sjkim
676298714Sjkim    /* Debug output - namespace and parse tree */
677118613Snjl
678298714Sjkim    if (AslCompilerdebug)
679118613Snjl    {
680298714Sjkim        AcpiOsPrintf ("/**** After second load and resource conversion\n");
681298714Sjkim        if (File)
682298714Sjkim        {
683298714Sjkim            NsSetupNamespaceListing (File);
684298714Sjkim            NsDisplayNamespace ();
685298714Sjkim        }
686118613Snjl
687298714Sjkim        AcpiOsPrintf ("*****/\n");
688298714Sjkim        AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
689272444Sjkim    }
690118613Snjl
691241973Sjkim    return (AE_OK);
692118613Snjl}
693118613Snjl
694167802Sjkim
695118613Snjl/******************************************************************************
696118613Snjl *
697298714Sjkim * FUNCTION:    AdDoExternalFileList
698118613Snjl *
699298714Sjkim * PARAMETERS:  Filename            - Input file for the table
700118613Snjl *
701118613Snjl * RETURN:      Status
702118613Snjl *
703298714Sjkim * DESCRIPTION: Process all tables found in the -e external files list
704118613Snjl *
705118613Snjl *****************************************************************************/
706118613Snjl
707298714Sjkimstatic ACPI_STATUS
708298714SjkimAdDoExternalFileList (
709298714Sjkim    char                    *Filename)
710118613Snjl{
711298714Sjkim    ACPI_EXTERNAL_FILE      *ExternalFileList;
712298714Sjkim    char                    *ExternalFilename;
713298714Sjkim    ACPI_NEW_TABLE_DESC     *ExternalListHead = NULL;
714298714Sjkim    ACPI_STATUS             Status;
715298714Sjkim    ACPI_STATUS             GlobalStatus = AE_OK;
716298714Sjkim    ACPI_OWNER_ID           OwnerId;
717118613Snjl
718118613Snjl
719298714Sjkim    /*
720298714Sjkim     * External filenames are specified on the command line like this:
721298714Sjkim     * Example: iasl -e file1,file2,file3 -d xxx.aml
722298714Sjkim     */
723298714Sjkim    ExternalFileList = AcpiGbl_ExternalFileList;
724118613Snjl
725298714Sjkim    /* Process each external file */
726118613Snjl
727298714Sjkim    while (ExternalFileList)
728118613Snjl    {
729298714Sjkim        ExternalFilename = ExternalFileList->Path;
730298714Sjkim        if (!strcmp (ExternalFilename, Filename))
731298714Sjkim        {
732298714Sjkim            /* Next external file */
733118613Snjl
734298714Sjkim            ExternalFileList = ExternalFileList->Next;
735298714Sjkim            continue;
736298714Sjkim        }
737118613Snjl
738298714Sjkim        AcpiOsPrintf ("External object resolution file %16s\n",
739298714Sjkim            ExternalFilename);
740118613Snjl
741298714Sjkim        Status = AcGetAllTablesFromFile (
742298714Sjkim            ExternalFilename, ACPI_GET_ONLY_AML_TABLES, &ExternalListHead);
743193529Sjkim        if (ACPI_FAILURE (Status))
744193529Sjkim        {
745298714Sjkim            if (Status == AE_TYPE)
746298714Sjkim            {
747298714Sjkim                ExternalFileList = ExternalFileList->Next;
748298714Sjkim                GlobalStatus = AE_TYPE;
749298714Sjkim                Status = AE_OK;
750298714Sjkim                continue;
751298714Sjkim            }
752298714Sjkim
753322877Sjkim            AcDeleteTableList (ExternalListHead);
754241973Sjkim            return (Status);
755193529Sjkim        }
756298714Sjkim
757298714Sjkim        /* Load external tables for symbol resolution */
758298714Sjkim
759298714Sjkim        while (ExternalListHead)
760193529Sjkim        {
761298714Sjkim            Status = AdParseTable (
762298714Sjkim                ExternalListHead->Table, &OwnerId, TRUE, TRUE);
763193529Sjkim            if (ACPI_FAILURE (Status))
764193529Sjkim            {
765298714Sjkim                AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
766298714Sjkim                    AcpiFormatException (Status));
767322877Sjkim                AcDeleteTableList (ExternalListHead);
768241973Sjkim                return (Status);
769193529Sjkim            }
770298714Sjkim
771298714Sjkim            /*
772298714Sjkim             * Load namespace from names created within control methods
773298714Sjkim             * Set owner id of nodes in external table
774298714Sjkim             */
775298714Sjkim            AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
776298714Sjkim                AcpiGbl_RootNode, OwnerId);
777298714Sjkim            AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
778298714Sjkim
779298714Sjkim            ExternalListHead = ExternalListHead->Next;
780193529Sjkim        }
781298714Sjkim
782298714Sjkim        /* Next external file */
783298714Sjkim
784298714Sjkim        ExternalFileList = ExternalFileList->Next;
785167802Sjkim    }
786167802Sjkim
787322877Sjkim    AcDeleteTableList (ExternalListHead);
788322877Sjkim
789298714Sjkim    if (ACPI_FAILURE (GlobalStatus))
790118613Snjl    {
791298714Sjkim        return (GlobalStatus);
792118613Snjl    }
793118613Snjl
794298714Sjkim    /* Clear external list generated by Scope in external tables */
795193529Sjkim
796298714Sjkim    if (AcpiGbl_ExternalFileList)
797193529Sjkim    {
798298714Sjkim        AcpiDmClearExternalList ();
799193529Sjkim    }
800193529Sjkim
801298714Sjkim    /* Load any externals defined in the optional external ref file */
802118613Snjl
803298714Sjkim    AcpiDmGetExternalsFromFile ();
804241973Sjkim    return (AE_OK);
805118613Snjl}
806