Sync ACPICA with Intel's version 20150515.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / dtcompile.c
1 /******************************************************************************
2  *
3  * Module Name: dtcompile.c - Front-end for data table compiler
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, 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 #define _DECLARE_DT_GLOBALS
45
46 #include "aslcompiler.h"
47 #include "dtcompiler.h"
48
49 #define _COMPONENT          DT_COMPILER
50         ACPI_MODULE_NAME    ("dtcompile")
51
52 static char                 VersionString[9];
53
54
55 /* Local prototypes */
56
57 static ACPI_STATUS
58 DtInitialize (
59     void);
60
61 static ACPI_STATUS
62 DtCompileDataTable (
63     DT_FIELD                **Field);
64
65 static void
66 DtInsertCompilerIds (
67     DT_FIELD                *FieldList);
68
69
70 /******************************************************************************
71  *
72  * FUNCTION:    DtDoCompile
73  *
74  * PARAMETERS:  None
75  *
76  * RETURN:      Status
77  *
78  * DESCRIPTION: Main entry point for the data table compiler.
79  *
80  * Note: Assumes Gbl_Files[ASL_FILE_INPUT] is initialized and the file is
81  *          open at seek offset zero.
82  *
83  *****************************************************************************/
84
85 ACPI_STATUS
86 DtDoCompile (
87     void)
88 {
89     ACPI_STATUS             Status;
90     UINT8                   Event;
91     DT_FIELD                *FieldList;
92
93
94     /* Initialize globals */
95
96     Status = DtInitialize ();
97     if (ACPI_FAILURE (Status))
98     {
99         printf ("Error during compiler initialization, 0x%X\n", Status);
100         return (Status);
101     }
102
103     /* Preprocessor */
104
105     if (Gbl_PreprocessFlag)
106     {
107         /* Preprocessor */
108
109         Event = UtBeginEvent ("Preprocess input file");
110         PrDoPreprocess ();
111         UtEndEvent (Event);
112
113         if (Gbl_PreprocessOnly)
114         {
115             return (AE_OK);
116         }
117     }
118
119     /*
120      * Scan the input file (file is already open) and
121      * build the parse tree
122      */
123     Event = UtBeginEvent ("Scan and parse input file");
124     FieldList = DtScanFile (Gbl_Files[ASL_FILE_INPUT].Handle);
125     UtEndEvent (Event);
126
127     /* Did the parse tree get successfully constructed? */
128
129     if (!FieldList)
130     {
131         /* TBD: temporary error message. Msgs should come from function above */
132
133         DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
134             "Input file does not appear to be an ASL or data table source file");
135
136         Status = AE_ERROR;
137         goto CleanupAndExit;
138     }
139
140     Event = UtBeginEvent ("Compile parse tree");
141
142     /*
143      * Compile the parse tree
144      */
145     Status = DtCompileDataTable (&FieldList);
146     UtEndEvent (Event);
147
148     if (ACPI_FAILURE (Status))
149     {
150         /* TBD: temporary error message. Msgs should come from function above */
151
152         DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
153             "Could not compile input file");
154
155         goto CleanupAndExit;
156     }
157
158     /* Create/open the binary output file */
159
160     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
161     Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
162     if (ACPI_FAILURE (Status))
163     {
164         goto CleanupAndExit;
165     }
166
167     /* Write the binary, then the optional hex file */
168
169     DtOutputBinary (Gbl_RootTable);
170     HxDoHexOutput ();
171     DtWriteTableToListing ();
172
173 CleanupAndExit:
174
175     AcpiUtDeleteCaches ();
176     DtDeleteCaches ();
177     CmCleanupAndExit ();
178     return (Status);
179 }
180
181
182 /******************************************************************************
183  *
184  * FUNCTION:    DtInitialize
185  *
186  * PARAMETERS:  None
187  *
188  * RETURN:      Status
189  *
190  * DESCRIPTION: Initialize data table compiler globals. Enables multiple
191  *              compiles per invocation.
192  *
193  *****************************************************************************/
194
195 static ACPI_STATUS
196 DtInitialize (
197     void)
198 {
199     ACPI_STATUS             Status;
200
201
202     Status = AcpiOsInitialize ();
203     if (ACPI_FAILURE (Status))
204     {
205         return (Status);
206     }
207
208     Status = AcpiUtInitGlobals ();
209     if (ACPI_FAILURE (Status))
210     {
211         return (Status);
212     }
213
214     Gbl_FieldList = NULL;
215     Gbl_RootTable = NULL;
216     Gbl_SubtableStack = NULL;
217
218     sprintf (VersionString, "%X", (UINT32) ACPI_CA_VERSION);
219     return (AE_OK);
220 }
221
222
223 /******************************************************************************
224  *
225  * FUNCTION:    DtInsertCompilerIds
226  *
227  * PARAMETERS:  FieldList           - Current field list pointer
228  *
229  * RETURN:      None
230  *
231  * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
232  *              the original ACPI table header.
233  *
234  *****************************************************************************/
235
236 static void
237 DtInsertCompilerIds (
238     DT_FIELD                *FieldList)
239 {
240     DT_FIELD                *Next;
241     UINT32                  i;
242
243
244     /*
245      * Don't insert current compiler ID if requested. Used for compiler
246      * debug/validation only.
247      */
248     if (Gbl_UseOriginalCompilerId)
249     {
250         return;
251     }
252
253     /* Walk to the Compiler fields at the end of the header */
254
255     Next = FieldList;
256     for (i = 0; i < 7; i++)
257     {
258         Next = Next->Next;
259     }
260
261     Next->Value = ASL_CREATOR_ID;
262     Next->Flags = DT_FIELD_NOT_ALLOCATED;
263
264     Next = Next->Next;
265     Next->Value = VersionString;
266     Next->Flags = DT_FIELD_NOT_ALLOCATED;
267 }
268
269
270 /******************************************************************************
271  *
272  * FUNCTION:    DtCompileDataTable
273  *
274  * PARAMETERS:  FieldList           - Current field list pointer
275  *
276  * RETURN:      Status
277  *
278  * DESCRIPTION: Entry point to compile one data table
279  *
280  *****************************************************************************/
281
282 static ACPI_STATUS
283 DtCompileDataTable (
284     DT_FIELD                **FieldList)
285 {
286     const ACPI_DMTABLE_DATA *TableData;
287     DT_SUBTABLE             *Subtable;
288     char                    *Signature;
289     ACPI_TABLE_HEADER       *AcpiTableHeader;
290     ACPI_STATUS             Status;
291     DT_FIELD                *RootField = *FieldList;
292
293
294     /* Verify that we at least have a table signature and save it */
295
296     Signature = DtGetFieldValue (*FieldList);
297     if (!Signature)
298     {
299         sprintf (MsgBuffer, "Expected \"%s\"", "Signature");
300         DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
301             *FieldList, MsgBuffer);
302         return (AE_ERROR);
303     }
304
305     Gbl_Signature = UtStringCacheCalloc (ACPI_STRLEN (Signature) + 1);
306     strcpy (Gbl_Signature, Signature);
307
308     /*
309      * Handle tables that don't use the common ACPI table header structure.
310      * Currently, these are the FACS and RSDP. Also check for an OEMx table,
311      * these tables have user-defined contents.
312      */
313     if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
314     {
315         Status = DtCompileFacs (FieldList);
316         if (ACPI_FAILURE (Status))
317         {
318             return (Status);
319         }
320
321         DtSetTableLength ();
322         return (Status);
323     }
324     else if (ACPI_VALIDATE_RSDP_SIG (Signature))
325     {
326         Status = DtCompileRsdp (FieldList);
327         return (Status);
328     }
329     else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_S3PT))
330     {
331         Status = DtCompileS3pt (FieldList);
332         if (ACPI_FAILURE (Status))
333         {
334             return (Status);
335         }
336
337         DtSetTableLength ();
338         return (Status);
339     }
340
341     /*
342      * All other tables must use the common ACPI table header. Insert the
343      * current iASL IDs (name, version), and compile the header now.
344      */
345     DtInsertCompilerIds (*FieldList);
346
347     Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
348                 &Gbl_RootTable, TRUE);
349     if (ACPI_FAILURE (Status))
350     {
351         return (Status);
352     }
353
354     DtPushSubtable (Gbl_RootTable);
355
356     /* Validate the signature via the ACPI table list */
357
358     TableData = AcpiDmGetTableData (Signature);
359     if (!TableData || Gbl_CompileGeneric)
360     {
361         DtCompileGeneric ((void **) FieldList, NULL, NULL);
362         goto FinishHeader;
363     }
364
365     /* Dispatch to per-table compile */
366
367     if (TableData->CmTableHandler)
368     {
369         /* Complex table, has a handler */
370
371         Status = TableData->CmTableHandler ((void **) FieldList);
372         if (ACPI_FAILURE (Status))
373         {
374             return (Status);
375         }
376     }
377     else if (TableData->TableInfo)
378     {
379         /* Simple table, just walk the info table */
380
381         Subtable = NULL;
382         Status = DtCompileTable (FieldList, TableData->TableInfo,
383                     &Subtable, TRUE);
384         if (ACPI_FAILURE (Status))
385         {
386             return (Status);
387         }
388
389         DtInsertSubtable (Gbl_RootTable, Subtable);
390         DtPopSubtable ();
391     }
392     else
393     {
394         DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
395             "Missing table dispatch info");
396         return (AE_ERROR);
397     }
398
399 FinishHeader:
400
401     /* Set the final table length and then the checksum */
402
403     DtSetTableLength ();
404     AcpiTableHeader = ACPI_CAST_PTR (
405         ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
406     DtSetTableChecksum (&AcpiTableHeader->Checksum);
407
408     DtDumpFieldList (RootField);
409     DtDumpSubtableList ();
410     return (AE_OK);
411 }
412
413
414 /******************************************************************************
415  *
416  * FUNCTION:    DtCompileTable
417  *
418  * PARAMETERS:  Field               - Current field list pointer
419  *              Info                - Info table for this ACPI table
420  *              RetSubtable         - Compile result of table
421  *              Required            - If this subtable must exist
422  *
423  * RETURN:      Status
424  *
425  * DESCRIPTION: Compile a subtable
426  *
427  *****************************************************************************/
428
429 ACPI_STATUS
430 DtCompileTable (
431     DT_FIELD                **Field,
432     ACPI_DMTABLE_INFO       *Info,
433     DT_SUBTABLE             **RetSubtable,
434     BOOLEAN                 Required)
435 {
436     DT_FIELD                *LocalField;
437     UINT32                  Length;
438     DT_SUBTABLE             *Subtable;
439     DT_SUBTABLE             *InlineSubtable = NULL;
440     UINT32                  FieldLength = 0;
441     UINT8                   FieldType;
442     UINT8                   *Buffer;
443     UINT8                   *FlagBuffer = NULL;
444     char                    *String;
445     UINT32                  CurrentFlagByteOffset = 0;
446     ACPI_STATUS             Status = AE_OK;
447
448
449     if (!Field || !*Field)
450     {
451         return (AE_BAD_PARAMETER);
452     }
453
454     /* Ignore optional subtable if name does not match */
455
456     if ((Info->Flags & DT_OPTIONAL) &&
457         ACPI_STRCMP ((*Field)->Name, Info->Name))
458     {
459         *RetSubtable = NULL;
460         return (AE_OK);
461     }
462
463     Length = DtGetSubtableLength (*Field, Info);
464     if (Length == ASL_EOF)
465     {
466         return (AE_ERROR);
467     }
468
469     Subtable = UtSubtableCacheCalloc ();
470
471     if (Length > 0)
472     {
473         String = UtStringCacheCalloc (Length);
474         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
475     }
476
477     Subtable->Length = Length;
478     Subtable->TotalLength = Length;
479     Buffer = Subtable->Buffer;
480
481     LocalField = *Field;
482     Subtable->Name = LocalField->Name;
483
484     /*
485      * Main loop walks the info table for this ACPI table or subtable
486      */
487     for (; Info->Name; Info++)
488     {
489         if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
490         {
491             continue;
492         }
493
494         if (!LocalField)
495         {
496             sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
497                 Info->Name);
498             DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
499             Status = AE_BAD_DATA;
500             goto Error;
501         }
502
503         /* Maintain table offsets */
504
505         LocalField->TableOffset = Gbl_CurrentTableOffset;
506         FieldLength = DtGetFieldLength (LocalField, Info);
507         Gbl_CurrentTableOffset += FieldLength;
508
509         FieldType = DtGetFieldType (Info);
510         Gbl_InputFieldCount++;
511
512         switch (FieldType)
513         {
514         case DT_FIELD_TYPE_FLAGS_INTEGER:
515             /*
516              * Start of the definition of a flags field.
517              * This master flags integer starts at value zero, in preparation
518              * to compile and insert the flag fields from the individual bits
519              */
520             LocalField = LocalField->Next;
521             *Field = LocalField;
522
523             FlagBuffer = Buffer;
524             CurrentFlagByteOffset = Info->Offset;
525             break;
526
527         case DT_FIELD_TYPE_FLAG:
528
529             /* Individual Flag field, can be multiple bits */
530
531             if (FlagBuffer)
532             {
533                 /*
534                  * We must increment the FlagBuffer when we have crossed
535                  * into the next flags byte within the flags field
536                  * of type DT_FIELD_TYPE_FLAGS_INTEGER.
537                  */
538                 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
539                 CurrentFlagByteOffset = Info->Offset;
540
541                 DtCompileFlag (FlagBuffer, LocalField, Info);
542             }
543             else
544             {
545                 /* TBD - this is an internal error */
546             }
547
548             LocalField = LocalField->Next;
549             *Field = LocalField;
550             break;
551
552         case DT_FIELD_TYPE_INLINE_SUBTABLE:
553             /*
554              * Recursion (one level max): compile GAS (Generic Address)
555              * or Notify in-line subtable
556              */
557             *Field = LocalField;
558
559             switch (Info->Opcode)
560             {
561             case ACPI_DMT_GAS:
562
563                 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
564                     &InlineSubtable, TRUE);
565                 break;
566
567             case ACPI_DMT_HESTNTFY:
568
569                 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
570                     &InlineSubtable, TRUE);
571                 break;
572
573             case ACPI_DMT_IORTMEM:
574
575                 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
576                     &InlineSubtable, TRUE);
577                 break;
578
579             default:
580                 sprintf (MsgBuffer, "Invalid DMT opcode: 0x%.2X",
581                     Info->Opcode);
582                 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
583                 Status = AE_BAD_DATA;
584                 break;
585             }
586
587             if (ACPI_FAILURE (Status))
588             {
589                 goto Error;
590             }
591
592             DtSetSubtableLength (InlineSubtable);
593
594             ACPI_MEMCPY (Buffer, InlineSubtable->Buffer, FieldLength);
595             LocalField = *Field;
596             break;
597
598         case DT_FIELD_TYPE_LABEL:
599
600             DtWriteFieldToListing (Buffer, LocalField, 0);
601             LocalField = LocalField->Next;
602             break;
603
604         default:
605
606             /* Normal case for most field types (Integer, String, etc.) */
607
608             DtCompileOneField (Buffer, LocalField,
609                 FieldLength, FieldType, Info->Flags);
610
611             DtWriteFieldToListing (Buffer, LocalField, FieldLength);
612             LocalField = LocalField->Next;
613
614             if (Info->Flags & DT_LENGTH)
615             {
616                 /* Field is an Integer that will contain a subtable length */
617
618                 Subtable->LengthField = Buffer;
619                 Subtable->SizeOfLengthField = FieldLength;
620             }
621             break;
622         }
623
624         Buffer += FieldLength;
625     }
626
627     *Field = LocalField;
628     *RetSubtable = Subtable;
629     return (AE_OK);
630
631 Error:
632     ACPI_FREE (Subtable->Buffer);
633     ACPI_FREE (Subtable);
634     return (Status);
635 }
636
637
638 /******************************************************************************
639  *
640  * FUNCTION:    DtCompilePadding
641  *
642  * PARAMETERS:  Length              - Padding field size
643  *              RetSubtable         - Compile result of table
644  *
645  * RETURN:      Status
646  *
647  * DESCRIPTION: Compile a subtable for padding purpose
648  *
649  *****************************************************************************/
650
651 ACPI_STATUS
652 DtCompilePadding (
653     UINT32                  Length,
654     DT_SUBTABLE             **RetSubtable)
655 {
656     DT_SUBTABLE             *Subtable;
657     /* UINT8                   *Buffer; */
658     char                    *String;
659
660
661     Subtable = UtSubtableCacheCalloc ();
662
663     if (Length > 0)
664     {
665         String = UtStringCacheCalloc (Length);
666         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
667     }
668
669     Subtable->Length = Length;
670     Subtable->TotalLength = Length;
671     /* Buffer = Subtable->Buffer; */
672
673     *RetSubtable = Subtable;
674     return (AE_OK);
675 }