1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: apdump - Dump routines for ACPI tables (acpidump)
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
12 /* Local prototypes */
15 ap_dump_table_buffer(struct acpi_table_header *table,
16 u32 instance, acpi_physical_address address);
18 /******************************************************************************
20 * FUNCTION: ap_is_valid_header
22 * PARAMETERS: table - Pointer to table to be validated
24 * RETURN: TRUE if the header appears to be valid. FALSE otherwise
26 * DESCRIPTION: Check for a valid ACPI table header
28 ******************************************************************************/
30 u8 ap_is_valid_header(struct acpi_table_header *table)
33 if (!ACPI_VALIDATE_RSDP_SIG(table->signature)) {
35 /* Make sure signature is all ASCII and a valid ACPI name */
37 if (!acpi_ut_valid_nameseg(table->signature)) {
39 "Table signature (0x%8.8X) is invalid\n",
40 *(u32 *)table->signature);
44 /* Check for minimum table length */
46 if (table->length < sizeof(struct acpi_table_header)) {
47 fprintf(stderr, "Table length (0x%8.8X) is invalid\n",
56 /******************************************************************************
58 * FUNCTION: ap_is_valid_checksum
60 * PARAMETERS: table - Pointer to table to be validated
62 * RETURN: TRUE if the checksum appears to be valid. FALSE otherwise.
64 * DESCRIPTION: Check for a valid ACPI table checksum.
66 ******************************************************************************/
68 u8 ap_is_valid_checksum(struct acpi_table_header *table)
71 struct acpi_table_rsdp *rsdp;
73 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
76 * Note: Other checksums are computed during the table dump.
78 rsdp = ACPI_CAST_PTR(struct acpi_table_rsdp, table);
79 status = acpi_tb_validate_rsdp(rsdp);
81 status = acpi_tb_verify_checksum(table, table->length);
84 if (ACPI_FAILURE(status)) {
85 fprintf(stderr, "%4.4s: Warning: wrong checksum in table\n",
92 /******************************************************************************
94 * FUNCTION: ap_get_table_length
96 * PARAMETERS: table - Pointer to the table
98 * RETURN: Table length
100 * DESCRIPTION: Obtain table length according to table signature.
102 ******************************************************************************/
104 u32 ap_get_table_length(struct acpi_table_header *table)
106 struct acpi_table_rsdp *rsdp;
108 /* Check if table is valid */
110 if (!ap_is_valid_header(table)) {
114 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
115 rsdp = ACPI_CAST_PTR(struct acpi_table_rsdp, table);
116 return (acpi_tb_get_rsdp_length(rsdp));
119 /* Normal ACPI table */
121 return (table->length);
124 /******************************************************************************
126 * FUNCTION: ap_dump_table_buffer
128 * PARAMETERS: table - ACPI table to be dumped
129 * instance - ACPI table instance no. to be dumped
130 * address - Physical address of the table
134 * DESCRIPTION: Dump an ACPI table in standard ASCII hex format, with a
135 * header that is compatible with the acpi_xtract utility.
137 ******************************************************************************/
140 ap_dump_table_buffer(struct acpi_table_header *table,
141 u32 instance, acpi_physical_address address)
145 table_length = ap_get_table_length(table);
147 /* Print only the header if requested */
149 if (gbl_summary_mode) {
150 acpi_tb_print_table_header(address, table);
154 /* Dump to binary file if requested */
156 if (gbl_binary_mode) {
157 return (ap_write_to_binary_file(table, instance));
161 * Dump the table with header for use with acpixtract utility.
162 * Note: simplest to just always emit a 64-bit address. acpi_xtract
163 * utility can handle this.
165 fprintf(gbl_output_file, "%4.4s @ 0x%8.8X%8.8X\n",
166 table->signature, ACPI_FORMAT_UINT64(address));
168 acpi_ut_dump_buffer_to_file(gbl_output_file,
169 ACPI_CAST_PTR(u8, table), table_length,
171 fprintf(gbl_output_file, "\n");
175 /******************************************************************************
177 * FUNCTION: ap_dump_all_tables
183 * DESCRIPTION: Get all tables from the RSDT/XSDT (or at least all of the
184 * tables that we can possibly get).
186 ******************************************************************************/
188 int ap_dump_all_tables(void)
190 struct acpi_table_header *table;
192 acpi_physical_address address;
197 /* Get and dump all available ACPI tables */
199 for (i = 0; i < AP_MAX_ACPI_FILES; i++) {
201 acpi_os_get_table_by_index(i, &table, &instance, &address);
202 if (ACPI_FAILURE(status)) {
204 /* AE_LIMIT means that no more tables are available */
206 if (status == AE_LIMIT) {
210 "Could not get ACPI tables, %s\n",
211 acpi_format_exception(status));
215 "Could not get ACPI table at index %u, %s\n",
216 i, acpi_format_exception(status));
221 table_status = ap_dump_table_buffer(table, instance, address);
229 /* Something seriously bad happened if the loop terminates here */
234 /******************************************************************************
236 * FUNCTION: ap_dump_table_by_address
238 * PARAMETERS: ascii_address - Address for requested ACPI table
242 * DESCRIPTION: Get an ACPI table via a physical address and dump it.
244 ******************************************************************************/
246 int ap_dump_table_by_address(char *ascii_address)
248 acpi_physical_address address;
249 struct acpi_table_header *table;
254 /* Convert argument to an integer physical address */
256 status = acpi_ut_strtoul64(ascii_address, &long_address);
257 if (ACPI_FAILURE(status)) {
258 fprintf(stderr, "%s: Could not convert to a physical address\n",
263 address = (acpi_physical_address)long_address;
264 status = acpi_os_get_table_by_address(address, &table);
265 if (ACPI_FAILURE(status)) {
266 fprintf(stderr, "Could not get table at 0x%8.8X%8.8X, %s\n",
267 ACPI_FORMAT_UINT64(address),
268 acpi_format_exception(status));
272 table_status = ap_dump_table_buffer(table, 0, address);
274 return (table_status);
277 /******************************************************************************
279 * FUNCTION: ap_dump_table_by_name
281 * PARAMETERS: signature - Requested ACPI table signature
285 * DESCRIPTION: Get an ACPI table via a signature and dump it. Handles
286 * multiple tables with the same signature (SSDTs).
288 ******************************************************************************/
290 int ap_dump_table_by_name(char *signature)
292 char local_signature[ACPI_NAME_SIZE + 1];
294 struct acpi_table_header *table;
295 acpi_physical_address address;
299 if (strlen(signature) != ACPI_NAME_SIZE) {
301 "Invalid table signature [%s]: must be exactly 4 characters\n",
306 /* Table signatures are expected to be uppercase */
308 strcpy(local_signature, signature);
309 acpi_ut_strupr(local_signature);
311 /* To be friendly, handle tables whose signatures do not match the name */
313 if (ACPI_COMPARE_NAME(local_signature, "FADT")) {
314 strcpy(local_signature, ACPI_SIG_FADT);
315 } else if (ACPI_COMPARE_NAME(local_signature, "MADT")) {
316 strcpy(local_signature, ACPI_SIG_MADT);
319 /* Dump all instances of this signature (to handle multiple SSDTs) */
321 for (instance = 0; instance < AP_MAX_ACPI_FILES; instance++) {
322 status = acpi_os_get_table_by_name(local_signature, instance,
324 if (ACPI_FAILURE(status)) {
326 /* AE_LIMIT means that no more tables are available */
328 if (status == AE_LIMIT) {
333 "Could not get ACPI table with signature [%s], %s\n",
334 local_signature, acpi_format_exception(status));
338 table_status = ap_dump_table_buffer(table, instance, address);
346 /* Something seriously bad happened if the loop terminates here */
351 /******************************************************************************
353 * FUNCTION: ap_dump_table_from_file
355 * PARAMETERS: pathname - File containing the binary ACPI table
359 * DESCRIPTION: Dump an ACPI table from a binary file
361 ******************************************************************************/
363 int ap_dump_table_from_file(char *pathname)
365 struct acpi_table_header *table;
367 int table_status = -1;
369 /* Get the entire ACPI table from the file */
371 table = ap_get_table_from_file(pathname, &file_size);
376 if (!acpi_ut_valid_nameseg(table->signature)) {
378 "No valid ACPI signature was found in input file %s\n",
382 /* File must be at least as long as the table length */
384 if (table->length > file_size) {
386 "Table length (0x%X) is too large for input file (0x%X) %s\n",
387 table->length, file_size, pathname);
391 if (gbl_verbose_mode) {
393 "Input file: %s contains table [%4.4s], 0x%X (%u) bytes\n",
394 pathname, table->signature, file_size, file_size);
397 table_status = ap_dump_table_buffer(table, 0, 0);
401 return (table_status);