dtcompile.c revision 1.1.1.10 1 /******************************************************************************
2 *
3 * Module Name: dtcompile.c - Front-end for data table compiler
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2017, 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 AcpiUtSetIntegerWidth (2); /* Set width to 64 bits */
215
216 Gbl_FieldList = NULL;
217 Gbl_RootTable = NULL;
218 Gbl_SubtableStack = NULL;
219
220 sprintf (VersionString, "%X", (UINT32) ACPI_CA_VERSION);
221 return (AE_OK);
222 }
223
224
225 /******************************************************************************
226 *
227 * FUNCTION: DtInsertCompilerIds
228 *
229 * PARAMETERS: FieldList - Current field list pointer
230 *
231 * RETURN: None
232 *
233 * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
234 * the original ACPI table header.
235 *
236 *****************************************************************************/
237
238 static void
239 DtInsertCompilerIds (
240 DT_FIELD *FieldList)
241 {
242 DT_FIELD *Next;
243 UINT32 i;
244
245
246 /*
247 * Don't insert current compiler ID if requested. Used for compiler
248 * debug/validation only.
249 */
250 if (Gbl_UseOriginalCompilerId)
251 {
252 return;
253 }
254
255 /* Walk to the Compiler fields at the end of the header */
256
257 Next = FieldList;
258 for (i = 0; i < 7; i++)
259 {
260 Next = Next->Next;
261 }
262
263 Next->Value = ASL_CREATOR_ID;
264 Next->Flags = DT_FIELD_NOT_ALLOCATED;
265
266 Next = Next->Next;
267 Next->Value = VersionString;
268 Next->Flags = DT_FIELD_NOT_ALLOCATED;
269 }
270
271
272 /******************************************************************************
273 *
274 * FUNCTION: DtCompileDataTable
275 *
276 * PARAMETERS: FieldList - Current field list pointer
277 *
278 * RETURN: Status
279 *
280 * DESCRIPTION: Entry point to compile one data table
281 *
282 *****************************************************************************/
283
284 static ACPI_STATUS
285 DtCompileDataTable (
286 DT_FIELD **FieldList)
287 {
288 const ACPI_DMTABLE_DATA *TableData;
289 DT_SUBTABLE *Subtable;
290 char *Signature;
291 ACPI_TABLE_HEADER *AcpiTableHeader;
292 ACPI_STATUS Status;
293 DT_FIELD *RootField = *FieldList;
294
295
296 /* Verify that we at least have a table signature and save it */
297
298 Signature = DtGetFieldValue (*FieldList);
299 if (!Signature)
300 {
301 sprintf (MsgBuffer, "Expected \"%s\"", "Signature");
302 DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
303 *FieldList, MsgBuffer);
304 return (AE_ERROR);
305 }
306
307 Gbl_Signature = UtStringCacheCalloc (strlen (Signature) + 1);
308 strcpy (Gbl_Signature, Signature);
309
310 /*
311 * Handle tables that don't use the common ACPI table header structure.
312 * Currently, these are the FACS and RSDP. Also check for an OEMx table,
313 * these tables have user-defined contents.
314 */
315 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
316 {
317 Status = DtCompileFacs (FieldList);
318 if (ACPI_FAILURE (Status))
319 {
320 return (Status);
321 }
322
323 DtSetTableLength ();
324 return (Status);
325 }
326 else if (ACPI_VALIDATE_RSDP_SIG (Signature))
327 {
328 Status = DtCompileRsdp (FieldList);
329 return (Status);
330 }
331 else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_S3PT))
332 {
333 Status = DtCompileS3pt (FieldList);
334 if (ACPI_FAILURE (Status))
335 {
336 return (Status);
337 }
338
339 DtSetTableLength ();
340 return (Status);
341 }
342
343 /*
344 * All other tables must use the common ACPI table header. Insert the
345 * current iASL IDs (name, version), and compile the header now.
346 */
347 DtInsertCompilerIds (*FieldList);
348
349 Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
350 &Gbl_RootTable, TRUE);
351 if (ACPI_FAILURE (Status))
352 {
353 return (Status);
354 }
355
356 DtPushSubtable (Gbl_RootTable);
357
358 /* Validate the signature via the ACPI table list */
359
360 TableData = AcpiDmGetTableData (Signature);
361 if (!TableData || Gbl_CompileGeneric)
362 {
363 /* Unknown table signature and/or force generic compile */
364
365 DtCompileGeneric ((void **) FieldList, NULL, NULL);
366 goto FinishHeader;
367 }
368
369 /* Dispatch to per-table compile */
370
371 if (TableData->CmTableHandler)
372 {
373 /* Complex table, has a handler */
374
375 Status = TableData->CmTableHandler ((void **) FieldList);
376 if (ACPI_FAILURE (Status))
377 {
378 return (Status);
379 }
380 }
381 else if (TableData->TableInfo)
382 {
383 /* Simple table, just walk the info table, unless its empty */
384
385 if (FieldList && *FieldList)
386 {
387 Subtable = NULL;
388 Status = DtCompileTable (FieldList, TableData->TableInfo,
389 &Subtable, TRUE);
390 if (ACPI_FAILURE (Status))
391 {
392 return (Status);
393 }
394
395 DtInsertSubtable (Gbl_RootTable, Subtable);
396 DtPopSubtable ();
397 }
398 }
399 else
400 {
401 DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
402 "Missing table dispatch info");
403 return (AE_ERROR);
404 }
405
406 FinishHeader:
407
408 /* Set the final table length and then the checksum */
409
410 DtSetTableLength ();
411 AcpiTableHeader = ACPI_CAST_PTR (
412 ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
413 DtSetTableChecksum (&AcpiTableHeader->Checksum);
414
415 DtDumpFieldList (RootField);
416 DtDumpSubtableList ();
417 return (AE_OK);
418 }
419
420
421 /******************************************************************************
422 *
423 * FUNCTION: DtCompileTable
424 *
425 * PARAMETERS: Field - Current field list pointer
426 * Info - Info table for this ACPI table
427 * RetSubtable - Compile result of table
428 * Required - If this subtable must exist
429 *
430 * RETURN: Status
431 *
432 * DESCRIPTION: Compile a subtable
433 *
434 *****************************************************************************/
435
436 ACPI_STATUS
437 DtCompileTable (
438 DT_FIELD **Field,
439 ACPI_DMTABLE_INFO *Info,
440 DT_SUBTABLE **RetSubtable,
441 BOOLEAN Required)
442 {
443 DT_FIELD *LocalField;
444 UINT32 Length;
445 DT_SUBTABLE *Subtable;
446 DT_SUBTABLE *InlineSubtable = NULL;
447 UINT32 FieldLength = 0;
448 UINT8 FieldType;
449 UINT8 *Buffer;
450 UINT8 *FlagBuffer = NULL;
451 char *String;
452 UINT32 CurrentFlagByteOffset = 0;
453 ACPI_STATUS Status = AE_OK;
454
455
456 if (!Field || !*Field)
457 {
458 return (AE_BAD_PARAMETER);
459 }
460
461 /* Ignore optional subtable if name does not match */
462
463 if ((Info->Flags & DT_OPTIONAL) &&
464 strcmp ((*Field)->Name, Info->Name))
465 {
466 *RetSubtable = NULL;
467 return (AE_OK);
468 }
469
470 Length = DtGetSubtableLength (*Field, Info);
471 if (Length == ASL_EOF)
472 {
473 return (AE_ERROR);
474 }
475
476 Subtable = UtSubtableCacheCalloc ();
477
478 if (Length > 0)
479 {
480 String = UtStringCacheCalloc (Length);
481 Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
482 }
483
484 Subtable->Length = Length;
485 Subtable->TotalLength = Length;
486 Buffer = Subtable->Buffer;
487
488 LocalField = *Field;
489 Subtable->Name = LocalField->Name;
490
491 /*
492 * Main loop walks the info table for this ACPI table or subtable
493 */
494 for (; Info->Name; Info++)
495 {
496 if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
497 {
498 continue;
499 }
500
501 if (!LocalField)
502 {
503 sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
504 Info->Name);
505 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
506 Status = AE_BAD_DATA;
507 goto Error;
508 }
509
510 /* Maintain table offsets */
511
512 LocalField->TableOffset = Gbl_CurrentTableOffset;
513 FieldLength = DtGetFieldLength (LocalField, Info);
514 Gbl_CurrentTableOffset += FieldLength;
515
516 FieldType = DtGetFieldType (Info);
517 Gbl_InputFieldCount++;
518
519 switch (FieldType)
520 {
521 case DT_FIELD_TYPE_FLAGS_INTEGER:
522 /*
523 * Start of the definition of a flags field.
524 * This master flags integer starts at value zero, in preparation
525 * to compile and insert the flag fields from the individual bits
526 */
527 LocalField = LocalField->Next;
528 *Field = LocalField;
529
530 FlagBuffer = Buffer;
531 CurrentFlagByteOffset = Info->Offset;
532 break;
533
534 case DT_FIELD_TYPE_FLAG:
535
536 /* Individual Flag field, can be multiple bits */
537
538 if (FlagBuffer)
539 {
540 /*
541 * We must increment the FlagBuffer when we have crossed
542 * into the next flags byte within the flags field
543 * of type DT_FIELD_TYPE_FLAGS_INTEGER.
544 */
545 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
546 CurrentFlagByteOffset = Info->Offset;
547
548 DtCompileFlag (FlagBuffer, LocalField, Info);
549 }
550 else
551 {
552 /* TBD - this is an internal error */
553 }
554
555 LocalField = LocalField->Next;
556 *Field = LocalField;
557 break;
558
559 case DT_FIELD_TYPE_INLINE_SUBTABLE:
560 /*
561 * Recursion (one level max): compile GAS (Generic Address)
562 * or Notify in-line subtable
563 */
564 *Field = LocalField;
565
566 switch (Info->Opcode)
567 {
568 case ACPI_DMT_GAS:
569
570 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
571 &InlineSubtable, TRUE);
572 break;
573
574 case ACPI_DMT_HESTNTFY:
575
576 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
577 &InlineSubtable, TRUE);
578 break;
579
580 case ACPI_DMT_IORTMEM:
581
582 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
583 &InlineSubtable, TRUE);
584 break;
585
586 default:
587 sprintf (MsgBuffer, "Invalid DMT opcode: 0x%.2X",
588 Info->Opcode);
589 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
590 Status = AE_BAD_DATA;
591 break;
592 }
593
594 if (ACPI_FAILURE (Status))
595 {
596 goto Error;
597 }
598
599 DtSetSubtableLength (InlineSubtable);
600
601 memcpy (Buffer, InlineSubtable->Buffer, FieldLength);
602 LocalField = *Field;
603 break;
604
605 case DT_FIELD_TYPE_LABEL:
606
607 DtWriteFieldToListing (Buffer, LocalField, 0);
608 LocalField = LocalField->Next;
609 break;
610
611 default:
612
613 /* Normal case for most field types (Integer, String, etc.) */
614
615 DtCompileOneField (Buffer, LocalField,
616 FieldLength, FieldType, Info->Flags);
617
618 DtWriteFieldToListing (Buffer, LocalField, FieldLength);
619 LocalField = LocalField->Next;
620
621 if (Info->Flags & DT_LENGTH)
622 {
623 /* Field is an Integer that will contain a subtable length */
624
625 Subtable->LengthField = Buffer;
626 Subtable->SizeOfLengthField = FieldLength;
627 }
628 break;
629 }
630
631 Buffer += FieldLength;
632 }
633
634 *Field = LocalField;
635 *RetSubtable = Subtable;
636 return (AE_OK);
637
638 Error:
639 ACPI_FREE (Subtable->Buffer);
640 ACPI_FREE (Subtable);
641 return (Status);
642 }
643
644
645 /******************************************************************************
646 *
647 * FUNCTION: DtCompileTwoSubtables
648 *
649 * PARAMETERS: List - Current field list pointer
650 * TableInfo1 - Info table 1
651 * TableInfo1 - Info table 2
652 *
653 * RETURN: Status
654 *
655 * DESCRIPTION: Compile tables with a header and one or more same subtables.
656 * Include CPEP, EINJ, ERST, MCFG, MSCT, WDAT
657 *
658 *****************************************************************************/
659
660 ACPI_STATUS
661 DtCompileTwoSubtables (
662 void **List,
663 ACPI_DMTABLE_INFO *TableInfo1,
664 ACPI_DMTABLE_INFO *TableInfo2)
665 {
666 ACPI_STATUS Status;
667 DT_SUBTABLE *Subtable;
668 DT_SUBTABLE *ParentTable;
669 DT_FIELD **PFieldList = (DT_FIELD **) List;
670
671
672 Status = DtCompileTable (PFieldList, TableInfo1, &Subtable, TRUE);
673 if (ACPI_FAILURE (Status))
674 {
675 return (Status);
676 }
677
678 ParentTable = DtPeekSubtable ();
679 DtInsertSubtable (ParentTable, Subtable);
680
681 while (*PFieldList)
682 {
683 Status = DtCompileTable (PFieldList, TableInfo2, &Subtable, FALSE);
684 if (ACPI_FAILURE (Status))
685 {
686 return (Status);
687 }
688
689 DtInsertSubtable (ParentTable, Subtable);
690 }
691
692 return (AE_OK);
693 }
694
695
696 /******************************************************************************
697 *
698 * FUNCTION: DtCompilePadding
699 *
700 * PARAMETERS: Length - Padding field size
701 * RetSubtable - Compile result of table
702 *
703 * RETURN: Status
704 *
705 * DESCRIPTION: Compile a subtable for padding purpose
706 *
707 *****************************************************************************/
708
709 ACPI_STATUS
710 DtCompilePadding (
711 UINT32 Length,
712 DT_SUBTABLE **RetSubtable)
713 {
714 DT_SUBTABLE *Subtable;
715 /* UINT8 *Buffer; */
716 char *String;
717
718
719 Subtable = UtSubtableCacheCalloc ();
720
721 if (Length > 0)
722 {
723 String = UtStringCacheCalloc (Length);
724 Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
725 }
726
727 Subtable->Length = Length;
728 Subtable->TotalLength = Length;
729 /* Buffer = Subtable->Buffer; */
730
731 *RetSubtable = Subtable;
732 return (AE_OK);
733 }
734