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 - 2013, 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")
64 * NOTE: this is here for lack of a better place. It is used in all
65 * flavors of the debugger, need LCD file
67 #ifdef ACPI_APPLICATION
69 FILE *AcpiGbl_DebugFile = NULL;
75 /* Local prototypes */
77 #ifdef ACPI_APPLICATION
80 AcpiDbCheckTextModeCorruption (
87 /*******************************************************************************
89 * FUNCTION: AcpiDbCloseDebugFile
95 * DESCRIPTION: If open, close the current debug output file
97 ******************************************************************************/
100 AcpiDbCloseDebugFile (
104 #ifdef ACPI_APPLICATION
106 if (AcpiGbl_DebugFile)
108 fclose (AcpiGbl_DebugFile);
109 AcpiGbl_DebugFile = NULL;
110 AcpiGbl_DbOutputToFile = FALSE;
111 AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename);
117 /*******************************************************************************
119 * FUNCTION: AcpiDbOpenDebugFile
121 * PARAMETERS: Name - Filename to open
125 * DESCRIPTION: Open a file where debug output will be directed.
127 ******************************************************************************/
130 AcpiDbOpenDebugFile (
134 #ifdef ACPI_APPLICATION
136 AcpiDbCloseDebugFile ();
137 AcpiGbl_DebugFile = fopen (Name, "w+");
138 if (!AcpiGbl_DebugFile)
140 AcpiOsPrintf ("Could not open debug file %s\n", Name);
144 AcpiOsPrintf ("Debug output file %s opened\n", Name);
145 ACPI_STRNCPY (AcpiGbl_DbDebugFilename, Name,
146 sizeof (AcpiGbl_DbDebugFilename));
147 AcpiGbl_DbOutputToFile = TRUE;
154 #ifdef ACPI_APPLICATION
155 /*******************************************************************************
157 * FUNCTION: AcpiDbCheckTextModeCorruption
159 * PARAMETERS: Table - Table buffer
160 * TableLength - Length of table from the table header
161 * FileLength - Length of the file that contains the table
165 * DESCRIPTION: Check table for text mode file corruption where all linefeed
166 * characters (LF) have been replaced by carriage return linefeed
169 ******************************************************************************/
172 AcpiDbCheckTextModeCorruption (
181 if (TableLength != FileLength)
183 ACPI_WARNING ((AE_INFO,
184 "File length (0x%X) is not the same as the table length (0x%X)",
185 FileLength, TableLength));
188 /* Scan entire table to determine if each LF has been prefixed with a CR */
190 for (i = 1; i < FileLength; i++)
192 if (Table[i] == 0x0A)
194 if (Table[i - 1] != 0x0D)
196 /* The LF does not have a preceding CR, table not corrupted */
202 /* Found a CR/LF pair */
216 * Entire table scanned, each CR is part of a CR/LF pair --
217 * meaning that the table was treated as a text file somewhere.
219 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the
220 * original table are left untouched by the text conversion process --
221 * meaning that we cannot simply replace CR/LF pairs with LFs.
223 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n");
224 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs);
225 AcpiOsPrintf ("Table cannot be repaired!\n");
226 return (AE_BAD_VALUE);
230 /*******************************************************************************
232 * FUNCTION: AcpiDbReadTable
234 * PARAMETERS: fp - File that contains table
235 * Table - Return value, buffer with table
236 * TableLength - Return value, length of table
240 * DESCRIPTION: Load the DSDT from the file pointer
242 ******************************************************************************/
247 ACPI_TABLE_HEADER **Table,
250 ACPI_TABLE_HEADER TableHeader;
254 BOOLEAN StandardHeader = TRUE;
257 /* Get the file size */
259 fseek (fp, 0, SEEK_END);
260 FileSize = (UINT32) ftell (fp);
261 fseek (fp, 0, SEEK_SET);
265 return (AE_BAD_HEADER);
268 /* Read the signature */
270 if (fread (&TableHeader, 1, 4, fp) != 4)
272 AcpiOsPrintf ("Could not read the table signature\n");
273 return (AE_BAD_HEADER);
276 fseek (fp, 0, SEEK_SET);
278 /* The RSDP table does not have standard ACPI header */
280 if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD "))
282 *TableLength = FileSize;
283 StandardHeader = FALSE;
287 /* Read the table header */
289 if (fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), fp) !=
290 sizeof (ACPI_TABLE_HEADER))
292 AcpiOsPrintf ("Could not read the table header\n");
293 return (AE_BAD_HEADER);
297 /* Validate the table header/length */
299 Status = AcpiTbValidateTableHeader (&TableHeader);
300 if (ACPI_FAILURE (Status))
302 AcpiOsPrintf ("Table header is invalid!\n");
307 /* File size must be at least as long as the Header-specified length */
309 if (TableHeader.Length > FileSize)
312 "TableHeader length [0x%X] greater than the input file size [0x%X]\n",
313 TableHeader.Length, FileSize);
315 #ifdef ACPI_ASL_COMPILER
316 Status = FlCheckForAscii (fp, NULL, FALSE);
317 if (ACPI_SUCCESS (Status))
319 AcpiOsPrintf ("File appears to be ASCII only, must be binary\n",
320 TableHeader.Length, FileSize);
323 return (AE_BAD_HEADER);
326 #ifdef ACPI_OBSOLETE_CODE
327 /* We only support a limited number of table types */
329 if (!ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_DSDT) &&
330 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_PSDT) &&
331 !ACPI_COMPARE_NAME ((char *) TableHeader.Signature, ACPI_SIG_SSDT))
333 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n",
334 (char *) TableHeader.Signature);
335 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER));
340 *TableLength = TableHeader.Length;
343 /* Allocate a buffer for the table */
345 *Table = AcpiOsAllocate ((size_t) FileSize);
349 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n",
350 TableHeader.Signature, *TableLength);
351 return (AE_NO_MEMORY);
354 /* Get the rest of the table */
356 fseek (fp, 0, SEEK_SET);
357 Actual = fread (*Table, 1, (size_t) FileSize, fp);
358 if (Actual == FileSize)
362 /* Now validate the checksum */
364 Status = AcpiTbVerifyChecksum ((void *) *Table,
365 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length);
367 if (Status == AE_BAD_CHECKSUM)
369 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table,
370 FileSize, (*Table)->Length);
379 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n",
384 AcpiOsPrintf ("Error - could not read the table file\n");
392 /*******************************************************************************
394 * FUNCTION: AeLocalLoadTable
396 * PARAMETERS: Table - pointer to a buffer containing the entire
401 * DESCRIPTION: This function is called to load a table from the caller's
402 * buffer. The buffer must contain an entire ACPI Table including
403 * a valid header. The header fields will be verified, and if it
404 * is determined that the table is invalid, the call will fail.
406 ******************************************************************************/
410 ACPI_TABLE_HEADER *Table)
412 ACPI_STATUS Status = AE_OK;
413 /* ACPI_TABLE_DESC TableInfo; */
416 ACPI_FUNCTION_TRACE (AeLocalLoadTable);
422 return_ACPI_STATUS (AE_BAD_PARAMETER);
425 TableInfo.Pointer = Table;
426 Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL);
427 if (ACPI_FAILURE (Status))
429 return_ACPI_STATUS (Status);
432 /* Install the new table into the local data structures */
434 Status = AcpiTbInstallTable (&TableInfo);
435 if (ACPI_FAILURE (Status))
437 if (Status == AE_ALREADY_EXISTS)
439 /* Table already exists, no error */
444 /* Free table allocated by AcpiTbGetTable */
446 AcpiTbDeleteSingleTable (&TableInfo);
447 return_ACPI_STATUS (Status);
450 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
452 Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode);
453 if (ACPI_FAILURE (Status))
455 /* Uninstall table and free the buffer */
457 AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT);
458 return_ACPI_STATUS (Status);
463 return_ACPI_STATUS (Status);
467 /*******************************************************************************
469 * FUNCTION: AcpiDbReadTableFromFile
471 * PARAMETERS: Filename - File where table is located
472 * Table - Where a pointer to the table is returned
476 * DESCRIPTION: Get an ACPI table from a file
478 ******************************************************************************/
481 AcpiDbReadTableFromFile (
483 ACPI_TABLE_HEADER **Table)
492 File = fopen (Filename, "rb");
495 AcpiOsPrintf ("Could not open input file %s\n", Filename);
499 /* Get the entire file */
501 fprintf (stderr, "Loading Acpi table from file %s\n", Filename);
502 Status = AcpiDbReadTable (File, Table, &TableLength);
505 if (ACPI_FAILURE (Status))
507 AcpiOsPrintf ("Could not get table from the file\n");
516 /*******************************************************************************
518 * FUNCTION: AcpiDbGetTableFromFile
520 * PARAMETERS: Filename - File where table is located
521 * ReturnTable - Where a pointer to the table is returned
525 * DESCRIPTION: Load an ACPI table from a file
527 ******************************************************************************/
530 AcpiDbGetTableFromFile (
532 ACPI_TABLE_HEADER **ReturnTable)
534 #ifdef ACPI_APPLICATION
536 ACPI_TABLE_HEADER *Table;
537 BOOLEAN IsAmlTable = TRUE;
540 Status = AcpiDbReadTableFromFile (Filename, &Table);
541 if (ACPI_FAILURE (Status))
546 #ifdef ACPI_DATA_TABLE_DISASSEMBLY
547 IsAmlTable = AcpiUtIsAmlTable (Table);
552 /* Attempt to recognize and install the table */
554 Status = AeLocalLoadTable (Table);
555 if (ACPI_FAILURE (Status))
557 if (Status == AE_ALREADY_EXISTS)
559 AcpiOsPrintf ("Table %4.4s is already installed\n",
564 AcpiOsPrintf ("Could not install table, %s\n",
565 AcpiFormatException (Status));
572 "Acpi table [%4.4s] successfully installed and loaded\n",
576 AcpiGbl_AcpiHardwarePresent = FALSE;
579 *ReturnTable = Table;
583 #endif /* ACPI_APPLICATION */
587 #endif /* ACPI_DEBUGGER */