xref: /freebsd/sys/contrib/dev/acpica/common/dmtables.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
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 - 2024, 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 <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include <contrib/dev/acpica/include/acdispat.h>
154 #include <contrib/dev/acpica/include/acnamesp.h>
155 #include <contrib/dev/acpica/include/actables.h>
156 #include <contrib/dev/acpica/include/acparser.h>
157 #include <contrib/dev/acpica/include/acapps.h>
158 #include <contrib/dev/acpica/include/acmacros.h>
159 #include <contrib/dev/acpica/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  *              optional 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     /* Header and input table info */
206 
207     AcpiOsPrintf ("/*\n");
208     AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));
209 
210     if (TableType == ACPI_IS_AML_TABLE)
211     {
212         if (AcpiGbl_CstyleDisassembly)
213         {
214             AcpiOsPrintf (
215                 " * Disassembling to symbolic ASL+ operators\n"
216                 " *\n");
217         }
218         else
219         {
220             AcpiOsPrintf (
221                 " * Disassembling to non-symbolic legacy ASL operators\n"
222                 " *\n");
223         }
224     }
225 
226     if (AslGbl_Deterministic)
227     {
228         AcpiOsPrintf (" * Disassembly of %s\n", Filename);
229     }
230     else
231     {
232         time (&Timer);
233         AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
234     }
235     AcpiOsPrintf (" *\n");
236 }
237 
238 
239 /******************************************************************************
240  *
241  * FUNCTION:    AdCreateTableHeader
242  *
243  * PARAMETERS:  Filename            - Input file for the table
244  *              Table               - Pointer to the raw table
245  *
246  * RETURN:      None
247  *
248  * DESCRIPTION: Create the ASL table header, including ACPICA signon with
249  *              current time and date.
250  *
251  *****************************************************************************/
252 
253 static void
254 AdCreateTableHeader (
255     char                    *Filename,
256     ACPI_TABLE_HEADER       *Table)
257 {
258     UINT8                   Checksum;
259 
260 
261     /* Reset globals for External statements */
262 
263     AcpiGbl_NumExternalMethods = 0;
264     AcpiGbl_ResolvedExternalMethods = 0;
265 
266     /*
267      * Print file header and dump original table header
268      */
269     AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
270 
271     AcpiOsPrintf (" * Original Table Header:\n");
272     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
273     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
274 
275     /* Print and validate the revision */
276 
277     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
278 
279     switch (Table->Revision)
280     {
281     case 0:
282 
283         AcpiOsPrintf (" **** Invalid Revision");
284         break;
285 
286     case 1:
287 
288         /* Revision of DSDT controls the ACPI integer width */
289 
290         if (ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_DSDT))
291         {
292             AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
293         }
294         break;
295 
296     default:
297 
298         break;
299     }
300 
301     /* Print and validate the table checksum */
302 
303     AcpiOsPrintf ("\n *     Checksum         0x%2.2X",        Table->Checksum);
304 
305     Checksum = AcpiUtChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
306     if (Checksum)
307     {
308         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
309             (UINT8) (Table->Checksum - Checksum));
310     }
311 
312     AcpiOsPrintf ("\n");
313     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
314     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
315     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
316     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
317     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
318     AcpiOsPrintf (" */\n");
319 
320     /*
321      * Print comments that come before this definition block.
322      */
323     if (AcpiGbl_CaptureComments)
324     {
325         ASL_CV_PRINT_ONE_COMMENT(AcpiGbl_ParseOpRoot,AML_COMMENT_STANDARD, NULL, 0);
326     }
327 
328     /*
329      * Open the ASL definition block.
330      *
331      * Note: the AMLFilename string is left zero-length in order to just let
332      * the compiler create it when the disassembled file is compiled. This
333      * makes it easier to rename the disassembled ASL file if needed.
334      */
335     AcpiOsPrintf (
336         "DefinitionBlock (\"\", \"%4.4s\", %u, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
337         Table->Signature, Table->Revision,
338         Table->OemId, Table->OemTableId, Table->OemRevision);
339 }
340 
341 
342 /******************************************************************************
343  *
344  * FUNCTION:    AdDisplayTables
345  *
346  * PARAMETERS:  Filename            - Input file for the table
347  *              Table               - Pointer to the raw table
348  *
349  * RETURN:      Status
350  *
351  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
352  *
353  *****************************************************************************/
354 
355 ACPI_STATUS
356 AdDisplayTables (
357     char                    *Filename,
358     ACPI_TABLE_HEADER       *Table)
359 {
360 
361 
362     if (!AcpiGbl_ParseOpRoot)
363     {
364         return (AE_NOT_EXIST);
365     }
366 
367     if (!AcpiGbl_DmOpt_Listing)
368     {
369         AdCreateTableHeader (Filename, Table);
370     }
371 
372     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
373     MpEmitMappingInfo ();
374 
375     if (AcpiGbl_DmOpt_Listing)
376     {
377         AcpiOsPrintf ("\n\nTable Header:\n");
378         AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
379             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
380 
381         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
382         AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
383             Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
384     }
385 
386     return (AE_OK);
387 }
388 
389 
390 /*******************************************************************************
391  *
392  * FUNCTION:    AdStoreTable
393  *
394  * PARAMETERS:  Table               - Table header
395  *              TableIndex          - Where the table index is returned
396  *
397  * RETURN:      Status and table index.
398  *
399  * DESCRIPTION: Add an ACPI table to the global table list
400  *
401  ******************************************************************************/
402 
403 static ACPI_STATUS
404 AdStoreTable (
405     ACPI_TABLE_HEADER       *Table,
406     UINT32                  *TableIndex)
407 {
408     ACPI_STATUS             Status;
409     ACPI_TABLE_DESC         *TableDesc;
410 
411 
412     Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
413     if (ACPI_FAILURE (Status))
414     {
415         return (Status);
416     }
417 
418     /* Initialize added table */
419 
420     AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
421         ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
422     Status = AcpiTbValidateTable (TableDesc);
423     return (Status);
424 }
425 
426 
427 /******************************************************************************
428  *
429  * FUNCTION:    AdGetLocalTables
430  *
431  * PARAMETERS:  None
432  *
433  * RETURN:      Status
434  *
435  * DESCRIPTION: Get the ACPI tables from either memory or a file
436  *
437  *****************************************************************************/
438 
439 ACPI_STATUS
440 AdGetLocalTables (
441     void)
442 {
443     ACPI_STATUS             Status;
444     ACPI_TABLE_HEADER       TableHeader;
445     ACPI_TABLE_HEADER       *NewTable;
446     UINT32                  TableIndex;
447 
448 
449     /* Get the DSDT via table override */
450 
451     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
452     Status = AcpiOsTableOverride (&TableHeader, &NewTable);
453     if (ACPI_FAILURE (Status) || !NewTable)
454     {
455         fprintf (stderr, "Could not obtain DSDT\n");
456         return (AE_NO_ACPI_TABLES);
457     }
458 
459     AdWriteTable (NewTable, NewTable->Length,
460         ACPI_SIG_DSDT, NewTable->OemTableId);
461 
462     /* Store DSDT in the Table Manager */
463 
464     Status = AdStoreTable (NewTable, &TableIndex);
465     if (ACPI_FAILURE (Status))
466     {
467         fprintf (stderr, "Could not store DSDT\n");
468         return (AE_NO_ACPI_TABLES);
469     }
470 
471     return (AE_OK);
472 }
473 
474 
475 /******************************************************************************
476  *
477  * FUNCTION:    AdParseTable
478  *
479  * PARAMETERS:  Table               - Pointer to the raw table
480  *              OwnerId             - Returned OwnerId of the table
481  *              LoadTable           - If add table to the global table list
482  *              External            - If this is an external table
483  *
484  * RETURN:      Status
485  *
486  * DESCRIPTION: Parse an ACPI AML table
487  *
488  *****************************************************************************/
489 
490 ACPI_STATUS
491 AdParseTable (
492     ACPI_TABLE_HEADER       *Table,
493     ACPI_OWNER_ID           *OwnerId,
494     BOOLEAN                 LoadTable,
495     BOOLEAN                 External)
496 {
497     ACPI_STATUS             Status = AE_OK;
498     ACPI_WALK_STATE         *WalkState;
499     UINT8                   *AmlStart;
500     UINT32                  AmlLength;
501     UINT32                  TableIndex;
502 
503 
504     if (!Table)
505     {
506         return (AE_NOT_EXIST);
507     }
508 
509     /* Pass 1:  Parse everything except control method bodies */
510 
511     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
512 
513     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
514     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
515 
516     AcpiUtSetIntegerWidth (Table->Revision);
517 
518     /* Create the root object */
519 
520     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
521     if (!AcpiGbl_ParseOpRoot)
522     {
523         return (AE_NO_MEMORY);
524     }
525 
526 #ifdef ACPI_ASL_COMPILER
527     if (AcpiGbl_CaptureComments)
528     {
529         AcpiGbl_ParseOpRoot->Common.CvFilename = AcpiGbl_FileTreeRoot->Filename;
530     }
531     else
532     {
533         AcpiGbl_ParseOpRoot->Common.CvFilename = NULL;
534     }
535 #endif
536 
537     /* Create and initialize a new walk state */
538 
539     WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);
540     if (!WalkState)
541     {
542         return (AE_NO_MEMORY);
543     }
544 
545     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
546         NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
547     if (ACPI_FAILURE (Status))
548     {
549         return (Status);
550     }
551 
552     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
553 
554     Status = AcpiPsParseAml (WalkState);
555     if (ACPI_FAILURE (Status))
556     {
557         return (Status);
558     }
559 
560     /* If LoadTable is FALSE, we are parsing the last loaded table */
561 
562     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
563 
564     /* Pass 2 */
565 
566     if (LoadTable)
567     {
568         Status = AdStoreTable (Table, &TableIndex);
569         if (ACPI_FAILURE (Status))
570         {
571             return (Status);
572         }
573         Status = AcpiTbAllocateOwnerId (TableIndex);
574         if (ACPI_FAILURE (Status))
575         {
576             return (Status);
577         }
578         if (OwnerId)
579         {
580             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
581             if (ACPI_FAILURE (Status))
582             {
583                 return (Status);
584             }
585         }
586     }
587 
588     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
589 
590     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
591     if (ACPI_FAILURE (Status))
592     {
593         return (Status);
594     }
595 
596     /* No need to parse control methods of external table */
597 
598     if (External)
599     {
600         return (AE_OK);
601     }
602 
603     /*
604      * Pass 3: Parse control methods and link their parse trees
605      * into the main parse tree
606      */
607     fprintf (stderr,
608         "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
609 
610     (void) AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
611     fprintf (stderr, "\n");
612 
613     /* Process Resource Templates */
614 
615     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
616 
617     fprintf (stderr, "Parsing completed\n");
618     return (AE_OK);
619 }
620