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