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