1 /******************************************************************************
3 * Module Name: acfileio - Get ACPI tables from file
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/actables.h>
47 #include <contrib/dev/acpica/include/acutils.h>
48 #include <contrib/dev/acpica/include/acapps.h>
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("acfileio")
54 /* Local prototypes */
57 AcGetOneTableFromFile (
60 UINT8 GetOnlyAmlTables,
61 ACPI_TABLE_HEADER **Table);
64 AcCheckTextModeCorruption (
65 ACPI_TABLE_HEADER *Table);
68 /*******************************************************************************
70 * FUNCTION: AcGetAllTablesFromFile
72 * PARAMETERS: Filename - Table filename
73 * GetOnlyAmlTables - TRUE if the tables must be AML tables
74 * ReturnListHead - Where table list is returned
78 * DESCRIPTION: Get all ACPI tables from within a single file.
80 ******************************************************************************/
83 AcGetAllTablesFromFile (
85 UINT8 GetOnlyAmlTables,
86 ACPI_NEW_TABLE_DESC **ReturnListHead)
88 ACPI_NEW_TABLE_DESC *ListHead = NULL;
89 ACPI_NEW_TABLE_DESC *ListTail = NULL;
90 ACPI_NEW_TABLE_DESC *TableDesc;
92 ACPI_TABLE_HEADER *Table = NULL;
94 ACPI_STATUS Status = AE_OK;
97 File = fopen (Filename, "rb");
100 fprintf (stderr, "Could not open input file: %s\n", Filename);
103 return (AE_NOT_EXIST);
109 /* Get the file size */
111 FileSize = CmGetFileSize (File);
112 if (FileSize == ACPI_UINT32_MAX)
119 "Input file %s, Length 0x%X (%u) bytes\n",
120 Filename, FileSize, FileSize);
122 /* We must have at least one ACPI table header */
124 if (FileSize < sizeof (ACPI_TABLE_HEADER))
126 Status = AE_BAD_HEADER;
130 /* Check for an non-binary file */
132 if (!AcIsFileBinary (File))
135 " %s: File does not appear to contain a valid AML table\n",
141 /* Read all tables within the file */
143 while (ACPI_SUCCESS (Status))
145 /* Get one entire ACPI table */
147 Status = AcGetOneTableFromFile (
148 Filename, File, GetOnlyAmlTables, &Table);
150 if (Status == AE_CTRL_TERMINATE)
155 else if (Status == AE_TYPE)
160 else if (ACPI_FAILURE (Status))
165 /* Print table header for iASL/disassembler only */
167 #ifdef ACPI_ASL_COMPILER
169 AcpiTbPrintTableHeader (0, Table);
172 /* Allocate and link a table descriptor */
174 TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
178 Status = AE_NO_MEMORY;
182 TableDesc->Table = Table;
183 TableDesc->Next = NULL;
185 /* Link at the end of the local table list */
189 ListHead = TableDesc;
190 ListTail = TableDesc;
194 ListTail->Next = TableDesc;
195 ListTail = TableDesc;
199 /* Add the local table list to the end of the global list */
203 ListTail = *ReturnListHead;
204 while (ListTail->Next)
206 ListTail = ListTail->Next;
209 ListTail->Next = ListHead;
213 *ReturnListHead = ListHead;
222 /*******************************************************************************
224 * FUNCTION: AcGetOneTableFromFile
226 * PARAMETERS: Filename - File where table is located
227 * File - Open FILE pointer to Filename
228 * GetOnlyAmlTables - TRUE if the tables must be AML tables.
229 * ReturnTable - Where a pointer to the table is returned
233 * DESCRIPTION: Read the next ACPI table from a file. Implements support
234 * for multiple tables within a single file. File must already
237 * Note: Loading an RSDP is not supported.
239 ******************************************************************************/
242 AcGetOneTableFromFile (
245 UINT8 GetOnlyAmlTables,
246 ACPI_TABLE_HEADER **ReturnTable)
248 ACPI_STATUS Status = AE_OK;
249 ACPI_TABLE_HEADER TableHeader;
250 ACPI_TABLE_HEADER *Table;
257 /* Get the table header to examine signature and length */
259 TableOffset = ftell (File);
260 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
261 if (Count != sizeof (ACPI_TABLE_HEADER))
263 return (AE_CTRL_TERMINATE);
266 /* Validate the table signature/header (limited ASCII chars) */
268 Status = AcValidateTableHeader (File, TableOffset);
269 if (ACPI_FAILURE (Status))
274 if (GetOnlyAmlTables)
277 * Table must be an AML table (DSDT/SSDT).
278 * Used for iASL -e option only.
280 if (!AcpiUtIsAmlTable (&TableHeader))
283 " %s: Table [%4.4s] is not an AML table - ignoring\n",
284 Filename, TableHeader.Signature);
290 /* Allocate a buffer for the entire table */
292 Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
295 return (AE_NO_MEMORY);
298 /* Read the entire ACPI table, including header */
300 fseek (File, TableOffset, SEEK_SET);
302 Count = fread (Table, 1, TableHeader.Length, File);
303 if (Count != (INT32) TableHeader.Length)
309 /* Validate the checksum (just issue a warning) */
311 Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
312 if (ACPI_FAILURE (Status))
314 Status = AcCheckTextModeCorruption (Table);
315 if (ACPI_FAILURE (Status))
321 *ReturnTable = Table;
331 /*******************************************************************************
333 * FUNCTION: AcIsFileBinary
335 * PARAMETERS: File - Open input file
337 * RETURN: TRUE if file appears to be binary
339 * DESCRIPTION: Scan a file for any non-ASCII bytes.
341 * Note: Maintains current file position.
343 ******************************************************************************/
350 BOOLEAN IsBinary = FALSE;
354 /* Scan entire file for any non-ASCII bytes */
356 FileOffset = ftell (File);
357 while (fread (&Byte, 1, 1, File) == 1)
359 if (!isprint (Byte) && !isspace (Byte))
367 fseek (File, FileOffset, SEEK_SET);
372 /*******************************************************************************
374 * FUNCTION: AcValidateTableHeader
376 * PARAMETERS: File - Open input file
380 * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
382 * following checks on what would be the table header:
383 * 1) File must be at least as long as an ACPI_TABLE_HEADER
384 * 2) There must be enough room in the file to hold entire table
385 * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
387 * Note: There can be multiple definition blocks per file, so we cannot
388 * expect/compare the file size to be equal to the table length. 12/2015.
390 * Note: Maintains current file position.
392 ******************************************************************************/
395 AcValidateTableHeader (
399 ACPI_TABLE_HEADER TableHeader;
406 ACPI_FUNCTION_TRACE (AcValidateTableHeader);
409 /* Read a potential table header */
411 OriginalOffset = ftell (File);
412 fseek (File, TableOffset, SEEK_SET);
414 Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
415 fseek (File, OriginalOffset, SEEK_SET);
417 if (Actual < sizeof (ACPI_TABLE_HEADER))
422 /* Validate the signature (limited ASCII chars) */
424 if (!AcpiUtValidNameseg (TableHeader.Signature))
426 fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
427 *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
428 return (AE_BAD_SIGNATURE);
431 /* Validate table length against bytes remaining in the file */
433 FileSize = CmGetFileSize (File);
434 if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
436 fprintf (stderr, "Table [%4.4s] is too long for file - "
437 "needs: 0x%.2X, remaining in file: 0x%.2X\n",
438 TableHeader.Signature, TableHeader.Length,
439 (UINT32) (FileSize - TableOffset));
440 return (AE_BAD_HEADER);
444 * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
445 * We allow a NULL terminator in OemId and OemTableId.
447 for (i = 0; i < ACPI_NAME_SIZE; i++)
449 if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
455 for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
457 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
463 for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
465 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
476 ACPI_WARNING ((AE_INFO,
477 "Table header for [%4.4s] has invalid ASCII character(s)",
478 TableHeader.Signature));
483 /*******************************************************************************
485 * FUNCTION: AcCheckTextModeCorruption
487 * PARAMETERS: Table - Table buffer starting with table header
491 * DESCRIPTION: Check table for text mode file corruption where all linefeed
492 * characters (LF) have been replaced by carriage return linefeed
495 ******************************************************************************/
498 AcCheckTextModeCorruption (
499 ACPI_TABLE_HEADER *Table)
503 UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
506 /* Scan entire table to determine if each LF has been prefixed with a CR */
508 for (i = 1; i < Table->Length; i++)
510 if (Buffer[i] == 0x0A)
512 if (Buffer[i - 1] != 0x0D)
514 /* The LF does not have a preceding CR, table not corrupted */
520 /* Found a CR/LF pair */
535 * Entire table scanned, each CR is part of a CR/LF pair --
536 * meaning that the table was treated as a text file somewhere.
538 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
539 * original table are left untouched by the text conversion process --
540 * meaning that we cannot simply replace CR/LF pairs with LFs.
542 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
543 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
544 AcpiOsPrintf ("Table cannot be repaired!\n");
546 return (AE_BAD_VALUE);