Debian Codegraph @

source/tools/acpixtract/acpixtract.c

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Module Name: acpixtract - Top level functions to convert ascii/hex
4 * ACPI tables to the original binary tables
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000 - 2026, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45 #include "acpixtract.h"
46
47
48 /******************************************************************************
49 *
50 * FUNCTION: AxExtractTables
51 *
52 * PARAMETERS: InputPathname - Filename for input acpidump file
53 * Signature - Requested ACPI signature to extract.
54 * NULL means extract ALL tables.
55 * MinimumInstances - Min instances that are acceptable
56 *
57 * RETURN: Status
58 *
59 * DESCRIPTION: Convert text ACPI tables to binary
60 *
61 ******************************************************************************/
62
63 int
64 AxExtractTables (
65 char *InputPathname,
66 char *Signature,
67 unsigned int MinimumInstances)
68 {
69 FILE *InputFile;
70 FILE *OutputFile = NULL;
71 int BytesConverted;
72 int ThisTableBytesWritten = 0;
73 unsigned int FoundTable = 0;
74 unsigned int Instances = 0;
75 unsigned int ThisInstance;
76 char ThisSignature[5];
77 char UpperSignature[5];
78 int Status = 0;
79 unsigned int State = AX_STATE_FIND_HEADER;
80
81 memset (UpperSignature, 0, sizeof(UpperSignature));
82
83 /* Open input in text mode, output is in binary mode */
84
85 InputFile = fopen (InputPathname, "r");
86 if (!InputFile)
87 {
88 printf ("Could not open input file %s\n", InputPathname);
89 return (-1);
90 }
91
92 if (!AxIsFileAscii (InputFile))
93 {
94 fclose (InputFile);
95 return (-1);
96 }
97
98 if (Signature)
99 {
100 memcpy (UpperSignature, Signature, ACPI_NAMESEG_SIZE);
101 AcpiUtStrupr (UpperSignature);
102
103 /* Are there enough instances of the table to continue? */
104
105 AxNormalizeSignature (UpperSignature);
106 Instances = AxCountTableInstances (InputPathname, UpperSignature);
107
108 if (Instances < MinimumInstances ||
109 (Instances == 0 && MinimumInstances == AX_OPTIONAL_TABLES))
110 {
111 printf ("Table [%s] was not found in %s\n",
112 UpperSignature, InputPathname);
113 fclose (InputFile);
114 return (0); /* Don't abort */
115 }
116
117 if (Instances == 0)
118 {
119 fclose (InputFile);
120 return (-1);
121 }
122 }
123
124 /* Convert all instances of the table to binary */
125
126 while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
127 {
128 /*
129 * Check up front if we have a header line of the form:
130 * DSDT @ 0xdfffd0c0 (10999 bytes)
131 */
132 if (AX_IS_TABLE_BLOCK_HEADER &&
133 (State == AX_STATE_EXTRACT_DATA))
134 {
135 /* End of previous table, start of new table */
136
137 if (ThisTableBytesWritten)
138 {
139 printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten,
140 ThisTableBytesWritten, Gbl_OutputFilename);
141 }
142 else
143 {
144 Gbl_TableCount--;
145 }
146
147 State = AX_STATE_FIND_HEADER;
148 }
149
150 switch (State)
151 {
152 case AX_STATE_FIND_HEADER:
153
154 if (!AxIsDataBlockHeader ())
155 {
156 continue;
157 }
158
159 ACPI_COPY_NAMESEG (ThisSignature, Gbl_LineBuffer);
160 if (Signature)
161 {
162 /* Ignore signatures that don't match */
163
164 if (!ACPI_COMPARE_NAMESEG (ThisSignature, UpperSignature))
165 {
166 continue;
167 }
168 }
169
170 /*
171 * Get the instance number for this signature. Only the
172 * SSDT and PSDT tables can have multiple instances.
173 */
174 ThisInstance = AxGetNextInstance (InputPathname, ThisSignature);
175
176 /* Build an output filename and create/open the output file */
177
178 if (ThisInstance > 0)
179 {
180 /* Add instance number to the output filename */
181
182 sprintf (Gbl_OutputFilename, "%4.4s%u.dat",
183 ThisSignature, ThisInstance);
184 }
185 else
186 {
187 sprintf (Gbl_OutputFilename, "%4.4s.dat",
188 ThisSignature);
189 }
190
191 AcpiUtStrlwr (Gbl_OutputFilename);
192 OutputFile = fopen (Gbl_OutputFilename, "w+b");
193 if (!OutputFile)
194 {
195 printf ("Could not open output file %s\n",
196 Gbl_OutputFilename);
197 fclose (InputFile);
198 return (-1);
199 }
200
201 /*
202 * Toss this block header of the form "<sig> @ <addr>" line
203 * and move on to the actual data block
204 */
205 Gbl_TableCount++;
206 FoundTable = 1;
207 ThisTableBytesWritten = 0;
208 State = AX_STATE_EXTRACT_DATA;
209 continue;
210
211 case AX_STATE_EXTRACT_DATA:
212
213 if (!AxIsHexDataLine ())
214 {
215 continue; /* Toss any lines that are not raw hex data */
216 }
217
218 /* Empty line or non-data line terminates the data block */
219
220 BytesConverted = AxConvertAndWrite (OutputFile, ThisSignature);
221 switch (BytesConverted)
222 {
223 case 0:
224
225 State = AX_STATE_FIND_HEADER; /* No more data block lines */
226 continue;
227
228 case -1:
229
230 Status = -1;
231 goto CleanupAndExit; /* There was a write error */
232
233 default: /* Normal case, get next line */
234
235 ThisTableBytesWritten += BytesConverted;
236 continue;
237 }
238
239 default:
240
241 Status = -1;
242 goto CleanupAndExit;
243 }
244 }
245
246 if (!FoundTable)
247 {
248 printf ("No ACPI tables were found in %s\n", InputPathname);
249 }
250
251
252 CleanupAndExit:
253
254 if (State == AX_STATE_EXTRACT_DATA)
255 {
256 /* Received an input file EOF while extracting data */
257
258 printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten,
259 ThisTableBytesWritten, Gbl_OutputFilename);
260 }
261
262 if (OutputFile)
263 {
264 fclose (OutputFile);
265 }
266
267 fclose (InputFile);
268 return (Status);
269 }
270
271
272 /******************************************************************************
273 *
274 * FUNCTION: AxExtractToMultiAmlFile
275 *
276 * PARAMETERS: InputPathname - Filename for input acpidump file
277 *
278 * RETURN: Status
279 *
280 * DESCRIPTION: Convert all DSDT/SSDT tables to binary and append them all
281 * into a single output file. Used to simplify the loading of
282 * multiple/many SSDTs into a utility like acpiexec -- instead
283 * of creating many separate output files.
284 *
285 ******************************************************************************/
286
287 int
288 AxExtractToMultiAmlFile (
289 char *InputPathname)
290 {
291 FILE *InputFile;
292 FILE *OutputFile;
293 int Status = 0;
294 int TotalBytesWritten = 0;
295 int ThisTableBytesWritten = 0;
296 unsigned int BytesConverted;
297 char ThisSignature[4];
298 unsigned int State = AX_STATE_FIND_HEADER;
299
300
301 strcpy (Gbl_OutputFilename, AX_MULTI_TABLE_FILENAME);
302
303 /* Open the input file in text mode */
304
305 InputFile = fopen (InputPathname, "r");
306 if (!InputFile)
307 {
308 printf ("Could not open input file %s\n", InputPathname);
309 return (-1);
310 }
311
312 if (!AxIsFileAscii (InputFile))
313 {
314 fclose (InputFile);
315 return (-1);
316 }
317
318 /* Open the output file in binary mode */
319
320 OutputFile = fopen (Gbl_OutputFilename, "w+b");
321 if (!OutputFile)
322 {
323 printf ("Could not open output file %s\n", Gbl_OutputFilename);
324 fclose (InputFile);
325 return (-1);
326 }
327
328 /* Convert the DSDT and all SSDTs to binary */
329
330 while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
331 {
332 /*
333 * Check up front if we have a header line of the form:
334 * DSDT @ 0xdfffd0c0 (10999 bytes)
335 */
336 if (AX_IS_TABLE_BLOCK_HEADER &&
337 (State == AX_STATE_EXTRACT_DATA))
338 {
339 /* End of previous table, start of new table */
340
341 if (ThisTableBytesWritten)
342 {
343 printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten,
344 ThisTableBytesWritten, Gbl_OutputFilename);
345 }
346 else
347 {
348 Gbl_TableCount--;
349 }
350
351 State = AX_STATE_FIND_HEADER;
352 }
353
354 switch (State)
355 {
356 case AX_STATE_FIND_HEADER:
357
358 if (!AxIsDataBlockHeader ())
359 {
360 continue;
361 }
362
363 ACPI_COPY_NAMESEG (ThisSignature, Gbl_LineBuffer);
364
365 /* Only want DSDT and SSDTs */
366
367 if (!ACPI_COMPARE_NAMESEG (ThisSignature, ACPI_SIG_DSDT) &&
368 !ACPI_COMPARE_NAMESEG (ThisSignature, ACPI_SIG_SSDT))
369 {
370 continue;
371 }
372
373 /*
374 * Toss this block header of the form "<sig> @ <addr>" line
375 * and move on to the actual data block
376 */
377 Gbl_TableCount++;
378 ThisTableBytesWritten = 0;
379 State = AX_STATE_EXTRACT_DATA;
380 continue;
381
382 case AX_STATE_EXTRACT_DATA:
383
384 if (!AxIsHexDataLine ())
385 {
386 continue; /* Toss any lines that are not raw hex data */
387 }
388
389 /* Empty line or non-data line terminates the data block */
390
391 BytesConverted = AxConvertAndWrite (OutputFile, ThisSignature);
392 switch (BytesConverted)
393 {
394 case 0:
395
396 State = AX_STATE_FIND_HEADER; /* No more data block lines */
397 continue;
398
399 case -1:
400
401 Status = -1;
402 goto CleanupAndExit; /* There was a write error */
403
404 default: /* Normal case, get next line */
405
406 ThisTableBytesWritten += BytesConverted;
407 TotalBytesWritten += BytesConverted;
408 continue;
409 }
410
411 default:
412
413 Status = -1;
414 goto CleanupAndExit;
415 }
416 }
417
418
419 CleanupAndExit:
420
421 if (State == AX_STATE_EXTRACT_DATA)
422 {
423 /* Received an input file EOF or error while writing data */
424
425 printf (AX_TABLE_INFO_FORMAT, ThisSignature, ThisTableBytesWritten,
426 ThisTableBytesWritten, Gbl_OutputFilename);
427 }
428
429 printf ("\n%u binary ACPI tables extracted and written to %s (%u bytes)\n",
430 Gbl_TableCount, Gbl_OutputFilename, TotalBytesWritten);
431
432 fclose (InputFile);
433 fclose (OutputFile);
434 return (Status);
435 }
436
437
438 /******************************************************************************
439 *
440 * FUNCTION: AxListAllTables
441 *
442 * PARAMETERS: InputPathname - Filename for acpidump file
443 *
444 * RETURN: Status
445 *
446 * DESCRIPTION: Display info for all ACPI tables found in input. Does not
447 * perform an actual extraction of the tables.
448 *
449 ******************************************************************************/
450
451 int
452 AxListAllTables (
453 char *InputPathname)
454 {
455 FILE *InputFile;
456 unsigned char Header[48];
457 UINT32 ByteCount = 0;
458 INT32 ThisLineByteCount;
459 unsigned int State = AX_STATE_FIND_HEADER;
460
461
462 /* Open input in text mode, output is in binary mode */
463
464 InputFile = fopen (InputPathname, "r");
465 if (!InputFile)
466 {
467 printf ("Could not open input file %s\n", InputPathname);
468 return (-1);
469 }
470
471 if (!AxIsFileAscii (InputFile))
472 {
473 fclose (InputFile);
474 return (-1);
475 }
476
477 /* Info header */
478
479 printf ("\n Signature Length Version Oem Oem "
480 "Oem Compiler Compiler\n");
481 printf ( " Id TableId "
482 "RevisionId Name Revision\n");
483 printf ( " _________ __________ ____ ________ __________ "
484 "__________ _______ __________\n\n");
485
486 /* Dump the headers for all tables found in the input file */
487
488 while (fgets (Gbl_LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
489 {
490 /* Ignore empty lines */
491
492 if (AxIsEmptyLine (Gbl_LineBuffer))
493 {
494 continue;
495 }
496
497 /*
498 * Check up front if we have a header line of the form:
499 * DSDT @ 0xdfffd0c0 (10999 bytes)
500 */
501 if (AX_IS_TABLE_BLOCK_HEADER &&
502 (State == AX_STATE_EXTRACT_DATA))
503 {
504 State = AX_STATE_FIND_HEADER;
505 }
506
507 switch (State)
508 {
509 case AX_STATE_FIND_HEADER:
510
511 ByteCount = 0;
512 if (!AxIsDataBlockHeader ())
513 {
514 continue;
515 }
516
517 State = AX_STATE_EXTRACT_DATA;
518 continue;
519
520 case AX_STATE_EXTRACT_DATA:
521
522 /* Ignore any lines that don't look like a data line */
523
524 if (!AxIsHexDataLine ())
525 {
526 continue; /* Toss any lines that are not raw hex data */
527 }
528
529 /* Convert header to hex and display it */
530
531 ThisLineByteCount = AxConvertToBinary (Gbl_LineBuffer,
532 &Header[ByteCount]);
533 if (ThisLineByteCount == EOF)
534 {
535 fclose (InputFile);
536 return (-1);
537 }
538
539 ByteCount += ThisLineByteCount;
540 if (ByteCount >= sizeof (ACPI_TABLE_HEADER))
541 {
542 AxDumpTableHeader (Header);
543 State = AX_STATE_FIND_HEADER;
544 }
545 continue;
546
547 default:
548 break;
549 }
550 }
551
552 printf ("\nFound %u ACPI tables in %s\n", Gbl_TableCount, InputPathname);
553 fclose (InputFile);
554 return (0);
555 }