xref: /freebsd/sys/contrib/dev/acpica/common/dmtbdump.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /******************************************************************************
2  *
3  * Module Name: dmtbdump - Dump ACPI data tables that contain no AML code
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/include/acpi.h>
153 #include <contrib/dev/acpica/include/accommon.h>
154 #include <contrib/dev/acpica/include/acdisasm.h>
155 #include <contrib/dev/acpica/include/actables.h>
156 
157 /* This module used for application-level code only */
158 
159 #define _COMPONENT          ACPI_CA_DISASSEMBLER
160         ACPI_MODULE_NAME    ("dmtbdump")
161 
162 
163 /* Local prototypes */
164 
165 static void
166 AcpiDmValidateFadtLength (
167     UINT32                  Revision,
168     UINT32                  Length);
169 
170 
171 /*******************************************************************************
172  *
173  * FUNCTION:    AcpiDmDumpBuffer
174  *
175  * PARAMETERS:  Table               - ACPI Table or subtable
176  *              BufferOffset        - Offset of buffer from Table above
177  *              Length              - Length of the buffer
178  *              AbsoluteOffset      - Offset of buffer in the main ACPI table
179  *              Header              - Name of the buffer field (printed on the
180  *                                    first line only.)
181  *
182  * RETURN:      None
183  *
184  * DESCRIPTION: Format the contents of an arbitrary length data buffer (in the
185  *              disassembler output format.)
186  *
187  ******************************************************************************/
188 
189 void
190 AcpiDmDumpBuffer (
191     void                    *Table,
192     UINT32                  BufferOffset,
193     UINT32                  Length,
194     UINT32                  AbsoluteOffset,
195     char                    *Header)
196 {
197     UINT8                   *Buffer;
198     UINT8                   BufChar;
199     UINT32                  i;
200     UINT32                  j;
201 
202 
203     if (!Length)
204     {
205         return;
206     }
207 
208     Buffer = ACPI_CAST_PTR (UINT8, Table) + BufferOffset;
209     i = 0;
210 
211     while (i < Length)
212     {
213         if ((Length > 16) && (i != 0))
214         {
215         if ((Length - i) < 16)
216             AcpiOsPrintf ("\n/* %3.3Xh %4.4u %3u */                            ", AbsoluteOffset, AbsoluteOffset, Length - i);
217         else
218             AcpiOsPrintf ("\n/* %3.3Xh %4.4u  16 */                            ", AbsoluteOffset, AbsoluteOffset);
219         }
220         AbsoluteOffset += 16;
221 
222         /* Emit the raw data bytes*/
223 
224         for (j = 0; j < 16; j++)
225         {
226             if (i + j >= Length)
227             {
228                 /* Dump fill spaces */
229 
230                 AcpiOsPrintf ("%*s", (48 - (3 * (Length -i))), " ");
231                 break;
232             }
233             AcpiOsPrintf ("%.02X ", Buffer[(ACPI_SIZE) i + j]);
234         }
235 
236         /* Emit the ASCII equivalent to the raw data bytes */
237 
238         for (j = 0; j < 16; j++)
239         {
240             if (i + j >= Length)
241             {
242                 AcpiOsPrintf (" */\\\n");
243                 return;
244             }
245 
246             /*
247              * Add comment characters so rest of line is ignored when
248              * compiled
249              */
250             if (j == 0)
251             {
252                 AcpiOsPrintf ("/* ");
253             }
254 
255             BufChar = Buffer[(ACPI_SIZE) i + j];
256             if (isprint (BufChar))
257             {
258                 AcpiOsPrintf ("%c", BufChar);
259             }
260             else
261             {
262                 AcpiOsPrintf (".");
263             }
264         }
265 
266         /* Done with that line. */
267         /* Close the comment and insert a backslash - line continuation character */
268 
269         AcpiOsPrintf (" */\\");
270 
271         i += 16; /* Point to next line */
272     }
273 
274     AcpiOsPrintf ("\n");
275 }
276 
277 
278 /*******************************************************************************
279  *
280  * FUNCTION:    AcpiDmDumpUnicode
281  *
282  * PARAMETERS:  Table               - ACPI Table or subtable
283  *              BufferOffset        - Offset of buffer from Table above
284  *              ByteLength          - Length of the buffer
285  *
286  * RETURN:      None
287  *
288  * DESCRIPTION: Validate and dump the contents of a buffer that contains
289  *              unicode data. The output is a standard ASCII string. If it
290  *              appears that the data is not unicode, the buffer is dumped
291  *              as hex characters.
292  *
293  ******************************************************************************/
294 
295 void
296 AcpiDmDumpUnicode (
297     void                    *Table,
298     UINT32                  BufferOffset,
299     UINT32                  ByteLength)
300 {
301     UINT8                   *Buffer;
302     UINT32                  Length;
303     UINT32                  i;
304 
305 
306     Buffer = ((UINT8 *) Table) + BufferOffset;
307     Length = ByteLength - 2; /* Last two bytes are the null terminator */
308 
309     /* Ensure all low bytes are entirely printable ASCII */
310 
311     for (i = 0; i < Length; i += 2)
312     {
313         if (!isprint (Buffer[i]))
314         {
315             goto DumpRawBuffer;
316         }
317     }
318 
319     /* Ensure all high bytes are zero */
320 
321     for (i = 1; i < Length; i += 2)
322     {
323         if (Buffer[i])
324         {
325             goto DumpRawBuffer;
326         }
327     }
328 
329     /* Dump the buffer as a normal string */
330 
331     AcpiOsPrintf ("\"");
332     for (i = 0; i < Length; i += 2)
333     {
334         AcpiOsPrintf ("%c", Buffer[i]);
335     }
336 
337     AcpiOsPrintf ("\"\n");
338     return;
339 
340 DumpRawBuffer:
341     AcpiDmDumpBuffer (Table, BufferOffset, ByteLength,
342         BufferOffset, NULL);
343     AcpiOsPrintf ("\n");
344 }
345 
346 
347 /*******************************************************************************
348  *
349  * FUNCTION:    AcpiDmDumpRsdp
350  *
351  * PARAMETERS:  Table               - A RSDP
352  *
353  * RETURN:      Length of the table (there is not always a length field,
354  *              use revision or length if available (ACPI 2.0+))
355  *
356  * DESCRIPTION: Format the contents of a RSDP
357  *
358  ******************************************************************************/
359 
360 UINT32
361 AcpiDmDumpRsdp (
362     ACPI_TABLE_HEADER       *Table)
363 {
364     ACPI_TABLE_RSDP         *Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
365     UINT32                  Length = sizeof (ACPI_RSDP_COMMON);
366     UINT8                   Checksum;
367     ACPI_STATUS             Status;
368 
369 
370     /* Dump the common ACPI 1.0 portion */
371 
372     Status = AcpiDmDumpTable (Length, 0, Table, 0, AcpiDmTableInfoRsdp1);
373     if (ACPI_FAILURE (Status))
374     {
375         return (Length);
376     }
377 
378     /* Validate the first checksum */
379 
380     Checksum = AcpiUtGenerateChecksum (Rsdp, sizeof (ACPI_RSDP_COMMON),
381         Rsdp->Checksum);
382     if (Checksum != Rsdp->Checksum)
383     {
384         AcpiOsPrintf ("/* Incorrect Checksum above, should be 0x%2.2X */\n",
385             Checksum);
386     }
387 
388     /* The RSDP for ACPI 2.0+ contains more data and has a Length field */
389 
390     if (Rsdp->Revision > 0)
391     {
392         Length = Rsdp->Length;
393         Status = AcpiDmDumpTable (Length, 0, Table, 0, AcpiDmTableInfoRsdp2);
394         if (ACPI_FAILURE (Status))
395         {
396             return (Length);
397         }
398 
399         /* Validate the extended checksum over entire RSDP */
400 
401         Checksum = AcpiUtGenerateChecksum (Rsdp, sizeof (ACPI_TABLE_RSDP),
402             Rsdp->ExtendedChecksum);
403         if (Checksum != Rsdp->ExtendedChecksum)
404         {
405             AcpiOsPrintf (
406                 "/* Incorrect Extended Checksum above, should be 0x%2.2X */\n",
407                 Checksum);
408         }
409     }
410 
411     return (Length);
412 }
413 
414 
415 /*******************************************************************************
416  *
417  * FUNCTION:    AcpiDmDumpRsdt
418  *
419  * PARAMETERS:  Table               - A RSDT
420  *
421  * RETURN:      None
422  *
423  * DESCRIPTION: Format the contents of a RSDT
424  *
425  ******************************************************************************/
426 
427 void
428 AcpiDmDumpRsdt (
429     ACPI_TABLE_HEADER       *Table)
430 {
431     UINT32                  *Array;
432     UINT32                  Entries;
433     UINT32                  Offset;
434     UINT32                  i;
435 
436 
437     /* Point to start of table pointer array */
438 
439     Array = ACPI_CAST_PTR (ACPI_TABLE_RSDT, Table)->TableOffsetEntry;
440     Offset = sizeof (ACPI_TABLE_HEADER);
441 
442     /* RSDT uses 32-bit pointers */
443 
444     Entries = (Table->Length - sizeof (ACPI_TABLE_HEADER)) / sizeof (UINT32);
445 
446     for (i = 0; i < Entries; i++)
447     {
448         AcpiDmLineHeader2 (Offset, sizeof (UINT32), "ACPI Table Address", i);
449         AcpiOsPrintf ("%8.8X\n", Array[i]);
450         Offset += sizeof (UINT32);
451     }
452 }
453 
454 
455 /*******************************************************************************
456  *
457  * FUNCTION:    AcpiDmDumpXsdt
458  *
459  * PARAMETERS:  Table               - A XSDT
460  *
461  * RETURN:      None
462  *
463  * DESCRIPTION: Format the contents of a XSDT
464  *
465  ******************************************************************************/
466 
467 void
468 AcpiDmDumpXsdt (
469     ACPI_TABLE_HEADER       *Table)
470 {
471     UINT64                  *Array;
472     UINT32                  Entries;
473     UINT32                  Offset;
474     UINT32                  i;
475 
476 
477     /* Point to start of table pointer array */
478 
479     Array = ACPI_CAST_PTR (ACPI_TABLE_XSDT, Table)->TableOffsetEntry;
480     Offset = sizeof (ACPI_TABLE_HEADER);
481 
482     /* XSDT uses 64-bit pointers */
483 
484     Entries = (Table->Length - sizeof (ACPI_TABLE_HEADER)) / sizeof (UINT64);
485 
486     for (i = 0; i < Entries; i++)
487     {
488         AcpiDmLineHeader2 (Offset, sizeof (UINT64), "ACPI Table Address", i);
489         AcpiOsPrintf ("%8.8X%8.8X\n", ACPI_FORMAT_UINT64 (Array[i]));
490         Offset += sizeof (UINT64);
491     }
492 }
493 
494 
495 /*******************************************************************************
496  *
497  * FUNCTION:    AcpiDmDumpFadt
498  *
499  * PARAMETERS:  Table               - A FADT
500  *
501  * RETURN:      None
502  *
503  * DESCRIPTION: Format the contents of a FADT
504  *
505  * NOTE:        We cannot depend on the FADT version to indicate the actual
506  *              contents of the FADT because of BIOS bugs. The table length
507  *              is the only reliable indicator.
508  *
509  ******************************************************************************/
510 
511 void
512 AcpiDmDumpFadt (
513     ACPI_TABLE_HEADER       *Table)
514 {
515     ACPI_STATUS             Status;
516 
517 
518     /* Always dump the minimum FADT revision 1 fields (ACPI 1.0) */
519 
520     Status = AcpiDmDumpTable (Table->Length, 0, Table, 0,
521         AcpiDmTableInfoFadt1);
522     if (ACPI_FAILURE (Status))
523     {
524         return;
525     }
526 
527     /* Check for FADT revision 2 fields (ACPI 1.0B MS extensions) */
528 
529     if ((Table->Length > ACPI_FADT_V1_SIZE) &&
530         (Table->Length <= ACPI_FADT_V2_SIZE))
531     {
532         Status = AcpiDmDumpTable (Table->Length, 0, Table, 0,
533             AcpiDmTableInfoFadt2);
534         if (ACPI_FAILURE (Status))
535         {
536             return;
537         }
538     }
539 
540     /* Check for FADT revision 3/4 fields and up (ACPI 2.0+ extended data) */
541 
542     else if (Table->Length > ACPI_FADT_V2_SIZE)
543     {
544         Status = AcpiDmDumpTable (Table->Length, 0, Table, 0,
545             AcpiDmTableInfoFadt3);
546         if (ACPI_FAILURE (Status))
547         {
548             return;
549         }
550 
551         /* Check for FADT revision 5 fields and up (ACPI 5.0+) */
552 
553         if (Table->Length > ACPI_FADT_V3_SIZE)
554         {
555             Status = AcpiDmDumpTable (Table->Length, 0, Table, 0,
556                 AcpiDmTableInfoFadt5);
557             if (ACPI_FAILURE (Status))
558             {
559                 return;
560             }
561         }
562 
563         /* Check for FADT revision 6 fields and up (ACPI 6.0+) */
564 
565         if (Table->Length > ACPI_FADT_V5_SIZE)
566         {
567             Status = AcpiDmDumpTable (Table->Length, 0, Table, 0,
568                 AcpiDmTableInfoFadt6);
569             if (ACPI_FAILURE (Status))
570             {
571                 return;
572             }
573         }
574     }
575 
576     /* Validate various fields in the FADT, including length */
577 
578     AcpiTbCreateLocalFadt (Table, Table->Length);
579 
580     /* Validate FADT length against the revision */
581 
582     AcpiDmValidateFadtLength (Table->Revision, Table->Length);
583 }
584 
585 
586 /*******************************************************************************
587  *
588  * FUNCTION:    AcpiDmValidateFadtLength
589  *
590  * PARAMETERS:  Revision            - FADT revision (Header->Revision)
591  *              Length              - FADT length (Header->Length
592  *
593  * RETURN:      None
594  *
595  * DESCRIPTION: Check the FADT revision against the expected table length for
596  *              that revision. Issue a warning if the length is not what was
597  *              expected. This seems to be such a common BIOS bug that the
598  *              FADT revision has been rendered virtually meaningless.
599  *
600  ******************************************************************************/
601 
602 static void
603 AcpiDmValidateFadtLength (
604     UINT32                  Revision,
605     UINT32                  Length)
606 {
607     UINT32                  ExpectedLength;
608 
609 
610     switch (Revision)
611     {
612     case 0:
613 
614         AcpiOsPrintf ("// ACPI Warning: Invalid FADT revision: 0\n");
615         return;
616 
617     case 1:
618 
619         ExpectedLength = ACPI_FADT_V1_SIZE;
620         break;
621 
622     case 2:
623 
624         ExpectedLength = ACPI_FADT_V2_SIZE;
625         break;
626 
627     case 3:
628     case 4:
629 
630         ExpectedLength = ACPI_FADT_V3_SIZE;
631         break;
632 
633     case 5:
634 
635         ExpectedLength = ACPI_FADT_V5_SIZE;
636         break;
637 
638     case 6:
639 
640         ExpectedLength = ACPI_FADT_V6_SIZE;
641         break;
642 
643     default:
644 
645         return;
646     }
647 
648     if (Length == ExpectedLength)
649     {
650         return;
651     }
652 
653     AcpiOsPrintf (
654         "\n// ACPI Warning: FADT revision %X does not match length: "
655         "found %X expected %X\n",
656         Revision, Length, ExpectedLength);
657 }
658