Sync ACPICA with Intel's version 20180508 (from previously 20170831).
[dragonfly.git] / sys / contrib / dev / acpica / source / common / dmtables.c
blob293b3d8bebe87b4d9734f7d846bf962cc637879f
1 /******************************************************************************
3 * Module Name: dmtables - disassembler ACPI table support
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
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.
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.
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.
150 *****************************************************************************/
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"
162 #define _COMPONENT ACPI_TOOLS
163 ACPI_MODULE_NAME ("dmtables")
166 /* Local prototypes */
168 static void
169 AdCreateTableHeader (
170 char *Filename,
171 ACPI_TABLE_HEADER *Table);
173 static ACPI_STATUS
174 AdStoreTable (
175 ACPI_TABLE_HEADER *Table,
176 UINT32 *TableIndex);
179 extern ACPI_TABLE_DESC LocalTables[1];
180 extern ACPI_PARSE_OBJECT *AcpiGbl_ParseOpRoot;
183 /******************************************************************************
185 * FUNCTION: AdDisassemblerHeader
187 * PARAMETERS: Filename - Input file for the table
188 * TableType - Either AML or DataTable
190 * RETURN: None
192 * DESCRIPTION: Create the disassembler header, including ACPICA signon with
193 * current time and date.
195 *****************************************************************************/
197 void
198 AdDisassemblerHeader (
199 char *Filename,
200 UINT8 TableType)
202 time_t Timer;
205 time (&Timer);
207 /* Header and input table info */
209 AcpiOsPrintf ("/*\n");
210 AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));
212 if (TableType == ACPI_IS_AML_TABLE)
214 if (AcpiGbl_CstyleDisassembly)
216 AcpiOsPrintf (
217 " * Disassembling to symbolic ASL+ operators\n"
218 " *\n");
220 else
222 AcpiOsPrintf (
223 " * Disassembling to non-symbolic legacy ASL operators\n"
224 " *\n");
228 AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
229 AcpiOsPrintf (" *\n");
233 /******************************************************************************
235 * FUNCTION: AdCreateTableHeader
237 * PARAMETERS: Filename - Input file for the table
238 * Table - Pointer to the raw table
240 * RETURN: None
242 * DESCRIPTION: Create the ASL table header, including ACPICA signon with
243 * current time and date.
245 *****************************************************************************/
247 static void
248 AdCreateTableHeader (
249 char *Filename,
250 ACPI_TABLE_HEADER *Table)
252 UINT8 Checksum;
255 /* Reset globals for External statements */
257 AcpiGbl_NumExternalMethods = 0;
258 AcpiGbl_ResolvedExternalMethods = 0;
261 * Print file header and dump original table header
263 AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
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);
269 /* Print and validate the revision */
271 AcpiOsPrintf (" * Revision 0x%2.2X", Table->Revision);
273 switch (Table->Revision)
275 case 0:
277 AcpiOsPrintf (" **** Invalid Revision");
278 break;
280 case 1:
282 /* Revision of DSDT controls the ACPI integer width */
284 if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
286 AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
288 break;
290 default:
292 break;
295 /* Print and validate the table checksum */
297 AcpiOsPrintf ("\n * Checksum 0x%2.2X", Table->Checksum);
299 Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
300 if (Checksum)
302 AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
303 (UINT8) (Table->Checksum - Checksum));
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");
315 * Print comments that come before this definition block.
317 if (AcpiGbl_CaptureComments)
319 ASL_CV_PRINT_ONE_COMMENT(AcpiGbl_ParseOpRoot,AML_COMMENT_STANDARD, NULL, 0);
323 * Open the ASL definition block.
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.
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);
336 /******************************************************************************
338 * FUNCTION: AdDisplayTables
340 * PARAMETERS: Filename - Input file for the table
341 * Table - Pointer to the raw table
343 * RETURN: Status
345 * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
347 *****************************************************************************/
349 ACPI_STATUS
350 AdDisplayTables (
351 char *Filename,
352 ACPI_TABLE_HEADER *Table)
356 if (!AcpiGbl_ParseOpRoot)
358 return (AE_NOT_EXIST);
361 if (!AcpiGbl_DmOpt_Listing)
363 AdCreateTableHeader (Filename, Table);
366 AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
367 MpEmitMappingInfo ();
369 if (AcpiGbl_DmOpt_Listing)
371 AcpiOsPrintf ("\n\nTable Header:\n");
372 AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
373 DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
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);
380 return (AE_OK);
384 /*******************************************************************************
386 * FUNCTION: AdStoreTable
388 * PARAMETERS: Table - Table header
389 * TableIndex - Where the table index is returned
391 * RETURN: Status and table index.
393 * DESCRIPTION: Add an ACPI table to the global table list
395 ******************************************************************************/
397 static ACPI_STATUS
398 AdStoreTable (
399 ACPI_TABLE_HEADER *Table,
400 UINT32 *TableIndex)
402 ACPI_STATUS Status;
403 ACPI_TABLE_DESC *TableDesc;
406 Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
407 if (ACPI_FAILURE (Status))
409 return (Status);
412 /* Initialize added table */
414 AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
415 ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
416 Status = AcpiTbValidateTable (TableDesc);
417 return (Status);
421 /******************************************************************************
423 * FUNCTION: AdGetLocalTables
425 * PARAMETERS: None
427 * RETURN: Status
429 * DESCRIPTION: Get the ACPI tables from either memory or a file
431 *****************************************************************************/
433 ACPI_STATUS
434 AdGetLocalTables (
435 void)
437 ACPI_STATUS Status;
438 ACPI_TABLE_HEADER TableHeader;
439 ACPI_TABLE_HEADER *NewTable;
440 UINT32 TableIndex;
443 /* Get the DSDT via table override */
445 ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
446 AcpiOsTableOverride (&TableHeader, &NewTable);
447 if (!NewTable)
449 fprintf (stderr, "Could not obtain DSDT\n");
450 return (AE_NO_ACPI_TABLES);
453 AdWriteTable (NewTable, NewTable->Length,
454 ACPI_SIG_DSDT, NewTable->OemTableId);
456 /* Store DSDT in the Table Manager */
458 Status = AdStoreTable (NewTable, &TableIndex);
459 if (ACPI_FAILURE (Status))
461 fprintf (stderr, "Could not store DSDT\n");
462 return (AE_NO_ACPI_TABLES);
465 return (AE_OK);
469 /******************************************************************************
471 * FUNCTION: AdParseTable
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
478 * RETURN: Status
480 * DESCRIPTION: Parse an ACPI AML table
482 *****************************************************************************/
484 ACPI_STATUS
485 AdParseTable (
486 ACPI_TABLE_HEADER *Table,
487 ACPI_OWNER_ID *OwnerId,
488 BOOLEAN LoadTable,
489 BOOLEAN External)
491 ACPI_STATUS Status = AE_OK;
492 ACPI_WALK_STATE *WalkState;
493 UINT8 *AmlStart;
494 UINT32 AmlLength;
495 UINT32 TableIndex;
498 if (!Table)
500 return (AE_NOT_EXIST);
503 /* Pass 1: Parse everything except control method bodies */
505 fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
507 AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
508 AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
509 ASL_CV_INIT_FILETREE(Table, AmlStart, AmlLength);
511 /* Create the root object */
513 AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
514 if (!AcpiGbl_ParseOpRoot)
516 return (AE_NO_MEMORY);
519 #ifdef ACPI_ASL_COMPILER
520 if (AcpiGbl_CaptureComments)
522 AcpiGbl_ParseOpRoot->Common.CvFilename = AcpiGbl_FileTreeRoot->Filename;
524 else
526 AcpiGbl_ParseOpRoot->Common.CvFilename = NULL;
528 #endif
530 /* Create and initialize a new walk state */
532 WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);
533 if (!WalkState)
535 return (AE_NO_MEMORY);
538 Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
539 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
540 if (ACPI_FAILURE (Status))
542 return (Status);
545 WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
546 WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
548 Status = AcpiPsParseAml (WalkState);
549 if (ACPI_FAILURE (Status))
551 return (Status);
554 /* If LoadTable is FALSE, we are parsing the last loaded table */
556 TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
558 /* Pass 2 */
560 if (LoadTable)
562 Status = AdStoreTable (Table, &TableIndex);
563 if (ACPI_FAILURE (Status))
565 return (Status);
567 Status = AcpiTbAllocateOwnerId (TableIndex);
568 if (ACPI_FAILURE (Status))
570 return (Status);
572 if (OwnerId)
574 Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
575 if (ACPI_FAILURE (Status))
577 return (Status);
582 fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
584 Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
585 if (ACPI_FAILURE (Status))
587 return (Status);
590 /* No need to parse control methods of external table */
592 if (External)
594 return (AE_OK);
598 * Pass 3: Parse control methods and link their parse trees
599 * into the main parse tree
601 fprintf (stderr,
602 "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
604 Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
605 fprintf (stderr, "\n");
607 /* Process Resource Templates */
609 AcpiDmFindResources (AcpiGbl_ParseOpRoot);
611 fprintf (stderr, "Parsing completed\n");
612 return (AE_OK);