aslhex.c revision 250838
1139749Simp/******************************************************************************
219410Sguido *
319410Sguido * Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)
419410Sguido *
519410Sguido *****************************************************************************/
619410Sguido
719410Sguido/*
819410Sguido * Copyright (C) 2000 - 2013, Intel Corp.
919410Sguido * All rights reserved.
1019410Sguido *
1119410Sguido * Redistribution and use in source and binary forms, with or without
1219410Sguido * modification, are permitted provided that the following conditions
1319410Sguido * are met:
1419410Sguido * 1. Redistributions of source code must retain the above copyright
1519410Sguido *    notice, this list of conditions, and the following disclaimer,
1619410Sguido *    without modification.
1719410Sguido * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1819410Sguido *    substantially similar to the "NO WARRANTY" disclaimer below
1919410Sguido *    ("Disclaimer") and any redistribution must be conditioned upon
2019410Sguido *    including a substantially similar Disclaimer requirement for further
2119410Sguido *    binary redistribution.
2259868Speter * 3. Neither the names of the above-listed copyright holders nor the names
2319410Sguido *    of any contributors may be used to endorse or promote products derived
2419410Sguido *    from this software without specific prior written permission.
2519410Sguido *
2619410Sguido * Alternatively, this software may be distributed under the terms of the
2719410Sguido * GNU General Public License ("GPL") version 2 as published by the Free
2819410Sguido * Software Foundation.
2919410Sguido *
3019410Sguido * NO WARRANTY
3119410Sguido * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3219410Sguido * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3319410Sguido * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34133980Sgibbs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35133980Sgibbs * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36133980Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37133980Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38133980Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39133980Sgibbs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
4019410Sguido * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41133980Sgibbs * POSSIBILITY OF SUCH DAMAGES.
42133980Sgibbs */
43133980Sgibbs
44133980Sgibbs
4519410Sguido#include <contrib/dev/acpica/compiler/aslcompiler.h>
4619410Sguido
4719410Sguido#define _COMPONENT          ACPI_COMPILER
4819410Sguido        ACPI_MODULE_NAME    ("ashex")
4919410Sguido
5019410Sguido/*
5119410Sguido * This module emits ASCII hex output files in either C, ASM, or ASL format
5219410Sguido */
5319410Sguido
5419410Sguido
5519410Sguido/* Local prototypes */
56121491Simp
5719410Sguidostatic void
5819410SguidoHxDoHexOutputC (
5919410Sguido    void);
6019410Sguido
61121491Simpstatic void
6219410SguidoHxDoHexOutputAsl (
6319410Sguido    void);
6419410Sguido
6519410Sguidostatic void
6619410SguidoHxDoHexOutputAsm (
6719410Sguido    void);
6819410Sguido
6919410Sguidostatic UINT32
7019410SguidoHxReadAmlOutputFile (
7119410Sguido    UINT8                   *Buffer);
7219410Sguido
7319410Sguido
7419410Sguido/*******************************************************************************
75121491Simp *
76121491Simp * FUNCTION:    HxDoHexOutput
77121491Simp *
7819410Sguido * PARAMETERS:  None
7919410Sguido *
80121491Simp * RETURN:      None
81121491Simp *
8219410Sguido * DESCRIPTION: Create the hex output file. Note: data is obtained by reading
83121491Simp *              the entire AML output file that was previously generated.
84121491Simp *
85121491Simp ******************************************************************************/
86121491Simp
87121491Simpvoid
88121491SimpHxDoHexOutput (
89121491Simp    void)
90121491Simp{
91121491Simp
92121491Simp    switch (Gbl_HexOutputFlag)
93121491Simp    {
94121491Simp    case HEX_OUTPUT_C:
95121491Simp
96121491Simp        HxDoHexOutputC ();
97121491Simp        break;
98121491Simp
99121491Simp    case HEX_OUTPUT_ASM:
100121491Simp
101121491Simp        HxDoHexOutputAsm ();
102121491Simp        break;
103121491Simp
104121491Simp    case HEX_OUTPUT_ASL:
105121491Simp
106121491Simp        HxDoHexOutputAsl ();
107121491Simp        break;
10819410Sguido
109121491Simp    default:
110121491Simp
11119410Sguido        /* No other output types supported */
11219410Sguido
11319410Sguido        break;
11419410Sguido    }
115218909Sbrucec}
11619410Sguido
11719410Sguido
11819410Sguido/*******************************************************************************
11919410Sguido *
12019410Sguido * FUNCTION:    HxReadAmlOutputFile
12119410Sguido *
12219410Sguido * PARAMETERS:  Buffer              - Where to return data
12319410Sguido *
12419410Sguido * RETURN:      None
12519410Sguido *
12619410Sguido * DESCRIPTION: Read a line of the AML output prior to formatting the data
12719410Sguido *
12819410Sguido ******************************************************************************/
12919410Sguido
13019410Sguidostatic UINT32
13119410SguidoHxReadAmlOutputFile (
132121491Simp    UINT8                   *Buffer)
13319410Sguido{
134133980Sgibbs    UINT32                  Actual;
13519410Sguido
13619410Sguido
13719410Sguido    Actual = fread (Buffer, 1, HEX_TABLE_LINE_SIZE,
13819410Sguido        Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
13919410Sguido
14019410Sguido    if (ferror (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle))
14119410Sguido    {
14219410Sguido        FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
14319410Sguido        AslAbort ();
14419410Sguido    }
14519410Sguido
14619410Sguido    return (Actual);
14719410Sguido}
14819410Sguido
14919410Sguido
15019410Sguido/*******************************************************************************
15119410Sguido *
15219410Sguido * FUNCTION:    HxDoHexOutputC
15319410Sguido *
15419410Sguido * PARAMETERS:  None
15519410Sguido *
15619410Sguido * RETURN:      None
15719410Sguido *
15819410Sguido * DESCRIPTION: Create the hex output file. This is the same data as the AML
15919410Sguido *              output file, but formatted into hex/ascii bytes suitable for
16019410Sguido *              inclusion into a C source file.
16119410Sguido *
16219410Sguido ******************************************************************************/
16319410Sguido
164121491Simpstatic void
16519410SguidoHxDoHexOutputC (
16619410Sguido    void)
16719410Sguido{
16819410Sguido    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
16919410Sguido    UINT32                  LineLength;
17019410Sguido    UINT32                  Offset = 0;
17119410Sguido    UINT32                  AmlFileSize;
17219410Sguido    UINT32                  i;
17319410Sguido
17419410Sguido
17519410Sguido    /* Get AML size, seek back to start */
17619410Sguido
17719410Sguido    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
17819410Sguido    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
17919410Sguido
18019410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n");
18119410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
18219410Sguido        AmlFileSize);
18319410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char AmlCode[] =\n{\n");
18419410Sguido
18519410Sguido    while (Offset < AmlFileSize)
18619410Sguido    {
18719410Sguido        /* Read enough bytes needed for one output line */
18819410Sguido
18919410Sguido        LineLength = HxReadAmlOutputFile (FileData);
19019410Sguido        if (!LineLength)
19119410Sguido        {
19219410Sguido            break;
19319410Sguido        }
19419410Sguido
19519410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "    ");
19619410Sguido
19719410Sguido        for (i = 0; i < LineLength; i++)
19819410Sguido        {
19919410Sguido            /*
20019410Sguido             * Print each hex byte.
20119410Sguido             * Add a comma until the very last byte of the AML file
20219410Sguido             * (Some C compilers complain about a trailing comma)
20319410Sguido             */
20419410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
20519410Sguido            if ((Offset + i + 1) < AmlFileSize)
20619410Sguido            {
20719410Sguido                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
20819410Sguido            }
20919410Sguido            else
21019410Sguido            {
21119410Sguido                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
21219410Sguido            }
21319410Sguido        }
21419410Sguido
21519410Sguido        /* Add fill spaces if needed for last line */
21619410Sguido
21719410Sguido        if (LineLength < HEX_TABLE_LINE_SIZE)
21819410Sguido        {
21919410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
22019410Sguido                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
22119410Sguido        }
22219410Sguido
22319410Sguido        /* Emit the offset and ascii dump for the entire line */
22419410Sguido
22519410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
22619410Sguido        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
22719410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
22819410Sguido            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
22919410Sguido
23019410Sguido        Offset += LineLength;
23119410Sguido    }
23219410Sguido
23319410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
23419410Sguido}
23519410Sguido
23619410Sguido
23719410Sguido/*******************************************************************************
23819410Sguido *
23919410Sguido * FUNCTION:    HxDoHexOutputAsl
24019410Sguido *
24119410Sguido * PARAMETERS:  None
24219410Sguido *
243133980Sgibbs * RETURN:      None
244133980Sgibbs *
24519410Sguido * DESCRIPTION: Create the hex output file. This is the same data as the AML
24619410Sguido *              output file, but formatted into hex/ascii bytes suitable for
247133980Sgibbs *              inclusion into a C source file.
248133980Sgibbs *
249133980Sgibbs ******************************************************************************/
250133980Sgibbs
251133980Sgibbsstatic void
252133980SgibbsHxDoHexOutputAsl (
253133980Sgibbs    void)
254133980Sgibbs{
25519410Sguido    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
25619410Sguido    UINT32                  LineLength;
25719410Sguido    UINT32                  Offset = 0;
258133980Sgibbs    UINT32                  AmlFileSize;
259133980Sgibbs    UINT32                  i;
260133980Sgibbs
261133980Sgibbs
26219410Sguido    /* Get AML size, seek back to start */
26319410Sguido
26419410Sguido    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
26519410Sguido    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
26619410Sguido
26719410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
26819410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
26919410Sguido        AmlFileSize);
27019410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
27119410Sguido
27219410Sguido    while (Offset < AmlFileSize)
27319410Sguido    {
27419410Sguido        /* Read enough bytes needed for one output line */
27519410Sguido
27619410Sguido        LineLength = HxReadAmlOutputFile (FileData);
27719410Sguido        if (!LineLength)
27819410Sguido        {
27919410Sguido            break;
28019410Sguido        }
28119410Sguido
28219410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
28319410Sguido
28419410Sguido        for (i = 0; i < LineLength; i++)
28519410Sguido        {
28619410Sguido            /*
28719410Sguido             * Print each hex byte.
28819410Sguido             * Add a comma until the very last byte of the AML file
28919410Sguido             * (Some C compilers complain about a trailing comma)
29019410Sguido             */
29119410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
29219410Sguido            if ((Offset + i + 1) < AmlFileSize)
29319410Sguido            {
29419410Sguido                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
29519410Sguido            }
29619410Sguido            else
29768417Swpaul            {
29819410Sguido                FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
299133980Sgibbs            }
30019410Sguido        }
30119410Sguido
30219410Sguido        /* Add fill spaces if needed for last line */
303121491Simp
30419410Sguido        if (LineLength < HEX_TABLE_LINE_SIZE)
30519410Sguido        {
30619410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
307133980Sgibbs                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
30819410Sguido        }
30919410Sguido
31019410Sguido        /* Emit the offset and ascii dump for the entire line */
31119410Sguido
31219410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
31319410Sguido        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
31419410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
31519410Sguido            HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
31619410Sguido
31719410Sguido        Offset += LineLength;
31819410Sguido    }
31919410Sguido
32019410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "    })\n");
32119410Sguido}
32219410Sguido
32319410Sguido
32419410Sguido/*******************************************************************************
32519410Sguido *
32619410Sguido * FUNCTION:    HxDoHexOutputAsm
32719410Sguido *
32819410Sguido * PARAMETERS:  None
32919410Sguido *
33019410Sguido * RETURN:      None
33119410Sguido *
33219410Sguido * DESCRIPTION: Create the hex output file. This is the same data as the AML
33319410Sguido *              output file, but formatted into hex/ascii bytes suitable for
33419410Sguido *              inclusion into a ASM source file.
33519410Sguido *
33619410Sguido ******************************************************************************/
33719410Sguido
338133980Sgibbsstatic void
33919410SguidoHxDoHexOutputAsm (
34019410Sguido    void)
34119410Sguido{
34219410Sguido    UINT8                   FileData[HEX_TABLE_LINE_SIZE];
34319410Sguido    UINT32                  LineLength;
34419410Sguido    UINT32                  Offset = 0;
34519410Sguido    UINT32                  AmlFileSize;
34619410Sguido    UINT32                  i;
34719410Sguido
34819410Sguido
34919410Sguido    /* Get AML size, seek back to start */
35019410Sguido
35119410Sguido    AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
35219410Sguido    FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
35319410Sguido
35419410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");
35519410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",
35619410Sguido        AmlFileSize);
35719410Sguido
35819410Sguido    while (Offset < AmlFileSize)
35919410Sguido    {
36019410Sguido        /* Read enough bytes needed for one output line */
36119410Sguido
36219410Sguido        LineLength = HxReadAmlOutputFile (FileData);
36319410Sguido        if (!LineLength)
36419410Sguido        {
36519410Sguido            break;
36619410Sguido        }
36719410Sguido
36819410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  db  ");
36919410Sguido
37019410Sguido        for (i = 0; i < LineLength; i++)
37119410Sguido        {
37219410Sguido            /*
37319410Sguido             * Print each hex byte.
37419410Sguido             * Add a comma until the last byte of the line
37519410Sguido             */
37619410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);
37719410Sguido            if ((i + 1) < LineLength)
37819410Sguido            {
37919410Sguido                FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
38019410Sguido            }
38119410Sguido        }
38219410Sguido
38319410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
38419410Sguido
38519410Sguido        /* Add fill spaces if needed for last line */
38619410Sguido
38719410Sguido        if (LineLength < HEX_TABLE_LINE_SIZE)
38819410Sguido        {
38919410Sguido            FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
39019410Sguido                5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
39119410Sguido        }
39219410Sguido
39319410Sguido        /* Emit the offset and ascii dump for the entire line */
39419410Sguido
39519410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "  ; %8.8X", Offset);
39619410Sguido        LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
39719410Sguido        FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
39819410Sguido
39919410Sguido        Offset += LineLength;
40019410Sguido    }
40119410Sguido
40219410Sguido    FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
40319410Sguido}
40419410Sguido