acfileio.c revision 1.1.1.12 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: acfileio - Get ACPI tables from file
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1.1.12 christos * Copyright (C) 2000 - 2022, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1.1.10 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #include "acpi.h"
45 1.1 christos #include "accommon.h"
46 1.1 christos #include "actables.h"
47 1.1 christos #include "acutils.h"
48 1.1.1.3 christos #include "acapps.h"
49 1.1 christos
50 1.1 christos #define _COMPONENT ACPI_UTILITIES
51 1.1 christos ACPI_MODULE_NAME ("acfileio")
52 1.1 christos
53 1.1 christos
54 1.1 christos /* Local prototypes */
55 1.1 christos
56 1.1 christos static ACPI_STATUS
57 1.1 christos AcGetOneTableFromFile (
58 1.1 christos char *Filename,
59 1.1 christos FILE *File,
60 1.1 christos UINT8 GetOnlyAmlTables,
61 1.1 christos ACPI_TABLE_HEADER **Table);
62 1.1 christos
63 1.1 christos static ACPI_STATUS
64 1.1 christos AcCheckTextModeCorruption (
65 1.1 christos ACPI_TABLE_HEADER *Table);
66 1.1 christos
67 1.1 christos
68 1.1 christos /*******************************************************************************
69 1.1 christos *
70 1.1.1.5 christos * FUNCTION: AcDeleteTableList
71 1.1.1.5 christos *
72 1.1.1.5 christos * PARAMETERS: ListHead - List to delete
73 1.1.1.5 christos *
74 1.1.1.5 christos * RETURN: Status
75 1.1.1.5 christos *
76 1.1.1.5 christos * DESCRIPTION: Delete a list of tables. This is useful for removing memory
77 1.1.1.5 christos * allocated by AcGetAllTablesFromFile
78 1.1.1.5 christos *
79 1.1.1.5 christos ******************************************************************************/
80 1.1.1.5 christos
81 1.1.1.5 christos void
82 1.1.1.5 christos AcDeleteTableList (
83 1.1.1.5 christos ACPI_NEW_TABLE_DESC *ListHead)
84 1.1.1.5 christos {
85 1.1.1.5 christos ACPI_NEW_TABLE_DESC *Current = ListHead;
86 1.1.1.5 christos ACPI_NEW_TABLE_DESC *Previous = Current;
87 1.1.1.5 christos
88 1.1.1.5 christos
89 1.1.1.5 christos while (Current)
90 1.1.1.5 christos {
91 1.1.1.5 christos Current = Current->Next;
92 1.1.1.5 christos AcpiOsFree (Previous);
93 1.1.1.5 christos Previous = Current;
94 1.1.1.5 christos }
95 1.1.1.5 christos }
96 1.1.1.5 christos
97 1.1.1.5 christos
98 1.1.1.5 christos /*******************************************************************************
99 1.1.1.5 christos *
100 1.1 christos * FUNCTION: AcGetAllTablesFromFile
101 1.1 christos *
102 1.1 christos * PARAMETERS: Filename - Table filename
103 1.1 christos * GetOnlyAmlTables - TRUE if the tables must be AML tables
104 1.1 christos * ReturnListHead - Where table list is returned
105 1.1 christos *
106 1.1 christos * RETURN: Status
107 1.1 christos *
108 1.1 christos * DESCRIPTION: Get all ACPI tables from within a single file.
109 1.1 christos *
110 1.1 christos ******************************************************************************/
111 1.1 christos
112 1.1 christos ACPI_STATUS
113 1.1 christos AcGetAllTablesFromFile (
114 1.1 christos char *Filename,
115 1.1 christos UINT8 GetOnlyAmlTables,
116 1.1 christos ACPI_NEW_TABLE_DESC **ReturnListHead)
117 1.1 christos {
118 1.1 christos ACPI_NEW_TABLE_DESC *ListHead = NULL;
119 1.1 christos ACPI_NEW_TABLE_DESC *ListTail = NULL;
120 1.1 christos ACPI_NEW_TABLE_DESC *TableDesc;
121 1.1 christos FILE *File;
122 1.1 christos ACPI_TABLE_HEADER *Table = NULL;
123 1.1 christos UINT32 FileSize;
124 1.1 christos ACPI_STATUS Status = AE_OK;
125 1.1 christos
126 1.1 christos
127 1.1 christos File = fopen (Filename, "rb");
128 1.1 christos if (!File)
129 1.1 christos {
130 1.1.1.3 christos fprintf (stderr, "Could not open input file: %s\n", Filename);
131 1.1 christos if (errno == ENOENT)
132 1.1 christos {
133 1.1 christos return (AE_NOT_EXIST);
134 1.1 christos }
135 1.1 christos
136 1.1 christos return (AE_ERROR);
137 1.1 christos }
138 1.1 christos
139 1.1 christos /* Get the file size */
140 1.1 christos
141 1.1 christos FileSize = CmGetFileSize (File);
142 1.1 christos if (FileSize == ACPI_UINT32_MAX)
143 1.1 christos {
144 1.1.1.2 christos Status = AE_ERROR;
145 1.1.1.4 christos goto Exit;
146 1.1 christos }
147 1.1 christos
148 1.1 christos fprintf (stderr,
149 1.1 christos "Input file %s, Length 0x%X (%u) bytes\n",
150 1.1 christos Filename, FileSize, FileSize);
151 1.1 christos
152 1.1 christos /* We must have at least one ACPI table header */
153 1.1 christos
154 1.1 christos if (FileSize < sizeof (ACPI_TABLE_HEADER))
155 1.1 christos {
156 1.1.1.2 christos Status = AE_BAD_HEADER;
157 1.1.1.4 christos goto Exit;
158 1.1 christos }
159 1.1 christos
160 1.1 christos /* Check for an non-binary file */
161 1.1 christos
162 1.1 christos if (!AcIsFileBinary (File))
163 1.1 christos {
164 1.1 christos fprintf (stderr,
165 1.1 christos " %s: File does not appear to contain a valid AML table\n",
166 1.1 christos Filename);
167 1.1.1.4 christos Status = AE_TYPE;
168 1.1.1.4 christos goto Exit;
169 1.1 christos }
170 1.1 christos
171 1.1 christos /* Read all tables within the file */
172 1.1 christos
173 1.1 christos while (ACPI_SUCCESS (Status))
174 1.1 christos {
175 1.1 christos /* Get one entire ACPI table */
176 1.1 christos
177 1.1 christos Status = AcGetOneTableFromFile (
178 1.1 christos Filename, File, GetOnlyAmlTables, &Table);
179 1.1 christos
180 1.1 christos if (Status == AE_CTRL_TERMINATE)
181 1.1 christos {
182 1.1 christos Status = AE_OK;
183 1.1 christos break;
184 1.1 christos }
185 1.1 christos else if (Status == AE_TYPE)
186 1.1 christos {
187 1.1.1.4 christos Status = AE_OK;
188 1.1.1.4 christos goto Exit;
189 1.1 christos }
190 1.1 christos else if (ACPI_FAILURE (Status))
191 1.1 christos {
192 1.1.1.4 christos goto Exit;
193 1.1 christos }
194 1.1 christos
195 1.1 christos /* Print table header for iASL/disassembler only */
196 1.1 christos
197 1.1 christos #ifdef ACPI_ASL_COMPILER
198 1.1 christos
199 1.1.1.4 christos AcpiTbPrintTableHeader (0, Table);
200 1.1 christos #endif
201 1.1 christos
202 1.1 christos /* Allocate and link a table descriptor */
203 1.1 christos
204 1.1 christos TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC));
205 1.1.1.4 christos if (!TableDesc)
206 1.1.1.4 christos {
207 1.1.1.4 christos AcpiOsFree (Table);
208 1.1.1.4 christos Status = AE_NO_MEMORY;
209 1.1.1.4 christos goto Exit;
210 1.1.1.4 christos }
211 1.1.1.4 christos
212 1.1 christos TableDesc->Table = Table;
213 1.1 christos TableDesc->Next = NULL;
214 1.1 christos
215 1.1 christos /* Link at the end of the local table list */
216 1.1 christos
217 1.1 christos if (!ListHead)
218 1.1 christos {
219 1.1 christos ListHead = TableDesc;
220 1.1 christos ListTail = TableDesc;
221 1.1 christos }
222 1.1 christos else
223 1.1 christos {
224 1.1 christos ListTail->Next = TableDesc;
225 1.1 christos ListTail = TableDesc;
226 1.1 christos }
227 1.1 christos }
228 1.1 christos
229 1.1 christos /* Add the local table list to the end of the global list */
230 1.1 christos
231 1.1 christos if (*ReturnListHead)
232 1.1 christos {
233 1.1 christos ListTail = *ReturnListHead;
234 1.1 christos while (ListTail->Next)
235 1.1 christos {
236 1.1 christos ListTail = ListTail->Next;
237 1.1 christos }
238 1.1 christos
239 1.1 christos ListTail->Next = ListHead;
240 1.1 christos }
241 1.1 christos else
242 1.1 christos {
243 1.1 christos *ReturnListHead = ListHead;
244 1.1 christos }
245 1.1 christos
246 1.1.1.4 christos Exit:
247 1.1 christos fclose(File);
248 1.1 christos return (Status);
249 1.1 christos }
250 1.1 christos
251 1.1 christos
252 1.1 christos /*******************************************************************************
253 1.1 christos *
254 1.1 christos * FUNCTION: AcGetOneTableFromFile
255 1.1 christos *
256 1.1 christos * PARAMETERS: Filename - File where table is located
257 1.1 christos * File - Open FILE pointer to Filename
258 1.1 christos * GetOnlyAmlTables - TRUE if the tables must be AML tables.
259 1.1 christos * ReturnTable - Where a pointer to the table is returned
260 1.1 christos *
261 1.1 christos * RETURN: Status
262 1.1 christos *
263 1.1 christos * DESCRIPTION: Read the next ACPI table from a file. Implements support
264 1.1 christos * for multiple tables within a single file. File must already
265 1.1 christos * be open.
266 1.1 christos *
267 1.1 christos * Note: Loading an RSDP is not supported.
268 1.1 christos *
269 1.1 christos ******************************************************************************/
270 1.1 christos
271 1.1 christos static ACPI_STATUS
272 1.1 christos AcGetOneTableFromFile (
273 1.1 christos char *Filename,
274 1.1 christos FILE *File,
275 1.1 christos UINT8 GetOnlyAmlTables,
276 1.1 christos ACPI_TABLE_HEADER **ReturnTable)
277 1.1 christos {
278 1.1 christos ACPI_STATUS Status = AE_OK;
279 1.1 christos ACPI_TABLE_HEADER TableHeader;
280 1.1 christos ACPI_TABLE_HEADER *Table;
281 1.1 christos INT32 Count;
282 1.1 christos long TableOffset;
283 1.1 christos
284 1.1 christos
285 1.1 christos *ReturnTable = NULL;
286 1.1 christos
287 1.1 christos /* Get the table header to examine signature and length */
288 1.1 christos
289 1.1 christos TableOffset = ftell (File);
290 1.1 christos Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
291 1.1 christos if (Count != sizeof (ACPI_TABLE_HEADER))
292 1.1 christos {
293 1.1 christos return (AE_CTRL_TERMINATE);
294 1.1 christos }
295 1.1 christos
296 1.1 christos if (GetOnlyAmlTables)
297 1.1 christos {
298 1.1.1.7 christos /* Validate the table signature/header (limited ASCII chars) */
299 1.1.1.7 christos
300 1.1.1.7 christos Status = AcValidateTableHeader (File, TableOffset);
301 1.1.1.7 christos if (ACPI_FAILURE (Status))
302 1.1.1.7 christos {
303 1.1.1.7 christos return (Status);
304 1.1.1.7 christos }
305 1.1.1.7 christos
306 1.1.1.3 christos /*
307 1.1.1.3 christos * Table must be an AML table (DSDT/SSDT).
308 1.1.1.3 christos * Used for iASL -e option only.
309 1.1.1.3 christos */
310 1.1.1.3 christos if (!AcpiUtIsAmlTable (&TableHeader))
311 1.1 christos {
312 1.1 christos fprintf (stderr,
313 1.1 christos " %s: Table [%4.4s] is not an AML table - ignoring\n",
314 1.1 christos Filename, TableHeader.Signature);
315 1.1 christos
316 1.1 christos return (AE_TYPE);
317 1.1 christos }
318 1.1 christos }
319 1.1 christos
320 1.1 christos /* Allocate a buffer for the entire table */
321 1.1 christos
322 1.1.1.3 christos Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
323 1.1 christos if (!Table)
324 1.1 christos {
325 1.1 christos return (AE_NO_MEMORY);
326 1.1 christos }
327 1.1 christos
328 1.1 christos /* Read the entire ACPI table, including header */
329 1.1 christos
330 1.1 christos fseek (File, TableOffset, SEEK_SET);
331 1.1 christos
332 1.1 christos Count = fread (Table, 1, TableHeader.Length, File);
333 1.1.1.7 christos
334 1.1.1.7 christos /*
335 1.1.1.7 christos * Checks for data table headers happen later in the execution. Only verify
336 1.1.1.7 christos * for Aml tables at this point in the code.
337 1.1.1.7 christos */
338 1.1.1.7 christos if (GetOnlyAmlTables && Count != (INT32) TableHeader.Length)
339 1.1 christos {
340 1.1 christos Status = AE_ERROR;
341 1.1 christos goto ErrorExit;
342 1.1 christos }
343 1.1 christos
344 1.1 christos /* Validate the checksum (just issue a warning) */
345 1.1 christos
346 1.1 christos Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
347 1.1 christos if (ACPI_FAILURE (Status))
348 1.1 christos {
349 1.1 christos Status = AcCheckTextModeCorruption (Table);
350 1.1 christos if (ACPI_FAILURE (Status))
351 1.1 christos {
352 1.1 christos goto ErrorExit;
353 1.1 christos }
354 1.1 christos }
355 1.1 christos
356 1.1 christos *ReturnTable = Table;
357 1.1 christos return (AE_OK);
358 1.1 christos
359 1.1 christos
360 1.1 christos ErrorExit:
361 1.1 christos AcpiOsFree (Table);
362 1.1 christos return (Status);
363 1.1 christos }
364 1.1 christos
365 1.1 christos
366 1.1 christos /*******************************************************************************
367 1.1 christos *
368 1.1 christos * FUNCTION: AcIsFileBinary
369 1.1 christos *
370 1.1 christos * PARAMETERS: File - Open input file
371 1.1 christos *
372 1.1 christos * RETURN: TRUE if file appears to be binary
373 1.1 christos *
374 1.1 christos * DESCRIPTION: Scan a file for any non-ASCII bytes.
375 1.1 christos *
376 1.1 christos * Note: Maintains current file position.
377 1.1 christos *
378 1.1 christos ******************************************************************************/
379 1.1 christos
380 1.1 christos BOOLEAN
381 1.1 christos AcIsFileBinary (
382 1.1 christos FILE *File)
383 1.1 christos {
384 1.1 christos UINT8 Byte;
385 1.1 christos BOOLEAN IsBinary = FALSE;
386 1.1 christos long FileOffset;
387 1.1 christos
388 1.1 christos
389 1.1 christos /* Scan entire file for any non-ASCII bytes */
390 1.1 christos
391 1.1 christos FileOffset = ftell (File);
392 1.1 christos while (fread (&Byte, 1, 1, File) == 1)
393 1.1 christos {
394 1.1 christos if (!isprint (Byte) && !isspace (Byte))
395 1.1 christos {
396 1.1 christos IsBinary = TRUE;
397 1.1 christos goto Exit;
398 1.1 christos }
399 1.1 christos }
400 1.1 christos
401 1.1 christos Exit:
402 1.1 christos fseek (File, FileOffset, SEEK_SET);
403 1.1 christos return (IsBinary);
404 1.1 christos }
405 1.1 christos
406 1.1 christos
407 1.1 christos /*******************************************************************************
408 1.1 christos *
409 1.1 christos * FUNCTION: AcValidateTableHeader
410 1.1 christos *
411 1.1 christos * PARAMETERS: File - Open input file
412 1.1 christos *
413 1.1 christos * RETURN: Status
414 1.1 christos *
415 1.1 christos * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI
416 1.1 christos * tables, via the
417 1.1 christos * following checks on what would be the table header:
418 1.1 christos * 1) File must be at least as long as an ACPI_TABLE_HEADER
419 1.1 christos * 2) There must be enough room in the file to hold entire table
420 1.1 christos * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII
421 1.1 christos *
422 1.1 christos * Note: There can be multiple definition blocks per file, so we cannot
423 1.1 christos * expect/compare the file size to be equal to the table length. 12/2015.
424 1.1 christos *
425 1.1 christos * Note: Maintains current file position.
426 1.1 christos *
427 1.1 christos ******************************************************************************/
428 1.1 christos
429 1.1 christos ACPI_STATUS
430 1.1 christos AcValidateTableHeader (
431 1.1 christos FILE *File,
432 1.1 christos long TableOffset)
433 1.1 christos {
434 1.1 christos ACPI_TABLE_HEADER TableHeader;
435 1.1.1.3 christos ACPI_SIZE Actual;
436 1.1 christos long OriginalOffset;
437 1.1 christos UINT32 FileSize;
438 1.1 christos UINT32 i;
439 1.1 christos
440 1.1 christos
441 1.1.1.4 christos ACPI_FUNCTION_TRACE (AcValidateTableHeader);
442 1.1 christos
443 1.1 christos
444 1.1 christos /* Read a potential table header */
445 1.1 christos
446 1.1 christos OriginalOffset = ftell (File);
447 1.1.1.11 christos if (fseek (File, TableOffset, SEEK_SET))
448 1.1.1.11 christos {
449 1.1.1.11 christos fprintf (stderr, "SEEK error\n");
450 1.1.1.11 christos }
451 1.1 christos Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File);
452 1.1.1.11 christos if (fseek (File, OriginalOffset, SEEK_SET))
453 1.1.1.11 christos {
454 1.1.1.11 christos fprintf (stderr, "SEEK error\n");
455 1.1.1.11 christos }
456 1.1 christos
457 1.1 christos if (Actual < sizeof (ACPI_TABLE_HEADER))
458 1.1 christos {
459 1.1.1.10 christos fprintf (stderr,
460 1.1.1.10 christos "Could not read entire table header: Actual %u, Requested %u\n",
461 1.1.1.10 christos (UINT32) Actual, (UINT32) sizeof (ACPI_TABLE_HEADER));
462 1.1 christos return (AE_ERROR);
463 1.1 christos }
464 1.1 christos
465 1.1 christos /* Validate the signature (limited ASCII chars) */
466 1.1 christos
467 1.1.1.2 christos if (!AcpiUtValidNameseg (TableHeader.Signature))
468 1.1 christos {
469 1.1 christos return (AE_BAD_SIGNATURE);
470 1.1 christos }
471 1.1 christos
472 1.1 christos /* Validate table length against bytes remaining in the file */
473 1.1 christos
474 1.1 christos FileSize = CmGetFileSize (File);
475 1.1 christos if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
476 1.1 christos {
477 1.1 christos fprintf (stderr, "Table [%4.4s] is too long for file - "
478 1.1 christos "needs: 0x%.2X, remaining in file: 0x%.2X\n",
479 1.1 christos TableHeader.Signature, TableHeader.Length,
480 1.1 christos (UINT32) (FileSize - TableOffset));
481 1.1 christos return (AE_BAD_HEADER);
482 1.1 christos }
483 1.1 christos
484 1.1 christos /*
485 1.1 christos * These fields must be ASCII: OemId, OemTableId, AslCompilerId.
486 1.1 christos * We allow a NULL terminator in OemId and OemTableId.
487 1.1 christos */
488 1.1.1.8 christos for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
489 1.1 christos {
490 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i]))
491 1.1 christos {
492 1.1 christos goto BadCharacters;
493 1.1 christos }
494 1.1 christos }
495 1.1 christos
496 1.1 christos for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++)
497 1.1 christos {
498 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i]))
499 1.1 christos {
500 1.1 christos goto BadCharacters;
501 1.1 christos }
502 1.1 christos }
503 1.1 christos
504 1.1 christos for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++)
505 1.1 christos {
506 1.1 christos if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i]))
507 1.1 christos {
508 1.1 christos goto BadCharacters;
509 1.1 christos }
510 1.1 christos }
511 1.1 christos
512 1.1 christos return (AE_OK);
513 1.1 christos
514 1.1 christos
515 1.1 christos BadCharacters:
516 1.1 christos
517 1.1 christos ACPI_WARNING ((AE_INFO,
518 1.1 christos "Table header for [%4.4s] has invalid ASCII character(s)",
519 1.1 christos TableHeader.Signature));
520 1.1 christos return (AE_OK);
521 1.1 christos }
522 1.1 christos
523 1.1 christos
524 1.1 christos /*******************************************************************************
525 1.1 christos *
526 1.1 christos * FUNCTION: AcCheckTextModeCorruption
527 1.1 christos *
528 1.1 christos * PARAMETERS: Table - Table buffer starting with table header
529 1.1 christos *
530 1.1 christos * RETURN: Status
531 1.1 christos *
532 1.1 christos * DESCRIPTION: Check table for text mode file corruption where all linefeed
533 1.1 christos * characters (LF) have been replaced by carriage return linefeed
534 1.1 christos * pairs (CR/LF).
535 1.1 christos *
536 1.1 christos ******************************************************************************/
537 1.1 christos
538 1.1 christos static ACPI_STATUS
539 1.1 christos AcCheckTextModeCorruption (
540 1.1 christos ACPI_TABLE_HEADER *Table)
541 1.1 christos {
542 1.1 christos UINT32 i;
543 1.1 christos UINT32 Pairs = 0;
544 1.1 christos UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table);
545 1.1 christos
546 1.1 christos
547 1.1 christos /* Scan entire table to determine if each LF has been prefixed with a CR */
548 1.1 christos
549 1.1 christos for (i = 1; i < Table->Length; i++)
550 1.1 christos {
551 1.1 christos if (Buffer[i] == 0x0A)
552 1.1 christos {
553 1.1 christos if (Buffer[i - 1] != 0x0D)
554 1.1 christos {
555 1.1 christos /* The LF does not have a preceding CR, table not corrupted */
556 1.1 christos
557 1.1 christos return (AE_OK);
558 1.1 christos }
559 1.1 christos else
560 1.1 christos {
561 1.1 christos /* Found a CR/LF pair */
562 1.1 christos
563 1.1 christos Pairs++;
564 1.1 christos }
565 1.1 christos
566 1.1 christos i++;
567 1.1 christos }
568 1.1 christos }
569 1.1 christos
570 1.1 christos if (!Pairs)
571 1.1 christos {
572 1.1 christos return (AE_OK);
573 1.1 christos }
574 1.1 christos
575 1.1 christos /*
576 1.1 christos * Entire table scanned, each CR is part of a CR/LF pair --
577 1.1 christos * meaning that the table was treated as a text file somewhere.
578 1.1 christos *
579 1.1 christos * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
580 1.1 christos * original table are left untouched by the text conversion process --
581 1.1 christos * meaning that we cannot simply replace CR/LF pairs with LFs.
582 1.1 christos */
583 1.1 christos AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
584 1.1 christos AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
585 1.1 christos AcpiOsPrintf ("Table cannot be repaired!\n");
586 1.1 christos
587 1.1 christos return (AE_BAD_VALUE);
588 1.1 christos }
589