acfileio.c revision 1.1 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: acfileio - Get ACPI tables from file
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2016, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpi.h"
45 1.1 christos #include "accommon.h"
46 1.1 christos #include "acapps.h"
47 1.1 christos #include "actables.h"
48 1.1 christos #include "acutils.h"
49 1.1 christos #include <errno.h>
50 1.1 christos
51 1.1 christos #define _COMPONENT ACPI_UTILITIES
52 1.1 christos ACPI_MODULE_NAME ("acfileio")
53 1.1 christos
54 1.1 christos
55 1.1 christos /* Local prototypes */
56 1.1 christos
57 1.1 christos static ACPI_STATUS
58 1.1 christos AcGetOneTableFromFile (
59 1.1 christos char *Filename,
60 1.1 christos FILE *File,
61 1.1 christos UINT8 GetOnlyAmlTables,
62 1.1 christos ACPI_TABLE_HEADER **Table);
63 1.1 christos
64 1.1 christos static ACPI_STATUS
65 1.1 christos AcCheckTextModeCorruption (
66 1.1 christos ACPI_TABLE_HEADER *Table);
67 1.1 christos
68 1.1 christos
69 1.1 christos /*******************************************************************************
70 1.1 christos *
71 1.1 christos * FUNCTION: AcGetAllTablesFromFile
72 1.1 christos *
73 1.1 christos * PARAMETERS: Filename - Table filename
74 1.1 christos * GetOnlyAmlTables - TRUE if the tables must be AML tables
75 1.1 christos * ReturnListHead - Where table list is returned
76 1.1 christos *
77 1.1 christos * RETURN: Status
78 1.1 christos *
79 1.1 christos * DESCRIPTION: Get all ACPI tables from within a single file.
80 1.1 christos *
81 1.1 christos ******************************************************************************/
82 1.1 christos
83 1.1 christos ACPI_STATUS
84 1.1 christos AcGetAllTablesFromFile (
85 1.1 christos char *Filename,
86 1.1 christos UINT8 GetOnlyAmlTables,
87 1.1 christos ACPI_NEW_TABLE_DESC **ReturnListHead)
88 1.1 christos {
89 1.1 christos ACPI_NEW_TABLE_DESC *ListHead = NULL;
90 1.1 christos ACPI_NEW_TABLE_DESC *ListTail = NULL;
91 1.1 christos ACPI_NEW_TABLE_DESC *TableDesc;
92 1.1 christos FILE *File;
93 1.1 christos ACPI_TABLE_HEADER *Table = NULL;
94 1.1 christos UINT32 FileSize;
95 1.1 christos ACPI_STATUS Status = AE_OK;
96 1.1 christos
97 1.1 christos
98 1.1 christos File = fopen (Filename, "rb");
99 1.1 christos if (!File)
100 1.1 christos {
101 1.1 christos perror ("Could not open input file");
102 1.1 christos if (errno == ENOENT)
103 1.1 christos {
104 1.1 christos return (AE_NOT_EXIST);
105 1.1 christos }
106 1.1 christos
107 1.1 christos return (AE_ERROR);
108 1.1 christos }
109 1.1 christos
110 1.1 christos /* Get the file size */
111 1.1 christos
112 1.1 christos FileSize = CmGetFileSize (File);
113 1.1 christos if (FileSize == ACPI_UINT32_MAX)
114 1.1 christos {
115 1.1 christos return (AE_ERROR);
116 1.1 christos }
117 1.1 christos
118 1.1 christos fprintf (stderr,
119 1.1 christos "Input file %s, Length 0x%X (%u) bytes\n",
120 1.1 christos Filename, FileSize, FileSize);
121 1.1 christos
122 1.1 christos /* We must have at least one ACPI table header */
123 1.1 christos
124 1.1 christos if (FileSize < sizeof (ACPI_TABLE_HEADER))
125 1.1 christos {
126 1.1 christos return (AE_BAD_HEADER);
127 1.1 christos }
128 1.1 christos
129 1.1 christos /* Check for an non-binary file */
130 1.1 christos
131 1.1 christos if (!AcIsFileBinary (File))
132 1.1 christos {
133 1.1 christos fprintf (stderr,
134 1.1 christos " %s: File does not appear to contain a valid AML table\n",
135 1.1 christos Filename);
136 1.1 christos return (AE_TYPE);
137 1.1 christos }
138 1.1 christos
139 1.1 christos /* Read all tables within the file */
140 1.1 christos
141 1.1 christos while (ACPI_SUCCESS (Status))
142 1.1 christos {
143 1.1 christos /* Get one entire ACPI table */
144 1.1 christos
145 1.1 christos Status = AcGetOneTableFromFile (
146 1.1 christos Filename, File, GetOnlyAmlTables, &Table);
147 1.1 christos
148 1.1 christos if (Status == AE_CTRL_TERMINATE)
149 1.1 christos {
150 1.1 christos Status = AE_OK;
151 1.1 christos break;
152 1.1 christos }
153 1.1 christos else if (Status == AE_TYPE)
154 1.1 christos {
155 1.1 christos return (AE_OK);
156 1.1 christos }
157 1.1 christos else if (ACPI_FAILURE (Status))
158 1.1 christos {
159 1.1 christos return (Status);
160 1.1 christos }
161 1.1 christos
162 1.1 christos /* Print table header for iASL/disassembler only */
163 1.1 christos
164 1.1 christos #ifdef ACPI_ASL_COMPILER
165 1.1 christos
166 1.1 christos AcpiTbPrintTableHeader (0, Table);
167 1.1 christos #endif
168 1.1 christos
169 1.1 christos /* Allocate and link a table descriptor */
170 1.1 christos
171 1.1 christos TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
172 1.1 christos TableDesc->Table = Table;
173 1.1 christos TableDesc->Next = NULL;
174 1.1 christos
175 1.1 christos /* Link at the end of the local table list */
176 1.1 christos
177 1.1 christos if (!ListHead)
178 1.1 christos {
179 1.1 christos ListHead = TableDesc;
180 1.1 christos ListTail = TableDesc;
181 1.1 christos }
182 1.1 christos else
183 1.1 christos {
184 1.1 christos ListTail->Next = TableDesc;
185 1.1 christos ListTail = TableDesc;
186 1.1 christos }
187 1.1 christos }
188 1.1 christos
189 1.1 christos /* Add the local table list to the end of the global list */
190 1.1 christos
191 1.1 christos if (*ReturnListHead)
192 1.1 christos {
193 1.1 christos ListTail = *ReturnListHead;
194 1.1 christos while (ListTail->Next)
195 1.1 christos {
196 1.1 christos ListTail = ListTail->Next;
197 1.1 christos }
198 1.1 christos
199 1.1 christos ListTail->Next = ListHead;
200 1.1 christos }
201 1.1 christos else
202 1.1 christos {
203 1.1 christos *ReturnListHead = ListHead;
204 1.1 christos }
205 1.1 christos
206 1.1 christos fclose(File);
207 1.1 christos return (Status);
208 1.1 christos }
209 1.1 christos
210 1.1 christos
211 1.1 christos /*******************************************************************************
212 1.1 christos *
213 1.1 christos * FUNCTION: AcGetOneTableFromFile
214 1.1 christos *
215 1.1 christos * PARAMETERS: Filename - File where table is located
216 1.1 christos * File - Open FILE pointer to Filename
217 1.1 christos * GetOnlyAmlTables - TRUE if the tables must be AML tables.
218 1.1 christos * ReturnTable - Where a pointer to the table is returned
219 1.1 christos *
220 1.1 christos * RETURN: Status
221 1.1 christos *
222 1.1 christos * DESCRIPTION: Read the next ACPI table from a file. Implements support
223 1.1 christos * for multiple tables within a single file. File must already
224 1.1 christos * be open.
225 1.1 christos *
226 1.1 christos * Note: Loading an RSDP is not supported.
227 1.1 christos *
228 1.1 christos ******************************************************************************/
229 1.1 christos
230 1.1 christos static ACPI_STATUS
231 1.1 christos AcGetOneTableFromFile (
232 1.1 christos char *Filename,
233 1.1 christos FILE *File,
234 1.1 christos UINT8 GetOnlyAmlTables,
235 1.1 christos ACPI_TABLE_HEADER **ReturnTable)
236 1.1 christos {
237 1.1 christos ACPI_STATUS Status = AE_OK;
238 1.1 christos ACPI_TABLE_HEADER TableHeader;
239 1.1 christos ACPI_TABLE_HEADER *Table;
240 1.1 christos INT32 Count;
241 1.1 christos long TableOffset;
242 1.1 christos
243 1.1 christos
244 1.1 christos *ReturnTable = NULL;
245 1.1 christos
246 1.1 christos /* Get the table header to examine signature and length */
247 1.1 christos
248 1.1 christos TableOffset = ftell (File);
249 1.1 christos Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
250 1.1 christos if (Count != sizeof (ACPI_TABLE_HEADER))
251 1.1 christos {
252 1.1 christos return (AE_CTRL_TERMINATE);
253 1.1 christos }
254 1.1 christos
255 1.1 christos /* Validate the table signature/header (limited ASCII chars) */
256 1.1 christos
257 1.1 christos Status = AcValidateTableHeader (File, TableOffset);
258 1.1 christos if (ACPI_FAILURE (Status))
259 1.1 christos {
260 1.1 christos return (Status);
261 1.1 christos }
262 1.1 christos
263 1.1 christos if (GetOnlyAmlTables)
264 1.1 christos {
265 1.1 christos /* Table must be an AML table (DSDT/SSDT) or FADT */
266 1.1 christos
267 1.1 christos if (!ACPI_COMPARE_NAME (TableHeader.Signature, ACPI_SIG_FADT) &&
268 1.1 christos !AcpiUtIsAmlTable (&TableHeader))
269 1.1 christos {
270 1.1 christos fprintf (stderr,
271 1.1 christos " %s: Table [%4.4s] is not an AML table - ignoring\n",
272 1.1 christos Filename, TableHeader.Signature);
273 1.1 christos
274 1.1 christos return (AE_TYPE);
275 1.1 christos }
276 1.1 christos }
277 1.1 christos
278 1.1 christos /* Allocate a buffer for the entire table */
279 1.1 christos
280 1.1 christos Table = AcpiOsAllocate ((size_t) TableHeader.Length);
281 1.1 christos if (!Table)
282 1.1 christos {
283 1.1 christos return (AE_NO_MEMORY);
284 1.1 christos }
285 1.1 christos
286 1.1 christos /* Read the entire ACPI table, including header */
287 1.1 christos
288 1.1 christos fseek (File, TableOffset, SEEK_SET);
289 1.1 christos
290 1.1 christos Count = fread (Table, 1, TableHeader.Length, File);
291 1.1 christos if (Count != (INT32) TableHeader.Length)
292 1.1 christos {
293 1.1 christos Status = AE_ERROR;
294 1.1 christos goto ErrorExit;
295 1.1 christos }
296 1.1 christos
297 1.1 christos /* Validate the checksum (just issue a warning) */
298 1.1 christos
299 1.1 christos Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
300 1.1 christos if (ACPI_FAILURE (Status))
301 1.1 christos {
302 1.1 christos Status = AcCheckTextModeCorruption (Table);
303 1.1 christos if (ACPI_FAILURE (Status))
304 1.1 christos {
305 1.1 christos goto ErrorExit;
306 1.1 christos }
307 1.1 christos }
308 1.1 christos
309 1.1 christos *ReturnTable = Table;
310 1.1 christos return (AE_OK);
311 1.1 christos
312 1.1 christos
313 1.1 christos ErrorExit:
314 1.1 christos AcpiOsFree (Table);
315 1.1 christos return (Status);
316 1.1 christos }
317 1.1 christos
318 1.1 christos
319 1.1 christos /*******************************************************************************
320 1.1 christos *
321 1.1 christos * FUNCTION: AcIsFileBinary
322 1.1 christos *
323 1.1 christos * PARAMETERS: File - Open input file
324 1.1 christos *
325 1.1 christos * RETURN: TRUE if file appears to be binary
326 1.1 christos *
327 1.1 christos * DESCRIPTION: Scan a file for any non-ASCII bytes.
328 1.1 christos *
329 1.1 christos * Note: Maintains current file position.
330 1.1 christos *
331 1.1 christos ******************************************************************************/
332 1.1 christos
333 1.1 christos BOOLEAN
334 1.1 christos AcIsFileBinary (
335 1.1 christos FILE *File)
336 1.1 christos {
337 1.1 christos UINT8 Byte;
338 1.1 christos BOOLEAN IsBinary = FALSE;
339 1.1 christos long FileOffset;
340 1.1 christos
341 1.1 christos
342 1.1 christos /* Scan entire file for any non-ASCII bytes */
343 1.1 christos
344 1.1 christos FileOffset = ftell (File);
345 1.1 christos while (fread (&Byte, 1, 1, File) == 1)
346 1.1 christos {
347 1.1 christos if (!isprint (Byte) && !isspace (Byte))
348 1.1 christos {
349 1.1 christos IsBinary = TRUE;
350 1.1 christos goto Exit;
351 1.1 christos }
352 1.1 christos }
353 1.1 christos
354 1.1 christos Exit:
355 1.1 christos fseek (File, FileOffset, SEEK_SET);
356 1.1 christos return (IsBinary);
357 1.1 christos }
358 1.1 christos
359 1.1 christos
360 1.1 christos /*******************************************************************************
361 1.1 christos *
362 1.1 christos * FUNCTION: AcValidateTableHeader
363 1.1 christos *
364 1.1 christos * PARAMETERS: File - Open input file
365 1.1 christos *
366 1.1 christos * RETURN: Status
367 1.1 christos *
368 1.1 christos * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
369 1.1 christos * tables, via the
370 1.1 christos * following checks on what would be the table header:
371 1.1 christos * 1) File must be at least as long as an ACPI_TABLE_HEADER
372 1.1 christos * 2) There must be enough room in the file to hold entire table
373 1.1 christos * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
374 1.1 christos *
375 1.1 christos * Note: There can be multiple definition blocks per file, so we cannot
376 1.1 christos * expect/compare the file size to be equal to the table length. 12/2015.
377 1.1 christos *
378 1.1 christos * Note: Maintains current file position.
379 1.1 christos *
380 1.1 christos ******************************************************************************/
381 1.1 christos
382 1.1 christos ACPI_STATUS
383 1.1 christos AcValidateTableHeader (
384 1.1 christos FILE *File,
385 1.1 christos long TableOffset)
386 1.1 christos {
387 1.1 christos ACPI_TABLE_HEADER TableHeader;
388 1.1 christos size_t Actual;
389 1.1 christos long OriginalOffset;
390 1.1 christos UINT32 FileSize;
391 1.1 christos UINT32 i;
392 1.1 christos
393 1.1 christos
394 1.1 christos ACPI_FUNCTION_TRACE ("AcValidateTableHeader");
395 1.1 christos
396 1.1 christos
397 1.1 christos /* Read a potential table header */
398 1.1 christos
399 1.1 christos OriginalOffset = ftell (File);
400 1.1 christos fseek (File, TableOffset, SEEK_SET);
401 1.1 christos
402 1.1 christos Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
403 1.1 christos fseek (File, OriginalOffset, SEEK_SET);
404 1.1 christos
405 1.1 christos if (Actual < sizeof (ACPI_TABLE_HEADER))
406 1.1 christos {
407 1.1 christos return (AE_ERROR);
408 1.1 christos }
409 1.1 christos
410 1.1 christos /* Validate the signature (limited ASCII chars) */
411 1.1 christos
412 1.1 christos if (!AcpiIsValidSignature (TableHeader.Signature))
413 1.1 christos {
414 1.1 christos fprintf (stderr, "Invalid table signature: 0x%8.8X\n",
415 1.1 christos *ACPI_CAST_PTR (UINT32, TableHeader.Signature));
416 1.1 christos return (AE_BAD_SIGNATURE);
417 1.1 christos }
418 1.1 christos
419 1.1 christos /* Validate table length against bytes remaining in the file */
420 1.1 christos
421 1.1 christos FileSize = CmGetFileSize (File);
422 1.1 christos if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
423 1.1 christos {
424 1.1 christos fprintf (stderr, "Table [%4.4s] is too long for file - "
425 1.1 christos "needs: 0x%.2X, remaining in file: 0x%.2X\n",
426 1.1 christos TableHeader.Signature, TableHeader.Length,
427 1.1 christos (UINT32) (FileSize - TableOffset));
428 1.1 christos return (AE_BAD_HEADER);
429 1.1 christos }
430 1.1 christos
431 1.1 christos /*
432 1.1 christos * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
433 1.1 christos * We allow a NULL terminator in OemId and OemTableId.
434 1.1 christos */
435 1.1 christos for (i = 0; i < ACPI_NAME_SIZE; i++)
436 1.1 christos {
437 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
438 1.1 christos {
439 1.1 christos goto BadCharacters;
440 1.1 christos }
441 1.1 christos }
442 1.1 christos
443 1.1 christos for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
444 1.1 christos {
445 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
446 1.1 christos {
447 1.1 christos goto BadCharacters;
448 1.1 christos }
449 1.1 christos }
450 1.1 christos
451 1.1 christos for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
452 1.1 christos {
453 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
454 1.1 christos {
455 1.1 christos goto BadCharacters;
456 1.1 christos }
457 1.1 christos }
458 1.1 christos
459 1.1 christos return (AE_OK);
460 1.1 christos
461 1.1 christos
462 1.1 christos BadCharacters:
463 1.1 christos
464 1.1 christos ACPI_WARNING ((AE_INFO,
465 1.1 christos "Table header for [%4.4s] has invalid ASCII character(s)",
466 1.1 christos TableHeader.Signature));
467 1.1 christos return (AE_OK);
468 1.1 christos }
469 1.1 christos
470 1.1 christos
471 1.1 christos /*******************************************************************************
472 1.1 christos *
473 1.1 christos * FUNCTION: AcCheckTextModeCorruption
474 1.1 christos *
475 1.1 christos * PARAMETERS: Table - Table buffer starting with table header
476 1.1 christos *
477 1.1 christos * RETURN: Status
478 1.1 christos *
479 1.1 christos * DESCRIPTION: Check table for text mode file corruption where all linefeed
480 1.1 christos * characters (LF) have been replaced by carriage return linefeed
481 1.1 christos * pairs (CR/LF).
482 1.1 christos *
483 1.1 christos ******************************************************************************/
484 1.1 christos
485 1.1 christos static ACPI_STATUS
486 1.1 christos AcCheckTextModeCorruption (
487 1.1 christos ACPI_TABLE_HEADER *Table)
488 1.1 christos {
489 1.1 christos UINT32 i;
490 1.1 christos UINT32 Pairs = 0;
491 1.1 christos UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
492 1.1 christos
493 1.1 christos
494 1.1 christos /* Scan entire table to determine if each LF has been prefixed with a CR */
495 1.1 christos
496 1.1 christos for (i = 1; i < Table->Length; i++)
497 1.1 christos {
498 1.1 christos if (Buffer[i] == 0x0A)
499 1.1 christos {
500 1.1 christos if (Buffer[i - 1] != 0x0D)
501 1.1 christos {
502 1.1 christos /* The LF does not have a preceding CR, table not corrupted */
503 1.1 christos
504 1.1 christos return (AE_OK);
505 1.1 christos }
506 1.1 christos else
507 1.1 christos {
508 1.1 christos /* Found a CR/LF pair */
509 1.1 christos
510 1.1 christos Pairs++;
511 1.1 christos }
512 1.1 christos
513 1.1 christos i++;
514 1.1 christos }
515 1.1 christos }
516 1.1 christos
517 1.1 christos if (!Pairs)
518 1.1 christos {
519 1.1 christos return (AE_OK);
520 1.1 christos }
521 1.1 christos
522 1.1 christos /*
523 1.1 christos * Entire table scanned, each CR is part of a CR/LF pair --
524 1.1 christos * meaning that the table was treated as a text file somewhere.
525 1.1 christos *
526 1.1 christos * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
527 1.1 christos * original table are left untouched by the text conversion process --
528 1.1 christos * meaning that we cannot simply replace CR/LF pairs with LFs.
529 1.1 christos */
530 1.1 christos AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
531 1.1 christos AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
532 1.1 christos AcpiOsPrintf ("Table cannot be repaired!\n");
533 1.1 christos
534 1.1 christos return (AE_BAD_VALUE);
535 1.1 christos }
536