1 /*******************************************************************************
3 * Module Name: dbfileio - Debugger file I/O commands. These can't usually
4 * be used when running the debugger in Ring 0 (Kernel mode)
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2014, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
50 #ifdef ACPI_APPLICATION
54 #ifdef ACPI_ASL_COMPILER
55 #include "aslcompiler.h"
58 #if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
60 #define _COMPONENT ACPI_CA_DEBUGGER
61 ACPI_MODULE_NAME ("dbfileio")
65 /* Local prototypes */
67 #ifdef ACPI_APPLICATION
70 AcpiDbCheckTextModeCorruption (
77 /*******************************************************************************
79 * FUNCTION: AcpiDbCloseDebugFile
85 * DESCRIPTION: If open, close the current debug output file
87 ******************************************************************************/
90 AcpiDbCloseDebugFile (
94 #ifdef ACPI_APPLICATION
96 if (AcpiGbl_DebugFile)
98 fclose (AcpiGbl_DebugFile);
99 AcpiGbl_DebugFile = NULL;
100 AcpiGbl_DbOutputToFile = FALSE;
101 AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
107 /*******************************************************************************
109 * FUNCTION: AcpiDbOpenDebugFile
111 * PARAMETERS: Name - Filename to open
115 * DESCRIPTION: Open a file where debug output will be directed.
117 ******************************************************************************/
120 AcpiDbOpenDebugFile (
124 #ifdef ACPI_APPLICATION
126 AcpiDbCloseDebugFile ();
127 AcpiGbl_DebugFile = fopen (Name, "w+");
128 if (!AcpiGbl_DebugFile)
130 AcpiOsPrintf ("Could not open debug file %s\n", Name);
134 AcpiOsPrintf ("Debug output file %s opened\n", Name);
135 ACPI_STRNCPY (AcpiGbl_DbDebugFilename, Name,
136 sizeof (AcpiGbl_DbDebugFilename));
137 AcpiGbl_DbOutputToFile = TRUE;
144 #ifdef ACPI_APPLICATION
145 /*******************************************************************************
147 * FUNCTION: AcpiDbCheckTextModeCorruption
149 * PARAMETERS: Table - Table buffer
150 * TableLength - Length of table from the table header
151 * FileLength - Length of the file that contains the table
155 * DESCRIPTION: Check table for text mode file corruption where all linefeed
156 * characters (LF) have been replaced by carriage return linefeed
159 ******************************************************************************/
162 AcpiDbCheckTextModeCorruption (
171 if (TableLength != FileLength)
173 ACPI_WARNING ((AE_INFO,
174 "File length (0x%X) is not the same as the table length (0x%X)",
175 FileLength, TableLength));
178 /* Scan entire table to determine if each LF has been prefixed with a CR */
180 for (i = 1; i < FileLength; i++)
182 if (Table[i] == 0x0A)
184 if (Table[i - 1] != 0x0D)
186 /* The LF does not have a preceding CR, table not corrupted */
192 /* Found a CR/LF pair */
206 * Entire table scanned, each CR is part of a CR/LF pair --
207 * meaning that the table was treated as a text file somewhere.
209 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
210 * original table are left untouched by the text conversion process --
211 * meaning that we cannot simply replace CR/LF pairs with LFs.
213 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
214 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
215 AcpiOsPrintf ("Table cannot be repaired!\n");
216 return (AE_BAD_VALUE);
220 /*******************************************************************************
222 * FUNCTION: AcpiDbReadTable
224 * PARAMETERS: fp - File that contains table
225 * Table - Return value, buffer with table
226 * TableLength - Return value, length of table
230 * DESCRIPTION: Load the DSDT from the file pointer
232 ******************************************************************************/
237 ACPI_TABLE_HEADER **Table,
240 ACPI_TABLE_HEADER TableHeader;
244 BOOLEAN StandardHeader = TRUE;
247 /* Get the file size */
249 fseek (fp, 0, SEEK_END);
250 FileSize = (UINT32) ftell (fp);
251 fseek (fp, 0, SEEK_SET);
255 return (AE_BAD_HEADER);
258 /* Read the signature */
260 if (fread (&TableHeader, 1, 4, fp) != 4)
262 AcpiOsPrintf ("Could not read the table signature\n");
263 return (AE_BAD_HEADER);
266 fseek (fp, 0, SEEK_SET);
268 /* The RSDP table does not have standard ACPI header */
270 if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD "))
272 *TableLength = FileSize;
273 StandardHeader = FALSE;
277 /* Read the table header */
279 if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
280 sizeof (ACPI_TABLE_HEADER))
282 AcpiOsPrintf ("Could not read the table header\n");
283 return (AE_BAD_HEADER);
287 /* Validate the table header/length */
289 Status = AcpiTbValidateTableHeader (&TableHeader);
290 if (ACPI_FAILURE (Status))
292 AcpiOsPrintf ("Table header is invalid!\n");
297 /* File size must be at least as long as the Header-specified length */
299 if (TableHeader.Length > FileSize)
302 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
303 TableHeader.Length, FileSize);
305 #ifdef ACPI_ASL_COMPILER
306 Status = FlCheckForAscii (fp, NULL, FALSE);
307 if (ACPI_SUCCESS (Status))
309 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
310 TableHeader.Length, FileSize);
313 return (AE_BAD_HEADER);
316 #ifdef ACPI_OBSOLETE_CODE
317 /* We only support a limited number of table types */
319 if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
320 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
321 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
323 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
324 (char *) TableHeader.Signature);
325 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
330 *TableLength = TableHeader.Length;
333 /* Allocate a buffer for the table */
335 *Table = AcpiOsAllocate ((size_t) FileSize);
339 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
340 TableHeader.Signature, *TableLength);
341 return (AE_NO_MEMORY);
344 /* Get the rest of the table */
346 fseek (fp, 0, SEEK_SET);
347 Actual = fread (*Table, 1, (size_t) FileSize, fp);
348 if (Actual == FileSize)
352 /* Now validate the checksum */
354 Status = AcpiTbVerifyChecksum ((void *) *Table,
355 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
357 if (Status == AE_BAD_CHECKSUM)
359 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
360 FileSize, (*Table)->Length);
369 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
374 AcpiOsPrintf ("Error - could not read the table file\n");
382 /*******************************************************************************
384 * FUNCTION: AeLocalLoadTable
386 * PARAMETERS: Table - pointer to a buffer containing the entire
391 * DESCRIPTION: This function is called to load a table from the caller's
392 * buffer. The buffer must contain an entire ACPI Table including
393 * a valid header. The header fields will be verified, and if it
394 * is determined that the table is invalid, the call will fail.
396 ******************************************************************************/
400 ACPI_TABLE_HEADER *Table)
402 ACPI_STATUS Status = AE_OK;
403 /* ACPI_TABLE_DESC TableInfo; */
406 ACPI_FUNCTION_TRACE (AeLocalLoadTable);
412 return_ACPI_STATUS (AE_BAD_PARAMETER);
415 TableInfo.Pointer = Table;
416 Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
417 if (ACPI_FAILURE (Status))
419 return_ACPI_STATUS (Status);
422 /* Install the new table into the local data structures */
424 Status = AcpiTbInstallTable (&TableInfo);
425 if (ACPI_FAILURE (Status))
427 if (Status == AE_ALREADY_EXISTS)
429 /* Table already exists, no error */
434 /* Free table allocated by AcpiTbGetTable */
436 AcpiTbDeleteSingleTable (&TableInfo);
437 return_ACPI_STATUS (Status);
440 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
442 Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
443 if (ACPI_FAILURE (Status))
445 /* Uninstall table and free the buffer */
447 AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
448 return_ACPI_STATUS (Status);
453 return_ACPI_STATUS (Status);
457 /*******************************************************************************
459 * FUNCTION: AcpiDbReadTableFromFile
461 * PARAMETERS: Filename - File where table is located
462 * Table - Where a pointer to the table is returned
466 * DESCRIPTION: Get an ACPI table from a file
468 ******************************************************************************/
471 AcpiDbReadTableFromFile (
473 ACPI_TABLE_HEADER **Table)
483 File = fopen (Filename, "rb");
486 perror ("Could not open input file");
490 /* Get the file size */
492 fseek (File, 0, SEEK_END);
493 FileSize = (UINT32) ftell (File);
494 fseek (File, 0, SEEK_SET);
496 /* Get the entire file */
498 fprintf (stderr, "Loading Acpi table from file %10s - Length %.8u (%06X)\n",
499 Filename, FileSize, FileSize);
501 Status = AcpiDbReadTable (File, Table, &TableLength);
504 if (ACPI_FAILURE (Status))
506 AcpiOsPrintf ("Could not get table from the file\n");
515 /*******************************************************************************
517 * FUNCTION: AcpiDbGetTableFromFile
519 * PARAMETERS: Filename - File where table is located
520 * ReturnTable - Where a pointer to the table is returned
524 * DESCRIPTION: Load an ACPI table from a file
526 ******************************************************************************/
529 AcpiDbGetTableFromFile (
531 ACPI_TABLE_HEADER **ReturnTable)
533 #ifdef ACPI_APPLICATION
535 ACPI_TABLE_HEADER *Table;
536 BOOLEAN IsAmlTable = TRUE;
539 Status = AcpiDbReadTableFromFile (Filename, &Table);
540 if (ACPI_FAILURE (Status))
545 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
546 IsAmlTable = AcpiUtIsAmlTable (Table);
551 /* Attempt to recognize and install the table */
553 Status = AeLocalLoadTable (Table);
554 if (ACPI_FAILURE (Status))
556 if (Status == AE_ALREADY_EXISTS)
558 AcpiOsPrintf ("Table %4.4s is already installed\n",
563 AcpiOsPrintf ("Could not install table, %s\n",
564 AcpiFormatException (Status));
571 "Acpi table [%4.4s] successfully installed and loaded\n",
575 AcpiGbl_AcpiHardwarePresent = FALSE;
578 *ReturnTable = Table;
582 #endif /* ACPI_APPLICATION */
586 #endif /* ACPI_DEBUGGER */