xref: /freebsd/sys/contrib/dev/acpica/common/dmtables.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /******************************************************************************
2  *
3  * Module Name: dmtables - disassembler ACPI table support
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/include/acapps.h>
46 #include <contrib/dev/acpica/include/acdispat.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
48 #include <contrib/dev/acpica/include/actables.h>
49 #include <contrib/dev/acpica/include/acparser.h>
50 
51 #include <stdio.h>
52 #include <time.h>
53 
54 #define _COMPONENT          ACPI_TOOLS
55         ACPI_MODULE_NAME    ("dmtables")
56 
57 
58 /* Local prototypes */
59 
60 static void
61 AdCreateTableHeader (
62     char                    *Filename,
63     ACPI_TABLE_HEADER       *Table);
64 
65 static ACPI_STATUS
66 AdStoreTable (
67     ACPI_TABLE_HEADER       *Table,
68     UINT32                  *TableIndex);
69 
70 
71 extern ACPI_TABLE_DESC      LocalTables[1];
72 extern ACPI_PARSE_OBJECT    *AcpiGbl_ParseOpRoot;
73 
74 
75 /******************************************************************************
76  *
77  * FUNCTION:    AdDisassemblerHeader
78  *
79  * PARAMETERS:  Filename            - Input file for the table
80  *              TableType           - Either AML or DataTable
81  *
82  * RETURN:      None
83  *
84  * DESCRIPTION: Create the disassembler header, including ACPICA signon with
85  *              current time and date.
86  *
87  *****************************************************************************/
88 
89 void
90 AdDisassemblerHeader (
91     char                    *Filename,
92     UINT8                   TableType)
93 {
94     time_t                  Timer;
95 
96 
97     time (&Timer);
98 
99     /* Header and input table info */
100 
101     AcpiOsPrintf ("/*\n");
102     AcpiOsPrintf (ACPI_COMMON_HEADER (AML_DISASSEMBLER_NAME, " * "));
103 
104     if (TableType == ACPI_IS_AML_TABLE)
105     {
106         if (AcpiGbl_CstyleDisassembly)
107         {
108             AcpiOsPrintf (
109                 " * Disassembling to symbolic ASL+ operators\n"
110                 " *\n");
111         }
112         else
113         {
114             AcpiOsPrintf (
115                 " * Disassembling to non-symbolic legacy ASL operators\n"
116                 " *\n");
117         }
118     }
119 
120     AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
121     AcpiOsPrintf (" *\n");
122 }
123 
124 
125 /******************************************************************************
126  *
127  * FUNCTION:    AdCreateTableHeader
128  *
129  * PARAMETERS:  Filename            - Input file for the table
130  *              Table               - Pointer to the raw table
131  *
132  * RETURN:      None
133  *
134  * DESCRIPTION: Create the ASL table header, including ACPICA signon with
135  *              current time and date.
136  *
137  *****************************************************************************/
138 
139 static void
140 AdCreateTableHeader (
141     char                    *Filename,
142     ACPI_TABLE_HEADER       *Table)
143 {
144     UINT8                   Checksum;
145 
146 
147     /* Reset globals for External statements */
148 
149     AcpiGbl_NumExternalMethods = 0;
150     AcpiGbl_ResolvedExternalMethods = 0;
151 
152     /*
153      * Print file header and dump original table header
154      */
155     AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
156 
157     AcpiOsPrintf (" * Original Table Header:\n");
158     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
159     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
160 
161     /* Print and validate the revision */
162 
163     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
164 
165     switch (Table->Revision)
166     {
167     case 0:
168 
169         AcpiOsPrintf (" **** Invalid Revision");
170         break;
171 
172     case 1:
173 
174         /* Revision of DSDT controls the ACPI integer width */
175 
176         if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
177         {
178             AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
179         }
180         break;
181 
182     default:
183 
184         break;
185     }
186 
187     /* Print and validate the table checksum */
188 
189     AcpiOsPrintf ("\n *     Checksum         0x%2.2X",        Table->Checksum);
190 
191     Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
192     if (Checksum)
193     {
194         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
195             (UINT8) (Table->Checksum - Checksum));
196     }
197 
198     AcpiOsPrintf ("\n");
199     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
200     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
201     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
202     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
203     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
204     AcpiOsPrintf (" */\n");
205 
206     /*
207      * Open the ASL definition block.
208      *
209      * Note: the AMLFilename string is left zero-length in order to just let
210      * the compiler create it when the disassembled file is compiled. This
211      * makes it easier to rename the disassembled ASL file if needed.
212      */
213     AcpiOsPrintf (
214         "DefinitionBlock (\"\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
215         Table->Signature, Table->Revision,
216         Table->OemId, Table->OemTableId, Table->OemRevision);
217 }
218 
219 
220 /******************************************************************************
221  *
222  * FUNCTION:    AdDisplayTables
223  *
224  * PARAMETERS:  Filename            - Input file for the table
225  *              Table               - Pointer to the raw table
226  *
227  * RETURN:      Status
228  *
229  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
230  *
231  *****************************************************************************/
232 
233 ACPI_STATUS
234 AdDisplayTables (
235     char                    *Filename,
236     ACPI_TABLE_HEADER       *Table)
237 {
238 
239 
240     if (!AcpiGbl_ParseOpRoot)
241     {
242         return (AE_NOT_EXIST);
243     }
244 
245     if (!AcpiGbl_DmOpt_Listing)
246     {
247         AdCreateTableHeader (Filename, Table);
248     }
249 
250     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
251     MpEmitMappingInfo ();
252 
253     if (AcpiGbl_DmOpt_Listing)
254     {
255         AcpiOsPrintf ("\n\nTable Header:\n");
256         AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
257             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
258 
259         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
260         AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
261             Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
262     }
263 
264     return (AE_OK);
265 }
266 
267 
268 /*******************************************************************************
269  *
270  * FUNCTION:    AdStoreTable
271  *
272  * PARAMETERS:  Table               - Table header
273  *              TableIndex          - Where the table index is returned
274  *
275  * RETURN:      Status and table index.
276  *
277  * DESCRIPTION: Add an ACPI table to the global table list
278  *
279  ******************************************************************************/
280 
281 static ACPI_STATUS
282 AdStoreTable (
283     ACPI_TABLE_HEADER       *Table,
284     UINT32                  *TableIndex)
285 {
286     ACPI_STATUS             Status;
287     ACPI_TABLE_DESC         *TableDesc;
288 
289 
290     Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
291     if (ACPI_FAILURE (Status))
292     {
293         return (Status);
294     }
295 
296     /* Initialize added table */
297 
298     AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
299         ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
300     Status = AcpiTbValidateTable (TableDesc);
301     return (Status);
302 }
303 
304 
305 /******************************************************************************
306  *
307  * FUNCTION:    AdGetLocalTables
308  *
309  * PARAMETERS:  None
310  *
311  * RETURN:      Status
312  *
313  * DESCRIPTION: Get the ACPI tables from either memory or a file
314  *
315  *****************************************************************************/
316 
317 ACPI_STATUS
318 AdGetLocalTables (
319     void)
320 {
321     ACPI_STATUS             Status;
322     ACPI_TABLE_HEADER       TableHeader;
323     ACPI_TABLE_HEADER       *NewTable;
324     UINT32                  TableIndex;
325 
326 
327     /* Get the DSDT via table override */
328 
329     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
330     AcpiOsTableOverride (&TableHeader, &NewTable);
331     if (!NewTable)
332     {
333         fprintf (stderr, "Could not obtain DSDT\n");
334         return (AE_NO_ACPI_TABLES);
335     }
336 
337     AdWriteTable (NewTable, NewTable->Length,
338         ACPI_SIG_DSDT, NewTable->OemTableId);
339 
340     /* Store DSDT in the Table Manager */
341 
342     Status = AdStoreTable (NewTable, &TableIndex);
343     if (ACPI_FAILURE (Status))
344     {
345         fprintf (stderr, "Could not store DSDT\n");
346         return (AE_NO_ACPI_TABLES);
347     }
348 
349     return (AE_OK);
350 }
351 
352 
353 /******************************************************************************
354  *
355  * FUNCTION:    AdParseTable
356  *
357  * PARAMETERS:  Table               - Pointer to the raw table
358  *              OwnerId             - Returned OwnerId of the table
359  *              LoadTable           - If add table to the global table list
360  *              External            - If this is an external table
361  *
362  * RETURN:      Status
363  *
364  * DESCRIPTION: Parse an ACPI AML table
365  *
366  *****************************************************************************/
367 
368 ACPI_STATUS
369 AdParseTable (
370     ACPI_TABLE_HEADER       *Table,
371     ACPI_OWNER_ID           *OwnerId,
372     BOOLEAN                 LoadTable,
373     BOOLEAN                 External)
374 {
375     ACPI_STATUS             Status = AE_OK;
376     ACPI_WALK_STATE         *WalkState;
377     UINT8                   *AmlStart;
378     UINT32                  AmlLength;
379     UINT32                  TableIndex;
380 
381 
382     if (!Table)
383     {
384         return (AE_NOT_EXIST);
385     }
386 
387     /* Pass 1:  Parse everything except control method bodies */
388 
389     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
390 
391     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
392     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
393 
394     /* Create the root object */
395 
396     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
397     if (!AcpiGbl_ParseOpRoot)
398     {
399         return (AE_NO_MEMORY);
400     }
401 
402     /* Create and initialize a new walk state */
403 
404     WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);
405     if (!WalkState)
406     {
407         return (AE_NO_MEMORY);
408     }
409 
410     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
411         NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
412     if (ACPI_FAILURE (Status))
413     {
414         return (Status);
415     }
416 
417     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
418     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
419 
420     Status = AcpiPsParseAml (WalkState);
421     if (ACPI_FAILURE (Status))
422     {
423         return (Status);
424     }
425 
426     /* If LoadTable is FALSE, we are parsing the last loaded table */
427 
428     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
429 
430     /* Pass 2 */
431 
432     if (LoadTable)
433     {
434         Status = AdStoreTable (Table, &TableIndex);
435         if (ACPI_FAILURE (Status))
436         {
437             return (Status);
438         }
439         Status = AcpiTbAllocateOwnerId (TableIndex);
440         if (ACPI_FAILURE (Status))
441         {
442             return (Status);
443         }
444         if (OwnerId)
445         {
446             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
447             if (ACPI_FAILURE (Status))
448             {
449                 return (Status);
450             }
451         }
452     }
453 
454     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
455 
456     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
457     if (ACPI_FAILURE (Status))
458     {
459         return (Status);
460     }
461 
462     /* No need to parse control methods of external table */
463 
464     if (External)
465     {
466         return (AE_OK);
467     }
468 
469     /*
470      * Pass 3: Parse control methods and link their parse trees
471      * into the main parse tree
472      */
473     fprintf (stderr,
474         "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
475 
476     Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
477     fprintf (stderr, "\n");
478 
479     /* Process Resource Templates */
480 
481     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
482 
483     fprintf (stderr, "Parsing completed\n");
484     return (AE_OK);
485 }
486