1 /*******************************************************************************
3 * Module Name: utfileio - simple file I/O routines
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.
49 #ifdef ACPI_ASL_COMPILER
50 #include "aslcompiler.h"
54 #define _COMPONENT ACPI_CA_DEBUGGER
55 ACPI_MODULE_NAME ("utfileio")
58 #ifdef ACPI_APPLICATION
60 /* Local prototypes */
63 AcpiUtCheckTextModeCorruption (
71 ACPI_TABLE_HEADER **Table,
75 /*******************************************************************************
77 * FUNCTION: AcpiUtCheckTextModeCorruption
79 * PARAMETERS: Table - Table buffer
80 * TableLength - Length of table from the table header
81 * FileLength - Length of the file that contains the table
85 * DESCRIPTION: Check table for text mode file corruption where all linefeed
86 * characters (LF) have been replaced by carriage return linefeed
89 ******************************************************************************/
92 AcpiUtCheckTextModeCorruption (
101 if (TableLength != FileLength)
103 ACPI_WARNING ((AE_INFO,
104 "File length (0x%X) is not the same as the table length (0x%X)",
105 FileLength, TableLength));
108 /* Scan entire table to determine if each LF has been prefixed with a CR */
110 for (i = 1; i < FileLength; i++)
112 if (Table[i] == 0x0A)
114 if (Table[i - 1] != 0x0D)
116 /* The LF does not have a preceding CR, table not corrupted */
122 /* Found a CR/LF pair */
136 * Entire table scanned, each CR is part of a CR/LF pair --
137 * meaning that the table was treated as a text file somewhere.
139 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
140 * original table are left untouched by the text conversion process --
141 * meaning that we cannot simply replace CR/LF pairs with LFs.
143 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
144 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
145 AcpiOsPrintf ("Table cannot be repaired!\n");
146 return (AE_BAD_VALUE);
150 /*******************************************************************************
152 * FUNCTION: AcpiUtReadTable
154 * PARAMETERS: fp - File that contains table
155 * Table - Return value, buffer with table
156 * TableLength - Return value, length of table
160 * DESCRIPTION: Load the DSDT from the file pointer
162 ******************************************************************************/
167 ACPI_TABLE_HEADER **Table,
170 ACPI_TABLE_HEADER TableHeader;
174 BOOLEAN StandardHeader = TRUE;
177 /* Get the file size */
179 FileSize = CmGetFileSize (fp);
180 if (FileSize == ACPI_UINT32_MAX)
187 return (AE_BAD_HEADER);
190 /* Read the signature */
192 fseek (fp, 0, SEEK_SET);
194 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp);
195 if (Count != sizeof (ACPI_TABLE_HEADER))
197 AcpiOsPrintf ("Could not read the table header\n");
198 return (AE_BAD_HEADER);
201 /* The RSDP table does not have standard ACPI header */
203 if (ACPI_VALIDATE_RSDP_SIG (TableHeader.Signature))
205 *TableLength = FileSize;
206 StandardHeader = FALSE;
212 /* Validate the table header/length */
214 Status = AcpiTbValidateTableHeader (&TableHeader);
215 if (ACPI_FAILURE (Status))
217 AcpiOsPrintf ("Table header is invalid!\n");
222 /* File size must be at least as long as the Header-specified length */
224 if (TableHeader.Length > FileSize)
227 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
228 TableHeader.Length, FileSize);
230 #ifdef ACPI_ASL_COMPILER
231 AcpiOsPrintf ("File is corrupt or is ASCII text -- "
232 "it must be a binary file\n");
234 return (AE_BAD_HEADER);
237 #ifdef ACPI_OBSOLETE_CODE
238 /* We only support a limited number of table types */
240 if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
241 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
242 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
244 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
245 (char *) TableHeader.Signature);
246 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
251 *TableLength = TableHeader.Length;
254 /* Allocate a buffer for the table */
256 *Table = AcpiOsAllocate ((size_t) FileSize);
260 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
261 TableHeader.Signature, *TableLength);
262 return (AE_NO_MEMORY);
265 /* Get the rest of the table */
267 fseek (fp, 0, SEEK_SET);
268 Actual = fread (*Table, 1, (size_t) FileSize, fp);
269 if (Actual == FileSize)
273 /* Now validate the checksum */
275 Status = AcpiTbVerifyChecksum ((void *) *Table,
276 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
278 if (Status == AE_BAD_CHECKSUM)
280 Status = AcpiUtCheckTextModeCorruption ((UINT8 *) *Table,
281 FileSize, (*Table)->Length);
290 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
295 AcpiOsPrintf ("Error - could not read the table file\n");
303 /*******************************************************************************
305 * FUNCTION: AcpiUtReadTableFromFile
307 * PARAMETERS: Filename - File where table is located
308 * Table - Where a pointer to the table is returned
312 * DESCRIPTION: Get an ACPI table from a file
314 ******************************************************************************/
317 AcpiUtReadTableFromFile (
319 ACPI_TABLE_HEADER **Table)
324 ACPI_STATUS Status = AE_ERROR;
327 /* Open the file, get current size */
329 File = fopen (Filename, "rb");
332 perror ("Could not open input file");
336 FileSize = CmGetFileSize (File);
337 if (FileSize == ACPI_UINT32_MAX)
342 /* Get the entire file */
344 fprintf (stderr, "Reading ACPI table from file %10s - Length %.8u (0x%06X)\n",
345 Filename, FileSize, FileSize);
347 Status = AcpiUtReadTable (File, Table, &TableLength);
348 if (ACPI_FAILURE (Status))
350 AcpiOsPrintf ("Could not get table from the file\n");