Home | History | Annotate | Line # | Download | only in compiler
dtfield.c revision 1.11.2.2
      1 /******************************************************************************
      2  *
      3  * Module Name: dtfield.c - Code generation for individual source fields
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2020, 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 #include "aslcompiler.h"
     45 
     46 #define _COMPONENT          DT_COMPILER
     47         ACPI_MODULE_NAME    ("dtfield")
     48 
     49 
     50 /* Local prototypes */
     51 
     52 static void
     53 DtCompileString (
     54     UINT8                   *Buffer,
     55     DT_FIELD                *Field,
     56     UINT32                  ByteLength);
     57 
     58 static void
     59 DtCompileUnicode (
     60     UINT8                   *Buffer,
     61     DT_FIELD                *Field,
     62     UINT32                  ByteLength);
     63 
     64 static ACPI_STATUS
     65 DtCompileUuid (
     66     UINT8                   *Buffer,
     67     DT_FIELD                *Field,
     68     UINT32                  ByteLength);
     69 
     70 static char *
     71 DtNormalizeBuffer (
     72     char                    *Buffer,
     73     UINT32                  *Count);
     74 
     75 
     76 /******************************************************************************
     77  *
     78  * FUNCTION:    DtCompileOneField
     79  *
     80  * PARAMETERS:  Buffer              - Output buffer
     81  *              Field               - Field to be compiled
     82  *              ByteLength          - Byte length of the field
     83  *              Type                - Field type
     84  *
     85  * RETURN:      None
     86  *
     87  * DESCRIPTION: Compile a field value to binary
     88  *
     89  *****************************************************************************/
     90 
     91 void
     92 DtCompileOneField (
     93     UINT8                   *Buffer,
     94     DT_FIELD                *Field,
     95     UINT32                  ByteLength,
     96     UINT8                   Type,
     97     UINT8                   Flags)
     98 {
     99     ACPI_STATUS             Status;
    100 
    101 
    102     switch (Type)
    103     {
    104     case DT_FIELD_TYPE_INTEGER:
    105 
    106         DtCompileInteger (Buffer, Field, ByteLength, Flags);
    107         break;
    108 
    109     case DT_FIELD_TYPE_STRING:
    110 
    111         DtCompileString (Buffer, Field, ByteLength);
    112         break;
    113 
    114     case DT_FIELD_TYPE_UUID:
    115 
    116         Status = DtCompileUuid (Buffer, Field, ByteLength);
    117         if (ACPI_SUCCESS (Status))
    118         {
    119             break;
    120         }
    121 
    122         /* Fall through. */
    123 
    124     case DT_FIELD_TYPE_BUFFER:
    125 
    126         DtCompileBuffer (Buffer, Field->Value, Field, ByteLength);
    127         break;
    128 
    129     case DT_FIELD_TYPE_UNICODE:
    130 
    131         DtCompileUnicode (Buffer, Field, ByteLength);
    132         break;
    133 
    134     case DT_FIELD_TYPE_DEVICE_PATH:
    135 
    136         break;
    137 
    138     default:
    139 
    140         DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid field type");
    141         break;
    142     }
    143 }
    144 
    145 
    146 /******************************************************************************
    147  *
    148  * FUNCTION:    DtCompileString
    149  *
    150  * PARAMETERS:  Buffer              - Output buffer
    151  *              Field               - String to be copied to buffer
    152  *              ByteLength          - Maximum length of string
    153  *
    154  * RETURN:      None
    155  *
    156  * DESCRIPTION: Copy string to the buffer
    157  *
    158  *****************************************************************************/
    159 
    160 static void
    161 DtCompileString (
    162     UINT8                   *Buffer,
    163     DT_FIELD                *Field,
    164     UINT32                  ByteLength)
    165 {
    166     UINT32                  Length;
    167 
    168 
    169     Length = strlen (Field->Value);
    170 
    171     /* Check if the string is too long for the field */
    172 
    173     if (Length > ByteLength)
    174     {
    175         snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "Maximum %u characters", ByteLength);
    176         DtError (ASL_ERROR, ASL_MSG_STRING_LENGTH, Field, AslGbl_MsgBuffer);
    177         Length = ByteLength;
    178     }
    179 
    180     memcpy (Buffer, Field->Value, Length);
    181 }
    182 
    183 
    184 /******************************************************************************
    185  *
    186  * FUNCTION:    DtCompileUnicode
    187  *
    188  * PARAMETERS:  Buffer              - Output buffer
    189  *              Field               - String to be copied to buffer
    190  *              ByteLength          - Maximum length of string
    191  *
    192  * RETURN:      None
    193  *
    194  * DESCRIPTION: Convert ASCII string to Unicode string
    195  *
    196  * Note:  The Unicode string is 16 bits per character, no leading signature,
    197  *        with a 16-bit terminating NULL.
    198  *
    199  *****************************************************************************/
    200 
    201 static void
    202 DtCompileUnicode (
    203     UINT8                   *Buffer,
    204     DT_FIELD                *Field,
    205     UINT32                  ByteLength)
    206 {
    207     UINT32                  Count;
    208     UINT32                  i;
    209     char                    *AsciiString;
    210     UINT16                  *UnicodeString;
    211 
    212 
    213     AsciiString = Field->Value;
    214     UnicodeString = (UINT16 *) Buffer;
    215     Count = strlen (AsciiString) + 1;
    216 
    217     /* Convert to Unicode string (including null terminator) */
    218 
    219     for (i = 0; i < Count; i++)
    220     {
    221         UnicodeString[i] = (UINT16) AsciiString[i];
    222     }
    223 }
    224 
    225 
    226 /*******************************************************************************
    227  *
    228  * FUNCTION:    DtCompileUuid
    229  *
    230  * PARAMETERS:  Buffer              - Output buffer
    231  *              Field               - String to be copied to buffer
    232  *              ByteLength          - Maximum length of string
    233  *
    234  * RETURN:      None
    235  *
    236  * DESCRIPTION: Convert UUID string to 16-byte buffer
    237  *
    238  ******************************************************************************/
    239 
    240 static ACPI_STATUS
    241 DtCompileUuid (
    242     UINT8                   *Buffer,
    243     DT_FIELD                *Field,
    244     UINT32                  ByteLength)
    245 {
    246     char                    *InString;
    247     ACPI_STATUS             Status;
    248 
    249 
    250     InString = Field->Value;
    251 
    252     Status = AuValidateUuid (InString);
    253     if (ACPI_FAILURE (Status))
    254     {
    255         snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "%s", Field->Value);
    256         DtNameError (ASL_ERROR, ASL_MSG_INVALID_UUID, Field, AslGbl_MsgBuffer);
    257     }
    258     else
    259     {
    260         AcpiUtConvertStringToUuid (InString, Buffer);
    261     }
    262 
    263     return (Status);
    264 }
    265 
    266 
    267 /******************************************************************************
    268  *
    269  * FUNCTION:    DtCompileInteger
    270  *
    271  * PARAMETERS:  Buffer              - Output buffer
    272  *              Field               - Field obj with Integer to be compiled
    273  *              ByteLength          - Byte length of the integer
    274  *              Flags               - Additional compile info
    275  *
    276  * RETURN:      None
    277  *
    278  * DESCRIPTION: Compile an integer. Supports integer expressions with C-style
    279  *              operators.
    280  *
    281  *****************************************************************************/
    282 
    283 void
    284 DtCompileInteger (
    285     UINT8                   *Buffer,
    286     DT_FIELD                *Field,
    287     UINT32                  ByteLength,
    288     UINT8                   Flags)
    289 {
    290     UINT64                  Value;
    291     UINT64                  MaxValue;
    292     ACPI_STATUS             Status;
    293 
    294 
    295     /* Output buffer byte length must be in range 1-8 */
    296 
    297     if ((ByteLength > 8) || (ByteLength == 0))
    298     {
    299         DtFatal (ASL_MSG_COMPILER_INTERNAL, Field,
    300             "Invalid internal Byte length");
    301         return;
    302     }
    303 
    304     /* Resolve integer expression to a single integer value */
    305 
    306     Status = DtResolveIntegerExpression (Field, &Value);
    307     if (ACPI_FAILURE (Status))
    308     {
    309         return;
    310     }
    311 
    312     /*
    313      * Ensure that reserved fields are set properly. Note: uses
    314      * the DT_NON_ZERO flag to indicate that the reserved value
    315      * must be exactly one. Otherwise, the value must be zero.
    316      * This is sufficient for now.
    317      */
    318 
    319     /* TBD: Should use a flag rather than compare "Reserved" */
    320 
    321     if (!strcmp (Field->Name, "Reserved"))
    322     {
    323         if (Flags & DT_NON_ZERO)
    324         {
    325             if (Value != 1)
    326             {
    327                 DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
    328                     "Must be one, setting to one");
    329                 Value = 1;
    330             }
    331         }
    332         else if (Value != 0)
    333         {
    334             DtError (ASL_WARNING, ASL_MSG_RESERVED_VALUE, Field,
    335                 "Must be zero, setting to zero");
    336             Value = 0;
    337         }
    338     }
    339 
    340     /* Check if the value must be non-zero */
    341 
    342     else if ((Flags & DT_NON_ZERO) && (Value == 0))
    343     {
    344         DtError (ASL_ERROR, ASL_MSG_ZERO_VALUE, Field, NULL);
    345     }
    346 
    347     /*
    348      * Generate the maximum value for the data type (ByteLength)
    349      * Note: construct chosen for maximum portability
    350      */
    351     MaxValue = ((UINT64) (-1)) >> (64 - (ByteLength * 8));
    352 
    353     /* Validate that the input value is within range of the target */
    354 
    355     if (Value > MaxValue)
    356     {
    357         snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "%8.8X%8.8X - max %u bytes",
    358             ACPI_FORMAT_UINT64 (Value), ByteLength);
    359         DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, AslGbl_MsgBuffer);
    360     }
    361 
    362     memcpy (Buffer, &Value, ByteLength);
    363     return;
    364 }
    365 
    366 
    367 /******************************************************************************
    368  *
    369  * FUNCTION:    DtNormalizeBuffer
    370  *
    371  * PARAMETERS:  Buffer              - Input buffer
    372  *              Count               - Output the count of hex numbers in
    373  *                                    the Buffer
    374  *
    375  * RETURN:      The normalized buffer, must be freed by caller
    376  *
    377  * DESCRIPTION: [1A,2B,3C,4D] or 1A, 2B, 3C, 4D will be normalized
    378  *              to 1A 2B 3C 4D
    379  *
    380  *****************************************************************************/
    381 
    382 static char *
    383 DtNormalizeBuffer (
    384     char                    *Buffer,
    385     UINT32                  *Count)
    386 {
    387     char                    *NewBuffer;
    388     char                    *TmpBuffer;
    389     UINT32                  BufferCount = 0;
    390     BOOLEAN                 Separator = TRUE;
    391     char                    c;
    392 
    393 
    394     NewBuffer = UtLocalCalloc (strlen (Buffer) + 1);
    395     TmpBuffer = NewBuffer;
    396 
    397     while ((c = *Buffer++))
    398     {
    399         switch (c)
    400         {
    401         /* Valid separators */
    402 
    403         case '[':
    404         case ']':
    405         case ' ':
    406         case ',':
    407 
    408             Separator = TRUE;
    409             break;
    410 
    411         default:
    412 
    413             if (Separator)
    414             {
    415                 /* Insert blank as the standard separator */
    416 
    417                 if (NewBuffer[0])
    418                 {
    419                     *TmpBuffer++ = ' ';
    420                     BufferCount++;
    421                 }
    422 
    423                 Separator = FALSE;
    424             }
    425 
    426             *TmpBuffer++ = c;
    427             break;
    428         }
    429     }
    430 
    431     *Count = BufferCount + 1;
    432     return (NewBuffer);
    433 }
    434 
    435 
    436 /******************************************************************************
    437  *
    438  * FUNCTION:    DtCompileBuffer
    439  *
    440  * PARAMETERS:  Buffer              - Output buffer
    441  *              StringValue         - Integer list to be compiled
    442  *              Field               - Current field object
    443  *              ByteLength          - Byte length of the integer list
    444  *
    445  * RETURN:      Count of remaining data in the input list
    446  *
    447  * DESCRIPTION: Compile and pack an integer list, for example
    448  *              "AA 1F 20 3B" ==> Buffer[] = {0xAA,0x1F,0x20,0x3B}
    449  *
    450  *****************************************************************************/
    451 
    452 UINT32
    453 DtCompileBuffer (
    454     UINT8                   *Buffer,
    455     char                    *StringValue,
    456     DT_FIELD                *Field,
    457     UINT32                  ByteLength)
    458 {
    459     char                    *Substring;
    460     ACPI_STATUS             Status;
    461     UINT32                  Count;
    462     UINT32                  i;
    463 
    464 
    465     /* Allow several different types of value separators */
    466 
    467     StringValue = DtNormalizeBuffer (StringValue, &Count);
    468     Substring = StringValue;
    469     if (Count != ByteLength)
    470     {
    471         sprintf(AslGbl_MsgBuffer,
    472             "Found %u values, must match expected count: %u",
    473             Count, ByteLength);
    474         DtError (ASL_ERROR, ASL_MSG_BUFFER_LIST, Field, AslGbl_MsgBuffer);
    475         goto Exit;
    476     }
    477 
    478     /* Each element of StringValue is now three chars (2 hex + 1 space) */
    479 
    480     for (i = 0; i < Count; i++, Substring += 3)
    481     {
    482         /* Check for byte value too long */
    483 
    484         if (*(&Substring[2]) &&
    485            (*(&Substring[2]) != ' '))
    486         {
    487             DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
    488             goto Exit;
    489         }
    490 
    491         /* Convert two ASCII characters to one hex byte */
    492 
    493         Status = AcpiUtAsciiToHexByte (Substring, &Buffer[i]);
    494         if (ACPI_FAILURE (Status))
    495         {
    496             DtError (ASL_ERROR, ASL_MSG_BUFFER_ELEMENT, Field, Substring);
    497             goto Exit;
    498         }
    499     }
    500 
    501 Exit:
    502     ACPI_FREE (StringValue);
    503     return (ByteLength - Count);
    504 }
    505 
    506 
    507 /******************************************************************************
    508  *
    509  * FUNCTION:    DtCompileFlag
    510  *
    511  * PARAMETERS:  Buffer                      - Output buffer
    512  *              Field                       - Field to be compiled
    513  *              Info                        - Flag info
    514  *
    515  * RETURN:      None
    516  *
    517  * DESCRIPTION: Compile a flag field. Handles flags up to 64 bits.
    518  *
    519  *****************************************************************************/
    520 
    521 void
    522 DtCompileFlag (
    523     UINT8                   *Buffer,
    524     DT_FIELD                *Field,
    525     ACPI_DMTABLE_INFO       *Info)
    526 {
    527     UINT64                  Value = 0;
    528     UINT32                  BitLength = 1;
    529     UINT8                   BitPosition = 0;
    530 
    531 
    532     Value = AcpiUtImplicitStrtoul64 (Field->Value);
    533 
    534     switch (Info->Opcode)
    535     {
    536     case ACPI_DMT_FLAG0:
    537     case ACPI_DMT_FLAG1:
    538     case ACPI_DMT_FLAG2:
    539     case ACPI_DMT_FLAG3:
    540     case ACPI_DMT_FLAG4:
    541     case ACPI_DMT_FLAG5:
    542     case ACPI_DMT_FLAG6:
    543     case ACPI_DMT_FLAG7:
    544 
    545         BitPosition = Info->Opcode;
    546         BitLength = 1;
    547         break;
    548 
    549     case ACPI_DMT_FLAGS0:
    550 
    551         BitPosition = 0;
    552         BitLength = 2;
    553         break;
    554 
    555 
    556     case ACPI_DMT_FLAGS1:
    557 
    558         BitPosition = 1;
    559         BitLength = 2;
    560         break;
    561 
    562 
    563     case ACPI_DMT_FLAGS2:
    564 
    565         BitPosition = 2;
    566         BitLength = 2;
    567         break;
    568 
    569     case ACPI_DMT_FLAGS4:
    570 
    571         BitPosition = 4;
    572         BitLength = 2;
    573         break;
    574 
    575     case ACPI_DMT_FLAGS4_0:
    576 
    577         BitPosition = 0;
    578         BitLength = 4;
    579         break;
    580 
    581     case ACPI_DMT_FLAGS4_4:
    582 
    583         BitPosition = 4;
    584         BitLength = 4;
    585         break;
    586 
    587     case ACPI_DMT_FLAGS4_8:
    588 
    589         BitPosition = 8;
    590         BitLength = 4;
    591         break;
    592 
    593     case ACPI_DMT_FLAGS4_12:
    594 
    595         BitPosition = 12;
    596         BitLength = 4;
    597         break;
    598 
    599     case ACPI_DMT_FLAGS16_16:
    600 
    601         BitPosition = 16;
    602         BitLength = 16;
    603         break;
    604 
    605     default:
    606 
    607         DtFatal (ASL_MSG_COMPILER_INTERNAL, Field, "Invalid flag opcode");
    608         break;
    609     }
    610 
    611     /* Check range of the input flag value */
    612 
    613     if (Value >= ((UINT64) 1 << BitLength))
    614     {
    615         snprintf (AslGbl_MsgBuffer, sizeof(AslGbl_MsgBuffer), "Maximum %u bit", BitLength);
    616         DtError (ASL_ERROR, ASL_MSG_FLAG_VALUE, Field, AslGbl_MsgBuffer);
    617         Value = 0;
    618     }
    619 
    620     *Buffer |= (UINT8) (Value << BitPosition);
    621 }
    622 
    623 
    624 /******************************************************************************
    625  *
    626  * FUNCTION:    DtCreateField
    627  *
    628  * PARAMETERS: Name
    629  *             Value
    630  *             Line
    631  *             Offset
    632  *             Column
    633  *             NameColumn
    634  *
    635  * RETURN:     None
    636  *
    637  * DESCRIPTION: Create a field
    638  *
    639  *****************************************************************************/
    640 
    641 void
    642 DtCreateField (
    643     DT_TABLE_UNIT           *FieldKey,
    644     DT_TABLE_UNIT           *FieldValue,
    645     UINT32                  Offset)
    646 {
    647     DT_FIELD                *Field = UtFieldCacheCalloc ();
    648 
    649 
    650     Field->StringLength = 0;
    651     if (FieldKey->Value)
    652     {
    653         Field->Name =
    654             strcpy (UtLocalCacheCalloc (strlen (FieldKey->Value) + 1), FieldKey->Value);
    655     }
    656 
    657     if (FieldValue->Value)
    658     {
    659         Field->StringLength = strlen (FieldValue->Value);
    660         Field->Value =
    661             strcpy (UtLocalCacheCalloc (Field->StringLength + 1), FieldValue->Value);
    662     }
    663 
    664     Field->Line = FieldValue->Line;
    665     Field->ByteOffset = Offset;
    666     Field->NameColumn = FieldKey->Column;
    667     Field->Column = FieldValue->Column;
    668     DtLinkField (Field);
    669 
    670     DtDumpFieldList (AslGbl_FieldList);
    671 }
    672 
    673 
    674 /******************************************************************************
    675  *
    676  * FUNCTION:    DtCreateTableUnit
    677  *
    678  * PARAMETERS: Data
    679  *             Line
    680  *             Column
    681  *
    682  * RETURN:     a table unit
    683  *
    684  * DESCRIPTION: Create a table unit
    685  *
    686  *****************************************************************************/
    687 
    688 DT_TABLE_UNIT *
    689 DtCreateTableUnit (
    690     char                    *Data,
    691     UINT32                  Line,
    692     UINT32                  Column)
    693 {
    694     DT_TABLE_UNIT           *Unit = (DT_TABLE_UNIT *) UtFieldCacheCalloc ();
    695 
    696 
    697     Unit->Value = Data;
    698     Unit->Line = Line;
    699     Unit->Column = Column;
    700     return (Unit);
    701 }
    702 
    703 
    704 /******************************************************************************
    705  *
    706  * FUNCTION:    DtLinkField
    707  *
    708  * PARAMETERS:  Field               - New field object to link
    709  *
    710  * RETURN:      None
    711  *
    712  * DESCRIPTION: Link one field name and value to the list
    713  *
    714  *****************************************************************************/
    715 
    716 void
    717 DtLinkField (
    718     DT_FIELD                *Field)
    719 {
    720     DT_FIELD                *Prev;
    721     DT_FIELD                *Next;
    722 
    723 
    724     Prev = Next = AslGbl_FieldList;
    725 
    726     while (Next)
    727     {
    728         Prev = Next;
    729         Next = Next->Next;
    730     }
    731 
    732     if (Prev)
    733     {
    734         Prev->Next = Field;
    735     }
    736     else
    737     {
    738         AslGbl_FieldList = Field;
    739     }
    740 }
    741