acpica.library: Initial import of Intel ACPICA, v20131115
[AROS.git] / arch / all-pc / acpica / source / tools / acpixtract / acpixtract.c
blobc06e3757dea39bb19b1c5beabfeb084e3a0d4545
1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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 "acpi.h"
45 #include "accommon.h"
46 #include "acapps.h"
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <ctype.h>
53 /* Local prototypes */
55 static void
56 AxStrlwr (
57 char *String);
59 static void
60 AxCheckAscii (
61 char *Name,
62 int Count);
64 static void
65 AxNormalizeSignature (
66 char *Signature);
68 static unsigned int
69 AxGetNextInstance (
70 char *InputPathname,
71 char *Signature);
73 static size_t
74 AxGetTableHeader (
75 FILE *InputFile,
76 unsigned char *OutputData);
78 static unsigned int
79 AxCountTableInstances (
80 char *InputPathname,
81 char *Signature);
83 int
84 AxExtractTables (
85 char *InputPathname,
86 char *Signature,
87 unsigned int MinimumInstances);
89 int
90 AxListTables (
91 char *InputPathname);
93 static size_t
94 AxConvertLine (
95 char *InputLine,
96 unsigned char *OutputData);
98 static int
99 AxIsEmptyLine (
100 char *Buffer);
102 typedef struct AxTableInfo
104 UINT32 Signature;
105 unsigned int Instances;
106 unsigned int NextInstance;
107 struct AxTableInfo *Next;
109 } AX_TABLE_INFO;
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 /*******************************************************************************
132 * FUNCTION: AxStrlwr
134 * PARAMETERS: String - Ascii string
136 * RETURN: None
138 * DESCRIPTION: String lowercase function.
140 ******************************************************************************/
142 static void
143 AxStrlwr (
144 char *String)
147 while (*String)
149 *String = (char) tolower ((int) *String);
150 String++;
155 /*******************************************************************************
157 * FUNCTION: AxCheckAscii
159 * PARAMETERS: Name - Ascii string, at least as long as Count
160 * Count - Number of characters to check
162 * RETURN: None
164 * DESCRIPTION: Ensure that the requested number of characters are printable
165 * Ascii characters. Sets non-printable and null chars to <space>.
167 ******************************************************************************/
169 static void
170 AxCheckAscii (
171 char *Name,
172 int Count)
174 int i;
177 for (i = 0; i < Count; i++)
179 if (!Name[i] || !isprint ((int) Name[i]))
181 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 ******************************************************************************/
199 static int
200 AxIsEmptyLine (
201 char *Buffer)
204 /* Skip all spaces */
206 while (*Buffer == ' ')
208 Buffer++;
211 /* If end-of-line, this line is empty */
213 if (*Buffer == '\n')
215 return (1);
218 return (0);
222 /*******************************************************************************
224 * FUNCTION: AxNormalizeSignature
226 * PARAMETERS: Name - Ascii string containing an ACPI signature
228 * RETURN: None
230 * DESCRIPTION: Change "RSD PTR" to "RSDP"
232 ******************************************************************************/
234 static void
235 AxNormalizeSignature (
236 char *Signature)
239 if (!strncmp (Signature, "RSD ", 4))
241 Signature[3] = 'P';
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 ******************************************************************************/
259 static size_t
260 AxConvertLine (
261 char *InputLine,
262 unsigned char *OutputData)
264 char *End;
265 int BytesConverted;
266 int Converted[16];
267 int i;
270 /* Terminate the input line at the end of the actual data (for sscanf) */
272 End = strstr (InputLine + 2, " ");
273 if (!End)
275 return (0); /* Don't understand the format */
277 *End = 0;
280 * Convert one line of table data, of the form:
281 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
283 * Example:
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 ******************************************************************************/
317 static size_t
318 AxGetTableHeader (
319 FILE *InputFile,
320 unsigned char *OutputData)
322 size_t BytesConverted;
323 size_t TotalConverted = 0;
324 int i;
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;
338 OutputData += 16;
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 ******************************************************************************/
364 static unsigned int
365 AxCountTableInstances (
366 char *InputPathname,
367 char *Signature)
369 FILE *InputFile;
370 unsigned int Instances = 0;
373 InputFile = fopen (InputPathname, "rt");
374 if (!InputFile)
376 printf ("Could not open file %s\n", InputPathname);
377 return (0);
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] == ' '))
389 continue;
392 AxNormalizeSignature (InstanceBuffer);
393 if (ACPI_COMPARE_NAME (InstanceBuffer, Signature))
395 Instances++;
399 fclose (InputFile);
400 return (Instances);
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
417 * instances.
419 ******************************************************************************/
421 static unsigned int
422 AxGetNextInstance (
423 char *InputPathname,
424 char *Signature)
426 AX_TABLE_INFO *Info;
429 Info = AxTableListHead;
430 while (Info)
432 if (*(UINT32 *) Signature == Info->Signature)
434 break;
437 Info = Info->Next;
440 if (!Info)
442 /* Signature not found, create new table info block */
444 Info = malloc (sizeof (AX_TABLE_INFO));
445 if (!Info)
447 printf ("Could not allocate memory\n");
448 exit (0);
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++);
463 return (0);
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
476 * RETURN: Status
478 * DESCRIPTION: Convert text ACPI tables to binary
480 ******************************************************************************/
483 AxExtractTables (
484 char *InputPathname,
485 char *Signature,
486 unsigned int MinimumInstances)
488 FILE *InputFile;
489 FILE *OutputFile = NULL;
490 size_t BytesWritten;
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];
498 int Status = 0;
501 /* Open input in text mode, output is in binary mode */
503 InputFile = fopen (InputPathname, "rt");
504 if (!InputFile)
506 printf ("Could not open file %s\n", InputPathname);
507 return (-1);
510 if (Signature)
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);
520 Status = -1;
521 goto CleanupAndExit;
524 if (Instances == 0)
526 goto CleanupAndExit;
530 /* Convert all instances of the table to binary */
532 while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
534 switch (State)
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)
542 continue;
545 /* Ignore empty lines and lines that start with a space */
547 if (AxIsEmptyLine (LineBuffer) ||
548 (LineBuffer[0] == ' '))
550 continue;
554 * Ignore lines that are not of the form <sig> @ <addr>.
555 * Examples of lines that must be supported:
557 * DSDT @ 0x737e4000
558 * XSDT @ 0x737f2fff
559 * RSD PTR @ 0xf6cd0
560 * SSDT @ (nil)
562 if (!strstr (LineBuffer, " @ "))
564 continue;
567 AxNormalizeSignature (LineBuffer);
568 ACPI_MOVE_NAME (ThisSignature, LineBuffer);
570 if (Signature)
572 /* Ignore signatures that don't match */
574 if (!ACPI_COMPARE_NAME (ThisSignature, Signature))
576 continue;
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);
592 else
594 sprintf (Filename, "%4.4s.dat", ThisSignature);
597 AxStrlwr (Filename);
598 OutputFile = fopen (Filename, "w+b");
599 if (!OutputFile)
601 printf ("Could not open file %s\n", Filename);
602 Status = -1;
603 goto CleanupAndExit;
606 State = AX_STATE_EXTRACT_DATA;
607 TotalBytesWritten = 0;
608 FoundTable = 1;
609 continue;
611 case AX_STATE_EXTRACT_DATA:
613 /* Empty line or non-data line terminates the data */
615 if (AxIsEmptyLine (LineBuffer) ||
616 (LineBuffer[0] != ' '))
618 fclose (OutputFile);
619 OutputFile = NULL;
620 State = AX_STATE_FIND_HEADER;
622 printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
623 ThisSignature, (unsigned int) TotalBytesWritten, Filename);
624 continue;
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);
637 fclose (OutputFile);
638 OutputFile = NULL;
639 Status = -1;
640 goto CleanupAndExit;
643 TotalBytesWritten += BytesConverted;
644 continue;
646 default:
648 Status = -1;
649 goto CleanupAndExit;
653 if (!FoundTable)
655 printf ("Table %s was not found in %s\n", Signature, InputPathname);
659 CleanupAndExit:
661 if (OutputFile)
663 fclose (OutputFile);
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);
673 fclose (InputFile);
674 return (Status);
678 /******************************************************************************
680 * FUNCTION: AxListTables
682 * PARAMETERS: InputPathname - Filename for acpidump file
684 * RETURN: Status
686 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
687 * perform an actual extraction of the tables.
689 ******************************************************************************/
692 AxListTables (
693 char *InputPathname)
695 FILE *InputFile;
696 size_t HeaderSize;
697 unsigned char Header[48];
698 int TableCount = 0;
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");
705 if (!InputFile)
707 printf ("Could not open file %s\n", InputPathname);
708 return (-1);
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] == ' '))
723 continue;
726 /* Get the 36 byte header and display the fields */
728 HeaderSize = AxGetTableHeader (InputFile, Header);
729 if (HeaderSize < 16)
731 continue;
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]);
740 TableCount++;
741 continue;
744 /* Minimum size for table with standard header */
746 if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
748 continue;
751 /* Signature and Table length */
753 TableCount++;
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"))
760 printf ("\n");
761 continue;
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);
777 fclose (InputFile);
778 return (0);