1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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
);
102 typedef struct AxTableInfo
105 unsigned int Instances
;
106 unsigned int NextInstance
;
107 struct AxTableInfo
*Next
;
111 /* Extraction states */
113 #define AX_STATE_FIND_HEADER 0
114 #define AX_STATE_EXTRACT_DATA 1
116 /* Miscellaneous constants */
118 #define AX_LINE_BUFFER_SIZE 256
119 #define AX_MIN_TABLE_NAME_LENGTH 6 /* strlen ("DSDT @") */
122 static AX_TABLE_INFO
*AxTableListHead
= NULL
;
123 static char Filename
[16];
124 static unsigned char Data
[16];
125 static char LineBuffer
[AX_LINE_BUFFER_SIZE
];
126 static char HeaderBuffer
[AX_LINE_BUFFER_SIZE
];
127 static char InstanceBuffer
[AX_LINE_BUFFER_SIZE
];
130 /*******************************************************************************
134 * PARAMETERS: String - Ascii string
138 * DESCRIPTION: String lowercase function.
140 ******************************************************************************/
149 *String
= (char) tolower ((int) *String
);
155 /*******************************************************************************
157 * FUNCTION: AxCheckAscii
159 * PARAMETERS: Name - Ascii string, at least as long as Count
160 * Count - Number of characters to check
164 * DESCRIPTION: Ensure that the requested number of characters are printable
165 * Ascii characters. Sets non-printable and null chars to <space>.
167 ******************************************************************************/
177 for (i
= 0; i
< Count
; i
++)
179 if (!Name
[i
] || !isprint ((int) Name
[i
]))
187 /******************************************************************************
189 * FUNCTION: AxIsEmptyLine
191 * PARAMETERS: Buffer - Line from input file
193 * RETURN: TRUE if line is empty (zero or more blanks only)
195 * DESCRIPTION: Determine if an input line is empty.
197 ******************************************************************************/
204 /* Skip all spaces */
206 while (*Buffer
== ' ')
211 /* If end-of-line, this line is empty */
222 /*******************************************************************************
224 * FUNCTION: AxNormalizeSignature
226 * PARAMETERS: Name - Ascii string containing an ACPI signature
230 * DESCRIPTION: Change "RSD PTR" to "RSDP"
232 ******************************************************************************/
235 AxNormalizeSignature (
239 if (!strncmp (Signature
, "RSD ", 4))
246 /******************************************************************************
248 * FUNCTION: AxConvertLine
250 * PARAMETERS: InputLine - One line from the input acpidump file
251 * OutputData - Where the converted data is returned
253 * RETURN: The number of bytes actually converted
255 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
257 ******************************************************************************/
262 unsigned char *OutputData
)
270 /* Terminate the input line at the end of the actual data (for sscanf) */
272 End
= strstr (InputLine
+ 2, " ");
275 return (0); /* Don't understand the format */
280 * Convert one line of table data, of the form:
281 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
284 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
286 BytesConverted
= sscanf (InputLine
,
287 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
288 &Converted
[0], &Converted
[1], &Converted
[2], &Converted
[3],
289 &Converted
[4], &Converted
[5], &Converted
[6], &Converted
[7],
290 &Converted
[8], &Converted
[9], &Converted
[10], &Converted
[11],
291 &Converted
[12], &Converted
[13], &Converted
[14], &Converted
[15]);
293 /* Pack converted data into a byte array */
295 for (i
= 0; i
< BytesConverted
; i
++)
297 OutputData
[i
] = (unsigned char) Converted
[i
];
300 return ((size_t) BytesConverted
);
304 /******************************************************************************
306 * FUNCTION: AxGetTableHeader
308 * PARAMETERS: InputFile - Handle for the input acpidump file
309 * OutputData - Where the table header is returned
311 * RETURN: The actual number of bytes converted
313 * DESCRIPTION: Extract and convert an ACPI table header
315 ******************************************************************************/
320 unsigned char *OutputData
)
322 size_t BytesConverted
;
323 size_t TotalConverted
= 0;
327 /* Get the full 36 byte ACPI table header, requires 3 input text lines */
329 for (i
= 0; i
< 3; i
++)
331 if (!fgets (HeaderBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
333 return (TotalConverted
);
336 BytesConverted
= AxConvertLine (HeaderBuffer
, OutputData
);
337 TotalConverted
+= BytesConverted
;
340 if (BytesConverted
!= 16)
342 return (TotalConverted
);
346 return (TotalConverted
);
350 /******************************************************************************
352 * FUNCTION: AxCountTableInstances
354 * PARAMETERS: InputPathname - Filename for acpidump file
355 * Signature - Requested signature to count
357 * RETURN: The number of instances of the signature
359 * DESCRIPTION: Count the instances of tables with the given signature within
360 * the input acpidump file.
362 ******************************************************************************/
365 AxCountTableInstances (
370 unsigned int Instances
= 0;
373 InputFile
= fopen (InputPathname
, "rt");
376 printf ("Could not open file %s\n", InputPathname
);
380 /* Count the number of instances of this signature */
382 while (fgets (InstanceBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
384 /* Ignore empty lines and lines that start with a space */
386 if (AxIsEmptyLine (InstanceBuffer
) ||
387 (InstanceBuffer
[0] == ' '))
392 AxNormalizeSignature (InstanceBuffer
);
393 if (ACPI_COMPARE_NAME (InstanceBuffer
, Signature
))
404 /******************************************************************************
406 * FUNCTION: AxGetNextInstance
408 * PARAMETERS: InputPathname - Filename for acpidump file
409 * Signature - Requested ACPI signature
411 * RETURN: The next instance number for this signature. Zero if this
412 * is the first instance of this signature.
414 * DESCRIPTION: Get the next instance number of the specified table. If this
415 * is the first instance of the table, create a new instance
416 * block. Note: only SSDT and PSDT tables can have multiple
419 ******************************************************************************/
429 Info
= AxTableListHead
;
432 if (*(UINT32
*) Signature
== Info
->Signature
)
442 /* Signature not found, create new table info block */
444 Info
= malloc (sizeof (AX_TABLE_INFO
));
447 printf ("Could not allocate memory\n");
451 Info
->Signature
= *(UINT32
*) Signature
;
452 Info
->Instances
= AxCountTableInstances (InputPathname
, Signature
);
453 Info
->NextInstance
= 1;
454 Info
->Next
= AxTableListHead
;
455 AxTableListHead
= Info
;
458 if (Info
->Instances
> 1)
460 return (Info
->NextInstance
++);
467 /******************************************************************************
469 * FUNCTION: AxExtractTables
471 * PARAMETERS: InputPathname - Filename for acpidump file
472 * Signature - Requested ACPI signature to extract.
473 * NULL means extract ALL tables.
474 * MinimumInstances - Min instances that are acceptable
478 * DESCRIPTION: Convert text ACPI tables to binary
480 ******************************************************************************/
486 unsigned int MinimumInstances
)
489 FILE *OutputFile
= NULL
;
491 size_t TotalBytesWritten
= 0;
492 size_t BytesConverted
;
493 unsigned int State
= AX_STATE_FIND_HEADER
;
494 unsigned int FoundTable
= 0;
495 unsigned int Instances
= 0;
496 unsigned int ThisInstance
;
497 char ThisSignature
[4];
501 /* Open input in text mode, output is in binary mode */
503 InputFile
= fopen (InputPathname
, "rt");
506 printf ("Could not open file %s\n", InputPathname
);
512 /* Are there enough instances of the table to continue? */
514 AxNormalizeSignature (Signature
);
516 Instances
= AxCountTableInstances (InputPathname
, Signature
);
517 if (Instances
< MinimumInstances
)
519 printf ("Table %s was not found in %s\n", Signature
, InputPathname
);
530 /* Convert all instances of the table to binary */
532 while (fgets (LineBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
536 case AX_STATE_FIND_HEADER
:
538 /* Ignore lines that are too short to be header lines */
540 if (strlen (LineBuffer
) < AX_MIN_TABLE_NAME_LENGTH
)
545 /* Ignore empty lines and lines that start with a space */
547 if (AxIsEmptyLine (LineBuffer
) ||
548 (LineBuffer
[0] == ' '))
554 * Ignore lines that are not of the form <sig> @ <addr>.
555 * Examples of lines that must be supported:
562 if (!strstr (LineBuffer
, " @ "))
567 AxNormalizeSignature (LineBuffer
);
568 ACPI_MOVE_NAME (ThisSignature
, LineBuffer
);
572 /* Ignore signatures that don't match */
574 if (!ACPI_COMPARE_NAME (ThisSignature
, Signature
))
581 * Get the instance number for this signature. Only the
582 * SSDT and PSDT tables can have multiple instances.
584 ThisInstance
= AxGetNextInstance (InputPathname
, ThisSignature
);
586 /* Build an output filename and create/open the output file */
588 if (ThisInstance
> 0)
590 sprintf (Filename
, "%4.4s%u.dat", ThisSignature
, ThisInstance
);
594 sprintf (Filename
, "%4.4s.dat", ThisSignature
);
598 OutputFile
= fopen (Filename
, "w+b");
601 printf ("Could not open file %s\n", Filename
);
606 State
= AX_STATE_EXTRACT_DATA
;
607 TotalBytesWritten
= 0;
611 case AX_STATE_EXTRACT_DATA
:
613 /* Empty line or non-data line terminates the data */
615 if (AxIsEmptyLine (LineBuffer
) ||
616 (LineBuffer
[0] != ' '))
620 State
= AX_STATE_FIND_HEADER
;
622 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
623 ThisSignature
, (unsigned int) TotalBytesWritten
, Filename
);
627 /* Convert the ascii data (one line of text) to binary */
629 BytesConverted
= AxConvertLine (LineBuffer
, Data
);
631 /* Write the binary data */
633 BytesWritten
= fwrite (Data
, 1, BytesConverted
, OutputFile
);
634 if (BytesWritten
!= BytesConverted
)
636 printf ("Error when writing file %s\n", Filename
);
643 TotalBytesWritten
+= BytesConverted
;
655 printf ("Table %s was not found in %s\n", Signature
, InputPathname
);
664 if (State
== AX_STATE_EXTRACT_DATA
)
666 /* Received an EOF while extracting data */
668 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
669 ThisSignature
, (unsigned int) TotalBytesWritten
, Filename
);
678 /******************************************************************************
680 * FUNCTION: AxListTables
682 * PARAMETERS: InputPathname - Filename for acpidump file
686 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
687 * perform an actual extraction of the tables.
689 ******************************************************************************/
697 unsigned char Header
[48];
699 ACPI_TABLE_HEADER
*TableHeader
= (ACPI_TABLE_HEADER
*) (void *) Header
;
702 /* Open input in text mode, output is in binary mode */
704 InputFile
= fopen (InputPathname
, "rt");
707 printf ("Could not open file %s\n", InputPathname
);
711 /* Dump the headers for all tables found in the input file */
713 printf ("\nSignature Length Revision OemId OemTableId"
714 " OemRevision CompilerId CompilerRevision\n\n");
716 while (fgets (LineBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
718 /* Ignore empty lines and lines that start with a space */
720 if (AxIsEmptyLine (LineBuffer
) ||
721 (LineBuffer
[0] == ' '))
726 /* Get the 36 byte header and display the fields */
728 HeaderSize
= AxGetTableHeader (InputFile
, Header
);
734 /* RSDP has an oddball signature and header */
736 if (!strncmp (TableHeader
->Signature
, "RSD PTR ", 8))
738 AxCheckAscii ((char *) &Header
[9], 6);
739 printf ("%7.4s \"%6.6s\"\n", "RSDP", &Header
[9]);
744 /* Minimum size for table with standard header */
746 if (HeaderSize
< sizeof (ACPI_TABLE_HEADER
))
751 /* Signature and Table length */
754 printf ("%7.4s 0x%8.8X", TableHeader
->Signature
, TableHeader
->Length
);
756 /* FACS has only signature and length */
758 if (ACPI_COMPARE_NAME (TableHeader
->Signature
, "FACS"))
764 /* OEM IDs and Compiler IDs */
766 AxCheckAscii (TableHeader
->OemId
, 6);
767 AxCheckAscii (TableHeader
->OemTableId
, 8);
768 AxCheckAscii (TableHeader
->AslCompilerId
, 4);
770 printf (" 0x%2.2X \"%6.6s\" \"%8.8s\" 0x%8.8X \"%4.4s\" 0x%8.8X\n",
771 TableHeader
->Revision
, TableHeader
->OemId
,
772 TableHeader
->OemTableId
, TableHeader
->OemRevision
,
773 TableHeader
->AslCompilerId
, TableHeader
->AslCompilerRevision
);
776 printf ("\nFound %u ACPI tables\n", TableCount
);