1 /*******************************************************************************
3 * Module Name: utfileio - simple file I/O routines
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
50 #ifdef ACPI_ASL_COMPILER
51 #include "aslcompiler.h"
55 #define _COMPONENT ACPI_CA_DEBUGGER
56 ACPI_MODULE_NAME ("utfileio")
59 #ifdef ACPI_APPLICATION
61 /* Local prototypes */
64 AcpiUtCheckTextModeCorruption (
72 ACPI_TABLE_HEADER **Table,
76 /*******************************************************************************
78 * FUNCTION: AcpiUtCheckTextModeCorruption
80 * PARAMETERS: Table - Table buffer
81 * TableLength - Length of table from the table header
82 * FileLength - Length of the file that contains the table
86 * DESCRIPTION: Check table for text mode file corruption where all linefeed
87 * characters (LF) have been replaced by carriage return linefeed
90 ******************************************************************************/
93 AcpiUtCheckTextModeCorruption (
102 if (TableLength != FileLength)
104 ACPI_WARNING ((AE_INFO,
105 "File length (0x%X) is not the same as the table length (0x%X)",
106 FileLength, TableLength));
109 /* Scan entire table to determine if each LF has been prefixed with a CR */
111 for (i = 1; i < FileLength; i++)
113 if (Table[i] == 0x0A)
115 if (Table[i - 1] != 0x0D)
117 /* The LF does not have a preceding CR, table not corrupted */
123 /* Found a CR/LF pair */
137 * Entire table scanned, each CR is part of a CR/LF pair --
138 * meaning that the table was treated as a text file somewhere.
140 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
141 * original table are left untouched by the text conversion process --
142 * meaning that we cannot simply replace CR/LF pairs with LFs.
144 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
145 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
146 AcpiOsPrintf ("Table cannot be repaired!\n");
147 return (AE_BAD_VALUE);
151 /*******************************************************************************
153 * FUNCTION: AcpiUtReadTable
155 * PARAMETERS: fp - File that contains table
156 * Table - Return value, buffer with table
157 * TableLength - Return value, length of table
161 * DESCRIPTION: Load the DSDT from the file pointer
163 ******************************************************************************/
168 ACPI_TABLE_HEADER **Table,
171 ACPI_TABLE_HEADER TableHeader;
175 BOOLEAN StandardHeader = TRUE;
178 /* Get the file size */
180 FileSize = CmGetFileSize (fp);
181 if (FileSize == ACPI_UINT32_MAX)
188 return (AE_BAD_HEADER);
191 /* Read the signature */
193 fseek (fp, 0, SEEK_SET);
195 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp);
196 if (Count != sizeof (ACPI_TABLE_HEADER))
198 AcpiOsPrintf ("Could not read the table header\n");
199 return (AE_BAD_HEADER);
202 /* The RSDP table does not have standard ACPI header */
204 if (ACPI_VALIDATE_RSDP_SIG (TableHeader.Signature))
206 *TableLength = FileSize;
207 StandardHeader = FALSE;
213 /* Validate the table header/length */
215 Status = AcpiTbValidateTableHeader (&TableHeader);
216 if (ACPI_FAILURE (Status))
218 AcpiOsPrintf ("Table header is invalid!\n");
223 /* File size must be at least as long as the Header-specified length */
225 if (TableHeader.Length > FileSize)
228 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
229 TableHeader.Length, FileSize);
231 #ifdef ACPI_ASL_COMPILER
232 Status = FlCheckForAscii (fp, NULL, FALSE);
233 if (ACPI_SUCCESS (Status))
235 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
236 TableHeader.Length, FileSize);
239 return (AE_BAD_HEADER);
242 #ifdef ACPI_OBSOLETE_CODE
243 /* We only support a limited number of table types */
245 if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
246 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
247 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
249 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
250 (char *) TableHeader.Signature);
251 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
256 *TableLength = TableHeader.Length;
259 /* Allocate a buffer for the table */
261 *Table = AcpiOsAllocate ((size_t) FileSize);
265 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
266 TableHeader.Signature, *TableLength);
267 return (AE_NO_MEMORY);
270 /* Get the rest of the table */
272 fseek (fp, 0, SEEK_SET);
273 Actual = fread (*Table, 1, (size_t) FileSize, fp);
274 if (Actual == FileSize)
278 /* Now validate the checksum */
280 Status = AcpiTbVerifyChecksum ((void *) *Table,
281 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
283 if (Status == AE_BAD_CHECKSUM)
285 Status = AcpiUtCheckTextModeCorruption ((UINT8 *) *Table,
286 FileSize, (*Table)->Length);
295 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
300 AcpiOsPrintf ("Error - could not read the table file\n");
308 /*******************************************************************************
310 * FUNCTION: AcpiUtReadTableFromFile
312 * PARAMETERS: Filename - File where table is located
313 * Table - Where a pointer to the table is returned
317 * DESCRIPTION: Get an ACPI table from a file
319 ******************************************************************************/
322 AcpiUtReadTableFromFile (
324 ACPI_TABLE_HEADER **Table)
329 ACPI_STATUS Status = AE_ERROR;
332 /* Open the file, get current size */
334 File = fopen (Filename, "rb");
337 perror ("Could not open input file");
341 FileSize = CmGetFileSize (File);
342 if (FileSize == ACPI_UINT32_MAX)
347 /* Get the entire file */
349 fprintf (stderr, "Loading Acpi table from file %10s - Length %.8u (%06X)\n",
350 Filename, FileSize, FileSize);
352 Status = AcpiUtReadTable (File, Table, &TableLength);
353 if (ACPI_FAILURE (Status))
355 AcpiOsPrintf ("Could not get table from the file\n");