kernel: Sync ACPICA with Intel's version 20140325.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpidump / apdump.c
1 /******************************************************************************
2  *
3  * Module Name: apdump - Dump routines for ACPI tables (acpidump)
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2014, 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 "acpidump.h"
45
46
47 /* Local prototypes */
48
49 static int
50 ApDumpTableBuffer (
51     ACPI_TABLE_HEADER       *Table,
52     UINT32                  Instance,
53     ACPI_PHYSICAL_ADDRESS   Address);
54
55
56 /******************************************************************************
57  *
58  * FUNCTION:    ApIsValidHeader
59  *
60  * PARAMETERS:  Table               - Pointer to table to be validated
61  *
62  * RETURN:      TRUE if the header appears to be valid. FALSE otherwise
63  *
64  * DESCRIPTION: Check for a valid ACPI table header
65  *
66  ******************************************************************************/
67
68 BOOLEAN
69 ApIsValidHeader (
70     ACPI_TABLE_HEADER       *Table)
71 {
72
73     if (!ACPI_VALIDATE_RSDP_SIG (Table->Signature))
74     {
75         /* Make sure signature is all ASCII and a valid ACPI name */
76
77         if (!AcpiUtValidAcpiName (Table->Signature))
78         {
79             fprintf (stderr, "Table signature (0x%8.8X) is invalid\n",
80                 *(UINT32 *) Table->Signature);
81             return (FALSE);
82         }
83
84         /* Check for minimum table length */
85
86         if (Table->Length < sizeof (ACPI_TABLE_HEADER))
87         {
88             fprintf (stderr, "Table length (0x%8.8X) is invalid\n",
89                 Table->Length);
90             return (FALSE);
91         }
92     }
93
94     return (TRUE);
95 }
96
97
98 /******************************************************************************
99  *
100  * FUNCTION:    ApIsValidChecksum
101  *
102  * PARAMETERS:  Table               - Pointer to table to be validated
103  *
104  * RETURN:      TRUE if the checksum appears to be valid. FALSE otherwise.
105  *
106  * DESCRIPTION: Check for a valid ACPI table checksum.
107  *
108  ******************************************************************************/
109
110 BOOLEAN
111 ApIsValidChecksum (
112     ACPI_TABLE_HEADER       *Table)
113 {
114     ACPI_STATUS             Status;
115     ACPI_TABLE_RSDP         *Rsdp;
116
117
118     if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
119     {
120         /*
121          * Checksum for RSDP.
122          * Note: Other checksums are computed during the table dump.
123          */
124         Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
125         Status = AcpiTbValidateRsdp (Rsdp);
126     }
127     else
128     {
129         Status = AcpiTbVerifyChecksum (Table, Table->Length);
130     }
131
132     if (ACPI_FAILURE (Status))
133     {
134         fprintf (stderr, "%4.4s: Warning: wrong checksum in table\n",
135             Table->Signature);
136     }
137
138     return (AE_OK);
139 }
140
141
142 /******************************************************************************
143  *
144  * FUNCTION:    ApGetTableLength
145  *
146  * PARAMETERS:  Table               - Pointer to the table
147  *
148  * RETURN:      Table length
149  *
150  * DESCRIPTION: Obtain table length according to table signature.
151  *
152  ******************************************************************************/
153
154 UINT32
155 ApGetTableLength (
156     ACPI_TABLE_HEADER       *Table)
157 {
158     ACPI_TABLE_RSDP         *Rsdp;
159
160
161     /* Check if table is valid */
162
163     if (!ApIsValidHeader (Table))
164     {
165         return (0);
166     }
167
168     if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
169     {
170         Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
171         return (Rsdp->Length);
172     }
173
174     /* Normal ACPI table */
175
176     return (Table->Length);
177 }
178
179
180 /******************************************************************************
181  *
182  * FUNCTION:    ApDumpTableBuffer
183  *
184  * PARAMETERS:  Table               - ACPI table to be dumped
185  *              Instance            - ACPI table instance no. to be dumped
186  *              Address             - Physical address of the table
187  *
188  * RETURN:      None
189  *
190  * DESCRIPTION: Dump an ACPI table in standard ASCII hex format, with a
191  *              header that is compatible with the AcpiXtract utility.
192  *
193  ******************************************************************************/
194
195 static int
196 ApDumpTableBuffer (
197     ACPI_TABLE_HEADER       *Table,
198     UINT32                  Instance,
199     ACPI_PHYSICAL_ADDRESS   Address)
200 {
201     UINT32                  TableLength;
202
203
204     TableLength = ApGetTableLength (Table);
205
206     /* Print only the header if requested */
207
208     if (Gbl_SummaryMode)
209     {
210         AcpiTbPrintTableHeader (Address, Table);
211         return (0);
212     }
213
214     /* Dump to binary file if requested */
215
216     if (Gbl_BinaryMode)
217     {
218         return (ApWriteToBinaryFile (Table, Instance));
219     }
220
221     /*
222      * Dump the table with header for use with acpixtract utility.
223      * Note: simplest to just always emit a 64-bit address. AcpiXtract
224      * utility can handle this.
225      */
226     printf ("%4.4s @ 0x%8.8X%8.8X\n", Table->Signature,
227         ACPI_FORMAT_UINT64 (Address));
228
229     AcpiUtDumpBuffer (ACPI_CAST_PTR (UINT8, Table), TableLength,
230         DB_BYTE_DISPLAY, 0);
231     printf ("\n");
232     return (0);
233 }
234
235
236 /******************************************************************************
237  *
238  * FUNCTION:    ApDumpAllTables
239  *
240  * PARAMETERS:  None
241  *
242  * RETURN:      Status
243  *
244  * DESCRIPTION: Get all tables from the RSDT/XSDT (or at least all of the
245  *              tables that we can possibly get).
246  *
247  ******************************************************************************/
248
249 int
250 ApDumpAllTables (
251     void)
252 {
253     ACPI_TABLE_HEADER       *Table;
254     UINT32                  Instance = 0;
255     ACPI_PHYSICAL_ADDRESS   Address;
256     ACPI_STATUS             Status;
257     int                     TableStatus;
258     UINT32                  i;
259
260
261     /* Get and dump all available ACPI tables */
262
263     for (i = 0; i < AP_MAX_ACPI_FILES; i++)
264     {
265         Status = AcpiOsGetTableByIndex (i, &Table, &Instance, &Address);
266         if (ACPI_FAILURE (Status))
267         {
268             /* AE_LIMIT means that no more tables are available */
269
270             if (Status == AE_LIMIT)
271             {
272                 return (0);
273             }
274             else if (i == 0)
275             {
276                 fprintf (stderr, "Could not get ACPI tables, %s\n",
277                     AcpiFormatException (Status));
278                 return (-1);
279             }
280             else
281             {
282                 fprintf (stderr, "Could not get ACPI table at index %u, %s\n",
283                     i, AcpiFormatException (Status));
284                 continue;
285             }
286         }
287
288         TableStatus = ApDumpTableBuffer (Table, Instance, Address);
289         free (Table);
290
291         if (TableStatus)
292         {
293             break;
294         }
295     }
296
297     /* Something seriously bad happened if the loop terminates here */
298
299     return (-1);
300 }
301
302
303 /******************************************************************************
304  *
305  * FUNCTION:    ApDumpTableByAddress
306  *
307  * PARAMETERS:  AsciiAddress        - Address for requested ACPI table
308  *
309  * RETURN:      Status
310  *
311  * DESCRIPTION: Get an ACPI table via a physical address and dump it.
312  *
313  ******************************************************************************/
314
315 int
316 ApDumpTableByAddress (
317     char                    *AsciiAddress)
318 {
319     ACPI_PHYSICAL_ADDRESS   Address;
320     ACPI_TABLE_HEADER       *Table;
321     ACPI_STATUS             Status;
322     int                     TableStatus;
323     UINT64                  LongAddress;
324
325
326     /* Convert argument to an integer physical address */
327
328     Status = AcpiUtStrtoul64 (AsciiAddress, 0, &LongAddress);
329     if (ACPI_FAILURE (Status))
330     {
331         fprintf (stderr, "%s: Could not convert to a physical address\n",
332             AsciiAddress);
333         return (-1);
334     }
335
336     Address = (ACPI_PHYSICAL_ADDRESS) LongAddress;
337     Status = AcpiOsGetTableByAddress (Address, &Table);
338     if (ACPI_FAILURE (Status))
339     {
340         fprintf (stderr, "Could not get table at 0x%8.8X%8.8X, %s\n",
341             ACPI_FORMAT_UINT64 (Address),
342             AcpiFormatException (Status));
343         return (-1);
344     }
345
346     TableStatus = ApDumpTableBuffer (Table, 0, Address);
347     free (Table);
348     return (TableStatus);
349 }
350
351
352 /******************************************************************************
353  *
354  * FUNCTION:    ApDumpTableByName
355  *
356  * PARAMETERS:  Signature           - Requested ACPI table signature
357  *
358  * RETURN:      Status
359  *
360  * DESCRIPTION: Get an ACPI table via a signature and dump it. Handles
361  *              multiple tables with the same signature (SSDTs).
362  *
363  ******************************************************************************/
364
365 int
366 ApDumpTableByName (
367     char                    *Signature)
368 {
369     char                    LocalSignature [ACPI_NAME_SIZE + 1];
370     UINT32                  Instance;
371     ACPI_TABLE_HEADER       *Table;
372     ACPI_PHYSICAL_ADDRESS   Address;
373     ACPI_STATUS             Status;
374     int                     TableStatus;
375
376
377     if (strlen (Signature) != ACPI_NAME_SIZE)
378     {
379         fprintf (stderr,
380             "Invalid table signature [%s]: must be exactly 4 characters\n",
381             Signature);
382         return (-1);
383     }
384
385     /* Table signatures are expected to be uppercase */
386
387     strcpy (LocalSignature, Signature);
388     AcpiUtStrupr (LocalSignature);
389
390     /* To be friendly, handle tables whose signatures do not match the name */
391
392     if (ACPI_COMPARE_NAME (LocalSignature, "FADT"))
393     {
394         strcpy (LocalSignature, ACPI_SIG_FADT);
395     }
396     else if (ACPI_COMPARE_NAME (LocalSignature, "MADT"))
397     {
398         strcpy (LocalSignature, ACPI_SIG_MADT);
399     }
400
401     /* Dump all instances of this signature (to handle multiple SSDTs) */
402
403     for (Instance = 0; Instance < AP_MAX_ACPI_FILES; Instance++)
404     {
405         Status = AcpiOsGetTableByName (LocalSignature, Instance,
406             &Table, &Address);
407         if (ACPI_FAILURE (Status))
408         {
409             /* AE_LIMIT means that no more tables are available */
410
411             if (Status == AE_LIMIT)
412             {
413                 return (0);
414             }
415
416             fprintf (stderr,
417                 "Could not get ACPI table with signature [%s], %s\n",
418                 LocalSignature, AcpiFormatException (Status));
419             return (-1);
420         }
421
422         TableStatus = ApDumpTableBuffer (Table, Instance, Address);
423         free (Table);
424
425         if (TableStatus)
426         {
427             break;
428         }
429     }
430
431     /* Something seriously bad happened if the loop terminates here */
432
433     return (-1);
434 }
435
436
437 /******************************************************************************
438  *
439  * FUNCTION:    ApDumpTableFromFile
440  *
441  * PARAMETERS:  Pathname            - File containing the binary ACPI table
442  *
443  * RETURN:      Status
444  *
445  * DESCRIPTION: Dump an ACPI table from a binary file
446  *
447  ******************************************************************************/
448
449 int
450 ApDumpTableFromFile (
451     char                    *Pathname)
452 {
453     ACPI_TABLE_HEADER       *Table;
454     UINT32                  FileSize = 0;
455     int                     TableStatus = -1;
456
457
458     /* Get the entire ACPI table from the file */
459
460     Table = ApGetTableFromFile (Pathname, &FileSize);
461     if (!Table)
462     {
463         return (-1);
464     }
465
466     /* File must be at least as long as the table length */
467
468     if (Table->Length > FileSize)
469     {
470         fprintf (stderr,
471             "Table length (0x%X) is too large for input file (0x%X) %s\n",
472             Table->Length, FileSize, Pathname);
473         goto Exit;
474     }
475
476     if (Gbl_VerboseMode)
477     {
478         fprintf (stderr,
479             "Input file:  %s contains table [%4.4s], 0x%X (%u) bytes\n",
480             Pathname, Table->Signature, FileSize, FileSize);
481     }
482
483     TableStatus = ApDumpTableBuffer (Table, 0, 0);
484
485 Exit:
486     free (Table);
487     return (TableStatus);
488 }
489
490
491 /******************************************************************************
492  *
493  * FUNCTION:    AcpiOs* print functions
494  *
495  * DESCRIPTION: Used for linkage with ACPICA modules
496  *
497  ******************************************************************************/
498
499 void ACPI_INTERNAL_VAR_XFACE
500 AcpiOsPrintf (
501     const char              *Fmt,
502     ...)
503 {
504     va_list                 Args;
505
506     va_start (Args, Fmt);
507     vfprintf (stdout, Fmt, Args);
508     va_end (Args);
509 }
510
511 void
512 AcpiOsVprintf (
513     const char              *Fmt,
514     va_list                 Args)
515 {
516     vfprintf (stdout, Fmt, Args);
517 }