aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power/acpi/tools/acpidump/apfiles.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/power/acpi/tools/acpidump/apfiles.c')
-rw-r--r--tools/power/acpi/tools/acpidump/apfiles.c92
1 files changed, 54 insertions, 38 deletions
diff --git a/tools/power/acpi/tools/acpidump/apfiles.c b/tools/power/acpi/tools/acpidump/apfiles.c
index 4488accc010b..d470046a6d81 100644
--- a/tools/power/acpi/tools/acpidump/apfiles.c
+++ b/tools/power/acpi/tools/acpidump/apfiles.c
@@ -44,6 +44,27 @@
#include "acpidump.h"
#include "acapps.h"
+/* Local prototypes */
+
+static int ap_is_existing_file(char *pathname);
+
+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;
+}
+
/******************************************************************************
*
* FUNCTION: ap_open_output_file
@@ -59,25 +80,19 @@
int ap_open_output_file(char *pathname)
{
- struct stat stat_info;
- FILE *file;
+ ACPI_FILE file;
/* If file exists, prompt for overwrite */
- if (!stat(pathname, &stat_info)) {
- fprintf(stderr,
- "Target path already exists, overwrite? [y|n] ");
-
- if (getchar() != 'y') {
- return (-1);
- }
+ if (ap_is_existing_file(pathname) != 0) {
+ return (-1);
}
/* Point stdout to the file */
- file = freopen(pathname, "w", stdout);
+ file = acpi_os_open_file(pathname, ACPI_FILE_WRITING);
if (!file) {
- perror("Could not open output file");
+ acpi_log_error("Could not open output file: %s\n", pathname);
return (-1);
}
@@ -106,7 +121,7 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
{
char filename[ACPI_NAME_SIZE + 16];
char instance_str[16];
- FILE *file;
+ ACPI_FILE file;
size_t actual;
u32 table_length;
@@ -130,35 +145,37 @@ int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
/* Handle multiple SSDts - create different filenames for each */
if (instance > 0) {
- sprintf(instance_str, "%u", instance);
- strcat(filename, instance_str);
+ acpi_ut_snprintf(instance_str, sizeof(instance_str), "%u",
+ instance);
+ ACPI_STRCAT(filename, instance_str);
}
- strcat(filename, ACPI_TABLE_FILE_SUFFIX);
+ ACPI_STRCAT(filename, ACPI_TABLE_FILE_SUFFIX);
if (gbl_verbose_mode) {
- fprintf(stderr,
- "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
- table->signature, filename, table->length,
- table->length);
+ 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 = fopen(filename, "wb");
+ file = acpi_os_open_file(filename,
+ ACPI_FILE_WRITING | ACPI_FILE_BINARY);
if (!file) {
- perror("Could not open output file");
+ acpi_log_error("Could not open output file: %s\n", filename);
return (-1);
}
- actual = fwrite(table, 1, table_length, file);
+ actual = acpi_os_write_file(file, table, 1, table_length);
if (actual != table_length) {
- perror("Error writing binary output file");
- fclose(file);
+ acpi_log_error("Error writing binary output file: %s\n",
+ filename);
+ acpi_os_close_file(file);
return (-1);
}
- fclose(file);
+ acpi_os_close_file(file);
return (0);
}
@@ -179,15 +196,16 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
u32 *out_file_size)
{
struct acpi_table_header *buffer = NULL;
- FILE *file;
+ ACPI_FILE file;
u32 file_size;
size_t actual;
/* Must use binary mode */
- file = fopen(pathname, "rb");
+ file =
+ acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY);
if (!file) {
- perror("Could not open input file");
+ acpi_log_error("Could not open input file: %s\n", pathname);
return (NULL);
}
@@ -195,27 +213,25 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
file_size = cm_get_file_size(file);
if (file_size == ACPI_UINT32_MAX) {
- fprintf(stderr,
- "Could not get input file size: %s\n", pathname);
+ acpi_log_error("Could not get input file size: %s\n", pathname);
goto cleanup;
}
/* Allocate a buffer for the entire file */
- buffer = calloc(1, file_size);
+ buffer = ACPI_ALLOCATE_ZEROED(file_size);
if (!buffer) {
- fprintf(stderr,
- "Could not allocate file buffer of size: %u\n",
- file_size);
+ acpi_log_error("Could not allocate file buffer of size: %u\n",
+ file_size);
goto cleanup;
}
/* 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) {
- fprintf(stderr, "Could not read input file: %s\n", pathname);
- free(buffer);
+ acpi_log_error("Could not read input file: %s\n", pathname);
+ ACPI_FREE(buffer);
buffer = NULL;
goto cleanup;
}
@@ -223,6 +239,6 @@ struct acpi_table_header *ap_get_table_from_file(char *pathname,
*out_file_size = file_size;
cleanup:
- fclose(file);
+ acpi_os_close_file(file);
return (buffer);
}