apdump.c revision 1.1 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: apdump - Dump routines for ACPI tables (acpidump)
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2013, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpidump.h"
45 1.1 christos
46 1.1 christos
47 1.1 christos /* Local prototypes */
48 1.1 christos
49 1.1 christos static int
50 1.1 christos ApDumpTableBuffer (
51 1.1 christos ACPI_TABLE_HEADER *Table,
52 1.1 christos UINT32 Instance,
53 1.1 christos ACPI_PHYSICAL_ADDRESS Address);
54 1.1 christos
55 1.1 christos
56 1.1 christos /******************************************************************************
57 1.1 christos *
58 1.1 christos * FUNCTION: ApIsValidHeader
59 1.1 christos *
60 1.1 christos * PARAMETERS: Table - Pointer to table to be validated
61 1.1 christos *
62 1.1 christos * RETURN: TRUE if the header appears to be valid. FALSE otherwise
63 1.1 christos *
64 1.1 christos * DESCRIPTION: Check for a valid ACPI table header
65 1.1 christos *
66 1.1 christos ******************************************************************************/
67 1.1 christos
68 1.1 christos BOOLEAN
69 1.1 christos ApIsValidHeader (
70 1.1 christos ACPI_TABLE_HEADER *Table)
71 1.1 christos {
72 1.1 christos if (!ACPI_VALIDATE_RSDP_SIG (Table->Signature))
73 1.1 christos {
74 1.1 christos /* Make sure signature is all ASCII and a valid ACPI name */
75 1.1 christos
76 1.1 christos if (!AcpiUtValidAcpiName (Table->Signature))
77 1.1 christos {
78 1.1 christos fprintf (stderr, "Table signature (0x%8.8X) is invalid\n",
79 1.1 christos *(UINT32 *) Table->Signature);
80 1.1 christos return (FALSE);
81 1.1 christos }
82 1.1 christos
83 1.1 christos /* Check for minimum table length */
84 1.1 christos
85 1.1 christos if (Table->Length < sizeof (ACPI_TABLE_HEADER))
86 1.1 christos {
87 1.1 christos fprintf (stderr, "Table length (0x%8.8X) is invalid\n",
88 1.1 christos Table->Length);
89 1.1 christos return (FALSE);
90 1.1 christos }
91 1.1 christos }
92 1.1 christos
93 1.1 christos return (TRUE);
94 1.1 christos }
95 1.1 christos
96 1.1 christos
97 1.1 christos /******************************************************************************
98 1.1 christos *
99 1.1 christos * FUNCTION: ApIsValidChecksum
100 1.1 christos *
101 1.1 christos * PARAMETERS: Table - Pointer to table to be validated
102 1.1 christos *
103 1.1 christos * RETURN: TRUE if the checksum appears to be valid. FALSE otherwise
104 1.1 christos *
105 1.1 christos * DESCRIPTION: Check for a valid ACPI table checksum
106 1.1 christos *
107 1.1 christos ******************************************************************************/
108 1.1 christos
109 1.1 christos BOOLEAN
110 1.1 christos ApIsValidChecksum (
111 1.1 christos ACPI_TABLE_HEADER *Table)
112 1.1 christos {
113 1.1 christos ACPI_STATUS Status;
114 1.1 christos ACPI_TABLE_RSDP *Rsdp;
115 1.1 christos
116 1.1 christos
117 1.1 christos if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
118 1.1 christos {
119 1.1 christos /*
120 1.1 christos * Checksum for RSDP.
121 1.1 christos * Note: Other checksums are computed during the table dump.
122 1.1 christos */
123 1.1 christos
124 1.1 christos Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
125 1.1 christos Status = AcpiTbValidateRsdp (Rsdp);
126 1.1 christos }
127 1.1 christos else
128 1.1 christos {
129 1.1 christos Status = AcpiTbVerifyChecksum (Table, Table->Length);
130 1.1 christos }
131 1.1 christos
132 1.1 christos if (ACPI_FAILURE (Status))
133 1.1 christos {
134 1.1 christos fprintf (stderr, "%4.4s: Warning: wrong checksum\n",
135 1.1 christos Table->Signature);
136 1.1 christos }
137 1.1 christos
138 1.1 christos return (AE_OK);
139 1.1 christos }
140 1.1 christos
141 1.1 christos
142 1.1 christos /******************************************************************************
143 1.1 christos *
144 1.1 christos * FUNCTION: ApGetTableLength
145 1.1 christos *
146 1.1 christos * PARAMETERS: Table - Pointer to the table
147 1.1 christos *
148 1.1 christos * RETURN: Table length
149 1.1 christos *
150 1.1 christos * DESCRIPTION: Obtain table length according to table signature
151 1.1 christos *
152 1.1 christos ******************************************************************************/
153 1.1 christos
154 1.1 christos UINT32
155 1.1 christos ApGetTableLength (
156 1.1 christos ACPI_TABLE_HEADER *Table)
157 1.1 christos {
158 1.1 christos ACPI_TABLE_RSDP *Rsdp;
159 1.1 christos
160 1.1 christos
161 1.1 christos /* Check if table is valid */
162 1.1 christos
163 1.1 christos if (!ApIsValidHeader (Table))
164 1.1 christos {
165 1.1 christos return (0);
166 1.1 christos }
167 1.1 christos
168 1.1 christos if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
169 1.1 christos {
170 1.1 christos Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
171 1.1 christos return (Rsdp->Length);
172 1.1 christos }
173 1.1 christos else
174 1.1 christos {
175 1.1 christos return (Table->Length);
176 1.1 christos }
177 1.1 christos }
178 1.1 christos
179 1.1 christos
180 1.1 christos /******************************************************************************
181 1.1 christos *
182 1.1 christos * FUNCTION: ApDumpTableBuffer
183 1.1 christos *
184 1.1 christos * PARAMETERS: Table - ACPI table to be dumped
185 1.1 christos * Instance - ACPI table instance no. to be dumped
186 1.1 christos * Address - Physical address of the table
187 1.1 christos *
188 1.1 christos * RETURN: None
189 1.1 christos *
190 1.1 christos * DESCRIPTION: Dump an ACPI table in standard ASCII hex format, with a
191 1.1 christos * header that is compatible with the AcpiXtract utility.
192 1.1 christos *
193 1.1 christos ******************************************************************************/
194 1.1 christos
195 1.1 christos static int
196 1.1 christos ApDumpTableBuffer (
197 1.1 christos ACPI_TABLE_HEADER *Table,
198 1.1 christos UINT32 Instance,
199 1.1 christos ACPI_PHYSICAL_ADDRESS Address)
200 1.1 christos {
201 1.1 christos UINT32 TableLength;
202 1.1 christos
203 1.1 christos
204 1.1 christos TableLength = ApGetTableLength (Table);
205 1.1 christos
206 1.1 christos /* Print only the header if requested */
207 1.1 christos
208 1.1 christos if (Gbl_SummaryMode)
209 1.1 christos {
210 1.1 christos AcpiTbPrintTableHeader (Address, Table);
211 1.1 christos return (0);
212 1.1 christos }
213 1.1 christos
214 1.1 christos /* Dump to binary file if requested */
215 1.1 christos
216 1.1 christos if (Gbl_BinaryMode)
217 1.1 christos {
218 1.1 christos return (ApWriteToBinaryFile (Table, Instance));
219 1.1 christos }
220 1.1 christos
221 1.1 christos /*
222 1.1 christos * Dump the table with header for use with acpixtract utility
223 1.1 christos * Note: simplest to just always emit a 64-bit address. AcpiXtract
224 1.1 christos * utility can handle this.
225 1.1 christos */
226 1.1 christos printf ("%4.4s @ 0x%8.8X%8.8X\n", Table->Signature,
227 1.1 christos ACPI_FORMAT_UINT64 (Address));
228 1.1 christos
229 1.1 christos AcpiUtDumpBuffer (ACPI_CAST_PTR (UINT8, Table), TableLength,
230 1.1 christos DB_BYTE_DISPLAY, 0);
231 1.1 christos printf ("\n");
232 1.1 christos return (0);
233 1.1 christos }
234 1.1 christos
235 1.1 christos
236 1.1 christos /******************************************************************************
237 1.1 christos *
238 1.1 christos * FUNCTION: ApDumpAllTables
239 1.1 christos *
240 1.1 christos * PARAMETERS: None
241 1.1 christos *
242 1.1 christos * RETURN: Status
243 1.1 christos *
244 1.1 christos * DESCRIPTION: Get all tables from the RSDT/XSDT (or at least all of the
245 1.1 christos * tables that we can possibly get).
246 1.1 christos *
247 1.1 christos ******************************************************************************/
248 1.1 christos
249 1.1 christos int
250 1.1 christos ApDumpAllTables (
251 1.1 christos void)
252 1.1 christos {
253 1.1 christos ACPI_TABLE_HEADER *Table;
254 1.1 christos UINT32 Instance = 0;
255 1.1 christos ACPI_PHYSICAL_ADDRESS Address;
256 1.1 christos ACPI_STATUS Status;
257 1.1 christos UINT32 i;
258 1.1 christos
259 1.1 christos
260 1.1 christos /* Get and dump all available ACPI tables */
261 1.1 christos
262 1.1 christos for (i = 0; i < AP_MAX_ACPI_FILES; i++)
263 1.1 christos {
264 1.1 christos Status = AcpiOsGetTableByIndex (i, &Table, &Instance, &Address);
265 1.1 christos if (ACPI_FAILURE (Status))
266 1.1 christos {
267 1.1 christos /* AE_LIMIT means that no more tables are available */
268 1.1 christos
269 1.1 christos if (Status == AE_LIMIT)
270 1.1 christos {
271 1.1 christos return (0);
272 1.1 christos }
273 1.1 christos else if (i == 0)
274 1.1 christos {
275 1.1 christos fprintf (stderr, "Could not get ACPI tables, %s\n",
276 1.1 christos AcpiFormatException (Status));
277 1.1 christos return (-1);
278 1.1 christos }
279 1.1 christos else
280 1.1 christos {
281 1.1 christos fprintf (stderr, "Could not get ACPI table at index %u, %s\n",
282 1.1 christos i, AcpiFormatException (Status));
283 1.1 christos continue;
284 1.1 christos }
285 1.1 christos }
286 1.1 christos
287 1.1 christos if (ApDumpTableBuffer (Table, Instance, Address))
288 1.1 christos {
289 1.1 christos return (-1);
290 1.1 christos }
291 1.1 christos free (Table);
292 1.1 christos }
293 1.1 christos
294 1.1 christos /* Something seriously bad happened if the loop terminates here */
295 1.1 christos
296 1.1 christos return (-1);
297 1.1 christos }
298 1.1 christos
299 1.1 christos
300 1.1 christos /******************************************************************************
301 1.1 christos *
302 1.1 christos * FUNCTION: ApDumpTableByAddress
303 1.1 christos *
304 1.1 christos * PARAMETERS: AsciiAddress - Address for requested ACPI table
305 1.1 christos *
306 1.1 christos * RETURN: Status
307 1.1 christos *
308 1.1 christos * DESCRIPTION: Get an ACPI table via a physical address and dump it.
309 1.1 christos *
310 1.1 christos ******************************************************************************/
311 1.1 christos
312 1.1 christos int
313 1.1 christos ApDumpTableByAddress (
314 1.1 christos char *AsciiAddress)
315 1.1 christos {
316 1.1 christos ACPI_PHYSICAL_ADDRESS Address;
317 1.1 christos ACPI_TABLE_HEADER *Table;
318 1.1 christos ACPI_STATUS Status;
319 1.1 christos int TableStatus;
320 1.1 christos UINT64 LongAddress;
321 1.1 christos
322 1.1 christos
323 1.1 christos /* Convert argument to an integer physical address */
324 1.1 christos
325 1.1 christos Status = AcpiUtStrtoul64 (AsciiAddress, 0, &LongAddress);
326 1.1 christos if (ACPI_FAILURE (Status))
327 1.1 christos {
328 1.1 christos fprintf (stderr, "%s: Could not convert to a physical address\n",
329 1.1 christos AsciiAddress);
330 1.1 christos return (-1);
331 1.1 christos }
332 1.1 christos
333 1.1 christos Address = (ACPI_PHYSICAL_ADDRESS) LongAddress;
334 1.1 christos Status = AcpiOsGetTableByAddress (Address, &Table);
335 1.1 christos if (ACPI_FAILURE (Status))
336 1.1 christos {
337 1.1 christos fprintf (stderr, "Could not get table at 0x%8.8X%8.8X, %s\n",
338 1.1 christos ACPI_FORMAT_UINT64 (Address),
339 1.1 christos AcpiFormatException (Status));
340 1.1 christos return (-1);
341 1.1 christos }
342 1.1 christos
343 1.1 christos TableStatus = ApDumpTableBuffer (Table, 0, Address);
344 1.1 christos free (Table);
345 1.1 christos return (TableStatus);
346 1.1 christos }
347 1.1 christos
348 1.1 christos
349 1.1 christos /******************************************************************************
350 1.1 christos *
351 1.1 christos * FUNCTION: ApDumpTableByName
352 1.1 christos *
353 1.1 christos * PARAMETERS: Signature - Requested ACPI table signature
354 1.1 christos *
355 1.1 christos * RETURN: Status
356 1.1 christos *
357 1.1 christos * DESCRIPTION: Get an ACPI table via a signature and dump it. Handles
358 1.1 christos * multiple tables with the same signature (SSDTs).
359 1.1 christos *
360 1.1 christos ******************************************************************************/
361 1.1 christos
362 1.1 christos int
363 1.1 christos ApDumpTableByName (
364 1.1 christos char *Signature)
365 1.1 christos {
366 1.1 christos char LocalSignature [ACPI_NAME_SIZE + 1];
367 1.1 christos UINT32 Instance;
368 1.1 christos ACPI_TABLE_HEADER *Table;
369 1.1 christos ACPI_PHYSICAL_ADDRESS Address;
370 1.1 christos ACPI_STATUS Status;
371 1.1 christos
372 1.1 christos
373 1.1 christos if (strlen (Signature) != ACPI_NAME_SIZE)
374 1.1 christos {
375 1.1 christos fprintf (stderr,
376 1.1 christos "Invalid table signature [%s]: must be exactly 4 characters\n",
377 1.1 christos Signature);
378 1.1 christos return (-1);
379 1.1 christos }
380 1.1 christos
381 1.1 christos /* Table signatures are expected to be uppercase */
382 1.1 christos
383 1.1 christos strcpy (LocalSignature, Signature);
384 1.1 christos AcpiUtStrupr (LocalSignature);
385 1.1 christos
386 1.1 christos /* To be friendly, handle tables whose signatures do not match the name */
387 1.1 christos
388 1.1 christos if (ACPI_COMPARE_NAME (LocalSignature, AP_DUMP_SIG_RSDP))
389 1.1 christos {
390 1.1 christos strcpy (LocalSignature, AP_DUMP_SIG_RSDP);
391 1.1 christos }
392 1.1 christos else if (ACPI_COMPARE_NAME (LocalSignature, "FADT"))
393 1.1 christos {
394 1.1 christos strcpy (LocalSignature, ACPI_SIG_FADT);
395 1.1 christos }
396 1.1 christos else if (ACPI_COMPARE_NAME (LocalSignature, "MADT"))
397 1.1 christos {
398 1.1 christos strcpy (LocalSignature, ACPI_SIG_MADT);
399 1.1 christos }
400 1.1 christos
401 1.1 christos /* Dump all instances of this signature (to handle multiple SSDTs) */
402 1.1 christos
403 1.1 christos for (Instance = 0; Instance < AP_MAX_ACPI_FILES; Instance++)
404 1.1 christos {
405 1.1 christos Status = AcpiOsGetTableByName (LocalSignature, Instance,
406 1.1 christos &Table, &Address);
407 1.1 christos if (ACPI_FAILURE (Status))
408 1.1 christos {
409 1.1 christos /* AE_LIMIT means that no more tables are available */
410 1.1 christos
411 1.1 christos if (Status == AE_LIMIT)
412 1.1 christos {
413 1.1 christos return (0);
414 1.1 christos }
415 1.1 christos
416 1.1 christos fprintf (stderr,
417 1.1 christos "Could not get ACPI table with signature [%s], %s\n",
418 1.1 christos LocalSignature, AcpiFormatException (Status));
419 1.1 christos return (-1);
420 1.1 christos }
421 1.1 christos
422 1.1 christos if (ApDumpTableBuffer (Table, Instance, Address))
423 1.1 christos {
424 1.1 christos return (-1);
425 1.1 christos }
426 1.1 christos free (Table);
427 1.1 christos }
428 1.1 christos
429 1.1 christos /* Something seriously bad happened if the loop terminates here */
430 1.1 christos
431 1.1 christos return (-1);
432 1.1 christos }
433 1.1 christos
434 1.1 christos
435 1.1 christos /******************************************************************************
436 1.1 christos *
437 1.1 christos * FUNCTION: ApDumpTableFromFile
438 1.1 christos *
439 1.1 christos * PARAMETERS: Pathname - File containing the binary ACPI table
440 1.1 christos *
441 1.1 christos * RETURN: Status
442 1.1 christos *
443 1.1 christos * DESCRIPTION: Dump an ACPI table from a binary file
444 1.1 christos *
445 1.1 christos ******************************************************************************/
446 1.1 christos
447 1.1 christos int
448 1.1 christos ApDumpTableFromFile (
449 1.1 christos char *Pathname)
450 1.1 christos {
451 1.1 christos ACPI_TABLE_HEADER *Table;
452 1.1 christos UINT32 FileSize = 0;
453 1.1 christos int TableStatus;
454 1.1 christos
455 1.1 christos
456 1.1 christos /* Get the entire ACPI table from the file */
457 1.1 christos
458 1.1 christos Table = ApGetTableFromFile (Pathname, &FileSize);
459 1.1 christos if (!Table)
460 1.1 christos {
461 1.1 christos return (-1);
462 1.1 christos }
463 1.1 christos
464 1.1 christos /* File must be at least as long as the table length */
465 1.1 christos
466 1.1 christos if (Table->Length > FileSize)
467 1.1 christos {
468 1.1 christos fprintf (stderr,
469 1.1 christos "Table length (0x%X) is too large for input file (0x%X) %s\n",
470 1.1 christos Table->Length, FileSize, Pathname);
471 1.1 christos return (-1);
472 1.1 christos }
473 1.1 christos
474 1.1 christos if (Gbl_VerboseMode)
475 1.1 christos {
476 1.1 christos fprintf (stderr,
477 1.1 christos "Input file: %s contains table [%4.4s], 0x%X (%u) bytes\n",
478 1.1 christos Pathname, Table->Signature, FileSize, FileSize);
479 1.1 christos }
480 1.1 christos
481 1.1 christos TableStatus = ApDumpTableBuffer (Table, 0, 0);
482 1.1 christos free (Table);
483 1.1 christos return (TableStatus);
484 1.1 christos }
485 1.1 christos
486 1.1 christos
487 1.1 christos /******************************************************************************
488 1.1 christos *
489 1.1 christos * FUNCTION: AcpiOs* print functions
490 1.1 christos *
491 1.1 christos * DESCRIPTION: Used for linkage with ACPICA modules
492 1.1 christos *
493 1.1 christos ******************************************************************************/
494 1.1 christos
495 1.1 christos void ACPI_INTERNAL_VAR_XFACE
496 1.1 christos AcpiOsPrintf (
497 1.1 christos const char *Fmt,
498 1.1 christos ...)
499 1.1 christos {
500 1.1 christos va_list Args;
501 1.1 christos
502 1.1 christos va_start (Args, Fmt);
503 1.1 christos vfprintf (stdout, Fmt, Args);
504 1.1 christos va_end (Args);
505 1.1 christos }
506 1.1 christos
507 1.1 christos void
508 1.1 christos AcpiOsVprintf (
509 1.1 christos const char *Fmt,
510 1.1 christos va_list Args)
511 1.1 christos {
512 1.1 christos vfprintf (stdout, Fmt, Args);
513 1.1 christos }
514