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