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