1 /*******************************************************************************
2  *
3  * Module Name: dbfileio - Debugger file I/O commands. These can't usually
4  *              be used when running the debugger in Ring 0 (Kernel mode)
5  *
6  ******************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2013, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44 
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
49 
50 #ifdef ACPI_APPLICATION
51 #include <contrib/dev/acpica/include/actables.h>
52 #endif
53 
54 #ifdef ACPI_ASL_COMPILER
55 #include <contrib/dev/acpica/compiler/aslcompiler.h>
56 #endif
57 
58 #if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
59 
60 #define _COMPONENT          ACPI_CA_DEBUGGER
61         ACPI_MODULE_NAME    ("dbfileio")
62 
63 /*
64  * NOTE: this is here for lack of a better place. It is used in all
65  * flavors of the debugger, need LCD file
66  */
67 #ifdef ACPI_APPLICATION
68 #include <stdio.h>
69 FILE                        *AcpiGbl_DebugFile = NULL;
70 #endif
71 
72 
73 #ifdef ACPI_DEBUGGER
74 
75 /* Local prototypes */
76 
77 #ifdef ACPI_APPLICATION
78 
79 static ACPI_STATUS
80 AcpiDbCheckTextModeCorruption (
81     UINT8                   *Table,
82     UINT32                  TableLength,
83     UINT32                  FileLength);
84 
85 #endif
86 
87 /*******************************************************************************
88  *
89  * FUNCTION:    AcpiDbCloseDebugFile
90  *
91  * PARAMETERS:  None
92  *
93  * RETURN:      None
94  *
95  * DESCRIPTION: If open, close the current debug output file
96  *
97  ******************************************************************************/
98 
99 void
100 AcpiDbCloseDebugFile (
101     void)
102 {
103 
104 #ifdef ACPI_APPLICATION
105 
106     if (AcpiGbl_DebugFile)
107     {
108        fclose (AcpiGbl_DebugFile);
109        AcpiGbl_DebugFile = NULL;
110        AcpiGbl_DbOutputToFile = FALSE;
111        AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
112     }
113 #endif
114 }
115 
116 
117 /*******************************************************************************
118  *
119  * FUNCTION:    AcpiDbOpenDebugFile
120  *
121  * PARAMETERS:  Name                - Filename to open
122  *
123  * RETURN:      None
124  *
125  * DESCRIPTION: Open a file where debug output will be directed.
126  *
127  ******************************************************************************/
128 
129 void
130 AcpiDbOpenDebugFile (
131     char                    *Name)
132 {
133 
134 #ifdef ACPI_APPLICATION
135 
136     AcpiDbCloseDebugFile ();
137     AcpiGbl_DebugFile = fopen (Name, "w+");
138     if (!AcpiGbl_DebugFile)
139     {
140         AcpiOsPrintf ("Could not open debug file %s\n", Name);
141         return;
142     }
143 
144     AcpiOsPrintf ("Debug output file %s opened\n", Name);
145     ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
146     AcpiGbl_DbOutputToFile = TRUE;
147 
148 #endif
149 }
150 #endif
151 
152 
153 #ifdef ACPI_APPLICATION
154 /*******************************************************************************
155  *
156  * FUNCTION:    AcpiDbCheckTextModeCorruption
157  *
158  * PARAMETERS:  Table           - Table buffer
159  *              TableLength     - Length of table from the table header
160  *              FileLength      - Length of the file that contains the table
161  *
162  * RETURN:      Status
163  *
164  * DESCRIPTION: Check table for text mode file corruption where all linefeed
165  *              characters (LF) have been replaced by carriage return linefeed
166  *              pairs (CR/LF).
167  *
168  ******************************************************************************/
169 
170 static ACPI_STATUS
171 AcpiDbCheckTextModeCorruption (
172     UINT8                   *Table,
173     UINT32                  TableLength,
174     UINT32                  FileLength)
175 {
176     UINT32                  i;
177     UINT32                  Pairs = 0;
178 
179 
180     if (TableLength != FileLength)
181     {
182         ACPI_WARNING ((AE_INFO,
183             "File length (0x%X) is not the same as the table length (0x%X)",
184             FileLength, TableLength));
185     }
186 
187     /* Scan entire table to determine if each LF has been prefixed with a CR */
188 
189     for (i = 1; i < FileLength; i++)
190     {
191         if (Table[i] == 0x0A)
192         {
193             if (Table[i - 1] != 0x0D)
194             {
195                 /* The LF does not have a preceding CR, table not corrupted */
196 
197                 return (AE_OK);
198             }
199             else
200             {
201                 /* Found a CR/LF pair */
202 
203                 Pairs++;
204             }
205             i++;
206         }
207     }
208 
209     if (!Pairs)
210     {
211         return (AE_OK);
212     }
213 
214     /*
215      * Entire table scanned, each CR is part of a CR/LF pair --
216      * meaning that the table was treated as a text file somewhere.
217      *
218      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
219      * original table are left untouched by the text conversion process --
220      * meaning that we cannot simply replace CR/LF pairs with LFs.
221      */
222     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
223     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
224     AcpiOsPrintf ("Table cannot be repaired!\n");
225     return (AE_BAD_VALUE);
226 }
227 
228 
229 /*******************************************************************************
230  *
231  * FUNCTION:    AcpiDbReadTable
232  *
233  * PARAMETERS:  fp              - File that contains table
234  *              Table           - Return value, buffer with table
235  *              TableLength     - Return value, length of table
236  *
237  * RETURN:      Status
238  *
239  * DESCRIPTION: Load the DSDT from the file pointer
240  *
241  ******************************************************************************/
242 
243 static ACPI_STATUS
244 AcpiDbReadTable (
245     FILE                    *fp,
246     ACPI_TABLE_HEADER       **Table,
247     UINT32                  *TableLength)
248 {
249     ACPI_TABLE_HEADER       TableHeader;
250     UINT32                  Actual;
251     ACPI_STATUS             Status;
252     UINT32                  FileSize;
253     BOOLEAN                 StandardHeader = TRUE;
254 
255 
256     /* Get the file size */
257 
258     fseek (fp, 0, SEEK_END);
259     FileSize = (UINT32) ftell (fp);
260     fseek (fp, 0, SEEK_SET);
261 
262     if (FileSize < 4)
263     {
264         return (AE_BAD_HEADER);
265     }
266 
267     /* Read the signature */
268 
269     if (fread (&TableHeader, 1, 4, fp) != 4)
270     {
271         AcpiOsPrintf ("Could not read the table signature\n");
272         return (AE_BAD_HEADER);
273     }
274 
275     fseek (fp, 0, SEEK_SET);
276 
277     /* The RSDT, FACS and S3PT tables do not have standard ACPI headers */
278 
279     if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") ||
280         ACPI_COMPARE_NAME (TableHeader.Signature, "FACS") ||
281         ACPI_COMPARE_NAME (TableHeader.Signature, "S3PT"))
282     {
283         *TableLength = FileSize;
284         StandardHeader = FALSE;
285     }
286     else
287     {
288         /* Read the table header */
289 
290         if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
291                 sizeof (ACPI_TABLE_HEADER))
292         {
293             AcpiOsPrintf ("Could not read the table header\n");
294             return (AE_BAD_HEADER);
295         }
296 
297 #if 0
298         /* Validate the table header/length */
299 
300         Status = AcpiTbValidateTableHeader (&TableHeader);
301         if (ACPI_FAILURE (Status))
302         {
303             AcpiOsPrintf ("Table header is invalid!\n");
304             return (Status);
305         }
306 #endif
307 
308         /* File size must be at least as long as the Header-specified length */
309 
310         if (TableHeader.Length > FileSize)
311         {
312             AcpiOsPrintf (
313                 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
314                 TableHeader.Length, FileSize);
315 
316 #ifdef ACPI_ASL_COMPILER
317             Status = FlCheckForAscii (fp, NULL, FALSE);
318             if (ACPI_SUCCESS (Status))
319             {
320                 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
321                     TableHeader.Length, FileSize);
322             }
323 #endif
324             return (AE_BAD_HEADER);
325         }
326 
327 #ifdef ACPI_OBSOLETE_CODE
328         /* We only support a limited number of table types */
329 
330         if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
331             !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
332             !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
333         {
334             AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
335                 (char *) TableHeader.Signature);
336             ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
337             return (AE_ERROR);
338         }
339 #endif
340 
341         *TableLength = TableHeader.Length;
342     }
343 
344     /* Allocate a buffer for the table */
345 
346     *Table = AcpiOsAllocate ((size_t) FileSize);
347     if (!*Table)
348     {
349         AcpiOsPrintf (
350             "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
351             TableHeader.Signature, *TableLength);
352         return (AE_NO_MEMORY);
353     }
354 
355     /* Get the rest of the table */
356 
357     fseek (fp, 0, SEEK_SET);
358     Actual = fread (*Table, 1, (size_t) FileSize, fp);
359     if (Actual == FileSize)
360     {
361         if (StandardHeader)
362         {
363             /* Now validate the checksum */
364 
365             Status = AcpiTbVerifyChecksum ((void *) *Table,
366                         ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
367 
368             if (Status == AE_BAD_CHECKSUM)
369             {
370                 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
371                             FileSize, (*Table)->Length);
372                 return (Status);
373             }
374         }
375         return (AE_OK);
376     }
377 
378     if (Actual > 0)
379     {
380         AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
381             FileSize, Actual);
382         return (AE_OK);
383     }
384 
385     AcpiOsPrintf ("Error - could not read the table file\n");
386     AcpiOsFree (*Table);
387     *Table = NULL;
388     *TableLength = 0;
389     return (AE_ERROR);
390 }
391 
392 
393 /*******************************************************************************
394  *
395  * FUNCTION:    AeLocalLoadTable
396  *
397  * PARAMETERS:  Table           - pointer to a buffer containing the entire
398  *                                table to be loaded
399  *
400  * RETURN:      Status
401  *
402  * DESCRIPTION: This function is called to load a table from the caller's
403  *              buffer. The buffer must contain an entire ACPI Table including
404  *              a valid header. The header fields will be verified, and if it
405  *              is determined that the table is invalid, the call will fail.
406  *
407  ******************************************************************************/
408 
409 static ACPI_STATUS
410 AeLocalLoadTable (
411     ACPI_TABLE_HEADER       *Table)
412 {
413     ACPI_STATUS             Status = AE_OK;
414 /*    ACPI_TABLE_DESC         TableInfo; */
415 
416 
417     ACPI_FUNCTION_TRACE (AeLocalLoadTable);
418 #if 0
419 
420 
421     if (!Table)
422     {
423         return_ACPI_STATUS (AE_BAD_PARAMETER);
424     }
425 
426     TableInfo.Pointer = Table;
427     Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
428     if (ACPI_FAILURE (Status))
429     {
430         return_ACPI_STATUS (Status);
431     }
432 
433     /* Install the new table into the local data structures */
434 
435     Status = AcpiTbInstallTable (&TableInfo);
436     if (ACPI_FAILURE (Status))
437     {
438         if (Status == AE_ALREADY_EXISTS)
439         {
440             /* Table already exists, no error */
441 
442             Status = AE_OK;
443         }
444 
445         /* Free table allocated by AcpiTbGetTable */
446 
447         AcpiTbDeleteSingleTable (&TableInfo);
448         return_ACPI_STATUS (Status);
449     }
450 
451 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
452 
453     Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
454     if (ACPI_FAILURE (Status))
455     {
456         /* Uninstall table and free the buffer */
457 
458         AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
459         return_ACPI_STATUS (Status);
460     }
461 #endif
462 #endif
463 
464     return_ACPI_STATUS (Status);
465 }
466 
467 
468 /*******************************************************************************
469  *
470  * FUNCTION:    AcpiDbReadTableFromFile
471  *
472  * PARAMETERS:  Filename         - File where table is located
473  *              Table            - Where a pointer to the table is returned
474  *
475  * RETURN:      Status
476  *
477  * DESCRIPTION: Get an ACPI table from a file
478  *
479  ******************************************************************************/
480 
481 ACPI_STATUS
482 AcpiDbReadTableFromFile (
483     char                    *Filename,
484     ACPI_TABLE_HEADER       **Table)
485 {
486     FILE                    *File;
487     UINT32                  TableLength;
488     ACPI_STATUS             Status;
489 
490 
491     /* Open the file */
492 
493     File = fopen (Filename, "rb");
494     if (!File)
495     {
496         AcpiOsPrintf ("Could not open input file %s\n", Filename);
497         return (AE_ERROR);
498     }
499 
500     /* Get the entire file */
501 
502     fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
503     Status = AcpiDbReadTable (File, Table, &TableLength);
504     fclose(File);
505 
506     if (ACPI_FAILURE (Status))
507     {
508         AcpiOsPrintf ("Could not get table from the file\n");
509         return (Status);
510     }
511 
512     return (AE_OK);
513  }
514 #endif
515 
516 
517 /*******************************************************************************
518  *
519  * FUNCTION:    AcpiDbGetTableFromFile
520  *
521  * PARAMETERS:  Filename        - File where table is located
522  *              ReturnTable     - Where a pointer to the table is returned
523  *
524  * RETURN:      Status
525  *
526  * DESCRIPTION: Load an ACPI table from a file
527  *
528  ******************************************************************************/
529 
530 ACPI_STATUS
531 AcpiDbGetTableFromFile (
532     char                    *Filename,
533     ACPI_TABLE_HEADER       **ReturnTable)
534 {
535 #ifdef ACPI_APPLICATION
536     ACPI_STATUS             Status;
537     ACPI_TABLE_HEADER       *Table;
538     BOOLEAN                 IsAmlTable = TRUE;
539 
540 
541     Status = AcpiDbReadTableFromFile (Filename, &Table);
542     if (ACPI_FAILURE (Status))
543     {
544         return (Status);
545     }
546 
547 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
548     IsAmlTable = AcpiUtIsAmlTable (Table);
549 #endif
550 
551     if (IsAmlTable)
552     {
553         /* Attempt to recognize and install the table */
554 
555         Status = AeLocalLoadTable (Table);
556         if (ACPI_FAILURE (Status))
557         {
558             if (Status == AE_ALREADY_EXISTS)
559             {
560                 AcpiOsPrintf ("Table %4.4s is already installed\n",
561                     Table->Signature);
562             }
563             else
564             {
565                 AcpiOsPrintf ("Could not install table, %s\n",
566                     AcpiFormatException (Status));
567             }
568 
569             return (Status);
570         }
571 
572         fprintf (stderr,
573             "Acpi table [%4.4s] successfully installed and loaded\n",
574             Table->Signature);
575     }
576 
577     AcpiGbl_AcpiHardwarePresent = FALSE;
578     if (ReturnTable)
579     {
580         *ReturnTable = Table;
581     }
582 
583 
584 #endif  /* ACPI_APPLICATION */
585     return (AE_OK);
586 }
587 
588 #endif  /* ACPI_DEBUGGER */
589