2 /******************************************************************************
4 * Module Name: acpixtract - convert ascii ACPI tables to binary
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
123 /* Note: This is a 32-bit program only */
125 #define VERSION 0x20100107
126 #define FIND_HEADER 0
127 #define EXTRACT_DATA 1
128 #define BUFFER_SIZE 256
131 /* Local prototypes */
151 unsigned int MinimumInstances);
156 unsigned char *OutputData);
159 CountTableInstances (
165 char *InputPathname);
170 unsigned char *OutputData);
177 typedef struct acpi_table_header
181 unsigned char Revision;
182 unsigned char Checksum;
186 char AslCompilerId[4];
187 int AslCompilerRevision;
193 unsigned int Signature;
194 unsigned int Instances;
195 unsigned int NextInstance;
196 struct TableInfo *Next;
199 static struct TableInfo *ListHead = NULL;
200 static char Filename[16];
201 static unsigned char Data[16];
204 /******************************************************************************
206 * FUNCTION: DisplayUsage
208 * DESCRIPTION: Usage message
210 ******************************************************************************/
217 printf ("Usage: acpixtract [option] <InputFile>\n");
218 printf ("\nExtract binary ACPI tables from text acpidump output\n");
219 printf ("Default invocation extracts all DSDTs and SSDTs\n");
220 printf ("Version %8.8X\n\n", VERSION);
221 printf ("Options:\n");
222 printf (" -a Extract all tables, not just DSDT/SSDT\n");
223 printf (" -l List table summaries, do not extract\n");
224 printf (" -s<Signature> Extract all tables named <Signature>\n");
229 /*******************************************************************************
231 * FUNCTION: CheckAscii
233 * PARAMETERS: Name - Ascii string, at least as long as Count
234 * Count - Number of characters to check
238 * DESCRIPTION: Ensure that the requested number of characters are printable
239 * Ascii characters. Sets non-printable and null chars to <space>.
241 ******************************************************************************/
251 for (i = 0; i < Count; i++)
253 if (!Name[i] || !isprint ((int) Name[i]))
261 /*******************************************************************************
263 * FUNCTION: NormalizeSignature
265 * PARAMETERS: Name - Ascii string containing an ACPI signature
269 * DESCRIPTION: Change "RSD PTR" to "RSDP"
271 ******************************************************************************/
278 if (!strncmp (Signature, "RSD ", 4))
285 /******************************************************************************
287 * FUNCTION: ConvertLine
289 * PARAMETERS: InputLine - One line from the input acpidump file
290 * OutputData - Where the converted data is returned
292 * RETURN: The number of bytes actually converted
294 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
296 ******************************************************************************/
301 unsigned char *OutputData)
309 /* Terminate the input line at the end of the actual data (for sscanf) */
311 End = strstr (InputLine + 2, " ");
314 return (0); /* Don't understand the format */
319 * Convert one line of table data, of the form:
320 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
323 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
325 BytesConverted = sscanf (InputLine,
326 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
327 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
328 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
329 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
330 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
332 /* Pack converted data into a byte array */
334 for (i = 0; i < BytesConverted; i++)
336 OutputData[i] = (unsigned char) Converted[i];
339 return ((size_t) BytesConverted);
343 /******************************************************************************
345 * FUNCTION: GetTableHeader
347 * PARAMETERS: InputFile - Handle for the input acpidump file
348 * OutputData - Where the table header is returned
350 * RETURN: The actual number of bytes converted
352 * DESCRIPTION: Extract and convert an ACPI table header
354 ******************************************************************************/
359 unsigned char *OutputData)
361 size_t BytesConverted;
362 size_t TotalConverted = 0;
363 char Buffer[BUFFER_SIZE];
367 /* Get the full 36 byte header, requires 3 lines */
369 for (i = 0; i < 3; i++)
371 if (!fgets (Buffer, BUFFER_SIZE, InputFile))
373 return (TotalConverted);
376 BytesConverted = ConvertLine (Buffer, OutputData);
377 TotalConverted += BytesConverted;
380 if (BytesConverted != 16)
382 return (TotalConverted);
386 return (TotalConverted);
390 /******************************************************************************
392 * FUNCTION: CountTableInstances
394 * PARAMETERS: InputPathname - Filename for acpidump file
395 * Signature - Requested signature to count
397 * RETURN: The number of instances of the signature
399 * DESCRIPTION: Count the instances of tables with the given signature within
400 * the input acpidump file.
402 ******************************************************************************/
405 CountTableInstances (
409 char Buffer[BUFFER_SIZE];
411 unsigned int Instances = 0;
414 InputFile = fopen (InputPathname, "rt");
417 printf ("Could not open %s\n", InputPathname);
421 /* Count the number of instances of this signature */
423 while (fgets (Buffer, BUFFER_SIZE, InputFile))
425 /* Ignore empty lines and lines that start with a space */
427 if ((Buffer[0] == ' ') ||
433 NormalizeSignature (Buffer);
434 if (!strncmp (Buffer, Signature, 4))
445 /******************************************************************************
447 * FUNCTION: GetNextInstance
449 * PARAMETERS: InputPathname - Filename for acpidump file
450 * Signature - Requested ACPI signature
452 * RETURN: The next instance number for this signature. Zero if this
453 * is the first instance of this signature.
455 * DESCRIPTION: Get the next instance number of the specified table. If this
456 * is the first instance of the table, create a new instance
457 * block. Note: only SSDT and PSDT tables can have multiple
460 ******************************************************************************/
467 struct TableInfo *Info;
473 if (*(unsigned int *) Signature == Info->Signature)
483 /* Signature not found, create new table info block */
485 Info = malloc (sizeof (struct TableInfo));
488 printf ("Could not allocate memory\n");
492 Info->Signature = *(unsigned int *) Signature;
493 Info->Instances = CountTableInstances (InputPathname, Signature);
494 Info->NextInstance = 1;
495 Info->Next = ListHead;
499 if (Info->Instances > 1)
501 return (Info->NextInstance++);
508 /******************************************************************************
510 * FUNCTION: ExtractTables
512 * PARAMETERS: InputPathname - Filename for acpidump file
513 * Signature - Requested ACPI signature to extract.
514 * NULL means extract ALL tables.
515 * MinimumInstances - Min instances that are acceptable
519 * DESCRIPTION: Convert text ACPI tables to binary
521 ******************************************************************************/
527 unsigned int MinimumInstances)
529 char Buffer[BUFFER_SIZE];
531 FILE *OutputFile = NULL;
533 size_t TotalBytesWritten = 0;
534 size_t BytesConverted;
535 unsigned int State = FIND_HEADER;
536 unsigned int FoundTable = 0;
537 unsigned int Instances = 0;
538 unsigned int ThisInstance;
539 char ThisSignature[4];
543 /* Open input in text mode, output is in binary mode */
545 InputFile = fopen (InputPathname, "rt");
548 printf ("Could not open %s\n", InputPathname);
554 /* Are there enough instances of the table to continue? */
556 NormalizeSignature (Signature);
558 Instances = CountTableInstances (InputPathname, Signature);
559 if (Instances < MinimumInstances)
561 printf ("Table %s was not found in %s\n", Signature, InputPathname);
572 /* Convert all instances of the table to binary */
574 while (fgets (Buffer, BUFFER_SIZE, InputFile))
580 /* Ignore empty lines and lines that start with a space */
582 if ((Buffer[0] == ' ') ||
588 NormalizeSignature (Buffer);
589 strncpy (ThisSignature, Buffer, 4);
593 /* Ignore signatures that don't match */
595 if (strncmp (ThisSignature, Signature, 4))
602 * Get the instance number for this signature. Only the
603 * SSDT and PSDT tables can have multiple instances.
605 ThisInstance = GetNextInstance (InputPathname, ThisSignature);
607 /* Build an output filename and create/open the output file */
609 if (ThisInstance > 0)
611 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
615 sprintf (Filename, "%4.4s.dat", ThisSignature);
618 OutputFile = fopen (Filename, "w+b");
621 printf ("Could not open %s\n", Filename);
626 State = EXTRACT_DATA;
627 TotalBytesWritten = 0;
633 /* Empty line or non-data line terminates the data */
635 if ((Buffer[0] == '\n') ||
642 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
643 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
647 /* Convert the ascii data (one line of text) to binary */
649 BytesConverted = ConvertLine (Buffer, Data);
651 /* Write the binary data */
653 BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
654 if (BytesWritten != BytesConverted)
656 printf ("Write error on %s\n", Filename);
663 TotalBytesWritten += BytesConverted;
674 printf ("Table %s was not found in %s\n", Signature, InputPathname);
683 if (State == EXTRACT_DATA)
685 /* Received an EOF while extracting data */
687 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
688 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
697 /******************************************************************************
699 * FUNCTION: ListTables
701 * PARAMETERS: InputPathname - Filename for acpidump file
705 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
706 * perform an actual extraction of the tables.
708 ******************************************************************************/
715 char Buffer[BUFFER_SIZE];
717 unsigned char Header[48];
719 ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
722 /* Open input in text mode, output is in binary mode */
724 InputFile = fopen (InputPathname, "rt");
727 printf ("Could not open %s\n", InputPathname);
731 /* Dump the headers for all tables found in the input file */
733 printf ("\nSignature Length Revision OemId OemTableId"
734 " OemRevision CompilerId CompilerRevision\n\n");
736 while (fgets (Buffer, BUFFER_SIZE, InputFile))
738 /* Ignore empty lines and lines that start with a space */
740 if ((Buffer[0] == ' ') ||
746 /* Get the 36 byte header and display the fields */
748 HeaderSize = GetTableHeader (InputFile, Header);
754 /* RSDP has an oddball signature and header */
756 if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
758 CheckAscii ((char *) &Header[9], 6);
759 printf ("%8.4s \"%6.6s\"\n", "RSDP", &Header[9]);
764 /* Minimum size for table with standard header */
771 /* Signature and Table length */
774 printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
776 /* FACS has only signature and length */
778 if (!strncmp (TableHeader->Signature, "FACS", 4))
784 /* OEM IDs and Compiler IDs */
786 CheckAscii (TableHeader->OemId, 6);
787 CheckAscii (TableHeader->OemTableId, 8);
788 CheckAscii (TableHeader->AslCompilerId, 4);
790 printf (" %2.2X \"%6.6s\" \"%8.8s\" %8.8X \"%4.4s\" %8.8X\n",
791 TableHeader->Revision, TableHeader->OemId,
792 TableHeader->OemTableId, TableHeader->OemRevision,
793 TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
796 printf ("\nFound %u ACPI tables [%8.8X]\n", TableCount, VERSION);
802 /******************************************************************************
806 * DESCRIPTION: C main function
808 ******************************************************************************/
824 if (argv[1][0] == '-')
836 /* Extract all tables found */
838 return (ExtractTables (argv[2], NULL, 0));
842 /* List tables only, do not extract */
844 return (ListTables (argv[2]));
848 /* Extract only tables with this signature */
850 return (ExtractTables (argv[2], &argv[1][2], 1));
859 * Default output is the DSDT and all SSDTs. One DSDT is required,
860 * any SSDTs are optional.
862 Status = ExtractTables (argv[1], "DSDT", 1);
868 Status = ExtractTables (argv[1], "SSDT", 0);