Home | History | Annotate | Line # | Download | only in compiler
aslcodegen.c revision 1.1.1.21
      1       1.1    jruoho /******************************************************************************
      2       1.1    jruoho  *
      3       1.1    jruoho  * Module Name: aslcodegen - AML code generation
      4       1.1    jruoho  *
      5       1.1    jruoho  *****************************************************************************/
      6       1.1    jruoho 
      7   1.1.1.2    jruoho /*
      8  1.1.1.21  christos  * Copyright (C) 2000 - 2023, Intel Corp.
      9       1.1    jruoho  * All rights reserved.
     10       1.1    jruoho  *
     11   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13   1.1.1.2    jruoho  * are met:
     14   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16   1.1.1.2    jruoho  *    without modification.
     17   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21   1.1.1.2    jruoho  *    binary redistribution.
     22   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24   1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25   1.1.1.2    jruoho  *
     26   1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27   1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.1.1.2    jruoho  * Software Foundation.
     29   1.1.1.2    jruoho  *
     30   1.1.1.2    jruoho  * NO WARRANTY
     31   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.18  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42   1.1.1.2    jruoho  */
     43       1.1    jruoho 
     44       1.1    jruoho #include "aslcompiler.h"
     45       1.1    jruoho #include "aslcompiler.y.h"
     46       1.1    jruoho #include "amlcode.h"
     47  1.1.1.10  christos #include "acconvert.h"
     48  1.1.1.20  christos #include "actbinfo.h"
     49       1.1    jruoho 
     50       1.1    jruoho #define _COMPONENT          ACPI_COMPILER
     51       1.1    jruoho         ACPI_MODULE_NAME    ("aslcodegen")
     52       1.1    jruoho 
     53       1.1    jruoho /* Local prototypes */
     54       1.1    jruoho 
     55       1.1    jruoho static ACPI_STATUS
     56       1.1    jruoho CgAmlWriteWalk (
     57       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
     58       1.1    jruoho     UINT32                  Level,
     59       1.1    jruoho     void                    *Context);
     60       1.1    jruoho 
     61       1.1    jruoho static void
     62       1.1    jruoho CgWriteAmlOpcode (
     63       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     64       1.1    jruoho 
     65       1.1    jruoho static void
     66       1.1    jruoho CgWriteTableHeader (
     67       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     68       1.1    jruoho 
     69       1.1    jruoho static void
     70  1.1.1.14  christos CgWriteNode (
     71  1.1.1.14  christos     ACPI_PARSE_OBJECT       *Op);
     72       1.1    jruoho 
     73       1.1    jruoho static void
     74  1.1.1.14  christos CgUpdateHeader (
     75       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     76       1.1    jruoho 
     77  1.1.1.20  christos static void
     78  1.1.1.20  christos CgUpdateCdatHeader (
     79  1.1.1.20  christos     ACPI_PARSE_OBJECT       *Op);
     80  1.1.1.20  christos 
     81       1.1    jruoho 
     82       1.1    jruoho /*******************************************************************************
     83       1.1    jruoho  *
     84       1.1    jruoho  * FUNCTION:    CgGenerateAmlOutput
     85       1.1    jruoho  *
     86       1.1    jruoho  * PARAMETERS:  None.
     87       1.1    jruoho  *
     88       1.1    jruoho  * RETURN:      None
     89       1.1    jruoho  *
     90   1.1.1.3  christos  * DESCRIPTION: Generate AML code. Currently generates the listing file
     91       1.1    jruoho  *              simultaneously.
     92       1.1    jruoho  *
     93       1.1    jruoho  ******************************************************************************/
     94       1.1    jruoho 
     95       1.1    jruoho void
     96       1.1    jruoho CgGenerateAmlOutput (
     97       1.1    jruoho     void)
     98       1.1    jruoho {
     99       1.1    jruoho 
    100       1.1    jruoho     /* Generate the AML output file */
    101       1.1    jruoho 
    102  1.1.1.14  christos     TrWalkParseTree (AslGbl_CurrentDB,
    103  1.1.1.14  christos         ASL_WALK_VISIT_DOWNWARD | ASL_WALK_VISIT_DB_SEPARATELY,
    104       1.1    jruoho         CgAmlWriteWalk, NULL, NULL);
    105   1.1.1.5  christos 
    106   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    107  1.1.1.20  christos     if (AcpiGbl_CDAT)
    108  1.1.1.20  christos     {
    109  1.1.1.20  christos         CgUpdateCdatHeader (AslGbl_CurrentDB);
    110  1.1.1.20  christos     }
    111  1.1.1.20  christos     else
    112  1.1.1.20  christos     {
    113  1.1.1.20  christos         CgUpdateHeader (AslGbl_CurrentDB);
    114  1.1.1.20  christos     }
    115       1.1    jruoho }
    116       1.1    jruoho 
    117       1.1    jruoho 
    118       1.1    jruoho /*******************************************************************************
    119       1.1    jruoho  *
    120       1.1    jruoho  * FUNCTION:    CgAmlWriteWalk
    121       1.1    jruoho  *
    122       1.1    jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    123       1.1    jruoho  *
    124       1.1    jruoho  * RETURN:      Status
    125       1.1    jruoho  *
    126       1.1    jruoho  * DESCRIPTION: Parse tree walk to generate the AML code.
    127       1.1    jruoho  *
    128       1.1    jruoho  ******************************************************************************/
    129       1.1    jruoho 
    130       1.1    jruoho static ACPI_STATUS
    131       1.1    jruoho CgAmlWriteWalk (
    132       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    133       1.1    jruoho     UINT32                  Level,
    134       1.1    jruoho     void                    *Context)
    135       1.1    jruoho {
    136       1.1    jruoho 
    137   1.1.1.8  christos     /* Generate the AML for this node */
    138   1.1.1.8  christos 
    139   1.1.1.8  christos     CgWriteNode (Op);
    140   1.1.1.8  christos 
    141  1.1.1.13  christos     if (!AslGbl_DebugFlag)
    142       1.1    jruoho     {
    143   1.1.1.8  christos         return (AE_OK);
    144       1.1    jruoho     }
    145       1.1    jruoho 
    146   1.1.1.8  christos     /* Print header at level 0. Alignment assumes 32-bit pointers */
    147       1.1    jruoho 
    148   1.1.1.8  christos     if (!Level)
    149       1.1    jruoho     {
    150       1.1    jruoho         DbgPrint (ASL_TREE_OUTPUT,
    151   1.1.1.8  christos             "\nFinal parse tree used for AML output:\n");
    152   1.1.1.8  christos         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    153       1.1    jruoho     }
    154   1.1.1.8  christos 
    155   1.1.1.8  christos     /* Dump ParseOp name and possible value */
    156   1.1.1.8  christos 
    157   1.1.1.8  christos     switch (Op->Asl.ParseOpcode)
    158       1.1    jruoho     {
    159   1.1.1.8  christos     case PARSEOP_NAMESEG:
    160   1.1.1.8  christos     case PARSEOP_NAMESTRING:
    161   1.1.1.8  christos     case PARSEOP_METHODCALL:
    162   1.1.1.8  christos     case PARSEOP_STRING_LITERAL:
    163   1.1.1.8  christos 
    164   1.1.1.8  christos         UtDumpStringOp (Op, Level);
    165   1.1.1.8  christos         break;
    166   1.1.1.8  christos 
    167   1.1.1.8  christos     default:
    168   1.1.1.8  christos 
    169   1.1.1.8  christos         UtDumpBasicOp (Op, Level);
    170   1.1.1.8  christos         break;
    171       1.1    jruoho     }
    172       1.1    jruoho 
    173   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
    174   1.1.1.7  christos         /* 1  */ (UINT32) Op->Asl.Value.Integer,
    175   1.1.1.7  christos         /* 2  */ Op->Asl.ParseOpcode,
    176   1.1.1.7  christos         /* 3  */ Op->Asl.AmlOpcode,
    177   1.1.1.7  christos         /* 4  */ Op->Asl.AmlOpcodeLength,
    178   1.1.1.7  christos         /* 5  */ Op->Asl.AmlPkgLenBytes,
    179   1.1.1.7  christos         /* 6  */ Op->Asl.AmlLength,
    180   1.1.1.7  christos         /* 7  */ Op->Asl.AmlSubtreeLength,
    181   1.1.1.7  christos         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
    182   1.1.1.7  christos         /* 9  */ Op,
    183   1.1.1.7  christos         /* 10 */ Op->Asl.Parent,
    184   1.1.1.7  christos         /* 11 */ Op->Asl.Child,
    185   1.1.1.7  christos         /* 12 */ Op->Asl.Next,
    186   1.1.1.7  christos         /* 13 */ Op->Asl.CompileFlags,
    187   1.1.1.7  christos         /* 14 */ Op->Asl.AcpiBtype,
    188   1.1.1.7  christos         /* 15 */ Op->Asl.FinalAmlLength,
    189   1.1.1.7  christos         /* 16 */ Op->Asl.Column,
    190   1.1.1.7  christos         /* 17 */ Op->Asl.LineNumber,
    191   1.1.1.7  christos         /* 18 */ Op->Asl.EndLine,
    192   1.1.1.7  christos         /* 19 */ Op->Asl.LogicalLineNumber,
    193   1.1.1.7  christos         /* 20 */ Op->Asl.EndLogicalLine);
    194       1.1    jruoho 
    195  1.1.1.11  christos     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_TREE_OUTPUT);
    196  1.1.1.11  christos     DbgPrint (ASL_TREE_OUTPUT, "\n");
    197       1.1    jruoho     return (AE_OK);
    198       1.1    jruoho }
    199       1.1    jruoho 
    200       1.1    jruoho 
    201       1.1    jruoho /*******************************************************************************
    202       1.1    jruoho  *
    203       1.1    jruoho  * FUNCTION:    CgLocalWriteAmlData
    204       1.1    jruoho  *
    205       1.1    jruoho  * PARAMETERS:  Op              - Current parse op
    206       1.1    jruoho  *              Buffer          - Buffer to write
    207       1.1    jruoho  *              Length          - Size of data in buffer
    208       1.1    jruoho  *
    209       1.1    jruoho  * RETURN:      None
    210       1.1    jruoho  *
    211       1.1    jruoho  * DESCRIPTION: Write a buffer of AML data to the AML output file.
    212       1.1    jruoho  *
    213       1.1    jruoho  ******************************************************************************/
    214       1.1    jruoho 
    215  1.1.1.10  christos void
    216       1.1    jruoho CgLocalWriteAmlData (
    217       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    218       1.1    jruoho     void                    *Buffer,
    219       1.1    jruoho     UINT32                  Length)
    220       1.1    jruoho {
    221       1.1    jruoho 
    222       1.1    jruoho     /* Write the raw data to the AML file */
    223       1.1    jruoho 
    224       1.1    jruoho     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
    225       1.1    jruoho 
    226       1.1    jruoho     /* Update the final AML length for this node (used for listings) */
    227       1.1    jruoho 
    228       1.1    jruoho     if (Op)
    229       1.1    jruoho     {
    230       1.1    jruoho         Op->Asl.FinalAmlLength += Length;
    231       1.1    jruoho     }
    232       1.1    jruoho }
    233       1.1    jruoho 
    234       1.1    jruoho 
    235       1.1    jruoho /*******************************************************************************
    236       1.1    jruoho  *
    237       1.1    jruoho  * FUNCTION:    CgWriteAmlOpcode
    238       1.1    jruoho  *
    239       1.1    jruoho  * PARAMETERS:  Op            - Parse node with an AML opcode
    240       1.1    jruoho  *
    241       1.1    jruoho  * RETURN:      None.
    242       1.1    jruoho  *
    243       1.1    jruoho  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
    244       1.1    jruoho  *
    245       1.1    jruoho  ******************************************************************************/
    246       1.1    jruoho 
    247       1.1    jruoho static void
    248       1.1    jruoho CgWriteAmlOpcode (
    249       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    250       1.1    jruoho {
    251       1.1    jruoho     UINT8                   PkgLenFirstByte;
    252       1.1    jruoho     UINT32                  i;
    253       1.1    jruoho     union {
    254       1.1    jruoho         UINT16                  Opcode;
    255       1.1    jruoho         UINT8                   OpcodeBytes[2];
    256       1.1    jruoho     } Aml;
    257       1.1    jruoho     union {
    258       1.1    jruoho         UINT32                  Len;
    259       1.1    jruoho         UINT8                   LenBytes[4];
    260       1.1    jruoho     } PkgLen;
    261       1.1    jruoho 
    262       1.1    jruoho 
    263       1.1    jruoho     /* We expect some DEFAULT_ARGs, just ignore them */
    264       1.1    jruoho 
    265       1.1    jruoho     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
    266       1.1    jruoho     {
    267       1.1    jruoho         return;
    268       1.1    jruoho     }
    269       1.1    jruoho 
    270  1.1.1.10  christos     /*
    271  1.1.1.10  christos      * Before printing the bytecode, generate comment byte codes
    272  1.1.1.10  christos      * associated with this node.
    273  1.1.1.10  christos      */
    274  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    275  1.1.1.10  christos     {
    276  1.1.1.10  christos         CgWriteAmlComment(Op);
    277  1.1.1.10  christos     }
    278  1.1.1.10  christos 
    279       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    280       1.1    jruoho     {
    281       1.1    jruoho     case AML_UNASSIGNED_OPCODE:
    282       1.1    jruoho 
    283       1.1    jruoho         /* These opcodes should not get here */
    284       1.1    jruoho 
    285       1.1    jruoho         printf ("Found a node with an unassigned AML opcode\n");
    286   1.1.1.7  christos         FlPrintFile (ASL_FILE_STDERR,
    287   1.1.1.7  christos             "Found a node with an unassigned AML opcode\n");
    288       1.1    jruoho         return;
    289       1.1    jruoho 
    290       1.1    jruoho     case AML_INT_RESERVEDFIELD_OP:
    291       1.1    jruoho 
    292       1.1    jruoho         /* Special opcodes for within a field definition */
    293       1.1    jruoho 
    294   1.1.1.3  christos         Aml.Opcode = AML_FIELD_OFFSET_OP;
    295       1.1    jruoho         break;
    296       1.1    jruoho 
    297       1.1    jruoho     case AML_INT_ACCESSFIELD_OP:
    298       1.1    jruoho 
    299   1.1.1.3  christos         Aml.Opcode = AML_FIELD_ACCESS_OP;
    300   1.1.1.3  christos         break;
    301   1.1.1.3  christos 
    302   1.1.1.3  christos     case AML_INT_CONNECTION_OP:
    303   1.1.1.3  christos 
    304   1.1.1.3  christos         Aml.Opcode = AML_FIELD_CONNECTION_OP;
    305       1.1    jruoho         break;
    306       1.1    jruoho 
    307       1.1    jruoho     default:
    308   1.1.1.3  christos 
    309       1.1    jruoho         Aml.Opcode = Op->Asl.AmlOpcode;
    310       1.1    jruoho         break;
    311       1.1    jruoho     }
    312       1.1    jruoho 
    313       1.1    jruoho 
    314       1.1    jruoho     switch (Aml.Opcode)
    315       1.1    jruoho     {
    316       1.1    jruoho     case AML_PACKAGE_LENGTH:
    317       1.1    jruoho 
    318       1.1    jruoho         /* Value is the length to be encoded (Used in field definitions) */
    319       1.1    jruoho 
    320       1.1    jruoho         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
    321       1.1    jruoho         break;
    322       1.1    jruoho 
    323       1.1    jruoho     default:
    324       1.1    jruoho 
    325       1.1    jruoho         /* Check for two-byte opcode */
    326       1.1    jruoho 
    327       1.1    jruoho         if (Aml.Opcode > 0x00FF)
    328       1.1    jruoho         {
    329       1.1    jruoho             /* Write the high byte first */
    330       1.1    jruoho 
    331       1.1    jruoho             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
    332       1.1    jruoho         }
    333       1.1    jruoho 
    334       1.1    jruoho         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
    335       1.1    jruoho 
    336       1.1    jruoho         /* Subtreelength doesn't include length of package length bytes */
    337       1.1    jruoho 
    338       1.1    jruoho         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
    339       1.1    jruoho         break;
    340       1.1    jruoho     }
    341       1.1    jruoho 
    342       1.1    jruoho     /* Does this opcode have an associated "PackageLength" field? */
    343       1.1    jruoho 
    344  1.1.1.11  christos     if (Op->Asl.CompileFlags & OP_AML_PACKAGE)
    345       1.1    jruoho     {
    346       1.1    jruoho         if (Op->Asl.AmlPkgLenBytes == 1)
    347       1.1    jruoho         {
    348       1.1    jruoho             /* Simplest case -- no bytes to follow, just write the count */
    349       1.1    jruoho 
    350       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
    351       1.1    jruoho         }
    352       1.1    jruoho         else if (Op->Asl.AmlPkgLenBytes != 0)
    353       1.1    jruoho         {
    354       1.1    jruoho             /*
    355       1.1    jruoho              * Encode the "bytes to follow" in the first byte, top two bits.
    356       1.1    jruoho              * The low-order nybble of the length is in the bottom 4 bits
    357       1.1    jruoho              */
    358       1.1    jruoho             PkgLenFirstByte = (UINT8)
    359       1.1    jruoho                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
    360       1.1    jruoho                 (PkgLen.LenBytes[0] & 0x0F));
    361       1.1    jruoho 
    362       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
    363       1.1    jruoho 
    364       1.1    jruoho             /*
    365       1.1    jruoho              * Shift the length over by the 4 bits we just stuffed
    366       1.1    jruoho              * in the first byte
    367       1.1    jruoho              */
    368       1.1    jruoho             PkgLen.Len >>= 4;
    369       1.1    jruoho 
    370   1.1.1.7  christos             /*
    371   1.1.1.7  christos              * Now we can write the remaining bytes -
    372   1.1.1.7  christos              * either 1, 2, or 3 bytes
    373   1.1.1.7  christos              */
    374       1.1    jruoho             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
    375       1.1    jruoho             {
    376       1.1    jruoho                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
    377       1.1    jruoho             }
    378       1.1    jruoho         }
    379       1.1    jruoho     }
    380       1.1    jruoho 
    381       1.1    jruoho     switch (Aml.Opcode)
    382       1.1    jruoho     {
    383       1.1    jruoho     case AML_BYTE_OP:
    384       1.1    jruoho 
    385       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
    386       1.1    jruoho         break;
    387       1.1    jruoho 
    388       1.1    jruoho     case AML_WORD_OP:
    389       1.1    jruoho 
    390       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
    391       1.1    jruoho        break;
    392       1.1    jruoho 
    393       1.1    jruoho     case AML_DWORD_OP:
    394       1.1    jruoho 
    395       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
    396       1.1    jruoho         break;
    397       1.1    jruoho 
    398       1.1    jruoho     case AML_QWORD_OP:
    399       1.1    jruoho 
    400       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
    401       1.1    jruoho         break;
    402       1.1    jruoho 
    403       1.1    jruoho     case AML_STRING_OP:
    404       1.1    jruoho 
    405       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    406       1.1    jruoho         break;
    407       1.1    jruoho 
    408       1.1    jruoho     default:
    409   1.1.1.3  christos 
    410       1.1    jruoho         /* All data opcodes must appear above */
    411   1.1.1.3  christos 
    412       1.1    jruoho         break;
    413       1.1    jruoho     }
    414       1.1    jruoho }
    415       1.1    jruoho 
    416       1.1    jruoho 
    417       1.1    jruoho /*******************************************************************************
    418       1.1    jruoho  *
    419       1.1    jruoho  * FUNCTION:    CgWriteTableHeader
    420       1.1    jruoho  *
    421       1.1    jruoho  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
    422       1.1    jruoho  *
    423       1.1    jruoho  * RETURN:      None
    424       1.1    jruoho  *
    425       1.1    jruoho  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
    426       1.1    jruoho  *
    427  1.1.1.14  christos  * NOTE: Input strings should be validated before this function is invoked.
    428  1.1.1.14  christos  *
    429       1.1    jruoho  ******************************************************************************/
    430       1.1    jruoho 
    431       1.1    jruoho static void
    432       1.1    jruoho CgWriteTableHeader (
    433       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    434       1.1    jruoho {
    435       1.1    jruoho     ACPI_PARSE_OBJECT       *Child;
    436  1.1.1.10  christos     UINT32                  CommentLength;
    437  1.1.1.10  christos     ACPI_COMMENT_NODE       *Current;
    438       1.1    jruoho 
    439       1.1    jruoho 
    440  1.1.1.14  christos     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
    441  1.1.1.14  christos 
    442       1.1    jruoho     /* AML filename */
    443       1.1    jruoho 
    444       1.1    jruoho     Child = Op->Asl.Child;
    445       1.1    jruoho 
    446       1.1    jruoho     /* Signature */
    447       1.1    jruoho 
    448       1.1    jruoho     Child = Child->Asl.Next;
    449  1.1.1.10  christos 
    450  1.1.1.10  christos     /*
    451  1.1.1.10  christos      * For ASL-/ASL+ converter: replace the table signature with
    452  1.1.1.10  christos      * "XXXX" and save the original table signature. This results in an AML
    453  1.1.1.10  christos      * file with the signature "XXXX". The converter should remove this AML
    454  1.1.1.10  christos      * file. In the event where this AML file does not get deleted, the
    455  1.1.1.10  christos      * "XXXX" table signature prevents this AML file from running on the AML
    456  1.1.1.10  christos      * interpreter.
    457  1.1.1.10  christos      */
    458  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    459  1.1.1.10  christos     {
    460  1.1.1.14  christos         ACPI_COPY_NAMESEG (AcpiGbl_TableSig, Child->Asl.Value.String);
    461  1.1.1.10  christos         Child->Asl.Value.String = ACPI_SIG_XXXX;
    462  1.1.1.10  christos     }
    463  1.1.1.10  christos 
    464  1.1.1.14  christos     ACPI_COPY_NAMESEG (AslGbl_TableHeader.Signature, Child->Asl.Value.String);
    465       1.1    jruoho 
    466       1.1    jruoho     /* Revision */
    467       1.1    jruoho 
    468       1.1    jruoho     Child = Child->Asl.Next;
    469  1.1.1.13  christos     AslGbl_TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
    470       1.1    jruoho 
    471       1.1    jruoho     /* Command-line Revision override */
    472       1.1    jruoho 
    473  1.1.1.13  christos     if (AslGbl_RevisionOverride)
    474       1.1    jruoho     {
    475  1.1.1.13  christos         AslGbl_TableHeader.Revision = AslGbl_RevisionOverride;
    476       1.1    jruoho     }
    477       1.1    jruoho 
    478       1.1    jruoho     /* OEMID */
    479       1.1    jruoho 
    480       1.1    jruoho     Child = Child->Asl.Next;
    481  1.1.1.14  christos     memcpy (AslGbl_TableHeader.OemId, Child->Asl.Value.String,
    482  1.1.1.14  christos         strlen (Child->Asl.Value.String));
    483       1.1    jruoho 
    484       1.1    jruoho     /* OEM TableID */
    485       1.1    jruoho 
    486       1.1    jruoho     Child = Child->Asl.Next;
    487  1.1.1.14  christos     memcpy (AslGbl_TableHeader.OemTableId, Child->Asl.Value.String,
    488  1.1.1.14  christos         strlen (Child->Asl.Value.String));
    489       1.1    jruoho 
    490       1.1    jruoho     /* OEM Revision */
    491       1.1    jruoho 
    492       1.1    jruoho     Child = Child->Asl.Next;
    493  1.1.1.13  christos     AslGbl_TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
    494       1.1    jruoho 
    495       1.1    jruoho     /* Compiler ID */
    496       1.1    jruoho 
    497  1.1.1.14  christos     ACPI_COPY_NAMESEG (AslGbl_TableHeader.AslCompilerId, ASL_CREATOR_ID);
    498       1.1    jruoho 
    499       1.1    jruoho     /* Compiler version */
    500       1.1    jruoho 
    501  1.1.1.13  christos     AslGbl_TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
    502       1.1    jruoho 
    503       1.1    jruoho     /* Table length. Checksum zero for now, will rewrite later */
    504       1.1    jruoho 
    505  1.1.1.13  christos     AslGbl_TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
    506   1.1.1.7  christos         Op->Asl.AmlSubtreeLength;
    507  1.1.1.10  christos 
    508  1.1.1.10  christos     /* Calculate the comment lengths for this definition block parseOp */
    509  1.1.1.10  christos 
    510  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    511  1.1.1.10  christos     {
    512  1.1.1.10  christos         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
    513  1.1.1.10  christos             Op->Asl.ParseOpName);
    514  1.1.1.10  christos 
    515  1.1.1.10  christos         /*
    516  1.1.1.10  christos          * Take the filename without extensions, add 3 for the new extension
    517  1.1.1.10  christos          * and another 3 for the a908 bytecode and null terminator.
    518  1.1.1.10  christos          */
    519  1.1.1.13  christos         AslGbl_TableHeader.Length += strrchr (AslGbl_ParseTreeRoot->Asl.Filename, '.')
    520  1.1.1.13  christos             - AslGbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
    521  1.1.1.13  christos 
    522  1.1.1.10  christos         Op->Asl.AmlSubtreeLength +=
    523  1.1.1.13  christos             strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3;
    524  1.1.1.13  christos 
    525  1.1.1.15  christos         CvDbgPrint ("     Length: %u\n",
    526  1.1.1.15  christos             (UINT32) strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3);
    527  1.1.1.10  christos 
    528  1.1.1.10  christos         if (Op->Asl.CommentList)
    529  1.1.1.10  christos         {
    530  1.1.1.10  christos             Current = Op->Asl.CommentList;
    531  1.1.1.10  christos             while (Current)
    532  1.1.1.10  christos             {
    533  1.1.1.10  christos                 CommentLength = strlen (Current->Comment)+3;
    534  1.1.1.10  christos                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
    535  1.1.1.10  christos                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
    536  1.1.1.13  christos                 AslGbl_TableHeader.Length += CommentLength;
    537  1.1.1.10  christos                 Op->Asl.AmlSubtreeLength += CommentLength;
    538  1.1.1.10  christos                 Current = Current->Next;
    539  1.1.1.13  christos                 CvDbgPrint ("    Length: %u\n", CommentLength);
    540  1.1.1.10  christos             }
    541  1.1.1.10  christos         }
    542  1.1.1.10  christos         if (Op->Asl.CloseBraceComment)
    543  1.1.1.10  christos         {
    544  1.1.1.10  christos             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
    545  1.1.1.10  christos             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
    546  1.1.1.10  christos             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
    547  1.1.1.13  christos             AslGbl_TableHeader.Length += CommentLength;
    548  1.1.1.10  christos             Op->Asl.AmlSubtreeLength += CommentLength;
    549  1.1.1.13  christos             CvDbgPrint ("    Length: %u\n", CommentLength);
    550  1.1.1.10  christos         }
    551  1.1.1.10  christos     }
    552  1.1.1.10  christos 
    553  1.1.1.13  christos     AslGbl_TableHeader.Checksum = 0;
    554  1.1.1.13  christos     Op->Asl.FinalAmlOffset = ftell (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);
    555   1.1.1.7  christos 
    556   1.1.1.7  christos     /* Write entire header and clear the table header global */
    557   1.1.1.7  christos 
    558  1.1.1.13  christos     CgLocalWriteAmlData (Op, &AslGbl_TableHeader, sizeof (ACPI_TABLE_HEADER));
    559  1.1.1.13  christos     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
    560       1.1    jruoho }
    561       1.1    jruoho 
    562       1.1    jruoho 
    563       1.1    jruoho /*******************************************************************************
    564       1.1    jruoho  *
    565  1.1.1.20  christos  * FUNCTION:    CgUpdateCdatHeader
    566  1.1.1.20  christos  *
    567  1.1.1.20  christos  * PARAMETERS:  Op                  - Op for the Definition Block
    568  1.1.1.20  christos  *
    569  1.1.1.20  christos  * RETURN:      None.
    570  1.1.1.20  christos  *
    571  1.1.1.20  christos  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    572  1.1.1.20  christos  *              re-writing the header for the input definition block
    573  1.1.1.20  christos  *
    574  1.1.1.20  christos  ******************************************************************************/
    575  1.1.1.20  christos 
    576  1.1.1.20  christos static void
    577  1.1.1.20  christos CgUpdateCdatHeader (
    578  1.1.1.20  christos     ACPI_PARSE_OBJECT       *Op)
    579  1.1.1.20  christos {
    580  1.1.1.20  christos     signed char             Sum;
    581  1.1.1.20  christos     UINT32                  i;
    582  1.1.1.20  christos     UINT32                  Length;
    583  1.1.1.20  christos     UINT8                   FileByte;
    584  1.1.1.20  christos     UINT8                   Checksum;
    585  1.1.1.20  christos 
    586  1.1.1.20  christos 
    587  1.1.1.20  christos     /* Calculate the checksum over the entire definition block */
    588  1.1.1.20  christos 
    589  1.1.1.20  christos     Sum = 0;
    590  1.1.1.20  christos     Length = sizeof (ACPI_TABLE_CDAT) + Op->Asl.AmlSubtreeLength;
    591  1.1.1.20  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
    592  1.1.1.20  christos 
    593  1.1.1.20  christos     for (i = 0; i < Length; i++)
    594  1.1.1.20  christos     {
    595  1.1.1.20  christos         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
    596  1.1.1.20  christos         {
    597  1.1.1.20  christos             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
    598  1.1.1.20  christos                 "Table length is greater than size of the input file");
    599  1.1.1.20  christos             return;
    600  1.1.1.20  christos         }
    601  1.1.1.20  christos 
    602  1.1.1.20  christos         Sum = (signed char) (Sum + FileByte);
    603  1.1.1.20  christos     }
    604  1.1.1.20  christos 
    605  1.1.1.20  christos     Checksum = (UINT8) (0 - Sum);
    606  1.1.1.20  christos 
    607  1.1.1.20  christos     DbgPrint (ASL_DEBUG_OUTPUT, "Computed checksum = %X\n", Checksum);
    608  1.1.1.20  christos 
    609  1.1.1.20  christos     /* Re-write the checksum byte */
    610  1.1.1.20  christos 
    611  1.1.1.20  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
    612  1.1.1.20  christos         ACPI_CDAT_OFFSET (Checksum));
    613  1.1.1.20  christos 
    614  1.1.1.20  christos     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
    615  1.1.1.20  christos 
    616  1.1.1.20  christos     /*
    617  1.1.1.20  christos      * Seek to the end of the file. This is done to support multiple file
    618  1.1.1.20  christos      * compilation. Doing this simplifies other parts of the codebase because
    619  1.1.1.20  christos      * it eliminates the need to seek for a different starting place.
    620  1.1.1.20  christos      */
    621  1.1.1.20  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset + Length);
    622  1.1.1.20  christos }
    623  1.1.1.20  christos 
    624  1.1.1.20  christos /*******************************************************************************
    625  1.1.1.20  christos  *
    626   1.1.1.7  christos  * FUNCTION:    CgUpdateHeader
    627       1.1    jruoho  *
    628   1.1.1.7  christos  * PARAMETERS:  Op                  - Op for the Definition Block
    629       1.1    jruoho  *
    630       1.1    jruoho  * RETURN:      None.
    631       1.1    jruoho  *
    632       1.1    jruoho  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    633   1.1.1.7  christos  *              re-writing the header for the input definition block
    634       1.1    jruoho  *
    635       1.1    jruoho  ******************************************************************************/
    636       1.1    jruoho 
    637       1.1    jruoho static void
    638   1.1.1.7  christos CgUpdateHeader (
    639  1.1.1.10  christos     ACPI_PARSE_OBJECT       *Op)
    640       1.1    jruoho {
    641  1.1.1.10  christos     signed char             Sum;
    642  1.1.1.10  christos     UINT32                  i;
    643  1.1.1.10  christos     UINT32                  Length;
    644  1.1.1.10  christos     UINT8                   FileByte;
    645  1.1.1.10  christos     UINT8                   Checksum;
    646       1.1    jruoho 
    647       1.1    jruoho 
    648   1.1.1.7  christos     /* Calculate the checksum over the entire definition block */
    649       1.1    jruoho 
    650   1.1.1.7  christos     Sum = 0;
    651   1.1.1.7  christos     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
    652   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
    653       1.1    jruoho 
    654   1.1.1.7  christos     for (i = 0; i < Length; i++)
    655       1.1    jruoho     {
    656   1.1.1.7  christos         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
    657   1.1.1.7  christos         {
    658  1.1.1.15  christos             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
    659  1.1.1.15  christos                 "Table length is greater than size of the input file");
    660   1.1.1.7  christos             return;
    661   1.1.1.7  christos         }
    662   1.1.1.7  christos 
    663       1.1    jruoho         Sum = (signed char) (Sum + FileByte);
    664       1.1    jruoho     }
    665       1.1    jruoho 
    666   1.1.1.7  christos     Checksum = (UINT8) (0 - Sum);
    667   1.1.1.7  christos 
    668  1.1.1.17  christos     /* Re-write the checksum byte */
    669       1.1    jruoho 
    670   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
    671   1.1.1.7  christos         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
    672       1.1    jruoho 
    673   1.1.1.7  christos     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
    674   1.1.1.7  christos 
    675  1.1.1.14  christos     /*
    676  1.1.1.14  christos      * Seek to the end of the file. This is done to support multiple file
    677  1.1.1.14  christos      * compilation. Doing this simplifies other parts of the codebase because
    678  1.1.1.14  christos      * it eliminates the need to seek for a different starting place.
    679  1.1.1.14  christos      */
    680  1.1.1.14  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset + Length);
    681       1.1    jruoho }
    682       1.1    jruoho 
    683       1.1    jruoho 
    684       1.1    jruoho /*******************************************************************************
    685       1.1    jruoho  *
    686       1.1    jruoho  * FUNCTION:    CgWriteNode
    687       1.1    jruoho  *
    688       1.1    jruoho  * PARAMETERS:  Op            - Parse node to write.
    689       1.1    jruoho  *
    690       1.1    jruoho  * RETURN:      None.
    691       1.1    jruoho  *
    692       1.1    jruoho  * DESCRIPTION: Write the AML that corresponds to a parse node.
    693       1.1    jruoho  *
    694       1.1    jruoho  ******************************************************************************/
    695       1.1    jruoho 
    696       1.1    jruoho static void
    697       1.1    jruoho CgWriteNode (
    698       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    699       1.1    jruoho {
    700       1.1    jruoho     ASL_RESOURCE_NODE       *Rnode;
    701       1.1    jruoho 
    702       1.1    jruoho 
    703  1.1.1.10  christos     /* Write all comments here. */
    704  1.1.1.12  christos 
    705  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    706  1.1.1.10  christos     {
    707  1.1.1.10  christos         CgWriteAmlComment(Op);
    708  1.1.1.10  christos     }
    709  1.1.1.10  christos 
    710       1.1    jruoho     /* Always check for DEFAULT_ARG and other "Noop" nodes */
    711       1.1    jruoho     /* TBD: this may not be the best place for this check */
    712       1.1    jruoho 
    713       1.1    jruoho     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
    714       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
    715       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
    716       1.1    jruoho     {
    717       1.1    jruoho         return;
    718       1.1    jruoho     }
    719       1.1    jruoho 
    720       1.1    jruoho     Op->Asl.FinalAmlLength = 0;
    721       1.1    jruoho 
    722       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    723       1.1    jruoho     {
    724       1.1    jruoho     case AML_RAW_DATA_BYTE:
    725       1.1    jruoho     case AML_RAW_DATA_WORD:
    726       1.1    jruoho     case AML_RAW_DATA_DWORD:
    727       1.1    jruoho     case AML_RAW_DATA_QWORD:
    728       1.1    jruoho 
    729       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
    730       1.1    jruoho         return;
    731       1.1    jruoho 
    732       1.1    jruoho 
    733       1.1    jruoho     case AML_RAW_DATA_BUFFER:
    734       1.1    jruoho 
    735       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
    736       1.1    jruoho         return;
    737       1.1    jruoho 
    738       1.1    jruoho 
    739       1.1    jruoho     case AML_RAW_DATA_CHAIN:
    740       1.1    jruoho 
    741       1.1    jruoho         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
    742       1.1    jruoho         while (Rnode)
    743       1.1    jruoho         {
    744       1.1    jruoho             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
    745       1.1    jruoho             Rnode = Rnode->Next;
    746       1.1    jruoho         }
    747       1.1    jruoho         return;
    748       1.1    jruoho 
    749       1.1    jruoho     default:
    750   1.1.1.3  christos 
    751       1.1    jruoho         /* Internal data opcodes must all appear above */
    752   1.1.1.3  christos 
    753       1.1    jruoho         break;
    754       1.1    jruoho     }
    755       1.1    jruoho 
    756       1.1    jruoho     switch (Op->Asl.ParseOpcode)
    757       1.1    jruoho     {
    758       1.1    jruoho     case PARSEOP_DEFAULT_ARG:
    759       1.1    jruoho 
    760       1.1    jruoho         break;
    761       1.1    jruoho 
    762   1.1.1.7  christos     case PARSEOP_DEFINITION_BLOCK:
    763       1.1    jruoho 
    764       1.1    jruoho         CgWriteTableHeader (Op);
    765  1.1.1.12  christos         if (AcpiGbl_CaptureComments)
    766  1.1.1.10  christos         {
    767  1.1.1.10  christos             CgWriteAmlDefBlockComment (Op);
    768  1.1.1.10  christos         }
    769       1.1    jruoho         break;
    770       1.1    jruoho 
    771       1.1    jruoho     case PARSEOP_NAMESEG:
    772       1.1    jruoho     case PARSEOP_NAMESTRING:
    773       1.1    jruoho     case PARSEOP_METHODCALL:
    774       1.1    jruoho 
    775       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    776       1.1    jruoho         break;
    777       1.1    jruoho 
    778       1.1    jruoho     default:
    779       1.1    jruoho 
    780       1.1    jruoho         CgWriteAmlOpcode (Op);
    781       1.1    jruoho         break;
    782       1.1    jruoho     }
    783       1.1    jruoho }
    784