0ec69aba7371ee706daae3393dd7d03afa344a2d
[dragonfly.git] / sys / contrib / dev / acpica / source / common / dmtables.c
1 /******************************************************************************
2  *
3  * Module Name: dmtables - disassembler ACPI table support
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151
152 #include "aslcompiler.h"
153 #include "acdispat.h"
154 #include "acnamesp.h"
155 #include "actables.h"
156 #include "acparser.h"
157 #include "acapps.h"
158 #include "acmacros.h"
159 #include "acconvert.h"
160
161
162 #define _COMPONENT          ACPI_TOOLS
163         ACPI_MODULE_NAME    ("dmtables")
164
165
166 /* Local prototypes */
167
168 static void
169 AdCreateTableHeader (
170     char                    *Filename,
171     ACPI_TABLE_HEADER       *Table);
172
173 static ACPI_STATUS
174 AdStoreTable (
175     ACPI_TABLE_HEADER       *Table,
176     UINT32                  *TableIndex);
177
178
179 extern ACPI_TABLE_DESC      LocalTables[1];
180 extern ACPI_PARSE_OBJECT    *AcpiGbl_ParseOpRoot;
181
182
183 /******************************************************************************
184  *
185  * FUNCTION:    AdDisassemblerHeader
186  *
187  * PARAMETERS:  Filename            - Input file for the table
188  *              TableType           - Either AML or DataTable
189  *
190  * RETURN:      None
191  *
192  * DESCRIPTION: Create the disassembler header, including ACPICA signon with
193  *              current time and date.
194  *
195  *****************************************************************************/
196
197 void
198 AdDisassemblerHeader (
199     char                    *Filename,
200     UINT8                   TableType)
201 {
202     time_t                  Timer;
203
204
205     time (&Timer);
206
207     /* Header and input table info */
208
209     AcpiOsPrintf ("/*\n");
210     AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));
211
212     if (TableType == ACPI_IS_AML_TABLE)
213     {
214         if (AcpiGbl_CstyleDisassembly)
215         {
216             AcpiOsPrintf (
217                 " * Disassembling to symbolic ASL+ operators\n"
218                 " *\n");
219         }
220         else
221         {
222             AcpiOsPrintf (
223                 " * Disassembling to non-symbolic legacy ASL operators\n"
224                 " *\n");
225         }
226     }
227
228     AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
229     AcpiOsPrintf (" *\n");
230 }
231
232
233 /******************************************************************************
234  *
235  * FUNCTION:    AdCreateTableHeader
236  *
237  * PARAMETERS:  Filename            - Input file for the table
238  *              Table               - Pointer to the raw table
239  *
240  * RETURN:      None
241  *
242  * DESCRIPTION: Create the ASL table header, including ACPICA signon with
243  *              current time and date.
244  *
245  *****************************************************************************/
246
247 static void
248 AdCreateTableHeader (
249     char                    *Filename,
250     ACPI_TABLE_HEADER       *Table)
251 {
252     UINT8                   Checksum;
253
254
255     /* Reset globals for External statements */
256
257     AcpiGbl_NumExternalMethods = 0;
258     AcpiGbl_ResolvedExternalMethods = 0;
259
260     /*
261      * Print file header and dump original table header
262      */
263     AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
264
265     AcpiOsPrintf (" * Original Table Header:\n");
266     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
267     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
268
269     /* Print and validate the revision */
270
271     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
272
273     switch (Table->Revision)
274     {
275     case 0:
276
277         AcpiOsPrintf (" **** Invalid Revision");
278         break;
279
280     case 1:
281
282         /* Revision of DSDT controls the ACPI integer width */
283
284         if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
285         {
286             AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
287         }
288         break;
289
290     default:
291
292         break;
293     }
294
295     /* Print and validate the table checksum */
296
297     AcpiOsPrintf ("\n *     Checksum         0x%2.2X",        Table->Checksum);
298
299     Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
300     if (Checksum)
301     {
302         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
303             (UINT8) (Table->Checksum - Checksum));
304     }
305
306     AcpiOsPrintf ("\n");
307     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
308     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
309     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
310     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
311     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
312     AcpiOsPrintf (" */\n");
313
314     /*
315      * Print comments that come before this definition block.
316      */
317     if (Gbl_CaptureComments)
318     {
319         ASL_CV_PRINT_ONE_COMMENT(AcpiGbl_ParseOpRoot,AML_COMMENT_STANDARD, NULL, 0);
320     }
321
322     /*
323      * Open the ASL definition block.
324      *
325      * Note: the AMLFilename string is left zero-length in order to just let
326      * the compiler create it when the disassembled file is compiled. This
327      * makes it easier to rename the disassembled ASL file if needed.
328      */
329     AcpiOsPrintf (
330         "DefinitionBlock (\"\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
331         Table->Signature, Table->Revision,
332         Table->OemId, Table->OemTableId, Table->OemRevision);
333 }
334
335
336 /******************************************************************************
337  *
338  * FUNCTION:    AdDisplayTables
339  *
340  * PARAMETERS:  Filename            - Input file for the table
341  *              Table               - Pointer to the raw table
342  *
343  * RETURN:      Status
344  *
345  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
346  *
347  *****************************************************************************/
348
349 ACPI_STATUS
350 AdDisplayTables (
351     char                    *Filename,
352     ACPI_TABLE_HEADER       *Table)
353 {
354
355
356     if (!AcpiGbl_ParseOpRoot)
357     {
358         return (AE_NOT_EXIST);
359     }
360
361     if (!AcpiGbl_DmOpt_Listing)
362     {
363         AdCreateTableHeader (Filename, Table);
364     }
365
366     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
367     MpEmitMappingInfo ();
368
369     if (AcpiGbl_DmOpt_Listing)
370     {
371         AcpiOsPrintf ("\n\nTable Header:\n");
372         AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
373             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
374
375         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
376         AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
377             Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
378     }
379
380     return (AE_OK);
381 }
382
383
384 /*******************************************************************************
385  *
386  * FUNCTION:    AdStoreTable
387  *
388  * PARAMETERS:  Table               - Table header
389  *              TableIndex          - Where the table index is returned
390  *
391  * RETURN:      Status and table index.
392  *
393  * DESCRIPTION: Add an ACPI table to the global table list
394  *
395  ******************************************************************************/
396
397 static ACPI_STATUS
398 AdStoreTable (
399     ACPI_TABLE_HEADER       *Table,
400     UINT32                  *TableIndex)
401 {
402     ACPI_STATUS             Status;
403     ACPI_TABLE_DESC         *TableDesc;
404
405
406     Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
407     if (ACPI_FAILURE (Status))
408     {
409         return (Status);
410     }
411
412     /* Initialize added table */
413
414     AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
415         ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
416     Status = AcpiTbValidateTable (TableDesc);
417     return (Status);
418 }
419
420
421 /******************************************************************************
422  *
423  * FUNCTION:    AdGetLocalTables
424  *
425  * PARAMETERS:  None
426  *
427  * RETURN:      Status
428  *
429  * DESCRIPTION: Get the ACPI tables from either memory or a file
430  *
431  *****************************************************************************/
432
433 ACPI_STATUS
434 AdGetLocalTables (
435     void)
436 {
437     ACPI_STATUS             Status;
438     ACPI_TABLE_HEADER       TableHeader;
439     ACPI_TABLE_HEADER       *NewTable;
440     UINT32                  TableIndex;
441
442
443     /* Get the DSDT via table override */
444
445     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
446     AcpiOsTableOverride (&TableHeader, &NewTable);
447     if (!NewTable)
448     {
449         fprintf (stderr, "Could not obtain DSDT\n");
450         return (AE_NO_ACPI_TABLES);
451     }
452
453     AdWriteTable (NewTable, NewTable->Length,
454         ACPI_SIG_DSDT, NewTable->OemTableId);
455
456     /* Store DSDT in the Table Manager */
457
458     Status = AdStoreTable (NewTable, &TableIndex);
459     if (ACPI_FAILURE (Status))
460     {
461         fprintf (stderr, "Could not store DSDT\n");
462         return (AE_NO_ACPI_TABLES);
463     }
464
465     return (AE_OK);
466 }
467
468
469 /******************************************************************************
470  *
471  * FUNCTION:    AdParseTable
472  *
473  * PARAMETERS:  Table               - Pointer to the raw table
474  *              OwnerId             - Returned OwnerId of the table
475  *              LoadTable           - If add table to the global table list
476  *              External            - If this is an external table
477  *
478  * RETURN:      Status
479  *
480  * DESCRIPTION: Parse an ACPI AML table
481  *
482  *****************************************************************************/
483
484 ACPI_STATUS
485 AdParseTable (
486     ACPI_TABLE_HEADER       *Table,
487     ACPI_OWNER_ID           *OwnerId,
488     BOOLEAN                 LoadTable,
489     BOOLEAN                 External)
490 {
491     ACPI_STATUS             Status = AE_OK;
492     ACPI_WALK_STATE         *WalkState;
493     UINT8                   *AmlStart;
494     UINT32                  AmlLength;
495     UINT32                  TableIndex;
496
497
498     if (!Table)
499     {
500         return (AE_NOT_EXIST);
501     }
502
503     /* Pass 1:  Parse everything except control method bodies */
504
505     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
506
507     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
508     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
509     ASL_CV_INIT_FILETREE(Table, AmlStart, AmlLength);
510
511     /* Create the root object */
512
513     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
514     if (!AcpiGbl_ParseOpRoot)
515     {
516         return (AE_NO_MEMORY);
517     }
518
519 #ifdef ACPI_ASL_COMPILER
520     if (Gbl_CaptureComments)
521     {
522         AcpiGbl_ParseOpRoot->Common.CvFilename = AcpiGbl_FileTreeRoot->Filename;
523     }
524     else
525     {
526         AcpiGbl_ParseOpRoot->Common.CvFilename = NULL;
527     }
528 #endif
529
530     /* Create and initialize a new walk state */
531
532     WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);
533     if (!WalkState)
534     {
535         return (AE_NO_MEMORY);
536     }
537
538     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
539         NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
540     if (ACPI_FAILURE (Status))
541     {
542         return (Status);
543     }
544
545     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
546     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
547
548     Status = AcpiPsParseAml (WalkState);
549     if (ACPI_FAILURE (Status))
550     {
551         return (Status);
552     }
553
554     /* If LoadTable is FALSE, we are parsing the last loaded table */
555
556     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
557
558     /* Pass 2 */
559
560     if (LoadTable)
561     {
562         Status = AdStoreTable (Table, &TableIndex);
563         if (ACPI_FAILURE (Status))
564         {
565             return (Status);
566         }
567         Status = AcpiTbAllocateOwnerId (TableIndex);
568         if (ACPI_FAILURE (Status))
569         {
570             return (Status);
571         }
572         if (OwnerId)
573         {
574             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
575             if (ACPI_FAILURE (Status))
576             {
577                 return (Status);
578             }
579         }
580     }
581
582     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
583
584     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
585     if (ACPI_FAILURE (Status))
586     {
587         return (Status);
588     }
589
590     /* No need to parse control methods of external table */
591
592     if (External)
593     {
594         return (AE_OK);
595     }
596
597     /*
598      * Pass 3: Parse control methods and link their parse trees
599      * into the main parse tree
600      */
601     fprintf (stderr,
602         "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
603
604     Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
605     fprintf (stderr, "\n");
606
607     /* Process Resource Templates */
608
609     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
610
611     fprintf (stderr, "Parsing completed\n");
612     return (AE_OK);
613 }