struct acpi_table_header *ap_get_table_from_file(char *pathname, u32 *out_file_size) { struct acpi_table_header *buffer = NULL; ACPI_FILE file; u32 file_size; size_t actual; /* Must use binary mode */ file = acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY); if (!file) { acpi_log_error("Could not open input file: %s\n", pathname); return (NULL); } /* Need file size to allocate a buffer */ file_size = cm_get_file_size(file); if (file_size == ACPI_UINT32_MAX) { acpi_log_error("Could not get input file size: %s\n", pathname); goto cleanup; } /* Allocate a buffer for the entire file */ buffer = ACPI_ALLOCATE_ZEROED(file_size); if (!buffer) { acpi_log_error("Could not allocate file buffer of size: %u\n", file_size); goto cleanup; } /* Read the entire file */ actual = acpi_os_read_file(file, buffer, 1, file_size); if (actual != file_size) { acpi_log_error("Could not read input file: %s\n", pathname); ACPI_FREE(buffer); buffer = NULL; goto cleanup; } *out_file_size = file_size; cleanup: acpi_os_close_file(file); return (buffer); }
u8 ap_is_valid_header(struct acpi_table_header *table) { if (!ACPI_VALIDATE_RSDP_SIG(table->signature)) { /* Make sure signature is all ASCII and a valid ACPI name */ if (!acpi_ut_valid_acpi_name(table->signature)) { acpi_log_error("Table signature (0x%8.8X) is invalid\n", *(u32 *)table->signature); return (FALSE); } /* Check for minimum table length */ if (table->length < sizeof(struct acpi_table_header)) { acpi_log_error("Table length (0x%8.8X) is invalid\n", table->length); return (FALSE); } } return (TRUE); }
static int ap_is_existing_file(char *pathname) { #ifndef _GNU_EFI struct stat stat_info; if (!stat(pathname, &stat_info)) { acpi_log_error("Target path already exists, overwrite? [y|n] "); if (getchar() != 'y') { return (-1); } } #endif return 0; }
static int ap_insert_action(char *argument, u32 to_be_done) { /* Insert action and check for table overflow */ action_table[current_action].argument = argument; action_table[current_action].to_be_done = to_be_done; current_action++; if (current_action > AP_MAX_ACTIONS) { acpi_log_error("Too many table options (max %u)\n", AP_MAX_ACTIONS); return (-1); } return (0); }
int ap_open_output_file(char *pathname) { ACPI_FILE file; /* If file exists, prompt for overwrite */ if (ap_is_existing_file(pathname) != 0) { return (-1); } /* Point stdout to the file */ file = acpi_os_open_file(pathname, ACPI_FILE_WRITING); if (!file) { acpi_log_error("Could not open output file: %s\n", pathname); return (-1); } /* Save the file and path */ gbl_output_file = file; gbl_output_filename = pathname; return (0); }
int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance) { char filename[ACPI_NAME_SIZE + 16]; char instance_str[16]; ACPI_FILE file; size_t actual; u32 table_length; /* Obtain table length */ table_length = ap_get_table_length(table); /* Construct lower-case filename from the table local signature */ if (ACPI_VALIDATE_RSDP_SIG(table->signature)) { ACPI_MOVE_NAME(filename, ACPI_RSDP_NAME); } else { ACPI_MOVE_NAME(filename, table->signature); } filename[0] = (char)ACPI_TOLOWER(filename[0]); filename[1] = (char)ACPI_TOLOWER(filename[1]); filename[2] = (char)ACPI_TOLOWER(filename[2]); filename[3] = (char)ACPI_TOLOWER(filename[3]); filename[ACPI_NAME_SIZE] = 0; /* Handle multiple SSDts - create different filenames for each */ if (instance > 0) { acpi_ut_snprintf(instance_str, sizeof(instance_str), "%u", instance); ACPI_STRCAT(filename, instance_str); } ACPI_STRCAT(filename, ACPI_TABLE_FILE_SUFFIX); if (gbl_verbose_mode) { acpi_log_error ("Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n", table->signature, filename, table->length, table->length); } /* Open the file and dump the entire table in binary mode */ file = acpi_os_open_file(filename, ACPI_FILE_WRITING | ACPI_FILE_BINARY); if (!file) { acpi_log_error("Could not open output file: %s\n", filename); return (-1); } actual = acpi_os_write_file(file, table, 1, table_length); if (actual != table_length) { acpi_log_error("Error writing binary output file: %s\n", filename); acpi_os_close_file(file); return (-1); } acpi_os_close_file(file); return (0); }
int ACPI_SYSTEM_XFACE acpi_main(int argc, char *argv[]) #endif { int status = 0; struct ap_dump_action *action; u32 file_size; u32 i; ACPI_DEBUG_INITIALIZE(); /* For debug version only */ acpi_os_initialize(); gbl_output_file = ACPI_FILE_OUT; /* Process command line options */ status = ap_do_options(argc, argv); if (status > 0) { return (0); } if (status < 0) { return (status); } /* Get/dump ACPI table(s) as requested */ for (i = 0; i < current_action; i++) { action = &action_table[i]; switch (action->to_be_done) { case AP_DUMP_ALL_TABLES: status = ap_dump_all_tables(); break; case AP_DUMP_TABLE_BY_ADDRESS: status = ap_dump_table_by_address(action->argument); break; case AP_DUMP_TABLE_BY_NAME: status = ap_dump_table_by_name(action->argument); break; case AP_DUMP_TABLE_BY_FILE: status = ap_dump_table_from_file(action->argument); break; default: acpi_log_error("Internal error, invalid action: 0x%X\n", action->to_be_done); return (-1); } if (status) { return (status); } } if (gbl_output_filename) { if (gbl_verbose_mode) { /* Summary for the output file */ file_size = cm_get_file_size(gbl_output_file); acpi_log_error ("Output file %s contains 0x%X (%u) bytes\n\n", gbl_output_filename, file_size, file_size); } acpi_os_close_file(gbl_output_file); } return (status); }
static int ap_do_options(int argc, char **argv) { int j; acpi_status status; /* Command line options */ while ((j = acpi_getopt(argc, argv, AP_SUPPORTED_OPTIONS)) != ACPI_OPT_END) switch (j) { /* * Global options */ case 'b': /* Dump all input tables to binary files */ gbl_binary_mode = TRUE; continue; case 'c': /* Dump customized tables */ if (!strcmp(acpi_gbl_optarg, "on")) { gbl_dump_customized_tables = TRUE; } else if (!strcmp(acpi_gbl_optarg, "off")) { gbl_dump_customized_tables = FALSE; } else { acpi_log_error ("%s: Cannot handle this switch, please use on|off\n", acpi_gbl_optarg); return (-1); } continue; case 'h': case '?': ap_display_usage(); return (1); case 'o': /* Redirect output to a single file */ if (ap_open_output_file(acpi_gbl_optarg)) { return (-1); } continue; case 'r': /* Dump tables from specified RSDP */ status = acpi_ut_strtoul64(acpi_gbl_optarg, 0, &gbl_rsdp_base); if (ACPI_FAILURE(status)) { acpi_log_error ("%s: Could not convert to a physical address\n", acpi_gbl_optarg); return (-1); } continue; case 's': /* Print table summaries only */ gbl_summary_mode = TRUE; continue; case 'x': /* Do not use XSDT */ if (!acpi_gbl_do_not_use_xsdt) { acpi_gbl_do_not_use_xsdt = TRUE; } else { gbl_do_not_dump_xsdt = TRUE; } continue; case 'v': /* Revision/version */ acpi_os_printf(ACPI_COMMON_SIGNON(AP_UTILITY_NAME)); return (1); case 'z': /* Verbose mode */ gbl_verbose_mode = TRUE; acpi_log_error(ACPI_COMMON_SIGNON(AP_UTILITY_NAME)); continue; /* * Table options */ case 'a': /* Get table by physical address */ if (ap_insert_action (acpi_gbl_optarg, AP_DUMP_TABLE_BY_ADDRESS)) { return (-1); } break; case 'f': /* Get table from a file */ if (ap_insert_action (acpi_gbl_optarg, AP_DUMP_TABLE_BY_FILE)) { return (-1); } break; case 'n': /* Get table by input name (signature) */ if (ap_insert_action (acpi_gbl_optarg, AP_DUMP_TABLE_BY_NAME)) { return (-1); } break; default: ap_display_usage(); return (-1); } /* If there are no actions, this means "get/dump all tables" */ if (current_action == 0) { if (ap_insert_action(NULL, AP_DUMP_ALL_TABLES)) { return (-1); } } return (0); }