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