1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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.
44 #include "acpixtract.h"
47 /* Local prototypes */
54 /******************************************************************************
56 * FUNCTION: AxExtractTables
58 * PARAMETERS: InputPathname - Filename for input acpidump file
59 * Signature - Requested ACPI signature to extract.
60 * NULL means extract ALL tables.
61 * MinimumInstances - Min instances that are acceptable
65 * DESCRIPTION: Convert text ACPI tables to binary
67 ******************************************************************************/
73 unsigned int MinimumInstances
)
76 FILE *OutputFile
= NULL
;
77 unsigned int BytesConverted
;
78 unsigned int ThisTableBytesWritten
= 0;
79 unsigned int FoundTable
= 0;
80 unsigned int Instances
= 0;
81 unsigned int ThisInstance
;
82 char ThisSignature
[5];
83 char UpperSignature
[5];
85 unsigned int State
= AX_STATE_FIND_HEADER
;
88 /* Open input in text mode, output is in binary mode */
90 InputFile
= fopen (InputPathname
, "rt");
93 printf ("Could not open input file %s\n", InputPathname
);
97 if (!AxIsFileAscii (InputFile
))
105 strncpy (UpperSignature
, Signature
, 4);
106 UpperSignature
[4] = 0;
107 AcpiUtStrupr (UpperSignature
);
109 /* Are there enough instances of the table to continue? */
111 AxNormalizeSignature (UpperSignature
);
113 Instances
= AxCountTableInstances (InputPathname
, UpperSignature
);
114 if (Instances
< MinimumInstances
)
116 printf ("Table [%s] was not found in %s\n",
117 UpperSignature
, InputPathname
);
129 /* Convert all instances of the table to binary */
131 while (fgets (Gbl_LineBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
135 case AX_STATE_FIND_HEADER
:
137 if (!AxIsDataBlockHeader ())
142 ACPI_MOVE_NAME (ThisSignature
, Gbl_LineBuffer
);
145 /* Ignore signatures that don't match */
147 if (!ACPI_COMPARE_NAME (ThisSignature
, UpperSignature
))
154 * Get the instance number for this signature. Only the
155 * SSDT and PSDT tables can have multiple instances.
157 ThisInstance
= AxGetNextInstance (InputPathname
, ThisSignature
);
159 /* Build an output filename and create/open the output file */
161 if (ThisInstance
> 0)
163 /* Add instance number to the output filename */
165 sprintf (Gbl_OutputFilename
, "%4.4s%u.dat",
166 ThisSignature
, ThisInstance
);
170 sprintf (Gbl_OutputFilename
, "%4.4s.dat",
174 AcpiUtStrlwr (Gbl_OutputFilename
);
175 OutputFile
= fopen (Gbl_OutputFilename
, "w+b");
178 printf ("Could not open output file %s\n",
185 * Toss this block header of the form "<sig> @ <addr>" line
186 * and move on to the actual data block
190 ThisTableBytesWritten
= 0;
191 State
= AX_STATE_EXTRACT_DATA
;
194 case AX_STATE_EXTRACT_DATA
:
196 /* Empty line or non-data line terminates the data block */
198 BytesConverted
= AxProcessOneTextLine (
199 OutputFile
, ThisSignature
, ThisTableBytesWritten
);
200 switch (BytesConverted
)
204 State
= AX_STATE_FIND_HEADER
; /* No more data block lines */
209 goto CleanupAndExit
; /* There was a write error */
211 default: /* Normal case, get next line */
213 ThisTableBytesWritten
+= BytesConverted
;
226 printf ("No ACPI tables were found in %s\n", InputPathname
);
232 if (State
== AX_STATE_EXTRACT_DATA
)
234 /* Received an input file EOF while extracting data */
236 printf (AX_TABLE_INFO_FORMAT
,
237 ThisSignature
, ThisTableBytesWritten
, Gbl_OutputFilename
);
240 if (Gbl_TableCount
> 1)
242 printf ("\n%u binary ACPI tables extracted\n",
256 /******************************************************************************
258 * FUNCTION: AxExtractToMultiAmlFile
260 * PARAMETERS: InputPathname - Filename for input acpidump file
264 * DESCRIPTION: Convert all DSDT/SSDT tables to binary and append them all
265 * into a single output file. Used to simplify the loading of
266 * multiple/many SSDTs into a utility like acpiexec -- instead
267 * of creating many separate output files.
269 ******************************************************************************/
272 AxExtractToMultiAmlFile (
278 unsigned int TotalBytesWritten
= 0;
279 unsigned int ThisTableBytesWritten
= 0;
280 unsigned int BytesConverted
;
281 char ThisSignature
[4];
282 unsigned int State
= AX_STATE_FIND_HEADER
;
285 strcpy (Gbl_OutputFilename
, AX_MULTI_TABLE_FILENAME
);
287 /* Open the input file in text mode */
289 InputFile
= fopen (InputPathname
, "rt");
292 printf ("Could not open input file %s\n", InputPathname
);
296 if (!AxIsFileAscii (InputFile
))
302 /* Open the output file in binary mode */
304 OutputFile
= fopen (Gbl_OutputFilename
, "w+b");
307 printf ("Could not open output file %s\n", Gbl_OutputFilename
);
312 /* Convert the DSDT and all SSDTs to binary */
314 while (fgets (Gbl_LineBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
318 case AX_STATE_FIND_HEADER
:
320 if (!AxIsDataBlockHeader ())
325 ACPI_MOVE_NAME (ThisSignature
, Gbl_LineBuffer
);
327 /* Only want DSDT and SSDTs */
329 if (!ACPI_COMPARE_NAME (ThisSignature
, ACPI_SIG_DSDT
) &&
330 !ACPI_COMPARE_NAME (ThisSignature
, ACPI_SIG_SSDT
))
336 * Toss this block header of the form "<sig> @ <addr>" line
337 * and move on to the actual data block
340 ThisTableBytesWritten
= 0;
341 State
= AX_STATE_EXTRACT_DATA
;
344 case AX_STATE_EXTRACT_DATA
:
346 /* Empty line or non-data line terminates the data block */
348 BytesConverted
= AxProcessOneTextLine (
349 OutputFile
, ThisSignature
, ThisTableBytesWritten
);
350 switch (BytesConverted
)
354 State
= AX_STATE_FIND_HEADER
; /* No more data block lines */
359 goto CleanupAndExit
; /* There was a write error */
361 default: /* Normal case, get next line */
363 ThisTableBytesWritten
+= BytesConverted
;
364 TotalBytesWritten
+= BytesConverted
;
378 if (State
== AX_STATE_EXTRACT_DATA
)
380 /* Received an input file EOF or error while writing data */
382 printf (AX_TABLE_INFO_FORMAT
,
383 ThisSignature
, ThisTableBytesWritten
, Gbl_OutputFilename
);
386 printf ("\n%u binary ACPI tables extracted and written to %s (%u bytes)\n",
387 Gbl_TableCount
, Gbl_OutputFilename
, TotalBytesWritten
);
395 /******************************************************************************
397 * FUNCTION: AxListTables
399 * PARAMETERS: InputPathname - Filename for acpidump file
403 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
404 * perform an actual extraction of the tables.
406 ******************************************************************************/
414 unsigned char Header
[48];
415 ACPI_TABLE_HEADER
*TableHeader
= (ACPI_TABLE_HEADER
*) (void *) Header
;
418 /* Open input in text mode, output is in binary mode */
420 InputFile
= fopen (InputPathname
, "rt");
423 printf ("Could not open input file %s\n", InputPathname
);
427 if (!AxIsFileAscii (InputFile
))
433 /* Dump the headers for all tables found in the input file */
435 printf ("\nSignature Length Revision OemId OemTableId"
436 " OemRevision CompilerId CompilerRevision\n\n");
438 while (fgets (Gbl_LineBuffer
, AX_LINE_BUFFER_SIZE
, InputFile
))
440 /* Ignore empty lines and lines that start with a space */
442 if (AxIsEmptyLine (Gbl_LineBuffer
) ||
443 (Gbl_LineBuffer
[0] == ' '))
448 /* Get the 36 byte header and display the fields */
450 HeaderSize
= AxGetTableHeader (InputFile
, Header
);
456 /* RSDP has an oddball signature and header */
458 if (!strncmp (TableHeader
->Signature
, "RSD PTR ", 8))
460 AxCheckAscii ((char *) &Header
[9], 6);
461 printf ("%7.4s \"%6.6s\"\n", "RSDP",
467 /* Minimum size for table with standard header */
469 if (HeaderSize
< sizeof (ACPI_TABLE_HEADER
))
474 if (!AcpiUtValidNameseg (TableHeader
->Signature
))
479 /* Signature and Table length */
482 printf ("%7.4s 0x%8.8X", TableHeader
->Signature
,
483 TableHeader
->Length
);
485 /* FACS has only signature and length */
487 if (ACPI_COMPARE_NAME (TableHeader
->Signature
, "FACS"))
493 /* OEM IDs and Compiler IDs */
495 AxCheckAscii (TableHeader
->OemId
, 6);
496 AxCheckAscii (TableHeader
->OemTableId
, 8);
497 AxCheckAscii (TableHeader
->AslCompilerId
, 4);
500 " 0x%2.2X \"%6.6s\" \"%8.8s\" 0x%8.8X"
501 " \"%4.4s\" 0x%8.8X\n",
502 TableHeader
->Revision
, TableHeader
->OemId
,
503 TableHeader
->OemTableId
, TableHeader
->OemRevision
,
504 TableHeader
->AslCompilerId
, TableHeader
->AslCompilerRevision
);
507 printf ("\nFound %u ACPI tables in %s\n", Gbl_TableCount
, InputPathname
);
513 /*******************************************************************************
515 * FUNCTION: AxIsFileAscii
517 * PARAMETERS: Handle - To open input file
519 * RETURN: TRUE if file is entirely ASCII and printable
521 * DESCRIPTION: Verify that the input file is entirely ASCII.
523 ******************************************************************************/
532 /* Read the entire file */
534 while (fread (&Byte
, 1, 1, Handle
) == 1)
536 /* Check for an ASCII character */
538 if (!ACPI_IS_ASCII (Byte
))
543 /* Ensure character is either printable or a "space" char */
545 else if (!isprint (Byte
) && !isspace (Byte
))
551 /* File is OK (100% ASCII) */
553 fseek (Handle
, 0, SEEK_SET
);
558 printf ("File is binary (contains non-text or non-ascii characters)\n");
559 fseek (Handle
, 0, SEEK_SET
);