1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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 */
65 AxNormalizeSignature (
76 unsigned char *OutputData);
79 AxCountTableInstances (
87 unsigned int MinimumInstances);
96 unsigned char *OutputData);
99 typedef struct AxTableInfo
102 unsigned int Instances;
103 unsigned int NextInstance;
104 struct AxTableInfo *Next;
108 /* Extraction states */
110 #define AX_STATE_FIND_HEADER 0
111 #define AX_STATE_EXTRACT_DATA 1
113 /* Miscellaneous constants */
115 #define AX_LINE_BUFFER_SIZE 256
116 #define AX_MIN_TABLE_NAME_LENGTH 6 /* strlen ("DSDT @") */
119 static AX_TABLE_INFO *AxTableListHead = NULL;
120 static char Filename[16];
121 static unsigned char Data[16];
122 static char LineBuffer[AX_LINE_BUFFER_SIZE];
123 static char HeaderBuffer[AX_LINE_BUFFER_SIZE];
124 static char InstanceBuffer[AX_LINE_BUFFER_SIZE];
127 /*******************************************************************************
131 * PARAMETERS: String - Ascii string
135 * DESCRIPTION: String lowercase function.
137 ******************************************************************************/
146 *String = (char) tolower ((int) *String);
152 /*******************************************************************************
154 * FUNCTION: AxCheckAscii
156 * PARAMETERS: Name - Ascii string, at least as long as Count
157 * Count - Number of characters to check
161 * DESCRIPTION: Ensure that the requested number of characters are printable
162 * Ascii characters. Sets non-printable and null chars to <space>.
164 ******************************************************************************/
174 for (i = 0; i < Count; i++)
176 if (!Name[i] || !isprint ((int) Name[i]))
184 /*******************************************************************************
186 * FUNCTION: AxNormalizeSignature
188 * PARAMETERS: Name - Ascii string containing an ACPI signature
192 * DESCRIPTION: Change "RSD PTR" to "RSDP"
194 ******************************************************************************/
197 AxNormalizeSignature (
201 if (!strncmp (Signature, "RSD ", 4))
208 /******************************************************************************
210 * FUNCTION: AxConvertLine
212 * PARAMETERS: InputLine - One line from the input acpidump file
213 * OutputData - Where the converted data is returned
215 * RETURN: The number of bytes actually converted
217 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
219 ******************************************************************************/
224 unsigned char *OutputData)
232 /* Terminate the input line at the end of the actual data (for sscanf) */
234 End = strstr (InputLine + 2, " ");
237 return (0); /* Don't understand the format */
242 * Convert one line of table data, of the form:
243 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
246 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
248 BytesConverted = sscanf (InputLine,
249 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
250 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
251 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
252 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
253 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
255 /* Pack converted data into a byte array */
257 for (i = 0; i < BytesConverted; i++)
259 OutputData[i] = (unsigned char) Converted[i];
262 return ((size_t) BytesConverted);
266 /******************************************************************************
268 * FUNCTION: AxGetTableHeader
270 * PARAMETERS: InputFile - Handle for the input acpidump file
271 * OutputData - Where the table header is returned
273 * RETURN: The actual number of bytes converted
275 * DESCRIPTION: Extract and convert an ACPI table header
277 ******************************************************************************/
282 unsigned char *OutputData)
284 size_t BytesConverted;
285 size_t TotalConverted = 0;
289 /* Get the full 36 byte ACPI table header, requires 3 input text lines */
291 for (i = 0; i < 3; i++)
293 if (!fgets (HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile))
295 return (TotalConverted);
298 BytesConverted = AxConvertLine (HeaderBuffer, OutputData);
299 TotalConverted += BytesConverted;
302 if (BytesConverted != 16)
304 return (TotalConverted);
308 return (TotalConverted);
312 /******************************************************************************
314 * FUNCTION: AxCountTableInstances
316 * PARAMETERS: InputPathname - Filename for acpidump file
317 * Signature - Requested signature to count
319 * RETURN: The number of instances of the signature
321 * DESCRIPTION: Count the instances of tables with the given signature within
322 * the input acpidump file.
324 ******************************************************************************/
327 AxCountTableInstances (
332 unsigned int Instances = 0;
335 InputFile = fopen (InputPathname, "rt");
338 printf ("Could not open %s\n", InputPathname);
342 /* Count the number of instances of this signature */
344 while (fgets (InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile))
346 /* Ignore empty lines and lines that start with a space */
348 if ((InstanceBuffer[0] == ' ') ||
349 (InstanceBuffer[0] == '\n'))
354 AxNormalizeSignature (InstanceBuffer);
355 if (!strncmp (InstanceBuffer, Signature, 4))
366 /******************************************************************************
368 * FUNCTION: AxGetNextInstance
370 * PARAMETERS: InputPathname - Filename for acpidump file
371 * Signature - Requested ACPI signature
373 * RETURN: The next instance number for this signature. Zero if this
374 * is the first instance of this signature.
376 * DESCRIPTION: Get the next instance number of the specified table. If this
377 * is the first instance of the table, create a new instance
378 * block. Note: only SSDT and PSDT tables can have multiple
381 ******************************************************************************/
391 Info = AxTableListHead;
394 if (*(UINT32 *) Signature == Info->Signature)
404 /* Signature not found, create new table info block */
406 Info = malloc (sizeof (AX_TABLE_INFO));
409 printf ("Could not allocate memory\n");
413 Info->Signature = *(UINT32 *) Signature;
414 Info->Instances = AxCountTableInstances (InputPathname, Signature);
415 Info->NextInstance = 1;
416 Info->Next = AxTableListHead;
417 AxTableListHead = Info;
420 if (Info->Instances > 1)
422 return (Info->NextInstance++);
429 /******************************************************************************
431 * FUNCTION: AxExtractTables
433 * PARAMETERS: InputPathname - Filename for acpidump file
434 * Signature - Requested ACPI signature to extract.
435 * NULL means extract ALL tables.
436 * MinimumInstances - Min instances that are acceptable
440 * DESCRIPTION: Convert text ACPI tables to binary
442 ******************************************************************************/
448 unsigned int MinimumInstances)
451 FILE *OutputFile = NULL;
453 size_t TotalBytesWritten = 0;
454 size_t BytesConverted;
455 unsigned int State = AX_STATE_FIND_HEADER;
456 unsigned int FoundTable = 0;
457 unsigned int Instances = 0;
458 unsigned int ThisInstance;
459 char ThisSignature[4];
463 /* Open input in text mode, output is in binary mode */
465 InputFile = fopen (InputPathname, "rt");
468 printf ("Could not open %s\n", InputPathname);
474 /* Are there enough instances of the table to continue? */
476 AxNormalizeSignature (Signature);
478 Instances = AxCountTableInstances (InputPathname, Signature);
479 if (Instances < MinimumInstances)
481 printf ("Table %s was not found in %s\n", Signature, InputPathname);
492 /* Convert all instances of the table to binary */
494 while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
498 case AX_STATE_FIND_HEADER:
500 /* Ignore lines that are too short to be header lines */
502 if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH)
507 /* Ignore empty lines and lines that start with a space */
509 if ((LineBuffer[0] == ' ') ||
510 (LineBuffer[0] == '\n'))
516 * Ignore lines that are not of the form <sig> @ <addr>.
517 * Examples of lines that must be supported:
524 if (!strstr (LineBuffer, " @ "))
529 AxNormalizeSignature (LineBuffer);
530 strncpy (ThisSignature, LineBuffer, 4);
534 /* Ignore signatures that don't match */
536 if (strncmp (ThisSignature, Signature, 4))
543 * Get the instance number for this signature. Only the
544 * SSDT and PSDT tables can have multiple instances.
546 ThisInstance = AxGetNextInstance (InputPathname, ThisSignature);
548 /* Build an output filename and create/open the output file */
550 if (ThisInstance > 0)
552 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
556 sprintf (Filename, "%4.4s.dat", ThisSignature);
560 OutputFile = fopen (Filename, "w+b");
563 printf ("Could not open %s\n", Filename);
568 State = AX_STATE_EXTRACT_DATA;
569 TotalBytesWritten = 0;
573 case AX_STATE_EXTRACT_DATA:
575 /* Empty line or non-data line terminates the data */
577 if ((LineBuffer[0] == '\n') ||
578 (LineBuffer[0] != ' '))
582 State = AX_STATE_FIND_HEADER;
584 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
585 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
589 /* Convert the ascii data (one line of text) to binary */
591 BytesConverted = AxConvertLine (LineBuffer, Data);
593 /* Write the binary data */
595 BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
596 if (BytesWritten != BytesConverted)
598 printf ("Write error on %s\n", Filename);
605 TotalBytesWritten += BytesConverted;
616 printf ("Table %s was not found in %s\n", Signature, InputPathname);
625 if (State == AX_STATE_EXTRACT_DATA)
627 /* Received an EOF while extracting data */
629 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
630 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
639 /******************************************************************************
641 * FUNCTION: AxListTables
643 * PARAMETERS: InputPathname - Filename for acpidump file
647 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
648 * perform an actual extraction of the tables.
650 ******************************************************************************/
658 unsigned char Header[48];
660 ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
663 /* Open input in text mode, output is in binary mode */
665 InputFile = fopen (InputPathname, "rt");
668 printf ("Could not open %s\n", InputPathname);
672 /* Dump the headers for all tables found in the input file */
674 printf ("\nSignature Length Revision OemId OemTableId"
675 " OemRevision CompilerId CompilerRevision\n\n");
677 while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
679 /* Ignore empty lines and lines that start with a space */
681 if ((LineBuffer[0] == ' ') ||
682 (LineBuffer[0] == '\n'))
687 /* Get the 36 byte header and display the fields */
689 HeaderSize = AxGetTableHeader (InputFile, Header);
695 /* RSDP has an oddball signature and header */
697 if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
699 AxCheckAscii ((char *) &Header[9], 6);
700 printf ("%8.4s \"%6.6s\"\n", "RSDP", &Header[9]);
705 /* Minimum size for table with standard header */
707 if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
712 /* Signature and Table length */
715 printf ("%8.4s % 7d", TableHeader->Signature, TableHeader->Length);
717 /* FACS has only signature and length */
719 if (!strncmp (TableHeader->Signature, "FACS", 4))
725 /* OEM IDs and Compiler IDs */
727 AxCheckAscii (TableHeader->OemId, 6);
728 AxCheckAscii (TableHeader->OemTableId, 8);
729 AxCheckAscii (TableHeader->AslCompilerId, 4);
731 printf (" %2.2X \"%6.6s\" \"%8.8s\" %8.8X \"%4.4s\" %8.8X\n",
732 TableHeader->Revision, TableHeader->OemId,
733 TableHeader->OemTableId, TableHeader->OemRevision,
734 TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
737 printf ("\nFound %u ACPI tables\n", TableCount);