acpixtract.c revision 1.5 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.4 christos * Copyright (C) 2000 - 2015, 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.2 christos #include "acpi.h"
45 1.2 christos #include "accommon.h"
46 1.2 christos #include "acapps.h"
47 1.1 jruoho
48 1.1 jruoho #include <stdio.h>
49 1.1 jruoho #include <stdlib.h>
50 1.1 jruoho #include <string.h>
51 1.1 jruoho #include <ctype.h>
52 1.1 jruoho
53 1.2 christos /* Local prototypes */
54 1.1 jruoho
55 1.2 christos static void
56 1.2 christos AxCheckAscii (
57 1.1 jruoho char *Name,
58 1.1 jruoho int Count);
59 1.1 jruoho
60 1.2 christos static void
61 1.2 christos AxNormalizeSignature (
62 1.1 jruoho char *Signature);
63 1.1 jruoho
64 1.2 christos static unsigned int
65 1.2 christos AxGetNextInstance (
66 1.1 jruoho char *InputPathname,
67 1.1 jruoho char *Signature);
68 1.1 jruoho
69 1.2 christos static size_t
70 1.2 christos AxGetTableHeader (
71 1.1 jruoho FILE *InputFile,
72 1.1 jruoho unsigned char *OutputData);
73 1.1 jruoho
74 1.2 christos static unsigned int
75 1.2 christos AxCountTableInstances (
76 1.1 jruoho char *InputPathname,
77 1.1 jruoho char *Signature);
78 1.1 jruoho
79 1.1 jruoho int
80 1.2 christos AxExtractTables (
81 1.2 christos char *InputPathname,
82 1.2 christos char *Signature,
83 1.2 christos unsigned int MinimumInstances);
84 1.2 christos
85 1.2 christos int
86 1.2 christos AxListTables (
87 1.1 jruoho char *InputPathname);
88 1.1 jruoho
89 1.2 christos static size_t
90 1.2 christos AxConvertLine (
91 1.1 jruoho char *InputLine,
92 1.1 jruoho unsigned char *OutputData);
93 1.1 jruoho
94 1.2 christos static int
95 1.2 christos AxIsEmptyLine (
96 1.2 christos char *Buffer);
97 1.1 jruoho
98 1.2 christos typedef struct AxTableInfo
99 1.2 christos {
100 1.2 christos UINT32 Signature;
101 1.2 christos unsigned int Instances;
102 1.2 christos unsigned int NextInstance;
103 1.2 christos struct AxTableInfo *Next;
104 1.2 christos
105 1.2 christos } AX_TABLE_INFO;
106 1.1 jruoho
107 1.2 christos /* Extraction states */
108 1.2 christos
109 1.2 christos #define AX_STATE_FIND_HEADER 0
110 1.2 christos #define AX_STATE_EXTRACT_DATA 1
111 1.2 christos
112 1.2 christos /* Miscellaneous constants */
113 1.1 jruoho
114 1.2 christos #define AX_LINE_BUFFER_SIZE 256
115 1.2 christos #define AX_MIN_TABLE_NAME_LENGTH 6 /* strlen ("DSDT @") */
116 1.1 jruoho
117 1.1 jruoho
118 1.2 christos static AX_TABLE_INFO *AxTableListHead = NULL;
119 1.2 christos static char Filename[16];
120 1.2 christos static unsigned char Data[16];
121 1.2 christos static char LineBuffer[AX_LINE_BUFFER_SIZE];
122 1.2 christos static char HeaderBuffer[AX_LINE_BUFFER_SIZE];
123 1.2 christos static char InstanceBuffer[AX_LINE_BUFFER_SIZE];
124 1.1 jruoho
125 1.1 jruoho
126 1.2 christos /*******************************************************************************
127 1.2 christos *
128 1.2 christos * FUNCTION: AxCheckAscii
129 1.1 jruoho *
130 1.1 jruoho * PARAMETERS: Name - Ascii string, at least as long as Count
131 1.1 jruoho * Count - Number of characters to check
132 1.1 jruoho *
133 1.1 jruoho * RETURN: None
134 1.1 jruoho *
135 1.1 jruoho * DESCRIPTION: Ensure that the requested number of characters are printable
136 1.1 jruoho * Ascii characters. Sets non-printable and null chars to <space>.
137 1.1 jruoho *
138 1.1 jruoho ******************************************************************************/
139 1.1 jruoho
140 1.2 christos static void
141 1.2 christos AxCheckAscii (
142 1.1 jruoho char *Name,
143 1.1 jruoho int Count)
144 1.1 jruoho {
145 1.1 jruoho int i;
146 1.1 jruoho
147 1.1 jruoho
148 1.1 jruoho for (i = 0; i < Count; i++)
149 1.1 jruoho {
150 1.1 jruoho if (!Name[i] || !isprint ((int) Name[i]))
151 1.1 jruoho {
152 1.1 jruoho Name[i] = ' ';
153 1.1 jruoho }
154 1.1 jruoho }
155 1.1 jruoho }
156 1.1 jruoho
157 1.1 jruoho
158 1.2 christos /******************************************************************************
159 1.2 christos *
160 1.2 christos * FUNCTION: AxIsEmptyLine
161 1.2 christos *
162 1.2 christos * PARAMETERS: Buffer - Line from input file
163 1.2 christos *
164 1.2 christos * RETURN: TRUE if line is empty (zero or more blanks only)
165 1.2 christos *
166 1.2 christos * DESCRIPTION: Determine if an input line is empty.
167 1.2 christos *
168 1.2 christos ******************************************************************************/
169 1.2 christos
170 1.2 christos static int
171 1.2 christos AxIsEmptyLine (
172 1.2 christos char *Buffer)
173 1.2 christos {
174 1.2 christos
175 1.2 christos /* Skip all spaces */
176 1.2 christos
177 1.2 christos while (*Buffer == ' ')
178 1.2 christos {
179 1.2 christos Buffer++;
180 1.2 christos }
181 1.2 christos
182 1.2 christos /* If end-of-line, this line is empty */
183 1.2 christos
184 1.2 christos if (*Buffer == '\n')
185 1.2 christos {
186 1.2 christos return (1);
187 1.2 christos }
188 1.2 christos
189 1.2 christos return (0);
190 1.2 christos }
191 1.2 christos
192 1.2 christos
193 1.1 jruoho /*******************************************************************************
194 1.1 jruoho *
195 1.2 christos * FUNCTION: AxNormalizeSignature
196 1.1 jruoho *
197 1.1 jruoho * PARAMETERS: Name - Ascii string containing an ACPI signature
198 1.1 jruoho *
199 1.1 jruoho * RETURN: None
200 1.1 jruoho *
201 1.1 jruoho * DESCRIPTION: Change "RSD PTR" to "RSDP"
202 1.1 jruoho *
203 1.1 jruoho ******************************************************************************/
204 1.1 jruoho
205 1.2 christos static void
206 1.2 christos AxNormalizeSignature (
207 1.1 jruoho char *Signature)
208 1.1 jruoho {
209 1.1 jruoho
210 1.1 jruoho if (!strncmp (Signature, "RSD ", 4))
211 1.1 jruoho {
212 1.1 jruoho Signature[3] = 'P';
213 1.1 jruoho }
214 1.1 jruoho }
215 1.1 jruoho
216 1.1 jruoho
217 1.1 jruoho /******************************************************************************
218 1.1 jruoho *
219 1.2 christos * FUNCTION: AxConvertLine
220 1.1 jruoho *
221 1.1 jruoho * PARAMETERS: InputLine - One line from the input acpidump file
222 1.1 jruoho * OutputData - Where the converted data is returned
223 1.1 jruoho *
224 1.1 jruoho * RETURN: The number of bytes actually converted
225 1.1 jruoho *
226 1.1 jruoho * DESCRIPTION: Convert one line of ascii text binary (up to 16 bytes)
227 1.1 jruoho *
228 1.1 jruoho ******************************************************************************/
229 1.1 jruoho
230 1.2 christos static size_t
231 1.2 christos AxConvertLine (
232 1.1 jruoho char *InputLine,
233 1.1 jruoho unsigned char *OutputData)
234 1.1 jruoho {
235 1.1 jruoho char *End;
236 1.1 jruoho int BytesConverted;
237 1.1 jruoho int Converted[16];
238 1.1 jruoho int i;
239 1.1 jruoho
240 1.1 jruoho
241 1.1 jruoho /* Terminate the input line at the end of the actual data (for sscanf) */
242 1.1 jruoho
243 1.1 jruoho End = strstr (InputLine + 2, " ");
244 1.1 jruoho if (!End)
245 1.1 jruoho {
246 1.1 jruoho return (0); /* Don't understand the format */
247 1.1 jruoho }
248 1.1 jruoho *End = 0;
249 1.1 jruoho
250 1.1 jruoho /*
251 1.1 jruoho * Convert one line of table data, of the form:
252 1.1 jruoho * <offset>: <up to 16 bytes of hex data> <ASCII representation> <newline>
253 1.1 jruoho *
254 1.1 jruoho * Example:
255 1.1 jruoho * 02C0: 5F 53 42 5F 4C 4E 4B 44 00 12 13 04 0C FF FF 08 _SB_LNKD........
256 1.1 jruoho */
257 1.1 jruoho BytesConverted = sscanf (InputLine,
258 1.1 jruoho "%*s %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
259 1.1 jruoho &Converted[0], &Converted[1], &Converted[2], &Converted[3],
260 1.1 jruoho &Converted[4], &Converted[5], &Converted[6], &Converted[7],
261 1.1 jruoho &Converted[8], &Converted[9], &Converted[10], &Converted[11],
262 1.1 jruoho &Converted[12], &Converted[13], &Converted[14], &Converted[15]);
263 1.1 jruoho
264 1.1 jruoho /* Pack converted data into a byte array */
265 1.1 jruoho
266 1.1 jruoho for (i = 0; i < BytesConverted; i++)
267 1.1 jruoho {
268 1.1 jruoho OutputData[i] = (unsigned char) Converted[i];
269 1.1 jruoho }
270 1.1 jruoho
271 1.1 jruoho return ((size_t) BytesConverted);
272 1.1 jruoho }
273 1.1 jruoho
274 1.1 jruoho
275 1.1 jruoho /******************************************************************************
276 1.1 jruoho *
277 1.2 christos * FUNCTION: AxGetTableHeader
278 1.1 jruoho *
279 1.1 jruoho * PARAMETERS: InputFile - Handle for the input acpidump file
280 1.1 jruoho * OutputData - Where the table header is returned
281 1.1 jruoho *
282 1.1 jruoho * RETURN: The actual number of bytes converted
283 1.1 jruoho *
284 1.1 jruoho * DESCRIPTION: Extract and convert an ACPI table header
285 1.1 jruoho *
286 1.1 jruoho ******************************************************************************/
287 1.1 jruoho
288 1.2 christos static size_t
289 1.2 christos AxGetTableHeader (
290 1.1 jruoho FILE *InputFile,
291 1.1 jruoho unsigned char *OutputData)
292 1.1 jruoho {
293 1.1 jruoho size_t BytesConverted;
294 1.1 jruoho size_t TotalConverted = 0;
295 1.1 jruoho int i;
296 1.1 jruoho
297 1.1 jruoho
298 1.2 christos /* Get the full 36 byte ACPI table header, requires 3 input text lines */
299 1.1 jruoho
300 1.1 jruoho for (i = 0; i < 3; i++)
301 1.1 jruoho {
302 1.2 christos if (!fgets (HeaderBuffer, AX_LINE_BUFFER_SIZE, InputFile))
303 1.1 jruoho {
304 1.1 jruoho return (TotalConverted);
305 1.1 jruoho }
306 1.1 jruoho
307 1.2 christos BytesConverted = AxConvertLine (HeaderBuffer, OutputData);
308 1.1 jruoho TotalConverted += BytesConverted;
309 1.1 jruoho OutputData += 16;
310 1.1 jruoho
311 1.1 jruoho if (BytesConverted != 16)
312 1.1 jruoho {
313 1.1 jruoho return (TotalConverted);
314 1.1 jruoho }
315 1.1 jruoho }
316 1.1 jruoho
317 1.1 jruoho return (TotalConverted);
318 1.1 jruoho }
319 1.1 jruoho
320 1.1 jruoho
321 1.1 jruoho /******************************************************************************
322 1.1 jruoho *
323 1.2 christos * FUNCTION: AxCountTableInstances
324 1.1 jruoho *
325 1.1 jruoho * PARAMETERS: InputPathname - Filename for acpidump file
326 1.1 jruoho * Signature - Requested signature to count
327 1.1 jruoho *
328 1.1 jruoho * RETURN: The number of instances of the signature
329 1.1 jruoho *
330 1.1 jruoho * DESCRIPTION: Count the instances of tables with the given signature within
331 1.1 jruoho * the input acpidump file.
332 1.1 jruoho *
333 1.1 jruoho ******************************************************************************/
334 1.1 jruoho
335 1.2 christos static unsigned int
336 1.2 christos AxCountTableInstances (
337 1.1 jruoho char *InputPathname,
338 1.1 jruoho char *Signature)
339 1.1 jruoho {
340 1.1 jruoho FILE *InputFile;
341 1.1 jruoho unsigned int Instances = 0;
342 1.1 jruoho
343 1.1 jruoho
344 1.1 jruoho InputFile = fopen (InputPathname, "rt");
345 1.1 jruoho if (!InputFile)
346 1.1 jruoho {
347 1.2 christos printf ("Could not open file %s\n", InputPathname);
348 1.1 jruoho return (0);
349 1.1 jruoho }
350 1.1 jruoho
351 1.1 jruoho /* Count the number of instances of this signature */
352 1.1 jruoho
353 1.2 christos while (fgets (InstanceBuffer, AX_LINE_BUFFER_SIZE, InputFile))
354 1.1 jruoho {
355 1.1 jruoho /* Ignore empty lines and lines that start with a space */
356 1.1 jruoho
357 1.2 christos if (AxIsEmptyLine (InstanceBuffer) ||
358 1.2 christos (InstanceBuffer[0] == ' '))
359 1.1 jruoho {
360 1.1 jruoho continue;
361 1.1 jruoho }
362 1.1 jruoho
363 1.2 christos AxNormalizeSignature (InstanceBuffer);
364 1.2 christos if (ACPI_COMPARE_NAME (InstanceBuffer, Signature))
365 1.1 jruoho {
366 1.1 jruoho Instances++;
367 1.1 jruoho }
368 1.1 jruoho }
369 1.1 jruoho
370 1.1 jruoho fclose (InputFile);
371 1.1 jruoho return (Instances);
372 1.1 jruoho }
373 1.1 jruoho
374 1.1 jruoho
375 1.1 jruoho /******************************************************************************
376 1.1 jruoho *
377 1.2 christos * FUNCTION: AxGetNextInstance
378 1.1 jruoho *
379 1.1 jruoho * PARAMETERS: InputPathname - Filename for acpidump file
380 1.1 jruoho * Signature - Requested ACPI signature
381 1.1 jruoho *
382 1.1 jruoho * RETURN: The next instance number for this signature. Zero if this
383 1.1 jruoho * is the first instance of this signature.
384 1.1 jruoho *
385 1.1 jruoho * DESCRIPTION: Get the next instance number of the specified table. If this
386 1.1 jruoho * is the first instance of the table, create a new instance
387 1.1 jruoho * block. Note: only SSDT and PSDT tables can have multiple
388 1.1 jruoho * instances.
389 1.1 jruoho *
390 1.1 jruoho ******************************************************************************/
391 1.1 jruoho
392 1.2 christos static unsigned int
393 1.2 christos AxGetNextInstance (
394 1.1 jruoho char *InputPathname,
395 1.1 jruoho char *Signature)
396 1.1 jruoho {
397 1.2 christos AX_TABLE_INFO *Info;
398 1.1 jruoho
399 1.1 jruoho
400 1.2 christos Info = AxTableListHead;
401 1.1 jruoho while (Info)
402 1.1 jruoho {
403 1.2 christos if (*(UINT32 *) Signature == Info->Signature)
404 1.1 jruoho {
405 1.1 jruoho break;
406 1.1 jruoho }
407 1.1 jruoho
408 1.1 jruoho Info = Info->Next;
409 1.1 jruoho }
410 1.1 jruoho
411 1.1 jruoho if (!Info)
412 1.1 jruoho {
413 1.1 jruoho /* Signature not found, create new table info block */
414 1.1 jruoho
415 1.2 christos Info = malloc (sizeof (AX_TABLE_INFO));
416 1.1 jruoho if (!Info)
417 1.1 jruoho {
418 1.1 jruoho printf ("Could not allocate memory\n");
419 1.1 jruoho exit (0);
420 1.1 jruoho }
421 1.1 jruoho
422 1.2 christos Info->Signature = *(UINT32 *) Signature;
423 1.2 christos Info->Instances = AxCountTableInstances (InputPathname, Signature);
424 1.1 jruoho Info->NextInstance = 1;
425 1.2 christos Info->Next = AxTableListHead;
426 1.2 christos AxTableListHead = Info;
427 1.1 jruoho }
428 1.1 jruoho
429 1.1 jruoho if (Info->Instances > 1)
430 1.1 jruoho {
431 1.1 jruoho return (Info->NextInstance++);
432 1.1 jruoho }
433 1.1 jruoho
434 1.1 jruoho return (0);
435 1.1 jruoho }
436 1.1 jruoho
437 1.1 jruoho
438 1.1 jruoho /******************************************************************************
439 1.1 jruoho *
440 1.2 christos * FUNCTION: AxExtractTables
441 1.1 jruoho *
442 1.1 jruoho * PARAMETERS: InputPathname - Filename for acpidump file
443 1.1 jruoho * Signature - Requested ACPI signature to extract.
444 1.1 jruoho * NULL means extract ALL tables.
445 1.1 jruoho * MinimumInstances - Min instances that are acceptable
446 1.1 jruoho *
447 1.1 jruoho * RETURN: Status
448 1.1 jruoho *
449 1.1 jruoho * DESCRIPTION: Convert text ACPI tables to binary
450 1.1 jruoho *
451 1.1 jruoho ******************************************************************************/
452 1.1 jruoho
453 1.1 jruoho int
454 1.2 christos AxExtractTables (
455 1.1 jruoho char *InputPathname,
456 1.1 jruoho char *Signature,
457 1.1 jruoho unsigned int MinimumInstances)
458 1.1 jruoho {
459 1.1 jruoho FILE *InputFile;
460 1.1 jruoho FILE *OutputFile = NULL;
461 1.1 jruoho size_t BytesWritten;
462 1.1 jruoho size_t TotalBytesWritten = 0;
463 1.1 jruoho size_t BytesConverted;
464 1.2 christos unsigned int State = AX_STATE_FIND_HEADER;
465 1.1 jruoho unsigned int FoundTable = 0;
466 1.1 jruoho unsigned int Instances = 0;
467 1.1 jruoho unsigned int ThisInstance;
468 1.1 jruoho char ThisSignature[4];
469 1.1 jruoho int Status = 0;
470 1.1 jruoho
471 1.1 jruoho
472 1.1 jruoho /* Open input in text mode, output is in binary mode */
473 1.1 jruoho
474 1.1 jruoho InputFile = fopen (InputPathname, "rt");
475 1.1 jruoho if (!InputFile)
476 1.1 jruoho {
477 1.2 christos printf ("Could not open file %s\n", InputPathname);
478 1.1 jruoho return (-1);
479 1.1 jruoho }
480 1.1 jruoho
481 1.1 jruoho if (Signature)
482 1.1 jruoho {
483 1.1 jruoho /* Are there enough instances of the table to continue? */
484 1.1 jruoho
485 1.2 christos AxNormalizeSignature (Signature);
486 1.1 jruoho
487 1.2 christos Instances = AxCountTableInstances (InputPathname, Signature);
488 1.1 jruoho if (Instances < MinimumInstances)
489 1.1 jruoho {
490 1.1 jruoho printf ("Table %s was not found in %s\n", Signature, InputPathname);
491 1.1 jruoho Status = -1;
492 1.1 jruoho goto CleanupAndExit;
493 1.1 jruoho }
494 1.1 jruoho
495 1.1 jruoho if (Instances == 0)
496 1.1 jruoho {
497 1.1 jruoho goto CleanupAndExit;
498 1.1 jruoho }
499 1.1 jruoho }
500 1.1 jruoho
501 1.1 jruoho /* Convert all instances of the table to binary */
502 1.1 jruoho
503 1.2 christos while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
504 1.1 jruoho {
505 1.1 jruoho switch (State)
506 1.1 jruoho {
507 1.2 christos case AX_STATE_FIND_HEADER:
508 1.2 christos
509 1.2 christos /* Ignore lines that are too short to be header lines */
510 1.2 christos
511 1.2 christos if (strlen (LineBuffer) < AX_MIN_TABLE_NAME_LENGTH)
512 1.2 christos {
513 1.2 christos continue;
514 1.2 christos }
515 1.1 jruoho
516 1.1 jruoho /* Ignore empty lines and lines that start with a space */
517 1.1 jruoho
518 1.2 christos if (AxIsEmptyLine (LineBuffer) ||
519 1.2 christos (LineBuffer[0] == ' '))
520 1.1 jruoho {
521 1.1 jruoho continue;
522 1.1 jruoho }
523 1.1 jruoho
524 1.2 christos /*
525 1.2 christos * Ignore lines that are not of the form <sig> @ <addr>.
526 1.2 christos * Examples of lines that must be supported:
527 1.2 christos *
528 1.2 christos * DSDT @ 0x737e4000
529 1.2 christos * XSDT @ 0x737f2fff
530 1.2 christos * RSD PTR @ 0xf6cd0
531 1.2 christos * SSDT @ (nil)
532 1.2 christos */
533 1.2 christos if (!strstr (LineBuffer, " @ "))
534 1.2 christos {
535 1.2 christos continue;
536 1.2 christos }
537 1.2 christos
538 1.2 christos AxNormalizeSignature (LineBuffer);
539 1.2 christos ACPI_MOVE_NAME (ThisSignature, LineBuffer);
540 1.1 jruoho
541 1.1 jruoho if (Signature)
542 1.1 jruoho {
543 1.1 jruoho /* Ignore signatures that don't match */
544 1.1 jruoho
545 1.2 christos if (!ACPI_COMPARE_NAME (ThisSignature, Signature))
546 1.1 jruoho {
547 1.1 jruoho continue;
548 1.1 jruoho }
549 1.1 jruoho }
550 1.1 jruoho
551 1.1 jruoho /*
552 1.1 jruoho * Get the instance number for this signature. Only the
553 1.1 jruoho * SSDT and PSDT tables can have multiple instances.
554 1.1 jruoho */
555 1.2 christos ThisInstance = AxGetNextInstance (InputPathname, ThisSignature);
556 1.1 jruoho
557 1.1 jruoho /* Build an output filename and create/open the output file */
558 1.1 jruoho
559 1.1 jruoho if (ThisInstance > 0)
560 1.1 jruoho {
561 1.2 christos snprintf (Filename, sizeof(Filename), "%4.4s%u.dat", ThisSignature, ThisInstance);
562 1.1 jruoho }
563 1.1 jruoho else
564 1.1 jruoho {
565 1.2 christos snprintf (Filename, sizeof(Filename), "%4.4s.dat", ThisSignature);
566 1.1 jruoho }
567 1.1 jruoho
568 1.5 christos AcpiUtStrlwr (Filename);
569 1.1 jruoho OutputFile = fopen (Filename, "w+b");
570 1.1 jruoho if (!OutputFile)
571 1.1 jruoho {
572 1.2 christos printf ("Could not open file %s\n", Filename);
573 1.1 jruoho Status = -1;
574 1.1 jruoho goto CleanupAndExit;
575 1.1 jruoho }
576 1.1 jruoho
577 1.2 christos State = AX_STATE_EXTRACT_DATA;
578 1.1 jruoho TotalBytesWritten = 0;
579 1.1 jruoho FoundTable = 1;
580 1.1 jruoho continue;
581 1.1 jruoho
582 1.2 christos case AX_STATE_EXTRACT_DATA:
583 1.1 jruoho
584 1.1 jruoho /* Empty line or non-data line terminates the data */
585 1.1 jruoho
586 1.2 christos if (AxIsEmptyLine (LineBuffer) ||
587 1.2 christos (LineBuffer[0] != ' '))
588 1.1 jruoho {
589 1.1 jruoho fclose (OutputFile);
590 1.1 jruoho OutputFile = NULL;
591 1.2 christos State = AX_STATE_FIND_HEADER;
592 1.1 jruoho
593 1.2 christos printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
594 1.2 christos ThisSignature, (unsigned int) TotalBytesWritten, Filename);
595 1.1 jruoho continue;
596 1.1 jruoho }
597 1.1 jruoho
598 1.1 jruoho /* Convert the ascii data (one line of text) to binary */
599 1.1 jruoho
600 1.2 christos BytesConverted = AxConvertLine (LineBuffer, Data);
601 1.1 jruoho
602 1.1 jruoho /* Write the binary data */
603 1.1 jruoho
604 1.1 jruoho BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
605 1.1 jruoho if (BytesWritten != BytesConverted)
606 1.1 jruoho {
607 1.2 christos printf ("Error when writing file %s\n", Filename);
608 1.1 jruoho fclose (OutputFile);
609 1.1 jruoho OutputFile = NULL;
610 1.1 jruoho Status = -1;
611 1.1 jruoho goto CleanupAndExit;
612 1.1 jruoho }
613 1.1 jruoho
614 1.1 jruoho TotalBytesWritten += BytesConverted;
615 1.1 jruoho continue;
616 1.1 jruoho
617 1.1 jruoho default:
618 1.2 christos
619 1.1 jruoho Status = -1;
620 1.1 jruoho goto CleanupAndExit;
621 1.1 jruoho }
622 1.1 jruoho }
623 1.1 jruoho
624 1.1 jruoho if (!FoundTable)
625 1.1 jruoho {
626 1.1 jruoho printf ("Table %s was not found in %s\n", Signature, InputPathname);
627 1.1 jruoho }
628 1.1 jruoho
629 1.1 jruoho
630 1.1 jruoho CleanupAndExit:
631 1.1 jruoho
632 1.1 jruoho if (OutputFile)
633 1.1 jruoho {
634 1.1 jruoho fclose (OutputFile);
635 1.2 christos if (State == AX_STATE_EXTRACT_DATA)
636 1.1 jruoho {
637 1.1 jruoho /* Received an EOF while extracting data */
638 1.1 jruoho
639 1.2 christos printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
640 1.2 christos ThisSignature, (unsigned int) TotalBytesWritten, Filename);
641 1.1 jruoho }
642 1.1 jruoho }
643 1.1 jruoho
644 1.1 jruoho fclose (InputFile);
645 1.1 jruoho return (Status);
646 1.1 jruoho }
647 1.1 jruoho
648 1.1 jruoho
649 1.1 jruoho /******************************************************************************
650 1.1 jruoho *
651 1.2 christos * FUNCTION: AxListTables
652 1.1 jruoho *
653 1.1 jruoho * PARAMETERS: InputPathname - Filename for acpidump file
654 1.1 jruoho *
655 1.1 jruoho * RETURN: Status
656 1.1 jruoho *
657 1.1 jruoho * DESCRIPTION: Display info for all ACPI tables found in input. Does not
658 1.1 jruoho * perform an actual extraction of the tables.
659 1.1 jruoho *
660 1.1 jruoho ******************************************************************************/
661 1.1 jruoho
662 1.1 jruoho int
663 1.2 christos AxListTables (
664 1.1 jruoho char *InputPathname)
665 1.1 jruoho {
666 1.1 jruoho FILE *InputFile;
667 1.1 jruoho size_t HeaderSize;
668 1.1 jruoho unsigned char Header[48];
669 1.5 christos unsigned int TableCount = 0;
670 1.1 jruoho ACPI_TABLE_HEADER *TableHeader = (ACPI_TABLE_HEADER *) (void *) Header;
671 1.1 jruoho
672 1.1 jruoho
673 1.1 jruoho /* Open input in text mode, output is in binary mode */
674 1.1 jruoho
675 1.1 jruoho InputFile = fopen (InputPathname, "rt");
676 1.1 jruoho if (!InputFile)
677 1.1 jruoho {
678 1.2 christos printf ("Could not open file %s\n", InputPathname);
679 1.1 jruoho return (-1);
680 1.1 jruoho }
681 1.1 jruoho
682 1.1 jruoho /* Dump the headers for all tables found in the input file */
683 1.1 jruoho
684 1.2 christos printf ("\nSignature Length Revision OemId OemTableId"
685 1.1 jruoho " OemRevision CompilerId CompilerRevision\n\n");
686 1.1 jruoho
687 1.2 christos while (fgets (LineBuffer, AX_LINE_BUFFER_SIZE, InputFile))
688 1.1 jruoho {
689 1.1 jruoho /* Ignore empty lines and lines that start with a space */
690 1.1 jruoho
691 1.2 christos if (AxIsEmptyLine (LineBuffer) ||
692 1.2 christos (LineBuffer[0] == ' '))
693 1.1 jruoho {
694 1.1 jruoho continue;
695 1.1 jruoho }
696 1.1 jruoho
697 1.1 jruoho /* Get the 36 byte header and display the fields */
698 1.1 jruoho
699 1.2 christos HeaderSize = AxGetTableHeader (InputFile, Header);
700 1.1 jruoho if (HeaderSize < 16)
701 1.1 jruoho {
702 1.1 jruoho continue;
703 1.1 jruoho }
704 1.1 jruoho
705 1.1 jruoho /* RSDP has an oddball signature and header */
706 1.1 jruoho
707 1.1 jruoho if (!strncmp (TableHeader->Signature, "RSD PTR ", 8))
708 1.1 jruoho {
709 1.2 christos AxCheckAscii ((char *) &Header[9], 6);
710 1.2 christos printf ("%7.4s \"%6.6s\"\n", "RSDP", &Header[9]);
711 1.1 jruoho TableCount++;
712 1.1 jruoho continue;
713 1.1 jruoho }
714 1.1 jruoho
715 1.1 jruoho /* Minimum size for table with standard header */
716 1.1 jruoho
717 1.2 christos if (HeaderSize < sizeof (ACPI_TABLE_HEADER))
718 1.1 jruoho {
719 1.1 jruoho continue;
720 1.1 jruoho }
721 1.1 jruoho
722 1.1 jruoho /* Signature and Table length */
723 1.1 jruoho
724 1.1 jruoho TableCount++;
725 1.2 christos printf ("%7.4s 0x%8.8X", TableHeader->Signature, TableHeader->Length);
726 1.1 jruoho
727 1.1 jruoho /* FACS has only signature and length */
728 1.1 jruoho
729 1.2 christos if (ACPI_COMPARE_NAME (TableHeader->Signature, "FACS"))
730 1.1 jruoho {
731 1.1 jruoho printf ("\n");
732 1.1 jruoho continue;
733 1.1 jruoho }
734 1.1 jruoho
735 1.1 jruoho /* OEM IDs and Compiler IDs */
736 1.1 jruoho
737 1.2 christos AxCheckAscii (TableHeader->OemId, 6);
738 1.2 christos AxCheckAscii (TableHeader->OemTableId, 8);
739 1.2 christos AxCheckAscii (TableHeader->AslCompilerId, 4);
740 1.1 jruoho
741 1.2 christos printf (" 0x%2.2X \"%6.6s\" \"%8.8s\" 0x%8.8X \"%4.4s\" 0x%8.8X\n",
742 1.1 jruoho TableHeader->Revision, TableHeader->OemId,
743 1.1 jruoho TableHeader->OemTableId, TableHeader->OemRevision,
744 1.1 jruoho TableHeader->AslCompilerId, TableHeader->AslCompilerRevision);
745 1.1 jruoho }
746 1.1 jruoho
747 1.2 christos printf ("\nFound %u ACPI tables\n", TableCount);
748 1.1 jruoho fclose (InputFile);
749 1.1 jruoho return (0);
750 1.1 jruoho }
751