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