1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: apfiles - File-related functions for acpidump utility
6 * Copyright (C) 2000 - 2018, Intel Corp.
8 *****************************************************************************/
12 /* Local prototypes */
14 static int ap_is_existing_file(char *pathname);
16 /******************************************************************************
18 * FUNCTION: ap_is_existing_file
20 * PARAMETERS: pathname - Output filename
22 * RETURN: 0 on success
24 * DESCRIPTION: Query for file overwrite if it already exists.
26 ******************************************************************************/
28 static int ap_is_existing_file(char *pathname)
30 #if !defined(_GNU_EFI) && !defined(_EDK2_EFI)
31 struct stat stat_info;
33 if (!stat(pathname, &stat_info)) {
35 "Target path already exists, overwrite? [y|n] ");
37 if (getchar() != 'y') {
46 /******************************************************************************
48 * FUNCTION: ap_open_output_file
50 * PARAMETERS: pathname - Output filename
52 * RETURN: Open file handle
54 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
57 ******************************************************************************/
59 int ap_open_output_file(char *pathname)
63 /* If file exists, prompt for overwrite */
65 if (ap_is_existing_file(pathname) != 0) {
69 /* Point stdout to the file */
71 file = fopen(pathname, "w");
73 fprintf(stderr, "Could not open output file: %s\n", pathname);
77 /* Save the file and path */
79 gbl_output_file = file;
80 gbl_output_filename = pathname;
84 /******************************************************************************
86 * FUNCTION: ap_write_to_binary_file
88 * PARAMETERS: table - ACPI table to be written
89 * instance - ACPI table instance no. to be written
93 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
94 * filename from the table signature.
96 ******************************************************************************/
98 int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
100 char filename[ACPI_NAME_SIZE + 16];
101 char instance_str[16];
106 /* Obtain table length */
108 table_length = ap_get_table_length(table);
110 /* Construct lower-case filename from the table local signature */
112 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
113 ACPI_MOVE_NAME(filename, ACPI_RSDP_NAME);
115 ACPI_MOVE_NAME(filename, table->signature);
118 filename[0] = (char)tolower((int)filename[0]);
119 filename[1] = (char)tolower((int)filename[1]);
120 filename[2] = (char)tolower((int)filename[2]);
121 filename[3] = (char)tolower((int)filename[3]);
122 filename[ACPI_NAME_SIZE] = 0;
124 /* Handle multiple SSDts - create different filenames for each */
127 snprintf(instance_str, sizeof(instance_str), "%u", instance);
128 strcat(filename, instance_str);
131 strcat(filename, FILE_SUFFIX_BINARY_TABLE);
133 if (gbl_verbose_mode) {
135 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
136 table->signature, filename, table->length,
140 /* Open the file and dump the entire table in binary mode */
142 file = fopen(filename, "wb");
144 fprintf(stderr, "Could not open output file: %s\n", filename);
148 actual = fwrite(table, 1, table_length, file);
149 if (actual != table_length) {
150 fprintf(stderr, "Error writing binary output file: %s\n",
160 /******************************************************************************
162 * FUNCTION: ap_get_table_from_file
164 * PARAMETERS: pathname - File containing the binary ACPI table
165 * out_file_size - Where the file size is returned
167 * RETURN: Buffer containing the ACPI table. NULL on error.
169 * DESCRIPTION: Open a file and read it entirely into a new buffer
171 ******************************************************************************/
173 struct acpi_table_header *ap_get_table_from_file(char *pathname,
176 struct acpi_table_header *buffer = NULL;
181 /* Must use binary mode */
183 file = fopen(pathname, "rb");
185 fprintf(stderr, "Could not open input file: %s\n", pathname);
189 /* Need file size to allocate a buffer */
191 file_size = cm_get_file_size(file);
192 if (file_size == ACPI_UINT32_MAX) {
194 "Could not get input file size: %s\n", pathname);
198 /* Allocate a buffer for the entire file */
200 buffer = ACPI_ALLOCATE_ZEROED(file_size);
203 "Could not allocate file buffer of size: %u\n",
208 /* Read the entire file */
210 actual = fread(buffer, 1, file_size, file);
211 if (actual != file_size) {
212 fprintf(stderr, "Could not read input file: %s\n", pathname);
218 *out_file_size = file_size;