Sync ACPICA with Intel's version 20181213.
[dragonfly.git] / sys / contrib / dev / acpica / source / tools / acpixtract / axutils.c
blob61cb2d8a8b6aaa712f33bd027d05e9b026659578
1 /******************************************************************************
3 * Module Name: axutils - Utility functions for acpixtract tool.
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
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.
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.
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.
150 *****************************************************************************/
152 #include "acpixtract.h"
155 /*******************************************************************************
157 * FUNCTION: AxCheckAscii
159 * PARAMETERS: Name - Ascii string, at least as long as Count
160 * Count - Number of characters to check
162 * RETURN: None
164 * DESCRIPTION: Ensure that the requested number of characters are printable
165 * Ascii characters. Sets non-printable and null chars to <space>.
167 ******************************************************************************/
169 void
170 AxCheckAscii (
171 char *Name,
172 int Count)
174 int i;
177 for (i = 0; i < Count; i++)
179 if (!Name[i] || !isprint ((int) Name[i]))
181 Name[i] = ' ';
187 /*******************************************************************************
189 * FUNCTION: AxIsFileAscii
191 * PARAMETERS: Handle - To open input file
193 * RETURN: TRUE if file is entirely ASCII and printable
195 * DESCRIPTION: Verify that the input file is entirely ASCII.
197 ******************************************************************************/
199 BOOLEAN
200 AxIsFileAscii (
201 FILE *Handle)
203 UINT8 Byte;
204 UINT32 Offset = 0;
207 /* Read the entire file */
209 fseek (Handle, 0, SEEK_SET);
210 while (fread (&Byte, 1, 1, Handle) == 1)
213 * Ignore null characters. Some acpidump-type utilities insert
214 * a few of these, probably a bug in the utility. As long as these
215 * characters are in lines that are tossed (non-data), they
216 * won't cause a problem.
218 if (!Byte)
220 continue;
223 /* Check for an ASCII character */
225 if (!ACPI_IS_ASCII (Byte))
227 printf ("Found non-ascii char: %2.2X at file offset %u (0x%X)\n",
228 Byte, Offset, Offset);
229 if (!Gbl_ForceExtraction)
231 goto ErrorExit;
235 /* Ensure character is either printable or a "space" char */
237 else if (!isprint (Byte) && !isspace (Byte))
239 printf ("Found non-printable char: %2.2X at file offset %u (0x%X)\n",
240 Byte, Offset, Offset);
241 if (!Gbl_ForceExtraction)
243 goto ErrorExit;
247 Offset++;
250 /* File is OK (100% ASCII) */
252 fseek (Handle, 0, SEEK_SET);
253 return (TRUE);
255 ErrorExit:
257 printf ("File appears to be binary "
258 "(contains non-text or non-ascii characters)\n");
259 fseek (Handle, 0, SEEK_SET);
260 return (FALSE);
264 /******************************************************************************
266 * FUNCTION: AxIsEmptyLine
268 * PARAMETERS: Buffer - Line from input file
270 * RETURN: TRUE if line is empty (zero or more blanks only)
272 * DESCRIPTION: Determine if an input line is empty.
274 ******************************************************************************/
276 BOOLEAN
277 AxIsEmptyLine (
278 char *Buffer)
281 /* Skip all spaces */
283 while (*Buffer == ' ')
285 Buffer++;
288 /* Line is empty when a Unix or DOS-style line terminator is found. */
290 if ((*Buffer == '\r') || (*Buffer == '\n'))
292 return (1);
295 return (0);
299 /******************************************************************************
301 * FUNCTION: AxIsHexDataLine
303 * PARAMETERS: None
305 * RETURN: Status. 1 if the table header is valid, 0 otherwise.
307 * DESCRIPTION: Check for a valid line of hex data of the form:
309 * 00a0: 0c 00 00 00 03 00 00 00 43 48 41 35 0c 00 00 00 ........CHA5....
311 ******************************************************************************/
313 BOOLEAN
314 AxIsHexDataLine (
315 void)
318 if (AxIsEmptyLine (Gbl_LineBuffer) ||
319 (Gbl_LineBuffer[0] != ' '))
321 return (FALSE);
324 if (!strstr (Gbl_LineBuffer, ": "))
326 /* Not valid data line */
328 return (FALSE);
331 return (TRUE);
335 /******************************************************************************
337 * FUNCTION: AxIsDataBlockHeader
339 * PARAMETERS: None
341 * RETURN: Status. 1 if the table header is valid, 0 otherwise.
343 * DESCRIPTION: Check if the ACPI table identifier in the input acpidump text
344 * file is valid (of the form: <sig> @ <addr>).
346 ******************************************************************************/
348 BOOLEAN
349 AxIsDataBlockHeader (
350 void)
353 /* Ignore lines that are too short to be header lines */
355 if (strlen (Gbl_LineBuffer) < AX_MIN_BLOCK_HEADER_LENGTH)
357 return (FALSE);
360 /* Ignore empty lines and lines that start with a space */
362 if (AxIsEmptyLine (Gbl_LineBuffer) ||
363 (Gbl_LineBuffer[0] == ' '))
365 return (FALSE);
369 * Ignore lines that are not headers of the form <sig> @ <addr>.
370 * Basically, just look for the '@' symbol, surrounded by spaces.
372 * Examples of headers that must be supported:
374 * DSDT @ 0x737e4000
375 * XSDT @ 0x737f2fff
376 * RSD PTR @ 0xf6cd0
377 * SSDT @ (nil)
379 if (!AX_IS_TABLE_BLOCK_HEADER)
381 return (FALSE);
384 AxNormalizeSignature (Gbl_LineBuffer);
385 return (TRUE);
389 /*******************************************************************************
391 * FUNCTION: AxNormalizeSignature
393 * PARAMETERS: Name - Ascii string containing an ACPI signature
395 * RETURN: None
397 * DESCRIPTION: Change "RSD PTR" to "RSDP"
399 ******************************************************************************/
401 void
402 AxNormalizeSignature (
403 char *Signature)
406 if (!strncmp (Signature, "RSD ", 4))
408 Signature[3] = 'P';
413 /******************************************************************************
415 * FUNCTION: AxConvertToBinary
417 * PARAMETERS: InputLine - One line from the input acpidump file
418 * OutputData - Where the converted data is returned
420 * RETURN: The number of bytes actually converted
422 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
424 * NOTE: Assumes the input data line has been validated to be of the form:
426 * 0010: 48 53 57 55 4c 54 2d 52 01 00 00 00 49 4e 54 4c HSWULT-R....INTL
428 ******************************************************************************/
431 AxConvertToBinary (
432 char *InputLine,
433 unsigned char *OutputData)
435 char *ColonDelimiter;
436 int BytesConverted;
437 int Converted[16];
438 int i;
442 * Terminate input line immediately after the data. Otherwise, the
443 * second line below will not scan correctly.
445 * This handles varying lengths for the offset: line prefix. This support
446 * for tables larger than 1mb was added 05/2018.
448 * 00b0: 03 00 00 00 43 48 41 36 0c 00 00 00 03 00 00 00 ....CHA6........
449 * 00c0: 43 48 41 37 CHA7
451 * 012340b0: 03 00 00 00 43 48 41 36 0c 00 00 00 03 00 00 00 ....CHA6........
452 * 012340c0: 43 48 41 37 CHA7
454 ColonDelimiter = strchr (InputLine, ':');
455 ColonDelimiter [AX_HEX_DATA_LENGTH] = 0;
458 * Convert one line of table data, of the form:
459 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
461 * Example:
462 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
464 BytesConverted = sscanf (InputLine,
465 "%*s %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X",
466 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
467 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
468 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
469 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
471 if (BytesConverted == EOF)
473 printf ("EOF while converting ASCII line to binary\n");
474 return (-1);
478 * Pack converted data into a byte array.
479 * Note: BytesConverted == 0 is acceptable.
481 for (i = 0; i < BytesConverted; i++)
483 OutputData[i] = (unsigned char) Converted[i];
486 return (BytesConverted);
490 /******************************************************************************
492 * FUNCTION: AxCountTableInstances
494 * PARAMETERS: InputPathname - Filename for acpidump file
495 * Signature - Requested signature to count
497 * RETURN: The number of instances of the signature
499 * DESCRIPTION: Count the instances of tables with the given signature within
500 * the input acpidump file.
502 ******************************************************************************/
504 unsigned int
505 AxCountTableInstances (
506 char *InputPathname,
507 char *Signature)
509 FILE *InputFile;
510 unsigned int Instances = 0;
513 InputFile = fopen (InputPathname, "r");
514 if (!InputFile)
516 printf ("Could not open input file %s\n", InputPathname);
517 return (0);
520 /* Count the number of instances of this signature */
522 while (fgets (Gbl_InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile))
524 /* Ignore empty lines and lines that start with a space */
526 if (AxIsEmptyLine (Gbl_InstanceBuffer) ||
527 (Gbl_InstanceBuffer[0] == ' '))
529 continue;
532 AxNormalizeSignature (Gbl_InstanceBuffer);
533 if (ACPI_COMPARE_NAME (Gbl_InstanceBuffer, Signature))
535 Instances++;
539 fclose (InputFile);
540 return (Instances);
544 /******************************************************************************
546 * FUNCTION: AxGetNextInstance
548 * PARAMETERS: InputPathname - Filename for acpidump file
549 * Signature - Requested ACPI signature
551 * RETURN: The next instance number for this signature. Zero if this
552 * is the first instance of this signature.
554 * DESCRIPTION: Get the next instance number of the specified table. If this
555 * is the first instance of the table, create a new instance
556 * block. Note: only SSDT and PSDT tables can have multiple
557 * instances.
559 ******************************************************************************/
561 unsigned int
562 AxGetNextInstance (
563 char *InputPathname,
564 char *Signature)
566 AX_TABLE_INFO *Info;
569 Info = Gbl_TableListHead;
570 while (Info)
572 if (*(UINT32 *) Signature == Info->Signature)
574 break;
577 Info = Info->Next;
580 if (!Info)
582 /* Signature not found, create new table info block */
584 Info = malloc (sizeof (AX_TABLE_INFO));
585 if (!Info)
587 printf ("Could not allocate memory (0x%X bytes)\n",
588 (unsigned int) sizeof (AX_TABLE_INFO));
589 exit (0);
592 Info->Signature = *(UINT32 *) Signature;
593 Info->Instances = AxCountTableInstances (InputPathname, Signature);
594 Info->NextInstance = 1;
595 Info->Next = Gbl_TableListHead;
596 Gbl_TableListHead = Info;
599 if (Info->Instances > 1)
601 return (Info->NextInstance++);
604 return (0);
608 /******************************************************************************
610 * FUNCTION: AxConvertAndWrite
612 * PARAMETERS: OutputFile - Where to write the binary data
613 * ThisSignature - Signature of current ACPI table
615 * RETURN: Length of the converted line
617 * DESCRIPTION: Convert one line of input hex ascii text to binary, and write
618 * the binary data to the table output file.
620 * NOTE: Assumes the input data line has been validated to be of the form:
622 * 0010: 48 53 57 55 4c 54 2d 52 01 00 00 00 49 4e 54 4c HSWULT-R....INTL
624 ******************************************************************************/
627 AxConvertAndWrite (
628 FILE *OutputFile,
629 char *ThisSignature)
631 int BytesWritten;
632 int BytesConverted;
635 /* Convert one line of ascii hex data to binary */
637 BytesConverted = AxConvertToBinary (Gbl_LineBuffer, Gbl_BinaryData);
638 if (BytesConverted == EOF)
640 return (EOF);
642 if (!BytesConverted)
644 return (0);
647 /* Write the binary data */
649 BytesWritten = fwrite (Gbl_BinaryData, 1, BytesConverted, OutputFile);
650 if (BytesWritten != BytesConverted)
652 printf ("Error while writing file %s\n", Gbl_OutputFilename);
653 return (-1);
656 return (BytesWritten);
660 /******************************************************************************
662 * FUNCTION: AxDumpTableHeader
664 * PARAMETERS: Header - A binary ACPI table header
666 * RETURN: None
668 * DESCRIPTION: Display the contents of a standard ACPI table header
670 ******************************************************************************/
672 void
673 AxDumpTableHeader (
674 unsigned char *Header)
676 ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
677 ACPI_TABLE_RSDP *Rsdp = (ACPI_TABLE_RSDP *) (void *) Header;
678 ACPI_TABLE_FACS *Facs = (ACPI_TABLE_FACS *) (void *) Header;
681 /* RSDP has an oddball signature and header */
683 if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
685 AxCheckAscii ((char *) &Header[9], 6);
687 Gbl_TableCount++;
688 printf (" %.2u) %5.4s 0x%8.8X 0x%2.2X \"%6.6s\"\n",
689 Gbl_TableCount, "RSDP", Rsdp->Length, Rsdp->Revision, Rsdp->OemId);
690 return;
693 if (!AcpiUtValidNameseg (TableHeader->Signature))
695 return;
698 /* Signature and Table length */
700 Gbl_TableCount++;
701 printf (" %.2u) %5.4s 0x%8.8X", Gbl_TableCount, TableHeader->Signature,
702 TableHeader->Length);
704 /* FACS has only signature and length */
706 if (ACPI_COMPARE_NAME (TableHeader->Signature, "FACS"))
708 printf (" 0x%2.2X\n", Facs->Version);
709 return;
712 /* OEM IDs and Compiler IDs */
714 AxCheckAscii (TableHeader->OemId, 6);
715 AxCheckAscii (TableHeader->OemTableId, 8);
716 AxCheckAscii (TableHeader->AslCompilerId, 4);
718 printf (
719 " 0x%2.2X \"%6.6s\" \"%8.8s\" 0x%8.8X"
720 " \"%4.4s\" 0x%8.8X\n",
721 TableHeader->Revision, TableHeader->OemId,
722 TableHeader->OemTableId, TableHeader->OemRevision,
723 TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
727 #ifdef _AX_FUTURE_ENHANCEMENTS
729 /* Possible enhancement to validate table lengths */
731 void
732 AxCheckTableLengths (
733 UINT32 ByteCount,
734 UINT32 AmlByteCount)
737 if (AmlByteCount == 0)
739 return;
742 if (ByteCount == 0)
744 return;
747 if ((ByteCount < sizeof (ACPI_TABLE_HEADER)) &&
748 (ByteCount >= ACPI_NAME_SIZE))
750 printf (" : (Table too short for an ACPI table)");
753 else if (ByteCount != AmlByteCount)
755 printf (" : (Hex data length mismatch with AML length 0x%X)",
756 AmlByteCount);
759 printf ("\n");
761 #endif