1 /******************************************************************************
3 * Module Name: acfileio - Get ACPI tables from file
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
51 #define _COMPONENT ACPI_UTILITIES
52 ACPI_MODULE_NAME ("acfileio")
55 /* Local prototypes */
58 AcGetOneTableFromFile (
61 UINT8 GetOnlyAmlTables,
62 ACPI_TABLE_HEADER **Table);
65 AcCheckTextModeCorruption (
66 ACPI_TABLE_HEADER *Table);
69 /*******************************************************************************
71 * FUNCTION: AcGetAllTablesFromFile
73 * PARAMETERS: Filename - Table filename
74 * GetOnlyAmlTables - TRUE if the tables must be AML tables
75 * ReturnListHead - Where table list is returned
79 * DESCRIPTION: Get all ACPI tables from within a single file.
81 ******************************************************************************/
84 AcGetAllTablesFromFile (
86 UINT8 GetOnlyAmlTables,
87 ACPI_NEW_TABLE_DESC **ReturnListHead)
89 ACPI_NEW_TABLE_DESC *ListHead = NULL;
90 ACPI_NEW_TABLE_DESC *ListTail = NULL;
91 ACPI_NEW_TABLE_DESC *TableDesc;
93 ACPI_TABLE_HEADER *Table = NULL;
95 ACPI_STATUS Status = AE_OK;
98 File = fopen (Filename, "rb");
101 perror ("Could not open input file");
104 return (AE_NOT_EXIST);
110 /* Get the file size */
112 FileSize = CmGetFileSize (File);
113 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 return (AE_BAD_HEADER);
129 /* Check for an non-binary file */
131 if (!AcIsFileBinary (File))
134 " %s: File does not appear to contain a valid AML table\n",
139 /* Read all tables within the file */
141 while (ACPI_SUCCESS (Status))
143 /* Get one entire ACPI table */
145 Status = AcGetOneTableFromFile (
146 Filename, File, GetOnlyAmlTables, &Table);
148 if (Status == AE_CTRL_TERMINATE)
153 else if (Status == AE_TYPE)
157 else if (ACPI_FAILURE (Status))
162 /* Print table header for iASL/disassembler only */
164 #ifdef ACPI_ASL_COMPILER
166 AcpiTbPrintTableHeader (0, Table);
169 /* Allocate and link a table descriptor */
171 TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
172 TableDesc->Table = Table;
173 TableDesc->Next = NULL;
175 /* Link at the end of the local table list */
179 ListHead = TableDesc;
180 ListTail = TableDesc;
184 ListTail->Next = TableDesc;
185 ListTail = TableDesc;
189 /* Add the local table list to the end of the global list */
193 ListTail = *ReturnListHead;
194 while (ListTail->Next)
196 ListTail = ListTail->Next;
199 ListTail->Next = ListHead;
203 *ReturnListHead = ListHead;
211 /*******************************************************************************
213 * FUNCTION: AcGetOneTableFromFile
215 * PARAMETERS: Filename - File where table is located
216 * File - Open FILE pointer to Filename
217 * GetOnlyAmlTables - TRUE if the tables must be AML tables.
218 * ReturnTable - Where a pointer to the table is returned
222 * DESCRIPTION: Read the next ACPI table from a file. Implements support
223 * for multiple tables within a single file. File must already
226 * Note: Loading an RSDP is not supported.
228 ******************************************************************************/
231 AcGetOneTableFromFile (
234 UINT8 GetOnlyAmlTables,
235 ACPI_TABLE_HEADER **ReturnTable)
237 ACPI_STATUS Status = AE_OK;
238 ACPI_TABLE_HEADER TableHeader;
239 ACPI_TABLE_HEADER *Table;
246 /* Get the table header to examine signature and length */
248 TableOffset = ftell (File);
249 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
250 if (Count != sizeof (ACPI_TABLE_HEADER))
252 return (AE_CTRL_TERMINATE);
255 /* Validate the table signature/header (limited ASCII chars) */
257 Status = AcValidateTableHeader (File, TableOffset);
258 if (ACPI_FAILURE (Status))
263 if (GetOnlyAmlTables)
265 /* Table must be an AML table (DSDT/SSDT) or FADT */
267 if (!ACPI_COMPARE_NAME (TableHeader.Signature, ACPI_SIG_FADT) &&
268 !AcpiUtIsAmlTable (&TableHeader))
271 " %s: Table [%4.4s] is not an AML table - ignoring\n",
272 Filename, TableHeader.Signature);
278 /* Allocate a buffer for the entire table */
280 Table = AcpiOsAllocate ((size_t) TableHeader.Length);
283 return (AE_NO_MEMORY);
286 /* Read the entire ACPI table, including header */
288 fseek (File, TableOffset, SEEK_SET);
290 Count = fread (Table, 1, TableHeader.Length, File);
291 if (Count != (INT32) TableHeader.Length)
297 /* Validate the checksum (just issue a warning) */
299 Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
300 if (ACPI_FAILURE (Status))
302 Status = AcCheckTextModeCorruption (Table);
303 if (ACPI_FAILURE (Status))
309 *ReturnTable = Table;
319 /*******************************************************************************
321 * FUNCTION: AcIsFileBinary
323 * PARAMETERS: File - Open input file
325 * RETURN: TRUE if file appears to be binary
327 * DESCRIPTION: Scan a file for any non-ASCII bytes.
329 * Note: Maintains current file position.
331 ******************************************************************************/
338 BOOLEAN IsBinary = FALSE;
342 /* Scan entire file for any non-ASCII bytes */
344 FileOffset = ftell (File);
345 while (fread (&Byte, 1, 1, File) == 1)
347 if (!isprint (Byte) && !isspace (Byte))
355 fseek (File, FileOffset, SEEK_SET);
360 /*******************************************************************************
362 * FUNCTION: AcValidateTableHeader
364 * PARAMETERS: File - Open input file
368 * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
370 * following checks on what would be the table header:
371 * 1) File must be at least as long as an ACPI_TABLE_HEADER
372 * 2) There must be enough room in the file to hold entire table
373 * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
375 * Note: There can be multiple definition blocks per file, so we cannot
376 * expect/compare the file size to be equal to the table length. 12/2015.
378 * Note: Maintains current file position.
380 ******************************************************************************/
383 AcValidateTableHeader (
387 ACPI_TABLE_HEADER TableHeader;
394 ACPI_FUNCTION_TRACE ("AcValidateTableHeader");
397 /* Read a potential table header */
399 OriginalOffset = ftell (File);
400 fseek (File, TableOffset, SEEK_SET);
402 Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
403 fseek (File, OriginalOffset, SEEK_SET);
405 if (Actual < sizeof (ACPI_TABLE_HEADER))
410 /* Validate the signature (limited ASCII chars) */
412 if (!AcpiIsValidSignature (TableHeader.Signature))
414 fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
415 *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
416 return (AE_BAD_SIGNATURE);
419 /* Validate table length against bytes remaining in the file */
421 FileSize = CmGetFileSize (File);
422 if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
424 fprintf (stderr, "Table [%4.4s] is too long for file - "
425 "needs: 0x%.2X, remaining in file: 0x%.2X\n",
426 TableHeader.Signature, TableHeader.Length,
427 (UINT32) (FileSize - TableOffset));
428 return (AE_BAD_HEADER);
432 * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
433 * We allow a NULL terminator in OemId and OemTableId.
435 for (i = 0; i < ACPI_NAME_SIZE; i++)
437 if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
443 for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
445 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
451 for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
453 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
464 ACPI_WARNING ((AE_INFO,
465 "Table header for [%4.4s] has invalid ASCII character(s)",
466 TableHeader.Signature));
471 /*******************************************************************************
473 * FUNCTION: AcCheckTextModeCorruption
475 * PARAMETERS: Table - Table buffer starting with table header
479 * DESCRIPTION: Check table for text mode file corruption where all linefeed
480 * characters (LF) have been replaced by carriage return linefeed
483 ******************************************************************************/
486 AcCheckTextModeCorruption (
487 ACPI_TABLE_HEADER *Table)
491 UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
494 /* Scan entire table to determine if each LF has been prefixed with a CR */
496 for (i = 1; i < Table->Length; i++)
498 if (Buffer[i] == 0x0A)
500 if (Buffer[i - 1] != 0x0D)
502 /* The LF does not have a preceding CR, table not corrupted */
508 /* Found a CR/LF pair */
523 * Entire table scanned, each CR is part of a CR/LF pair --
524 * meaning that the table was treated as a text file somewhere.
526 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
527 * original table are left untouched by the text conversion process --
528 * meaning that we cannot simply replace CR/LF pairs with LFs.
530 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
531 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
532 AcpiOsPrintf ("Table cannot be repaired!\n");
534 return (AE_BAD_VALUE);