dtcompile.c revision 1.1.1.21 1 /******************************************************************************
2 *
3 * Module Name: dtcompile.c - Front-end for data table compiler
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2023, 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 * If the first field is named "CDAT Table Length" (not "Signature"),
348 * assume that we have a CDAT table (whose table header does not have
349 * a signature). Instead, the TableLength field is where the
350 * signature would (normally) be.
351 */
352 else if (!strcmp ((*FieldList)->Name, "CDAT Table Length"))
353 {
354 /* No longer true: (However, use this technique in the disassembler)
355 * We are assuming that there
356 * should be at least one non-ASCII byte in the 4-character
357 * Signature field, (At least the high-order byte should be zero).
358 */
359 Status = DtCompileTable (FieldList, AcpiDmTableInfoCdatTableHdr,
360 &AslGbl_RootTable);
361 if (ACPI_FAILURE (Status))
362 {
363 return (Status);
364 }
365
366 /* Compile the CDAT */
367
368 DtPushSubtable (AslGbl_RootTable);
369 Status = DtCompileCdat ((void **) FieldList);
370 if (ACPI_FAILURE (Status))
371 {
372 return (Status);
373 }
374
375 /*
376 * Set the overall table length and the table checksum.
377 * The entire compiled table (including the CDAT table header with
378 * the table length and checksum) is in AslGbl_RootTable->Buffer.
379 */
380 DtSetTableLength ();
381 DtSetTableChecksum (&ACPI_CAST_PTR (ACPI_TABLE_CDAT, AslGbl_RootTable->Buffer)->Checksum);
382
383 DtDumpFieldList (RootField);
384 DtDumpSubtableList ();
385 return (AE_OK);
386 }
387
388 /*
389 * All other tables must use the common ACPI table header. Insert the
390 * current iASL IDs (name, version), and compile the header now.
391 */
392 DtInsertCompilerIds (*FieldList);
393
394 Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
395 &AslGbl_RootTable);
396 if (ACPI_FAILURE (Status))
397 {
398 return (Status);
399 }
400
401 DtPushSubtable (AslGbl_RootTable);
402
403 /* Validate the signature via the ACPI table list */
404
405 TableData = AcpiDmGetTableData (Signature);
406 if (!TableData || AslGbl_CompileGeneric)
407 {
408 /* Unknown table signature and/or force generic compile */
409
410 DtCompileGeneric ((void **) FieldList, NULL, NULL);
411 goto FinishHeader;
412 }
413
414 /* Dispatch to per-table compile */
415
416 if (TableData->CmTableHandler)
417 {
418 /* Complex table, has a handler */
419
420 Status = TableData->CmTableHandler ((void **) FieldList);
421 if (ACPI_FAILURE (Status))
422 {
423 return (Status);
424 }
425 }
426 else if (TableData->TableInfo)
427 {
428 /* Simple table, just walk the info table, unless its empty */
429
430 if (FieldList && *FieldList)
431 {
432 Subtable = NULL;
433 Status = DtCompileTable (FieldList, TableData->TableInfo,
434 &Subtable);
435 if (ACPI_FAILURE (Status))
436 {
437 return (Status);
438 }
439
440 DtInsertSubtable (AslGbl_RootTable, Subtable);
441 DtPopSubtable ();
442 }
443 }
444 else
445 {
446 DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
447 "Missing table dispatch info");
448 return (AE_ERROR);
449 }
450
451 FinishHeader:
452
453 /* Set the final table length and then the checksum */
454
455 DtSetTableLength ();
456 AcpiTableHeader = ACPI_CAST_PTR (
457 ACPI_TABLE_HEADER, AslGbl_RootTable->Buffer);
458 DtSetTableChecksum (&AcpiTableHeader->Checksum);
459
460 DtDumpFieldList (RootField);
461 DtDumpSubtableList ();
462 return (AE_OK);
463 }
464
465
466 /******************************************************************************
467 *
468 * FUNCTION: DtCompileTable
469 *
470 * PARAMETERS: Field - Current field list pointer
471 * Info - Info table for this ACPI table
472 * RetSubtable - Compile result of table
473 *
474 * RETURN: Status
475 *
476 * DESCRIPTION: Compile a subtable
477 *
478 *****************************************************************************/
479
480 ACPI_STATUS
481 DtCompileTable (
482 DT_FIELD **Field,
483 ACPI_DMTABLE_INFO *Info,
484 DT_SUBTABLE **RetSubtable)
485 {
486 DT_FIELD *LocalField;
487 UINT32 Length;
488 DT_SUBTABLE *Subtable;
489 DT_SUBTABLE *InlineSubtable = NULL;
490 UINT32 FieldLength = 0;
491 UINT8 FieldType;
492 UINT8 *Buffer;
493 UINT8 *FlagBuffer = NULL;
494 char *String;
495 UINT32 CurrentFlagByteOffset = 0;
496 ACPI_STATUS Status = AE_OK;
497
498
499 if (!Field || !Info)
500 {
501 return (AE_BAD_PARAMETER);
502 }
503 if (!*Field)
504 {
505 /*
506 * The field list is empty, this means that we are out of fields to
507 * parse. In other words, we are at the end of the table.
508 */
509 return (AE_END_OF_TABLE);
510 }
511
512 /* Ignore optional subtable if name does not match */
513
514 if ((Info->Flags & DT_OPTIONAL) &&
515 strcmp ((*Field)->Name, Info->Name))
516 {
517 *RetSubtable = NULL;
518 return (AE_OK);
519 }
520
521 Length = DtGetSubtableLength (*Field, Info);
522 if (Length == ASL_EOF)
523 {
524 return (AE_ERROR);
525 }
526
527 Subtable = UtSubtableCacheCalloc ();
528
529 if (Length > 0)
530 {
531 String = UtLocalCacheCalloc (Length);
532 Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
533 }
534
535 Subtable->Length = Length;
536 Subtable->TotalLength = Length;
537 Buffer = Subtable->Buffer;
538
539 LocalField = *Field;
540 Subtable->Name = LocalField->Name;
541
542 /*
543 * Main loop walks the info table for this ACPI table or subtable
544 */
545 for (; Info->Name; Info++)
546 {
547 if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
548 {
549 continue;
550 }
551
552 if (!LocalField)
553 {
554 sprintf (AslGbl_MsgBuffer, "Found NULL field - Field name \"%s\" needed",
555 Info->Name);
556 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, AslGbl_MsgBuffer);
557 Status = AE_BAD_DATA;
558 goto Error;
559 }
560
561 /* Maintain table offsets */
562
563 LocalField->TableOffset = AslGbl_CurrentTableOffset;
564 FieldLength = DtGetFieldLength (LocalField, Info);
565 AslGbl_CurrentTableOffset += FieldLength;
566
567 FieldType = DtGetFieldType (Info);
568 AslGbl_InputFieldCount++;
569
570 if (FieldType != DT_FIELD_TYPE_INLINE_SUBTABLE &&
571 strcmp (Info->Name, LocalField->Name))
572 {
573 sprintf (AslGbl_MsgBuffer, "found \"%s\" expected \"%s\"",
574 LocalField->Name, Info->Name);
575 DtError (ASL_ERROR, ASL_MSG_INVALID_LABEL, LocalField, AslGbl_MsgBuffer);
576 }
577
578 switch (FieldType)
579 {
580 case DT_FIELD_TYPE_FLAGS_INTEGER:
581 /*
582 * Start of the definition of a flags field.
583 * This master flags integer starts at value zero, in preparation
584 * to compile and insert the flag fields from the individual bits
585 */
586 LocalField = LocalField->Next;
587 *Field = LocalField;
588
589 FlagBuffer = Buffer;
590 CurrentFlagByteOffset = Info->Offset;
591 break;
592
593 case DT_FIELD_TYPE_FLAG:
594
595 /* Individual Flag field, can be multiple bits */
596
597 if (FlagBuffer)
598 {
599 /*
600 * We must increment the FlagBuffer when we have crossed
601 * into the next flags byte within the flags field
602 * of type DT_FIELD_TYPE_FLAGS_INTEGER.
603 */
604 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
605 CurrentFlagByteOffset = Info->Offset;
606
607 DtCompileFlag (FlagBuffer, LocalField, Info);
608 }
609 else
610 {
611 /* TBD - this is an internal error */
612 }
613
614 LocalField = LocalField->Next;
615 *Field = LocalField;
616 break;
617
618 case DT_FIELD_TYPE_INLINE_SUBTABLE:
619 /*
620 * Recursion (one level max): compile GAS (Generic Address)
621 * or Notify in-line subtable
622 */
623 *Field = LocalField;
624
625 switch (Info->Opcode)
626 {
627 case ACPI_DMT_GAS:
628
629 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
630 &InlineSubtable);
631 break;
632
633 case ACPI_DMT_HESTNTFY:
634
635 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
636 &InlineSubtable);
637 break;
638
639 case ACPI_DMT_IORTMEM:
640
641 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
642 &InlineSubtable);
643 break;
644
645 default:
646 sprintf (AslGbl_MsgBuffer, "Invalid DMT opcode: 0x%.2X",
647 Info->Opcode);
648 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, AslGbl_MsgBuffer);
649 Status = AE_BAD_DATA;
650 break;
651 }
652
653 if (ACPI_FAILURE (Status))
654 {
655 goto Error;
656 }
657
658 DtSetSubtableLength (InlineSubtable);
659
660 memcpy (Buffer, InlineSubtable->Buffer, FieldLength);
661 LocalField = *Field;
662 break;
663
664 case DT_FIELD_TYPE_LABEL:
665
666 DtWriteFieldToListing (Buffer, LocalField, 0);
667 LocalField = LocalField->Next;
668 break;
669
670 default:
671
672 /* Normal case for most field types (Integer, String, etc.) */
673
674 DtCompileOneField (Buffer, LocalField,
675 FieldLength, FieldType, Info->Flags);
676
677 DtWriteFieldToListing (Buffer, LocalField, FieldLength);
678 LocalField = LocalField->Next;
679
680 if (Info->Flags & DT_LENGTH)
681 {
682 /* Field is an Integer that will contain a subtable length */
683
684 Subtable->LengthField = Buffer;
685 Subtable->SizeOfLengthField = FieldLength;
686 }
687 break;
688 }
689
690 Buffer += FieldLength;
691 }
692
693 *Field = LocalField;
694 *RetSubtable = Subtable;
695 return (AE_OK);
696
697 Error:
698 ACPI_FREE (Subtable->Buffer);
699 ACPI_FREE (Subtable);
700 return (Status);
701 }
702
703
704 /******************************************************************************
705 *
706 * FUNCTION: DtCompileTwoSubtables
707 *
708 * PARAMETERS: List - Current field list pointer
709 * TableInfo1 - Info table 1
710 * TableInfo1 - Info table 2
711 *
712 * RETURN: Status
713 *
714 * DESCRIPTION: Compile tables with a header and one or more same subtables.
715 * Include CPEP, EINJ, ERST, MCFG, MSCT, WDAT
716 *
717 *****************************************************************************/
718
719 ACPI_STATUS
720 DtCompileTwoSubtables (
721 void **List,
722 ACPI_DMTABLE_INFO *TableInfo1,
723 ACPI_DMTABLE_INFO *TableInfo2)
724 {
725 ACPI_STATUS Status;
726 DT_SUBTABLE *Subtable;
727 DT_SUBTABLE *ParentTable;
728 DT_FIELD **PFieldList = (DT_FIELD **) List;
729
730
731 Status = DtCompileTable (PFieldList, TableInfo1, &Subtable);
732 if (ACPI_FAILURE (Status))
733 {
734 return (Status);
735 }
736
737 ParentTable = DtPeekSubtable ();
738 DtInsertSubtable (ParentTable, Subtable);
739
740 while (*PFieldList)
741 {
742 Status = DtCompileTable (PFieldList, TableInfo2, &Subtable);
743 if (ACPI_FAILURE (Status))
744 {
745 return (Status);
746 }
747
748 DtInsertSubtable (ParentTable, Subtable);
749 }
750
751 return (AE_OK);
752 }
753
754
755 /******************************************************************************
756 *
757 * FUNCTION: DtCompilePadding
758 *
759 * PARAMETERS: Length - Padding field size
760 * RetSubtable - Compile result of table
761 *
762 * RETURN: Status
763 *
764 * DESCRIPTION: Compile a subtable for padding purpose
765 *
766 *****************************************************************************/
767
768 ACPI_STATUS
769 DtCompilePadding (
770 UINT32 Length,
771 DT_SUBTABLE **RetSubtable)
772 {
773 DT_SUBTABLE *Subtable;
774 /* UINT8 *Buffer; */
775 char *String;
776
777
778 Subtable = UtSubtableCacheCalloc ();
779
780 if (Length > 0)
781 {
782 String = UtLocalCacheCalloc (Length);
783 Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
784 }
785
786 Subtable->Length = Length;
787 Subtable->TotalLength = Length;
788 /* Buffer = Subtable->Buffer; */
789
790 *RetSubtable = Subtable;
791 return (AE_OK);
792 }
793