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