Home | History | Annotate | Line # | Download | only in compiler
aslhex.c revision 1.1.1.7.2.2
      1          1.1  christos /******************************************************************************
      2          1.1  christos  *
      3          1.1  christos  * Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)
      4          1.1  christos  *
      5          1.1  christos  *****************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8  1.1.1.7.2.2    martin  * Copyright (C) 2000 - 2020, Intel Corp.
      9          1.1  christos  * All rights reserved.
     10          1.1  christos  *
     11          1.1  christos  * Redistribution and use in source and binary forms, with or without
     12          1.1  christos  * modification, are permitted provided that the following conditions
     13          1.1  christos  * are met:
     14          1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15          1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16          1.1  christos  *    without modification.
     17          1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18          1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19          1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20          1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21          1.1  christos  *    binary redistribution.
     22          1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23          1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24          1.1  christos  *    from this software without specific prior written permission.
     25          1.1  christos  *
     26          1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27          1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28          1.1  christos  * Software Foundation.
     29          1.1  christos  *
     30          1.1  christos  * NO WARRANTY
     31          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35          1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36          1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37          1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38          1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39          1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40          1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41          1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42          1.1  christos  */
     43          1.1  christos 
     44          1.1  christos #include "aslcompiler.h"
     45      1.1.1.7  christos #include "acapps.h"
     46          1.1  christos 
     47          1.1  christos #define _COMPONENT          ACPI_COMPILER
     48          1.1  christos         ACPI_MODULE_NAME    ("ashex")
     49          1.1  christos 
     50          1.1  christos /*
     51          1.1  christos  * This module emits ASCII hex output files in either C, ASM, or ASL format
     52          1.1  christos  */
     53          1.1  christos 
     54          1.1  christos /* Local prototypes */
     55          1.1  christos 
     56          1.1  christos static void
     57          1.1  christos HxDoHexOutputC (
     58          1.1  christos     void);
     59          1.1  christos 
     60          1.1  christos static void
     61          1.1  christos HxDoHexOutputAsl (
     62          1.1  christos     void);
     63          1.1  christos 
     64          1.1  christos static void
     65          1.1  christos HxDoHexOutputAsm (
     66          1.1  christos     void);
     67          1.1  christos 
     68          1.1  christos static UINT32
     69          1.1  christos HxReadAmlOutputFile (
     70          1.1  christos     UINT8                   *Buffer);
     71          1.1  christos 
     72          1.1  christos 
     73          1.1  christos /*******************************************************************************
     74          1.1  christos  *
     75          1.1  christos  * FUNCTION:    HxDoHexOutput
     76          1.1  christos  *
     77          1.1  christos  * PARAMETERS:  None
     78          1.1  christos  *
     79          1.1  christos  * RETURN:      None
     80          1.1  christos  *
     81          1.1  christos  * DESCRIPTION: Create the hex output file. Note: data is obtained by reading
     82          1.1  christos  *              the entire AML output file that was previously generated.
     83          1.1  christos  *
     84          1.1  christos  ******************************************************************************/
     85          1.1  christos 
     86          1.1  christos void
     87          1.1  christos HxDoHexOutput (
     88          1.1  christos     void)
     89          1.1  christos {
     90          1.1  christos 
     91  1.1.1.7.2.1  christos     switch (AslGbl_HexOutputFlag)
     92          1.1  christos     {
     93          1.1  christos     case HEX_OUTPUT_C:
     94          1.1  christos 
     95          1.1  christos         HxDoHexOutputC ();
     96          1.1  christos         break;
     97          1.1  christos 
     98          1.1  christos     case HEX_OUTPUT_ASM:
     99          1.1  christos 
    100          1.1  christos         HxDoHexOutputAsm ();
    101          1.1  christos         break;
    102          1.1  christos 
    103          1.1  christos     case HEX_OUTPUT_ASL:
    104          1.1  christos 
    105          1.1  christos         HxDoHexOutputAsl ();
    106          1.1  christos         break;
    107          1.1  christos 
    108          1.1  christos     default:
    109          1.1  christos 
    110          1.1  christos         /* No other output types supported */
    111          1.1  christos 
    112          1.1  christos         break;
    113          1.1  christos     }
    114          1.1  christos }
    115          1.1  christos 
    116          1.1  christos 
    117          1.1  christos /*******************************************************************************
    118          1.1  christos  *
    119          1.1  christos  * FUNCTION:    HxReadAmlOutputFile
    120          1.1  christos  *
    121          1.1  christos  * PARAMETERS:  Buffer              - Where to return data
    122          1.1  christos  *
    123          1.1  christos  * RETURN:      None
    124          1.1  christos  *
    125          1.1  christos  * DESCRIPTION: Read a line of the AML output prior to formatting the data
    126          1.1  christos  *
    127          1.1  christos  ******************************************************************************/
    128          1.1  christos 
    129          1.1  christos static UINT32
    130          1.1  christos HxReadAmlOutputFile (
    131          1.1  christos     UINT8                   *Buffer)
    132          1.1  christos {
    133          1.1  christos     UINT32                  Actual;
    134          1.1  christos 
    135          1.1  christos 
    136          1.1  christos     Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
    137  1.1.1.7.2.1  christos         AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);
    138          1.1  christos 
    139  1.1.1.7.2.1  christos     if (ferror (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle))
    140          1.1  christos     {
    141          1.1  christos         FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
    142          1.1  christos         AslAbort ();
    143          1.1  christos     }
    144          1.1  christos 
    145          1.1  christos     return (Actual);
    146          1.1  christos }
    147          1.1  christos 
    148          1.1  christos 
    149          1.1  christos /*******************************************************************************
    150          1.1  christos  *
    151          1.1  christos  * FUNCTION:    HxDoHexOutputC
    152          1.1  christos  *
    153          1.1  christos  * PARAMETERS:  None
    154          1.1  christos  *
    155          1.1  christos  * RETURN:      None
    156          1.1  christos  *
    157          1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    158          1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    159          1.1  christos  *              inclusion into a C source file.
    160          1.1  christos  *
    161      1.1.1.7  christos  *              Note: the base name of the hex output file is prepended to
    162      1.1.1.7  christos  *              all symbols as they are output to the file.
    163      1.1.1.7  christos  *
    164          1.1  christos  ******************************************************************************/
    165          1.1  christos 
    166          1.1  christos static void
    167          1.1  christos HxDoHexOutputC (
    168          1.1  christos     void)
    169          1.1  christos {
    170          1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    171          1.1  christos     UINT32                  LineLength;
    172          1.1  christos     UINT32                  Offset = 0;
    173          1.1  christos     UINT32                  AmlFileSize;
    174          1.1  christos     UINT32                  i;
    175      1.1.1.7  christos     char                    *FileBasename;
    176      1.1.1.7  christos 
    177          1.1  christos 
    178      1.1.1.7  christos     /* Obtain the file basename (filename with no extension) */
    179      1.1.1.7  christos 
    180  1.1.1.7.2.1  christos     FileBasename = FlGetFileBasename (AslGbl_Files [ASL_FILE_HEX_OUTPUT].Filename);
    181          1.1  christos 
    182          1.1  christos     /* Get AML size, seek back to start */
    183          1.1  christos 
    184          1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    185          1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    186          1.1  christos 
    187      1.1.1.7  christos     /* Finish the file header and emit the non-data symbols */
    188      1.1.1.7  christos 
    189          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");
    190          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
    191          1.1  christos         AmlFileSize);
    192      1.1.1.7  christos 
    193      1.1.1.7  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "#ifndef __%s_HEX__\n", FileBasename);
    194      1.1.1.7  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "#define __%s_HEX__\n\n", FileBasename);
    195      1.1.1.7  christos 
    196      1.1.1.7  christos     AcpiUtStrlwr (FileBasename);
    197      1.1.1.7  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char %s_aml_code[] =\n{\n", FileBasename);
    198          1.1  christos 
    199          1.1  christos     while (Offset < AmlFileSize)
    200          1.1  christos     {
    201          1.1  christos         /* Read enough bytes needed for one output line */
    202          1.1  christos 
    203          1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    204          1.1  christos         if (!LineLength)
    205          1.1  christos         {
    206          1.1  christos             break;
    207          1.1  christos         }
    208          1.1  christos 
    209          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
    210          1.1  christos 
    211          1.1  christos         for (i = 0; i < LineLength; i++)
    212          1.1  christos         {
    213          1.1  christos             /*
    214      1.1.1.7  christos              * Output each hex byte in the form: "0xnn,"
    215          1.1  christos              * Add a comma until the very last byte of the AML file
    216          1.1  christos              * (Some C compilers complain about a trailing comma)
    217          1.1  christos              */
    218          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
    219          1.1  christos             if ((Offset + i + 1) < AmlFileSize)
    220          1.1  christos             {
    221          1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    222          1.1  christos             }
    223          1.1  christos             else
    224          1.1  christos             {
    225          1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    226          1.1  christos             }
    227          1.1  christos         }
    228          1.1  christos 
    229          1.1  christos         /* Add fill spaces if needed for last line */
    230          1.1  christos 
    231          1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    232          1.1  christos         {
    233          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    234          1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    235          1.1  christos         }
    236          1.1  christos 
    237          1.1  christos         /* Emit the offset and ascii dump for the entire line */
    238          1.1  christos 
    239          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
    240          1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    241      1.1.1.4  christos 
    242          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
    243          1.1  christos             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
    244          1.1  christos 
    245          1.1  christos         Offset += LineLength;
    246          1.1  christos     }
    247          1.1  christos 
    248      1.1.1.7  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n\n");
    249      1.1.1.7  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "#endif\n");
    250          1.1  christos }
    251          1.1  christos 
    252          1.1  christos 
    253          1.1  christos /*******************************************************************************
    254          1.1  christos  *
    255          1.1  christos  * FUNCTION:    HxDoHexOutputAsl
    256          1.1  christos  *
    257          1.1  christos  * PARAMETERS:  None
    258          1.1  christos  *
    259          1.1  christos  * RETURN:      None
    260          1.1  christos  *
    261          1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    262          1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    263          1.1  christos  *              inclusion into a C source file.
    264          1.1  christos  *
    265          1.1  christos  ******************************************************************************/
    266          1.1  christos 
    267          1.1  christos static void
    268          1.1  christos HxDoHexOutputAsl (
    269          1.1  christos     void)
    270          1.1  christos {
    271          1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    272          1.1  christos     UINT32                  LineLength;
    273          1.1  christos     UINT32                  Offset = 0;
    274          1.1  christos     UINT32                  AmlFileSize;
    275          1.1  christos     UINT32                  i;
    276          1.1  christos 
    277          1.1  christos 
    278          1.1  christos     /* Get AML size, seek back to start */
    279          1.1  christos 
    280          1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    281          1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    282          1.1  christos 
    283          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
    284          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
    285          1.1  christos         AmlFileSize);
    286          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
    287          1.1  christos 
    288          1.1  christos     while (Offset < AmlFileSize)
    289          1.1  christos     {
    290          1.1  christos         /* Read enough bytes needed for one output line */
    291          1.1  christos 
    292          1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    293          1.1  christos         if (!LineLength)
    294          1.1  christos         {
    295          1.1  christos             break;
    296          1.1  christos         }
    297          1.1  christos 
    298          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
    299          1.1  christos 
    300          1.1  christos         for (i = 0; i < LineLength; i++)
    301          1.1  christos         {
    302          1.1  christos             /*
    303          1.1  christos              * Print each hex byte.
    304          1.1  christos              * Add a comma until the very last byte of the AML file
    305          1.1  christos              * (Some C compilers complain about a trailing comma)
    306          1.1  christos              */
    307          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
    308          1.1  christos             if ((Offset + i + 1) < AmlFileSize)
    309          1.1  christos             {
    310          1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    311          1.1  christos             }
    312          1.1  christos             else
    313          1.1  christos             {
    314          1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    315          1.1  christos             }
    316          1.1  christos         }
    317          1.1  christos 
    318          1.1  christos         /* Add fill spaces if needed for last line */
    319          1.1  christos 
    320          1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    321          1.1  christos         {
    322          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    323          1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    324          1.1  christos         }
    325          1.1  christos 
    326          1.1  christos         /* Emit the offset and ascii dump for the entire line */
    327          1.1  christos 
    328          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
    329          1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    330      1.1.1.4  christos 
    331          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
    332          1.1  christos             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
    333          1.1  christos 
    334          1.1  christos         Offset += LineLength;
    335          1.1  christos     }
    336          1.1  christos 
    337          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    })\n");
    338          1.1  christos }
    339          1.1  christos 
    340          1.1  christos 
    341          1.1  christos /*******************************************************************************
    342          1.1  christos  *
    343          1.1  christos  * FUNCTION:    HxDoHexOutputAsm
    344          1.1  christos  *
    345          1.1  christos  * PARAMETERS:  None
    346          1.1  christos  *
    347          1.1  christos  * RETURN:      None
    348          1.1  christos  *
    349          1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    350          1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    351          1.1  christos  *              inclusion into a ASM source file.
    352          1.1  christos  *
    353          1.1  christos  ******************************************************************************/
    354          1.1  christos 
    355          1.1  christos static void
    356          1.1  christos HxDoHexOutputAsm (
    357          1.1  christos     void)
    358          1.1  christos {
    359          1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    360          1.1  christos     UINT32                  LineLength;
    361          1.1  christos     UINT32                  Offset = 0;
    362          1.1  christos     UINT32                  AmlFileSize;
    363          1.1  christos     UINT32                  i;
    364          1.1  christos 
    365          1.1  christos 
    366          1.1  christos     /* Get AML size, seek back to start */
    367          1.1  christos 
    368          1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    369          1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    370          1.1  christos 
    371          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");
    372          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",
    373          1.1  christos         AmlFileSize);
    374          1.1  christos 
    375          1.1  christos     while (Offset < AmlFileSize)
    376          1.1  christos     {
    377          1.1  christos         /* Read enough bytes needed for one output line */
    378          1.1  christos 
    379          1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    380          1.1  christos         if (!LineLength)
    381          1.1  christos         {
    382          1.1  christos             break;
    383          1.1  christos         }
    384          1.1  christos 
    385          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  db  ");
    386          1.1  christos 
    387          1.1  christos         for (i = 0; i < LineLength; i++)
    388          1.1  christos         {
    389          1.1  christos             /*
    390          1.1  christos              * Print each hex byte.
    391          1.1  christos              * Add a comma until the last byte of the line
    392          1.1  christos              */
    393          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);
    394          1.1  christos             if ((i + 1) < LineLength)
    395          1.1  christos             {
    396          1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    397          1.1  christos             }
    398          1.1  christos         }
    399          1.1  christos 
    400          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    401          1.1  christos 
    402          1.1  christos         /* Add fill spaces if needed for last line */
    403          1.1  christos 
    404          1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    405          1.1  christos         {
    406          1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    407          1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    408          1.1  christos         }
    409          1.1  christos 
    410          1.1  christos         /* Emit the offset and ascii dump for the entire line */
    411          1.1  christos 
    412          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  ; %8.8X", Offset);
    413          1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    414      1.1.1.4  christos 
    415          1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
    416          1.1  christos 
    417          1.1  christos         Offset += LineLength;
    418          1.1  christos     }
    419          1.1  christos 
    420          1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
    421          1.1  christos }
    422