xref: /freebsd/sys/contrib/dev/acpica/common/adisasm.c (revision 6186fd1857626de0f7cb1a9e4dff19082f9ebb11)
1 /******************************************************************************
2  *
3  * Module Name: adisasm - Application-level disassembler routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, 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/acparser.h>
46 #include <contrib/dev/acpica/include/amlcode.h>
47 #include <contrib/dev/acpica/include/acdisasm.h>
48 #include <contrib/dev/acpica/include/acdispat.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/actables.h>
51 #include <contrib/dev/acpica/include/acapps.h>
52 
53 #include <stdio.h>
54 #include <time.h>
55 
56 
57 #define _COMPONENT          ACPI_TOOLS
58         ACPI_MODULE_NAME    ("adisasm")
59 
60 /* Local prototypes */
61 
62 static void
63 AdCreateTableHeader (
64     char                    *Filename,
65     ACPI_TABLE_HEADER       *Table);
66 
67 /* Stubs for ASL compiler */
68 
69 #ifndef ACPI_ASL_COMPILER
70 BOOLEAN
71 AcpiDsIsResultUsed (
72     ACPI_PARSE_OBJECT       *Op,
73     ACPI_WALK_STATE         *WalkState)
74 {
75     return TRUE;
76 }
77 
78 ACPI_STATUS
79 AcpiDsMethodError (
80     ACPI_STATUS             Status,
81     ACPI_WALK_STATE         *WalkState)
82 {
83     return (Status);
84 }
85 #endif
86 
87 ACPI_STATUS
88 AcpiNsLoadTable (
89     UINT32                  TableIndex,
90     ACPI_NAMESPACE_NODE     *Node)
91 {
92     return (AE_NOT_IMPLEMENTED);
93 }
94 
95 ACPI_STATUS
96 AcpiDsRestartControlMethod (
97     ACPI_WALK_STATE         *WalkState,
98     ACPI_OPERAND_OBJECT     *ReturnDesc)
99 {
100     return (AE_OK);
101 }
102 
103 void
104 AcpiDsTerminateControlMethod (
105     ACPI_OPERAND_OBJECT     *MethodDesc,
106     ACPI_WALK_STATE         *WalkState)
107 {
108     return;
109 }
110 
111 ACPI_STATUS
112 AcpiDsCallControlMethod (
113     ACPI_THREAD_STATE       *Thread,
114     ACPI_WALK_STATE         *WalkState,
115     ACPI_PARSE_OBJECT       *Op)
116 {
117     return (AE_OK);
118 }
119 
120 ACPI_STATUS
121 AcpiDsMethodDataInitArgs (
122     ACPI_OPERAND_OBJECT     **Params,
123     UINT32                  MaxParamCount,
124     ACPI_WALK_STATE         *WalkState)
125 {
126     return (AE_OK);
127 }
128 
129 
130 static ACPI_TABLE_DESC      LocalTables[1];
131 ACPI_PARSE_OBJECT    *AcpiGbl_ParseOpRoot;
132 
133 
134 /*******************************************************************************
135  *
136  * FUNCTION:    AdInitialize
137  *
138  * PARAMETERS:  None
139  *
140  * RETURN:      Status
141  *
142  * DESCRIPTION: ACPICA and local initialization
143  *
144  ******************************************************************************/
145 
146 ACPI_STATUS
147 AdInitialize (
148     void)
149 {
150     ACPI_STATUS             Status;
151 
152 
153     /* ACPICA subsystem initialization */
154 
155     Status = AcpiOsInitialize ();
156     if (ACPI_FAILURE (Status))
157     {
158         return (Status);
159     }
160 
161     Status = AcpiUtInitGlobals ();
162     if (ACPI_FAILURE (Status))
163     {
164         return (Status);
165     }
166 
167     Status = AcpiUtMutexInitialize ();
168     if (ACPI_FAILURE (Status))
169     {
170         return (Status);
171     }
172 
173     Status = AcpiNsRootInitialize ();
174     if (ACPI_FAILURE (Status))
175     {
176         return (Status);
177     }
178 
179     /* Setup the Table Manager (cheat - there is no RSDT) */
180 
181     AcpiGbl_RootTableList.MaxTableCount = 1;
182     AcpiGbl_RootTableList.CurrentTableCount = 0;
183     AcpiGbl_RootTableList.Tables = LocalTables;
184 
185     return (Status);
186 }
187 
188 
189 /******************************************************************************
190  *
191  * FUNCTION:    AdAmlDisassemble
192  *
193  * PARAMETERS:  Filename            - AML input filename
194  *              OutToFile           - TRUE if output should go to a file
195  *              Prefix              - Path prefix for output
196  *              OutFilename         - where the filename is returned
197  *
198  * RETURN:      Status
199  *
200  * DESCRIPTION: Disassemble an entire ACPI table
201  *
202  *****************************************************************************/
203 
204 ACPI_STATUS
205 AdAmlDisassemble (
206     BOOLEAN                 OutToFile,
207     char                    *Filename,
208     char                    *Prefix,
209     char                    **OutFilename)
210 {
211     ACPI_STATUS             Status;
212     char                    *DisasmFilename = NULL;
213     char                    *ExternalFilename;
214     ACPI_EXTERNAL_FILE      *ExternalFileList = AcpiGbl_ExternalFileList;
215     FILE                    *File = NULL;
216     ACPI_TABLE_HEADER       *Table = NULL;
217     ACPI_TABLE_HEADER       *ExternalTable;
218     ACPI_OWNER_ID           OwnerId;
219 
220 
221     /*
222      * Input: AML code from either a file or via GetTables (memory or
223      * registry)
224      */
225     if (Filename)
226     {
227         Status = AcpiDbGetTableFromFile (Filename, &Table);
228         if (ACPI_FAILURE (Status))
229         {
230             return (Status);
231         }
232 
233         /*
234          * External filenames separated by commas
235          * Example: iasl -e file1,file2,file3 -d xxx.aml
236          */
237         while (ExternalFileList)
238         {
239             ExternalFilename = ExternalFileList->Path;
240             if (!ACPI_STRCMP (ExternalFilename, Filename))
241             {
242                 /* Next external file */
243 
244                 ExternalFileList = ExternalFileList->Next;
245                 continue;
246             }
247 
248             Status = AcpiDbGetTableFromFile (ExternalFilename, &ExternalTable);
249             if (ACPI_FAILURE (Status))
250             {
251                 return (Status);
252             }
253 
254             /* Load external table for symbol resolution */
255 
256             if (ExternalTable)
257             {
258                 Status = AdParseTable (ExternalTable, &OwnerId, TRUE, TRUE);
259                 if (ACPI_FAILURE (Status))
260                 {
261                     AcpiOsPrintf ("Could not parse external ACPI tables, %s\n",
262                         AcpiFormatException (Status));
263                     return (Status);
264                 }
265 
266                 /*
267                  * Load namespace from names created within control methods
268                  * Set owner id of nodes in external table
269                  */
270                 AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
271                     AcpiGbl_RootNode, OwnerId);
272                 AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
273             }
274 
275             /* Next external file */
276 
277             ExternalFileList = ExternalFileList->Next;
278         }
279 
280         /* Clear external list generated by Scope in external tables */
281 
282         if (AcpiGbl_ExternalFileList)
283         {
284             AcpiDmClearExternalList ();
285         }
286 
287         /* Load any externals defined in the optional external ref file */
288 
289         AcpiDmGetExternalsFromFile ();
290     }
291     else
292     {
293         Status = AdGetLocalTables ();
294         if (ACPI_FAILURE (Status))
295         {
296             AcpiOsPrintf ("Could not get ACPI tables, %s\n",
297                 AcpiFormatException (Status));
298             return (Status);
299         }
300 
301         if (!AcpiGbl_DbOpt_disasm)
302         {
303             return (AE_OK);
304         }
305 
306         /* Obtained the local tables, just disassemble the DSDT */
307 
308         Status = AcpiGetTable (ACPI_SIG_DSDT, 0, &Table);
309         if (ACPI_FAILURE (Status))
310         {
311             AcpiOsPrintf ("Could not get DSDT, %s\n",
312                 AcpiFormatException (Status));
313             return (Status);
314         }
315 
316         AcpiOsPrintf ("\nDisassembly of DSDT\n");
317         Prefix = AdGenerateFilename ("dsdt", Table->OemTableId);
318     }
319 
320     /*
321      * Output: ASL code. Redirect to a file if requested
322      */
323     if (OutToFile)
324     {
325         /* Create/Open a disassembly output file */
326 
327         DisasmFilename = FlGenerateFilename (Prefix, FILE_SUFFIX_DISASSEMBLY);
328         if (!DisasmFilename)
329         {
330             fprintf (stderr, "Could not generate output filename\n");
331             Status = AE_ERROR;
332             goto Cleanup;
333         }
334 
335         File = fopen (DisasmFilename, "w+");
336         if (!File)
337         {
338             fprintf (stderr, "Could not open output file %s\n", DisasmFilename);
339             Status = AE_ERROR;
340             ACPI_FREE (DisasmFilename);
341             goto Cleanup;
342         }
343 
344         AcpiOsRedirectOutput (File);
345     }
346 
347     *OutFilename = DisasmFilename;
348 
349     if (!AcpiUtIsAmlTable (Table))
350     {
351         AdDisassemblerHeader (Filename);
352         AcpiOsPrintf (" * ACPI Data Table [%4.4s]\n *\n",
353             Table->Signature);
354         AcpiOsPrintf (" * Format: [HexOffset DecimalOffset ByteLength]  "
355             "FieldName : FieldValue\n */\n\n");
356 
357         AcpiDmDumpDataTable (Table);
358         fprintf (stderr, "Acpi Data Table [%4.4s] decoded\n",
359             Table->Signature);
360         fprintf (stderr, "Formatted output:  %s - %u bytes\n",
361             DisasmFilename, CmGetFileSize (File));
362     }
363     else
364     {
365         /* Always parse the tables, only option is what to display */
366 
367         Status = AdParseTable (Table, &OwnerId, TRUE, FALSE);
368         if (ACPI_FAILURE (Status))
369         {
370             AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
371                 AcpiFormatException (Status));
372             goto Cleanup;
373         }
374 
375         if (AslCompilerdebug)
376         {
377             AcpiOsPrintf ("/**** Before second load\n");
378 
379             NsSetupNamespaceListing (File);
380             NsDisplayNamespace ();
381             AcpiOsPrintf ("*****/\n");
382         }
383 
384         /* Load namespace from names created within control methods */
385 
386         AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
387             AcpiGbl_RootNode, OwnerId);
388 
389         /*
390          * Cross reference the namespace here, in order to
391          * generate External() statements
392          */
393         AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
394             AcpiGbl_RootNode, OwnerId);
395 
396         if (AslCompilerdebug)
397         {
398             AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
399         }
400 
401         /* Find possible calls to external control methods */
402 
403         AcpiDmFindOrphanMethods (AcpiGbl_ParseOpRoot);
404 
405         /*
406          * If we found any external control methods, we must reparse
407          * the entire tree with the new information (namely, the
408          * number of arguments per method)
409          */
410         if (AcpiDmGetExternalMethodCount ())
411         {
412             fprintf (stderr,
413                 "\nFound %u external control methods, "
414                 "reparsing with new information\n",
415                 AcpiDmGetExternalMethodCount ());
416 
417             /* Reparse, rebuild namespace */
418 
419             AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
420             AcpiGbl_ParseOpRoot = NULL;
421             AcpiNsDeleteNamespaceSubtree (AcpiGbl_RootNode);
422 
423             AcpiGbl_RootNode                    = NULL;
424             AcpiGbl_RootNodeStruct.Name.Integer = ACPI_ROOT_NAME;
425             AcpiGbl_RootNodeStruct.DescriptorType = ACPI_DESC_TYPE_NAMED;
426             AcpiGbl_RootNodeStruct.Type         = ACPI_TYPE_DEVICE;
427             AcpiGbl_RootNodeStruct.Parent       = NULL;
428             AcpiGbl_RootNodeStruct.Child        = NULL;
429             AcpiGbl_RootNodeStruct.Peer         = NULL;
430             AcpiGbl_RootNodeStruct.Object       = NULL;
431             AcpiGbl_RootNodeStruct.Flags        = 0;
432 
433             Status = AcpiNsRootInitialize ();
434 
435             /* New namespace, add the external definitions first */
436 
437             AcpiDmAddExternalsToNamespace ();
438 
439             /* Parse the table again. No need to reload it, however */
440 
441             Status = AdParseTable (Table, NULL, FALSE, FALSE);
442             if (ACPI_FAILURE (Status))
443             {
444                 AcpiOsPrintf ("Could not parse ACPI tables, %s\n",
445                     AcpiFormatException (Status));
446                 goto Cleanup;
447             }
448 
449             /* Cross reference the namespace again */
450 
451             AcpiDmFinishNamespaceLoad (AcpiGbl_ParseOpRoot,
452                 AcpiGbl_RootNode, OwnerId);
453 
454             AcpiDmCrossReferenceNamespace (AcpiGbl_ParseOpRoot,
455                 AcpiGbl_RootNode, OwnerId);
456 
457             if (AslCompilerdebug)
458             {
459                 AcpiOsPrintf ("/**** After second load and resource conversion\n");
460                 NsSetupNamespaceListing (File);
461                 NsDisplayNamespace ();
462                 AcpiOsPrintf ("*****/\n");
463 
464                 AcpiDmDumpTree (AcpiGbl_ParseOpRoot);
465             }
466         }
467 
468         /*
469          * Now that the namespace is finalized, we can perform namespace
470          * transforms.
471          *
472          * 1) Convert fixed-offset references to resource descriptors
473          *    to symbolic references (Note: modifies namespace)
474          */
475         AcpiDmConvertResourceIndexes (AcpiGbl_ParseOpRoot, AcpiGbl_RootNode);
476 
477         /* Optional displays */
478 
479         if (AcpiGbl_DbOpt_disasm)
480         {
481             /* This is the real disassembly */
482 
483             AdDisplayTables (Filename, Table);
484 
485             /* Dump hex table if requested (-vt) */
486 
487             AcpiDmDumpDataTable (Table);
488 
489             fprintf (stderr, "Disassembly completed\n");
490             fprintf (stderr, "ASL Output:    %s - %u bytes\n",
491                 DisasmFilename, CmGetFileSize (File));
492 
493             if (Gbl_MapfileFlag)
494             {
495                 fprintf (stderr, "%14s %s - %u bytes\n",
496                     Gbl_Files[ASL_FILE_MAP_OUTPUT].ShortDescription,
497                     Gbl_Files[ASL_FILE_MAP_OUTPUT].Filename,
498                     FlGetFileSize (ASL_FILE_MAP_OUTPUT));
499             }
500         }
501     }
502 
503 Cleanup:
504 
505     if (Table && !AcpiUtIsAmlTable (Table))
506     {
507         ACPI_FREE (Table);
508     }
509 
510     if (OutToFile && File)
511     {
512         if (AslCompilerdebug) /* Display final namespace, with transforms */
513         {
514             NsSetupNamespaceListing (File);
515             NsDisplayNamespace ();
516         }
517 
518         fclose (File);
519         AcpiOsRedirectOutput (stdout);
520     }
521 
522     AcpiPsDeleteParseTree (AcpiGbl_ParseOpRoot);
523     AcpiGbl_ParseOpRoot = NULL;
524     return (Status);
525 }
526 
527 
528 /******************************************************************************
529  *
530  * FUNCTION:    AdDisassemblerHeader
531  *
532  * PARAMETERS:  Filename            - Input file for the table
533  *
534  * RETURN:      None
535  *
536  * DESCRIPTION: Create the disassembler header, including ACPICA signon with
537  *              current time and date.
538  *
539  *****************************************************************************/
540 
541 void
542 AdDisassemblerHeader (
543     char                    *Filename)
544 {
545     time_t                  Timer;
546 
547     time (&Timer);
548 
549     /* Header and input table info */
550 
551     AcpiOsPrintf ("/*\n");
552     AcpiOsPrintf (ACPI_COMMON_HEADER ("AML Disassembler", " * "));
553 
554     AcpiOsPrintf (" * Disassembly of %s, %s", Filename, ctime (&Timer));
555     AcpiOsPrintf (" *\n");
556 }
557 
558 
559 /******************************************************************************
560  *
561  * FUNCTION:    AdCreateTableHeader
562  *
563  * PARAMETERS:  Filename            - Input file for the table
564  *              Table               - Pointer to the raw table
565  *
566  * RETURN:      None
567  *
568  * DESCRIPTION: Create the ASL table header, including ACPICA signon with
569  *              current time and date.
570  *
571  *****************************************************************************/
572 
573 static void
574 AdCreateTableHeader (
575     char                    *Filename,
576     ACPI_TABLE_HEADER       *Table)
577 {
578     char                    *NewFilename;
579     UINT8                   Checksum;
580 
581 
582     /*
583      * Print file header and dump original table header
584      */
585     AdDisassemblerHeader (Filename);
586 
587     AcpiOsPrintf (" * Original Table Header:\n");
588     AcpiOsPrintf (" *     Signature        \"%4.4s\"\n",    Table->Signature);
589     AcpiOsPrintf (" *     Length           0x%8.8X (%u)\n", Table->Length, Table->Length);
590 
591     /* Print and validate the revision */
592 
593     AcpiOsPrintf (" *     Revision         0x%2.2X",      Table->Revision);
594 
595     switch (Table->Revision)
596     {
597     case 0:
598 
599         AcpiOsPrintf (" **** Invalid Revision");
600         break;
601 
602     case 1:
603 
604         /* Revision of DSDT controls the ACPI integer width */
605 
606         if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT))
607         {
608             AcpiOsPrintf (" **** 32-bit table (V1), no 64-bit math support");
609         }
610         break;
611 
612     default:
613 
614         break;
615     }
616     AcpiOsPrintf ("\n");
617 
618     /* Print and validate the table checksum */
619 
620     AcpiOsPrintf (" *     Checksum         0x%2.2X",        Table->Checksum);
621 
622     Checksum = AcpiTbChecksum (ACPI_CAST_PTR (UINT8, Table), Table->Length);
623     if (Checksum)
624     {
625         AcpiOsPrintf (" **** Incorrect checksum, should be 0x%2.2X",
626             (UINT8) (Table->Checksum - Checksum));
627     }
628     AcpiOsPrintf ("\n");
629 
630     AcpiOsPrintf (" *     OEM ID           \"%.6s\"\n",     Table->OemId);
631     AcpiOsPrintf (" *     OEM Table ID     \"%.8s\"\n",     Table->OemTableId);
632     AcpiOsPrintf (" *     OEM Revision     0x%8.8X (%u)\n", Table->OemRevision, Table->OemRevision);
633     AcpiOsPrintf (" *     Compiler ID      \"%.4s\"\n",     Table->AslCompilerId);
634     AcpiOsPrintf (" *     Compiler Version 0x%8.8X (%u)\n", Table->AslCompilerRevision, Table->AslCompilerRevision);
635     AcpiOsPrintf (" */\n");
636 
637     /* Create AML output filename based on input filename */
638 
639     if (Filename)
640     {
641         NewFilename = FlGenerateFilename (Filename, "aml");
642     }
643     else
644     {
645         NewFilename = UtStringCacheCalloc (9);
646         if (NewFilename)
647         {
648             strncat (NewFilename, Table->Signature, 4);
649             strcat (NewFilename, ".aml");
650         }
651     }
652 
653     if (!NewFilename)
654     {
655         AcpiOsPrintf (" **** Could not generate AML output filename\n");
656         return;
657     }
658 
659     /* Open the ASL definition block */
660 
661     AcpiOsPrintf (
662         "DefinitionBlock (\"%s\", \"%4.4s\", %hu, \"%.6s\", \"%.8s\", 0x%8.8X)\n",
663         NewFilename, Table->Signature, Table->Revision,
664         Table->OemId, Table->OemTableId, Table->OemRevision);
665 }
666 
667 
668 /******************************************************************************
669  *
670  * FUNCTION:    AdDisplayTables
671  *
672  * PARAMETERS:  Filename            - Input file for the table
673  *              Table               - Pointer to the raw table
674  *
675  * RETURN:      Status
676  *
677  * DESCRIPTION: Display (disassemble) loaded tables and dump raw tables
678  *
679  *****************************************************************************/
680 
681 ACPI_STATUS
682 AdDisplayTables (
683     char                    *Filename,
684     ACPI_TABLE_HEADER       *Table)
685 {
686 
687 
688     if (!AcpiGbl_ParseOpRoot)
689     {
690         return (AE_NOT_EXIST);
691     }
692 
693     if (!AcpiGbl_DbOpt_verbose)
694     {
695         AdCreateTableHeader (Filename, Table);
696     }
697 
698     AcpiDmDisassemble (NULL, AcpiGbl_ParseOpRoot, ACPI_UINT32_MAX);
699     MpEmitMappingInfo ();
700 
701     if (AcpiGbl_DbOpt_verbose)
702     {
703         AcpiOsPrintf ("\n\nTable Header:\n");
704         AcpiUtDebugDumpBuffer ((UINT8 *) Table, sizeof (ACPI_TABLE_HEADER),
705             DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
706 
707         AcpiOsPrintf ("Table Body (Length 0x%X)\n", Table->Length);
708         AcpiUtDebugDumpBuffer (((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER)),
709             Table->Length, DB_BYTE_DISPLAY, ACPI_UINT32_MAX);
710     }
711 
712     return (AE_OK);
713 }
714 
715 
716 /******************************************************************************
717  *
718  * FUNCTION:    AdGetLocalTables
719  *
720  * PARAMETERS:  None
721  *
722  * RETURN:      Status
723  *
724  * DESCRIPTION: Get the ACPI tables from either memory or a file
725  *
726  *****************************************************************************/
727 
728 ACPI_STATUS
729 AdGetLocalTables (
730     void)
731 {
732     ACPI_STATUS             Status;
733     ACPI_TABLE_HEADER       TableHeader;
734     ACPI_TABLE_HEADER       *NewTable;
735     UINT32                  TableIndex;
736 
737 
738     /* Get the DSDT via table override */
739 
740     ACPI_MOVE_32_TO_32 (TableHeader.Signature, ACPI_SIG_DSDT);
741     AcpiOsTableOverride (&TableHeader, &NewTable);
742     if (!NewTable)
743     {
744         fprintf (stderr, "Could not obtain DSDT\n");
745         return (AE_NO_ACPI_TABLES);
746     }
747 
748     AdWriteTable (NewTable, NewTable->Length,
749         ACPI_SIG_DSDT, NewTable->OemTableId);
750 
751     /* Store DSDT in the Table Manager */
752 
753     Status = AcpiTbStoreTable (0, NewTable, NewTable->Length,
754                 0, &TableIndex);
755     if (ACPI_FAILURE (Status))
756     {
757         fprintf (stderr, "Could not store DSDT\n");
758         return (AE_NO_ACPI_TABLES);
759     }
760 
761     return (AE_OK);
762 }
763 
764 
765 /******************************************************************************
766  *
767  * FUNCTION:    AdParseTable
768  *
769  * PARAMETERS:  Table               - Pointer to the raw table
770  *              OwnerId             - Returned OwnerId of the table
771  *              LoadTable           - If add table to the global table list
772  *              External            - If this is an external table
773  *
774  * RETURN:      Status
775  *
776  * DESCRIPTION: Parse the DSDT.
777  *
778  *****************************************************************************/
779 
780 ACPI_STATUS
781 AdParseTable (
782     ACPI_TABLE_HEADER       *Table,
783     ACPI_OWNER_ID           *OwnerId,
784     BOOLEAN                 LoadTable,
785     BOOLEAN                 External)
786 {
787     ACPI_STATUS             Status = AE_OK;
788     ACPI_WALK_STATE         *WalkState;
789     UINT8                   *AmlStart;
790     UINT32                  AmlLength;
791     UINT32                  TableIndex;
792 
793 
794     if (!Table)
795     {
796         return (AE_NOT_EXIST);
797     }
798 
799     /* Pass 1:  Parse everything except control method bodies */
800 
801     fprintf (stderr, "Pass 1 parse of [%4.4s]\n", (char *) Table->Signature);
802 
803     AmlLength = Table->Length - sizeof (ACPI_TABLE_HEADER);
804     AmlStart = ((UINT8 *) Table + sizeof (ACPI_TABLE_HEADER));
805 
806     /* Create the root object */
807 
808     AcpiGbl_ParseOpRoot = AcpiPsCreateScopeOp ();
809     if (!AcpiGbl_ParseOpRoot)
810     {
811         return (AE_NO_MEMORY);
812     }
813 
814     /* Create and initialize a new walk state */
815 
816     WalkState = AcpiDsCreateWalkState (0,
817                         AcpiGbl_ParseOpRoot, NULL, NULL);
818     if (!WalkState)
819     {
820         return (AE_NO_MEMORY);
821     }
822 
823     Status = AcpiDsInitAmlWalk (WalkState, AcpiGbl_ParseOpRoot,
824                 NULL, AmlStart, AmlLength, NULL, ACPI_IMODE_LOAD_PASS1);
825     if (ACPI_FAILURE (Status))
826     {
827         return (Status);
828     }
829 
830     WalkState->ParseFlags &= ~ACPI_PARSE_DELETE_TREE;
831     WalkState->ParseFlags |= ACPI_PARSE_DISASSEMBLE;
832 
833     Status = AcpiPsParseAml (WalkState);
834     if (ACPI_FAILURE (Status))
835     {
836         return (Status);
837     }
838 
839     /* If LoadTable is FALSE, we are parsing the last loaded table */
840 
841     TableIndex = AcpiGbl_RootTableList.CurrentTableCount - 1;
842 
843     /* Pass 2 */
844 
845     if (LoadTable)
846     {
847         Status = AcpiTbStoreTable ((ACPI_PHYSICAL_ADDRESS) Table, Table,
848                     Table->Length, ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
849                     &TableIndex);
850         if (ACPI_FAILURE (Status))
851         {
852             return (Status);
853         }
854         Status = AcpiTbAllocateOwnerId (TableIndex);
855         if (ACPI_FAILURE (Status))
856         {
857             return (Status);
858         }
859         if (OwnerId)
860         {
861             Status = AcpiTbGetOwnerId (TableIndex, OwnerId);
862             if (ACPI_FAILURE (Status))
863             {
864                 return (Status);
865             }
866         }
867     }
868 
869     fprintf (stderr, "Pass 2 parse of [%4.4s]\n", (char *) Table->Signature);
870 
871     Status = AcpiNsOneCompleteParse (ACPI_IMODE_LOAD_PASS2, TableIndex, NULL);
872     if (ACPI_FAILURE (Status))
873     {
874         return (Status);
875     }
876 
877     /* No need to parse control methods of external table */
878 
879     if (External)
880     {
881         return (AE_OK);
882     }
883 
884     /*
885      * Pass 3: Parse control methods and link their parse trees
886      * into the main parse tree
887      */
888     fprintf (stderr,
889         "Parsing Deferred Opcodes (Methods/Buffers/Packages/Regions)\n");
890     Status = AcpiDmParseDeferredOps (AcpiGbl_ParseOpRoot);
891     fprintf (stderr, "\n");
892 
893     /* Process Resource Templates */
894 
895     AcpiDmFindResources (AcpiGbl_ParseOpRoot);
896 
897     fprintf (stderr, "Parsing completed\n");
898     return (AE_OK);
899 }
900