1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
53 /* Local prototypes */
61 AxNormalizeSignature (
72 unsigned char *OutputData);
75 AxCountTableInstances (
83 unsigned int MinimumInstances);
92 unsigned char *OutputData);
98 typedef struct AxTableInfo
101 unsigned int Instances;
102 unsigned int NextInstance;
103 struct AxTableInfo *Next;
107 /* Extraction states */
109 #define AX_STATE_FIND_HEADER 0
110 #define AX_STATE_EXTRACT_DATA 1
112 /* Miscellaneous constants */
114 #define AX_LINE_BUFFER_SIZE 256
115 #define AX_MIN_TABLE_NAME_LENGTH 6 /* strlen ("DSDT @") */
118 static AX_TABLE_INFO *AxTableListHead = NULL;
119 static char Filename[16];
120 static unsigned char Data[16];
121 static char LineBuffer[AX_LINE_BUFFER_SIZE];
122 static char HeaderBuffer[AX_LINE_BUFFER_SIZE];
123 static char InstanceBuffer[AX_LINE_BUFFER_SIZE];
126 /*******************************************************************************
128 * FUNCTION: AxCheckAscii
130 * PARAMETERS: Name - Ascii string, at least as long as Count
131 * Count - Number of characters to check
135 * DESCRIPTION: Ensure that the requested number of characters are printable
136 * Ascii characters. Sets non-printable and null chars to <space>.
138 ******************************************************************************/
148 for (i = 0; i < Count; i++)
150 if (!Name[i] || !isprint ((int) Name[i]))
158 /******************************************************************************
160 * FUNCTION: AxIsEmptyLine
162 * PARAMETERS: Buffer - Line from input file
164 * RETURN: TRUE if line is empty (zero or more blanks only)
166 * DESCRIPTION: Determine if an input line is empty.
168 ******************************************************************************/
175 /* Skip all spaces */
177 while (*Buffer == ' ')
182 /* If end-of-line, this line is empty */
193 /*******************************************************************************
195 * FUNCTION: AxNormalizeSignature
197 * PARAMETERS: Name - Ascii string containing an ACPI signature
201 * DESCRIPTION: Change "RSD PTR" to "RSDP"
203 ******************************************************************************/
206 AxNormalizeSignature (
210 if (!strncmp (Signature, "RSD ", 4))
217 /******************************************************************************
219 * FUNCTION: AxConvertLine
221 * PARAMETERS: InputLine - One line from the input acpidump file
222 * OutputData - Where the converted data is returned
224 * RETURN: The number of bytes actually converted
226 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
228 ******************************************************************************/
233 unsigned char *OutputData)
241 /* Terminate the input line at the end of the actual data (for sscanf) */
243 End = strstr (InputLine + 2, " ");
246 return (0); /* Don't understand the format */
251 * Convert one line of table data, of the form:
252 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
255 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
257 BytesConverted = sscanf (InputLine,
258 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
259 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
260 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
261 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
262 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
264 /* Pack converted data into a byte array */
266 for (i = 0; i < BytesConverted; i++)
268 OutputData[i] = (unsigned char) Converted[i];
271 return ((size_t) BytesConverted);
275 /******************************************************************************
277 * FUNCTION: AxGetTableHeader
279 * PARAMETERS: InputFile - Handle for the input acpidump file
280 * OutputData - Where the table header is returned
282 * RETURN: The actual number of bytes converted
284 * DESCRIPTION: Extract and convert an ACPI table header
286 ******************************************************************************/
291 unsigned char *OutputData)
293 size_t BytesConverted;
294 size_t TotalConverted = 0;
298 /* Get the full 36 byte ACPI table header, requires 3 input text lines */
300 for (i = 0; i < 3; i++)
302 if (!fgets (HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile))
304 return (TotalConverted);
307 BytesConverted = AxConvertLine (HeaderBuffer, OutputData);
308 TotalConverted += BytesConverted;
311 if (BytesConverted != 16)
313 return (TotalConverted);
317 return (TotalConverted);
321 /******************************************************************************
323 * FUNCTION: AxCountTableInstances
325 * PARAMETERS: InputPathname - Filename for acpidump file
326 * Signature - Requested signature to count
328 * RETURN: The number of instances of the signature
330 * DESCRIPTION: Count the instances of tables with the given signature within
331 * the input acpidump file.
333 ******************************************************************************/
336 AxCountTableInstances (
341 unsigned int Instances = 0;
344 InputFile = fopen (InputPathname, "rt");
347 printf ("Could not open file %s\n", InputPathname);
351 /* Count the number of instances of this signature */
353 while (fgets (InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile))
355 /* Ignore empty lines and lines that start with a space */
357 if (AxIsEmptyLine (InstanceBuffer) ||
358 (InstanceBuffer[0] == ' '))
363 AxNormalizeSignature (InstanceBuffer);
364 if (ACPI_COMPARE_NAME (InstanceBuffer, Signature))
375 /******************************************************************************
377 * FUNCTION: AxGetNextInstance
379 * PARAMETERS: InputPathname - Filename for acpidump file
380 * Signature - Requested ACPI signature
382 * RETURN: The next instance number for this signature. Zero if this
383 * is the first instance of this signature.
385 * DESCRIPTION: Get the next instance number of the specified table. If this
386 * is the first instance of the table, create a new instance
387 * block. Note: only SSDT and PSDT tables can have multiple
390 ******************************************************************************/
400 Info = AxTableListHead;
403 if (*(UINT32 *) Signature == Info->Signature)
413 /* Signature not found, create new table info block */
415 Info = malloc (sizeof (AX_TABLE_INFO));
418 printf ("Could not allocate memory\n");
422 Info->Signature = *(UINT32 *) Signature;
423 Info->Instances = AxCountTableInstances (InputPathname, Signature);
424 Info->NextInstance = 1;
425 Info->Next = AxTableListHead;
426 AxTableListHead = Info;
429 if (Info->Instances > 1)
431 return (Info->NextInstance++);
438 /******************************************************************************
440 * FUNCTION: AxExtractTables
442 * PARAMETERS: InputPathname - Filename for acpidump file
443 * Signature - Requested ACPI signature to extract.
444 * NULL means extract ALL tables.
445 * MinimumInstances - Min instances that are acceptable
449 * DESCRIPTION: Convert text ACPI tables to binary
451 ******************************************************************************/
457 unsigned int MinimumInstances)
460 FILE *OutputFile = NULL;
462 size_t TotalBytesWritten = 0;
463 size_t BytesConverted;
464 unsigned int State = AX_STATE_FIND_HEADER;
465 unsigned int FoundTable = 0;
466 unsigned int Instances = 0;
467 unsigned int ThisInstance;
468 char ThisSignature[4];
472 /* Open input in text mode, output is in binary mode */
474 InputFile = fopen (InputPathname, "rt");
477 printf ("Could not open file %s\n", InputPathname);
483 /* Are there enough instances of the table to continue? */
485 AxNormalizeSignature (Signature);
487 Instances = AxCountTableInstances (InputPathname, Signature);
488 if (Instances < MinimumInstances)
490 printf ("Table %s was not found in %s\n", Signature, InputPathname);
501 /* Convert all instances of the table to binary */
503 while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
507 case AX_STATE_FIND_HEADER:
509 /* Ignore lines that are too short to be header lines */
511 if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH)
516 /* Ignore empty lines and lines that start with a space */
518 if (AxIsEmptyLine (LineBuffer) ||
519 (LineBuffer[0] == ' '))
525 * Ignore lines that are not of the form <sig> @ <addr>.
526 * Examples of lines that must be supported:
533 if (!strstr (LineBuffer, " @ "))
538 AxNormalizeSignature (LineBuffer);
539 ACPI_MOVE_NAME (ThisSignature, LineBuffer);
543 /* Ignore signatures that don't match */
545 if (!ACPI_COMPARE_NAME (ThisSignature, Signature))
552 * Get the instance number for this signature. Only the
553 * SSDT and PSDT tables can have multiple instances.
555 ThisInstance = AxGetNextInstance (InputPathname, ThisSignature);
557 /* Build an output filename and create/open the output file */
559 if (ThisInstance > 0)
561 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
565 sprintf (Filename, "%4.4s.dat", ThisSignature);
568 AcpiUtStrlwr (Filename);
569 OutputFile = fopen (Filename, "w+b");
572 printf ("Could not open file %s\n", Filename);
577 State = AX_STATE_EXTRACT_DATA;
578 TotalBytesWritten = 0;
582 case AX_STATE_EXTRACT_DATA:
584 /* Empty line or non-data line terminates the data */
586 if (AxIsEmptyLine (LineBuffer) ||
587 (LineBuffer[0] != ' '))
591 State = AX_STATE_FIND_HEADER;
593 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
594 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
598 /* Convert the ascii data (one line of text) to binary */
600 BytesConverted = AxConvertLine (LineBuffer, Data);
602 /* Write the binary data */
604 BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
605 if (BytesWritten != BytesConverted)
607 printf ("Error when writing file %s\n", Filename);
614 TotalBytesWritten += BytesConverted;
626 printf ("Table %s was not found in %s\n", Signature, InputPathname);
635 if (State == AX_STATE_EXTRACT_DATA)
637 /* Received an EOF while extracting data */
639 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
640 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
649 /******************************************************************************
651 * FUNCTION: AxListTables
653 * PARAMETERS: InputPathname - Filename for acpidump file
657 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
658 * perform an actual extraction of the tables.
660 ******************************************************************************/
668 unsigned char Header[48];
669 unsigned int TableCount = 0;
670 ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
673 /* Open input in text mode, output is in binary mode */
675 InputFile = fopen (InputPathname, "rt");
678 printf ("Could not open file %s\n", InputPathname);
682 /* Dump the headers for all tables found in the input file */
684 printf ("\nSignature Length Revision OemId OemTableId"
685 " OemRevision CompilerId CompilerRevision\n\n");
687 while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
689 /* Ignore empty lines and lines that start with a space */
691 if (AxIsEmptyLine (LineBuffer) ||
692 (LineBuffer[0] == ' '))
697 /* Get the 36 byte header and display the fields */
699 HeaderSize = AxGetTableHeader (InputFile, Header);
705 /* RSDP has an oddball signature and header */
707 if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
709 AxCheckAscii ((char *) &Header[9], 6);
710 printf ("%7.4s \"%6.6s\"\n", "RSDP", &Header[9]);
715 /* Minimum size for table with standard header */
717 if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
722 /* Signature and Table length */
725 printf ("%7.4s 0x%8.8X", TableHeader->Signature, TableHeader->Length);
727 /* FACS has only signature and length */
729 if (ACPI_COMPARE_NAME (TableHeader->Signature, "FACS"))
735 /* OEM IDs and Compiler IDs */
737 AxCheckAscii (TableHeader->OemId, 6);
738 AxCheckAscii (TableHeader->OemTableId, 8);
739 AxCheckAscii (TableHeader->AslCompilerId, 4);
741 printf (" 0x%2.2X \"%6.6s\" \"%8.8s\" 0x%8.8X \"%4.4s\" 0x%8.8X\n",
742 TableHeader->Revision, TableHeader->OemId,
743 TableHeader->OemTableId, TableHeader->OemRevision,
744 TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
747 printf ("\nFound %u ACPI tables\n", TableCount);