Commit | Line | Data |
---|---|---|
e740304c LZ |
1 | /******************************************************************************* |
2 | * | |
3 | * Module Name: utfileio - simple file I/O routines | |
4 | * | |
5 | ******************************************************************************/ | |
6 | ||
7 | /* | |
82a80941 | 8 | * Copyright (C) 2000 - 2015, Intel Corp. |
e740304c 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 <acpi/acpi.h> | |
45 | #include "accommon.h" | |
46 | #include "actables.h" | |
47 | #include "acapps.h" | |
92dc9619 | 48 | #include "errno.h" |
e740304c LZ |
49 | |
50 | #ifdef ACPI_ASL_COMPILER | |
51 | #include "aslcompiler.h" | |
52 | #endif | |
53 | ||
54 | #define _COMPONENT ACPI_CA_DEBUGGER | |
55 | ACPI_MODULE_NAME("utfileio") | |
56 | ||
57 | #ifdef ACPI_APPLICATION | |
58 | /* Local prototypes */ | |
59 | static acpi_status | |
60 | acpi_ut_check_text_mode_corruption(u8 *table, | |
61 | u32 table_length, u32 file_length); | |
62 | ||
63 | static acpi_status | |
64 | acpi_ut_read_table(FILE * fp, | |
65 | struct acpi_table_header **table, u32 *table_length); | |
66 | ||
67 | /******************************************************************************* | |
68 | * | |
69 | * FUNCTION: acpi_ut_check_text_mode_corruption | |
70 | * | |
71 | * PARAMETERS: table - Table buffer | |
72 | * table_length - Length of table from the table header | |
73 | * file_length - Length of the file that contains the table | |
74 | * | |
75 | * RETURN: Status | |
76 | * | |
77 | * DESCRIPTION: Check table for text mode file corruption where all linefeed | |
78 | * characters (LF) have been replaced by carriage return linefeed | |
79 | * pairs (CR/LF). | |
80 | * | |
81 | ******************************************************************************/ | |
82 | ||
83 | static acpi_status | |
84 | acpi_ut_check_text_mode_corruption(u8 *table, u32 table_length, u32 file_length) | |
85 | { | |
86 | u32 i; | |
87 | u32 pairs = 0; | |
88 | ||
89 | if (table_length != file_length) { | |
90 | ACPI_WARNING((AE_INFO, | |
91 | "File length (0x%X) is not the same as the table length (0x%X)", | |
92 | file_length, table_length)); | |
93 | } | |
94 | ||
95 | /* Scan entire table to determine if each LF has been prefixed with a CR */ | |
96 | ||
97 | for (i = 1; i < file_length; i++) { | |
98 | if (table[i] == 0x0A) { | |
99 | if (table[i - 1] != 0x0D) { | |
100 | ||
101 | /* The LF does not have a preceding CR, table not corrupted */ | |
102 | ||
103 | return (AE_OK); | |
104 | } else { | |
105 | /* Found a CR/LF pair */ | |
106 | ||
107 | pairs++; | |
108 | } | |
1fad8738 | 109 | |
e740304c LZ |
110 | i++; |
111 | } | |
112 | } | |
113 | ||
114 | if (!pairs) { | |
115 | return (AE_OK); | |
116 | } | |
117 | ||
118 | /* | |
119 | * Entire table scanned, each CR is part of a CR/LF pair -- | |
120 | * meaning that the table was treated as a text file somewhere. | |
121 | * | |
122 | * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the | |
123 | * original table are left untouched by the text conversion process -- | |
124 | * meaning that we cannot simply replace CR/LF pairs with LFs. | |
125 | */ | |
126 | acpi_os_printf("Table has been corrupted by text mode conversion\n"); | |
127 | acpi_os_printf("All LFs (%u) were changed to CR/LF pairs\n", pairs); | |
128 | acpi_os_printf("Table cannot be repaired!\n"); | |
1fad8738 | 129 | |
e740304c LZ |
130 | return (AE_BAD_VALUE); |
131 | } | |
132 | ||
133 | /******************************************************************************* | |
134 | * | |
135 | * FUNCTION: acpi_ut_read_table | |
136 | * | |
137 | * PARAMETERS: fp - File that contains table | |
138 | * table - Return value, buffer with table | |
139 | * table_length - Return value, length of table | |
140 | * | |
141 | * RETURN: Status | |
142 | * | |
143 | * DESCRIPTION: Load the DSDT from the file pointer | |
144 | * | |
145 | ******************************************************************************/ | |
146 | ||
147 | static acpi_status | |
148 | acpi_ut_read_table(FILE * fp, | |
149 | struct acpi_table_header **table, u32 *table_length) | |
150 | { | |
151 | struct acpi_table_header table_header; | |
152 | u32 actual; | |
153 | acpi_status status; | |
154 | u32 file_size; | |
155 | u8 standard_header = TRUE; | |
ce5eb073 | 156 | s32 count; |
e740304c LZ |
157 | |
158 | /* Get the file size */ | |
159 | ||
160 | file_size = cm_get_file_size(fp); | |
161 | if (file_size == ACPI_UINT32_MAX) { | |
162 | return (AE_ERROR); | |
163 | } | |
164 | ||
165 | if (file_size < 4) { | |
166 | return (AE_BAD_HEADER); | |
167 | } | |
168 | ||
169 | /* Read the signature */ | |
170 | ||
ce5eb073 DB |
171 | fseek(fp, 0, SEEK_SET); |
172 | ||
173 | count = fread(&table_header, 1, sizeof(struct acpi_table_header), fp); | |
174 | if (count != sizeof(struct acpi_table_header)) { | |
175 | acpi_os_printf("Could not read the table header\n"); | |
e740304c LZ |
176 | return (AE_BAD_HEADER); |
177 | } | |
178 | ||
e740304c LZ |
179 | /* The RSDP table does not have standard ACPI header */ |
180 | ||
ce5eb073 | 181 | if (ACPI_VALIDATE_RSDP_SIG(table_header.signature)) { |
e740304c LZ |
182 | *table_length = file_size; |
183 | standard_header = FALSE; | |
184 | } else { | |
e740304c | 185 | |
e740304c LZ |
186 | #if 0 |
187 | /* Validate the table header/length */ | |
188 | ||
189 | status = acpi_tb_validate_table_header(&table_header); | |
190 | if (ACPI_FAILURE(status)) { | |
191 | acpi_os_printf("Table header is invalid!\n"); | |
192 | return (status); | |
193 | } | |
194 | #endif | |
195 | ||
196 | /* File size must be at least as long as the Header-specified length */ | |
197 | ||
198 | if (table_header.length > file_size) { | |
199 | acpi_os_printf | |
200 | ("TableHeader length [0x%X] greater than the input file size [0x%X]\n", | |
201 | table_header.length, file_size); | |
202 | ||
203 | #ifdef ACPI_ASL_COMPILER | |
a7372222 BM |
204 | acpi_os_printf("File is corrupt or is ASCII text -- " |
205 | "it must be a binary file\n"); | |
e740304c LZ |
206 | #endif |
207 | return (AE_BAD_HEADER); | |
208 | } | |
209 | #ifdef ACPI_OBSOLETE_CODE | |
210 | /* We only support a limited number of table types */ | |
211 | ||
212 | if (!ACPI_COMPARE_NAME | |
213 | ((char *)table_header.signature, ACPI_SIG_DSDT) | |
214 | && !ACPI_COMPARE_NAME((char *)table_header.signature, | |
215 | ACPI_SIG_PSDT) | |
216 | && !ACPI_COMPARE_NAME((char *)table_header.signature, | |
217 | ACPI_SIG_SSDT)) { | |
218 | acpi_os_printf | |
219 | ("Table signature [%4.4s] is invalid or not supported\n", | |
220 | (char *)table_header.signature); | |
221 | ACPI_DUMP_BUFFER(&table_header, | |
222 | sizeof(struct acpi_table_header)); | |
223 | return (AE_ERROR); | |
224 | } | |
225 | #endif | |
226 | ||
227 | *table_length = table_header.length; | |
228 | } | |
229 | ||
230 | /* Allocate a buffer for the table */ | |
231 | ||
232 | *table = acpi_os_allocate((size_t) file_size); | |
233 | if (!*table) { | |
234 | acpi_os_printf | |
235 | ("Could not allocate memory for ACPI table %4.4s (size=0x%X)\n", | |
236 | table_header.signature, *table_length); | |
237 | return (AE_NO_MEMORY); | |
238 | } | |
239 | ||
240 | /* Get the rest of the table */ | |
241 | ||
242 | fseek(fp, 0, SEEK_SET); | |
243 | actual = fread(*table, 1, (size_t) file_size, fp); | |
244 | if (actual == file_size) { | |
245 | if (standard_header) { | |
246 | ||
247 | /* Now validate the checksum */ | |
248 | ||
249 | status = acpi_tb_verify_checksum((void *)*table, | |
250 | ACPI_CAST_PTR(struct | |
251 | acpi_table_header, | |
252 | *table)-> | |
253 | length); | |
254 | ||
255 | if (status == AE_BAD_CHECKSUM) { | |
256 | status = | |
257 | acpi_ut_check_text_mode_corruption((u8 *) | |
258 | *table, | |
259 | file_size, | |
260 | (*table)-> | |
261 | length); | |
262 | return (status); | |
263 | } | |
264 | } | |
265 | return (AE_OK); | |
266 | } | |
267 | ||
268 | if (actual > 0) { | |
269 | acpi_os_printf("Warning - reading table, asked for %X got %X\n", | |
270 | file_size, actual); | |
271 | return (AE_OK); | |
272 | } | |
273 | ||
274 | acpi_os_printf("Error - could not read the table file\n"); | |
275 | acpi_os_free(*table); | |
276 | *table = NULL; | |
277 | *table_length = 0; | |
278 | return (AE_ERROR); | |
279 | } | |
280 | ||
281 | /******************************************************************************* | |
282 | * | |
283 | * FUNCTION: acpi_ut_read_table_from_file | |
284 | * | |
285 | * PARAMETERS: filename - File where table is located | |
286 | * table - Where a pointer to the table is returned | |
287 | * | |
288 | * RETURN: Status | |
289 | * | |
290 | * DESCRIPTION: Get an ACPI table from a file | |
291 | * | |
292 | ******************************************************************************/ | |
293 | ||
294 | acpi_status | |
295 | acpi_ut_read_table_from_file(char *filename, struct acpi_table_header ** table) | |
296 | { | |
297 | FILE *file; | |
298 | u32 file_size; | |
299 | u32 table_length; | |
300 | acpi_status status = AE_ERROR; | |
301 | ||
302 | /* Open the file, get current size */ | |
303 | ||
304 | file = fopen(filename, "rb"); | |
305 | if (!file) { | |
306 | perror("Could not open input file"); | |
92dc9619 BM |
307 | |
308 | if (errno == ENOENT) { | |
309 | return (AE_NOT_EXIST); | |
310 | } | |
311 | ||
e740304c LZ |
312 | return (status); |
313 | } | |
314 | ||
315 | file_size = cm_get_file_size(file); | |
316 | if (file_size == ACPI_UINT32_MAX) { | |
317 | goto exit; | |
318 | } | |
319 | ||
320 | /* Get the entire file */ | |
321 | ||
322 | fprintf(stderr, | |
93cdafa0 | 323 | "Reading ACPI table from file %12s - Length %.8u (0x%06X)\n", |
e740304c LZ |
324 | filename, file_size, file_size); |
325 | ||
326 | status = acpi_ut_read_table(file, table, &table_length); | |
327 | if (ACPI_FAILURE(status)) { | |
328 | acpi_os_printf("Could not get table from the file\n"); | |
329 | } | |
330 | ||
331 | exit: | |
332 | fclose(file); | |
333 | return (status); | |
334 | } | |
335 | ||
336 | #endif |