Home | History | Annotate | Line # | Download | only in compiler
dtcompile.c revision 1.1.1.11
      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 
     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 Gbl_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 (Gbl_PreprocessFlag)
    105     {
    106         /* Preprocessor */
    107 
    108         Event = UtBeginEvent ("Preprocess input file");
    109         PrDoPreprocess ();
    110         UtEndEvent (Event);
    111 
    112         if (Gbl_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 (Gbl_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     Gbl_Files[ASL_FILE_AML_OUTPUT].Filename = NULL;
    160     Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
    161     if (ACPI_FAILURE (Status))
    162     {
    163         goto CleanupAndExit;
    164     }
    165 
    166     /* Write the binary, then the optional hex file */
    167 
    168     DtOutputBinary (Gbl_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     Gbl_FieldList = NULL;
    215     Gbl_RootTable = NULL;
    216     Gbl_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 (Gbl_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 (MsgBuffer, "Expected \"%s\"", "Signature");
    300         DtNameError (ASL_ERROR, ASL_MSG_INVALID_FIELD_NAME,
    301             *FieldList, MsgBuffer);
    302         return (AE_ERROR);
    303     }
    304 
    305     Gbl_Signature = UtLocalCacheCalloc (strlen (Signature) + 1);
    306     strcpy (Gbl_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         &Gbl_RootTable, TRUE);
    349     if (ACPI_FAILURE (Status))
    350     {
    351         return (Status);
    352     }
    353 
    354     DtPushSubtable (Gbl_RootTable);
    355 
    356     /* Validate the signature via the ACPI table list */
    357 
    358     TableData = AcpiDmGetTableData (Signature);
    359     if (!TableData || Gbl_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, TRUE);
    388             if (ACPI_FAILURE (Status))
    389             {
    390                 return (Status);
    391             }
    392 
    393             DtInsertSubtable (Gbl_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, Gbl_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  *              Required            - If this subtable must exist
    427  *
    428  * RETURN:      Status
    429  *
    430  * DESCRIPTION: Compile a subtable
    431  *
    432  *****************************************************************************/
    433 
    434 ACPI_STATUS
    435 DtCompileTable (
    436     DT_FIELD                **Field,
    437     ACPI_DMTABLE_INFO       *Info,
    438     DT_SUBTABLE             **RetSubtable,
    439     BOOLEAN                 Required)
    440 {
    441     DT_FIELD                *LocalField;
    442     UINT32                  Length;
    443     DT_SUBTABLE             *Subtable;
    444     DT_SUBTABLE             *InlineSubtable = NULL;
    445     UINT32                  FieldLength = 0;
    446     UINT8                   FieldType;
    447     UINT8                   *Buffer;
    448     UINT8                   *FlagBuffer = NULL;
    449     char                    *String;
    450     UINT32                  CurrentFlagByteOffset = 0;
    451     ACPI_STATUS             Status = AE_OK;
    452 
    453 
    454     if (!Field || !*Field)
    455     {
    456         return (AE_BAD_PARAMETER);
    457     }
    458 
    459     /* Ignore optional subtable if name does not match */
    460 
    461     if ((Info->Flags & DT_OPTIONAL) &&
    462         strcmp ((*Field)->Name, Info->Name))
    463     {
    464         *RetSubtable = NULL;
    465         return (AE_OK);
    466     }
    467 
    468     Length = DtGetSubtableLength (*Field, Info);
    469     if (Length == ASL_EOF)
    470     {
    471         return (AE_ERROR);
    472     }
    473 
    474     Subtable = UtSubtableCacheCalloc ();
    475 
    476     if (Length > 0)
    477     {
    478         String = UtLocalCacheCalloc (Length);
    479         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
    480     }
    481 
    482     Subtable->Length = Length;
    483     Subtable->TotalLength = Length;
    484     Buffer = Subtable->Buffer;
    485 
    486     LocalField = *Field;
    487     Subtable->Name = LocalField->Name;
    488 
    489     /*
    490      * Main loop walks the info table for this ACPI table or subtable
    491      */
    492     for (; Info->Name; Info++)
    493     {
    494         if (Info->Opcode == ACPI_DMT_EXTRA_TEXT)
    495         {
    496             continue;
    497         }
    498 
    499         if (!LocalField)
    500         {
    501             sprintf (MsgBuffer, "Found NULL field - Field name \"%s\" needed",
    502                 Info->Name);
    503             DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
    504             Status = AE_BAD_DATA;
    505             goto Error;
    506         }
    507 
    508         /* Maintain table offsets */
    509 
    510         LocalField->TableOffset = Gbl_CurrentTableOffset;
    511         FieldLength = DtGetFieldLength (LocalField, Info);
    512         Gbl_CurrentTableOffset += FieldLength;
    513 
    514         FieldType = DtGetFieldType (Info);
    515         Gbl_InputFieldCount++;
    516 
    517         switch (FieldType)
    518         {
    519         case DT_FIELD_TYPE_FLAGS_INTEGER:
    520             /*
    521              * Start of the definition of a flags field.
    522              * This master flags integer starts at value zero, in preparation
    523              * to compile and insert the flag fields from the individual bits
    524              */
    525             LocalField = LocalField->Next;
    526             *Field = LocalField;
    527 
    528             FlagBuffer = Buffer;
    529             CurrentFlagByteOffset = Info->Offset;
    530             break;
    531 
    532         case DT_FIELD_TYPE_FLAG:
    533 
    534             /* Individual Flag field, can be multiple bits */
    535 
    536             if (FlagBuffer)
    537             {
    538                 /*
    539                  * We must increment the FlagBuffer when we have crossed
    540                  * into the next flags byte within the flags field
    541                  * of type DT_FIELD_TYPE_FLAGS_INTEGER.
    542                  */
    543                 FlagBuffer += (Info->Offset - CurrentFlagByteOffset);
    544                 CurrentFlagByteOffset = Info->Offset;
    545 
    546                 DtCompileFlag (FlagBuffer, LocalField, Info);
    547             }
    548             else
    549             {
    550                 /* TBD - this is an internal error */
    551             }
    552 
    553             LocalField = LocalField->Next;
    554             *Field = LocalField;
    555             break;
    556 
    557         case DT_FIELD_TYPE_INLINE_SUBTABLE:
    558             /*
    559              * Recursion (one level max): compile GAS (Generic Address)
    560              * or Notify in-line subtable
    561              */
    562             *Field = LocalField;
    563 
    564             switch (Info->Opcode)
    565             {
    566             case ACPI_DMT_GAS:
    567 
    568                 Status = DtCompileTable (Field, AcpiDmTableInfoGas,
    569                     &InlineSubtable, TRUE);
    570                 break;
    571 
    572             case ACPI_DMT_HESTNTFY:
    573 
    574                 Status = DtCompileTable (Field, AcpiDmTableInfoHestNotify,
    575                     &InlineSubtable, TRUE);
    576                 break;
    577 
    578             case ACPI_DMT_IORTMEM:
    579 
    580                 Status = DtCompileTable (Field, AcpiDmTableInfoIortAcc,
    581                     &InlineSubtable, TRUE);
    582                 break;
    583 
    584             default:
    585                 sprintf (MsgBuffer, "Invalid DMT opcode: 0x%.2X",
    586                     Info->Opcode);
    587                 DtFatal (ASL_MSG_COMPILER_INTERNAL, NULL, MsgBuffer);
    588                 Status = AE_BAD_DATA;
    589                 break;
    590             }
    591 
    592             if (ACPI_FAILURE (Status))
    593             {
    594                 goto Error;
    595             }
    596 
    597             DtSetSubtableLength (InlineSubtable);
    598 
    599             memcpy (Buffer, InlineSubtable->Buffer, FieldLength);
    600             LocalField = *Field;
    601             break;
    602 
    603         case DT_FIELD_TYPE_LABEL:
    604 
    605             DtWriteFieldToListing (Buffer, LocalField, 0);
    606             LocalField = LocalField->Next;
    607             break;
    608 
    609         default:
    610 
    611             /* Normal case for most field types (Integer, String, etc.) */
    612 
    613             DtCompileOneField (Buffer, LocalField,
    614                 FieldLength, FieldType, Info->Flags);
    615 
    616             DtWriteFieldToListing (Buffer, LocalField, FieldLength);
    617             LocalField = LocalField->Next;
    618 
    619             if (Info->Flags & DT_LENGTH)
    620             {
    621                 /* Field is an Integer that will contain a subtable length */
    622 
    623                 Subtable->LengthField = Buffer;
    624                 Subtable->SizeOfLengthField = FieldLength;
    625             }
    626             break;
    627         }
    628 
    629         Buffer += FieldLength;
    630     }
    631 
    632     *Field = LocalField;
    633     *RetSubtable = Subtable;
    634     return (AE_OK);
    635 
    636 Error:
    637     ACPI_FREE (Subtable->Buffer);
    638     ACPI_FREE (Subtable);
    639     return (Status);
    640 }
    641 
    642 
    643 /******************************************************************************
    644  *
    645  * FUNCTION:    DtCompileTwoSubtables
    646  *
    647  * PARAMETERS:  List                - Current field list pointer
    648  *              TableInfo1          - Info table 1
    649  *              TableInfo1          - Info table 2
    650  *
    651  * RETURN:      Status
    652  *
    653  * DESCRIPTION: Compile tables with a header and one or more same subtables.
    654  *              Include CPEP, EINJ, ERST, MCFG, MSCT, WDAT
    655  *
    656  *****************************************************************************/
    657 
    658 ACPI_STATUS
    659 DtCompileTwoSubtables (
    660     void                    **List,
    661     ACPI_DMTABLE_INFO       *TableInfo1,
    662     ACPI_DMTABLE_INFO       *TableInfo2)
    663 {
    664     ACPI_STATUS             Status;
    665     DT_SUBTABLE             *Subtable;
    666     DT_SUBTABLE             *ParentTable;
    667     DT_FIELD                **PFieldList = (DT_FIELD **) List;
    668 
    669 
    670     Status = DtCompileTable (PFieldList, TableInfo1, &Subtable, TRUE);
    671     if (ACPI_FAILURE (Status))
    672     {
    673         return (Status);
    674     }
    675 
    676     ParentTable = DtPeekSubtable ();
    677     DtInsertSubtable (ParentTable, Subtable);
    678 
    679     while (*PFieldList)
    680     {
    681         Status = DtCompileTable (PFieldList, TableInfo2, &Subtable, FALSE);
    682         if (ACPI_FAILURE (Status))
    683         {
    684             return (Status);
    685         }
    686 
    687         DtInsertSubtable (ParentTable, Subtable);
    688     }
    689 
    690     return (AE_OK);
    691 }
    692 
    693 
    694 /******************************************************************************
    695  *
    696  * FUNCTION:    DtCompilePadding
    697  *
    698  * PARAMETERS:  Length              - Padding field size
    699  *              RetSubtable         - Compile result of table
    700  *
    701  * RETURN:      Status
    702  *
    703  * DESCRIPTION: Compile a subtable for padding purpose
    704  *
    705  *****************************************************************************/
    706 
    707 ACPI_STATUS
    708 DtCompilePadding (
    709     UINT32                  Length,
    710     DT_SUBTABLE             **RetSubtable)
    711 {
    712     DT_SUBTABLE             *Subtable;
    713     /* UINT8                   *Buffer; */
    714     char                    *String;
    715 
    716 
    717     Subtable = UtSubtableCacheCalloc ();
    718 
    719     if (Length > 0)
    720     {
    721         String = UtLocalCacheCalloc (Length);
    722         Subtable->Buffer = ACPI_CAST_PTR (UINT8, String);
    723     }
    724 
    725     Subtable->Length = Length;
    726     Subtable->TotalLength = Length;
    727     /* Buffer = Subtable->Buffer; */
    728 
    729     *RetSubtable = Subtable;
    730     return (AE_OK);
    731 }
    732