1 /******************************************************************************
3 * Module Name: acfileio - Get ACPI tables from file
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 AcpiAcGetOneTableFromFile (
61 UINT8 GetOnlyAmlTables,
62 ACPI_TABLE_HEADER **Table);
65 AcpiAcCheckTextModeCorruption (
66 ACPI_TABLE_HEADER *Table);
69 /*******************************************************************************
71 * FUNCTION: AcpiAcGetAllTablesFromFile
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 AcpiAcGetAllTablesFromFile (
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)
120 return (AE_BAD_HEADER);
123 /* Read all tables within the file */
125 while (ACPI_SUCCESS (Status))
127 /* Get one entire ACPI table */
129 Status = AcpiAcGetOneTableFromFile (
130 Filename, File, GetOnlyAmlTables, &Table);
131 if (Status == AE_CTRL_TERMINATE)
136 else if (Status == AE_TYPE)
140 else if (ACPI_FAILURE (Status))
145 /* Allocate and link a table descriptor */
147 TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
148 TableDesc->Table = Table;
149 TableDesc->Next = NULL;
151 /* Link at the end of the local table list */
155 ListHead = TableDesc;
156 ListTail = TableDesc;
160 ListTail->Next = TableDesc;
161 ListTail = TableDesc;
165 /* Add the local table list to the end of the global list */
169 ListTail = *ReturnListHead;
170 while (ListTail->Next)
172 ListTail = ListTail->Next;
175 ListTail->Next = ListHead;
179 *ReturnListHead = ListHead;
187 /*******************************************************************************
189 * FUNCTION: AcpiAcGetOneTableFromFile
191 * PARAMETERS: Filename - File where table is located
192 * File - Open FILE pointer to Filename
193 * GetOnlyAmlTables - TRUE if the tables must be AML tables.
194 * ReturnTable - Where a pointer to the table is returned
198 * DESCRIPTION: Read the next ACPI table from a file. Implements support
199 * for multiple tables within a single file. File must already
202 * Note: Loading an RSDP is not supported.
204 ******************************************************************************/
207 AcpiAcGetOneTableFromFile (
210 UINT8 GetOnlyAmlTables,
211 ACPI_TABLE_HEADER **ReturnTable)
213 ACPI_STATUS Status = AE_OK;
214 ACPI_TABLE_HEADER TableHeader;
215 ACPI_TABLE_HEADER *Table;
223 /* Get just the table header to get signature and length */
225 Position = ftell (File);
226 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
227 if (Count != sizeof (ACPI_TABLE_HEADER))
229 return (AE_CTRL_TERMINATE);
232 if (GetOnlyAmlTables)
234 /* Table must be an AML table (DSDT/SSDT) or FADT */
236 if (!ACPI_COMPARE_NAME (TableHeader.Signature, ACPI_SIG_FADT) &&
237 !AcpiUtIsAmlTable (&TableHeader))
240 " %s: [%4.4s] is not an AML table - ignoring\n",
241 Filename, TableHeader.Signature);
247 /* Allocate a buffer for the entire table */
249 Table = AcpiOsAllocate ((size_t) TableHeader.Length);
252 return (AE_NO_MEMORY);
255 /* Now read the entire table */
257 fseek (File, Position, SEEK_SET);
259 Count = fread (Table, 1, TableHeader.Length, File);
260 if (Count != (INT32) TableHeader.Length)
266 /* Validate the checksum (just issue a warning) */
268 Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
269 if (ACPI_FAILURE (Status))
271 Status = AcpiAcCheckTextModeCorruption (Table);
272 if (ACPI_FAILURE (Status))
279 "Loading ACPI table [%4.4s] from file %12s - Length 0x%06X (%u)\n",
280 TableHeader.Signature, Filename,
281 TableHeader.Length, TableHeader.Length);
283 *ReturnTable = Table;
293 /*******************************************************************************
295 * FUNCTION: AcpiAcCheckTextModeCorruption
297 * PARAMETERS: Table - Table buffer starting with table header
301 * DESCRIPTION: Check table for text mode file corruption where all linefeed
302 * characters (LF) have been replaced by carriage return linefeed
305 ******************************************************************************/
308 AcpiAcCheckTextModeCorruption (
309 ACPI_TABLE_HEADER *Table)
313 UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
316 /* Scan entire table to determine if each LF has been prefixed with a CR */
318 for (i = 1; i < Table->Length; i++)
320 if (Buffer[i] == 0x0A)
322 if (Buffer[i - 1] != 0x0D)
324 /* The LF does not have a preceding CR, table not corrupted */
330 /* Found a CR/LF pair */
345 * Entire table scanned, each CR is part of a CR/LF pair --
346 * meaning that the table was treated as a text file somewhere.
348 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
349 * original table are left untouched by the text conversion process --
350 * meaning that we cannot simply replace CR/LF pairs with LFs.
352 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
353 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
354 AcpiOsPrintf ("Table cannot be repaired!\n");
356 return (AE_BAD_VALUE);