xref: /freebsd/sys/contrib/dev/acpica/compiler/dtcompile.c (revision 86c9d9918f1db7cdd968b60f8902466887bcd9e9)
1 /******************************************************************************
2  *
3  * Module Name: dtcompile.c - Front-end for data table compiler
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 #define _DECLARE_DT_GLOBALS
45 
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include <contrib/dev/acpica/compiler/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 (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         /* Unknown table signature and/or force generic compile */
362 
363         DtCompileGeneric ((void **) FieldList, NULL, NULL);
364         goto FinishHeader;
365     }
366 
367     /* Dispatch to per-table compile */
368 
369     if (TableData->CmTableHandler)
370     {
371         /* Complex table, has a handler */
372 
373         Status = TableData->CmTableHandler ((void **) FieldList);
374         if (ACPI_FAILURE (Status))
375         {
376             return (Status);
377         }
378     }
379     else if (TableData->TableInfo)
380     {
381         /* Simple table, just walk the info table */
382 
383         Subtable = NULL;
384         Status = DtCompileTable (FieldList, TableData->TableInfo,
385             &Subtable, TRUE);
386         if (ACPI_FAILURE (Status))
387         {
388             return (Status);
389         }
390 
391         DtInsertSubtable (Gbl_RootTable, Subtable);
392         DtPopSubtable ();
393     }
394     else
395     {
396         DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
397             "Missing table dispatch info");
398         return (AE_ERROR);
399     }
400 
401 FinishHeader:
402 
403     /* Set the final table length and then the checksum */
404 
405     DtSetTableLength ();
406     AcpiTableHeader = ACPI_CAST_PTR (
407         ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
408     DtSetTableChecksum (&AcpiTableHeader->Checksum);
409 
410     DtDumpFieldList (RootField);
411     DtDumpSubtableList ();
412     return (AE_OK);
413 }
414 
415 
416 /******************************************************************************
417  *
418  * FUNCTION:    DtCompileTable
419  *
420  * PARAMETERS:  Field               - Current field list pointer
421  *              Info                - Info table for this ACPI table
422  *              RetSubtable         - Compile result of table
423  *              Required            - If this subtable must exist
424  *
425  * RETURN:      Status
426  *
427  * DESCRIPTION: Compile a subtable
428  *
429  *****************************************************************************/
430 
431 ACPI_STATUS
432 DtCompileTable (
433     DT_FIELD                **Field,
434     ACPI_DMTABLE_INFO       *Info,
435     DT_SUBTABLE             **RetSubtable,
436     BOOLEAN                 Required)
437 {
438     DT_FIELD                *LocalField;
439     UINT32                  Length;
440     DT_SUBTABLE             *Subtable;
441     DT_SUBTABLE             *InlineSubtable = NULL;
442     UINT32                  FieldLength = 0;
443     UINT8                   FieldType;
444     UINT8                   *Buffer;
445     UINT8                   *FlagBuffer = NULL;
446     char                    *String;
447     UINT32                  CurrentFlagByteOffset = 0;
448     ACPI_STATUS             Status = AE_OK;
449 
450 
451     if (!Field || !*Field)
452     {
453         return (AE_BAD_PARAMETER);
454     }
455 
456     /* Ignore optional subtable if name does not match */
457 
458     if ((Info->Flags & DT_OPTIONAL) &&
459         strcmp ((*Field)->Name, Info->Name))
460     {
461         *RetSubtable = NULL;
462         return (AE_OK);
463     }
464 
465     Length = DtGetSubtableLength (*Field, Info);
466     if (Length == ASL_EOF)
467     {
468         return (AE_ERROR);
469     }
470 
471     Subtable = UtSubtableCacheCalloc ();
472 
473     if (Length > 0)
474     {
475         String = UtStringCacheCalloc (Length);
476         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
477     }
478 
479     Subtable->Length = Length;
480     Subtable->TotalLength = Length;
481     Buffer = Subtable->Buffer;
482 
483     LocalField = *Field;
484     Subtable->Name = LocalField->Name;
485 
486     /*
487      * Main loop walks the info table for this ACPI table or subtable
488      */
489     for (; Info->Name; Info++)
490     {
491         if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
492         {
493             continue;
494         }
495 
496         if (!LocalField)
497         {
498             sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
499                 Info->Name);
500             DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
501             Status = AE_BAD_DATA;
502             goto Error;
503         }
504 
505         /* Maintain table offsets */
506 
507         LocalField->TableOffset = Gbl_CurrentTableOffset;
508         FieldLength = DtGetFieldLength (LocalField, Info);
509         Gbl_CurrentTableOffset += FieldLength;
510 
511         FieldType = DtGetFieldType (Info);
512         Gbl_InputFieldCount++;
513 
514         switch (FieldType)
515         {
516         case DT_FIELD_TYPE_FLAGS_INTEGER:
517             /*
518              * Start of the definition of a flags field.
519              * This master flags integer starts at value zero, in preparation
520              * to compile and insert the flag fields from the individual bits
521              */
522             LocalField = LocalField->Next;
523             *Field = LocalField;
524 
525             FlagBuffer = Buffer;
526             CurrentFlagByteOffset = Info->Offset;
527             break;
528 
529         case DT_FIELD_TYPE_FLAG:
530 
531             /* Individual Flag field, can be multiple bits */
532 
533             if (FlagBuffer)
534             {
535                 /*
536                  * We must increment the FlagBuffer when we have crossed
537                  * into the next flags byte within the flags field
538                  * of type DT_FIELD_TYPE_FLAGS_INTEGER.
539                  */
540                 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
541                 CurrentFlagByteOffset = Info->Offset;
542 
543                 DtCompileFlag (FlagBuffer, LocalField, Info);
544             }
545             else
546             {
547                 /* TBD - this is an internal error */
548             }
549 
550             LocalField = LocalField->Next;
551             *Field = LocalField;
552             break;
553 
554         case DT_FIELD_TYPE_INLINE_SUBTABLE:
555             /*
556              * Recursion (one level max): compile GAS (Generic Address)
557              * or Notify in-line subtable
558              */
559             *Field = LocalField;
560 
561             switch (Info->Opcode)
562             {
563             case ACPI_DMT_GAS:
564 
565                 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
566                     &InlineSubtable, TRUE);
567                 break;
568 
569             case ACPI_DMT_HESTNTFY:
570 
571                 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
572                     &InlineSubtable, TRUE);
573                 break;
574 
575             case ACPI_DMT_IORTMEM:
576 
577                 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
578                     &InlineSubtable, TRUE);
579                 break;
580 
581             default:
582                 sprintf (MsgBuffer, "Invalid DMT opcode: 0x%.2X",
583                     Info->Opcode);
584                 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
585                 Status = AE_BAD_DATA;
586                 break;
587             }
588 
589             if (ACPI_FAILURE (Status))
590             {
591                 goto Error;
592             }
593 
594             DtSetSubtableLength (InlineSubtable);
595 
596             memcpy (Buffer, InlineSubtable->Buffer, FieldLength);
597             LocalField = *Field;
598             break;
599 
600         case DT_FIELD_TYPE_LABEL:
601 
602             DtWriteFieldToListing (Buffer, LocalField, 0);
603             LocalField = LocalField->Next;
604             break;
605 
606         default:
607 
608             /* Normal case for most field types (Integer, String, etc.) */
609 
610             DtCompileOneField (Buffer, LocalField,
611                 FieldLength, FieldType, Info->Flags);
612 
613             DtWriteFieldToListing (Buffer, LocalField, FieldLength);
614             LocalField = LocalField->Next;
615 
616             if (Info->Flags & DT_LENGTH)
617             {
618                 /* Field is an Integer that will contain a subtable length */
619 
620                 Subtable->LengthField = Buffer;
621                 Subtable->SizeOfLengthField = FieldLength;
622             }
623             break;
624         }
625 
626         Buffer += FieldLength;
627     }
628 
629     *Field = LocalField;
630     *RetSubtable = Subtable;
631     return (AE_OK);
632 
633 Error:
634     ACPI_FREE (Subtable->Buffer);
635     ACPI_FREE (Subtable);
636     return (Status);
637 }
638 
639 
640 /******************************************************************************
641  *
642  * FUNCTION:    DtCompileTwoSubtables
643  *
644  * PARAMETERS:  List                - Current field list pointer
645  *              TableInfo1          - Info table 1
646  *              TableInfo1          - Info table 2
647  *
648  * RETURN:      Status
649  *
650  * DESCRIPTION: Compile tables with a header and one or more same subtables.
651  *              Include CPEP, EINJ, ERST, MCFG, MSCT, WDAT
652  *
653  *****************************************************************************/
654 
655 ACPI_STATUS
656 DtCompileTwoSubtables (
657     void                    **List,
658     ACPI_DMTABLE_INFO       *TableInfo1,
659     ACPI_DMTABLE_INFO       *TableInfo2)
660 {
661     ACPI_STATUS             Status;
662     DT_SUBTABLE             *Subtable;
663     DT_SUBTABLE             *ParentTable;
664     DT_FIELD                **PFieldList = (DT_FIELD **) List;
665 
666 
667     Status = DtCompileTable (PFieldList, TableInfo1, &Subtable, TRUE);
668     if (ACPI_FAILURE (Status))
669     {
670         return (Status);
671     }
672 
673     ParentTable = DtPeekSubtable ();
674     DtInsertSubtable (ParentTable, Subtable);
675 
676     while (*PFieldList)
677     {
678         Status = DtCompileTable (PFieldList, TableInfo2, &Subtable, FALSE);
679         if (ACPI_FAILURE (Status))
680         {
681             return (Status);
682         }
683 
684         DtInsertSubtable (ParentTable, Subtable);
685     }
686 
687     return (AE_OK);
688 }
689 
690 
691 /******************************************************************************
692  *
693  * FUNCTION:    DtCompilePadding
694  *
695  * PARAMETERS:  Length              - Padding field size
696  *              RetSubtable         - Compile result of table
697  *
698  * RETURN:      Status
699  *
700  * DESCRIPTION: Compile a subtable for padding purpose
701  *
702  *****************************************************************************/
703 
704 ACPI_STATUS
705 DtCompilePadding (
706     UINT32                  Length,
707     DT_SUBTABLE             **RetSubtable)
708 {
709     DT_SUBTABLE             *Subtable;
710     /* UINT8                   *Buffer; */
711     char                    *String;
712 
713 
714     Subtable = UtSubtableCacheCalloc ();
715 
716     if (Length > 0)
717     {
718         String = UtStringCacheCalloc (Length);
719         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
720     }
721 
722     Subtable->Length = Length;
723     Subtable->TotalLength = Length;
724     /* Buffer = Subtable->Buffer; */
725 
726     *RetSubtable = Subtable;
727     return (AE_OK);
728 }
729