2 /******************************************************************************
4 * Module Name: acpixtract - convert ascii ACPI tables to binary
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
51 /* Note: This is a 32-bit program only */
53 #define VERSION 0x20110225
55 #define EXTRACT_DATA 1
56 #define BUFFER_SIZE 256
57 #define HEADER_LINE_LENGTH 17 /* strlen ("FACP @ 0x737e1000") */
59 /* Local prototypes */
79 unsigned int MinimumInstances);
84 unsigned char *OutputData);
98 unsigned char *OutputData);
105 typedef struct acpi_table_header
109 unsigned char Revision;
110 unsigned char Checksum;
114 char AslCompilerId[4];
115 int AslCompilerRevision;
121 unsigned int Signature;
122 unsigned int Instances;
123 unsigned int NextInstance;
124 struct TableInfo *Next;
127 static struct TableInfo *ListHead = NULL;
128 static char Filename[16];
129 static unsigned char Data[16];
132 /******************************************************************************
134 * FUNCTION: DisplayUsage
136 * DESCRIPTION: Usage message
138 ******************************************************************************/
145 printf ("Usage: acpixtract [option] <InputFile>\n");
146 printf ("\nExtract binary ACPI tables from text acpidump output\n");
147 printf ("Default invocation extracts all DSDTs and SSDTs\n");
148 printf ("Version %8.8X\n\n", VERSION);
149 printf ("Options:\n");
150 printf (" -a Extract all tables, not just DSDT/SSDT\n");
151 printf (" -l List table summaries, do not extract\n");
152 printf (" -s<Signature> Extract all tables named <Signature>\n");
157 /*******************************************************************************
159 * FUNCTION: CheckAscii
161 * PARAMETERS: Name - Ascii string, at least as long as Count
162 * Count - Number of characters to check
166 * DESCRIPTION: Ensure that the requested number of characters are printable
167 * Ascii characters. Sets non-printable and null chars to <space>.
169 ******************************************************************************/
179 for (i = 0; i < Count; i++)
181 if (!Name[i] || !isprint ((int) Name[i]))
189 /*******************************************************************************
191 * FUNCTION: NormalizeSignature
193 * PARAMETERS: Name - Ascii string containing an ACPI signature
197 * DESCRIPTION: Change "RSD PTR" to "RSDP"
199 ******************************************************************************/
206 if (!strncmp (Signature, "RSD ", 4))
213 /******************************************************************************
215 * FUNCTION: ConvertLine
217 * PARAMETERS: InputLine - One line from the input acpidump file
218 * OutputData - Where the converted data is returned
220 * RETURN: The number of bytes actually converted
222 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
224 ******************************************************************************/
229 unsigned char *OutputData)
237 /* Terminate the input line at the end of the actual data (for sscanf) */
239 End = strstr (InputLine + 2, " ");
242 return (0); /* Don't understand the format */
247 * Convert one line of table data, of the form:
248 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
251 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
253 BytesConverted = sscanf (InputLine,
254 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
255 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
256 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
257 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
258 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
260 /* Pack converted data into a byte array */
262 for (i = 0; i < BytesConverted; i++)
264 OutputData[i] = (unsigned char) Converted[i];
267 return ((size_t) BytesConverted);
271 /******************************************************************************
273 * FUNCTION: GetTableHeader
275 * PARAMETERS: InputFile - Handle for the input acpidump file
276 * OutputData - Where the table header is returned
278 * RETURN: The actual number of bytes converted
280 * DESCRIPTION: Extract and convert an ACPI table header
282 ******************************************************************************/
287 unsigned char *OutputData)
289 size_t BytesConverted;
290 size_t TotalConverted = 0;
291 char Buffer[BUFFER_SIZE];
295 /* Get the full 36 byte header, requires 3 lines */
297 for (i = 0; i < 3; i++)
299 if (!fgets (Buffer, BUFFER_SIZE, InputFile))
301 return (TotalConverted);
304 BytesConverted = ConvertLine (Buffer, OutputData);
305 TotalConverted += BytesConverted;
308 if (BytesConverted != 16)
310 return (TotalConverted);
314 return (TotalConverted);
318 /******************************************************************************
320 * FUNCTION: CountTableInstances
322 * PARAMETERS: InputPathname - Filename for acpidump file
323 * Signature - Requested signature to count
325 * RETURN: The number of instances of the signature
327 * DESCRIPTION: Count the instances of tables with the given signature within
328 * the input acpidump file.
330 ******************************************************************************/
333 CountTableInstances (
337 char Buffer[BUFFER_SIZE];
339 unsigned int Instances = 0;
342 InputFile = fopen (InputPathname, "rt");
345 printf ("Could not open %s\n", InputPathname);
349 /* Count the number of instances of this signature */
351 while (fgets (Buffer, BUFFER_SIZE, InputFile))
353 /* Ignore empty lines and lines that start with a space */
355 if ((Buffer[0] == ' ') ||
361 NormalizeSignature (Buffer);
362 if (!strncmp (Buffer, Signature, 4))
373 /******************************************************************************
375 * FUNCTION: GetNextInstance
377 * PARAMETERS: InputPathname - Filename for acpidump file
378 * Signature - Requested ACPI signature
380 * RETURN: The next instance number for this signature. Zero if this
381 * is the first instance of this signature.
383 * DESCRIPTION: Get the next instance number of the specified table. If this
384 * is the first instance of the table, create a new instance
385 * block. Note: only SSDT and PSDT tables can have multiple
388 ******************************************************************************/
395 struct TableInfo *Info;
401 if (*(unsigned int *) Signature == Info->Signature)
411 /* Signature not found, create new table info block */
413 Info = malloc (sizeof (struct TableInfo));
416 printf ("Could not allocate memory\n");
420 Info->Signature = *(unsigned int *) Signature;
421 Info->Instances = CountTableInstances (InputPathname, Signature);
422 Info->NextInstance = 1;
423 Info->Next = ListHead;
427 if (Info->Instances > 1)
429 return (Info->NextInstance++);
436 /******************************************************************************
438 * FUNCTION: ExtractTables
440 * PARAMETERS: InputPathname - Filename for acpidump file
441 * Signature - Requested ACPI signature to extract.
442 * NULL means extract ALL tables.
443 * MinimumInstances - Min instances that are acceptable
447 * DESCRIPTION: Convert text ACPI tables to binary
449 ******************************************************************************/
455 unsigned int MinimumInstances)
457 char Buffer[BUFFER_SIZE];
459 FILE *OutputFile = NULL;
461 size_t TotalBytesWritten = 0;
462 size_t BytesConverted;
463 unsigned int State = FIND_HEADER;
464 unsigned int FoundTable = 0;
465 unsigned int Instances = 0;
466 unsigned int ThisInstance;
467 char ThisSignature[4];
471 /* Open input in text mode, output is in binary mode */
473 InputFile = fopen (InputPathname, "rt");
476 printf ("Could not open %s\n", InputPathname);
482 /* Are there enough instances of the table to continue? */
484 NormalizeSignature (Signature);
486 Instances = CountTableInstances (InputPathname, Signature);
487 if (Instances < MinimumInstances)
489 printf ("Table %s was not found in %s\n", Signature, InputPathname);
500 /* Convert all instances of the table to binary */
502 while (fgets (Buffer, BUFFER_SIZE, InputFile))
508 /* Ignore lines that are too short to be header lines */
510 if (strlen (Buffer) < HEADER_LINE_LENGTH)
515 /* Ignore empty lines and lines that start with a space */
517 if ((Buffer[0] == ' ') ||
523 /* Ignore lines that are not of the form "ABCD @ " */
525 if ((Buffer[4] != ' ') ||
526 (Buffer[5] != '@') ||
532 NormalizeSignature (Buffer);
533 strncpy (ThisSignature, Buffer, 4);
537 /* Ignore signatures that don't match */
539 if (strncmp (ThisSignature, Signature, 4))
546 * Get the instance number for this signature. Only the
547 * SSDT and PSDT tables can have multiple instances.
549 ThisInstance = GetNextInstance (InputPathname, ThisSignature);
551 /* Build an output filename and create/open the output file */
553 if (ThisInstance > 0)
555 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
559 sprintf (Filename, "%4.4s.dat", ThisSignature);
562 OutputFile = fopen (Filename, "w+b");
565 printf ("Could not open %s\n", Filename);
570 State = EXTRACT_DATA;
571 TotalBytesWritten = 0;
577 /* Empty line or non-data line terminates the data */
579 if ((Buffer[0] == '\n') ||
586 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
587 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
591 /* Convert the ascii data (one line of text) to binary */
593 BytesConverted = ConvertLine (Buffer, Data);
595 /* Write the binary data */
597 BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
598 if (BytesWritten != BytesConverted)
600 printf ("Write error on %s\n", Filename);
607 TotalBytesWritten += BytesConverted;
618 printf ("Table %s was not found in %s\n", Signature, InputPathname);
627 if (State == EXTRACT_DATA)
629 /* Received an EOF while extracting data */
631 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
632 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
641 /******************************************************************************
643 * FUNCTION: ListTables
645 * PARAMETERS: InputPathname - Filename for acpidump file
649 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
650 * perform an actual extraction of the tables.
652 ******************************************************************************/
659 char Buffer[BUFFER_SIZE];
661 unsigned char Header[48];
663 ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
666 /* Open input in text mode, output is in binary mode */
668 InputFile = fopen (InputPathname, "rt");
671 printf ("Could not open %s\n", InputPathname);
675 /* Dump the headers for all tables found in the input file */
677 printf ("\nSignature Length Revision OemId OemTableId"
678 " OemRevision CompilerId CompilerRevision\n\n");
680 while (fgets (Buffer, BUFFER_SIZE, InputFile))
682 /* Ignore empty lines and lines that start with a space */
684 if ((Buffer[0] == ' ') ||
690 /* Get the 36 byte header and display the fields */
692 HeaderSize = GetTableHeader (InputFile, Header);
698 /* RSDP has an oddball signature and header */
700 if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
702 CheckAscii ((char *) &Header[9], 6);
703 printf ("%8.4s \"%6.6s\"\n", "RSDP", &Header[9]);
708 /* Minimum size for table with standard header */
715 /* Signature and Table length */
718 printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
720 /* FACS has only signature and length */
722 if (!strncmp (TableHeader->Signature, "FACS", 4))
728 /* OEM IDs and Compiler IDs */
730 CheckAscii (TableHeader->OemId, 6);
731 CheckAscii (TableHeader->OemTableId, 8);
732 CheckAscii (TableHeader->AslCompilerId, 4);
734 printf (" %2.2X \"%6.6s\" \"%8.8s\" %8.8X \"%4.4s\" %8.8X\n",
735 TableHeader->Revision, TableHeader->OemId,
736 TableHeader->OemTableId, TableHeader->OemRevision,
737 TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
740 printf ("\nFound %u ACPI tables [%8.8X]\n", TableCount, VERSION);
746 /******************************************************************************
750 * DESCRIPTION: C main function
752 ******************************************************************************/
768 if (argv[1][0] == '-')
780 /* Extract all tables found */
782 return (ExtractTables (argv[2], NULL, 0));
786 /* List tables only, do not extract */
788 return (ListTables (argv[2]));
792 /* Extract only tables with this signature */
794 return (ExtractTables (argv[2], &argv[1][2], 1));
803 * Default output is the DSDT and all SSDTs. One DSDT is required,
804 * any SSDTs are optional.
806 Status = ExtractTables (argv[1], "DSDT", 1);
812 Status = ExtractTables (argv[1], "SSDT", 0);