]>
Commit | Line | Data |
---|---|---|
506f57dd LZ |
1 | /****************************************************************************** |
2 | * | |
3 | * Module Name: apfiles - File-related functions for acpidump utility | |
4 | * | |
5 | *****************************************************************************/ | |
6 | ||
7 | /* | |
82a80941 | 8 | * Copyright (C) 2000 - 2015, 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" | |
45 | #include "acapps.h" | |
46 | ||
4d2c8223 LZ |
47 | /* Local prototypes */ |
48 | ||
49 | static int ap_is_existing_file(char *pathname); | |
50 | ||
51 | static int ap_is_existing_file(char *pathname) | |
52 | { | |
53 | #ifndef _GNU_EFI | |
54 | struct stat stat_info; | |
55 | ||
56 | if (!stat(pathname, &stat_info)) { | |
57 | acpi_log_error("Target path already exists, overwrite? [y|n] "); | |
58 | ||
59 | if (getchar() != 'y') { | |
60 | return (-1); | |
61 | } | |
62 | } | |
63 | #endif | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
506f57dd LZ |
68 | /****************************************************************************** |
69 | * | |
70 | * FUNCTION: ap_open_output_file | |
71 | * | |
72 | * PARAMETERS: pathname - Output filename | |
73 | * | |
74 | * RETURN: Open file handle | |
75 | * | |
76 | * DESCRIPTION: Open a text output file for acpidump. Checks if file already | |
77 | * exists. | |
78 | * | |
79 | ******************************************************************************/ | |
80 | ||
81 | int ap_open_output_file(char *pathname) | |
82 | { | |
846d6ef4 | 83 | ACPI_FILE file; |
506f57dd LZ |
84 | |
85 | /* If file exists, prompt for overwrite */ | |
86 | ||
4d2c8223 LZ |
87 | if (ap_is_existing_file(pathname) != 0) { |
88 | return (-1); | |
506f57dd LZ |
89 | } |
90 | ||
91 | /* Point stdout to the file */ | |
92 | ||
846d6ef4 | 93 | file = acpi_os_open_file(pathname, ACPI_FILE_WRITING); |
506f57dd | 94 | if (!file) { |
846d6ef4 | 95 | acpi_log_error("Could not open output file: %s\n", pathname); |
506f57dd LZ |
96 | return (-1); |
97 | } | |
98 | ||
99 | /* Save the file and path */ | |
100 | ||
101 | gbl_output_file = file; | |
102 | gbl_output_filename = pathname; | |
103 | return (0); | |
104 | } | |
105 | ||
106 | /****************************************************************************** | |
107 | * | |
108 | * FUNCTION: ap_write_to_binary_file | |
109 | * | |
110 | * PARAMETERS: table - ACPI table to be written | |
111 | * instance - ACPI table instance no. to be written | |
112 | * | |
113 | * RETURN: Status | |
114 | * | |
115 | * DESCRIPTION: Write an ACPI table to a binary file. Builds the output | |
116 | * filename from the table signature. | |
117 | * | |
118 | ******************************************************************************/ | |
119 | ||
120 | int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance) | |
121 | { | |
122 | char filename[ACPI_NAME_SIZE + 16]; | |
123 | char instance_str[16]; | |
dcaff16d | 124 | ACPI_FILE file; |
506f57dd LZ |
125 | size_t actual; |
126 | u32 table_length; | |
127 | ||
128 | /* Obtain table length */ | |
129 | ||
130 | table_length = ap_get_table_length(table); | |
131 | ||
132 | /* Construct lower-case filename from the table local signature */ | |
133 | ||
134 | if (ACPI_VALIDATE_RSDP_SIG(table->signature)) { | |
135 | ACPI_MOVE_NAME(filename, ACPI_RSDP_NAME); | |
136 | } else { | |
137 | ACPI_MOVE_NAME(filename, table->signature); | |
138 | } | |
139 | filename[0] = (char)ACPI_TOLOWER(filename[0]); | |
140 | filename[1] = (char)ACPI_TOLOWER(filename[1]); | |
141 | filename[2] = (char)ACPI_TOLOWER(filename[2]); | |
142 | filename[3] = (char)ACPI_TOLOWER(filename[3]); | |
143 | filename[ACPI_NAME_SIZE] = 0; | |
144 | ||
145 | /* Handle multiple SSDts - create different filenames for each */ | |
146 | ||
147 | if (instance > 0) { | |
fbee6b21 LZ |
148 | acpi_ut_snprintf(instance_str, sizeof(instance_str), "%u", |
149 | instance); | |
150 | ACPI_STRCAT(filename, instance_str); | |
506f57dd LZ |
151 | } |
152 | ||
fbee6b21 | 153 | ACPI_STRCAT(filename, ACPI_TABLE_FILE_SUFFIX); |
506f57dd LZ |
154 | |
155 | if (gbl_verbose_mode) { | |
dcaff16d LZ |
156 | acpi_log_error |
157 | ("Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n", | |
158 | table->signature, filename, table->length, table->length); | |
506f57dd LZ |
159 | } |
160 | ||
161 | /* Open the file and dump the entire table in binary mode */ | |
162 | ||
dcaff16d LZ |
163 | file = acpi_os_open_file(filename, |
164 | ACPI_FILE_WRITING | ACPI_FILE_BINARY); | |
506f57dd | 165 | if (!file) { |
dcaff16d | 166 | acpi_log_error("Could not open output file: %s\n", filename); |
506f57dd LZ |
167 | return (-1); |
168 | } | |
169 | ||
dcaff16d | 170 | actual = acpi_os_write_file(file, table, 1, table_length); |
506f57dd | 171 | if (actual != table_length) { |
dcaff16d LZ |
172 | acpi_log_error("Error writing binary output file: %s\n", |
173 | filename); | |
174 | acpi_os_close_file(file); | |
506f57dd LZ |
175 | return (-1); |
176 | } | |
177 | ||
dcaff16d | 178 | acpi_os_close_file(file); |
506f57dd LZ |
179 | return (0); |
180 | } | |
181 | ||
182 | /****************************************************************************** | |
183 | * | |
184 | * FUNCTION: ap_get_table_from_file | |
185 | * | |
186 | * PARAMETERS: pathname - File containing the binary ACPI table | |
187 | * out_file_size - Where the file size is returned | |
188 | * | |
189 | * RETURN: Buffer containing the ACPI table. NULL on error. | |
190 | * | |
191 | * DESCRIPTION: Open a file and read it entirely into a new buffer | |
192 | * | |
193 | ******************************************************************************/ | |
194 | ||
195 | struct acpi_table_header *ap_get_table_from_file(char *pathname, | |
196 | u32 *out_file_size) | |
197 | { | |
198 | struct acpi_table_header *buffer = NULL; | |
dcaff16d | 199 | ACPI_FILE file; |
506f57dd LZ |
200 | u32 file_size; |
201 | size_t actual; | |
202 | ||
203 | /* Must use binary mode */ | |
204 | ||
dcaff16d LZ |
205 | file = |
206 | acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY); | |
506f57dd | 207 | if (!file) { |
dcaff16d | 208 | acpi_log_error("Could not open input file: %s\n", pathname); |
506f57dd LZ |
209 | return (NULL); |
210 | } | |
211 | ||
212 | /* Need file size to allocate a buffer */ | |
213 | ||
214 | file_size = cm_get_file_size(file); | |
215 | if (file_size == ACPI_UINT32_MAX) { | |
dcaff16d | 216 | acpi_log_error("Could not get input file size: %s\n", pathname); |
506f57dd LZ |
217 | goto cleanup; |
218 | } | |
219 | ||
220 | /* Allocate a buffer for the entire file */ | |
221 | ||
fbee6b21 | 222 | buffer = ACPI_ALLOCATE_ZEROED(file_size); |
506f57dd | 223 | if (!buffer) { |
dcaff16d LZ |
224 | acpi_log_error("Could not allocate file buffer of size: %u\n", |
225 | file_size); | |
506f57dd LZ |
226 | goto cleanup; |
227 | } | |
228 | ||
229 | /* Read the entire file */ | |
230 | ||
dcaff16d | 231 | actual = acpi_os_read_file(file, buffer, 1, file_size); |
506f57dd | 232 | if (actual != file_size) { |
dcaff16d | 233 | acpi_log_error("Could not read input file: %s\n", pathname); |
fbee6b21 | 234 | ACPI_FREE(buffer); |
506f57dd LZ |
235 | buffer = NULL; |
236 | goto cleanup; | |
237 | } | |
238 | ||
239 | *out_file_size = file_size; | |
240 | ||
241 | cleanup: | |
dcaff16d | 242 | acpi_os_close_file(file); |
506f57dd LZ |
243 | return (buffer); |
244 | } |