OSDN Git Service

klibc基本機能実装. ACPICAの準備
[vaneos/DivergeMirror.git] / drivers / acpi / compiler / aslascii.c
1 /******************************************************************************
2  *
3  * Module Name: aslascii - ASCII detection and support routines
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115
116 #include "aslcompiler.h"
117 #include <acapps.h>
118
119 #define _COMPONENT          ACPI_COMPILER
120         ACPI_MODULE_NAME    ("aslascii")
121
122
123 /* Local prototypes */
124
125 static void
126 FlConsumeAnsiComment (
127     FILE                    *Handle,
128     ASL_FILE_STATUS         *Status);
129
130 static void
131 FlConsumeNewComment (
132     FILE                    *Handle,
133     ASL_FILE_STATUS         *Status);
134
135
136 /*******************************************************************************
137  *
138  * FUNCTION:    FlCheckForAcpiTable
139  *
140  * PARAMETERS:  Handle              - Open input file
141  *
142  * RETURN:      Status
143  *
144  * DESCRIPTION: Determine if a file seems to be a binary ACPI table, via the
145  *              following checks on what would be the table header:
146  *              0) File must be at least as long as an ACPI_TABLE_HEADER
147  *              1) The header length field must match the file size
148  *              2) Signature, OemId, OemTableId, AslCompilerId must be ASCII
149  *
150  ******************************************************************************/
151
152 ACPI_STATUS
153 FlCheckForAcpiTable (
154     FILE                    *Handle)
155 {
156     ACPI_TABLE_HEADER       Table;
157     UINT32                  FileSize;
158     size_t                  Actual;
159     UINT32                  i;
160
161
162     /* Read a potential table header */
163
164     Actual = fread (&Table, 1, sizeof (ACPI_TABLE_HEADER), Handle);
165     fseek (Handle, 0, SEEK_SET);
166
167     if (Actual < sizeof (ACPI_TABLE_HEADER))
168     {
169         return (AE_ERROR);
170     }
171
172     /* Header length field must match the file size */
173
174     FileSize = CmGetFileSize (Handle);
175     if (Table.Length != FileSize)
176     {
177         return (AE_ERROR);
178     }
179
180     /*
181      * These fields must be ASCII:
182      * Signature, OemId, OemTableId, AslCompilerId.
183      * We allow a NULL terminator in OemId and OemTableId.
184      */
185     for (i = 0; i < ACPI_NAME_SIZE; i++)
186     {
187         if (!ACPI_IS_ASCII ((UINT8) Table.Signature[i]))
188         {
189             return (AE_ERROR);
190         }
191
192         if (!ACPI_IS_ASCII ((UINT8) Table.AslCompilerId[i]))
193         {
194             return (AE_ERROR);
195         }
196     }
197
198     for (i = 0; (i < ACPI_OEM_ID_SIZE) && (Table.OemId[i]); i++)
199     {
200         if (!ACPI_IS_ASCII ((UINT8) Table.OemId[i]))
201         {
202             return (AE_ERROR);
203         }
204     }
205
206     for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (Table.OemTableId[i]); i++)
207     {
208         if (!ACPI_IS_ASCII ((UINT8) Table.OemTableId[i]))
209         {
210             return (AE_ERROR);
211         }
212     }
213
214     printf ("Binary file appears to be a valid ACPI table, disassembling\n");
215     return (AE_OK);
216 }
217
218
219 /*******************************************************************************
220  *
221  * FUNCTION:    FlCheckForAscii
222  *
223  * PARAMETERS:  Filename            - Full input filename
224  *              DisplayErrors       - TRUE if error messages desired
225  *
226  * RETURN:      Status
227  *
228  * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
229  *              within comments. Note: does not handle nested comments and does
230  *              not handle comment delimiters within string literals. However,
231  *              on the rare chance this happens and an invalid character is
232  *              missed, the parser will catch the error by failing in some
233  *              spectactular manner.
234  *
235  ******************************************************************************/
236
237 ACPI_STATUS
238 FlCheckForAscii (
239     char                    *Filename,
240     BOOLEAN                 DisplayErrors)
241 {
242     UINT8                   Byte;
243     ACPI_SIZE               BadBytes = 0;
244     BOOLEAN                 OpeningComment = FALSE;
245     ASL_FILE_STATUS         Status;
246     FILE                    *Handle;
247
248
249     /* Open file in text mode so file offset is always accurate */
250
251     Handle = fopen (Filename, "rb");
252
253     Status.Line = 1;
254     Status.Offset = 0;
255
256     /* Read the entire file */
257
258     while (fread (&Byte, 1, 1, Handle) == 1)
259     {
260         /* Ignore comment fields (allow non-ascii within) */
261
262         if (OpeningComment)
263         {
264             /* Check for second comment open delimiter */
265
266             if (Byte == '*')
267             {
268                 FlConsumeAnsiComment (Handle, &Status);
269             }
270
271             if (Byte == '/')
272             {
273                 FlConsumeNewComment (Handle, &Status);
274             }
275
276             /* Reset */
277
278             OpeningComment = FALSE;
279         }
280         else if (Byte == '/')
281         {
282             OpeningComment = TRUE;
283         }
284
285         /* Check for an ASCII character */
286
287         if (!ACPI_IS_ASCII (Byte))
288         {
289             if ((BadBytes < 10) && (DisplayErrors))
290             {
291                 AcpiOsPrintf (
292                     "Found non-ASCII character in source text: "
293                     "0x%2.2X in line %u, file offset 0x%2.2X\n",
294                     Byte, Status.Line, Status.Offset);
295             }
296             BadBytes++;
297         }
298
299         /* Ensure character is either printable or a "space" char */
300
301         else if (!ACPI_IS_PRINT (Byte) && !ACPI_IS_SPACE (Byte))
302         {
303             if ((BadBytes < 10) && (DisplayErrors))
304             {
305                 AcpiOsPrintf (
306                     "Found invalid character in source text: "
307                     "0x%2.2X in line %u, file offset 0x%2.2X\n",
308                     Byte, Status.Line, Status.Offset);
309             }
310             BadBytes++;
311         }
312
313         /* Update line counter as necessary */
314
315         if (Byte == 0x0A)
316         {
317             Status.Line++;
318         }
319
320         Status.Offset++;
321     }
322
323     fclose (Handle);
324
325     /* Were there any non-ASCII characters in the file? */
326
327     if (BadBytes)
328     {
329         if (DisplayErrors)
330         {
331             AcpiOsPrintf (
332                 "Total %u invalid characters found in input source text, "
333                 "could be a binary file\n", BadBytes);
334             AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
335         }
336
337         return (AE_BAD_CHARACTER);
338     }
339
340     /* File is OK (100% ASCII) */
341
342     return (AE_OK);
343 }
344
345
346 /*******************************************************************************
347  *
348  * FUNCTION:    FlConsumeAnsiComment
349  *
350  * PARAMETERS:  Handle              - Open input file
351  *              Status              - File current status struct
352  *
353  * RETURN:      Number of lines consumed
354  *
355  * DESCRIPTION: Step over a normal slash-star type comment
356  *
357  ******************************************************************************/
358
359 static void
360 FlConsumeAnsiComment (
361     FILE                    *Handle,
362     ASL_FILE_STATUS         *Status)
363 {
364     UINT8                   Byte;
365     BOOLEAN                 ClosingComment = FALSE;
366
367
368     while (fread (&Byte, 1, 1, Handle) == 1)
369     {
370         /* Scan until comment close is found */
371
372         if (ClosingComment)
373         {
374             if (Byte == '/')
375             {
376                 Status->Offset++;
377                 return;
378             }
379
380             if (Byte != '*')
381             {
382                 /* Reset */
383
384                 ClosingComment = FALSE;
385             }
386         }
387         else if (Byte == '*')
388         {
389             ClosingComment = TRUE;
390         }
391
392         /* Maintain line count */
393
394         if (Byte == 0x0A)
395         {
396             Status->Line++;
397         }
398
399         Status->Offset++;
400     }
401 }
402
403
404 /*******************************************************************************
405  *
406  * FUNCTION:    FlConsumeNewComment
407  *
408  * PARAMETERS:  Handle              - Open input file
409  *              Status              - File current status struct
410  *
411  * RETURN:      Number of lines consumed
412  *
413  * DESCRIPTION: Step over a slash-slash type of comment
414  *
415  ******************************************************************************/
416
417 static void
418 FlConsumeNewComment (
419     FILE                    *Handle,
420     ASL_FILE_STATUS         *Status)
421 {
422     UINT8                   Byte;
423
424
425     while (fread (&Byte, 1, 1, Handle) == 1)
426     {
427         Status->Offset++;
428
429         /* Comment ends at newline */
430
431         if (Byte == 0x0A)
432         {
433             Status->Line++;
434             return;
435         }
436     }
437 }