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