blob: 5fcd9700ac182a631b229e12c5ff071530982bb3 [file] [log] [blame]
Lv Zheng506f57d2014-04-04 12:39:42 +08001/******************************************************************************
2 *
3 * Module Name: apfiles - File-related functions for acpidump utility
4 *
5 *****************************************************************************/
6
7/*
Bob Moorec8100dc2016-01-15 08:17:03 +08008 * Copyright (C) 2000 - 2016, Intel Corp.
Lv Zheng506f57d2014-04-04 12:39:42 +08009 * 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
Lv Zheng4d2c8222014-07-08 10:07:59 +080047/* Local prototypes */
48
49static int ap_is_existing_file(char *pathname);
50
Bob Moore1fad8732015-12-29 13:54:36 +080051/******************************************************************************
52 *
53 * FUNCTION: ap_is_existing_file
54 *
55 * PARAMETERS: pathname - Output filename
56 *
57 * RETURN: 0 on success
58 *
59 * DESCRIPTION: Query for file overwrite if it already exists.
60 *
61 ******************************************************************************/
62
Lv Zheng4d2c8222014-07-08 10:07:59 +080063static int ap_is_existing_file(char *pathname)
64{
65#ifndef _GNU_EFI
66 struct stat stat_info;
67
68 if (!stat(pathname, &stat_info)) {
69 acpi_log_error("Target path already exists, overwrite? [y|n] ");
70
71 if (getchar() != 'y') {
72 return (-1);
73 }
74 }
75#endif
76
77 return 0;
78}
79
Lv Zheng506f57d2014-04-04 12:39:42 +080080/******************************************************************************
81 *
82 * FUNCTION: ap_open_output_file
83 *
84 * PARAMETERS: pathname - Output filename
85 *
86 * RETURN: Open file handle
87 *
88 * DESCRIPTION: Open a text output file for acpidump. Checks if file already
89 * exists.
90 *
91 ******************************************************************************/
92
93int ap_open_output_file(char *pathname)
94{
Lv Zheng846d6ef2014-07-08 10:07:52 +080095 ACPI_FILE file;
Lv Zheng506f57d2014-04-04 12:39:42 +080096
97 /* If file exists, prompt for overwrite */
98
Lv Zheng4d2c8222014-07-08 10:07:59 +080099 if (ap_is_existing_file(pathname) != 0) {
100 return (-1);
Lv Zheng506f57d2014-04-04 12:39:42 +0800101 }
102
103 /* Point stdout to the file */
104
Lv Zheng846d6ef2014-07-08 10:07:52 +0800105 file = acpi_os_open_file(pathname, ACPI_FILE_WRITING);
Lv Zheng506f57d2014-04-04 12:39:42 +0800106 if (!file) {
Lv Zheng846d6ef2014-07-08 10:07:52 +0800107 acpi_log_error("Could not open output file: %s\n", pathname);
Lv Zheng506f57d2014-04-04 12:39:42 +0800108 return (-1);
109 }
110
111 /* Save the file and path */
112
113 gbl_output_file = file;
114 gbl_output_filename = pathname;
115 return (0);
116}
117
118/******************************************************************************
119 *
120 * FUNCTION: ap_write_to_binary_file
121 *
122 * PARAMETERS: table - ACPI table to be written
123 * instance - ACPI table instance no. to be written
124 *
125 * RETURN: Status
126 *
127 * DESCRIPTION: Write an ACPI table to a binary file. Builds the output
128 * filename from the table signature.
129 *
130 ******************************************************************************/
131
132int ap_write_to_binary_file(struct acpi_table_header *table, u32 instance)
133{
134 char filename[ACPI_NAME_SIZE + 16];
135 char instance_str[16];
Lv Zhengdcaff162014-07-08 10:07:46 +0800136 ACPI_FILE file;
Lv Zheng506f57d2014-04-04 12:39:42 +0800137 size_t actual;
138 u32 table_length;
139
140 /* Obtain table length */
141
142 table_length = ap_get_table_length(table);
143
144 /* Construct lower-case filename from the table local signature */
145
146 if (ACPI_VALIDATE_RSDP_SIG(table->signature)) {
147 ACPI_MOVE_NAME(filename, ACPI_RSDP_NAME);
148 } else {
149 ACPI_MOVE_NAME(filename, table->signature);
150 }
Bob Moore1fad8732015-12-29 13:54:36 +0800151
Bob Moore4fa46162015-07-01 14:45:11 +0800152 filename[0] = (char)tolower((int)filename[0]);
153 filename[1] = (char)tolower((int)filename[1]);
154 filename[2] = (char)tolower((int)filename[2]);
155 filename[3] = (char)tolower((int)filename[3]);
Lv Zheng506f57d2014-04-04 12:39:42 +0800156 filename[ACPI_NAME_SIZE] = 0;
157
158 /* Handle multiple SSDts - create different filenames for each */
159
160 if (instance > 0) {
Lv Zhengfbee6b22014-07-08 10:07:33 +0800161 acpi_ut_snprintf(instance_str, sizeof(instance_str), "%u",
162 instance);
Bob Moore4fa46162015-07-01 14:45:11 +0800163 strcat(filename, instance_str);
Lv Zheng506f57d2014-04-04 12:39:42 +0800164 }
165
Bob Moore842e71332015-10-19 10:25:04 +0800166 strcat(filename, FILE_SUFFIX_BINARY_TABLE);
Lv Zheng506f57d2014-04-04 12:39:42 +0800167
168 if (gbl_verbose_mode) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800169 acpi_log_error
170 ("Writing [%4.4s] to binary file: %s 0x%X (%u) bytes\n",
171 table->signature, filename, table->length, table->length);
Lv Zheng506f57d2014-04-04 12:39:42 +0800172 }
173
174 /* Open the file and dump the entire table in binary mode */
175
Lv Zhengdcaff162014-07-08 10:07:46 +0800176 file = acpi_os_open_file(filename,
177 ACPI_FILE_WRITING | ACPI_FILE_BINARY);
Lv Zheng506f57d2014-04-04 12:39:42 +0800178 if (!file) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800179 acpi_log_error("Could not open output file: %s\n", filename);
Lv Zheng506f57d2014-04-04 12:39:42 +0800180 return (-1);
181 }
182
Lv Zhengdcaff162014-07-08 10:07:46 +0800183 actual = acpi_os_write_file(file, table, 1, table_length);
Lv Zheng506f57d2014-04-04 12:39:42 +0800184 if (actual != table_length) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800185 acpi_log_error("Error writing binary output file: %s\n",
186 filename);
187 acpi_os_close_file(file);
Lv Zheng506f57d2014-04-04 12:39:42 +0800188 return (-1);
189 }
190
Lv Zhengdcaff162014-07-08 10:07:46 +0800191 acpi_os_close_file(file);
Lv Zheng506f57d2014-04-04 12:39:42 +0800192 return (0);
193}
194
195/******************************************************************************
196 *
197 * FUNCTION: ap_get_table_from_file
198 *
199 * PARAMETERS: pathname - File containing the binary ACPI table
200 * out_file_size - Where the file size is returned
201 *
202 * RETURN: Buffer containing the ACPI table. NULL on error.
203 *
204 * DESCRIPTION: Open a file and read it entirely into a new buffer
205 *
206 ******************************************************************************/
207
208struct acpi_table_header *ap_get_table_from_file(char *pathname,
209 u32 *out_file_size)
210{
211 struct acpi_table_header *buffer = NULL;
Lv Zhengdcaff162014-07-08 10:07:46 +0800212 ACPI_FILE file;
Lv Zheng506f57d2014-04-04 12:39:42 +0800213 u32 file_size;
214 size_t actual;
215
216 /* Must use binary mode */
217
Lv Zhengdcaff162014-07-08 10:07:46 +0800218 file =
219 acpi_os_open_file(pathname, ACPI_FILE_READING | ACPI_FILE_BINARY);
Lv Zheng506f57d2014-04-04 12:39:42 +0800220 if (!file) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800221 acpi_log_error("Could not open input file: %s\n", pathname);
Lv Zheng506f57d2014-04-04 12:39:42 +0800222 return (NULL);
223 }
224
225 /* Need file size to allocate a buffer */
226
227 file_size = cm_get_file_size(file);
228 if (file_size == ACPI_UINT32_MAX) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800229 acpi_log_error("Could not get input file size: %s\n", pathname);
Lv Zheng506f57d2014-04-04 12:39:42 +0800230 goto cleanup;
231 }
232
233 /* Allocate a buffer for the entire file */
234
Lv Zhengfbee6b22014-07-08 10:07:33 +0800235 buffer = ACPI_ALLOCATE_ZEROED(file_size);
Lv Zheng506f57d2014-04-04 12:39:42 +0800236 if (!buffer) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800237 acpi_log_error("Could not allocate file buffer of size: %u\n",
238 file_size);
Lv Zheng506f57d2014-04-04 12:39:42 +0800239 goto cleanup;
240 }
241
242 /* Read the entire file */
243
Lv Zhengdcaff162014-07-08 10:07:46 +0800244 actual = acpi_os_read_file(file, buffer, 1, file_size);
Lv Zheng506f57d2014-04-04 12:39:42 +0800245 if (actual != file_size) {
Lv Zhengdcaff162014-07-08 10:07:46 +0800246 acpi_log_error("Could not read input file: %s\n", pathname);
Lv Zhengfbee6b22014-07-08 10:07:33 +0800247 ACPI_FREE(buffer);
Lv Zheng506f57d2014-04-04 12:39:42 +0800248 buffer = NULL;
249 goto cleanup;
250 }
251
252 *out_file_size = file_size;
253
254cleanup:
Lv Zhengdcaff162014-07-08 10:07:46 +0800255 acpi_os_close_file(file);
Lv Zheng506f57d2014-04-04 12:39:42 +0800256 return (buffer);
257}