Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / dtcompile.c
blob2b92322c136fac22e1520aedec83f10d70b641ed
1 /******************************************************************************
3 * Module Name: dtcompile.c - Front-end for data table compiler
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #define __DTCOMPILE_C__
45 #define _DECLARE_DT_GLOBALS
47 #include "aslcompiler.h"
48 #include "dtcompiler.h"
50 #define _COMPONENT DT_COMPILER
51 ACPI_MODULE_NAME ("dtcompile")
53 static char VersionString[9];
56 /* Local prototypes */
58 static ACPI_STATUS
59 DtInitialize (
60 void);
62 static ACPI_STATUS
63 DtCompileDataTable (
64 DT_FIELD **Field);
66 static void
67 DtInsertCompilerIds (
68 DT_FIELD *FieldList);
71 /******************************************************************************
73 * FUNCTION: DtDoCompile
75 * PARAMETERS: None
77 * RETURN: Status
79 * DESCRIPTION: Main entry point for the data table compiler.
81 * Note: Assumes Gbl_Files[ASL_FILE_INPUT] is initialized and the file is
82 * open at seek offset zero.
84 *****************************************************************************/
86 ACPI_STATUS
87 DtDoCompile (
88 void)
90 ACPI_STATUS Status;
91 UINT8 Event;
92 DT_FIELD *FieldList;
95 /* Initialize globals */
97 Status = DtInitialize ();
98 if (ACPI_FAILURE (Status))
100 printf ("Error during compiler initialization, 0x%X\n", Status);
101 return (Status);
104 /* Preprocessor */
106 Event = UtBeginEvent ("Preprocess input file");
107 PrDoPreprocess ();
108 UtEndEvent (Event);
110 if (Gbl_PreprocessOnly)
112 return (AE_OK);
116 * Scan the input file (file is already open) and
117 * build the parse tree
119 Event = UtBeginEvent ("Scan and parse input file");
120 FieldList = DtScanFile (Gbl_Files[ASL_FILE_INPUT].Handle);
121 UtEndEvent (Event);
123 /* Did the parse tree get successfully constructed? */
125 if (!FieldList)
127 /* TBD: temporary error message. Msgs should come from function above */
129 DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
130 "Input file does not appear to be an ASL or data table source file");
132 Status = AE_ERROR;
133 goto CleanupAndExit;
136 Event = UtBeginEvent ("Compile parse tree");
139 * Compile the parse tree
141 Status = DtCompileDataTable (&FieldList);
142 UtEndEvent (Event);
144 DtFreeFieldList ();
146 if (ACPI_FAILURE (Status))
148 /* TBD: temporary error message. Msgs should come from function above */
150 DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
151 "Could not compile input file");
153 goto CleanupAndExit;
156 /* Create/open the binary output file */
158 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
159 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
160 if (ACPI_FAILURE (Status))
162 goto CleanupAndExit;
165 /* Write the binary, then the optional hex file */
167 DtOutputBinary (Gbl_RootTable);
168 HxDoHexOutput ();
169 DtWriteTableToListing ();
171 CleanupAndExit:
173 CmCleanupAndExit ();
174 return (Status);
178 /******************************************************************************
180 * FUNCTION: DtInitialize
182 * PARAMETERS: None
184 * RETURN: Status
186 * DESCRIPTION: Initialize data table compiler globals. Enables multiple
187 * compiles per invocation.
189 *****************************************************************************/
191 static ACPI_STATUS
192 DtInitialize (
193 void)
195 ACPI_STATUS Status;
198 Status = AcpiOsInitialize ();
199 if (ACPI_FAILURE (Status))
201 return (Status);
204 Status = AcpiUtInitGlobals ();
205 if (ACPI_FAILURE (Status))
207 return (Status);
210 Gbl_FieldList = NULL;
211 Gbl_RootTable = NULL;
212 Gbl_SubtableStack = NULL;
214 sprintf (VersionString, "%X", (UINT32) ACPI_CA_VERSION);
215 return (AE_OK);
219 /******************************************************************************
221 * FUNCTION: DtInsertCompilerIds
223 * PARAMETERS: FieldList - Current field list pointer
225 * RETURN: None
227 * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
228 * the original ACPI table header.
230 *****************************************************************************/
232 static void
233 DtInsertCompilerIds (
234 DT_FIELD *FieldList)
236 DT_FIELD *Next;
237 UINT32 i;
241 * Don't insert current compiler ID if requested. Used for compiler
242 * debug/validation only.
244 if (Gbl_UseOriginalCompilerId)
246 return;
249 /* Walk to the Compiler fields at the end of the header */
251 Next = FieldList;
252 for (i = 0; i < 7; i++)
254 Next = Next->Next;
257 Next->Value = ASL_CREATOR_ID;
258 Next->Flags = DT_FIELD_NOT_ALLOCATED;
260 Next = Next->Next;
261 Next->Value = VersionString;
262 Next->Flags = DT_FIELD_NOT_ALLOCATED;
266 /******************************************************************************
268 * FUNCTION: DtCompileDataTable
270 * PARAMETERS: FieldList - Current field list pointer
272 * RETURN: Status
274 * DESCRIPTION: Entry point to compile one data table
276 *****************************************************************************/
278 static ACPI_STATUS
279 DtCompileDataTable (
280 DT_FIELD **FieldList)
282 ACPI_DMTABLE_DATA *TableData;
283 DT_SUBTABLE *Subtable;
284 char *Signature;
285 ACPI_TABLE_HEADER *AcpiTableHeader;
286 ACPI_STATUS Status;
287 DT_FIELD *RootField = *FieldList;
290 /* Verify that we at least have a table signature and save it */
292 Signature = DtGetFieldValue (*FieldList);
293 if (!Signature)
295 sprintf (MsgBuffer, "Expected \"%s\"", "Signature");
296 DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
297 *FieldList, MsgBuffer);
298 return (AE_ERROR);
301 Gbl_Signature = UtLocalCalloc (ACPI_STRLEN (Signature) + 1);
302 strcpy (Gbl_Signature, Signature);
305 * Handle tables that don't use the common ACPI table header structure.
306 * Currently, these are the FACS and RSDP. Also check for an OEMx table,
307 * these tables have user-defined contents.
309 if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_FACS))
311 Status = DtCompileFacs (FieldList);
312 if (ACPI_FAILURE (Status))
314 return (Status);
317 DtSetTableLength ();
318 return (Status);
320 else if (ACPI_VALIDATE_RSDP_SIG (Signature))
322 Status = DtCompileRsdp (FieldList);
323 return (Status);
325 else if (ACPI_COMPARE_NAME (Signature, ACPI_SIG_S3PT))
327 Status = DtCompileS3pt (FieldList);
328 if (ACPI_FAILURE (Status))
330 return (Status);
333 DtSetTableLength ();
334 return (Status);
338 * All other tables must use the common ACPI table header. Insert the
339 * current iASL IDs (name, version), and compile the header now.
341 DtInsertCompilerIds (*FieldList);
343 Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
344 &Gbl_RootTable, TRUE);
345 if (ACPI_FAILURE (Status))
347 return (Status);
350 DtPushSubtable (Gbl_RootTable);
352 /* Validate the signature via the ACPI table list */
354 TableData = AcpiDmGetTableData (Signature);
355 if (!TableData || Gbl_CompileGeneric)
357 DtCompileGeneric ((void **) FieldList);
358 goto FinishHeader;
361 /* Dispatch to per-table compile */
363 if (TableData->CmTableHandler)
365 /* Complex table, has a handler */
367 Status = TableData->CmTableHandler ((void **) FieldList);
368 if (ACPI_FAILURE (Status))
370 return (Status);
373 else if (TableData->TableInfo)
375 /* Simple table, just walk the info table */
377 Subtable = NULL;
378 Status = DtCompileTable (FieldList, TableData->TableInfo,
379 &Subtable, TRUE);
380 if (ACPI_FAILURE (Status))
382 return (Status);
385 DtInsertSubtable (Gbl_RootTable, Subtable);
386 DtPopSubtable ();
388 else
390 DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
391 "Missing table dispatch info");
392 return (AE_ERROR);
395 FinishHeader:
397 /* Set the final table length and then the checksum */
399 DtSetTableLength ();
400 AcpiTableHeader = ACPI_CAST_PTR (
401 ACPI_TABLE_HEADER, Gbl_RootTable->Buffer);
402 DtSetTableChecksum (&AcpiTableHeader->Checksum);
404 DtDumpFieldList (RootField);
405 DtDumpSubtableList ();
406 return (AE_OK);
410 /******************************************************************************
412 * FUNCTION: DtCompileTable
414 * PARAMETERS: Field - Current field list pointer
415 * Info - Info table for this ACPI table
416 * RetSubtable - Compile result of table
417 * Required - If this subtable must exist
419 * RETURN: Status
421 * DESCRIPTION: Compile a subtable
423 *****************************************************************************/
425 ACPI_STATUS
426 DtCompileTable (
427 DT_FIELD **Field,
428 ACPI_DMTABLE_INFO *Info,
429 DT_SUBTABLE **RetSubtable,
430 BOOLEAN Required)
432 DT_FIELD *LocalField;
433 UINT32 Length;
434 DT_SUBTABLE *Subtable;
435 DT_SUBTABLE *InlineSubtable;
436 UINT32 FieldLength = 0;
437 UINT8 FieldType;
438 UINT8 *Buffer;
439 UINT8 *FlagBuffer = NULL;
440 UINT32 CurrentFlagByteOffset = 0;
441 ACPI_STATUS Status;
444 if (!Field || !*Field)
446 return (AE_BAD_PARAMETER);
449 Length = DtGetSubtableLength (*Field, Info);
450 if (Length == ASL_EOF)
452 return (AE_ERROR);
455 Subtable = UtLocalCalloc (sizeof (DT_SUBTABLE));
457 if (Length > 0)
459 Subtable->Buffer = UtLocalCalloc (Length);
461 Subtable->Length = Length;
462 Subtable->TotalLength = Length;
463 Buffer = Subtable->Buffer;
465 LocalField = *Field;
468 * Main loop walks the info table for this ACPI table or subtable
470 for (; Info->Name; Info++)
472 if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
474 continue;
477 if (!LocalField)
479 sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
480 Info->Name);
481 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
482 Status = AE_BAD_DATA;
483 goto Error;
486 /* Maintain table offsets */
488 LocalField->TableOffset = Gbl_CurrentTableOffset;
489 FieldLength = DtGetFieldLength (LocalField, Info);
490 Gbl_CurrentTableOffset += FieldLength;
492 FieldType = DtGetFieldType (Info);
493 Gbl_InputFieldCount++;
495 switch (FieldType)
497 case DT_FIELD_TYPE_FLAGS_INTEGER:
499 * Start of the definition of a flags field.
500 * This master flags integer starts at value zero, in preparation
501 * to compile and insert the flag fields from the individual bits
503 LocalField = LocalField->Next;
504 *Field = LocalField;
506 FlagBuffer = Buffer;
507 CurrentFlagByteOffset = Info->Offset;
508 break;
510 case DT_FIELD_TYPE_FLAG:
512 /* Individual Flag field, can be multiple bits */
514 if (FlagBuffer)
517 * We must increment the FlagBuffer when we have crossed
518 * into the next flags byte within the flags field
519 * of type DT_FIELD_TYPE_FLAGS_INTEGER.
521 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
522 CurrentFlagByteOffset = Info->Offset;
524 DtCompileFlag (FlagBuffer, LocalField, Info);
526 else
528 /* TBD - this is an internal error */
531 LocalField = LocalField->Next;
532 *Field = LocalField;
533 break;
535 case DT_FIELD_TYPE_INLINE_SUBTABLE:
537 * Recursion (one level max): compile GAS (Generic Address)
538 * or Notify in-line subtable
540 *Field = LocalField;
542 if (Info->Opcode == ACPI_DMT_GAS)
544 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
545 &InlineSubtable, TRUE);
547 else
549 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
550 &InlineSubtable, TRUE);
553 if (ACPI_FAILURE (Status))
555 goto Error;
558 DtSetSubtableLength (InlineSubtable);
560 ACPI_MEMCPY (Buffer, InlineSubtable->Buffer, FieldLength);
561 ACPI_FREE (InlineSubtable->Buffer);
562 ACPI_FREE (InlineSubtable);
563 LocalField = *Field;
564 break;
566 case DT_FIELD_TYPE_LABEL:
568 DtWriteFieldToListing (Buffer, LocalField, 0);
569 LocalField = LocalField->Next;
570 break;
572 default:
574 /* Normal case for most field types (Integer, String, etc.) */
576 DtCompileOneField (Buffer, LocalField,
577 FieldLength, FieldType, Info->Flags);
579 DtWriteFieldToListing (Buffer, LocalField, FieldLength);
580 LocalField = LocalField->Next;
582 if (Info->Flags & DT_LENGTH)
584 /* Field is an Integer that will contain a subtable length */
586 Subtable->LengthField = Buffer;
587 Subtable->SizeOfLengthField = FieldLength;
590 break;
593 Buffer += FieldLength;
596 *Field = LocalField;
597 *RetSubtable = Subtable;
598 return (AE_OK);
600 Error:
601 ACPI_FREE (Subtable->Buffer);
602 ACPI_FREE (Subtable);
603 return (Status);