aslhex.c revision 306536
1219820Sjeff/******************************************************************************
2219820Sjeff *
3219820Sjeff * Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)
4219820Sjeff *
5271127Shselasky *****************************************************************************/
6219820Sjeff
7219820Sjeff/*
8219820Sjeff * Copyright (C) 2000 - 2016, Intel Corp.
9219820Sjeff * All rights reserved.
10219820Sjeff *
11219820Sjeff * Redistribution and use in source and binary forms, with or without
12219820Sjeff * modification, are permitted provided that the following conditions
13219820Sjeff * are met:
14219820Sjeff * 1. Redistributions of source code must retain the above copyright
15219820Sjeff *    notice, this list of conditions, and the following disclaimer,
16219820Sjeff *    without modification.
17219820Sjeff * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18219820Sjeff *    substantially similar to the "NO WARRANTY" disclaimer below
19219820Sjeff *    ("Disclaimer") and any redistribution must be conditioned upon
20219820Sjeff *    including a substantially similar Disclaimer requirement for further
21219820Sjeff *    binary redistribution.
22219820Sjeff * 3. Neither the names of the above-listed copyright holders nor the names
23219820Sjeff *    of any contributors may be used to endorse or promote products derived
24219820Sjeff *    from this software without specific prior written permission.
25219820Sjeff *
26219820Sjeff * Alternatively, this software may be distributed under the terms of the
27219820Sjeff * GNU General Public License ("GPL") version 2 as published by the Free
28219820Sjeff * Software Foundation.
29219820Sjeff *
30219820Sjeff * NO WARRANTY
31219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32219820Sjeff * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33219820Sjeff * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34219820Sjeff * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35219820Sjeff * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39219820Sjeff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40219820Sjeff * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41219820Sjeff * POSSIBILITY OF SUCH DAMAGES.
42219820Sjeff */
43219820Sjeff
44219820Sjeff#include <contrib/dev/acpica/compiler/aslcompiler.h>
45219820Sjeff
46219820Sjeff#define _COMPONENT          ACPI_COMPILER
47219820Sjeff        ACPI_MODULE_NAME    ("ashex")
48255932Salfred
49219820Sjeff/*
50219820Sjeff * This module emits ASCII hex output files in either C, ASM, or ASL format
51219820Sjeff */
52219820Sjeff
53219820Sjeff/* Local prototypes */
54219820Sjeff
55219820Sjeffstatic void
56219820SjeffHxDoHexOutputC (
57219820Sjeff    void);
58219820Sjeff
59219820Sjeffstatic void
60219820SjeffHxDoHexOutputAsl (
61219820Sjeff    void);
62219820Sjeff
63219820Sjeffstatic void
64219820SjeffHxDoHexOutputAsm (
65219820Sjeff    void);
66219820Sjeff
67219820Sjeffstatic UINT32
68219820SjeffHxReadAmlOutputFile (
69219820Sjeff    UINT8                   *Buffer);
70219820Sjeff
71219820Sjeff
72219820Sjeff/*******************************************************************************
73219820Sjeff *
74219820Sjeff * FUNCTION:    HxDoHexOutput
75219820Sjeff *
76219820Sjeff * PARAMETERS:  None
77219820Sjeff *
78219820Sjeff * RETURN:      None
79219820Sjeff *
80219820Sjeff * DESCRIPTION: Create the hex output file. Note: data is obtained by reading
81219820Sjeff *              the entire AML output file that was previously generated.
82219820Sjeff *
83219820Sjeff ******************************************************************************/
84219820Sjeff
85219820Sjeffvoid
86255932SalfredHxDoHexOutput (
87255932Salfred    void)
88255932Salfred{
89255932Salfred
90255932Salfred    switch (Gbl_HexOutputFlag)
91255932Salfred    {
92255932Salfred    case HEX_OUTPUT_C:
93255932Salfred
94255932Salfred        HxDoHexOutputC ();
95255932Salfred        break;
96255932Salfred
97255932Salfred    case HEX_OUTPUT_ASM:
98255932Salfred
99255932Salfred        HxDoHexOutputAsm ();
100255932Salfred        break;
101255932Salfred
102255932Salfred    case HEX_OUTPUT_ASL:
103255932Salfred
104255932Salfred        HxDoHexOutputAsl ();
105255932Salfred        break;
106219820Sjeff
107    default:
108
109        /* No other output types supported */
110
111        break;
112    }
113}
114
115
116/*******************************************************************************
117 *
118 * FUNCTION:    HxReadAmlOutputFile
119 *
120 * PARAMETERS:  Buffer              - Where to return data
121 *
122 * RETURN:      None
123 *
124 * DESCRIPTION: Read a line of the AML output prior to formatting the data
125 *
126 ******************************************************************************/
127
128static UINT32
129HxReadAmlOutputFile (
130    UINT8                   *Buffer)
131{
132    UINT32                  Actual;
133
134
135    Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
136        Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
137
138    if (ferror (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle))
139    {
140        FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
141        AslAbort ();
142    }
143
144    return (Actual);
145}
146
147
148/*******************************************************************************
149 *
150 * FUNCTION:    HxDoHexOutputC
151 *
152 * PARAMETERS:  None
153 *
154 * RETURN:      None
155 *
156 * DESCRIPTION: Create the hex output file. This is the same data as the AML
157 *              output file, but formatted into hex/ascii bytes suitable for
158 *              inclusion into a C source file.
159 *
160 ******************************************************************************/
161
162static void
163HxDoHexOutputC (
164    void)
165{
166    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
167    UINT32                  LineLength;
168    UINT32                  Offset = 0;
169    UINT32                  AmlFileSize;
170    UINT32                  i;
171
172
173    /* Get AML size, seek back to start */
174
175    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
176    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
177
178    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");
179    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
180        AmlFileSize);
181    FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char AmlCode[] =\n{\n");
182
183    while (Offset < AmlFileSize)
184    {
185        /* Read enough bytes needed for one output line */
186
187        LineLength = HxReadAmlOutputFile (FileData);
188        if (!LineLength)
189        {
190            break;
191        }
192
193        FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
194
195        for (i = 0; i < LineLength; i++)
196        {
197            /*
198             * Print each hex byte.
199             * Add a comma until the very last byte of the AML file
200             * (Some C compilers complain about a trailing comma)
201             */
202            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
203            if ((Offset + i + 1) < AmlFileSize)
204            {
205                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
206            }
207            else
208            {
209                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
210            }
211        }
212
213        /* Add fill spaces if needed for last line */
214
215        if (LineLength < HEX_TABLE_LINE_SIZE)
216        {
217            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
218                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
219        }
220
221        /* Emit the offset and ascii dump for the entire line */
222
223        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
224        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
225
226        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
227            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
228
229        Offset += LineLength;
230    }
231
232    FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
233}
234
235
236/*******************************************************************************
237 *
238 * FUNCTION:    HxDoHexOutputAsl
239 *
240 * PARAMETERS:  None
241 *
242 * RETURN:      None
243 *
244 * DESCRIPTION: Create the hex output file. This is the same data as the AML
245 *              output file, but formatted into hex/ascii bytes suitable for
246 *              inclusion into a C source file.
247 *
248 ******************************************************************************/
249
250static void
251HxDoHexOutputAsl (
252    void)
253{
254    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
255    UINT32                  LineLength;
256    UINT32                  Offset = 0;
257    UINT32                  AmlFileSize;
258    UINT32                  i;
259
260
261    /* Get AML size, seek back to start */
262
263    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
264    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
265
266    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
267    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
268        AmlFileSize);
269    FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
270
271    while (Offset < AmlFileSize)
272    {
273        /* Read enough bytes needed for one output line */
274
275        LineLength = HxReadAmlOutputFile (FileData);
276        if (!LineLength)
277        {
278            break;
279        }
280
281        FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
282
283        for (i = 0; i < LineLength; i++)
284        {
285            /*
286             * Print each hex byte.
287             * Add a comma until the very last byte of the AML file
288             * (Some C compilers complain about a trailing comma)
289             */
290            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
291            if ((Offset + i + 1) < AmlFileSize)
292            {
293                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
294            }
295            else
296            {
297                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
298            }
299        }
300
301        /* Add fill spaces if needed for last line */
302
303        if (LineLength < HEX_TABLE_LINE_SIZE)
304        {
305            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
306                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
307        }
308
309        /* Emit the offset and ascii dump for the entire line */
310
311        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
312        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
313
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
338static void
339HxDoHexOutputAsm (
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
398        FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
399
400        Offset += LineLength;
401    }
402
403    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
404}
405