Home | History | Annotate | Line # | Download | only in compiler
dtcompile.c revision 1.13
      1 /******************************************************************************
      2  *
      3  * Module Name: dtcompile.c - Front-end for data table compiler
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2019, 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 
     48 #define _COMPONENT          DT_COMPILER
     49         ACPI_MODULE_NAME    ("dtcompile")
     50 
     51 static char                 VersionString[9];
     52 
     53 
     54 /* Local prototypes */
     55 
     56 static ACPI_STATUS
     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 
     92 
     93     /* Initialize globals */
     94 
     95     Status = DtInitialize ();
     96     if (ACPI_FAILURE (Status))
     97     {
     98         printf ("Error during compiler initialization, 0x%X\n", Status);
     99         return (Status);
    100     }
    101 
    102     /* Preprocessor */
    103 
    104     if (AslGbl_PreprocessFlag)
    105     {
    106         /* Preprocessor */
    107 
    108         Event = UtBeginEvent ("Preprocess input file");
    109         PrDoPreprocess ();
    110         UtEndEvent (Event);
    111 
    112         if (AslGbl_PreprocessOnly)
    113         {
    114             return (AE_OK);
    115         }
    116     }
    117 
    118     /*
    119      * Scan the input file (file is already open) and
    120      * build the parse tree
    121      */
    122     Event = UtBeginEvent ("Scan and parse input file");
    123     FieldList = DtScanFile (AslGbl_Files[ASL_FILE_INPUT].Handle);
    124     UtEndEvent (Event);
    125 
    126     /* Did the parse tree get successfully constructed? */
    127 
    128     if (!FieldList)
    129     {
    130         /* TBD: temporary error message. Msgs should come from function above */
    131 
    132         DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
    133             "Input file does not appear to be an ASL or data table source file");
    134 
    135         return (AE_ERROR);
    136     }
    137 
    138     Event = UtBeginEvent ("Compile parse tree");
    139 
    140     /*
    141      * Compile the parse tree
    142      */
    143     Status = DtCompileDataTable (&FieldList);
    144     UtEndEvent (Event);
    145 
    146     if (ACPI_FAILURE (Status))
    147     {
    148         /* TBD: temporary error message. Msgs should come from function above */
    149 
    150         DtError (ASL_ERROR, ASL_MSG_SYNTAX, NULL,
    151             "Could not compile input file");
    152 
    153         return (Status);
    154     }
    155 
    156     /* Create/open the binary output file */
    157 
    158     AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
    159     Status = FlOpenAmlOutputFile (AslGbl_OutputFilenamePrefix);
    160     if (ACPI_FAILURE (Status))
    161     {
    162         return (Status);
    163     }
    164 
    165     /* Write the binary, then the optional hex file */
    166 
    167     DtOutputBinary (AslGbl_RootTable);
    168     HxDoHexOutput ();
    169     DtWriteTableToListing ();
    170 
    171     return (Status);
    172 }
    173 
    174 
    175 /******************************************************************************
    176  *
    177  * FUNCTION:    DtInitialize
    178  *
    179  * PARAMETERS:  None
    180  *
    181  * RETURN:      Status
    182  *
    183  * DESCRIPTION: Initialize data table compiler globals. Enables multiple
    184  *              compiles per invocation.
    185  *
    186  *****************************************************************************/
    187 
    188 static ACPI_STATUS
    189 DtInitialize (
    190     void)
    191 {
    192     ACPI_STATUS             Status;
    193 
    194 
    195     Status = AcpiOsInitialize ();
    196     if (ACPI_FAILURE (Status))
    197     {
    198         return (Status);
    199     }
    200 
    201     Status = AcpiUtInitGlobals ();
    202     if (ACPI_FAILURE (Status))
    203     {
    204         return (Status);
    205     }
    206 
    207     AcpiUtSetIntegerWidth (2); /* Set width to 64 bits */
    208 
    209     AslGbl_FieldList = NULL;
    210     AslGbl_RootTable = NULL;
    211     AslGbl_SubtableStack = NULL;
    212 
    213     snprintf (VersionString, sizeof(VersionString), "%X",
    214 	(UINT32) ACPI_CA_VERSION);
    215     return (AE_OK);
    216 }
    217 
    218 
    219 /******************************************************************************
    220  *
    221  * FUNCTION:    DtInsertCompilerIds
    222  *
    223  * PARAMETERS:  FieldList           - Current field list pointer
    224  *
    225  * RETURN:      None
    226  *
    227  * DESCRIPTION: Insert the IDs (Name, Version) of the current compiler into
    228  *              the original ACPI table header.
    229  *
    230  *****************************************************************************/
    231 
    232 static void
    233 DtInsertCompilerIds (
    234     DT_FIELD                *FieldList)
    235 {
    236     DT_FIELD                *Next;
    237     UINT32                  i;
    238 
    239 
    240     /*
    241      * Don't insert current compiler ID if requested. Used for compiler
    242      * debug/validation only.
    243      */
    244     if (AslGbl_UseOriginalCompilerId)
    245     {
    246         return;
    247     }
    248 
    249     /* Walk to the Compiler fields at the end of the header */
    250 
    251     Next = FieldList;
    252     for (i = 0; i < 7; i++)
    253     {
    254         Next = Next->Next;
    255     }
    256 
    257     Next->Value = ASL_CREATOR_ID;
    258     Next->Flags = DT_FIELD_NOT_ALLOCATED;
    259 
    260     Next = Next->Next;
    261     Next->Value = VersionString;
    262     Next->Flags = DT_FIELD_NOT_ALLOCATED;
    263 }
    264 
    265 
    266 /******************************************************************************
    267  *
    268  * FUNCTION:    DtCompileDataTable
    269  *
    270  * PARAMETERS:  FieldList           - Current field list pointer
    271  *
    272  * RETURN:      Status
    273  *
    274  * DESCRIPTION: Entry point to compile one data table
    275  *
    276  *****************************************************************************/
    277 
    278 static ACPI_STATUS
    279 DtCompileDataTable (
    280     DT_FIELD                **FieldList)
    281 {
    282     const 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;
    288 
    289 
    290     /* Verify that we at least have a table signature and save it */
    291 
    292     Signature = DtGetFieldValue (*FieldList);
    293     if (!Signature)
    294     {
    295         snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "Expected \"%s\"", "Signature");
    296         DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
    297             *FieldList, AslGbl_MsgBuffer);
    298         return (AE_ERROR);
    299     }
    300 
    301     AslGbl_Signature = UtLocalCacheCalloc (strlen (Signature) + 1);
    302     strcpy (AslGbl_Signature, Signature);
    303 
    304     /*
    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.
    308      */
    309     if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_FACS))
    310     {
    311         Status = DtCompileFacs (FieldList);
    312         if (ACPI_FAILURE (Status))
    313         {
    314             return (Status);
    315         }
    316 
    317         DtSetTableLength ();
    318         return (Status);
    319     }
    320     else if (ACPI_VALIDATE_RSDP_SIG (Signature))
    321     {
    322         Status = DtCompileRsdp (FieldList);
    323         return (Status);
    324     }
    325     else if (ACPI_COMPARE_NAMESEG (Signature, ACPI_SIG_S3PT))
    326     {
    327         Status = DtCompileS3pt (FieldList);
    328         if (ACPI_FAILURE (Status))
    329         {
    330             return (Status);
    331         }
    332 
    333         DtSetTableLength ();
    334         return (Status);
    335     }
    336 
    337     /*
    338      * All other tables must use the common ACPI table header. Insert the
    339      * current iASL IDs (name, version), and compile the header now.
    340      */
    341     DtInsertCompilerIds (*FieldList);
    342 
    343     Status = DtCompileTable (FieldList, AcpiDmTableInfoHeader,
    344         &AslGbl_RootTable);
    345     if (ACPI_FAILURE (Status))
    346     {
    347         return (Status);
    348     }
    349 
    350     DtPushSubtable (AslGbl_RootTable);
    351 
    352     /* Validate the signature via the ACPI table list */
    353 
    354     TableData = AcpiDmGetTableData (Signature);
    355     if (!TableData || AslGbl_CompileGeneric)
    356     {
    357         /* Unknown table signature and/or force generic compile */
    358 
    359         DtCompileGeneric ((void **) FieldList, NULL, NULL);
    360         goto FinishHeader;
    361     }
    362 
    363     /* Dispatch to per-table compile */
    364 
    365     if (TableData->CmTableHandler)
    366     {
    367         /* Complex table, has a handler */
    368 
    369         Status = TableData->CmTableHandler ((void **) FieldList);
    370         if (ACPI_FAILURE (Status))
    371         {
    372             return (Status);
    373         }
    374     }
    375     else if (TableData->TableInfo)
    376     {
    377         /* Simple table, just walk the info table, unless its empty */
    378 
    379         if (FieldList && *FieldList)
    380         {
    381             Subtable = NULL;
    382             Status = DtCompileTable (FieldList, TableData->TableInfo,
    383                 &Subtable);
    384             if (ACPI_FAILURE (Status))
    385             {
    386                 return (Status);
    387             }
    388 
    389             DtInsertSubtable (AslGbl_RootTable, Subtable);
    390             DtPopSubtable ();
    391         }
    392     }
    393     else
    394     {
    395         DtFatal (ASL_MSG_COMPILER_INTERNAL, *FieldList,
    396             "Missing table dispatch info");
    397         return (AE_ERROR);
    398     }
    399 
    400 FinishHeader:
    401 
    402     /* Set the final table length and then the checksum */
    403 
    404     DtSetTableLength ();
    405     AcpiTableHeader = ACPI_CAST_PTR (
    406         ACPI_TABLE_HEADER, AslGbl_RootTable->Buffer);
    407     DtSetTableChecksum (&AcpiTableHeader->Checksum);
    408 
    409     DtDumpFieldList (RootField);
    410     DtDumpSubtableList ();
    411     return (AE_OK);
    412 }
    413 
    414 
    415 /******************************************************************************
    416  *
    417  * FUNCTION:    DtCompileTable
    418  *
    419  * PARAMETERS:  Field               - Current field list pointer
    420  *              Info                - Info table for this ACPI table
    421  *              RetSubtable         - Compile result of table
    422  *
    423  * RETURN:      Status
    424  *
    425  * DESCRIPTION: Compile a subtable
    426  *
    427  *****************************************************************************/
    428 
    429 ACPI_STATUS
    430 DtCompileTable (
    431     DT_FIELD                **Field,
    432     ACPI_DMTABLE_INFO       *Info,
    433     DT_SUBTABLE             **RetSubtable)
    434 {
    435     DT_FIELD                *LocalField;
    436     UINT32                  Length;
    437     DT_SUBTABLE             *Subtable;
    438     DT_SUBTABLE             *InlineSubtable = NULL;
    439     UINT32                  FieldLength = 0;
    440     UINT8                   FieldType;
    441     UINT8                   *Buffer;
    442     UINT8                   *FlagBuffer = NULL;
    443     char                    *String;
    444     UINT32                  CurrentFlagByteOffset = 0;
    445     ACPI_STATUS             Status = AE_OK;
    446 
    447 
    448     if (!Field)
    449     {
    450         return (AE_BAD_PARAMETER);
    451     }
    452     if (!*Field)
    453     {
    454         /*
    455          * The field list is empty, this means that we are out of fields to
    456          * parse. In other words, we are at the end of the table.
    457          */
    458         return (AE_END_OF_TABLE);
    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 = UtLocalCacheCalloc (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             snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "Found NULL field - Field name \"%s\" needed",
    504                 Info->Name);
    505             DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, AslGbl_MsgBuffer);
    506             Status = AE_BAD_DATA;
    507             goto Error;
    508         }
    509 
    510         /* Maintain table offsets */
    511 
    512         LocalField->TableOffset = AslGbl_CurrentTableOffset;
    513         FieldLength = DtGetFieldLength (LocalField, Info);
    514         AslGbl_CurrentTableOffset += FieldLength;
    515 
    516         FieldType = DtGetFieldType (Info);
    517         AslGbl_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);
    572                 break;
    573 
    574             case ACPI_DMT_HESTNTFY:
    575 
    576                 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
    577                     &InlineSubtable);
    578                 break;
    579 
    580             case ACPI_DMT_IORTMEM:
    581 
    582                 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
    583                     &InlineSubtable);
    584                 break;
    585 
    586             default:
    587                 sprintf (AslGbl_MsgBuffer, "Invalid DMT opcode: 0x%.2X",
    588                     Info->Opcode);
    589                 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, AslGbl_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);
    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);
    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 = UtLocalCacheCalloc (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