xref: /freebsd/sys/contrib/dev/acpica/common/acfileio.c (revision cfd1ed46817c680fa7822bc523b054ff644055dc)
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 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
1129f37e9bSJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2021, Intel Corp.
12f8146b88SJung-uk Kim  * All rights reserved.
13f8146b88SJung-uk Kim  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-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  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-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  *
1500d84335fSJung-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  *
1785f9b24faSJung-uk Kim  * FUNCTION:    AcDeleteTableList
1795f9b24faSJung-uk Kim  *
1805f9b24faSJung-uk Kim  * PARAMETERS:  ListHead            - List to delete
1815f9b24faSJung-uk Kim  *
1825f9b24faSJung-uk Kim  * RETURN:      Status
1835f9b24faSJung-uk Kim  *
1845f9b24faSJung-uk Kim  * DESCRIPTION: Delete a list of tables. This is useful for removing memory
1855f9b24faSJung-uk Kim  *              allocated by AcGetAllTablesFromFile
1865f9b24faSJung-uk Kim  *
1875f9b24faSJung-uk Kim  ******************************************************************************/
1885f9b24faSJung-uk Kim 
1895f9b24faSJung-uk Kim void
1905f9b24faSJung-uk Kim AcDeleteTableList (
1915f9b24faSJung-uk Kim     ACPI_NEW_TABLE_DESC     *ListHead)
1925f9b24faSJung-uk Kim {
1935f9b24faSJung-uk Kim     ACPI_NEW_TABLE_DESC     *Current = ListHead;
1945f9b24faSJung-uk Kim     ACPI_NEW_TABLE_DESC     *Previous = Current;
1955f9b24faSJung-uk Kim 
1965f9b24faSJung-uk Kim 
1975f9b24faSJung-uk Kim     while (Current)
1985f9b24faSJung-uk Kim     {
1995f9b24faSJung-uk Kim         Current = Current->Next;
2005f9b24faSJung-uk Kim         AcpiOsFree (Previous);
2015f9b24faSJung-uk Kim         Previous = Current;
2025f9b24faSJung-uk Kim     }
2035f9b24faSJung-uk Kim }
2045f9b24faSJung-uk Kim 
2055f9b24faSJung-uk Kim 
2065f9b24faSJung-uk Kim /*******************************************************************************
2075f9b24faSJung-uk Kim  *
208f8146b88SJung-uk Kim  * FUNCTION:    AcGetAllTablesFromFile
209f8146b88SJung-uk Kim  *
210f8146b88SJung-uk Kim  * PARAMETERS:  Filename            - Table filename
211f8146b88SJung-uk Kim  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables
212f8146b88SJung-uk Kim  *              ReturnListHead      - Where table list is returned
213f8146b88SJung-uk Kim  *
214f8146b88SJung-uk Kim  * RETURN:      Status
215f8146b88SJung-uk Kim  *
216f8146b88SJung-uk Kim  * DESCRIPTION: Get all ACPI tables from within a single file.
217f8146b88SJung-uk Kim  *
218f8146b88SJung-uk Kim  ******************************************************************************/
219f8146b88SJung-uk Kim 
220f8146b88SJung-uk Kim ACPI_STATUS
221f8146b88SJung-uk Kim AcGetAllTablesFromFile (
222f8146b88SJung-uk Kim     char                    *Filename,
223f8146b88SJung-uk Kim     UINT8                   GetOnlyAmlTables,
224f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     **ReturnListHead)
225f8146b88SJung-uk Kim {
226f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *ListHead = NULL;
227f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *ListTail = NULL;
228f8146b88SJung-uk Kim     ACPI_NEW_TABLE_DESC     *TableDesc;
229f8146b88SJung-uk Kim     FILE                    *File;
230f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table = NULL;
231f8146b88SJung-uk Kim     UINT32                  FileSize;
232f8146b88SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
233f8146b88SJung-uk Kim 
234f8146b88SJung-uk Kim 
235f8146b88SJung-uk Kim     File = fopen (Filename, "rb");
236f8146b88SJung-uk Kim     if (!File)
237f8146b88SJung-uk Kim     {
238493deb39SJung-uk Kim         fprintf (stderr, "Could not open input file: %s\n", Filename);
239f8146b88SJung-uk Kim         if (errno == ENOENT)
240f8146b88SJung-uk Kim         {
241f8146b88SJung-uk Kim             return (AE_NOT_EXIST);
242f8146b88SJung-uk Kim         }
243f8146b88SJung-uk Kim 
244f8146b88SJung-uk Kim         return (AE_ERROR);
245f8146b88SJung-uk Kim     }
246f8146b88SJung-uk Kim 
247f8146b88SJung-uk Kim     /* Get the file size */
248f8146b88SJung-uk Kim 
249f8146b88SJung-uk Kim     FileSize = CmGetFileSize (File);
250f8146b88SJung-uk Kim     if (FileSize == ACPI_UINT32_MAX)
251f8146b88SJung-uk Kim     {
252f8146b88SJung-uk Kim         Status = AE_ERROR;
2531cc50d6bSJung-uk Kim         goto Exit;
254f8146b88SJung-uk Kim     }
255f8146b88SJung-uk Kim 
256f8146b88SJung-uk Kim     fprintf (stderr,
257f8146b88SJung-uk Kim         "Input file %s, Length 0x%X (%u) bytes\n",
258f8146b88SJung-uk Kim         Filename, FileSize, FileSize);
259f8146b88SJung-uk Kim 
260f8146b88SJung-uk Kim     /* We must have at least one ACPI table header */
261f8146b88SJung-uk Kim 
262f8146b88SJung-uk Kim     if (FileSize < sizeof (ACPI_TABLE_HEADER))
263f8146b88SJung-uk Kim     {
264f8146b88SJung-uk Kim         Status = AE_BAD_HEADER;
2651cc50d6bSJung-uk Kim         goto Exit;
266f8146b88SJung-uk Kim     }
267f8146b88SJung-uk Kim 
268f8146b88SJung-uk Kim     /* Check for an non-binary file */
269f8146b88SJung-uk Kim 
270f8146b88SJung-uk Kim     if (!AcIsFileBinary (File))
271f8146b88SJung-uk Kim     {
272f8146b88SJung-uk Kim         fprintf (stderr,
273f8146b88SJung-uk Kim             "    %s: File does not appear to contain a valid AML table\n",
274f8146b88SJung-uk Kim             Filename);
2751cc50d6bSJung-uk Kim         Status = AE_TYPE;
2761cc50d6bSJung-uk Kim         goto Exit;
277f8146b88SJung-uk Kim     }
278f8146b88SJung-uk Kim 
279f8146b88SJung-uk Kim     /* Read all tables within the file */
280f8146b88SJung-uk Kim 
281f8146b88SJung-uk Kim     while (ACPI_SUCCESS (Status))
282f8146b88SJung-uk Kim     {
283f8146b88SJung-uk Kim         /* Get one entire ACPI table */
284f8146b88SJung-uk Kim 
285f8146b88SJung-uk Kim         Status = AcGetOneTableFromFile (
286f8146b88SJung-uk Kim             Filename, File, GetOnlyAmlTables, &Table);
287f8146b88SJung-uk Kim 
288f8146b88SJung-uk Kim         if (Status == AE_CTRL_TERMINATE)
289f8146b88SJung-uk Kim         {
290f8146b88SJung-uk Kim             Status = AE_OK;
291f8146b88SJung-uk Kim             break;
292f8146b88SJung-uk Kim         }
293f8146b88SJung-uk Kim         else if (Status == AE_TYPE)
294f8146b88SJung-uk Kim         {
2951cc50d6bSJung-uk Kim             Status = AE_OK;
2961cc50d6bSJung-uk Kim             goto Exit;
297f8146b88SJung-uk Kim         }
298f8146b88SJung-uk Kim         else if (ACPI_FAILURE (Status))
299f8146b88SJung-uk Kim         {
3001cc50d6bSJung-uk Kim             goto Exit;
301f8146b88SJung-uk Kim         }
302f8146b88SJung-uk Kim 
303f8146b88SJung-uk Kim         /* Print table header for iASL/disassembler only */
304f8146b88SJung-uk Kim 
305f8146b88SJung-uk Kim #ifdef ACPI_ASL_COMPILER
306f8146b88SJung-uk Kim 
307f8146b88SJung-uk Kim         AcpiTbPrintTableHeader (0, Table);
308f8146b88SJung-uk Kim #endif
309f8146b88SJung-uk Kim 
310f8146b88SJung-uk Kim         /* Allocate and link a table descriptor */
311f8146b88SJung-uk Kim 
312f8146b88SJung-uk Kim         TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
3131cc50d6bSJung-uk Kim         if (!TableDesc)
3141cc50d6bSJung-uk Kim         {
3151cc50d6bSJung-uk Kim             AcpiOsFree (Table);
3161cc50d6bSJung-uk Kim             Status = AE_NO_MEMORY;
3171cc50d6bSJung-uk Kim             goto Exit;
3181cc50d6bSJung-uk Kim         }
3191cc50d6bSJung-uk Kim 
320f8146b88SJung-uk Kim         TableDesc->Table = Table;
321f8146b88SJung-uk Kim         TableDesc->Next = NULL;
322f8146b88SJung-uk Kim 
323f8146b88SJung-uk Kim         /* Link at the end of the local table list */
324f8146b88SJung-uk Kim 
325f8146b88SJung-uk Kim         if (!ListHead)
326f8146b88SJung-uk Kim         {
327f8146b88SJung-uk Kim             ListHead = TableDesc;
328f8146b88SJung-uk Kim             ListTail = TableDesc;
329f8146b88SJung-uk Kim         }
330f8146b88SJung-uk Kim         else
331f8146b88SJung-uk Kim         {
332f8146b88SJung-uk Kim             ListTail->Next = TableDesc;
333f8146b88SJung-uk Kim             ListTail = TableDesc;
334f8146b88SJung-uk Kim         }
335f8146b88SJung-uk Kim     }
336f8146b88SJung-uk Kim 
337f8146b88SJung-uk Kim     /* Add the local table list to the end of the global list */
338f8146b88SJung-uk Kim 
339f8146b88SJung-uk Kim     if (*ReturnListHead)
340f8146b88SJung-uk Kim     {
341f8146b88SJung-uk Kim         ListTail = *ReturnListHead;
342f8146b88SJung-uk Kim         while (ListTail->Next)
343f8146b88SJung-uk Kim         {
344f8146b88SJung-uk Kim             ListTail = ListTail->Next;
345f8146b88SJung-uk Kim         }
346f8146b88SJung-uk Kim 
347f8146b88SJung-uk Kim         ListTail->Next = ListHead;
348f8146b88SJung-uk Kim     }
349f8146b88SJung-uk Kim     else
350f8146b88SJung-uk Kim     {
351f8146b88SJung-uk Kim         *ReturnListHead = ListHead;
352f8146b88SJung-uk Kim     }
353f8146b88SJung-uk Kim 
3541cc50d6bSJung-uk Kim Exit:
355f8146b88SJung-uk Kim     fclose(File);
356f8146b88SJung-uk Kim     return (Status);
357f8146b88SJung-uk Kim }
358f8146b88SJung-uk Kim 
359f8146b88SJung-uk Kim 
360f8146b88SJung-uk Kim /*******************************************************************************
361f8146b88SJung-uk Kim  *
362f8146b88SJung-uk Kim  * FUNCTION:    AcGetOneTableFromFile
363f8146b88SJung-uk Kim  *
364f8146b88SJung-uk Kim  * PARAMETERS:  Filename            - File where table is located
365f8146b88SJung-uk Kim  *              File                - Open FILE pointer to Filename
366f8146b88SJung-uk Kim  *              GetOnlyAmlTables    - TRUE if the tables must be AML tables.
367f8146b88SJung-uk Kim  *              ReturnTable         - Where a pointer to the table is returned
368f8146b88SJung-uk Kim  *
369f8146b88SJung-uk Kim  * RETURN:      Status
370f8146b88SJung-uk Kim  *
371f8146b88SJung-uk Kim  * DESCRIPTION: Read the next ACPI table from a file. Implements support
372f8146b88SJung-uk Kim  *              for multiple tables within a single file. File must already
373f8146b88SJung-uk Kim  *              be open.
374f8146b88SJung-uk Kim  *
375f8146b88SJung-uk Kim  * Note: Loading an RSDP is not supported.
376f8146b88SJung-uk Kim  *
377f8146b88SJung-uk Kim  ******************************************************************************/
378f8146b88SJung-uk Kim 
379f8146b88SJung-uk Kim static ACPI_STATUS
380f8146b88SJung-uk Kim AcGetOneTableFromFile (
381f8146b88SJung-uk Kim     char                    *Filename,
382f8146b88SJung-uk Kim     FILE                    *File,
383f8146b88SJung-uk Kim     UINT8                   GetOnlyAmlTables,
384f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       **ReturnTable)
385f8146b88SJung-uk Kim {
386f8146b88SJung-uk Kim     ACPI_STATUS             Status = AE_OK;
387f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       TableHeader;
388f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table;
389f8146b88SJung-uk Kim     INT32                   Count;
390f8146b88SJung-uk Kim     long                    TableOffset;
391f8146b88SJung-uk Kim 
392f8146b88SJung-uk Kim 
393f8146b88SJung-uk Kim     *ReturnTable = NULL;
394f8146b88SJung-uk Kim 
395f8146b88SJung-uk Kim     /* Get the table header to examine signature and length */
396f8146b88SJung-uk Kim 
397f8146b88SJung-uk Kim     TableOffset = ftell (File);
398f8146b88SJung-uk Kim     Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
399f8146b88SJung-uk Kim     if (Count != sizeof (ACPI_TABLE_HEADER))
400f8146b88SJung-uk Kim     {
401f8146b88SJung-uk Kim         return (AE_CTRL_TERMINATE);
402f8146b88SJung-uk Kim     }
403f8146b88SJung-uk Kim 
4043d90091dSJung-uk Kim     if (GetOnlyAmlTables)
4053d90091dSJung-uk Kim     {
406f8146b88SJung-uk Kim         /* Validate the table signature/header (limited ASCII chars) */
407f8146b88SJung-uk Kim 
408f8146b88SJung-uk Kim         Status = AcValidateTableHeader (File, TableOffset);
409f8146b88SJung-uk Kim         if (ACPI_FAILURE (Status))
410f8146b88SJung-uk Kim         {
411f8146b88SJung-uk Kim             return (Status);
412f8146b88SJung-uk Kim         }
413f8146b88SJung-uk Kim 
414493deb39SJung-uk Kim         /*
415493deb39SJung-uk Kim          * Table must be an AML table (DSDT/SSDT).
416493deb39SJung-uk Kim          * Used for iASL -e option only.
417493deb39SJung-uk Kim          */
418493deb39SJung-uk Kim         if (!AcpiUtIsAmlTable (&TableHeader))
419f8146b88SJung-uk Kim         {
420f8146b88SJung-uk Kim             fprintf (stderr,
421f8146b88SJung-uk Kim                 "    %s: Table [%4.4s] is not an AML table - ignoring\n",
422f8146b88SJung-uk Kim                 Filename, TableHeader.Signature);
423f8146b88SJung-uk Kim 
424f8146b88SJung-uk Kim             return (AE_TYPE);
425f8146b88SJung-uk Kim         }
426f8146b88SJung-uk Kim     }
427f8146b88SJung-uk Kim 
428f8146b88SJung-uk Kim     /* Allocate a buffer for the entire table */
429f8146b88SJung-uk Kim 
430493deb39SJung-uk Kim     Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
431f8146b88SJung-uk Kim     if (!Table)
432f8146b88SJung-uk Kim     {
433f8146b88SJung-uk Kim         return (AE_NO_MEMORY);
434f8146b88SJung-uk Kim     }
435f8146b88SJung-uk Kim 
436f8146b88SJung-uk Kim     /* Read the entire ACPI table, including header */
437f8146b88SJung-uk Kim 
438f8146b88SJung-uk Kim     fseek (File, TableOffset, SEEK_SET);
439f8146b88SJung-uk Kim 
440f8146b88SJung-uk Kim     Count = fread (Table, 1, TableHeader.Length, File);
4413d90091dSJung-uk Kim 
4423d90091dSJung-uk Kim     /*
4433d90091dSJung-uk Kim      * Checks for data table headers happen later in the execution. Only verify
4443d90091dSJung-uk Kim      * for Aml tables at this point in the code.
4453d90091dSJung-uk Kim      */
4463d90091dSJung-uk Kim     if (GetOnlyAmlTables && Count != (INT32) TableHeader.Length)
447f8146b88SJung-uk Kim     {
448f8146b88SJung-uk Kim         Status = AE_ERROR;
449f8146b88SJung-uk Kim         goto ErrorExit;
450f8146b88SJung-uk Kim     }
451f8146b88SJung-uk Kim 
452f8146b88SJung-uk Kim     /* Validate the checksum (just issue a warning) */
453f8146b88SJung-uk Kim 
454f8146b88SJung-uk Kim     Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
455f8146b88SJung-uk Kim     if (ACPI_FAILURE (Status))
456f8146b88SJung-uk Kim     {
457f8146b88SJung-uk Kim         Status = AcCheckTextModeCorruption (Table);
458f8146b88SJung-uk Kim         if (ACPI_FAILURE (Status))
459f8146b88SJung-uk Kim         {
460f8146b88SJung-uk Kim             goto ErrorExit;
461f8146b88SJung-uk Kim         }
462f8146b88SJung-uk Kim     }
463f8146b88SJung-uk Kim 
464f8146b88SJung-uk Kim     *ReturnTable = Table;
465f8146b88SJung-uk Kim     return (AE_OK);
466f8146b88SJung-uk Kim 
467f8146b88SJung-uk Kim 
468f8146b88SJung-uk Kim ErrorExit:
469f8146b88SJung-uk Kim     AcpiOsFree (Table);
470f8146b88SJung-uk Kim     return (Status);
471f8146b88SJung-uk Kim }
472f8146b88SJung-uk Kim 
473f8146b88SJung-uk Kim 
474f8146b88SJung-uk Kim /*******************************************************************************
475f8146b88SJung-uk Kim  *
476f8146b88SJung-uk Kim  * FUNCTION:    AcIsFileBinary
477f8146b88SJung-uk Kim  *
478f8146b88SJung-uk Kim  * PARAMETERS:  File                - Open input file
479f8146b88SJung-uk Kim  *
480f8146b88SJung-uk Kim  * RETURN:      TRUE if file appears to be binary
481f8146b88SJung-uk Kim  *
482f8146b88SJung-uk Kim  * DESCRIPTION: Scan a file for any non-ASCII bytes.
483f8146b88SJung-uk Kim  *
484f8146b88SJung-uk Kim  * Note: Maintains current file position.
485f8146b88SJung-uk Kim  *
486f8146b88SJung-uk Kim  ******************************************************************************/
487f8146b88SJung-uk Kim 
488f8146b88SJung-uk Kim BOOLEAN
489f8146b88SJung-uk Kim AcIsFileBinary (
490f8146b88SJung-uk Kim     FILE                    *File)
491f8146b88SJung-uk Kim {
492f8146b88SJung-uk Kim     UINT8                   Byte;
493f8146b88SJung-uk Kim     BOOLEAN                 IsBinary = FALSE;
494f8146b88SJung-uk Kim     long                    FileOffset;
495f8146b88SJung-uk Kim 
496f8146b88SJung-uk Kim 
497f8146b88SJung-uk Kim     /* Scan entire file for any non-ASCII bytes */
498f8146b88SJung-uk Kim 
499f8146b88SJung-uk Kim     FileOffset = ftell (File);
500f8146b88SJung-uk Kim     while (fread (&Byte, 1, 1, File) == 1)
501f8146b88SJung-uk Kim     {
502f8146b88SJung-uk Kim         if (!isprint (Byte) && !isspace (Byte))
503f8146b88SJung-uk Kim         {
504f8146b88SJung-uk Kim             IsBinary = TRUE;
505f8146b88SJung-uk Kim             goto Exit;
506f8146b88SJung-uk Kim         }
507f8146b88SJung-uk Kim     }
508f8146b88SJung-uk Kim 
509f8146b88SJung-uk Kim Exit:
510f8146b88SJung-uk Kim     fseek (File, FileOffset, SEEK_SET);
511f8146b88SJung-uk Kim     return (IsBinary);
512f8146b88SJung-uk Kim }
513f8146b88SJung-uk Kim 
514f8146b88SJung-uk Kim 
515f8146b88SJung-uk Kim /*******************************************************************************
516f8146b88SJung-uk Kim  *
517f8146b88SJung-uk Kim  * FUNCTION:    AcValidateTableHeader
518f8146b88SJung-uk Kim  *
519f8146b88SJung-uk Kim  * PARAMETERS:  File                - Open input file
520f8146b88SJung-uk Kim  *
521f8146b88SJung-uk Kim  * RETURN:      Status
522f8146b88SJung-uk Kim  *
523f8146b88SJung-uk Kim  * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
524f8146b88SJung-uk Kim  *              tables, via the
525f8146b88SJung-uk Kim  *              following checks on what would be the table header:
526f8146b88SJung-uk Kim  *              1) File must be at least as long as an ACPI_TABLE_HEADER
527f8146b88SJung-uk Kim  *              2) There must be enough room in the file to hold entire table
528f8146b88SJung-uk Kim  *              3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
529f8146b88SJung-uk Kim  *
530f8146b88SJung-uk Kim  * Note: There can be multiple definition blocks per file, so we cannot
531f8146b88SJung-uk Kim  * expect/compare the file size to be equal to the table length. 12/2015.
532f8146b88SJung-uk Kim  *
533f8146b88SJung-uk Kim  * Note: Maintains current file position.
534f8146b88SJung-uk Kim  *
535f8146b88SJung-uk Kim  ******************************************************************************/
536f8146b88SJung-uk Kim 
537f8146b88SJung-uk Kim ACPI_STATUS
538f8146b88SJung-uk Kim AcValidateTableHeader (
539f8146b88SJung-uk Kim     FILE                    *File,
540f8146b88SJung-uk Kim     long                    TableOffset)
541f8146b88SJung-uk Kim {
542f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       TableHeader;
543493deb39SJung-uk Kim     ACPI_SIZE               Actual;
544f8146b88SJung-uk Kim     long                    OriginalOffset;
545f8146b88SJung-uk Kim     UINT32                  FileSize;
546f8146b88SJung-uk Kim     UINT32                  i;
547f8146b88SJung-uk Kim 
548f8146b88SJung-uk Kim 
54928482948SJung-uk Kim     ACPI_FUNCTION_TRACE (AcValidateTableHeader);
550f8146b88SJung-uk Kim 
551f8146b88SJung-uk Kim 
552f8146b88SJung-uk Kim     /* Read a potential table header */
553f8146b88SJung-uk Kim 
554f8146b88SJung-uk Kim     OriginalOffset = ftell (File);
555f8146b88SJung-uk Kim     fseek (File, TableOffset, SEEK_SET);
556f8146b88SJung-uk Kim 
557f8146b88SJung-uk Kim     Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
558f8146b88SJung-uk Kim     fseek (File, OriginalOffset, SEEK_SET);
559f8146b88SJung-uk Kim 
560f8146b88SJung-uk Kim     if (Actual < sizeof (ACPI_TABLE_HEADER))
561f8146b88SJung-uk Kim     {
562*cfd1ed46SJung-uk Kim         fprintf (stderr,
563*cfd1ed46SJung-uk Kim             "Could not read entire table header: Actual %u, Requested %u\n",
564*cfd1ed46SJung-uk Kim             (UINT32) Actual, (UINT32) sizeof (ACPI_TABLE_HEADER));
565f8146b88SJung-uk Kim         return (AE_ERROR);
566f8146b88SJung-uk Kim     }
567f8146b88SJung-uk Kim 
568f8146b88SJung-uk Kim     /* Validate the signature (limited ASCII chars) */
569f8146b88SJung-uk Kim 
570f8146b88SJung-uk Kim     if (!AcpiUtValidNameseg (TableHeader.Signature))
571f8146b88SJung-uk Kim     {
572f8146b88SJung-uk Kim         return (AE_BAD_SIGNATURE);
573f8146b88SJung-uk Kim     }
574f8146b88SJung-uk Kim 
575f8146b88SJung-uk Kim     /* Validate table length against bytes remaining in the file */
576f8146b88SJung-uk Kim 
577f8146b88SJung-uk Kim     FileSize = CmGetFileSize (File);
578f8146b88SJung-uk Kim     if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
579f8146b88SJung-uk Kim     {
580f8146b88SJung-uk Kim         fprintf (stderr, "Table [%4.4s] is too long for file - "
581f8146b88SJung-uk Kim             "needs: 0x%.2X, remaining in file: 0x%.2X\n",
582f8146b88SJung-uk Kim             TableHeader.Signature, TableHeader.Length,
583f8146b88SJung-uk Kim             (UINT32) (FileSize - TableOffset));
584f8146b88SJung-uk Kim         return (AE_BAD_HEADER);
585f8146b88SJung-uk Kim     }
586f8146b88SJung-uk Kim 
587f8146b88SJung-uk Kim     /*
588f8146b88SJung-uk Kim      * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
589f8146b88SJung-uk Kim      * We allow a NULL terminator in OemId and OemTableId.
590f8146b88SJung-uk Kim      */
591278f0de6SJung-uk Kim     for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
592f8146b88SJung-uk Kim     {
593f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
594f8146b88SJung-uk Kim         {
595f8146b88SJung-uk Kim             goto BadCharacters;
596f8146b88SJung-uk Kim         }
597f8146b88SJung-uk Kim     }
598f8146b88SJung-uk Kim 
599f8146b88SJung-uk Kim     for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
600f8146b88SJung-uk Kim     {
601f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
602f8146b88SJung-uk Kim         {
603f8146b88SJung-uk Kim             goto BadCharacters;
604f8146b88SJung-uk Kim         }
605f8146b88SJung-uk Kim     }
606f8146b88SJung-uk Kim 
607f8146b88SJung-uk Kim     for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
608f8146b88SJung-uk Kim     {
609f8146b88SJung-uk Kim         if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
610f8146b88SJung-uk Kim         {
611f8146b88SJung-uk Kim             goto BadCharacters;
612f8146b88SJung-uk Kim         }
613f8146b88SJung-uk Kim     }
614f8146b88SJung-uk Kim 
615f8146b88SJung-uk Kim     return (AE_OK);
616f8146b88SJung-uk Kim 
617f8146b88SJung-uk Kim 
618f8146b88SJung-uk Kim BadCharacters:
619f8146b88SJung-uk Kim 
620f8146b88SJung-uk Kim     ACPI_WARNING ((AE_INFO,
621f8146b88SJung-uk Kim         "Table header for [%4.4s] has invalid ASCII character(s)",
622f8146b88SJung-uk Kim         TableHeader.Signature));
623f8146b88SJung-uk Kim     return (AE_OK);
624f8146b88SJung-uk Kim }
625f8146b88SJung-uk Kim 
626f8146b88SJung-uk Kim 
627f8146b88SJung-uk Kim /*******************************************************************************
628f8146b88SJung-uk Kim  *
629f8146b88SJung-uk Kim  * FUNCTION:    AcCheckTextModeCorruption
630f8146b88SJung-uk Kim  *
631f8146b88SJung-uk Kim  * PARAMETERS:  Table           - Table buffer starting with table header
632f8146b88SJung-uk Kim  *
633f8146b88SJung-uk Kim  * RETURN:      Status
634f8146b88SJung-uk Kim  *
635f8146b88SJung-uk Kim  * DESCRIPTION: Check table for text mode file corruption where all linefeed
636f8146b88SJung-uk Kim  *              characters (LF) have been replaced by carriage return linefeed
637f8146b88SJung-uk Kim  *              pairs (CR/LF).
638f8146b88SJung-uk Kim  *
639f8146b88SJung-uk Kim  ******************************************************************************/
640f8146b88SJung-uk Kim 
641f8146b88SJung-uk Kim static ACPI_STATUS
642f8146b88SJung-uk Kim AcCheckTextModeCorruption (
643f8146b88SJung-uk Kim     ACPI_TABLE_HEADER       *Table)
644f8146b88SJung-uk Kim {
645f8146b88SJung-uk Kim     UINT32                  i;
646f8146b88SJung-uk Kim     UINT32                  Pairs = 0;
647f8146b88SJung-uk Kim     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Table);
648f8146b88SJung-uk Kim 
649f8146b88SJung-uk Kim 
650f8146b88SJung-uk Kim     /* Scan entire table to determine if each LF has been prefixed with a CR */
651f8146b88SJung-uk Kim 
652f8146b88SJung-uk Kim     for (i = 1; i < Table->Length; i++)
653f8146b88SJung-uk Kim     {
654f8146b88SJung-uk Kim         if (Buffer[i] == 0x0A)
655f8146b88SJung-uk Kim         {
656f8146b88SJung-uk Kim             if (Buffer[i - 1] != 0x0D)
657f8146b88SJung-uk Kim             {
658f8146b88SJung-uk Kim                 /* The LF does not have a preceding CR, table not corrupted */
659f8146b88SJung-uk Kim 
660f8146b88SJung-uk Kim                 return (AE_OK);
661f8146b88SJung-uk Kim             }
662f8146b88SJung-uk Kim             else
663f8146b88SJung-uk Kim             {
664f8146b88SJung-uk Kim                 /* Found a CR/LF pair */
665f8146b88SJung-uk Kim 
666f8146b88SJung-uk Kim                 Pairs++;
667f8146b88SJung-uk Kim             }
668f8146b88SJung-uk Kim 
669f8146b88SJung-uk Kim             i++;
670f8146b88SJung-uk Kim         }
671f8146b88SJung-uk Kim     }
672f8146b88SJung-uk Kim 
673f8146b88SJung-uk Kim     if (!Pairs)
674f8146b88SJung-uk Kim     {
675f8146b88SJung-uk Kim         return (AE_OK);
676f8146b88SJung-uk Kim     }
677f8146b88SJung-uk Kim 
678f8146b88SJung-uk Kim     /*
679f8146b88SJung-uk Kim      * Entire table scanned, each CR is part of a CR/LF pair --
680f8146b88SJung-uk Kim      * meaning that the table was treated as a text file somewhere.
681f8146b88SJung-uk Kim      *
682f8146b88SJung-uk Kim      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
683f8146b88SJung-uk Kim      * original table are left untouched by the text conversion process --
684f8146b88SJung-uk Kim      * meaning that we cannot simply replace CR/LF pairs with LFs.
685f8146b88SJung-uk Kim      */
686f8146b88SJung-uk Kim     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
687f8146b88SJung-uk Kim     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
688f8146b88SJung-uk Kim     AcpiOsPrintf ("Table cannot be repaired!\n");
689f8146b88SJung-uk Kim 
690f8146b88SJung-uk Kim     return (AE_BAD_VALUE);
691f8146b88SJung-uk Kim }
692