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 - 2023, 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;
34 if (!stat(pathname, &stat_info)) {
36 "Target path already exists, overwrite? [y|n] ");
38 in_char = fgetc(stdin);
39 if (in_char == '\n') {
40 in_char = fgetc(stdin);
43 if (in_char != 'y' && in_char != 'Y') {
52 /******************************************************************************
54 * FUNCTION: ap_open_output_file
56 * PARAMETERS: pathname - Output filename
58 * RETURN: Open file handle
60 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
63 ******************************************************************************/
65 int ap_open_output_file(char *pathname)
69 /* If file exists, prompt for overwrite */
71 if (ap_is_existing_file(pathname) != 0) {
75 /* Point stdout to the file */
77 file = fopen(pathname, "w");
79 fprintf(stderr, "Could not open output file: %s\n", pathname);
83 /* Save the file and path */
85 gbl_output_file = file;
86 gbl_output_filename = pathname;
90 /******************************************************************************
92 * FUNCTION: ap_write_to_binary_file
94 * PARAMETERS: table - ACPI table to be written
95 * instance - ACPI table instance no. to be written
99 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
100 * filename from the table signature.
102 ******************************************************************************/
104 int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
106 char filename[ACPI_NAMESEG_SIZE + 16];
107 char instance_str[16];
112 /* Obtain table length */
114 table_length = ap_get_table_length(table);
116 /* Construct lower-case filename from the table local signature */
118 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
119 ACPI_COPY_NAMESEG(filename, ACPI_RSDP_NAME);
121 ACPI_COPY_NAMESEG(filename, table->signature);
124 filename[0] = (char)tolower((int)filename[0]);
125 filename[1] = (char)tolower((int)filename[1]);
126 filename[2] = (char)tolower((int)filename[2]);
127 filename[3] = (char)tolower((int)filename[3]);
128 filename[ACPI_NAMESEG_SIZE] = 0;
130 /* Handle multiple SSDts - create different filenames for each */
133 snprintf(instance_str, sizeof(instance_str), "%u", instance);
134 strcat(filename, instance_str);
137 strcat(filename, FILE_SUFFIX_BINARY_TABLE);
139 if (gbl_verbose_mode) {
141 "Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
142 table->signature, filename, table->length,
146 /* Open the file and dump the entire table in binary mode */
148 file = fopen(filename, "wb");
150 fprintf(stderr, "Could not open output file: %s\n", filename);
154 actual = fwrite(table, 1, table_length, file);
155 if (actual != table_length) {
156 fprintf(stderr, "Error writing binary output file: %s\n",
166 /******************************************************************************
168 * FUNCTION: ap_get_table_from_file
170 * PARAMETERS: pathname - File containing the binary ACPI table
171 * out_file_size - Where the file size is returned
173 * RETURN: Buffer containing the ACPI table. NULL on error.
175 * DESCRIPTION: Open a file and read it entirely into a new buffer
177 ******************************************************************************/
179 struct acpi_table_header *ap_get_table_from_file(char *pathname,
182 struct acpi_table_header *buffer = NULL;
187 /* Must use binary mode */
189 file = fopen(pathname, "rb");
191 fprintf(stderr, "Could not open input file: %s\n", pathname);
195 /* Need file size to allocate a buffer */
197 file_size = cm_get_file_size(file);
198 if (file_size == ACPI_UINT32_MAX) {
200 "Could not get input file size: %s\n", pathname);
204 /* Allocate a buffer for the entire file */
206 buffer = ACPI_ALLOCATE_ZEROED(file_size);
209 "Could not allocate file buffer of size: %u\n",
214 /* Read the entire file */
216 actual = fread(buffer, 1, file_size, file);
217 if (actual != file_size) {
218 fprintf(stderr, "Could not read input file: %s\n", pathname);
224 *out_file_size = file_size;