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 - 2011, 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.
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acdebug.h>
50 #ifdef ACPI_APPLICATION
51 #include <contrib/dev/acpica/include/actables.h>
54 #if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER)
56 #define _COMPONENT ACPI_CA_DEBUGGER
57 ACPI_MODULE_NAME ("dbfileio")
60 * NOTE: this is here for lack of a better place. It is used in all
61 * flavors of the debugger, need LCD file
63 #ifdef ACPI_APPLICATION
65 FILE *AcpiGbl_DebugFile = NULL;
71 /* Local prototypes */
73 #ifdef ACPI_APPLICATION
76 AcpiDbCheckTextModeCorruption (
83 /*******************************************************************************
85 * FUNCTION: AcpiDbCloseDebugFile
91 * DESCRIPTION: If open, close the current debug output file
93 ******************************************************************************/
96 AcpiDbCloseDebugFile (
100 #ifdef ACPI_APPLICATION
102 if (AcpiGbl_DebugFile)
104 fclose (AcpiGbl_DebugFile);
105 AcpiGbl_DebugFile = NULL;
106 AcpiGbl_DbOutputToFile = FALSE;
107 AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
113 /*******************************************************************************
115 * FUNCTION: AcpiDbOpenDebugFile
117 * PARAMETERS: Name - Filename to open
121 * DESCRIPTION: Open a file where debug output will be directed.
123 ******************************************************************************/
126 AcpiDbOpenDebugFile (
130 #ifdef ACPI_APPLICATION
132 AcpiDbCloseDebugFile ();
133 AcpiGbl_DebugFile = fopen (Name, "w+");
134 if (AcpiGbl_DebugFile)
136 AcpiOsPrintf ("Debug output file %s opened\n", Name);
137 ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name);
138 AcpiGbl_DbOutputToFile = TRUE;
142 AcpiOsPrintf ("Could not open debug file %s\n", Name);
150 #ifdef ACPI_APPLICATION
151 /*******************************************************************************
153 * FUNCTION: AcpiDbCheckTextModeCorruption
155 * PARAMETERS: Table - Table buffer
156 * TableLength - Length of table from the table header
157 * FileLength - Length of the file that contains the table
161 * DESCRIPTION: Check table for text mode file corruption where all linefeed
162 * characters (LF) have been replaced by carriage return linefeed
165 ******************************************************************************/
168 AcpiDbCheckTextModeCorruption (
177 if (TableLength != FileLength)
179 ACPI_WARNING ((AE_INFO,
180 "File length (0x%X) is not the same as the table length (0x%X)",
181 FileLength, TableLength));
184 /* Scan entire table to determine if each LF has been prefixed with a CR */
186 for (i = 1; i < FileLength; i++)
188 if (Table[i] == 0x0A)
190 if (Table[i - 1] != 0x0D)
192 /* The LF does not have a preceding CR, table not corrupted */
198 /* Found a CR/LF pair */
212 * Entire table scanned, each CR is part of a CR/LF pair --
213 * meaning that the table was treated as a text file somewhere.
215 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
216 * original table are left untouched by the text conversion process --
217 * meaning that we cannot simply replace CR/LF pairs with LFs.
219 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
220 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
221 AcpiOsPrintf ("Table cannot be repaired!\n");
222 return (AE_BAD_VALUE);
226 /*******************************************************************************
228 * FUNCTION: AcpiDbReadTable
230 * PARAMETERS: fp - File that contains table
231 * Table - Return value, buffer with table
232 * TableLength - Return value, length of table
236 * DESCRIPTION: Load the DSDT from the file pointer
238 ******************************************************************************/
243 ACPI_TABLE_HEADER **Table,
246 ACPI_TABLE_HEADER TableHeader;
250 BOOLEAN StandardHeader = TRUE;
253 /* Get the file size */
255 fseek (fp, 0, SEEK_END);
256 FileSize = (UINT32) ftell (fp);
257 fseek (fp, 0, SEEK_SET);
261 return (AE_BAD_HEADER);
264 /* Read the signature */
266 if (fread (&TableHeader, 1, 4, fp) != 4)
268 AcpiOsPrintf ("Could not read the table signature\n");
269 return (AE_BAD_HEADER);
272 fseek (fp, 0, SEEK_SET);
274 /* The RSDT and FACS tables do not have standard ACPI headers */
276 if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") ||
277 ACPI_COMPARE_NAME (TableHeader.Signature, "FACS"))
279 *TableLength = FileSize;
280 StandardHeader = FALSE;
284 /* Read the table header */
286 if (fread (&TableHeader, 1, sizeof (TableHeader), fp) !=
287 sizeof (ACPI_TABLE_HEADER))
289 AcpiOsPrintf ("Could not read the table header\n");
290 return (AE_BAD_HEADER);
294 /* Validate the table header/length */
296 Status = AcpiTbValidateTableHeader (&TableHeader);
297 if (ACPI_FAILURE (Status))
299 AcpiOsPrintf ("Table header is invalid!\n");
304 /* File size must be at least as long as the Header-specified length */
306 if (TableHeader.Length > FileSize)
309 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
310 TableHeader.Length, FileSize);
311 return (AE_BAD_HEADER);
314 #ifdef ACPI_OBSOLETE_CODE
315 /* We only support a limited number of table types */
317 if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) &&
318 ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) &&
319 ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4))
321 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
322 (char *) TableHeader.Signature);
323 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
328 *TableLength = TableHeader.Length;
331 /* Allocate a buffer for the table */
333 *Table = AcpiOsAllocate ((size_t) FileSize);
337 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
338 TableHeader.Signature, *TableLength);
339 return (AE_NO_MEMORY);
342 /* Get the rest of the table */
344 fseek (fp, 0, SEEK_SET);
345 Actual = fread (*Table, 1, (size_t) FileSize, fp);
346 if (Actual == FileSize)
350 /* Now validate the checksum */
352 Status = AcpiTbVerifyChecksum ((void *) *Table,
353 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
355 if (Status == AE_BAD_CHECKSUM)
357 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
358 FileSize, (*Table)->Length);
367 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
372 AcpiOsPrintf ("Error - could not read the table file\n");
381 /*******************************************************************************
383 * FUNCTION: AeLocalLoadTable
385 * PARAMETERS: Table - pointer to a buffer containing the entire
390 * DESCRIPTION: This function is called to load a table from the caller's
391 * buffer. The buffer must contain an entire ACPI Table including
392 * a valid header. The header fields will be verified, and if it
393 * is determined that the table is invalid, the call will fail.
395 ******************************************************************************/
399 ACPI_TABLE_HEADER *Table)
401 ACPI_STATUS Status = AE_OK;
402 /* ACPI_TABLE_DESC TableInfo; */
405 ACPI_FUNCTION_TRACE (AeLocalLoadTable);
411 return_ACPI_STATUS (AE_BAD_PARAMETER);
414 TableInfo.Pointer = Table;
415 Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
416 if (ACPI_FAILURE (Status))
418 return_ACPI_STATUS (Status);
421 /* Install the new table into the local data structures */
423 Status = AcpiTbInstallTable (&TableInfo);
424 if (ACPI_FAILURE (Status))
426 if (Status == AE_ALREADY_EXISTS)
428 /* Table already exists, no error */
433 /* Free table allocated by AcpiTbGetTable */
435 AcpiTbDeleteSingleTable (&TableInfo);
436 return_ACPI_STATUS (Status);
439 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
441 Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
442 if (ACPI_FAILURE (Status))
444 /* Uninstall table and free the buffer */
446 AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
447 return_ACPI_STATUS (Status);
452 return_ACPI_STATUS (Status);
456 /*******************************************************************************
458 * FUNCTION: AcpiDbReadTableFromFile
460 * PARAMETERS: Filename - File where table is located
461 * Table - Where a pointer to the table is returned
465 * DESCRIPTION: Get an ACPI table from a file
467 ******************************************************************************/
470 AcpiDbReadTableFromFile (
472 ACPI_TABLE_HEADER **Table)
481 fp = fopen (Filename, "rb");
484 AcpiOsPrintf ("Could not open input file %s\n", Filename);
488 /* Get the entire file */
490 fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
491 Status = AcpiDbReadTable (fp, Table, &TableLength);
494 if (ACPI_FAILURE (Status))
496 AcpiOsPrintf ("Could not get table from the file\n");
505 /*******************************************************************************
507 * FUNCTION: AcpiDbGetTableFromFile
509 * PARAMETERS: Filename - File where table is located
510 * ReturnTable - Where a pointer to the table is returned
514 * DESCRIPTION: Load an ACPI table from a file
516 ******************************************************************************/
519 AcpiDbGetTableFromFile (
521 ACPI_TABLE_HEADER **ReturnTable)
523 #ifdef ACPI_APPLICATION
525 ACPI_TABLE_HEADER *Table;
526 BOOLEAN IsAmlTable = TRUE;
529 Status = AcpiDbReadTableFromFile (Filename, &Table);
530 if (ACPI_FAILURE (Status))
535 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
536 IsAmlTable = AcpiUtIsAmlTable (Table);
541 /* Attempt to recognize and install the table */
543 Status = AeLocalLoadTable (Table);
544 if (ACPI_FAILURE (Status))
546 if (Status == AE_ALREADY_EXISTS)
548 AcpiOsPrintf ("Table %4.4s is already installed\n",
553 AcpiOsPrintf ("Could not install table, %s\n",
554 AcpiFormatException (Status));
561 "Acpi table [%4.4s] successfully installed and loaded\n",
565 AcpiGbl_AcpiHardwarePresent = FALSE;
568 *ReturnTable = Table;
572 #endif /* ACPI_APPLICATION */
576 #endif /* ACPI_DEBUGGER */