xref: /freebsd/sys/contrib/dev/acpica/common/acfileio.c (revision 0d84335f991f528c6f038e79dd5cc0a7770532da)
1f8146b88SJung-uk Kim /******************************************************************************
2f8146b88SJung-uk Kim  *
3f8146b88SJung-uk Kim  * Module Name: acfileio - Get ACPI tables from file
4f8146b88SJung-uk Kim  *
5f8146b88SJung-uk Kim  *****************************************************************************/
6f8146b88SJung-uk Kim 
7*0d84335fSJung-uk Kim /******************************************************************************
8*0d84335fSJung-uk Kim  *
9*0d84335fSJung-uk Kim  * 1. Copyright Notice
10*0d84335fSJung-uk Kim  *
11*0d84335fSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12f8146b88SJung-uk Kim  * All rights reserved.
13f8146b88SJung-uk Kim  *
14*0d84335fSJung-uk Kim  * 2. License
15*0d84335fSJung-uk Kim  *
16*0d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
17*0d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
18*0d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
19*0d84335fSJung-uk Kim  * property rights.
20*0d84335fSJung-uk Kim  *
21*0d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22*0d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
23*0d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24*0d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
25*0d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
26*0d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
27*0d84335fSJung-uk Kim  *
28*0d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29*0d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
30*0d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
31*0d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
32*0d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
33*0d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
34*0d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
35*0d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
36*0d84335fSJung-uk Kim  *
37*0d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
38*0d84335fSJung-uk Kim  * conditions are met:
39*0d84335fSJung-uk Kim  *
40*0d84335fSJung-uk Kim  * 3. Conditions
41*0d84335fSJung-uk Kim  *
42*0d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43*0d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
44*0d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
45*0d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
46*0d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
47*0d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
48*0d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
49*0d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
50*0d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
51*0d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
52*0d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
53*0d84335fSJung-uk Kim  *
54*0d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55*0d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
56*0d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
57*0d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
58*0d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
59*0d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
60*0d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
61*0d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
62*0d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
63*0d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
64*0d84335fSJung-uk Kim  * make.
65*0d84335fSJung-uk Kim  *
66*0d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67*0d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
68*0d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
69*0d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
70*0d84335fSJung-uk Kim  * distribution.
71*0d84335fSJung-uk Kim  *
72*0d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
73*0d84335fSJung-uk Kim  * Intel Code.
74*0d84335fSJung-uk Kim  *
75*0d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76*0d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
77*0d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
78*0d84335fSJung-uk Kim  * without prior written authorization from Intel.
79*0d84335fSJung-uk Kim  *
80*0d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
81*0d84335fSJung-uk Kim  *
82*0d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83*0d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84*0d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85*0d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86*0d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87*0d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88*0d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
89*0d84335fSJung-uk Kim  *
90*0d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91*0d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92*0d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93*0d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94*0d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95*0d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96*0d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97*0d84335fSJung-uk Kim  * LIMITED REMEDY.
98*0d84335fSJung-uk Kim  *
99*0d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100*0d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
101*0d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
102*0d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
103*0d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
104*0d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
105*0d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
106*0d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
107*0d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108*0d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
109*0d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
110*0d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
111*0d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
112*0d84335fSJung-uk Kim  * such license, approval or letter.
113*0d84335fSJung-uk Kim  *
114*0d84335fSJung-uk Kim  *****************************************************************************
115*0d84335fSJung-uk Kim  *
116*0d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
117*0d84335fSJung-uk Kim  * following license:
118*0d84335fSJung-uk Kim  *
119f8146b88SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120f8146b88SJung-uk Kim  * modification, are permitted provided that the following conditions
121f8146b88SJung-uk Kim  * are met:
122f8146b88SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123f8146b88SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124f8146b88SJung-uk Kim  *    without modification.
125f8146b88SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126f8146b88SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127f8146b88SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128f8146b88SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129f8146b88SJung-uk Kim  *    binary redistribution.
130f8146b88SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131f8146b88SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132f8146b88SJung-uk Kim  *    from this software without specific prior written permission.
133f8146b88SJung-uk Kim  *
134*0d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135*0d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136*0d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137*0d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138*0d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139*0d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140*0d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141*0d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142*0d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143*0d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144*0d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145*0d84335fSJung-uk Kim  *
146*0d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147f8146b88SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148f8146b88SJung-uk Kim  * Software Foundation.
149f8146b88SJung-uk Kim  *
150*0d84335fSJung-uk Kim  *****************************************************************************/
151f8146b88SJung-uk Kim 
152f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
153f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
154f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/actables.h>
155f8146b88SJung-uk Kim #include <contrib/dev/acpica/include/acutils.h>
156493deb39SJung-uk Kim #include <contrib/dev/acpica/include/acapps.h>
157f8146b88SJung-uk Kim 
158f8146b88SJung-uk Kim #define _COMPONENT          ACPI_UTILITIES
159f8146b88SJung-uk Kim         ACPI_MODULE_NAME    ("acfileio")
160f8146b88SJung-uk Kim 
161f8146b88SJung-uk Kim 
162f8146b88SJung-uk Kim /* Local prototypes */
163f8146b88SJung-uk Kim 
164f8146b88SJung-uk Kim static ACPI_STATUS
165f8146b88SJung-uk Kim AcGetOneTableFromFile (
166f8146b88SJung-uk Kim     char                    *Filename,
167f8146b88SJung-uk Kim     FILE                    *File,
168f8146b88SJung-uk Kim     UINT8                   GetOnlyAmlTables,
169f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       **Table);
170f8146b88SJung-uk Kim 
171f8146b88SJung-uk Kim static ACPI_STATUS
172f8146b88SJung-uk Kim AcCheckTextModeCorruption (
173f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table);
174f8146b88SJung-uk Kim 
175f8146b88SJung-uk Kim 
176f8146b88SJung-uk Kim /*******************************************************************************
177f8146b88SJung-uk Kim  *
178f8146b88SJung-uk Kim  * FUNCTION:    AcGetAllTablesFromFile
179f8146b88SJung-uk Kim  *
180f8146b88SJung-uk Kim  * PARAMETERS:  Filename            - Table filename
181f8146b88SJung-uk Kim  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables
182f8146b88SJung-uk Kim  *              ReturnListHead      - Where table list is returned
183f8146b88SJung-uk Kim  *
184f8146b88SJung-uk Kim  * RETURN:      Status
185f8146b88SJung-uk Kim  *
186f8146b88SJung-uk Kim  * DESCRIPTION: Get all ACPI tables from within a single file.
187f8146b88SJung-uk Kim  *
188f8146b88SJung-uk Kim  ******************************************************************************/
189f8146b88SJung-uk Kim 
190f8146b88SJung-uk Kim ACPI_STATUS
191f8146b88SJung-uk Kim AcGetAllTablesFromFile (
192f8146b88SJung-uk Kim     char                    *Filename,
193f8146b88SJung-uk Kim     UINT8                   GetOnlyAmlTables,
194f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     **ReturnListHead)
195f8146b88SJung-uk Kim {
196f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
197f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *ListTail = NULL;
198f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *TableDesc;
199f8146b88SJung-uk Kim     FILE                    *File;
200f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table = NULL;
201f8146b88SJung-uk Kim     UINT32                  FileSize;
202f8146b88SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
203f8146b88SJung-uk Kim 
204f8146b88SJung-uk Kim 
205f8146b88SJung-uk Kim     File = fopen (Filename, "rb");
206f8146b88SJung-uk Kim     if (!File)
207f8146b88SJung-uk Kim     {
208493deb39SJung-uk Kim         fprintf (stderr, "Could not open input file: %s\n", Filename);
209f8146b88SJung-uk Kim         if (errno == ENOENT)
210f8146b88SJung-uk Kim         {
211f8146b88SJung-uk Kim             return (AE_NOT_EXIST);
212f8146b88SJung-uk Kim         }
213f8146b88SJung-uk Kim 
214f8146b88SJung-uk Kim         return (AE_ERROR);
215f8146b88SJung-uk Kim     }
216f8146b88SJung-uk Kim 
217f8146b88SJung-uk Kim     /* Get the file size */
218f8146b88SJung-uk Kim 
219f8146b88SJung-uk Kim     FileSize = CmGetFileSize (File);
220f8146b88SJung-uk Kim     if (FileSize == ACPI_UINT32_MAX)
221f8146b88SJung-uk Kim     {
222f8146b88SJung-uk Kim         Status = AE_ERROR;
2231cc50d6bSJung-uk Kim         goto Exit;
224f8146b88SJung-uk Kim     }
225f8146b88SJung-uk Kim 
226f8146b88SJung-uk Kim     fprintf (stderr,
227f8146b88SJung-uk Kim         "Input file %s, Length 0x%X (%u) bytes\n",
228f8146b88SJung-uk Kim         Filename, FileSize, FileSize);
229f8146b88SJung-uk Kim 
230f8146b88SJung-uk Kim     /* We must have at least one ACPI table header */
231f8146b88SJung-uk Kim 
232f8146b88SJung-uk Kim     if (FileSize < sizeof (ACPI_TABLE_HEADER))
233f8146b88SJung-uk Kim     {
234f8146b88SJung-uk Kim         Status = AE_BAD_HEADER;
2351cc50d6bSJung-uk Kim         goto Exit;
236f8146b88SJung-uk Kim     }
237f8146b88SJung-uk Kim 
238f8146b88SJung-uk Kim     /* Check for an non-binary file */
239f8146b88SJung-uk Kim 
240f8146b88SJung-uk Kim     if (!AcIsFileBinary (File))
241f8146b88SJung-uk Kim     {
242f8146b88SJung-uk Kim         fprintf (stderr,
243f8146b88SJung-uk Kim             "    %s: File does not appear to contain a valid AML table\n",
244f8146b88SJung-uk Kim             Filename);
2451cc50d6bSJung-uk Kim         Status = AE_TYPE;
2461cc50d6bSJung-uk Kim         goto Exit;
247f8146b88SJung-uk Kim     }
248f8146b88SJung-uk Kim 
249f8146b88SJung-uk Kim     /* Read all tables within the file */
250f8146b88SJung-uk Kim 
251f8146b88SJung-uk Kim     while (ACPI_SUCCESS (Status))
252f8146b88SJung-uk Kim     {
253f8146b88SJung-uk Kim         /* Get one entire ACPI table */
254f8146b88SJung-uk Kim 
255f8146b88SJung-uk Kim         Status = AcGetOneTableFromFile (
256f8146b88SJung-uk Kim             Filename, File, GetOnlyAmlTables, &Table);
257f8146b88SJung-uk Kim 
258f8146b88SJung-uk Kim         if (Status == AE_CTRL_TERMINATE)
259f8146b88SJung-uk Kim         {
260f8146b88SJung-uk Kim             Status = AE_OK;
261f8146b88SJung-uk Kim             break;
262f8146b88SJung-uk Kim         }
263f8146b88SJung-uk Kim         else if (Status == AE_TYPE)
264f8146b88SJung-uk Kim         {
2651cc50d6bSJung-uk Kim             Status = AE_OK;
2661cc50d6bSJung-uk Kim             goto Exit;
267f8146b88SJung-uk Kim         }
268f8146b88SJung-uk Kim         else if (ACPI_FAILURE (Status))
269f8146b88SJung-uk Kim         {
2701cc50d6bSJung-uk Kim             goto Exit;
271f8146b88SJung-uk Kim         }
272f8146b88SJung-uk Kim 
273f8146b88SJung-uk Kim         /* Print table header for iASL/disassembler only */
274f8146b88SJung-uk Kim 
275f8146b88SJung-uk Kim #ifdef ACPI_ASL_COMPILER
276f8146b88SJung-uk Kim 
277f8146b88SJung-uk Kim         AcpiTbPrintTableHeader (0, Table);
278f8146b88SJung-uk Kim #endif
279f8146b88SJung-uk Kim 
280f8146b88SJung-uk Kim         /* Allocate and link a table descriptor */
281f8146b88SJung-uk Kim 
282f8146b88SJung-uk Kim         TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
2831cc50d6bSJung-uk Kim         if (!TableDesc)
2841cc50d6bSJung-uk Kim         {
2851cc50d6bSJung-uk Kim             AcpiOsFree (Table);
2861cc50d6bSJung-uk Kim             Status = AE_NO_MEMORY;
2871cc50d6bSJung-uk Kim             goto Exit;
2881cc50d6bSJung-uk Kim         }
2891cc50d6bSJung-uk Kim 
290f8146b88SJung-uk Kim         TableDesc->Table = Table;
291f8146b88SJung-uk Kim         TableDesc->Next = NULL;
292f8146b88SJung-uk Kim 
293f8146b88SJung-uk Kim         /* Link at the end of the local table list */
294f8146b88SJung-uk Kim 
295f8146b88SJung-uk Kim         if (!ListHead)
296f8146b88SJung-uk Kim         {
297f8146b88SJung-uk Kim             ListHead = TableDesc;
298f8146b88SJung-uk Kim             ListTail = TableDesc;
299f8146b88SJung-uk Kim         }
300f8146b88SJung-uk Kim         else
301f8146b88SJung-uk Kim         {
302f8146b88SJung-uk Kim             ListTail->Next = TableDesc;
303f8146b88SJung-uk Kim             ListTail = TableDesc;
304f8146b88SJung-uk Kim         }
305f8146b88SJung-uk Kim     }
306f8146b88SJung-uk Kim 
307f8146b88SJung-uk Kim     /* Add the local table list to the end of the global list */
308f8146b88SJung-uk Kim 
309f8146b88SJung-uk Kim     if (*ReturnListHead)
310f8146b88SJung-uk Kim     {
311f8146b88SJung-uk Kim         ListTail = *ReturnListHead;
312f8146b88SJung-uk Kim         while (ListTail->Next)
313f8146b88SJung-uk Kim         {
314f8146b88SJung-uk Kim             ListTail = ListTail->Next;
315f8146b88SJung-uk Kim         }
316f8146b88SJung-uk Kim 
317f8146b88SJung-uk Kim         ListTail->Next = ListHead;
318f8146b88SJung-uk Kim     }
319f8146b88SJung-uk Kim     else
320f8146b88SJung-uk Kim     {
321f8146b88SJung-uk Kim         *ReturnListHead = ListHead;
322f8146b88SJung-uk Kim     }
323f8146b88SJung-uk Kim 
3241cc50d6bSJung-uk Kim Exit:
325f8146b88SJung-uk Kim     fclose(File);
326f8146b88SJung-uk Kim     return (Status);
327f8146b88SJung-uk Kim }
328f8146b88SJung-uk Kim 
329f8146b88SJung-uk Kim 
330f8146b88SJung-uk Kim /*******************************************************************************
331f8146b88SJung-uk Kim  *
332f8146b88SJung-uk Kim  * FUNCTION:    AcGetOneTableFromFile
333f8146b88SJung-uk Kim  *
334f8146b88SJung-uk Kim  * PARAMETERS:  Filename            - File where table is located
335f8146b88SJung-uk Kim  *              File                - Open FILE pointer to Filename
336f8146b88SJung-uk Kim  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables.
337f8146b88SJung-uk Kim  *              ReturnTable         - Where a pointer to the table is returned
338f8146b88SJung-uk Kim  *
339f8146b88SJung-uk Kim  * RETURN:      Status
340f8146b88SJung-uk Kim  *
341f8146b88SJung-uk Kim  * DESCRIPTION: Read the next ACPI table from a file. Implements support
342f8146b88SJung-uk Kim  *              for multiple tables within a single file. File must already
343f8146b88SJung-uk Kim  *              be open.
344f8146b88SJung-uk Kim  *
345f8146b88SJung-uk Kim  * Note: Loading an RSDP is not supported.
346f8146b88SJung-uk Kim  *
347f8146b88SJung-uk Kim  ******************************************************************************/
348f8146b88SJung-uk Kim 
349f8146b88SJung-uk Kim static ACPI_STATUS
350f8146b88SJung-uk Kim AcGetOneTableFromFile (
351f8146b88SJung-uk Kim     char                    *Filename,
352f8146b88SJung-uk Kim     FILE                    *File,
353f8146b88SJung-uk Kim     UINT8                   GetOnlyAmlTables,
354f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       **ReturnTable)
355f8146b88SJung-uk Kim {
356f8146b88SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
357f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       TableHeader;
358f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table;
359f8146b88SJung-uk Kim     INT32                   Count;
360f8146b88SJung-uk Kim     long                    TableOffset;
361f8146b88SJung-uk Kim 
362f8146b88SJung-uk Kim 
363f8146b88SJung-uk Kim     *ReturnTable = NULL;
364f8146b88SJung-uk Kim 
365f8146b88SJung-uk Kim     /* Get the table header to examine signature and length */
366f8146b88SJung-uk Kim 
367f8146b88SJung-uk Kim     TableOffset = ftell (File);
368f8146b88SJung-uk Kim     Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
369f8146b88SJung-uk Kim     if (Count != sizeof (ACPI_TABLE_HEADER))
370f8146b88SJung-uk Kim     {
371f8146b88SJung-uk Kim         return (AE_CTRL_TERMINATE);
372f8146b88SJung-uk Kim     }
373f8146b88SJung-uk Kim 
374f8146b88SJung-uk Kim     /* Validate the table signature/header (limited ASCII chars) */
375f8146b88SJung-uk Kim 
376f8146b88SJung-uk Kim     Status = AcValidateTableHeader (File, TableOffset);
377f8146b88SJung-uk Kim     if (ACPI_FAILURE (Status))
378f8146b88SJung-uk Kim     {
379f8146b88SJung-uk Kim         return (Status);
380f8146b88SJung-uk Kim     }
381f8146b88SJung-uk Kim 
382f8146b88SJung-uk Kim     if (GetOnlyAmlTables)
383f8146b88SJung-uk Kim     {
384493deb39SJung-uk Kim         /*
385493deb39SJung-uk Kim          * Table must be an AML table (DSDT/SSDT).
386493deb39SJung-uk Kim          * Used for iASL -e option only.
387493deb39SJung-uk Kim          */
388493deb39SJung-uk Kim         if (!AcpiUtIsAmlTable (&TableHeader))
389f8146b88SJung-uk Kim         {
390f8146b88SJung-uk Kim             fprintf (stderr,
391f8146b88SJung-uk Kim                 "    %s: Table [%4.4s] is not an AML table - ignoring\n",
392f8146b88SJung-uk Kim                 Filename, TableHeader.Signature);
393f8146b88SJung-uk Kim 
394f8146b88SJung-uk Kim             return (AE_TYPE);
395f8146b88SJung-uk Kim         }
396f8146b88SJung-uk Kim     }
397f8146b88SJung-uk Kim 
398f8146b88SJung-uk Kim     /* Allocate a buffer for the entire table */
399f8146b88SJung-uk Kim 
400493deb39SJung-uk Kim     Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
401f8146b88SJung-uk Kim     if (!Table)
402f8146b88SJung-uk Kim     {
403f8146b88SJung-uk Kim         return (AE_NO_MEMORY);
404f8146b88SJung-uk Kim     }
405f8146b88SJung-uk Kim 
406f8146b88SJung-uk Kim     /* Read the entire ACPI table, including header */
407f8146b88SJung-uk Kim 
408f8146b88SJung-uk Kim     fseek (File, TableOffset, SEEK_SET);
409f8146b88SJung-uk Kim 
410f8146b88SJung-uk Kim     Count = fread (Table, 1, TableHeader.Length, File);
411f8146b88SJung-uk Kim     if (Count != (INT32) TableHeader.Length)
412f8146b88SJung-uk Kim     {
413f8146b88SJung-uk Kim         Status = AE_ERROR;
414f8146b88SJung-uk Kim         goto ErrorExit;
415f8146b88SJung-uk Kim     }
416f8146b88SJung-uk Kim 
417f8146b88SJung-uk Kim     /* Validate the checksum (just issue a warning) */
418f8146b88SJung-uk Kim 
419f8146b88SJung-uk Kim     Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
420f8146b88SJung-uk Kim     if (ACPI_FAILURE (Status))
421f8146b88SJung-uk Kim     {
422f8146b88SJung-uk Kim         Status = AcCheckTextModeCorruption (Table);
423f8146b88SJung-uk Kim         if (ACPI_FAILURE (Status))
424f8146b88SJung-uk Kim         {
425f8146b88SJung-uk Kim             goto ErrorExit;
426f8146b88SJung-uk Kim         }
427f8146b88SJung-uk Kim     }
428f8146b88SJung-uk Kim 
429f8146b88SJung-uk Kim     *ReturnTable = Table;
430f8146b88SJung-uk Kim     return (AE_OK);
431f8146b88SJung-uk Kim 
432f8146b88SJung-uk Kim 
433f8146b88SJung-uk Kim ErrorExit:
434f8146b88SJung-uk Kim     AcpiOsFree (Table);
435f8146b88SJung-uk Kim     return (Status);
436f8146b88SJung-uk Kim }
437f8146b88SJung-uk Kim 
438f8146b88SJung-uk Kim 
439f8146b88SJung-uk Kim /*******************************************************************************
440f8146b88SJung-uk Kim  *
441f8146b88SJung-uk Kim  * FUNCTION:    AcIsFileBinary
442f8146b88SJung-uk Kim  *
443f8146b88SJung-uk Kim  * PARAMETERS:  File                - Open input file
444f8146b88SJung-uk Kim  *
445f8146b88SJung-uk Kim  * RETURN:      TRUE if file appears to be binary
446f8146b88SJung-uk Kim  *
447f8146b88SJung-uk Kim  * DESCRIPTION: Scan a file for any non-ASCII bytes.
448f8146b88SJung-uk Kim  *
449f8146b88SJung-uk Kim  * Note: Maintains current file position.
450f8146b88SJung-uk Kim  *
451f8146b88SJung-uk Kim  ******************************************************************************/
452f8146b88SJung-uk Kim 
453f8146b88SJung-uk Kim BOOLEAN
454f8146b88SJung-uk Kim AcIsFileBinary (
455f8146b88SJung-uk Kim     FILE                    *File)
456f8146b88SJung-uk Kim {
457f8146b88SJung-uk Kim     UINT8                   Byte;
458f8146b88SJung-uk Kim     BOOLEAN                 IsBinary = FALSE;
459f8146b88SJung-uk Kim     long                    FileOffset;
460f8146b88SJung-uk Kim 
461f8146b88SJung-uk Kim 
462f8146b88SJung-uk Kim     /* Scan entire file for any non-ASCII bytes */
463f8146b88SJung-uk Kim 
464f8146b88SJung-uk Kim     FileOffset = ftell (File);
465f8146b88SJung-uk Kim     while (fread (&Byte, 1, 1, File) == 1)
466f8146b88SJung-uk Kim     {
467f8146b88SJung-uk Kim         if (!isprint (Byte) && !isspace (Byte))
468f8146b88SJung-uk Kim         {
469f8146b88SJung-uk Kim             IsBinary = TRUE;
470f8146b88SJung-uk Kim             goto Exit;
471f8146b88SJung-uk Kim         }
472f8146b88SJung-uk Kim     }
473f8146b88SJung-uk Kim 
474f8146b88SJung-uk Kim Exit:
475f8146b88SJung-uk Kim     fseek (File, FileOffset, SEEK_SET);
476f8146b88SJung-uk Kim     return (IsBinary);
477f8146b88SJung-uk Kim }
478f8146b88SJung-uk Kim 
479f8146b88SJung-uk Kim 
480f8146b88SJung-uk Kim /*******************************************************************************
481f8146b88SJung-uk Kim  *
482f8146b88SJung-uk Kim  * FUNCTION:    AcValidateTableHeader
483f8146b88SJung-uk Kim  *
484f8146b88SJung-uk Kim  * PARAMETERS:  File                - Open input file
485f8146b88SJung-uk Kim  *
486f8146b88SJung-uk Kim  * RETURN:      Status
487f8146b88SJung-uk Kim  *
488f8146b88SJung-uk Kim  * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
489f8146b88SJung-uk Kim  *              tables, via the
490f8146b88SJung-uk Kim  *              following checks on what would be the table header:
491f8146b88SJung-uk Kim  *              1) File must be at least as long as an ACPI_TABLE_HEADER
492f8146b88SJung-uk Kim  *              2) There must be enough room in the file to hold entire table
493f8146b88SJung-uk Kim  *              3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
494f8146b88SJung-uk Kim  *
495f8146b88SJung-uk Kim  * Note: There can be multiple definition blocks per file, so we cannot
496f8146b88SJung-uk Kim  * expect/compare the file size to be equal to the table length. 12/2015.
497f8146b88SJung-uk Kim  *
498f8146b88SJung-uk Kim  * Note: Maintains current file position.
499f8146b88SJung-uk Kim  *
500f8146b88SJung-uk Kim  ******************************************************************************/
501f8146b88SJung-uk Kim 
502f8146b88SJung-uk Kim ACPI_STATUS
503f8146b88SJung-uk Kim AcValidateTableHeader (
504f8146b88SJung-uk Kim     FILE                    *File,
505f8146b88SJung-uk Kim     long                    TableOffset)
506f8146b88SJung-uk Kim {
507f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       TableHeader;
508493deb39SJung-uk Kim     ACPI_SIZE               Actual;
509f8146b88SJung-uk Kim     long                    OriginalOffset;
510f8146b88SJung-uk Kim     UINT32                  FileSize;
511f8146b88SJung-uk Kim     UINT32                  i;
512f8146b88SJung-uk Kim 
513f8146b88SJung-uk Kim 
51428482948SJung-uk Kim     ACPI_FUNCTION_TRACE (AcValidateTableHeader);
515f8146b88SJung-uk Kim 
516f8146b88SJung-uk Kim 
517f8146b88SJung-uk Kim     /* Read a potential table header */
518f8146b88SJung-uk Kim 
519f8146b88SJung-uk Kim     OriginalOffset = ftell (File);
520f8146b88SJung-uk Kim     fseek (File, TableOffset, SEEK_SET);
521f8146b88SJung-uk Kim 
522f8146b88SJung-uk Kim     Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
523f8146b88SJung-uk Kim     fseek (File, OriginalOffset, SEEK_SET);
524f8146b88SJung-uk Kim 
525f8146b88SJung-uk Kim     if (Actual < sizeof (ACPI_TABLE_HEADER))
526f8146b88SJung-uk Kim     {
527f8146b88SJung-uk Kim         return (AE_ERROR);
528f8146b88SJung-uk Kim     }
529f8146b88SJung-uk Kim 
530f8146b88SJung-uk Kim     /* Validate the signature (limited ASCII chars) */
531f8146b88SJung-uk Kim 
532f8146b88SJung-uk Kim     if (!AcpiUtValidNameseg (TableHeader.Signature))
533f8146b88SJung-uk Kim     {
534f8146b88SJung-uk Kim         fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
535f8146b88SJung-uk Kim             *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
536f8146b88SJung-uk Kim         return (AE_BAD_SIGNATURE);
537f8146b88SJung-uk Kim     }
538f8146b88SJung-uk Kim 
539f8146b88SJung-uk Kim     /* Validate table length against bytes remaining in the file */
540f8146b88SJung-uk Kim 
541f8146b88SJung-uk Kim     FileSize = CmGetFileSize (File);
542f8146b88SJung-uk Kim     if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
543f8146b88SJung-uk Kim     {
544f8146b88SJung-uk Kim         fprintf (stderr, "Table [%4.4s] is too long for file - "
545f8146b88SJung-uk Kim             "needs: 0x%.2X, remaining in file: 0x%.2X\n",
546f8146b88SJung-uk Kim             TableHeader.Signature, TableHeader.Length,
547f8146b88SJung-uk Kim             (UINT32) (FileSize - TableOffset));
548f8146b88SJung-uk Kim         return (AE_BAD_HEADER);
549f8146b88SJung-uk Kim     }
550f8146b88SJung-uk Kim 
551f8146b88SJung-uk Kim     /*
552f8146b88SJung-uk Kim      * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
553f8146b88SJung-uk Kim      * We allow a NULL terminator in OemId and OemTableId.
554f8146b88SJung-uk Kim      */
555f8146b88SJung-uk Kim     for (i = 0; i < ACPI_NAME_SIZE; i++)
556f8146b88SJung-uk Kim     {
557f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
558f8146b88SJung-uk Kim         {
559f8146b88SJung-uk Kim             goto BadCharacters;
560f8146b88SJung-uk Kim         }
561f8146b88SJung-uk Kim     }
562f8146b88SJung-uk Kim 
563f8146b88SJung-uk Kim     for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
564f8146b88SJung-uk Kim     {
565f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
566f8146b88SJung-uk Kim         {
567f8146b88SJung-uk Kim             goto BadCharacters;
568f8146b88SJung-uk Kim         }
569f8146b88SJung-uk Kim     }
570f8146b88SJung-uk Kim 
571f8146b88SJung-uk Kim     for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
572f8146b88SJung-uk Kim     {
573f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
574f8146b88SJung-uk Kim         {
575f8146b88SJung-uk Kim             goto BadCharacters;
576f8146b88SJung-uk Kim         }
577f8146b88SJung-uk Kim     }
578f8146b88SJung-uk Kim 
579f8146b88SJung-uk Kim     return (AE_OK);
580f8146b88SJung-uk Kim 
581f8146b88SJung-uk Kim 
582f8146b88SJung-uk Kim BadCharacters:
583f8146b88SJung-uk Kim 
584f8146b88SJung-uk Kim     ACPI_WARNING ((AE_INFO,
585f8146b88SJung-uk Kim         "Table header for [%4.4s] has invalid ASCII character(s)",
586f8146b88SJung-uk Kim         TableHeader.Signature));
587f8146b88SJung-uk Kim     return (AE_OK);
588f8146b88SJung-uk Kim }
589f8146b88SJung-uk Kim 
590f8146b88SJung-uk Kim 
591f8146b88SJung-uk Kim /*******************************************************************************
592f8146b88SJung-uk Kim  *
593f8146b88SJung-uk Kim  * FUNCTION:    AcCheckTextModeCorruption
594f8146b88SJung-uk Kim  *
595f8146b88SJung-uk Kim  * PARAMETERS:  Table           - Table buffer starting with table header
596f8146b88SJung-uk Kim  *
597f8146b88SJung-uk Kim  * RETURN:      Status
598f8146b88SJung-uk Kim  *
599f8146b88SJung-uk Kim  * DESCRIPTION: Check table for text mode file corruption where all linefeed
600f8146b88SJung-uk Kim  *              characters (LF) have been replaced by carriage return linefeed
601f8146b88SJung-uk Kim  *              pairs (CR/LF).
602f8146b88SJung-uk Kim  *
603f8146b88SJung-uk Kim  ******************************************************************************/
604f8146b88SJung-uk Kim 
605f8146b88SJung-uk Kim static ACPI_STATUS
606f8146b88SJung-uk Kim AcCheckTextModeCorruption (
607f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table)
608f8146b88SJung-uk Kim {
609f8146b88SJung-uk Kim     UINT32                  i;
610f8146b88SJung-uk Kim     UINT32                  Pairs = 0;
611f8146b88SJung-uk Kim     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Table);
612f8146b88SJung-uk Kim 
613f8146b88SJung-uk Kim 
614f8146b88SJung-uk Kim     /* Scan entire table to determine if each LF has been prefixed with a CR */
615f8146b88SJung-uk Kim 
616f8146b88SJung-uk Kim     for (i = 1; i < Table->Length; i++)
617f8146b88SJung-uk Kim     {
618f8146b88SJung-uk Kim         if (Buffer[i] == 0x0A)
619f8146b88SJung-uk Kim         {
620f8146b88SJung-uk Kim             if (Buffer[i - 1] != 0x0D)
621f8146b88SJung-uk Kim             {
622f8146b88SJung-uk Kim                 /* The LF does not have a preceding CR, table not corrupted */
623f8146b88SJung-uk Kim 
624f8146b88SJung-uk Kim                 return (AE_OK);
625f8146b88SJung-uk Kim             }
626f8146b88SJung-uk Kim             else
627f8146b88SJung-uk Kim             {
628f8146b88SJung-uk Kim                 /* Found a CR/LF pair */
629f8146b88SJung-uk Kim 
630f8146b88SJung-uk Kim                 Pairs++;
631f8146b88SJung-uk Kim             }
632f8146b88SJung-uk Kim 
633f8146b88SJung-uk Kim             i++;
634f8146b88SJung-uk Kim         }
635f8146b88SJung-uk Kim     }
636f8146b88SJung-uk Kim 
637f8146b88SJung-uk Kim     if (!Pairs)
638f8146b88SJung-uk Kim     {
639f8146b88SJung-uk Kim         return (AE_OK);
640f8146b88SJung-uk Kim     }
641f8146b88SJung-uk Kim 
642f8146b88SJung-uk Kim     /*
643f8146b88SJung-uk Kim      * Entire table scanned, each CR is part of a CR/LF pair --
644f8146b88SJung-uk Kim      * meaning that the table was treated as a text file somewhere.
645f8146b88SJung-uk Kim      *
646f8146b88SJung-uk Kim      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
647f8146b88SJung-uk Kim      * original table are left untouched by the text conversion process --
648f8146b88SJung-uk Kim      * meaning that we cannot simply replace CR/LF pairs with LFs.
649f8146b88SJung-uk Kim      */
650f8146b88SJung-uk Kim     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
651f8146b88SJung-uk Kim     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
652f8146b88SJung-uk Kim     AcpiOsPrintf ("Table cannot be repaired!\n");
653f8146b88SJung-uk Kim 
654f8146b88SJung-uk Kim     return (AE_BAD_VALUE);
655f8146b88SJung-uk Kim }
656