]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/contrib/dev/acpica/components/debugger/dbfileio.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / contrib / dev / acpica / components / debugger / dbfileio.c
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_STRNCPY (AcpiGbl_DbDebugFilename, Name,
146         sizeof (AcpiGbl_DbDebugFilename));
147     AcpiGbl_DbOutputToFile = TRUE;
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 RSDP table does not have standard ACPI header */
279
280     if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD "))
281     {
282         *TableLength = FileSize;
283         StandardHeader = FALSE;
284     }
285     else
286     {
287         /* Read the table header */
288
289         if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
290                 sizeof (ACPI_TABLE_HEADER))
291         {
292             AcpiOsPrintf ("Could not read the table header\n");
293             return (AE_BAD_HEADER);
294         }
295
296 #if 0
297         /* Validate the table header/length */
298
299         Status = AcpiTbValidateTableHeader (&TableHeader);
300         if (ACPI_FAILURE (Status))
301         {
302             AcpiOsPrintf ("Table header is invalid!\n");
303             return (Status);
304         }
305 #endif
306
307         /* File size must be at least as long as the Header-specified length */
308
309         if (TableHeader.Length > FileSize)
310         {
311             AcpiOsPrintf (
312                 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
313                 TableHeader.Length, FileSize);
314
315 #ifdef ACPI_ASL_COMPILER
316             Status = FlCheckForAscii (fp, NULL, FALSE);
317             if (ACPI_SUCCESS (Status))
318             {
319                 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
320                     TableHeader.Length, FileSize);
321             }
322 #endif
323             return (AE_BAD_HEADER);
324         }
325
326 #ifdef ACPI_OBSOLETE_CODE
327         /* We only support a limited number of table types */
328
329         if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
330             !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
331             !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
332         {
333             AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
334                 (char *) TableHeader.Signature);
335             ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
336             return (AE_ERROR);
337         }
338 #endif
339
340         *TableLength = TableHeader.Length;
341     }
342
343     /* Allocate a buffer for the table */
344
345     *Table = AcpiOsAllocate ((size_t) FileSize);
346     if (!*Table)
347     {
348         AcpiOsPrintf (
349             "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
350             TableHeader.Signature, *TableLength);
351         return (AE_NO_MEMORY);
352     }
353
354     /* Get the rest of the table */
355
356     fseek (fp, 0, SEEK_SET);
357     Actual = fread (*Table, 1, (size_t) FileSize, fp);
358     if (Actual == FileSize)
359     {
360         if (StandardHeader)
361         {
362             /* Now validate the checksum */
363
364             Status = AcpiTbVerifyChecksum ((void *) *Table,
365                         ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
366
367             if (Status == AE_BAD_CHECKSUM)
368             {
369                 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
370                             FileSize, (*Table)->Length);
371                 return (Status);
372             }
373         }
374         return (AE_OK);
375     }
376
377     if (Actual > 0)
378     {
379         AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
380             FileSize, Actual);
381         return (AE_OK);
382     }
383
384     AcpiOsPrintf ("Error - could not read the table file\n");
385     AcpiOsFree (*Table);
386     *Table = NULL;
387     *TableLength = 0;
388     return (AE_ERROR);
389 }
390
391
392 /*******************************************************************************
393  *
394  * FUNCTION:    AeLocalLoadTable
395  *
396  * PARAMETERS:  Table           - pointer to a buffer containing the entire
397  *                                table to be loaded
398  *
399  * RETURN:      Status
400  *
401  * DESCRIPTION: This function is called to load a table from the caller's
402  *              buffer. The buffer must contain an entire ACPI Table including
403  *              a valid header. The header fields will be verified, and if it
404  *              is determined that the table is invalid, the call will fail.
405  *
406  ******************************************************************************/
407
408 static ACPI_STATUS
409 AeLocalLoadTable (
410     ACPI_TABLE_HEADER       *Table)
411 {
412     ACPI_STATUS             Status = AE_OK;
413 /*    ACPI_TABLE_DESC         TableInfo; */
414
415
416     ACPI_FUNCTION_TRACE (AeLocalLoadTable);
417 #if 0
418
419
420     if (!Table)
421     {
422         return_ACPI_STATUS (AE_BAD_PARAMETER);
423     }
424
425     TableInfo.Pointer = Table;
426     Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
427     if (ACPI_FAILURE (Status))
428     {
429         return_ACPI_STATUS (Status);
430     }
431
432     /* Install the new table into the local data structures */
433
434     Status = AcpiTbInstallTable (&TableInfo);
435     if (ACPI_FAILURE (Status))
436     {
437         if (Status == AE_ALREADY_EXISTS)
438         {
439             /* Table already exists, no error */
440
441             Status = AE_OK;
442         }
443
444         /* Free table allocated by AcpiTbGetTable */
445
446         AcpiTbDeleteSingleTable (&TableInfo);
447         return_ACPI_STATUS (Status);
448     }
449
450 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
451
452     Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
453     if (ACPI_FAILURE (Status))
454     {
455         /* Uninstall table and free the buffer */
456
457         AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
458         return_ACPI_STATUS (Status);
459     }
460 #endif
461 #endif
462
463     return_ACPI_STATUS (Status);
464 }
465
466
467 /*******************************************************************************
468  *
469  * FUNCTION:    AcpiDbReadTableFromFile
470  *
471  * PARAMETERS:  Filename         - File where table is located
472  *              Table            - Where a pointer to the table is returned
473  *
474  * RETURN:      Status
475  *
476  * DESCRIPTION: Get an ACPI table from a file
477  *
478  ******************************************************************************/
479
480 ACPI_STATUS
481 AcpiDbReadTableFromFile (
482     char                    *Filename,
483     ACPI_TABLE_HEADER       **Table)
484 {
485     FILE                    *File;
486     UINT32                  TableLength;
487     ACPI_STATUS             Status;
488
489
490     /* Open the file */
491
492     File = fopen (Filename, "rb");
493     if (!File)
494     {
495         AcpiOsPrintf ("Could not open input file %s\n", Filename);
496         return (AE_ERROR);
497     }
498
499     /* Get the entire file */
500
501     fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
502     Status = AcpiDbReadTable (File, Table, &TableLength);
503     fclose(File);
504
505     if (ACPI_FAILURE (Status))
506     {
507         AcpiOsPrintf ("Could not get table from the file\n");
508         return (Status);
509     }
510
511     return (AE_OK);
512  }
513 #endif
514
515
516 /*******************************************************************************
517  *
518  * FUNCTION:    AcpiDbGetTableFromFile
519  *
520  * PARAMETERS:  Filename        - File where table is located
521  *              ReturnTable     - Where a pointer to the table is returned
522  *
523  * RETURN:      Status
524  *
525  * DESCRIPTION: Load an ACPI table from a file
526  *
527  ******************************************************************************/
528
529 ACPI_STATUS
530 AcpiDbGetTableFromFile (
531     char                    *Filename,
532     ACPI_TABLE_HEADER       **ReturnTable)
533 {
534 #ifdef ACPI_APPLICATION
535     ACPI_STATUS             Status;
536     ACPI_TABLE_HEADER       *Table;
537     BOOLEAN                 IsAmlTable = TRUE;
538
539
540     Status = AcpiDbReadTableFromFile (Filename, &Table);
541     if (ACPI_FAILURE (Status))
542     {
543         return (Status);
544     }
545
546 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
547     IsAmlTable = AcpiUtIsAmlTable (Table);
548 #endif
549
550     if (IsAmlTable)
551     {
552         /* Attempt to recognize and install the table */
553
554         Status = AeLocalLoadTable (Table);
555         if (ACPI_FAILURE (Status))
556         {
557             if (Status == AE_ALREADY_EXISTS)
558             {
559                 AcpiOsPrintf ("Table %4.4s is already installed\n",
560                     Table->Signature);
561             }
562             else
563             {
564                 AcpiOsPrintf ("Could not install table, %s\n",
565                     AcpiFormatException (Status));
566             }
567
568             return (Status);
569         }
570
571         fprintf (stderr,
572             "Acpi table [%4.4s] successfully installed and loaded\n",
573             Table->Signature);
574     }
575
576     AcpiGbl_AcpiHardwarePresent = FALSE;
577     if (ReturnTable)
578     {
579         *ReturnTable = Table;
580     }
581
582
583 #endif  /* ACPI_APPLICATION */
584     return (AE_OK);
585 }
586
587 #endif  /* ACPI_DEBUGGER */