aslhex.c revision 281075
1155179Sandre/******************************************************************************
2155179Sandre *
3155179Sandre * Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)
4155179Sandre *
5155179Sandre *****************************************************************************/
6155179Sandre
7155179Sandre/*
8155179Sandre * Copyright (C) 2000 - 2015, Intel Corp.
9155179Sandre * All rights reserved.
10155179Sandre *
11155179Sandre * Redistribution and use in source and binary forms, with or without
12155179Sandre * modification, are permitted provided that the following conditions
13155179Sandre * are met:
14155179Sandre * 1. Redistributions of source code must retain the above copyright
15155179Sandre *    notice, this list of conditions, and the following disclaimer,
16155179Sandre *    without modification.
17155179Sandre * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18155179Sandre *    substantially similar to the "NO WARRANTY" disclaimer below
19155179Sandre *    ("Disclaimer") and any redistribution must be conditioned upon
20155179Sandre *    including a substantially similar Disclaimer requirement for further
21155179Sandre *    binary redistribution.
22155179Sandre * 3. Neither the names of the above-listed copyright holders nor the names
23155179Sandre *    of any contributors may be used to endorse or promote products derived
24155179Sandre *    from this software without specific prior written permission.
25155179Sandre *
26155179Sandre * Alternatively, this software may be distributed under the terms of the
27155179Sandre * GNU General Public License ("GPL") version 2 as published by the Free
28155179Sandre * Software Foundation.
29155179Sandre *
30172467Ssilby * NO WARRANTY
31172467Ssilby * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32172467Ssilby * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33155179Sandre * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34188066Srrs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35155179Sandre * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36155179Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37155179Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38177175Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39155179Sandre * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40155179Sandre * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41155179Sandre * POSSIBILITY OF SUCH DAMAGES.
42155179Sandre */
43155179Sandre
44155179Sandre#include <contrib/dev/acpica/compiler/aslcompiler.h>
45155179Sandre
46155179Sandre#define _COMPONENT          ACPI_COMPILER
47155179Sandre        ACPI_MODULE_NAME    ("ashex")
48195699Srwatson
49155179Sandre/*
50155179Sandre * This module emits ASCII hex output files in either C, ASM, or ASL format
51155179Sandre */
52155179Sandre
53155179Sandre
54155179Sandre/* Local prototypes */
55155179Sandre
56155179Sandrestatic void
57155179SandreHxDoHexOutputC (
58188066Srrs    void);
59188066Srrs
60188066Srrsstatic void
61155179SandreHxDoHexOutputAsl (
62155179Sandre    void);
63155179Sandre
64171167Sgnnstatic void
65155179SandreHxDoHexOutputAsm (
66155179Sandre    void);
67155179Sandre
68171167Sgnnstatic UINT32
69155179SandreHxReadAmlOutputFile (
70155179Sandre    UINT8                   *Buffer);
71155179Sandre
72195699Srwatson
73195699Srwatson/*******************************************************************************
74215701Sdim *
75195699Srwatson * FUNCTION:    HxDoHexOutput
76215701Sdim *
77192648Sbz * PARAMETERS:  None
78195727Srwatson *
79192648Sbz * RETURN:      None
80195699Srwatson *
81195699Srwatson * DESCRIPTION: Create the hex output file. Note: data is obtained by reading
82195699Srwatson *              the entire AML output file that was previously generated.
83195699Srwatson *
84195699Srwatson ******************************************************************************/
85195699Srwatson
86155179Sandrevoid
87155179SandreHxDoHexOutput (
88155179Sandre    void)
89155179Sandre{
90155179Sandre
91155179Sandre    switch (Gbl_HexOutputFlag)
92171732Sbz    {
93155179Sandre    case HEX_OUTPUT_C:
94230442Sbz
95192648Sbz        HxDoHexOutputC ();
96155179Sandre        break;
97222845Sbz
98155179Sandre    case HEX_OUTPUT_ASM:
99192648Sbz
100192648Sbz        HxDoHexOutputAsm ();
101155179Sandre        break;
102155179Sandre
103155179Sandre    case HEX_OUTPUT_ASL:
104155179Sandre
105155179Sandre        HxDoHexOutputAsl ();
106155179Sandre        break;
107155179Sandre
108155179Sandre    default:
109155179Sandre
110155179Sandre        /* No other output types supported */
111155179Sandre
112155179Sandre        break;
113155179Sandre    }
114155179Sandre}
115171167Sgnn
116155179Sandre
117155179Sandre/*******************************************************************************
118155179Sandre *
119241686Sandre * FUNCTION:    HxReadAmlOutputFile
120171133Sgnn *
121155179Sandre * PARAMETERS:  Buffer              - Where to return data
122155179Sandre *
123155179Sandre * RETURN:      None
124155179Sandre *
125155179Sandre * DESCRIPTION: Read a line of the AML output prior to formatting the data
126155179Sandre *
127155179Sandre ******************************************************************************/
128155179Sandre
129155179Sandrestatic UINT32
130155179SandreHxReadAmlOutputFile (
131155179Sandre    UINT8                   *Buffer)
132155179Sandre{
133155179Sandre    UINT32                  Actual;
134155179Sandre
135155179Sandre
136155179Sandre    Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
137155179Sandre        Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
138155179Sandre
139155179Sandre    if (ferror (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle))
140155179Sandre    {
141190951Srwatson        FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
142155179Sandre        AslAbort ();
143155179Sandre    }
144171167Sgnn
145155179Sandre    return (Actual);
146155179Sandre}
147155179Sandre
148155179Sandre
149155179Sandre/*******************************************************************************
150155179Sandre *
151155179Sandre * FUNCTION:    HxDoHexOutputC
152155179Sandre *
153155179Sandre * PARAMETERS:  None
154155179Sandre *
155155179Sandre * RETURN:      None
156155179Sandre *
157155179Sandre * DESCRIPTION: Create the hex output file. This is the same data as the AML
158195699Srwatson *              output file, but formatted into hex/ascii bytes suitable for
159155179Sandre *              inclusion into a C source file.
160155179Sandre *
161155179Sandre ******************************************************************************/
162155179Sandre
163241686Sandrestatic void
164155179SandreHxDoHexOutputC (
165155179Sandre    void)
166155179Sandre{
167155179Sandre    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
168155179Sandre    UINT32                  LineLength;
169155179Sandre    UINT32                  Offset = 0;
170155179Sandre    UINT32                  AmlFileSize;
171155179Sandre    UINT32                  i;
172155179Sandre
173155179Sandre
174155179Sandre    /* Get AML size, seek back to start */
175155179Sandre
176155179Sandre    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
177155179Sandre    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
178155179Sandre
179155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");
180155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
181155179Sandre        AmlFileSize);
182155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char AmlCode[] =\n{\n");
183155179Sandre
184155179Sandre    while (Offset < AmlFileSize)
185155179Sandre    {
186155179Sandre        /* Read enough bytes needed for one output line */
187155179Sandre
188155179Sandre        LineLength = HxReadAmlOutputFile (FileData);
189155179Sandre        if (!LineLength)
190155179Sandre        {
191155179Sandre            break;
192155179Sandre        }
193155179Sandre
194155179Sandre        FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
195155179Sandre
196155179Sandre        for (i = 0; i < LineLength; i++)
197155179Sandre        {
198155179Sandre            /*
199171167Sgnn             * Print each hex byte.
200155179Sandre             * Add a comma until the very last byte of the AML file
201155179Sandre             * (Some C compilers complain about a trailing comma)
202155179Sandre             */
203155179Sandre            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
204155179Sandre            if ((Offset + i + 1) < AmlFileSize)
205155179Sandre            {
206155179Sandre                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
207155179Sandre            }
208155179Sandre            else
209178029Sbz            {
210155179Sandre                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
211155179Sandre            }
212155179Sandre        }
213155179Sandre
214155179Sandre        /* Add fill spaces if needed for last line */
215155179Sandre
216155179Sandre        if (LineLength < HEX_TABLE_LINE_SIZE)
217283901Sae        {
218155179Sandre            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
219155179Sandre                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
220155179Sandre        }
221155179Sandre
222155179Sandre        /* Emit the offset and ascii dump for the entire line */
223155179Sandre
224171167Sgnn        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
225155179Sandre        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
226155179Sandre        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
227199102Strasz            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
228155179Sandre
229171167Sgnn        Offset += LineLength;
230155179Sandre    }
231155179Sandre
232155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
233291355Sgnn}
234291355Sgnn
235291355Sgnn
236291355Sgnn/*******************************************************************************
237155179Sandre *
238155179Sandre * FUNCTION:    HxDoHexOutputAsl
239155179Sandre *
240155179Sandre * PARAMETERS:  None
241155179Sandre *
242155179Sandre * RETURN:      None
243155179Sandre *
244155179Sandre * DESCRIPTION: Create the hex output file. This is the same data as the AML
245155179Sandre *              output file, but formatted into hex/ascii bytes suitable for
246155179Sandre *              inclusion into a C source file.
247155179Sandre *
248155179Sandre ******************************************************************************/
249155179Sandre
250155179Sandrestatic void
251155179SandreHxDoHexOutputAsl (
252155179Sandre    void)
253155179Sandre{
254155179Sandre    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
255155179Sandre    UINT32                  LineLength;
256155179Sandre    UINT32                  Offset = 0;
257155179Sandre    UINT32                  AmlFileSize;
258155179Sandre    UINT32                  i;
259155179Sandre
260155179Sandre
261155179Sandre    /* Get AML size, seek back to start */
262155179Sandre
263155179Sandre    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
264155179Sandre    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
265155179Sandre
266155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
267155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
268155179Sandre        AmlFileSize);
269155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
270155179Sandre
271155179Sandre    while (Offset < AmlFileSize)
272155179Sandre    {
273155179Sandre        /* Read enough bytes needed for one output line */
274155179Sandre
275155179Sandre        LineLength = HxReadAmlOutputFile (FileData);
276155179Sandre        if (!LineLength)
277155179Sandre        {
278155179Sandre            break;
279155179Sandre        }
280155179Sandre
281155179Sandre        FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
282155179Sandre
283155179Sandre        for (i = 0; i < LineLength; i++)
284155179Sandre        {
285155179Sandre            /*
286155179Sandre             * Print each hex byte.
287155179Sandre             * Add a comma until the very last byte of the AML file
288155179Sandre             * (Some C compilers complain about a trailing comma)
289155179Sandre             */
290155179Sandre            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
291155179Sandre            if ((Offset + i + 1) < AmlFileSize)
292155179Sandre            {
293155179Sandre                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
294155179Sandre            }
295155179Sandre            else
296155179Sandre            {
297155179Sandre                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
298155179Sandre            }
299155179Sandre        }
300155179Sandre
301155179Sandre        /* Add fill spaces if needed for last line */
302155179Sandre
303155179Sandre        if (LineLength < HEX_TABLE_LINE_SIZE)
304155179Sandre        {
305155179Sandre            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
306155179Sandre                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
307188066Srrs        }
308188066Srrs
309241913Sglebius        /* Emit the offset and ascii dump for the entire line */
310241913Sglebius
311205104Srrs        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
312188066Srrs        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
313188066Srrs        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
314188066Srrs            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
315155179Sandre
316155179Sandre        Offset += LineLength;
317155179Sandre    }
318177175Sbz
319177175Sbz    FlPrintFile (ASL_FILE_HEX_OUTPUT, "    })\n");
320177175Sbz}
321177175Sbz
322177175Sbz
323177175Sbz/*******************************************************************************
324177175Sbz *
325177175Sbz * FUNCTION:    HxDoHexOutputAsm
326177175Sbz *
327155179Sandre * PARAMETERS:  None
328155179Sandre *
329155179Sandre * RETURN:      None
330155179Sandre *
331155179Sandre * DESCRIPTION: Create the hex output file. This is the same data as the AML
332155179Sandre *              output file, but formatted into hex/ascii bytes suitable for
333155179Sandre *              inclusion into a ASM source file.
334155179Sandre *
335155179Sandre ******************************************************************************/
336155179Sandre
337155179Sandrestatic void
338155179SandreHxDoHexOutputAsm (
339155179Sandre    void)
340155179Sandre{
341155179Sandre    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
342155179Sandre    UINT32                  LineLength;
343155179Sandre    UINT32                  Offset = 0;
344155179Sandre    UINT32                  AmlFileSize;
345155179Sandre    UINT32                  i;
346155179Sandre
347155179Sandre
348155179Sandre    /* Get AML size, seek back to start */
349155179Sandre
350155179Sandre    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
351155179Sandre    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
352155179Sandre
353155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");
354155179Sandre    FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",
355155179Sandre        AmlFileSize);
356155179Sandre
357155179Sandre    while (Offset < AmlFileSize)
358155179Sandre    {
359155179Sandre        /* Read enough bytes needed for one output line */
360155179Sandre
361155179Sandre        LineLength = HxReadAmlOutputFile (FileData);
362155179Sandre        if (!LineLength)
363155179Sandre        {
364155179Sandre            break;
365171167Sgnn        }
366155179Sandre
367155179Sandre        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  db  ");
368
369        for (i = 0; i < LineLength; i++)
370        {
371            /*
372             * Print each hex byte.
373             * Add a comma until the last byte of the line
374             */
375            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);
376            if ((i + 1) < LineLength)
377            {
378                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
379            }
380        }
381
382        FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
383
384        /* Add fill spaces if needed for last line */
385
386        if (LineLength < HEX_TABLE_LINE_SIZE)
387        {
388            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
389                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
390        }
391
392        /* Emit the offset and ascii dump for the entire line */
393
394        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  ; %8.8X", Offset);
395        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
396        FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
397
398        Offset += LineLength;
399    }
400
401    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
402}
403