i386/identcpu.c: Add VIA Nano support
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / tools / acpixtract / acpixtract.c
blob0d4af40fb4e7fe31ea820bfa5eed6820044cb7d4
2 /******************************************************************************
4 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 * $Revision: 1.5 $
7 *****************************************************************************/
9 /******************************************************************************
11 * 1. Copyright Notice
13 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
14 * All rights reserved.
16 * 2. License
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
42 * 3. Conditions
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
118 #include <stdio.h>
119 #include <stdlib.h>
120 #include <string.h>
121 #include <ctype.h>
123 #define VERSION 0x20060324
125 #define FIND_HEADER 0
126 #define EXTRACT_DATA 1
127 #define BUFFER_SIZE 256
129 char Filename[16];
130 unsigned char Data[16];
132 /* Local prototypes */
134 void
135 CheckAscii (
136 unsigned char *Name,
137 int Count);
139 void
140 NormalizeSignature (
141 char *Signature);
143 unsigned int
144 GetNextInstance (
145 char *InputPathname,
146 char *Signature);
149 ExtractTables (
150 char *InputPathname,
151 char *Signature,
152 unsigned int MinimumInstances);
154 size_t
155 GetTableHeader (
156 FILE *InputFile,
157 unsigned char *OutputData);
159 unsigned int
160 CountTableInstances (
161 char *InputPathname,
162 char *Signature);
165 ListTables (
166 char *InputPathname);
168 size_t
169 ConvertLine (
170 char *InputLine,
171 unsigned char *OutputData);
173 void
174 DisplayUsage (
175 void);
178 struct TableInfo
180 unsigned int Signature;
181 unsigned int Instances;
182 unsigned int NextInstance;
183 struct TableInfo *Next;
186 struct TableInfo *ListHead = NULL;
189 /******************************************************************************
191 * FUNCTION: DisplayUsage
193 * DESCRIPTION: Usage message
195 ******************************************************************************/
197 void
198 DisplayUsage (
199 void)
202 printf ("Usage: acpixtract [option] <InputFile>\n");
203 printf ("\nExtract binary ACPI tables from text acpidump output\n");
204 printf ("Default invocation extracts all DSDTs and SSDTs\n");
205 printf ("Version %8.8X\n\n", VERSION);
206 printf ("Options:\n");
207 printf (" -a Extract all tables, not just DSDT/SSDT\n");
208 printf (" -l List table summaries, do not extract\n");
209 printf (" -s<Signature> Extract all tables named <Signature>\n");
210 printf ("\n");
214 /*******************************************************************************
216 * FUNCTION: CheckAscii
218 * PARAMETERS: Name - Ascii string, at least as long as Count
219 * Count - Number of characters to check
221 * RETURN: None
223 * DESCRIPTION: Ensure that the requested number of characters are printable
224 * Ascii characters. Sets non-printable and null chars to <space>.
226 ******************************************************************************/
228 void
229 CheckAscii (
230 unsigned char *Name,
231 int Count)
233 int i;
236 for (i = 0; i < Count; i++)
238 if (!Name[i] || !isprint (Name[i]))
240 Name[i] = ' ';
246 /*******************************************************************************
248 * FUNCTION: NormalizeSignature
250 * PARAMETERS: Name - Ascii string
252 * RETURN: None
254 * DESCRIPTION: Change "RSD PTR" to "RSDP"
256 ******************************************************************************/
258 void
259 NormalizeSignature (
260 char *Signature)
263 if (!strncmp (Signature, "RSD ", 4))
265 Signature[3] = 'P';
270 /******************************************************************************
272 * FUNCTION: ConvertLine
274 * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
276 ******************************************************************************/
278 size_t
279 ConvertLine (
280 char *InputLine,
281 unsigned char *OutputData)
283 char *End;
284 int BytesConverted;
285 int Converted[16];
286 int i;
289 /* Terminate the input line at the end of the actual data (for sscanf) */
291 End = strstr (InputLine + 2, " ");
292 if (!End)
294 return 0; /* Don't understand the format */
296 *End = 0;
299 * Convert one line of table data, of the form:
300 * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
302 * Example:
303 * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
305 BytesConverted = sscanf (InputLine,
306 "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
307 &Converted[0], &Converted[1], &Converted[2], &Converted[3],
308 &Converted[4], &Converted[5], &Converted[6], &Converted[7],
309 &Converted[8], &Converted[9], &Converted[10], &Converted[11],
310 &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
312 /* Pack converted data into a byte array */
314 for (i = 0; i < BytesConverted; i++)
316 OutputData[i] = (unsigned char) Converted[i];
319 return ((size_t) BytesConverted);
323 /******************************************************************************
325 * FUNCTION: GetTableHeader
327 * DESCRIPTION: Extract and convert a table heaader
329 ******************************************************************************/
331 size_t
332 GetTableHeader (
333 FILE *InputFile,
334 unsigned char *OutputData)
336 size_t BytesConverted;
337 size_t TotalConverted = 0;
338 char Buffer[BUFFER_SIZE];
339 int i;
342 /* Get the full 36 byte header, requires 3 lines */
344 for (i = 0; i < 3; i++)
346 if (!fgets (Buffer, BUFFER_SIZE, InputFile))
348 return TotalConverted;
351 BytesConverted = ConvertLine (Buffer, OutputData);
352 TotalConverted += BytesConverted;
353 OutputData += 16;
355 if (BytesConverted != 16)
357 return TotalConverted;
361 return TotalConverted;
365 /******************************************************************************
367 * FUNCTION: CountTableInstances
369 * DESCRIPTION: Count the instances of table <Signature> within the input file
371 ******************************************************************************/
373 unsigned int
374 CountTableInstances (
375 char *InputPathname,
376 char *Signature)
378 char Buffer[BUFFER_SIZE];
379 FILE *InputFile;
380 unsigned int Instances = 0;
383 InputFile = fopen (InputPathname, "rt");
384 if (!InputFile)
386 printf ("Could not open %s\n", InputPathname);
387 return 0;
390 /* Count the number of instances of this signature */
392 while (fgets (Buffer, BUFFER_SIZE, InputFile))
394 /* Ignore empty lines and lines that start with a space */
396 if ((Buffer[0] == ' ') ||
397 (Buffer[0] == '\n'))
399 continue;
402 NormalizeSignature (Buffer);
403 if (!strncmp (Buffer, Signature, 4))
405 Instances++;
409 fclose (InputFile);
410 return Instances;
414 /******************************************************************************
416 * FUNCTION: GetNextInstance
418 * DESCRIPTION:
420 ******************************************************************************/
422 unsigned int
423 GetNextInstance (
424 char *InputPathname,
425 char *Signature)
427 struct TableInfo *Info;
430 Info = ListHead;
431 while (Info)
433 if (*(unsigned int *) Signature == Info->Signature)
435 break;
438 Info = Info->Next;
441 if (!Info)
443 Info = malloc (sizeof (struct TableInfo));
445 Info->Signature = *(unsigned int *) Signature;
446 Info->Instances = CountTableInstances (InputPathname, Signature);
447 Info->NextInstance = 1;
449 Info->Next = ListHead;
450 ListHead = Info;
453 if (Info->Instances > 1)
455 return (Info->NextInstance++);
458 return (0);
462 /******************************************************************************
464 * FUNCTION: ExtractTables
466 * DESCRIPTION: Convert text ACPI tables to binary
468 ******************************************************************************/
471 ExtractTables (
472 char *InputPathname,
473 char *Signature,
474 unsigned int MinimumInstances)
476 char Buffer[BUFFER_SIZE];
477 FILE *InputFile;
478 FILE *OutputFile = NULL;
479 size_t BytesWritten;
480 size_t TotalBytesWritten = 0;
481 size_t BytesConverted;
482 unsigned int State = FIND_HEADER;
483 unsigned int FoundTable = 0;
484 unsigned int Instances = 0;
485 unsigned int ThisInstance;
486 char ThisSignature[4];
489 /* Open input in text mode, output is in binary mode */
491 InputFile = fopen (InputPathname, "rt");
492 if (!InputFile)
494 printf ("Could not open %s\n", InputPathname);
495 return -1;
498 if (Signature)
500 /* Are there enough instances of the table to continue? */
502 NormalizeSignature (Signature);
504 Instances = CountTableInstances (InputPathname, Signature);
505 if (Instances < MinimumInstances)
507 printf ("Table %s was not found in %s\n", Signature, InputPathname);
508 return -1;
511 if (Instances == 0)
513 return 0;
517 /* Convert all instances of the table to binary */
519 while (fgets (Buffer, BUFFER_SIZE, InputFile))
521 switch (State)
523 case FIND_HEADER:
525 /* Ignore empty lines and lines that start with a space */
527 if ((Buffer[0] == ' ') ||
528 (Buffer[0] == '\n'))
530 continue;
533 NormalizeSignature (Buffer);
534 strncpy (ThisSignature, Buffer, 4);
536 if (Signature)
538 /* Ignore signatures that don't match */
540 if (strncmp (ThisSignature, Signature, 4))
542 continue;
546 /* Get the instance # for this signature */
548 ThisInstance = GetNextInstance (InputPathname, ThisSignature);
550 /* Build an output filename and create/open the output file */
552 if (ThisInstance > 0)
554 sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
556 else
558 sprintf (Filename, "%4.4s.dat", ThisSignature);
561 OutputFile = fopen (Filename, "w+b");
562 if (!OutputFile)
564 printf ("Could not open %s\n", Filename);
565 return -1;
568 State = EXTRACT_DATA;
569 TotalBytesWritten = 0;
570 FoundTable = 1;
571 continue;
573 case EXTRACT_DATA:
575 /* Empty line or non-data line terminates the data */
577 if ((Buffer[0] == '\n') ||
578 (Buffer[0] != ' '))
580 fclose (OutputFile);
581 OutputFile = NULL;
582 State = FIND_HEADER;
584 printf ("Acpi table [%4.4s] - % 6d bytes written to %s\n",
585 ThisSignature, TotalBytesWritten, Filename);
586 continue;
589 /* Convert the ascii data (one line of text) to binary */
591 BytesConverted = ConvertLine (Buffer, 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);
599 fclose (OutputFile);
600 return -1;
603 TotalBytesWritten += BytesConverted;
604 continue;
606 default:
607 return -1;
611 if (!FoundTable)
613 printf ("Table %s was not found in %s\n", Signature, InputPathname);
616 if (OutputFile)
618 fclose (OutputFile);
621 fclose (InputFile);
622 return 0;
626 /******************************************************************************
628 * FUNCTION: ListTables
630 * DESCRIPTION: Display info for all ACPI tables found in input
632 ******************************************************************************/
635 ListTables (
636 char *InputPathname)
638 FILE *InputFile;
639 char Buffer[BUFFER_SIZE];
640 size_t HeaderSize;
641 unsigned char Header[48];
642 int TableCount = 0;
645 /* Open input in text mode, output is in binary mode */
647 InputFile = fopen (InputPathname, "rt");
648 if (!InputFile)
650 printf ("Could not open %s\n", InputPathname);
651 return -1;
654 printf ("\nSignature Length OemId OemTableId OemRevision CompilerId CompilerRevision\n\n");
656 while (fgets (Buffer, BUFFER_SIZE, InputFile))
658 /* Ignore empty lines and lines that start with a space */
660 if ((Buffer[0] == ' ') ||
661 (Buffer[0] == '\n'))
663 continue;
666 /* Get the 36 byte header and display the fields */
668 HeaderSize = GetTableHeader (InputFile, Header);
669 if (HeaderSize < 16)
671 continue;
674 /* RSDP has an oddball signature and header */
676 if (!strncmp ((char *) Header, "RSD PTR ", 8))
678 CheckAscii (&Header[9], 6);
679 printf ("%8.4s \"%6.6s\"\n", "RSDP", &Header[9]);
680 TableCount++;
681 continue;
684 /* Minimum size */
686 if (HeaderSize < 36)
688 continue;
691 /* Signature and Table length */
693 TableCount++;
694 printf ("%8.4s % 7d", Header, *(int *) &Header[4]);
696 /* FACS has only signature and length */
698 if (!strncmp ((char *) Header, "FACS", 4))
700 printf ("\n");
701 continue;
704 /* OEM IDs and Compiler IDs */
706 CheckAscii (&Header[10], 6);
707 CheckAscii (&Header[16], 8);
708 CheckAscii (&Header[28], 4);
710 printf (" \"%6.6s\" \"%8.8s\" %8.8X \"%4.4s\" %8.8X\n",
711 &Header[10], &Header[16], *(int *) &Header[24],
712 &Header[28], *(int *) &Header[32]);
715 printf ("\nFound %d ACPI tables [%8.8X]\n", TableCount, VERSION);
716 fclose (InputFile);
717 return 0;
721 /******************************************************************************
723 * FUNCTION: main
725 * DESCRIPTION: C main function
727 ******************************************************************************/
730 main (
731 int argc,
732 char *argv[])
734 int Status;
737 if (argc < 2)
739 DisplayUsage ();
740 return 0;
743 if (argv[1][0] == '-')
745 if (argc < 3)
747 DisplayUsage ();
748 return 0;
751 switch (argv[1][1])
753 case 'a':
754 return (ExtractTables (argv[2], NULL, 0));
756 case 'l':
757 return (ListTables (argv[2]));
759 case 's':
760 return (ExtractTables (argv[2], &argv[1][2], 1));
762 default:
763 DisplayUsage ();
764 return 0;
769 * Default output is the DSDT and all SSDTs. One DSDT is required,
770 * any SSDTs are optional.
772 Status = ExtractTables (argv[1], "DSDT", 1);
773 if (Status)
775 return Status;
778 Status = ExtractTables (argv[1], "SSDT", 0);
779 return (Status);