xref: /freebsd/sys/contrib/dev/acpica/components/debugger/dbfileio.c (revision b7d683e608ac894e75b2de7eac5eaa083650de54)
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 - 2012, 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 ("Debug output file %s opened\n", Name);
141         ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
142         AcpiGbl_DbOutputToFile = TRUE;
143     }
144     else
145     {
146         AcpiOsPrintf ("Could not open debug file %s\n", Name);
147     }
148 
149 #endif
150 }
151 #endif
152 
153 
154 #ifdef ACPI_APPLICATION
155 /*******************************************************************************
156  *
157  * FUNCTION:    AcpiDbCheckTextModeCorruption
158  *
159  * PARAMETERS:  Table           - Table buffer
160  *              TableLength     - Length of table from the table header
161  *              FileLength      - Length of the file that contains the table
162  *
163  * RETURN:      Status
164  *
165  * DESCRIPTION: Check table for text mode file corruption where all linefeed
166  *              characters (LF) have been replaced by carriage return linefeed
167  *              pairs (CR/LF).
168  *
169  ******************************************************************************/
170 
171 static ACPI_STATUS
172 AcpiDbCheckTextModeCorruption (
173     UINT8                   *Table,
174     UINT32                  TableLength,
175     UINT32                  FileLength)
176 {
177     UINT32                  i;
178     UINT32                  Pairs = 0;
179 
180 
181     if (TableLength != FileLength)
182     {
183         ACPI_WARNING ((AE_INFO,
184             "File length (0x%X) is not the same as the table length (0x%X)",
185             FileLength, TableLength));
186     }
187 
188     /* Scan entire table to determine if each LF has been prefixed with a CR */
189 
190     for (i = 1; i < FileLength; i++)
191     {
192         if (Table[i] == 0x0A)
193         {
194             if (Table[i - 1] != 0x0D)
195             {
196                 /* The LF does not have a preceding CR, table not corrupted */
197 
198                 return (AE_OK);
199             }
200             else
201             {
202                 /* Found a CR/LF pair */
203 
204                 Pairs++;
205             }
206             i++;
207         }
208     }
209 
210     if (!Pairs)
211     {
212         return (AE_OK);
213     }
214 
215     /*
216      * Entire table scanned, each CR is part of a CR/LF pair --
217      * meaning that the table was treated as a text file somewhere.
218      *
219      * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
220      * original table are left untouched by the text conversion process --
221      * meaning that we cannot simply replace CR/LF pairs with LFs.
222      */
223     AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
224     AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
225     AcpiOsPrintf ("Table cannot be repaired!\n");
226     return (AE_BAD_VALUE);
227 }
228 
229 
230 /*******************************************************************************
231  *
232  * FUNCTION:    AcpiDbReadTable
233  *
234  * PARAMETERS:  fp              - File that contains table
235  *              Table           - Return value, buffer with table
236  *              TableLength     - Return value, length of table
237  *
238  * RETURN:      Status
239  *
240  * DESCRIPTION: Load the DSDT from the file pointer
241  *
242  ******************************************************************************/
243 
244 static ACPI_STATUS
245 AcpiDbReadTable (
246     FILE                    *fp,
247     ACPI_TABLE_HEADER       **Table,
248     UINT32                  *TableLength)
249 {
250     ACPI_TABLE_HEADER       TableHeader;
251     UINT32                  Actual;
252     ACPI_STATUS             Status;
253     UINT32                  FileSize;
254     BOOLEAN                 StandardHeader = TRUE;
255 
256 
257     /* Get the file size */
258 
259     fseek (fp, 0, SEEK_END);
260     FileSize = (UINT32) ftell (fp);
261     fseek (fp, 0, SEEK_SET);
262 
263     if (FileSize < 4)
264     {
265         return (AE_BAD_HEADER);
266     }
267 
268     /* Read the signature */
269 
270     if (fread (&TableHeader, 1, 4, fp) != 4)
271     {
272         AcpiOsPrintf ("Could not read the table signature\n");
273         return (AE_BAD_HEADER);
274     }
275 
276     fseek (fp, 0, SEEK_SET);
277 
278     /* The RSDT, FACS and S3PT tables do not have standard ACPI headers */
279 
280     if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") ||
281         ACPI_COMPARE_NAME (TableHeader.Signature, "FACS") ||
282         ACPI_COMPARE_NAME (TableHeader.Signature, "S3PT"))
283     {
284         *TableLength = FileSize;
285         StandardHeader = FALSE;
286     }
287     else
288     {
289         /* Read the table header */
290 
291         if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
292                 sizeof (ACPI_TABLE_HEADER))
293         {
294             AcpiOsPrintf ("Could not read the table header\n");
295             return (AE_BAD_HEADER);
296         }
297 
298 #if 0
299         /* Validate the table header/length */
300 
301         Status = AcpiTbValidateTableHeader (&TableHeader);
302         if (ACPI_FAILURE (Status))
303         {
304             AcpiOsPrintf ("Table header is invalid!\n");
305             return (Status);
306         }
307 #endif
308 
309         /* File size must be at least as long as the Header-specified length */
310 
311         if (TableHeader.Length > FileSize)
312         {
313             AcpiOsPrintf (
314                 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
315                 TableHeader.Length, FileSize);
316 
317 #ifdef ACPI_ASL_COMPILER
318             Status = FlCheckForAscii (fp, NULL, FALSE);
319             if (ACPI_SUCCESS (Status))
320             {
321                 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
322                     TableHeader.Length, FileSize);
323             }
324 #endif
325             return (AE_BAD_HEADER);
326         }
327 
328 #ifdef ACPI_OBSOLETE_CODE
329         /* We only support a limited number of table types */
330 
331         if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
332             ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
333             ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
334         {
335             AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
336                 (char *) TableHeader.Signature);
337             ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
338             return (AE_ERROR);
339         }
340 #endif
341 
342         *TableLength = TableHeader.Length;
343     }
344 
345     /* Allocate a buffer for the table */
346 
347     *Table = AcpiOsAllocate ((size_t) FileSize);
348     if (!*Table)
349     {
350         AcpiOsPrintf (
351             "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
352             TableHeader.Signature, *TableLength);
353         return (AE_NO_MEMORY);
354     }
355 
356     /* Get the rest of the table */
357 
358     fseek (fp, 0, SEEK_SET);
359     Actual = fread (*Table, 1, (size_t) FileSize, fp);
360     if (Actual == FileSize)
361     {
362         if (StandardHeader)
363         {
364             /* Now validate the checksum */
365 
366             Status = AcpiTbVerifyChecksum ((void *) *Table,
367                         ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
368 
369             if (Status == AE_BAD_CHECKSUM)
370             {
371                 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
372                             FileSize, (*Table)->Length);
373                 return (Status);
374             }
375         }
376         return (AE_OK);
377     }
378 
379     if (Actual > 0)
380     {
381         AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
382             FileSize, Actual);
383         return (AE_OK);
384     }
385 
386     AcpiOsPrintf ("Error - could not read the table file\n");
387     AcpiOsFree (*Table);
388     *Table = NULL;
389     *TableLength = 0;
390 
391     return (AE_ERROR);
392 }
393 
394 
395 /*******************************************************************************
396  *
397  * FUNCTION:    AeLocalLoadTable
398  *
399  * PARAMETERS:  Table           - pointer to a buffer containing the entire
400  *                                table to be loaded
401  *
402  * RETURN:      Status
403  *
404  * DESCRIPTION: This function is called to load a table from the caller's
405  *              buffer. The buffer must contain an entire ACPI Table including
406  *              a valid header. The header fields will be verified, and if it
407  *              is determined that the table is invalid, the call will fail.
408  *
409  ******************************************************************************/
410 
411 static ACPI_STATUS
412 AeLocalLoadTable (
413     ACPI_TABLE_HEADER       *Table)
414 {
415     ACPI_STATUS             Status = AE_OK;
416 /*    ACPI_TABLE_DESC         TableInfo; */
417 
418 
419     ACPI_FUNCTION_TRACE (AeLocalLoadTable);
420 #if 0
421 
422 
423     if (!Table)
424     {
425         return_ACPI_STATUS (AE_BAD_PARAMETER);
426     }
427 
428     TableInfo.Pointer = Table;
429     Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
430     if (ACPI_FAILURE (Status))
431     {
432         return_ACPI_STATUS (Status);
433     }
434 
435     /* Install the new table into the local data structures */
436 
437     Status = AcpiTbInstallTable (&TableInfo);
438     if (ACPI_FAILURE (Status))
439     {
440         if (Status == AE_ALREADY_EXISTS)
441         {
442             /* Table already exists, no error */
443 
444             Status = AE_OK;
445         }
446 
447         /* Free table allocated by AcpiTbGetTable */
448 
449         AcpiTbDeleteSingleTable (&TableInfo);
450         return_ACPI_STATUS (Status);
451     }
452 
453 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
454 
455     Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
456     if (ACPI_FAILURE (Status))
457     {
458         /* Uninstall table and free the buffer */
459 
460         AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
461         return_ACPI_STATUS (Status);
462     }
463 #endif
464 #endif
465 
466     return_ACPI_STATUS (Status);
467 }
468 
469 
470 /*******************************************************************************
471  *
472  * FUNCTION:    AcpiDbReadTableFromFile
473  *
474  * PARAMETERS:  Filename         - File where table is located
475  *              Table            - Where a pointer to the table is returned
476  *
477  * RETURN:      Status
478  *
479  * DESCRIPTION: Get an ACPI table from a file
480  *
481  ******************************************************************************/
482 
483 ACPI_STATUS
484 AcpiDbReadTableFromFile (
485     char                    *Filename,
486     ACPI_TABLE_HEADER       **Table)
487 {
488     FILE                    *fp;
489     UINT32                  TableLength;
490     ACPI_STATUS             Status;
491 
492 
493     /* Open the file */
494 
495     fp = fopen (Filename, "rb");
496     if (!fp)
497     {
498         AcpiOsPrintf ("Could not open input file %s\n", Filename);
499         return (AE_ERROR);
500     }
501 
502     /* Get the entire file */
503 
504     fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
505     Status = AcpiDbReadTable (fp, Table, &TableLength);
506     fclose(fp);
507 
508     if (ACPI_FAILURE (Status))
509     {
510         AcpiOsPrintf ("Could not get table from the file\n");
511         return (Status);
512     }
513 
514     return (AE_OK);
515  }
516 #endif
517 
518 
519 /*******************************************************************************
520  *
521  * FUNCTION:    AcpiDbGetTableFromFile
522  *
523  * PARAMETERS:  Filename        - File where table is located
524  *              ReturnTable     - Where a pointer to the table is returned
525  *
526  * RETURN:      Status
527  *
528  * DESCRIPTION: Load an ACPI table from a file
529  *
530  ******************************************************************************/
531 
532 ACPI_STATUS
533 AcpiDbGetTableFromFile (
534     char                    *Filename,
535     ACPI_TABLE_HEADER       **ReturnTable)
536 {
537 #ifdef ACPI_APPLICATION
538     ACPI_STATUS             Status;
539     ACPI_TABLE_HEADER       *Table;
540     BOOLEAN                 IsAmlTable = TRUE;
541 
542 
543     Status = AcpiDbReadTableFromFile (Filename, &Table);
544     if (ACPI_FAILURE (Status))
545     {
546         return (Status);
547     }
548 
549 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
550     IsAmlTable = AcpiUtIsAmlTable (Table);
551 #endif
552 
553     if (IsAmlTable)
554     {
555         /* Attempt to recognize and install the table */
556 
557         Status = AeLocalLoadTable (Table);
558         if (ACPI_FAILURE (Status))
559         {
560             if (Status == AE_ALREADY_EXISTS)
561             {
562                 AcpiOsPrintf ("Table %4.4s is already installed\n",
563                     Table->Signature);
564             }
565             else
566             {
567                 AcpiOsPrintf ("Could not install table, %s\n",
568                     AcpiFormatException (Status));
569             }
570 
571             return (Status);
572         }
573 
574         fprintf (stderr,
575             "Acpi table [%4.4s] successfully installed and loaded\n",
576             Table->Signature);
577     }
578 
579     AcpiGbl_AcpiHardwarePresent = FALSE;
580     if (ReturnTable)
581     {
582         *ReturnTable = Table;
583     }
584 
585 
586 #endif  /* ACPI_APPLICATION */
587     return (AE_OK);
588 }
589 
590 #endif  /* ACPI_DEBUGGER */
591 
592