No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / intel-public / acpica / dist / tools / acpixtract / acpixtract.c
blob324db12c92fac42ca05b6d6c1d971e3c5623ca3c
2 /******************************************************************************
4 * Module Name: acpixtract - convert ascii ACPI tables to binary
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
15 * 2. License
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
41 * 3. Conditions
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <string.h>
120 #include <ctype.h>
122 #define VERSION 0x20060324
124 #define FIND_HEADER 0
125 #define EXTRACT_DATA 1
126 #define BUFFER_SIZE 256
128 char Filename[16];
129 unsigned char Data[16];
131 /* Local prototypes */
133 void
134 CheckAscii (
135 unsigned char *Name,
136 int Count);
138 void
139 NormalizeSignature (
140 char *Signature);
142 unsigned int
143 GetNextInstance (
144 char *InputPathname,
145 char *Signature);
148 ExtractTables (
149 char *InputPathname,
150 char *Signature,
151 unsigned int MinimumInstances);
153 size_t
154 GetTableHeader (
155 FILE *InputFile,
156 unsigned char *OutputData);
158 unsigned int
159 CountTableInstances (
160 char *InputPathname,
161 char *Signature);
164 ListTables (
165 char *InputPathname);
167 size_t
168 ConvertLine (
169 char *InputLine,
170 unsigned char *OutputData);
172 void
173 DisplayUsage (
174 void);
177 struct TableInfo
179 unsigned int Signature;
180 unsigned int Instances;
181 unsigned int NextInstance;
182 struct TableInfo *Next;
185 struct TableInfo *ListHead = NULL;
188 /******************************************************************************
190 * FUNCTION: DisplayUsage
192 * DESCRIPTION: Usage message
194 ******************************************************************************/
196 void
197 DisplayUsage (
198 void)
201 printf ("Usage: acpixtract [option] <InputFile>\n");
202 printf ("\nExtract binary ACPI tables from text acpidump output\n");
203 printf ("Default invocation extracts all DSDTs and SSDTs\n");
204 printf ("Version %8.8X\n\n", VERSION);
205 printf ("Options:\n");
206 printf (" -a Extract all tables, not just DSDT/SSDT\n");
207 printf (" -l List table summaries, do not extract\n");
208 printf (" -s<Signature> Extract all tables named <Signature>\n");
209 printf ("\n");
213 /*******************************************************************************
215 * FUNCTION: CheckAscii
217 * PARAMETERS: Name - Ascii string, at least as long as Count
218 * Count - Number of characters to check
220 * RETURN: None
222 * DESCRIPTION: Ensure that the requested number of characters are printable
223 * Ascii characters. Sets non-printable and null chars to <space>.
225 ******************************************************************************/
227 void
228 CheckAscii (
229 unsigned char *Name,
230 int Count)
232 int i;
235 for (i = 0; i < Count; i++)
237 if (!Name[i] || !isprint (Name[i]))
239 Name[i] = ' ';
245 /*******************************************************************************
247 * FUNCTION: NormalizeSignature
249 * PARAMETERS: Name - Ascii string
251 * RETURN: None
253 * DESCRIPTION: Change "RSD PTR" to "RSDP"
255 ******************************************************************************/
257 void
258 NormalizeSignature (
259 char *Signature)
262 if (!strncmp (Signature, "RSD ", 4))
264 Signature[3] = 'P';
269 /******************************************************************************
271 * FUNCTION: ConvertLine
273 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
275 ******************************************************************************/
277 size_t
278 ConvertLine (
279 char *InputLine,
280 unsigned char *OutputData)
282 char *End;
283 int BytesConverted;
284 int Converted[16];
285 int i;
288 /* Terminate the input line at the end of the actual data (for sscanf) */
290 End = strstr (InputLine + 2, " ");
291 if (!End)
293 return 0; /* Don't understand the format */
295 *End = 0;
298 * Convert one line of table data, of the form:
299 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
301 * Example:
302 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
304 BytesConverted = sscanf (InputLine,
305 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
306 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
307 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
308 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
309 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
311 /* Pack converted data into a byte array */
313 for (i = 0; i < BytesConverted; i++)
315 OutputData[i] = (unsigned char) Converted[i];
318 return ((size_t) BytesConverted);
322 /******************************************************************************
324 * FUNCTION: GetTableHeader
326 * DESCRIPTION: Extract and convert a table heaader
328 ******************************************************************************/
330 size_t
331 GetTableHeader (
332 FILE *InputFile,
333 unsigned char *OutputData)
335 size_t BytesConverted;
336 size_t TotalConverted = 0;
337 char Buffer[BUFFER_SIZE];
338 int i;
341 /* Get the full 36 byte header, requires 3 lines */
343 for (i = 0; i < 3; i++)
345 if (!fgets (Buffer, BUFFER_SIZE, InputFile))
347 return TotalConverted;
350 BytesConverted = ConvertLine (Buffer, OutputData);
351 TotalConverted += BytesConverted;
352 OutputData += 16;
354 if (BytesConverted != 16)
356 return TotalConverted;
360 return TotalConverted;
364 /******************************************************************************
366 * FUNCTION: CountTableInstances
368 * DESCRIPTION: Count the instances of table <Signature> within the input file
370 ******************************************************************************/
372 unsigned int
373 CountTableInstances (
374 char *InputPathname,
375 char *Signature)
377 char Buffer[BUFFER_SIZE];
378 FILE *InputFile;
379 unsigned int Instances = 0;
382 InputFile = fopen (InputPathname, "rt");
383 if (!InputFile)
385 printf ("Could not open %s\n", InputPathname);
386 return 0;
389 /* Count the number of instances of this signature */
391 while (fgets (Buffer, BUFFER_SIZE, InputFile))
393 /* Ignore empty lines and lines that start with a space */
395 if ((Buffer[0] == ' ') ||
396 (Buffer[0] == '\n'))
398 continue;
401 NormalizeSignature (Buffer);
402 if (!strncmp (Buffer, Signature, 4))
404 Instances++;
408 fclose (InputFile);
409 return Instances;
413 /******************************************************************************
415 * FUNCTION: GetNextInstance
417 * DESCRIPTION:
419 ******************************************************************************/
421 unsigned int
422 GetNextInstance (
423 char *InputPathname,
424 char *Signature)
426 struct TableInfo *Info;
429 Info = ListHead;
430 while (Info)
432 if (*(unsigned int *) Signature == Info->Signature)
434 break;
437 Info = Info->Next;
440 if (!Info)
442 Info = malloc (sizeof (struct TableInfo));
444 Info->Signature = *(unsigned int *) Signature;
445 Info->Instances = CountTableInstances (InputPathname, Signature);
446 Info->NextInstance = 1;
448 Info->Next = ListHead;
449 ListHead = Info;
452 if (Info->Instances > 1)
454 return (Info->NextInstance++);
457 return (0);
461 /******************************************************************************
463 * FUNCTION: ExtractTables
465 * DESCRIPTION: Convert text ACPI tables to binary
467 ******************************************************************************/
470 ExtractTables (
471 char *InputPathname,
472 char *Signature,
473 unsigned int MinimumInstances)
475 char Buffer[BUFFER_SIZE];
476 FILE *InputFile;
477 FILE *OutputFile = NULL;
478 size_t BytesWritten;
479 size_t TotalBytesWritten = 0;
480 size_t BytesConverted;
481 unsigned int State = FIND_HEADER;
482 unsigned int FoundTable = 0;
483 unsigned int Instances = 0;
484 unsigned int ThisInstance;
485 char ThisSignature[4];
488 /* Open input in text mode, output is in binary mode */
490 InputFile = fopen (InputPathname, "rt");
491 if (!InputFile)
493 printf ("Could not open %s\n", InputPathname);
494 return -1;
497 if (Signature)
499 /* Are there enough instances of the table to continue? */
501 NormalizeSignature (Signature);
503 Instances = CountTableInstances (InputPathname, Signature);
504 if (Instances < MinimumInstances)
506 printf ("Table %s was not found in %s\n", Signature, InputPathname);
507 return -1;
510 if (Instances == 0)
512 return 0;
516 /* Convert all instances of the table to binary */
518 while (fgets (Buffer, BUFFER_SIZE, InputFile))
520 switch (State)
522 case FIND_HEADER:
524 /* Ignore empty lines and lines that start with a space */
526 if ((Buffer[0] == ' ') ||
527 (Buffer[0] == '\n'))
529 continue;
532 NormalizeSignature (Buffer);
533 strncpy (ThisSignature, Buffer, 4);
535 if (Signature)
537 /* Ignore signatures that don't match */
539 if (strncmp (ThisSignature, Signature, 4))
541 continue;
545 /* Get the instance # for this signature */
547 ThisInstance = GetNextInstance (InputPathname, ThisSignature);
549 /* Build an output filename and create/open the output file */
551 if (ThisInstance > 0)
553 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
555 else
557 sprintf (Filename, "%4.4s.dat", ThisSignature);
560 OutputFile = fopen (Filename, "w+b");
561 if (!OutputFile)
563 printf ("Could not open %s\n", Filename);
564 return -1;
567 State = EXTRACT_DATA;
568 TotalBytesWritten = 0;
569 FoundTable = 1;
570 continue;
572 case EXTRACT_DATA:
574 /* Empty line or non-data line terminates the data */
576 if ((Buffer[0] == '\n') ||
577 (Buffer[0] != ' '))
579 fclose (OutputFile);
580 OutputFile = NULL;
581 State = FIND_HEADER;
583 printf ("Acpi table [%4.4s] - % 6d bytes written to %s\n",
584 ThisSignature, TotalBytesWritten, Filename);
585 continue;
588 /* Convert the ascii data (one line of text) to binary */
590 BytesConverted = ConvertLine (Buffer, Data);
592 /* Write the binary data */
594 BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
595 if (BytesWritten != BytesConverted)
597 printf ("Write error on %s\n", Filename);
598 fclose (OutputFile);
599 return -1;
602 TotalBytesWritten += BytesConverted;
603 continue;
605 default:
606 return -1;
610 if (!FoundTable)
612 printf ("Table %s was not found in %s\n", Signature, InputPathname);
615 if (OutputFile)
617 fclose (OutputFile);
618 if (State == EXTRACT_DATA)
620 /* Received an EOF while extracting data */
622 printf ("Acpi table [%4.4s] - % 6d bytes written to %s\n",
623 ThisSignature, TotalBytesWritten, Filename);
627 fclose (InputFile);
628 return 0;
632 /******************************************************************************
634 * FUNCTION: ListTables
636 * DESCRIPTION: Display info for all ACPI tables found in input
638 ******************************************************************************/
641 ListTables (
642 char *InputPathname)
644 FILE *InputFile;
645 char Buffer[BUFFER_SIZE];
646 size_t HeaderSize;
647 unsigned char Header[48];
648 int TableCount = 0;
651 /* Open input in text mode, output is in binary mode */
653 InputFile = fopen (InputPathname, "rt");
654 if (!InputFile)
656 printf ("Could not open %s\n", InputPathname);
657 return -1;
660 printf ("\nSignature Length OemId OemTableId OemRevision CompilerId CompilerRevision\n\n");
662 while (fgets (Buffer, BUFFER_SIZE, InputFile))
664 /* Ignore empty lines and lines that start with a space */
666 if ((Buffer[0] == ' ') ||
667 (Buffer[0] == '\n'))
669 continue;
672 /* Get the 36 byte header and display the fields */
674 HeaderSize = GetTableHeader (InputFile, Header);
675 if (HeaderSize < 16)
677 continue;
680 /* RSDP has an oddball signature and header */
682 if (!strncmp ((char *) Header, "RSD PTR ", 8))
684 CheckAscii (&Header[9], 6);
685 printf ("%8.4s \"%6.6s\"\n", "RSDP", &Header[9]);
686 TableCount++;
687 continue;
690 /* Minimum size */
692 if (HeaderSize < 36)
694 continue;
697 /* Signature and Table length */
699 TableCount++;
700 printf ("%8.4s % 7d", Header, *(int *) &Header[4]);
702 /* FACS has only signature and length */
704 if (!strncmp ((char *) Header, "FACS", 4))
706 printf ("\n");
707 continue;
710 /* OEM IDs and Compiler IDs */
712 CheckAscii (&Header[10], 6);
713 CheckAscii (&Header[16], 8);
714 CheckAscii (&Header[28], 4);
716 printf (" \"%6.6s\" \"%8.8s\" %8.8X \"%4.4s\" %8.8X\n",
717 &Header[10], &Header[16], *(int *) &Header[24],
718 &Header[28], *(int *) &Header[32]);
721 printf ("\nFound %d ACPI tables [%8.8X]\n", TableCount, VERSION);
722 fclose (InputFile);
723 return 0;
727 /******************************************************************************
729 * FUNCTION: main
731 * DESCRIPTION: C main function
733 ******************************************************************************/
736 main (
737 int argc,
738 char *argv[])
740 int Status;
743 if (argc < 2)
745 DisplayUsage ();
746 return 0;
749 if (argv[1][0] == '-')
751 if (argc < 3)
753 DisplayUsage ();
754 return 0;
757 switch (argv[1][1])
759 case 'a':
760 return (ExtractTables (argv[2], NULL, 0));
762 case 'l':
763 return (ListTables (argv[2]));
765 case 's':
766 return (ExtractTables (argv[2], &argv[1][2], 1));
768 default:
769 DisplayUsage ();
770 return 0;
775 * Default output is the DSDT and all SSDTs. One DSDT is required,
776 * any SSDTs are optional.
778 Status = ExtractTables (argv[1], "DSDT", 1);
779 if (Status)
781 return Status;
784 Status = ExtractTables (argv[1], "SSDT", 0);
785 return (Status);