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 "aslcompiler.h"
45 #include "acapps.h"
46 #include "acdispat.h"
47 #include "acnamesp.h"
48 #include "actables.h"
49 #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     char                    *NewFilename;
145     UINT8                   Checksum;
146 
147 
148     /*
149      * Print file header and dump original table header
150      */
151     AdDisassemblerHeader (Filename, ACPI_IS_AML_TABLE);
152 
153     AcpiOsPrintf (" * Original Table Header:\n");
154     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
155     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
156 
157     /* Print and validate the revision */
158 
159     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
160 
161     switch (Table->Revision)
162     {
163     case 0:
164 
165         AcpiOsPrintf (" **** Invalid Revision");
166         break;
167 
168     case 1:
169 
170         /* Revision of DSDT controls the ACPI integer width */
171 
172         if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
173         {
174             AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
175         }
176         break;
177 
178     default:
179 
180         break;
181     }
182     AcpiOsPrintf ("\n");
183 
184     /* Print and validate the table checksum */
185 
186     AcpiOsPrintf (" *     Checksum         0x%2.2X",        Table->Checksum);
187 
188     Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
189     if (Checksum)
190     {
191         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
192             (UINT8) (Table->Checksum - Checksum));
193     }
194 
195     AcpiOsPrintf ("\n");
196     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
197     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
198     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
199     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
200     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
201     AcpiOsPrintf (" */\n");
202 
203     /* Create AML output filename based on input filename */
204 
205     if (Filename)
206     {
207         NewFilename = FlGenerateFilename (Filename, "aml");
208     }
209     else
210     {
211         NewFilename = UtStringCacheCalloc (9);
212         if (NewFilename)
213         {
214             strncat (NewFilename, Table->Signature, 4);
215             strcat (NewFilename, ".aml");
216         }
217     }
218 
219     if (!NewFilename)
220     {
221         AcpiOsPrintf (" **** Could not generate AML output filename\n");
222         return;
223     }
224 
225     /* Open the ASL definition block */
226 
227     AcpiOsPrintf (
228         "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
229         NewFilename, Table->Signature, Table->Revision,
230         Table->OemId, Table->OemTableId, Table->OemRevision);
231 }
232 
233 
234 /******************************************************************************
235  *
236  * FUNCTION:    AdDisplayTables
237  *
238  * PARAMETERS:  Filename            - Input file for the table
239  *              Table               - Pointer to the raw table
240  *
241  * RETURN:      Status
242  *
243  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
244  *
245  *****************************************************************************/
246 
247 ACPI_STATUS
248 AdDisplayTables (
249     char                    *Filename,
250     ACPI_TABLE_HEADER       *Table)
251 {
252 
253 
254     if (!AcpiGbl_ParseOpRoot)
255     {
256         return (AE_NOT_EXIST);
257     }
258 
259     if (!AcpiGbl_DmOpt_Listing)
260     {
261         AdCreateTableHeader (Filename, Table);
262     }
263 
264     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
265     MpEmitMappingInfo ();
266 
267     if (AcpiGbl_DmOpt_Listing)
268     {
269         AcpiOsPrintf ("\n\nTable Header:\n");
270         AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
271             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
272 
273         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
274         AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
275             Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
276     }
277 
278     return (AE_OK);
279 }
280 
281 
282 /*******************************************************************************
283  *
284  * FUNCTION:    AdStoreTable
285  *
286  * PARAMETERS:  Table               - Table header
287  *              TableIndex          - Where the table index is returned
288  *
289  * RETURN:      Status and table index.
290  *
291  * DESCRIPTION: Add an ACPI table to the global table list
292  *
293  ******************************************************************************/
294 
295 static ACPI_STATUS
296 AdStoreTable (
297     ACPI_TABLE_HEADER       *Table,
298     UINT32                  *TableIndex)
299 {
300     ACPI_STATUS             Status;
301     ACPI_TABLE_DESC         *TableDesc;
302 
303 
304     Status = AcpiTbGetNextTableDescriptor (TableIndex, &TableDesc);
305     if (ACPI_FAILURE (Status))
306     {
307         return (Status);
308     }
309 
310     /* Initialize added table */
311 
312     AcpiTbInitTableDescriptor (TableDesc, ACPI_PTR_TO_PHYSADDR (Table),
313         ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL, Table);
314     Status = AcpiTbValidateTable (TableDesc);
315     return (Status);
316 }
317 
318 
319 /******************************************************************************
320  *
321  * FUNCTION:    AdGetLocalTables
322  *
323  * PARAMETERS:  None
324  *
325  * RETURN:      Status
326  *
327  * DESCRIPTION: Get the ACPI tables from either memory or a file
328  *
329  *****************************************************************************/
330 
331 ACPI_STATUS
332 AdGetLocalTables (
333     void)
334 {
335     ACPI_STATUS             Status;
336     ACPI_TABLE_HEADER       TableHeader;
337     ACPI_TABLE_HEADER       *NewTable;
338     UINT32                  TableIndex;
339 
340 
341     /* Get the DSDT via table override */
342 
343     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
344     AcpiOsTableOverride (&TableHeader, &NewTable);
345     if (!NewTable)
346     {
347         fprintf (stderr, "Could not obtain DSDT\n");
348         return (AE_NO_ACPI_TABLES);
349     }
350 
351     AdWriteTable (NewTable, NewTable->Length,
352         ACPI_SIG_DSDT, NewTable->OemTableId);
353 
354     /* Store DSDT in the Table Manager */
355 
356     Status = AdStoreTable (NewTable, &TableIndex);
357     if (ACPI_FAILURE (Status))
358     {
359         fprintf (stderr, "Could not store DSDT\n");
360         return (AE_NO_ACPI_TABLES);
361     }
362 
363     return (AE_OK);
364 }
365 
366 
367 /******************************************************************************
368  *
369  * FUNCTION:    AdParseTable
370  *
371  * PARAMETERS:  Table               - Pointer to the raw table
372  *              OwnerId             - Returned OwnerId of the table
373  *              LoadTable           - If add table to the global table list
374  *              External            - If this is an external table
375  *
376  * RETURN:      Status
377  *
378  * DESCRIPTION: Parse an ACPI AML table
379  *
380  *****************************************************************************/
381 
382 ACPI_STATUS
383 AdParseTable (
384     ACPI_TABLE_HEADER       *Table,
385     ACPI_OWNER_ID           *OwnerId,
386     BOOLEAN                 LoadTable,
387     BOOLEAN                 External)
388 {
389     ACPI_STATUS             Status = AE_OK;
390     ACPI_WALK_STATE         *WalkState;
391     UINT8                   *AmlStart;
392     UINT32                  AmlLength;
393     UINT32                  TableIndex;
394 
395 
396     if (!Table)
397     {
398         return (AE_NOT_EXIST);
399     }
400 
401     /* Pass 1:  Parse everything except control method bodies */
402 
403     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
404 
405     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
406     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
407 
408     /* Create the root object */
409 
410     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp (AmlStart);
411     if (!AcpiGbl_ParseOpRoot)
412     {
413         return (AE_NO_MEMORY);
414     }
415 
416     /* Create and initialize a new walk state */
417 
418     WalkState = AcpiDsCreateWalkState (0, AcpiGbl_ParseOpRoot, NULL, NULL);
419     if (!WalkState)
420     {
421         return (AE_NO_MEMORY);
422     }
423 
424     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
425         NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
426     if (ACPI_FAILURE (Status))
427     {
428         return (Status);
429     }
430 
431     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
432     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
433 
434     Status = AcpiPsParseAml (WalkState);
435     if (ACPI_FAILURE (Status))
436     {
437         return (Status);
438     }
439 
440     /* If LoadTable is FALSE, we are parsing the last loaded table */
441 
442     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
443 
444     /* Pass 2 */
445 
446     if (LoadTable)
447     {
448         Status = AdStoreTable (Table, &TableIndex);
449         if (ACPI_FAILURE (Status))
450         {
451             return (Status);
452         }
453         Status = AcpiTbAllocateOwnerId (TableIndex);
454         if (ACPI_FAILURE (Status))
455         {
456             return (Status);
457         }
458         if (OwnerId)
459         {
460             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
461             if (ACPI_FAILURE (Status))
462             {
463                 return (Status);
464             }
465         }
466     }
467 
468     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
469 
470     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
471     if (ACPI_FAILURE (Status))
472     {
473         return (Status);
474     }
475 
476     /* No need to parse control methods of external table */
477 
478     if (External)
479     {
480         return (AE_OK);
481     }
482 
483     /*
484      * Pass 3: Parse control methods and link their parse trees
485      * into the main parse tree
486      */
487     fprintf (stderr,
488         "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
489 
490     Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
491     fprintf (stderr, "\n");
492 
493     /* Process Resource Templates */
494 
495     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
496 
497     fprintf (stderr, "Parsing completed\n");
498     return (AE_OK);
499 }
500