xref: /illumos-gate/usr/src/cmd/acpi/acpidump/apdump.c (revision 5f82aa32fbc5dc2c59bca6ff315f44a4c4c9ea86)
1 /******************************************************************************
2  *
3  * Module Name: apdump - Dump routines for ACPI tables (acpidump)
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 "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 (!AcpiUtValidNameseg (Table->Signature))
78         {
79             AcpiLogError ("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             AcpiLogError ("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         AcpiLogError ("%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 (AcpiTbGetRsdpLength (Rsdp));
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     AcpiUtFilePrintf (Gbl_OutputFile, "%4.4s @ 0x%8.8X%8.8X\n",
227         Table->Signature, ACPI_FORMAT_UINT64 (Address));
228 
229     AcpiUtDumpBufferToFile (Gbl_OutputFile,
230         ACPI_CAST_PTR (UINT8, Table), TableLength,
231         DB_BYTE_DISPLAY, 0);
232     AcpiUtFilePrintf (Gbl_OutputFile, "\n");
233     return (0);
234 }
235 
236 
237 /******************************************************************************
238  *
239  * FUNCTION:    ApDumpAllTables
240  *
241  * PARAMETERS:  None
242  *
243  * RETURN:      Status
244  *
245  * DESCRIPTION: Get all tables from the RSDT/XSDT (or at least all of the
246  *              tables that we can possibly get).
247  *
248  ******************************************************************************/
249 
250 int
251 ApDumpAllTables (
252     void)
253 {
254     ACPI_TABLE_HEADER       *Table;
255     UINT32                  Instance = 0;
256     ACPI_PHYSICAL_ADDRESS   Address;
257     ACPI_STATUS             Status;
258     int                     TableStatus;
259     UINT32                  i;
260 
261 
262     /* Get and dump all available ACPI tables */
263 
264     for (i = 0; i < AP_MAX_ACPI_FILES; i++)
265     {
266         Status = AcpiOsGetTableByIndex (i, &Table, &Instance, &Address);
267         if (ACPI_FAILURE (Status))
268         {
269             /* AE_LIMIT means that no more tables are available */
270 
271             if (Status == AE_LIMIT)
272             {
273                 return (0);
274             }
275             else if (i == 0)
276             {
277                 AcpiLogError ("Could not get ACPI tables, %s\n",
278                     AcpiFormatException (Status));
279                 return (-1);
280             }
281             else
282             {
283                 AcpiLogError ("Could not get ACPI table at index %u, %s\n",
284                     i, AcpiFormatException (Status));
285                 continue;
286             }
287         }
288 
289         TableStatus = ApDumpTableBuffer (Table, Instance, Address);
290         ACPI_FREE (Table);
291 
292         if (TableStatus)
293         {
294             break;
295         }
296     }
297 
298     /* Something seriously bad happened if the loop terminates here */
299 
300     return (-1);
301 }
302 
303 
304 /******************************************************************************
305  *
306  * FUNCTION:    ApDumpTableByAddress
307  *
308  * PARAMETERS:  AsciiAddress        - Address for requested ACPI table
309  *
310  * RETURN:      Status
311  *
312  * DESCRIPTION: Get an ACPI table via a physical address and dump it.
313  *
314  ******************************************************************************/
315 
316 int
317 ApDumpTableByAddress (
318     char                    *AsciiAddress)
319 {
320     ACPI_PHYSICAL_ADDRESS   Address;
321     ACPI_TABLE_HEADER       *Table;
322     ACPI_STATUS             Status;
323     int                     TableStatus;
324     UINT64                  LongAddress;
325 
326 
327     /* Convert argument to an integer physical address */
328 
329     Status = AcpiUtStrtoul64 (AsciiAddress, ACPI_ANY_BASE,
330         ACPI_MAX64_BYTE_WIDTH, &LongAddress);
331     if (ACPI_FAILURE (Status))
332     {
333         AcpiLogError ("%s: Could not convert to a physical address\n",
334             AsciiAddress);
335         return (-1);
336     }
337 
338     Address = (ACPI_PHYSICAL_ADDRESS) LongAddress;
339     Status = AcpiOsGetTableByAddress (Address, &Table);
340     if (ACPI_FAILURE (Status))
341     {
342         AcpiLogError ("Could not get table at 0x%8.8X%8.8X, %s\n",
343             ACPI_FORMAT_UINT64 (Address),
344             AcpiFormatException (Status));
345         return (-1);
346     }
347 
348     TableStatus = ApDumpTableBuffer (Table, 0, Address);
349     ACPI_FREE (Table);
350     return (TableStatus);
351 }
352 
353 
354 /******************************************************************************
355  *
356  * FUNCTION:    ApDumpTableByName
357  *
358  * PARAMETERS:  Signature           - Requested ACPI table signature
359  *
360  * RETURN:      Status
361  *
362  * DESCRIPTION: Get an ACPI table via a signature and dump it. Handles
363  *              multiple tables with the same signature (SSDTs).
364  *
365  ******************************************************************************/
366 
367 int
368 ApDumpTableByName (
369     char                    *Signature)
370 {
371     char                    LocalSignature [ACPI_NAME_SIZE + 1];
372     UINT32                  Instance;
373     ACPI_TABLE_HEADER       *Table;
374     ACPI_PHYSICAL_ADDRESS   Address;
375     ACPI_STATUS             Status;
376     int                     TableStatus;
377 
378 
379     if (strlen (Signature) != ACPI_NAME_SIZE)
380     {
381         AcpiLogError (
382             "Invalid table signature [%s]: must be exactly 4 characters\n",
383             Signature);
384         return (-1);
385     }
386 
387     /* Table signatures are expected to be uppercase */
388 
389     strcpy (LocalSignature, Signature);
390     AcpiUtStrupr (LocalSignature);
391 
392     /* To be friendly, handle tables whose signatures do not match the name */
393 
394     if (ACPI_COMPARE_NAME (LocalSignature, "FADT"))
395     {
396         strcpy (LocalSignature, ACPI_SIG_FADT);
397     }
398     else if (ACPI_COMPARE_NAME (LocalSignature, "MADT"))
399     {
400         strcpy (LocalSignature, ACPI_SIG_MADT);
401     }
402 
403     /* Dump all instances of this signature (to handle multiple SSDTs) */
404 
405     for (Instance = 0; Instance < AP_MAX_ACPI_FILES; Instance++)
406     {
407         Status = AcpiOsGetTableByName (LocalSignature, Instance,
408             &Table, &Address);
409         if (ACPI_FAILURE (Status))
410         {
411             /* AE_LIMIT means that no more tables are available */
412 
413             if (Status == AE_LIMIT)
414             {
415                 return (0);
416             }
417 
418             AcpiLogError (
419                 "Could not get ACPI table with signature [%s], %s\n",
420                 LocalSignature, AcpiFormatException (Status));
421             return (-1);
422         }
423 
424         TableStatus = ApDumpTableBuffer (Table, Instance, Address);
425         ACPI_FREE (Table);
426 
427         if (TableStatus)
428         {
429             break;
430         }
431     }
432 
433     /* Something seriously bad happened if the loop terminates here */
434 
435     return (-1);
436 }
437 
438 
439 /******************************************************************************
440  *
441  * FUNCTION:    ApDumpTableFromFile
442  *
443  * PARAMETERS:  Pathname            - File containing the binary ACPI table
444  *
445  * RETURN:      Status
446  *
447  * DESCRIPTION: Dump an ACPI table from a binary file
448  *
449  ******************************************************************************/
450 
451 int
452 ApDumpTableFromFile (
453     char                    *Pathname)
454 {
455     ACPI_TABLE_HEADER       *Table;
456     UINT32                  FileSize = 0;
457     int                     TableStatus = -1;
458 
459 
460     /* Get the entire ACPI table from the file */
461 
462     Table = ApGetTableFromFile (Pathname, &FileSize);
463     if (!Table)
464     {
465         return (-1);
466     }
467 
468     if (!AcpiUtValidNameseg (Table->Signature))
469     {
470         AcpiLogError (
471             "No valid ACPI signature was found in input file %s\n",
472             Pathname);
473     }
474 
475     /* File must be at least as long as the table length */
476 
477     if (Table->Length > FileSize)
478     {
479         AcpiLogError (
480             "Table length (0x%X) is too large for input file (0x%X) %s\n",
481             Table->Length, FileSize, Pathname);
482         goto Exit;
483     }
484 
485     if (Gbl_VerboseMode)
486     {
487         AcpiLogError (
488             "Input file:  %s contains table [%4.4s], 0x%X (%u) bytes\n",
489             Pathname, Table->Signature, FileSize, FileSize);
490     }
491 
492     TableStatus = ApDumpTableBuffer (Table, 0, 0);
493 
494 Exit:
495     ACPI_FREE (Table);
496     return (TableStatus);
497 }
498