xref: /freebsd/sys/contrib/dev/acpica/compiler/aslhex.c (revision 11c5cac53f6cc9a2d94cb6f58728b2655e92d3a5)
1 /******************************************************************************
2  *
3  * Module Name: aslhex - ASCII hex output file generation (C, ASM, and ASL)
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2013, 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 <contrib/dev/acpica/compiler/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         /* No other output types supported */
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 
128 static UINT32
129 HxReadAmlOutputFile (
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 
162 static void
163 HxDoHexOutputC (
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         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
226             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
227 
228         Offset += LineLength;
229     }
230 
231     FlPrintFile (ASL_FILE_HEX_OUTPUT, "};\n");
232 }
233 
234 
235 /*******************************************************************************
236  *
237  * FUNCTION:    HxDoHexOutputAsl
238  *
239  * PARAMETERS:  None
240  *
241  * RETURN:      None
242  *
243  * DESCRIPTION: Create the hex output file. This is the same data as the AML
244  *              output file, but formatted into hex/ascii bytes suitable for
245  *              inclusion into a C source file.
246  *
247  ******************************************************************************/
248 
249 static void
250 HxDoHexOutputAsl (
251     void)
252 {
253     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
254     UINT32                  LineLength;
255     UINT32                  Offset = 0;
256     UINT32                  AmlFileSize;
257     UINT32                  i;
258 
259 
260     /* Get AML size, seek back to start */
261 
262     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
263     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
264 
265     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * ASL source code output\n");
266     FlPrintFile (ASL_FILE_HEX_OUTPUT, " * AML code block contains 0x%X bytes\n *\n */\n",
267         AmlFileSize);
268     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    Name (BUF1, Buffer()\n    {\n");
269 
270     while (Offset < AmlFileSize)
271     {
272         /* Read enough bytes needed for one output line */
273 
274         LineLength = HxReadAmlOutputFile (FileData);
275         if (!LineLength)
276         {
277             break;
278         }
279 
280         FlPrintFile (ASL_FILE_HEX_OUTPUT, "        ");
281 
282         for (i = 0; i < LineLength; i++)
283         {
284             /*
285              * Print each hex byte.
286              * Add a comma until the very last byte of the AML file
287              * (Some C compilers complain about a trailing comma)
288              */
289             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0x%2.2X", FileData[i]);
290             if ((Offset + i + 1) < AmlFileSize)
291             {
292                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
293             }
294             else
295             {
296                 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
297             }
298         }
299 
300         /* Add fill spaces if needed for last line */
301 
302         if (LineLength < HEX_TABLE_LINE_SIZE)
303         {
304             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
305                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
306         }
307 
308         /* Emit the offset and ascii dump for the entire line */
309 
310         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  /* %8.8X", Offset);
311         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
312         FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s*/\n",
313             HEX_TABLE_LINE_SIZE - LineLength + 1, " ");
314 
315         Offset += LineLength;
316     }
317 
318     FlPrintFile (ASL_FILE_HEX_OUTPUT, "    })\n");
319 }
320 
321 
322 /*******************************************************************************
323  *
324  * FUNCTION:    HxDoHexOutputAsm
325  *
326  * PARAMETERS:  None
327  *
328  * RETURN:      None
329  *
330  * DESCRIPTION: Create the hex output file. This is the same data as the AML
331  *              output file, but formatted into hex/ascii bytes suitable for
332  *              inclusion into a ASM source file.
333  *
334  ******************************************************************************/
335 
336 static void
337 HxDoHexOutputAsm (
338     void)
339 {
340     UINT8                   FileData[HEX_TABLE_LINE_SIZE];
341     UINT32                  LineLength;
342     UINT32                  Offset = 0;
343     UINT32                  AmlFileSize;
344     UINT32                  i;
345 
346 
347     /* Get AML size, seek back to start */
348 
349     AmlFileSize = FlGetFileSize (ASL_FILE_AML_OUTPUT);
350     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
351 
352     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n");
353     FlPrintFile (ASL_FILE_HEX_OUTPUT, "; AML code block contains 0x%X bytes\n;\n",
354         AmlFileSize);
355 
356     while (Offset < AmlFileSize)
357     {
358         /* Read enough bytes needed for one output line */
359 
360         LineLength = HxReadAmlOutputFile (FileData);
361         if (!LineLength)
362         {
363             break;
364         }
365 
366         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  db  ");
367 
368         for (i = 0; i < LineLength; i++)
369         {
370             /*
371              * Print each hex byte.
372              * Add a comma until the last byte of the line
373              */
374             FlPrintFile (ASL_FILE_HEX_OUTPUT, "0%2.2Xh", FileData[i]);
375             if ((i + 1) < LineLength)
376             {
377                 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
378             }
379         }
380 
381         FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
382 
383         /* Add fill spaces if needed for last line */
384 
385         if (LineLength < HEX_TABLE_LINE_SIZE)
386         {
387             FlPrintFile (ASL_FILE_HEX_OUTPUT, "%*s",
388                 5 * (HEX_TABLE_LINE_SIZE - LineLength), " ");
389         }
390 
391         /* Emit the offset and ascii dump for the entire line */
392 
393         FlPrintFile (ASL_FILE_HEX_OUTPUT, "  ; %8.8X", Offset);
394         LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT, LineLength, FileData);
395         FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
396 
397         Offset += LineLength;
398     }
399 
400     FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
401 }
402