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
147 /*******************************************************************************
149 * FUNCTION: AcpiDbCheckTextModeCorruption
151 * PARAMETERS: Table - Table buffer
152 * TableLength - Length of table from the table header
153 * FileLength - Length of the file that contains the table
157 * DESCRIPTION: Check table for text mode file corruption where all linefeed
158 * characters (LF) have been replaced by carriage return linefeed
161 ******************************************************************************/
164 AcpiDbCheckTextModeCorruption (
173 if (TableLength != FileLength)
175 ACPI_WARNING ((AE_INFO,
176 "File length (0x%X) is not the same as the table length (0x%X)",
177 FileLength, TableLength));
180 /* Scan entire table to determine if each LF has been prefixed with a CR */
182 for (i = 1; i < FileLength; i++)
184 if (Table[i] == 0x0A)
186 if (Table[i - 1] != 0x0D)
188 /* The LF does not have a preceding CR, table not corrupted */
194 /* Found a CR/LF pair */
208 * Entire table scanned, each CR is part of a CR/LF pair --
209 * meaning that the table was treated as a text file somewhere.
211 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
212 * original table are left untouched by the text conversion process --
213 * meaning that we cannot simply replace CR/LF pairs with LFs.
215 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
216 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
217 AcpiOsPrintf ("Table cannot be repaired!\n");
218 return (AE_BAD_VALUE);
222 /*******************************************************************************
224 * FUNCTION: AcpiDbReadTable
226 * PARAMETERS: fp - File that contains table
227 * Table - Return value, buffer with table
228 * TableLength - Return value, length of table
232 * DESCRIPTION: Load the DSDT from the file pointer
234 ******************************************************************************/
239 ACPI_TABLE_HEADER **Table,
242 ACPI_TABLE_HEADER TableHeader;
246 BOOLEAN StandardHeader = TRUE;
249 /* Get the file size */
251 FileSize = CmGetFileSize (fp);
252 if (FileSize == ACPI_UINT32_MAX)
259 return (AE_BAD_HEADER);
262 /* Read the signature */
264 if (fread (&TableHeader, 1, 4, fp) != 4)
266 AcpiOsPrintf ("Could not read the table signature\n");
267 return (AE_BAD_HEADER);
270 fseek (fp, 0, SEEK_SET);
272 /* The RSDP table does not have standard ACPI header */
274 if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD "))
276 *TableLength = FileSize;
277 StandardHeader = FALSE;
281 /* Read the table header */
283 if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
284 sizeof (ACPI_TABLE_HEADER))
286 AcpiOsPrintf ("Could not read the table header\n");
287 return (AE_BAD_HEADER);
291 /* Validate the table header/length */
293 Status = AcpiTbValidateTableHeader (&TableHeader);
294 if (ACPI_FAILURE (Status))
296 AcpiOsPrintf ("Table header is invalid!\n");
301 /* File size must be at least as long as the Header-specified length */
303 if (TableHeader.Length > FileSize)
306 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
307 TableHeader.Length, FileSize);
309 #ifdef ACPI_ASL_COMPILER
310 Status = FlCheckForAscii (fp, NULL, FALSE);
311 if (ACPI_SUCCESS (Status))
313 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
314 TableHeader.Length, FileSize);
317 return (AE_BAD_HEADER);
320 #ifdef ACPI_OBSOLETE_CODE
321 /* We only support a limited number of table types */
323 if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
324 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
325 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
327 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
328 (char *) TableHeader.Signature);
329 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
334 *TableLength = TableHeader.Length;
337 /* Allocate a buffer for the table */
339 *Table = AcpiOsAllocate ((size_t) FileSize);
343 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
344 TableHeader.Signature, *TableLength);
345 return (AE_NO_MEMORY);
348 /* Get the rest of the table */
350 fseek (fp, 0, SEEK_SET);
351 Actual = fread (*Table, 1, (size_t) FileSize, fp);
352 if (Actual == FileSize)
356 /* Now validate the checksum */
358 Status = AcpiTbVerifyChecksum ((void *) *Table,
359 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
361 if (Status == AE_BAD_CHECKSUM)
363 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
364 FileSize, (*Table)->Length);
373 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
378 AcpiOsPrintf ("Error - could not read the table file\n");
386 /*******************************************************************************
388 * FUNCTION: AeLocalLoadTable
390 * PARAMETERS: Table - pointer to a buffer containing the entire
395 * DESCRIPTION: This function is called to load a table from the caller's
396 * buffer. The buffer must contain an entire ACPI Table including
397 * a valid header. The header fields will be verified, and if it
398 * is determined that the table is invalid, the call will fail.
400 ******************************************************************************/
404 ACPI_TABLE_HEADER *Table)
406 ACPI_STATUS Status = AE_OK;
407 /* ACPI_TABLE_DESC TableInfo; */
410 ACPI_FUNCTION_TRACE (AeLocalLoadTable);
416 return_ACPI_STATUS (AE_BAD_PARAMETER);
419 TableInfo.Pointer = Table;
420 Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
421 if (ACPI_FAILURE (Status))
423 return_ACPI_STATUS (Status);
426 /* Install the new table into the local data structures */
428 Status = AcpiTbInitTableDescriptor (&TableInfo);
429 if (ACPI_FAILURE (Status))
431 if (Status == AE_ALREADY_EXISTS)
433 /* Table already exists, no error */
438 /* Free table allocated by AcpiTbGetTable */
440 AcpiTbDeleteSingleTable (&TableInfo);
441 return_ACPI_STATUS (Status);
444 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
446 Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
447 if (ACPI_FAILURE (Status))
449 /* Uninstall table and free the buffer */
451 AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
452 return_ACPI_STATUS (Status);
457 return_ACPI_STATUS (Status);
461 /*******************************************************************************
463 * FUNCTION: AcpiDbReadTableFromFile
465 * PARAMETERS: Filename - File where table is located
466 * Table - Where a pointer to the table is returned
470 * DESCRIPTION: Get an ACPI table from a file
472 ******************************************************************************/
475 AcpiDbReadTableFromFile (
477 ACPI_TABLE_HEADER **Table)
482 ACPI_STATUS Status = AE_ERROR;
485 /* Open the file, get current size */
487 File = fopen (Filename, "rb");
490 perror ("Could not open input file");
494 FileSize = CmGetFileSize (File);
495 if (FileSize == ACPI_UINT32_MAX)
500 /* Get the entire file */
502 fprintf (stderr, "Loading Acpi table from file %10s - Length %.8u (%06X)\n",
503 Filename, FileSize, FileSize);
505 Status = AcpiDbReadTable (File, Table, &TableLength);
506 if (ACPI_FAILURE (Status))
508 AcpiOsPrintf ("Could not get table from the file\n");
518 /*******************************************************************************
520 * FUNCTION: AcpiDbGetTableFromFile
522 * PARAMETERS: Filename - File where table is located
523 * ReturnTable - Where a pointer to the table is returned
527 * DESCRIPTION: Load an ACPI table from a file
529 ******************************************************************************/
532 AcpiDbGetTableFromFile (
534 ACPI_TABLE_HEADER **ReturnTable)
536 #ifdef ACPI_APPLICATION
538 ACPI_TABLE_HEADER *Table;
539 BOOLEAN IsAmlTable = TRUE;
542 Status = AcpiDbReadTableFromFile (Filename, &Table);
543 if (ACPI_FAILURE (Status))
548 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
549 IsAmlTable = AcpiUtIsAmlTable (Table);
554 /* Attempt to recognize and install the table */
556 Status = AeLocalLoadTable (Table);
557 if (ACPI_FAILURE (Status))
559 if (Status == AE_ALREADY_EXISTS)
561 AcpiOsPrintf ("Table %4.4s is already installed\n",
566 AcpiOsPrintf ("Could not install table, %s\n",
567 AcpiFormatException (Status));
573 AcpiTbPrintTableHeader (0, Table);
576 "Acpi table [%4.4s] successfully installed and loaded\n",
580 AcpiGbl_AcpiHardwarePresent = FALSE;
583 *ReturnTable = Table;
587 #endif /* ACPI_APPLICATION */
591 #endif /* ACPI_DEBUGGER */