xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/dtcompile.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
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     snprintf (VersionString, sizeof(VersionString), "%X",
219 	(UINT32) ACPI_CA_VERSION);
220     return (AE_OK);
221 }
222 
223 
224 /******************************************************************************
225  *
226  * FUNCTION:    DtInsertCompilerIds
227  *
228  * PARAMETERS:  FieldList           - Current field list pointer
229  *
230  * RETURN:      None
231  *
232  * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
233  *              the original ACPI table header.
234  *
235  *****************************************************************************/
236 
237 static void
238 DtInsertCompilerIds (
239     DT_FIELD                *FieldList)
240 {
241     DT_FIELD                *Next;
242     UINT32                  i;
243 
244 
245     /*
246      * Don't insert current compiler ID if requested. Used for compiler
247      * debug/validation only.
248      */
249     if (Gbl_UseOriginalCompilerId)
250     {
251         return;
252     }
253 
254     /* Walk to the Compiler fields at the end of the header */
255 
256     Next = FieldList;
257     for (i = 0; i < 7; i++)
258     {
259         Next = Next->Next;
260     }
261 
262     Next->Value = ASL_CREATOR_ID;
263     Next->Flags = DT_FIELD_NOT_ALLOCATED;
264 
265     Next = Next->Next;
266     Next->Value = VersionString;
267     Next->Flags = DT_FIELD_NOT_ALLOCATED;
268 }
269 
270 
271 /******************************************************************************
272  *
273  * FUNCTION:    DtCompileDataTable
274  *
275  * PARAMETERS:  FieldList           - Current field list pointer
276  *
277  * RETURN:      Status
278  *
279  * DESCRIPTION: Entry point to compile one data table
280  *
281  *****************************************************************************/
282 
283 static ACPI_STATUS
284 DtCompileDataTable (
285     DT_FIELD                **FieldList)
286 {
287     ACPI_DMTABLE_DATA       *TableData;
288     DT_SUBTABLE             *Subtable;
289     char                    *Signature;
290     ACPI_TABLE_HEADER       *AcpiTableHeader;
291     ACPI_STATUS             Status;
292     DT_FIELD                *RootField = *FieldList;
293 
294 
295     /* Verify that we at least have a table signature and save it */
296 
297     Signature = DtGetFieldValue (*FieldList);
298     if (!Signature)
299     {
300         snprintf (MsgBuffer, sizeof(MsgBuffer), "Expected \"%s\"", "Signature");
301         DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
302             *FieldList, MsgBuffer);
303         return (AE_ERROR);
304     }
305 
306     Gbl_Signature = UtStringCacheCalloc (ACPI_STRLEN (Signature) + 1);
307     strcpy (Gbl_Signature, Signature);
308 
309     /*
310      * Handle tables that don't use the common ACPI table header structure.
311      * Currently, these are the FACS and RSDP. Also check for an OEMx table,
312      * these tables have user-defined contents.
313      */
314     if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
315     {
316         Status = DtCompileFacs (FieldList);
317         if (ACPI_FAILURE (Status))
318         {
319             return (Status);
320         }
321 
322         DtSetTableLength ();
323         return (Status);
324     }
325     else if (ACPI_VALIDATE_RSDP_SIG (Signature))
326     {
327         Status = DtCompileRsdp (FieldList);
328         return (Status);
329     }
330     else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_S3PT))
331     {
332         Status = DtCompileS3pt (FieldList);
333         if (ACPI_FAILURE (Status))
334         {
335             return (Status);
336         }
337 
338         DtSetTableLength ();
339         return (Status);
340     }
341 
342     /*
343      * All other tables must use the common ACPI table header. Insert the
344      * current iASL IDs (name, version), and compile the header now.
345      */
346     DtInsertCompilerIds (*FieldList);
347 
348     Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
349                 &Gbl_RootTable, TRUE);
350     if (ACPI_FAILURE (Status))
351     {
352         return (Status);
353     }
354 
355     DtPushSubtable (Gbl_RootTable);
356 
357     /* Validate the signature via the ACPI table list */
358 
359     TableData = AcpiDmGetTableData (Signature);
360     if (!TableData || Gbl_CompileGeneric)
361     {
362         DtCompileGeneric ((void **) FieldList);
363         goto FinishHeader;
364     }
365 
366     /* Dispatch to per-table compile */
367 
368     if (TableData->CmTableHandler)
369     {
370         /* Complex table, has a handler */
371 
372         Status = TableData->CmTableHandler ((void **) FieldList);
373         if (ACPI_FAILURE (Status))
374         {
375             return (Status);
376         }
377     }
378     else if (TableData->TableInfo)
379     {
380         /* Simple table, just walk the info table */
381 
382         Subtable = NULL;
383         Status = DtCompileTable (FieldList, TableData->TableInfo,
384                     &Subtable, TRUE);
385         if (ACPI_FAILURE (Status))
386         {
387             return (Status);
388         }
389 
390         DtInsertSubtable (Gbl_RootTable, Subtable);
391         DtPopSubtable ();
392     }
393     else
394     {
395         DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
396             "Missing table dispatch info");
397         return (AE_ERROR);
398     }
399 
400 FinishHeader:
401 
402     /* Set the final table length and then the checksum */
403 
404     DtSetTableLength ();
405     AcpiTableHeader = ACPI_CAST_PTR (
406         ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
407     DtSetTableChecksum (&AcpiTableHeader->Checksum);
408 
409     DtDumpFieldList (RootField);
410     DtDumpSubtableList ();
411     return (AE_OK);
412 }
413 
414 
415 /******************************************************************************
416  *
417  * FUNCTION:    DtCompileTable
418  *
419  * PARAMETERS:  Field               - Current field list pointer
420  *              Info                - Info table for this ACPI table
421  *              RetSubtable         - Compile result of table
422  *              Required            - If this subtable must exist
423  *
424  * RETURN:      Status
425  *
426  * DESCRIPTION: Compile a subtable
427  *
428  *****************************************************************************/
429 
430 ACPI_STATUS
431 DtCompileTable (
432     DT_FIELD                **Field,
433     ACPI_DMTABLE_INFO       *Info,
434     DT_SUBTABLE             **RetSubtable,
435     BOOLEAN                 Required)
436 {
437     DT_FIELD                *LocalField;
438     UINT32                  Length;
439     DT_SUBTABLE             *Subtable;
440     DT_SUBTABLE             *InlineSubtable;
441     UINT32                  FieldLength = 0;
442     UINT8                   FieldType;
443     UINT8                   *Buffer;
444     UINT8                   *FlagBuffer = NULL;
445     char                    *String;
446     UINT32                  CurrentFlagByteOffset = 0;
447     ACPI_STATUS             Status;
448 
449 
450     if (!Field || !*Field)
451     {
452         return (AE_BAD_PARAMETER);
453     }
454 
455     /* Ignore optional subtable if name does not match */
456 
457     if ((Info->Flags & DT_OPTIONAL) &&
458         ACPI_STRCMP ((*Field)->Name, Info->Name))
459     {
460         *RetSubtable = NULL;
461         return (AE_OK);
462     }
463 
464     Length = DtGetSubtableLength (*Field, Info);
465     if (Length == ASL_EOF)
466     {
467         return (AE_ERROR);
468     }
469 
470     Subtable = UtSubtableCacheCalloc ();
471 
472     if (Length > 0)
473     {
474         String = UtStringCacheCalloc (Length);
475         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
476     }
477 
478     Subtable->Length = Length;
479     Subtable->TotalLength = Length;
480     Buffer = Subtable->Buffer;
481 
482     LocalField = *Field;
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             snprintf (MsgBuffer, sizeof(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             if (Info->Opcode == ACPI_DMT_GAS)
560             {
561                 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
562                     &InlineSubtable, TRUE);
563             }
564             else
565             {
566                 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
567                     &InlineSubtable, TRUE);
568             }
569 
570             if (ACPI_FAILURE (Status))
571             {
572                 goto Error;
573             }
574 
575             DtSetSubtableLength (InlineSubtable);
576 
577             ACPI_MEMCPY (Buffer, InlineSubtable->Buffer, FieldLength);
578             LocalField = *Field;
579             break;
580 
581         case DT_FIELD_TYPE_LABEL:
582 
583             DtWriteFieldToListing (Buffer, LocalField, 0);
584             LocalField = LocalField->Next;
585             break;
586 
587         default:
588 
589             /* Normal case for most field types (Integer, String, etc.) */
590 
591             DtCompileOneField (Buffer, LocalField,
592                 FieldLength, FieldType, Info->Flags);
593 
594             DtWriteFieldToListing (Buffer, LocalField, FieldLength);
595             LocalField = LocalField->Next;
596 
597             if (Info->Flags & DT_LENGTH)
598             {
599                 /* Field is an Integer that will contain a subtable length */
600 
601                 Subtable->LengthField = Buffer;
602                 Subtable->SizeOfLengthField = FieldLength;
603             }
604 
605             break;
606         }
607 
608         Buffer += FieldLength;
609     }
610 
611     *Field = LocalField;
612     *RetSubtable = Subtable;
613     return (AE_OK);
614 
615 Error:
616     ACPI_FREE (Subtable->Buffer);
617     ACPI_FREE (Subtable);
618     return (Status);
619 }
620