8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / acpi / acpixtract / acpixtract.c
blob1573ceeff065ff3f3c33fd642b04584e1f5b1c3e
1 /******************************************************************************
3 * Module Name: acpixtract - convert ascii ACPI tables to binary
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, 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 "acpixtract.h"
47 /* Local prototypes */
49 static BOOLEAN
50 AxIsFileAscii (
51 FILE *Handle);
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
63 * RETURN: Status
65 * DESCRIPTION: Convert text ACPI tables to binary
67 ******************************************************************************/
69 int
70 AxExtractTables (
71 char *InputPathname,
72 char *Signature,
73 unsigned int MinimumInstances)
75 FILE *InputFile;
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];
84 int Status = 0;
85 unsigned int State = AX_STATE_FIND_HEADER;
88 /* Open input in text mode, output is in binary mode */
90 InputFile = fopen (InputPathname, "rt");
91 if (!InputFile)
93 printf ("Could not open input file %s\n", InputPathname);
94 return (-1);
97 if (!AxIsFileAscii (InputFile))
99 fclose (InputFile);
100 return (-1);
103 if (Signature)
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);
118 fclose (InputFile);
119 return (-1);
122 if (Instances == 0)
124 fclose (InputFile);
125 return (-1);
129 /* Convert all instances of the table to binary */
131 while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
133 switch (State)
135 case AX_STATE_FIND_HEADER:
137 if (!AxIsDataBlockHeader ())
139 continue;
142 ACPI_MOVE_NAME (ThisSignature, Gbl_LineBuffer);
143 if (Signature)
145 /* Ignore signatures that don't match */
147 if (!ACPI_COMPARE_NAME (ThisSignature, UpperSignature))
149 continue;
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);
168 else
170 sprintf (Gbl_OutputFilename, "%4.4s.dat",
171 ThisSignature);
174 AcpiUtStrlwr (Gbl_OutputFilename);
175 OutputFile = fopen (Gbl_OutputFilename, "w+b");
176 if (!OutputFile)
178 printf ("Could not open output file %s\n",
179 Gbl_OutputFilename);
180 fclose (InputFile);
181 return (-1);
185 * Toss this block header of the form "<sig> @ <addr>" line
186 * and move on to the actual data block
188 Gbl_TableCount++;
189 FoundTable = 1;
190 ThisTableBytesWritten = 0;
191 State = AX_STATE_EXTRACT_DATA;
192 continue;
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)
202 case 0:
204 State = AX_STATE_FIND_HEADER; /* No more data block lines */
205 continue;
207 case -1:
209 goto CleanupAndExit; /* There was a write error */
211 default: /* Normal case, get next line */
213 ThisTableBytesWritten += BytesConverted;
214 continue;
217 default:
219 Status = -1;
220 goto CleanupAndExit;
224 if (!FoundTable)
226 printf ("No ACPI tables were found in %s\n", InputPathname);
230 CleanupAndExit:
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",
243 Gbl_TableCount);
246 if (OutputFile)
248 fclose (OutputFile);
251 fclose (InputFile);
252 return (Status);
256 /******************************************************************************
258 * FUNCTION: AxExtractToMultiAmlFile
260 * PARAMETERS: InputPathname - Filename for input acpidump file
262 * RETURN: Status
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 (
273 char *InputPathname)
275 FILE *InputFile;
276 FILE *OutputFile;
277 int Status = 0;
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");
290 if (!InputFile)
292 printf ("Could not open input file %s\n", InputPathname);
293 return (-1);
296 if (!AxIsFileAscii (InputFile))
298 fclose (InputFile);
299 return (-1);
302 /* Open the output file in binary mode */
304 OutputFile = fopen (Gbl_OutputFilename, "w+b");
305 if (!OutputFile)
307 printf ("Could not open output file %s\n", Gbl_OutputFilename);
308 fclose (InputFile);
309 return (-1);
312 /* Convert the DSDT and all SSDTs to binary */
314 while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
316 switch (State)
318 case AX_STATE_FIND_HEADER:
320 if (!AxIsDataBlockHeader ())
322 continue;
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))
332 continue;
336 * Toss this block header of the form "<sig> @ <addr>" line
337 * and move on to the actual data block
339 Gbl_TableCount++;
340 ThisTableBytesWritten = 0;
341 State = AX_STATE_EXTRACT_DATA;
342 continue;
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)
352 case 0:
354 State = AX_STATE_FIND_HEADER; /* No more data block lines */
355 continue;
357 case -1:
359 goto CleanupAndExit; /* There was a write error */
361 default: /* Normal case, get next line */
363 ThisTableBytesWritten += BytesConverted;
364 TotalBytesWritten += BytesConverted;
365 continue;
368 default:
370 Status = -1;
371 goto CleanupAndExit;
376 CleanupAndExit:
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);
389 fclose (InputFile);
390 fclose (OutputFile);
391 return (Status);
395 /******************************************************************************
397 * FUNCTION: AxListTables
399 * PARAMETERS: InputPathname - Filename for acpidump file
401 * RETURN: Status
403 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
404 * perform an actual extraction of the tables.
406 ******************************************************************************/
409 AxListTables (
410 char *InputPathname)
412 FILE *InputFile;
413 size_t HeaderSize;
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");
421 if (!InputFile)
423 printf ("Could not open input file %s\n", InputPathname);
424 return (-1);
427 if (!AxIsFileAscii (InputFile))
429 fclose (InputFile);
430 return (-1);
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] == ' '))
445 continue;
448 /* Get the 36 byte header and display the fields */
450 HeaderSize = AxGetTableHeader (InputFile, Header);
451 if (HeaderSize < 16)
453 continue;
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",
462 &Header[9]);
463 Gbl_TableCount++;
464 continue;
467 /* Minimum size for table with standard header */
469 if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
471 continue;
474 if (!AcpiUtValidNameseg (TableHeader->Signature))
476 continue;
479 /* Signature and Table length */
481 Gbl_TableCount++;
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"))
489 printf ("\n");
490 continue;
493 /* OEM IDs and Compiler IDs */
495 AxCheckAscii (TableHeader->OemId, 6);
496 AxCheckAscii (TableHeader->OemTableId, 8);
497 AxCheckAscii (TableHeader->AslCompilerId, 4);
499 printf (
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);
508 fclose (InputFile);
509 return (0);
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 ******************************************************************************/
525 static BOOLEAN
526 AxIsFileAscii (
527 FILE *Handle)
529 UINT8 Byte;
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))
540 goto ErrorExit;
543 /* Ensure character is either printable or a "space" char */
545 else if (!isprint (Byte) && !isspace (Byte))
547 goto ErrorExit;
551 /* File is OK (100% ASCII) */
553 fseek (Handle, 0, SEEK_SET);
554 return (TRUE);
556 ErrorExit:
558 printf ("File is binary (contains non-text or non-ascii characters)\n");
559 fseek (Handle, 0, SEEK_SET);
560 return (FALSE);