Commit dcaff16d authored by Lv Zheng's avatar Lv Zheng Committed by Rafael J. Wysocki

ACPICA: acpidump: Replace file IOs with new APIs to improve portability

The new APIs are enabled to offer a portable layer to access files:
 1. acpi_os_XXX_file_XXX: Wrapper of fopen/fclose/fread/fwrite
 2. acpi_os_printf: Wrapper of printf
 3. acpi_log_error: Wrapper of fprintf(stderr)

This patch deploys such mechanisms to acpidump to improve the portability
of this tool. Lv Zheng.
Signed-off-by: default avatarLv Zheng <lv.zheng@intel.com>
Signed-off-by: default avatarBob Moore <robert.moore@intel.com>
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent 135610f7
...@@ -68,7 +68,7 @@ EXTERN u8 INIT_GLOBAL(gbl_verbose_mode, FALSE); ...@@ -68,7 +68,7 @@ EXTERN u8 INIT_GLOBAL(gbl_verbose_mode, FALSE);
EXTERN u8 INIT_GLOBAL(gbl_binary_mode, FALSE); EXTERN u8 INIT_GLOBAL(gbl_binary_mode, FALSE);
EXTERN u8 INIT_GLOBAL(gbl_dump_customized_tables, FALSE); EXTERN u8 INIT_GLOBAL(gbl_dump_customized_tables, FALSE);
EXTERN u8 INIT_GLOBAL(gbl_do_not_dump_xsdt, FALSE); EXTERN u8 INIT_GLOBAL(gbl_do_not_dump_xsdt, FALSE);
EXTERN FILE INIT_GLOBAL(*gbl_output_file, NULL); EXTERN ACPI_FILE INIT_GLOBAL(gbl_output_file, NULL);
EXTERN char INIT_GLOBAL(*gbl_output_filename, NULL); EXTERN char INIT_GLOBAL(*gbl_output_filename, NULL);
EXTERN u64 INIT_GLOBAL(gbl_rsdp_base, 0); EXTERN u64 INIT_GLOBAL(gbl_rsdp_base, 0);
......
...@@ -69,8 +69,7 @@ u8 ap_is_valid_header(struct acpi_table_header *table) ...@@ -69,8 +69,7 @@ u8 ap_is_valid_header(struct acpi_table_header *table)
/* Make sure signature is all ASCII and a valid ACPI name */ /* Make sure signature is all ASCII and a valid ACPI name */
if (!acpi_ut_valid_acpi_name(table->signature)) { if (!acpi_ut_valid_acpi_name(table->signature)) {
fprintf(stderr, acpi_log_error("Table signature (0x%8.8X) is invalid\n",
"Table signature (0x%8.8X) is invalid\n",
*(u32 *)table->signature); *(u32 *)table->signature);
return (FALSE); return (FALSE);
} }
...@@ -78,7 +77,7 @@ u8 ap_is_valid_header(struct acpi_table_header *table) ...@@ -78,7 +77,7 @@ u8 ap_is_valid_header(struct acpi_table_header *table)
/* Check for minimum table length */ /* Check for minimum table length */
if (table->length < sizeof(struct acpi_table_header)) { if (table->length < sizeof(struct acpi_table_header)) {
fprintf(stderr, "Table length (0x%8.8X) is invalid\n", acpi_log_error("Table length (0x%8.8X) is invalid\n",
table->length); table->length);
return (FALSE); return (FALSE);
} }
...@@ -116,7 +115,7 @@ u8 ap_is_valid_checksum(struct acpi_table_header *table) ...@@ -116,7 +115,7 @@ u8 ap_is_valid_checksum(struct acpi_table_header *table)
} }
if (ACPI_FAILURE(status)) { if (ACPI_FAILURE(status)) {
fprintf(stderr, "%4.4s: Warning: wrong checksum in table\n", acpi_log_error("%4.4s: Warning: wrong checksum in table\n",
table->signature); table->signature);
} }
...@@ -196,12 +195,12 @@ ap_dump_table_buffer(struct acpi_table_header *table, ...@@ -196,12 +195,12 @@ ap_dump_table_buffer(struct acpi_table_header *table,
* Note: simplest to just always emit a 64-bit address. acpi_xtract * Note: simplest to just always emit a 64-bit address. acpi_xtract
* utility can handle this. * utility can handle this.
*/ */
printf("%4.4s @ 0x%8.8X%8.8X\n", table->signature, acpi_os_printf("%4.4s @ 0x%8.8X%8.8X\n", table->signature,
ACPI_FORMAT_UINT64(address)); ACPI_FORMAT_UINT64(address));
acpi_ut_dump_buffer(ACPI_CAST_PTR(u8, table), table_length, acpi_ut_dump_buffer(ACPI_CAST_PTR(u8, table), table_length,
DB_BYTE_DISPLAY, 0); DB_BYTE_DISPLAY, 0);
printf("\n"); acpi_os_printf("\n");
return (0); return (0);
} }
...@@ -239,13 +238,13 @@ int ap_dump_all_tables(void) ...@@ -239,13 +238,13 @@ int ap_dump_all_tables(void)
if (status == AE_LIMIT) { if (status == AE_LIMIT) {
return (0); return (0);
} else if (i == 0) { } else if (i == 0) {
fprintf(stderr, acpi_log_error
"Could not get ACPI tables, %s\n", ("Could not get ACPI tables, %s\n",
acpi_format_exception(status)); acpi_format_exception(status));
return (-1); return (-1);
} else { } else {
fprintf(stderr, acpi_log_error
"Could not get ACPI table at index %u, %s\n", ("Could not get ACPI table at index %u, %s\n",
i, acpi_format_exception(status)); i, acpi_format_exception(status));
continue; continue;
} }
...@@ -288,7 +287,7 @@ int ap_dump_table_by_address(char *ascii_address) ...@@ -288,7 +287,7 @@ int ap_dump_table_by_address(char *ascii_address)
status = acpi_ut_strtoul64(ascii_address, 0, &long_address); status = acpi_ut_strtoul64(ascii_address, 0, &long_address);
if (ACPI_FAILURE(status)) { if (ACPI_FAILURE(status)) {
fprintf(stderr, "%s: Could not convert to a physical address\n", acpi_log_error("%s: Could not convert to a physical address\n",
ascii_address); ascii_address);
return (-1); return (-1);
} }
...@@ -296,7 +295,7 @@ int ap_dump_table_by_address(char *ascii_address) ...@@ -296,7 +295,7 @@ int ap_dump_table_by_address(char *ascii_address)
address = (acpi_physical_address) long_address; address = (acpi_physical_address) long_address;
status = acpi_os_get_table_by_address(address, &table); status = acpi_os_get_table_by_address(address, &table);
if (ACPI_FAILURE(status)) { if (ACPI_FAILURE(status)) {
fprintf(stderr, "Could not get table at 0x%8.8X%8.8X, %s\n", acpi_log_error("Could not get table at 0x%8.8X%8.8X, %s\n",
ACPI_FORMAT_UINT64(address), ACPI_FORMAT_UINT64(address),
acpi_format_exception(status)); acpi_format_exception(status));
return (-1); return (-1);
...@@ -330,8 +329,8 @@ int ap_dump_table_by_name(char *signature) ...@@ -330,8 +329,8 @@ int ap_dump_table_by_name(char *signature)
int table_status; int table_status;
if (ACPI_STRLEN(signature) != ACPI_NAME_SIZE) { if (ACPI_STRLEN(signature) != ACPI_NAME_SIZE) {
fprintf(stderr, acpi_log_error
"Invalid table signature [%s]: must be exactly 4 characters\n", ("Invalid table signature [%s]: must be exactly 4 characters\n",
signature); signature);
return (-1); return (-1);
} }
...@@ -362,8 +361,8 @@ int ap_dump_table_by_name(char *signature) ...@@ -362,8 +361,8 @@ int ap_dump_table_by_name(char *signature)
return (0); return (0);
} }
fprintf(stderr, acpi_log_error
"Could not get ACPI table with signature [%s], %s\n", ("Could not get ACPI table with signature [%s], %s\n",
local_signature, acpi_format_exception(status)); local_signature, acpi_format_exception(status));
return (-1); return (-1);
} }
...@@ -409,15 +408,15 @@ int ap_dump_table_from_file(char *pathname) ...@@ -409,15 +408,15 @@ int ap_dump_table_from_file(char *pathname)
/* File must be at least as long as the table length */ /* File must be at least as long as the table length */
if (table->length > file_size) { if (table->length > file_size) {
fprintf(stderr, acpi_log_error
"Table length (0x%X) is too large for input file (0x%X) %s\n", ("Table length (0x%X) is too large for input file (0x%X) %s\n",
table->length, file_size, pathname); table->length, file_size, pathname);
goto exit; goto exit;
} }
if (gbl_verbose_mode) { if (gbl_verbose_mode) {
fprintf(stderr, acpi_log_error
"Input file: %s contains table [%4.4s], 0x%X (%u) bytes\n", ("Input file: %s contains table [%4.4s], 0x%X (%u) bytes\n",
pathname, table->signature, file_size, file_size); pathname, table->signature, file_size, file_size);
} }
......
...@@ -65,8 +65,7 @@ int ap_open_output_file(char *pathname) ...@@ -65,8 +65,7 @@ int ap_open_output_file(char *pathname)
/* If file exists, prompt for overwrite */ /* If file exists, prompt for overwrite */
if (!stat(pathname, &stat_info)) { if (!stat(pathname, &stat_info)) {
fprintf(stderr, acpi_log_error("Target path already exists, overwrite? [y|n] ");
"Target path already exists, overwrite? [y|n] ");
if (getchar() != 'y') { if (getchar() != 'y') {
return (-1); return (-1);
...@@ -106,7 +105,7 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance) ...@@ -106,7 +105,7 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
{ {
char filename[ACPI_NAME_SIZE + 16]; char filename[ACPI_NAME_SIZE + 16];
char instance_str[16]; char instance_str[16];
FILE *file; ACPI_FILE file;
size_t actual; size_t actual;
u32 table_length; u32 table_length;
...@@ -138,28 +137,29 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance) ...@@ -138,28 +137,29 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
ACPI_STRCAT(filename, ACPI_TABLE_FILE_SUFFIX); ACPI_STRCAT(filename, ACPI_TABLE_FILE_SUFFIX);
if (gbl_verbose_mode) { if (gbl_verbose_mode) {
fprintf(stderr, acpi_log_error
"Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n", ("Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
table->signature, filename, table->length, table->signature, filename, table->length, table->length);
table->length);
} }
/* Open the file and dump the entire table in binary mode */ /* Open the file and dump the entire table in binary mode */
file = fopen(filename, "wb"); file = acpi_os_open_file(filename,
ACPI_FILE_WRITING | ACPI_FILE_BINARY);
if (!file) { if (!file) {
perror("Could not open output file"); acpi_log_error("Could not open output file: %s\n", filename);
return (-1); return (-1);
} }
actual = fwrite(table, 1, table_length, file); actual = acpi_os_write_file(file, table, 1, table_length);
if (actual != table_length) { if (actual != table_length) {
perror("Error writing binary output file"); acpi_log_error("Error writing binary output file: %s\n",
fclose(file); filename);
acpi_os_close_file(file);
return (-1); return (-1);
} }
fclose(file); acpi_os_close_file(file);
return (0); return (0);
} }
...@@ -180,15 +180,16 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname, ...@@ -180,15 +180,16 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
u32 *out_file_size) u32 *out_file_size)
{ {
struct acpi_table_header *buffer = NULL; struct acpi_table_header *buffer = NULL;
FILE *file; ACPI_FILE file;
u32 file_size; u32 file_size;
size_t actual; size_t actual;
/* Must use binary mode */ /* Must use binary mode */
file = fopen(pathname, "rb"); file =
acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY);
if (!file) { if (!file) {
perror("Could not open input file"); acpi_log_error("Could not open input file: %s\n", pathname);
return (NULL); return (NULL);
} }
...@@ -196,8 +197,7 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname, ...@@ -196,8 +197,7 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
file_size = cm_get_file_size(file); file_size = cm_get_file_size(file);
if (file_size == ACPI_UINT32_MAX) { if (file_size == ACPI_UINT32_MAX) {
fprintf(stderr, acpi_log_error("Could not get input file size: %s\n", pathname);
"Could not get input file size: %s\n", pathname);
goto cleanup; goto cleanup;
} }
...@@ -205,17 +205,16 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname, ...@@ -205,17 +205,16 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
buffer = ACPI_ALLOCATE_ZEROED(file_size); buffer = ACPI_ALLOCATE_ZEROED(file_size);
if (!buffer) { if (!buffer) {
fprintf(stderr, acpi_log_error("Could not allocate file buffer of size: %u\n",
"Could not allocate file buffer of size: %u\n",
file_size); file_size);
goto cleanup; goto cleanup;
} }
/* Read the entire file */ /* Read the entire file */
actual = fread(buffer, 1, file_size, file); actual = acpi_os_read_file(file, buffer, 1, file_size);
if (actual != file_size) { if (actual != file_size) {
fprintf(stderr, "Could not read input file: %s\n", pathname); acpi_log_error("Could not read input file: %s\n", pathname);
ACPI_FREE(buffer); ACPI_FREE(buffer);
buffer = NULL; buffer = NULL;
goto cleanup; goto cleanup;
...@@ -224,6 +223,6 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname, ...@@ -224,6 +223,6 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
*out_file_size = file_size; *out_file_size = file_size;
cleanup: cleanup:
fclose(file); acpi_os_close_file(file);
return (buffer); return (buffer);
} }
...@@ -140,7 +140,7 @@ static int ap_insert_action(char *argument, u32 to_be_done) ...@@ -140,7 +140,7 @@ static int ap_insert_action(char *argument, u32 to_be_done)
current_action++; current_action++;
if (current_action > AP_MAX_ACTIONS) { if (current_action > AP_MAX_ACTIONS) {
fprintf(stderr, "Too many table options (max %u)\n", acpi_log_error("Too many table options (max %u)\n",
AP_MAX_ACTIONS); AP_MAX_ACTIONS);
return (-1); return (-1);
} }
...@@ -203,8 +203,8 @@ static int ap_do_options(int argc, char **argv) ...@@ -203,8 +203,8 @@ static int ap_do_options(int argc, char **argv)
acpi_ut_strtoul64(acpi_gbl_optarg, 0, acpi_ut_strtoul64(acpi_gbl_optarg, 0,
&gbl_rsdp_base); &gbl_rsdp_base);
if (ACPI_FAILURE(status)) { if (ACPI_FAILURE(status)) {
fprintf(stderr, acpi_log_error
"%s: Could not convert to a physical address\n", ("%s: Could not convert to a physical address\n",
acpi_gbl_optarg); acpi_gbl_optarg);
return (-1); return (-1);
} }
...@@ -226,13 +226,13 @@ static int ap_do_options(int argc, char **argv) ...@@ -226,13 +226,13 @@ static int ap_do_options(int argc, char **argv)
case 'v': /* Revision/version */ case 'v': /* Revision/version */
printf(ACPI_COMMON_SIGNON(AP_UTILITY_NAME)); acpi_os_printf(ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
return (1); return (1);
case 'z': /* Verbose mode */ case 'z': /* Verbose mode */
gbl_verbose_mode = TRUE; gbl_verbose_mode = TRUE;
fprintf(stderr, ACPI_COMMON_SIGNON(AP_UTILITY_NAME)); acpi_log_error(ACPI_COMMON_SIGNON(AP_UTILITY_NAME));
continue; continue;
/* /*
...@@ -338,8 +338,7 @@ int ACPI_SYSTEM_XFACE main(int argc, char *argv[]) ...@@ -338,8 +338,7 @@ int ACPI_SYSTEM_XFACE main(int argc, char *argv[])
default: default:
fprintf(stderr, acpi_log_error("Internal error, invalid action: 0x%X\n",
"Internal error, invalid action: 0x%X\n",
action->to_be_done); action->to_be_done);
return (-1); return (-1);
} }
...@@ -355,12 +354,12 @@ int ACPI_SYSTEM_XFACE main(int argc, char *argv[]) ...@@ -355,12 +354,12 @@ int ACPI_SYSTEM_XFACE main(int argc, char *argv[])
/* Summary for the output file */ /* Summary for the output file */
file_size = cm_get_file_size(gbl_output_file); file_size = cm_get_file_size(gbl_output_file);
fprintf(stderr, acpi_log_error
"Output file %s contains 0x%X (%u) bytes\n\n", ("Output file %s contains 0x%X (%u) bytes\n\n",
gbl_output_filename, file_size, file_size); gbl_output_filename, file_size, file_size);
} }
fclose(gbl_output_file); acpi_os_close_file(gbl_output_file);
} }
return (status); return (status);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment