]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/power/acpi/tools/acpidump/apfiles.c
ACPICA: MSVC9: Fix <sys/stat.h> inclusion order issue
[mirror_ubuntu-hirsute-kernel.git] / tools / power / acpi / tools / acpidump / apfiles.c
CommitLineData
506f57dd
LZ
1/******************************************************************************
2 *
3 * Module Name: apfiles - File-related functions for acpidump utility
4 *
5 *****************************************************************************/
6
7/*
c8100dc4 8 * Copyright (C) 2000 - 2016, Intel Corp.
506f57dd
LZ
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44#include "acpidump.h"
506f57dd 45
4d2c8223
LZ
46/* Local prototypes */
47
48static int ap_is_existing_file(char *pathname);
49
1fad8738
BM
50/******************************************************************************
51 *
52 * FUNCTION: ap_is_existing_file
53 *
54 * PARAMETERS: pathname - Output filename
55 *
56 * RETURN: 0 on success
57 *
58 * DESCRIPTION: Query for file overwrite if it already exists.
59 *
60 ******************************************************************************/
61
4d2c8223
LZ
62static int ap_is_existing_file(char *pathname)
63{
64#ifndef _GNU_EFI
65 struct stat stat_info;
66
67 if (!stat(pathname, &stat_info)) {
68 acpi_log_error("Target path already exists, overwrite? [y|n] ");
69
70 if (getchar() != 'y') {
71 return (-1);
72 }
73 }
74#endif
75
76 return 0;
77}
78
506f57dd
LZ
79/******************************************************************************
80 *
81 * FUNCTION: ap_open_output_file
82 *
83 * PARAMETERS: pathname - Output filename
84 *
85 * RETURN: Open file handle
86 *
87 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
88 * exists.
89 *
90 ******************************************************************************/
91
92int ap_open_output_file(char *pathname)
93{
846d6ef4 94 ACPI_FILE file;
506f57dd
LZ
95
96 /* If file exists, prompt for overwrite */
97
4d2c8223
LZ
98 if (ap_is_existing_file(pathname) != 0) {
99 return (-1);
506f57dd
LZ
100 }
101
102 /* Point stdout to the file */
103
846d6ef4 104 file = acpi_os_open_file(pathname, ACPI_FILE_WRITING);
506f57dd 105 if (!file) {
846d6ef4 106 acpi_log_error("Could not open output file: %s\n", pathname);
506f57dd
LZ
107 return (-1);
108 }
109
110 /* Save the file and path */
111
112 gbl_output_file = file;
113 gbl_output_filename = pathname;
114 return (0);
115}
116
117/******************************************************************************
118 *
119 * FUNCTION: ap_write_to_binary_file
120 *
121 * PARAMETERS: table - ACPI table to be written
122 * instance - ACPI table instance no. to be written
123 *
124 * RETURN: Status
125 *
126 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
127 * filename from the table signature.
128 *
129 ******************************************************************************/
130
131int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
132{
133 char filename[ACPI_NAME_SIZE + 16];
134 char instance_str[16];
dcaff16d 135 ACPI_FILE file;
b597664f 136 acpi_size actual;
506f57dd
LZ
137 u32 table_length;
138
139 /* Obtain table length */
140
141 table_length = ap_get_table_length(table);
142
143 /* Construct lower-case filename from the table local signature */
144
145 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
146 ACPI_MOVE_NAME(filename, ACPI_RSDP_NAME);
147 } else {
148 ACPI_MOVE_NAME(filename, table->signature);
149 }
1fad8738 150
4fa4616e
BM
151 filename[0] = (char)tolower((int)filename[0]);
152 filename[1] = (char)tolower((int)filename[1]);
153 filename[2] = (char)tolower((int)filename[2]);
154 filename[3] = (char)tolower((int)filename[3]);
506f57dd
LZ
155 filename[ACPI_NAME_SIZE] = 0;
156
157 /* Handle multiple SSDts - create different filenames for each */
158
159 if (instance > 0) {
fbee6b21
LZ
160 acpi_ut_snprintf(instance_str, sizeof(instance_str), "%u",
161 instance);
4fa4616e 162 strcat(filename, instance_str);
506f57dd
LZ
163 }
164
842e7133 165 strcat(filename, FILE_SUFFIX_BINARY_TABLE);
506f57dd
LZ
166
167 if (gbl_verbose_mode) {
dcaff16d
LZ
168 acpi_log_error
169 ("Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
170 table->signature, filename, table->length, table->length);
506f57dd
LZ
171 }
172
173 /* Open the file and dump the entire table in binary mode */
174
dcaff16d
LZ
175 file = acpi_os_open_file(filename,
176 ACPI_FILE_WRITING | ACPI_FILE_BINARY);
506f57dd 177 if (!file) {
dcaff16d 178 acpi_log_error("Could not open output file: %s\n", filename);
506f57dd
LZ
179 return (-1);
180 }
181
dcaff16d 182 actual = acpi_os_write_file(file, table, 1, table_length);
506f57dd 183 if (actual != table_length) {
dcaff16d
LZ
184 acpi_log_error("Error writing binary output file: %s\n",
185 filename);
186 acpi_os_close_file(file);
506f57dd
LZ
187 return (-1);
188 }
189
dcaff16d 190 acpi_os_close_file(file);
506f57dd
LZ
191 return (0);
192}
193
194/******************************************************************************
195 *
196 * FUNCTION: ap_get_table_from_file
197 *
198 * PARAMETERS: pathname - File containing the binary ACPI table
199 * out_file_size - Where the file size is returned
200 *
201 * RETURN: Buffer containing the ACPI table. NULL on error.
202 *
203 * DESCRIPTION: Open a file and read it entirely into a new buffer
204 *
205 ******************************************************************************/
206
207struct acpi_table_header *ap_get_table_from_file(char *pathname,
208 u32 *out_file_size)
209{
210 struct acpi_table_header *buffer = NULL;
dcaff16d 211 ACPI_FILE file;
506f57dd 212 u32 file_size;
b597664f 213 acpi_size actual;
506f57dd
LZ
214
215 /* Must use binary mode */
216
dcaff16d
LZ
217 file =
218 acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY);
506f57dd 219 if (!file) {
dcaff16d 220 acpi_log_error("Could not open input file: %s\n", pathname);
506f57dd
LZ
221 return (NULL);
222 }
223
224 /* Need file size to allocate a buffer */
225
226 file_size = cm_get_file_size(file);
227 if (file_size == ACPI_UINT32_MAX) {
dcaff16d 228 acpi_log_error("Could not get input file size: %s\n", pathname);
506f57dd
LZ
229 goto cleanup;
230 }
231
232 /* Allocate a buffer for the entire file */
233
fbee6b21 234 buffer = ACPI_ALLOCATE_ZEROED(file_size);
506f57dd 235 if (!buffer) {
dcaff16d
LZ
236 acpi_log_error("Could not allocate file buffer of size: %u\n",
237 file_size);
506f57dd
LZ
238 goto cleanup;
239 }
240
241 /* Read the entire file */
242
dcaff16d 243 actual = acpi_os_read_file(file, buffer, 1, file_size);
506f57dd 244 if (actual != file_size) {
dcaff16d 245 acpi_log_error("Could not read input file: %s\n", pathname);
fbee6b21 246 ACPI_FREE(buffer);
506f57dd
LZ
247 buffer = NULL;
248 goto cleanup;
249 }
250
251 *out_file_size = file_size;
252
253cleanup:
dcaff16d 254 acpi_os_close_file(file);
506f57dd
LZ
255 return (buffer);
256}