Home | History | Annotate | Line # | Download | only in compiler
aslhex.c revision 1.1
      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  christos  * Copyright (C) 2000 - 2013, 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 
     45  1.1  christos #include "aslcompiler.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 
     55  1.1  christos /* Local prototypes */
     56  1.1  christos 
     57  1.1  christos static void
     58  1.1  christos HxDoHexOutputC (
     59  1.1  christos     void);
     60  1.1  christos 
     61  1.1  christos static void
     62  1.1  christos HxDoHexOutputAsl (
     63  1.1  christos     void);
     64  1.1  christos 
     65  1.1  christos static void
     66  1.1  christos HxDoHexOutputAsm (
     67  1.1  christos     void);
     68  1.1  christos 
     69  1.1  christos static UINT32
     70  1.1  christos HxReadAmlOutputFile (
     71  1.1  christos     UINT8                   *Buffer);
     72  1.1  christos 
     73  1.1  christos 
     74  1.1  christos /*******************************************************************************
     75  1.1  christos  *
     76  1.1  christos  * FUNCTION:    HxDoHexOutput
     77  1.1  christos  *
     78  1.1  christos  * PARAMETERS:  None
     79  1.1  christos  *
     80  1.1  christos  * RETURN:      None
     81  1.1  christos  *
     82  1.1  christos  * DESCRIPTION: Create the hex output file. Note: data is obtained by reading
     83  1.1  christos  *              the entire AML output file that was previously generated.
     84  1.1  christos  *
     85  1.1  christos  ******************************************************************************/
     86  1.1  christos 
     87  1.1  christos void
     88  1.1  christos HxDoHexOutput (
     89  1.1  christos     void)
     90  1.1  christos {
     91  1.1  christos 
     92  1.1  christos     switch (Gbl_HexOutputFlag)
     93  1.1  christos     {
     94  1.1  christos     case HEX_OUTPUT_C:
     95  1.1  christos 
     96  1.1  christos         HxDoHexOutputC ();
     97  1.1  christos         break;
     98  1.1  christos 
     99  1.1  christos     case HEX_OUTPUT_ASM:
    100  1.1  christos 
    101  1.1  christos         HxDoHexOutputAsm ();
    102  1.1  christos         break;
    103  1.1  christos 
    104  1.1  christos     case HEX_OUTPUT_ASL:
    105  1.1  christos 
    106  1.1  christos         HxDoHexOutputAsl ();
    107  1.1  christos         break;
    108  1.1  christos 
    109  1.1  christos     default:
    110  1.1  christos 
    111  1.1  christos         /* No other output types supported */
    112  1.1  christos 
    113  1.1  christos         break;
    114  1.1  christos     }
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos 
    118  1.1  christos /*******************************************************************************
    119  1.1  christos  *
    120  1.1  christos  * FUNCTION:    HxReadAmlOutputFile
    121  1.1  christos  *
    122  1.1  christos  * PARAMETERS:  Buffer              - Where to return data
    123  1.1  christos  *
    124  1.1  christos  * RETURN:      None
    125  1.1  christos  *
    126  1.1  christos  * DESCRIPTION: Read a line of the AML output prior to formatting the data
    127  1.1  christos  *
    128  1.1  christos  ******************************************************************************/
    129  1.1  christos 
    130  1.1  christos static UINT32
    131  1.1  christos HxReadAmlOutputFile (
    132  1.1  christos     UINT8                   *Buffer)
    133  1.1  christos {
    134  1.1  christos     UINT32                  Actual;
    135  1.1  christos 
    136  1.1  christos 
    137  1.1  christos     Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
    138  1.1  christos         Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
    139  1.1  christos 
    140  1.1  christos     if (ferror (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle))
    141  1.1  christos     {
    142  1.1  christos         FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
    143  1.1  christos         AslAbort ();
    144  1.1  christos     }
    145  1.1  christos 
    146  1.1  christos     return (Actual);
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos 
    150  1.1  christos /*******************************************************************************
    151  1.1  christos  *
    152  1.1  christos  * FUNCTION:    HxDoHexOutputC
    153  1.1  christos  *
    154  1.1  christos  * PARAMETERS:  None
    155  1.1  christos  *
    156  1.1  christos  * RETURN:      None
    157  1.1  christos  *
    158  1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    159  1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    160  1.1  christos  *              inclusion into a C source file.
    161  1.1  christos  *
    162  1.1  christos  ******************************************************************************/
    163  1.1  christos 
    164  1.1  christos static void
    165  1.1  christos HxDoHexOutputC (
    166  1.1  christos     void)
    167  1.1  christos {
    168  1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    169  1.1  christos     UINT32                  LineLength;
    170  1.1  christos     UINT32                  Offset = 0;
    171  1.1  christos     UINT32                  AmlFileSize;
    172  1.1  christos     UINT32                  i;
    173  1.1  christos 
    174  1.1  christos 
    175  1.1  christos     /* Get AML size, seek back to start */
    176  1.1  christos 
    177  1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    178  1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    179  1.1  christos 
    180  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");
    181  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
    182  1.1  christos         AmlFileSize);
    183  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char AmlCode[] =\n{\n");
    184  1.1  christos 
    185  1.1  christos     while (Offset < AmlFileSize)
    186  1.1  christos     {
    187  1.1  christos         /* Read enough bytes needed for one output line */
    188  1.1  christos 
    189  1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    190  1.1  christos         if (!LineLength)
    191  1.1  christos         {
    192  1.1  christos             break;
    193  1.1  christos         }
    194  1.1  christos 
    195  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
    196  1.1  christos 
    197  1.1  christos         for (i = 0; i < LineLength; i++)
    198  1.1  christos         {
    199  1.1  christos             /*
    200  1.1  christos              * Print each hex byte.
    201  1.1  christos              * Add a comma until the very last byte of the AML file
    202  1.1  christos              * (Some C compilers complain about a trailing comma)
    203  1.1  christos              */
    204  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
    205  1.1  christos             if ((Offset + i + 1) < AmlFileSize)
    206  1.1  christos             {
    207  1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    208  1.1  christos             }
    209  1.1  christos             else
    210  1.1  christos             {
    211  1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    212  1.1  christos             }
    213  1.1  christos         }
    214  1.1  christos 
    215  1.1  christos         /* Add fill spaces if needed for last line */
    216  1.1  christos 
    217  1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    218  1.1  christos         {
    219  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    220  1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    221  1.1  christos         }
    222  1.1  christos 
    223  1.1  christos         /* Emit the offset and ascii dump for the entire line */
    224  1.1  christos 
    225  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
    226  1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    227  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
    228  1.1  christos             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
    229  1.1  christos 
    230  1.1  christos         Offset += LineLength;
    231  1.1  christos     }
    232  1.1  christos 
    233  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
    234  1.1  christos }
    235  1.1  christos 
    236  1.1  christos 
    237  1.1  christos /*******************************************************************************
    238  1.1  christos  *
    239  1.1  christos  * FUNCTION:    HxDoHexOutputAsl
    240  1.1  christos  *
    241  1.1  christos  * PARAMETERS:  None
    242  1.1  christos  *
    243  1.1  christos  * RETURN:      None
    244  1.1  christos  *
    245  1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    246  1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    247  1.1  christos  *              inclusion into a C source file.
    248  1.1  christos  *
    249  1.1  christos  ******************************************************************************/
    250  1.1  christos 
    251  1.1  christos static void
    252  1.1  christos HxDoHexOutputAsl (
    253  1.1  christos     void)
    254  1.1  christos {
    255  1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    256  1.1  christos     UINT32                  LineLength;
    257  1.1  christos     UINT32                  Offset = 0;
    258  1.1  christos     UINT32                  AmlFileSize;
    259  1.1  christos     UINT32                  i;
    260  1.1  christos 
    261  1.1  christos 
    262  1.1  christos     /* Get AML size, seek back to start */
    263  1.1  christos 
    264  1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    265  1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    266  1.1  christos 
    267  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
    268  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
    269  1.1  christos         AmlFileSize);
    270  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
    271  1.1  christos 
    272  1.1  christos     while (Offset < AmlFileSize)
    273  1.1  christos     {
    274  1.1  christos         /* Read enough bytes needed for one output line */
    275  1.1  christos 
    276  1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    277  1.1  christos         if (!LineLength)
    278  1.1  christos         {
    279  1.1  christos             break;
    280  1.1  christos         }
    281  1.1  christos 
    282  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
    283  1.1  christos 
    284  1.1  christos         for (i = 0; i < LineLength; i++)
    285  1.1  christos         {
    286  1.1  christos             /*
    287  1.1  christos              * Print each hex byte.
    288  1.1  christos              * Add a comma until the very last byte of the AML file
    289  1.1  christos              * (Some C compilers complain about a trailing comma)
    290  1.1  christos              */
    291  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
    292  1.1  christos             if ((Offset + i + 1) < AmlFileSize)
    293  1.1  christos             {
    294  1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    295  1.1  christos             }
    296  1.1  christos             else
    297  1.1  christos             {
    298  1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    299  1.1  christos             }
    300  1.1  christos         }
    301  1.1  christos 
    302  1.1  christos         /* Add fill spaces if needed for last line */
    303  1.1  christos 
    304  1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    305  1.1  christos         {
    306  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    307  1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    308  1.1  christos         }
    309  1.1  christos 
    310  1.1  christos         /* Emit the offset and ascii dump for the entire line */
    311  1.1  christos 
    312  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
    313  1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    314  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
    315  1.1  christos             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
    316  1.1  christos 
    317  1.1  christos         Offset += LineLength;
    318  1.1  christos     }
    319  1.1  christos 
    320  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    })\n");
    321  1.1  christos }
    322  1.1  christos 
    323  1.1  christos 
    324  1.1  christos /*******************************************************************************
    325  1.1  christos  *
    326  1.1  christos  * FUNCTION:    HxDoHexOutputAsm
    327  1.1  christos  *
    328  1.1  christos  * PARAMETERS:  None
    329  1.1  christos  *
    330  1.1  christos  * RETURN:      None
    331  1.1  christos  *
    332  1.1  christos  * DESCRIPTION: Create the hex output file. This is the same data as the AML
    333  1.1  christos  *              output file, but formatted into hex/ascii bytes suitable for
    334  1.1  christos  *              inclusion into a ASM source file.
    335  1.1  christos  *
    336  1.1  christos  ******************************************************************************/
    337  1.1  christos 
    338  1.1  christos static void
    339  1.1  christos HxDoHexOutputAsm (
    340  1.1  christos     void)
    341  1.1  christos {
    342  1.1  christos     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
    343  1.1  christos     UINT32                  LineLength;
    344  1.1  christos     UINT32                  Offset = 0;
    345  1.1  christos     UINT32                  AmlFileSize;
    346  1.1  christos     UINT32                  i;
    347  1.1  christos 
    348  1.1  christos 
    349  1.1  christos     /* Get AML size, seek back to start */
    350  1.1  christos 
    351  1.1  christos     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
    352  1.1  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    353  1.1  christos 
    354  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");
    355  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",
    356  1.1  christos         AmlFileSize);
    357  1.1  christos 
    358  1.1  christos     while (Offset < AmlFileSize)
    359  1.1  christos     {
    360  1.1  christos         /* Read enough bytes needed for one output line */
    361  1.1  christos 
    362  1.1  christos         LineLength = HxReadAmlOutputFile (FileData);
    363  1.1  christos         if (!LineLength)
    364  1.1  christos         {
    365  1.1  christos             break;
    366  1.1  christos         }
    367  1.1  christos 
    368  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  db  ");
    369  1.1  christos 
    370  1.1  christos         for (i = 0; i < LineLength; i++)
    371  1.1  christos         {
    372  1.1  christos             /*
    373  1.1  christos              * Print each hex byte.
    374  1.1  christos              * Add a comma until the last byte of the line
    375  1.1  christos              */
    376  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);
    377  1.1  christos             if ((i + 1) < LineLength)
    378  1.1  christos             {
    379  1.1  christos                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
    380  1.1  christos             }
    381  1.1  christos         }
    382  1.1  christos 
    383  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
    384  1.1  christos 
    385  1.1  christos         /* Add fill spaces if needed for last line */
    386  1.1  christos 
    387  1.1  christos         if (LineLength < HEX_TABLE_LINE_SIZE)
    388  1.1  christos         {
    389  1.1  christos             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
    390  1.1  christos                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
    391  1.1  christos         }
    392  1.1  christos 
    393  1.1  christos         /* Emit the offset and ascii dump for the entire line */
    394  1.1  christos 
    395  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  ; %8.8X", Offset);
    396  1.1  christos         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
    397  1.1  christos         FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
    398  1.1  christos 
    399  1.1  christos         Offset += LineLength;
    400  1.1  christos     }
    401  1.1  christos 
    402  1.1  christos     FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
    403  1.1  christos }
    404