Debian Codegraph @

source/tools/acpidump/apdump.c

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Module Name: apdump - Dump routines for ACPI tables (acpidump)
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2026, Intel Corp.
9 * All rights reserved.
10 *
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.
25 *
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.
29 *
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 MERCHANTABILITY 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.
42 */
43
44 #include "acpidump.h"
45
46
47 /* Local prototypes */
48
49 static int
50 ApDumpTableBuffer (
51 ACPI_TABLE_HEADER *Table,
52 UINT32 Instance,
53 ACPI_PHYSICAL_ADDRESS Address);
54
55
56 /******************************************************************************
57 *
58 * FUNCTION: ApIsValidHeader
59 *
60 * PARAMETERS: Table - Pointer to table to be validated
61 *
62 * RETURN: TRUE if the header appears to be valid. FALSE otherwise
63 *
64 * DESCRIPTION: Check for a valid ACPI table header
65 *
66 ******************************************************************************/
67
68 BOOLEAN
69 ApIsValidHeader (
70 ACPI_TABLE_HEADER *Table)
71 {
72
73 if (!ACPI_VALIDATE_RSDP_SIG (Table->Signature))
74 {
75 /* Make sure signature is all ASCII and a valid ACPI name */
76
77 if (!AcpiUtValidNameseg (Table->Signature))
78 {
79 fprintf (stderr, "Table signature (0x%8.8X) is invalid\n",
80 *(UINT32 *) Table->Signature);
81 return (FALSE);
82 }
83
84 /* Check for minimum table length */
85
86 if (Table->Length < sizeof (ACPI_TABLE_HEADER))
87 {
88 fprintf (stderr, "Table length (0x%8.8X) is invalid\n",
89 Table->Length);
90 return (FALSE);
91 }
92 }
93
94 return (TRUE);
95 }
96
97
98 /******************************************************************************
99 *
100 * FUNCTION: ApIsValidChecksum
101 *
102 * PARAMETERS: Table - Pointer to table to be validated
103 *
104 * RETURN: TRUE if the checksum appears to be valid. FALSE otherwise.
105 *
106 * DESCRIPTION: Check for a valid ACPI table checksum.
107 *
108 ******************************************************************************/
109
110 BOOLEAN
111 ApIsValidChecksum (
112 ACPI_TABLE_HEADER *Table)
113 {
114 ACPI_STATUS Status;
115 ACPI_TABLE_RSDP *Rsdp;
116
117
118 if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
119 {
120 /*
121 * Checksum for RSDP.
122 * Note: Other checksums are computed during the table dump.
123 */
124 Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
125 Status = AcpiTbValidateRsdp (Rsdp);
126 }
127 else
128 {
129 /* We don't have to check for a CDAT here, since CDAT is not in the RSDT/XSDT */
130
131 Status = AcpiUtVerifyChecksum (Table, Table->Length);
132 }
133
134 if (ACPI_FAILURE (Status))
135 {
136 fprintf (stderr, "%4.4s: Warning: wrong checksum in table\n",
137 Table->Signature);
138 return (FALSE);
139 }
140
141 return (TRUE);
142 }
143
144
145 /******************************************************************************
146 *
147 * FUNCTION: ApGetTableLength
148 *
149 * PARAMETERS: Table - Pointer to the table
150 *
151 * RETURN: Table length
152 *
153 * DESCRIPTION: Obtain table length according to table signature.
154 *
155 ******************************************************************************/
156
157 UINT32
158 ApGetTableLength (
159 ACPI_TABLE_HEADER *Table)
160 {
161 ACPI_TABLE_RSDP *Rsdp;
162
163
164 /* Check if table is valid */
165
166 if (!ApIsValidHeader (Table))
167 {
168 return (0);
169 }
170
171 if (ACPI_VALIDATE_RSDP_SIG (Table->Signature))
172 {
173 Rsdp = ACPI_CAST_PTR (ACPI_TABLE_RSDP, Table);
174 return (AcpiTbGetRsdpLength (Rsdp));
175 }
176
177 /* Normal ACPI table */
178
179 return (Table->Length);
180 }
181
182
183 /******************************************************************************
184 *
185 * FUNCTION: ApDumpTableBuffer
186 *
187 * PARAMETERS: Table - ACPI table to be dumped
188 * Instance - ACPI table instance no. to be dumped
189 * Address - Physical address of the table
190 *
191 * RETURN: None
192 *
193 * DESCRIPTION: Dump an ACPI table in standard ASCII hex format, with a
194 * header that is compatible with the AcpiXtract utility.
195 *
196 ******************************************************************************/
197
198 static int
199 ApDumpTableBuffer (
200 ACPI_TABLE_HEADER *Table,
201 UINT32 Instance,
202 ACPI_PHYSICAL_ADDRESS Address)
203 {
204 UINT32 TableLength;
205
206
207 TableLength = ApGetTableLength (Table);
208
209 /* Print only the header if requested */
210
211 if (Gbl_SummaryMode)
212 {
213 AcpiTbPrintTableHeader (Address, Table);
214 return (0);
215 }
216
217 /* Dump to binary file if requested */
218
219 if (Gbl_BinaryMode)
220 {
221 return (ApWriteToBinaryFile (Table, Instance));
222 }
223
224 /*
225 * Dump the table with header for use with acpixtract utility.
226 * Note: simplest to just always emit a 64-bit address. AcpiXtract
227 * utility can handle this.
228 */
229 fprintf (Gbl_OutputFile, "%4.4s @ 0x%8.8X%8.8X\n",
230 Table->Signature, ACPI_FORMAT_UINT64 (Address));
231
232 AcpiUtDumpBufferToFile (Gbl_OutputFile,
233 ACPI_CAST_PTR (UINT8, Table), TableLength,
234 DB_BYTE_DISPLAY, 0);
235 fprintf (Gbl_OutputFile, "\n");
236 return (0);
237 }
238
239
240 /******************************************************************************
241 *
242 * FUNCTION: ApDumpAllTables
243 *
244 * PARAMETERS: None
245 *
246 * RETURN: Status
247 *
248 * DESCRIPTION: Get all tables from the RSDT/XSDT (or at least all of the
249 * tables that we can possibly get).
250 *
251 ******************************************************************************/
252
253 int
254 ApDumpAllTables (
255 void)
256 {
257 ACPI_TABLE_HEADER *Table;
258 UINT32 Instance = 0;
259 ACPI_PHYSICAL_ADDRESS Address;
260 ACPI_STATUS Status;
261 int TableStatus;
262 UINT32 i;
263
264
265 /* Get and dump all available ACPI tables */
266
267 for (i = 0; i < AP_MAX_ACPI_FILES; i++)
268 {
269 Status = AcpiOsGetTableByIndex (i, &Table, &Instance, &Address);
270 if (ACPI_FAILURE (Status))
271 {
272 /* AE_LIMIT means that no more tables are available */
273
274 if (Status == AE_LIMIT)
275 {
276 return (0);
277 }
278 else if (i == 0)
279 {
280 fprintf (stderr, "Could not get ACPI tables, %s\n",
281 AcpiFormatException (Status));
282 return (-1);
283 }
284 else
285 {
286 fprintf (stderr, "Could not get ACPI table at index %u, %s\n",
287 i, AcpiFormatException (Status));
288 continue;
289 }
290 }
291
292 TableStatus = ApDumpTableBuffer (Table, Instance, Address);
293 ACPI_FREE (Table);
294
295 if (TableStatus)
296 {
297 break;
298 }
299 }
300
301 /* Something seriously bad happened if the loop terminates here */
302
303 return (-1);
304 }
305
306
307 /******************************************************************************
308 *
309 * FUNCTION: ApDumpTableByAddress
310 *
311 * PARAMETERS: AsciiAddress - Address for requested ACPI table
312 *
313 * RETURN: Status
314 *
315 * DESCRIPTION: Get an ACPI table via a physical address and dump it.
316 *
317 ******************************************************************************/
318
319 int
320 ApDumpTableByAddress (
321 char *AsciiAddress)
322 {
323 ACPI_PHYSICAL_ADDRESS Address;
324 ACPI_TABLE_HEADER *Table;
325 ACPI_STATUS Status;
326 int TableStatus;
327 UINT64 LongAddress;
328
329
330 /* Convert argument to an integer physical address */
331
332 Status = AcpiUtStrtoul64 (AsciiAddress, &LongAddress);
333 if (ACPI_FAILURE (Status))
334 {
335 fprintf (stderr, "%s: Could not convert to a physical address\n",
336 AsciiAddress);
337 return (-1);
338 }
339
340 Address = (ACPI_PHYSICAL_ADDRESS) LongAddress;
341 Status = AcpiOsGetTableByAddress (Address, &Table);
342 if (ACPI_FAILURE (Status))
343 {
344 fprintf (stderr, "Could not get table at 0x%8.8X%8.8X, %s\n",
345 ACPI_FORMAT_UINT64 (Address),
346 AcpiFormatException (Status));
347 return (-1);
348 }
349
350 TableStatus = ApDumpTableBuffer (Table, 0, Address);
351 ACPI_FREE (Table);
352 return (TableStatus);
353 }
354
355
356 /******************************************************************************
357 *
358 * FUNCTION: ApDumpTableByName
359 *
360 * PARAMETERS: Signature - Requested ACPI table signature
361 *
362 * RETURN: Status
363 *
364 * DESCRIPTION: Get an ACPI table via a signature and dump it. Handles
365 * multiple tables with the same signature (SSDTs).
366 *
367 ******************************************************************************/
368
369 int
370 ApDumpTableByName (
371 char *Signature)
372 {
373 char LocalSignature [ACPI_NAMESEG_SIZE + 1];
374 UINT32 Instance;
375 ACPI_TABLE_HEADER *Table;
376 ACPI_PHYSICAL_ADDRESS Address;
377 ACPI_STATUS Status;
378 int TableStatus;
379
380
381 if (strlen (Signature) != ACPI_NAMESEG_SIZE)
382 {
383 fprintf (stderr,
384 "Invalid table signature [%s]: must be exactly 4 characters\n",
385 Signature);
386 return (-1);
387 }
388
389 /* Table signatures are expected to be uppercase */
390
391 strcpy (LocalSignature, Signature);
392 AcpiUtStrupr (LocalSignature);
393
394 /* To be friendly, handle tables whose signatures do not match the name */
395
396 if (ACPI_COMPARE_NAMESEG (LocalSignature, "FADT"))
397 {
398 strcpy (LocalSignature, ACPI_SIG_FADT);
399 }
400 else if (ACPI_COMPARE_NAMESEG (LocalSignature, "MADT"))
401 {
402 strcpy (LocalSignature, ACPI_SIG_MADT);
403 }
404
405 /* Dump all instances of this signature (to handle multiple SSDTs) */
406
407 for (Instance = 0; Instance < AP_MAX_ACPI_FILES; Instance++)
408 {
409 Status = AcpiOsGetTableByName (LocalSignature, Instance,
410 &Table, &Address);
411 if (ACPI_FAILURE (Status))
412 {
413 /* AE_LIMIT means that no more tables are available */
414
415 if (Status == AE_LIMIT)
416 {
417 return (0);
418 }
419
420 fprintf (stderr,
421 "Could not get ACPI table with signature [%s], %s\n",
422 LocalSignature, AcpiFormatException (Status));
423 return (-1);
424 }
425
426 TableStatus = ApDumpTableBuffer (Table, Instance, Address);
427 ACPI_FREE (Table);
428
429 if (TableStatus)
430 {
431 break;
432 }
433 }
434
435 /* Something seriously bad happened if the loop terminates here */
436
437 return (-1);
438 }
439
440
441 /******************************************************************************
442 *
443 * FUNCTION: ApDumpTableFromFile
444 *
445 * PARAMETERS: Pathname - File containing the binary ACPI table
446 *
447 * RETURN: Status
448 *
449 * DESCRIPTION: Dump an ACPI table from a binary file
450 *
451 ******************************************************************************/
452
453 int
454 ApDumpTableFromFile (
455 char *Pathname)
456 {
457 ACPI_TABLE_HEADER *Table;
458 UINT32 FileSize = 0;
459 int TableStatus = -1;
460
461
462 /* Get the entire ACPI table from the file */
463
464 Table = ApGetTableFromFile (Pathname, &FileSize);
465 if (!Table)
466 {
467 return (-1);
468 }
469
470 if (!AcpiUtValidNameseg (Table->Signature))
471 {
472 fprintf (stderr,
473 "No valid ACPI signature was found in input file %s\n",
474 Pathname);
475 }
476
477 /* File must be at least as long as the table length */
478
479 if (Table->Length > FileSize)
480 {
481 fprintf (stderr,
482 "Table length (0x%X) is too large for input file (0x%X) %s\n",
483 Table->Length, FileSize, Pathname);
484 goto Exit;
485 }
486
487 if (Gbl_VerboseMode)
488 {
489 fprintf (stderr,
490 "Input file: %s contains table [%4.4s], 0x%X (%u) bytes\n",
491 Pathname, Table->Signature, FileSize, FileSize);
492 }
493
494 TableStatus = ApDumpTableBuffer (Table, 0, 0);
495
496 Exit:
497 ACPI_FREE (Table);
498 return (TableStatus);
499 }