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