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