1 /******************************************************************************* 2 * 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) 5 * 6 ******************************************************************************/ 7 8 /* 9 * Copyright (C) 2000 - 2012, Intel Corp. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 26 * 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. 30 * 31 * NO WARRANTY 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. 43 */ 44 45 46 #include <contrib/dev/acpica/include/acpi.h> 47 #include <contrib/dev/acpica/include/accommon.h> 48 #include <contrib/dev/acpica/include/acdebug.h> 49 50 #ifdef ACPI_APPLICATION 51 #include <contrib/dev/acpica/include/actables.h> 52 #endif 53 54 #if (defined ACPI_DEBUGGER || defined ACPI_DISASSEMBLER) 55 56 #define _COMPONENT ACPI_CA_DEBUGGER 57 ACPI_MODULE_NAME ("dbfileio") 58 59 /* 60 * NOTE: this is here for lack of a better place. It is used in all 61 * flavors of the debugger, need LCD file 62 */ 63 #ifdef ACPI_APPLICATION 64 #include <stdio.h> 65 FILE *AcpiGbl_DebugFile = NULL; 66 #endif 67 68 69 #ifdef ACPI_DEBUGGER 70 71 /* Local prototypes */ 72 73 #ifdef ACPI_APPLICATION 74 75 static ACPI_STATUS 76 AcpiDbCheckTextModeCorruption ( 77 UINT8 *Table, 78 UINT32 TableLength, 79 UINT32 FileLength); 80 81 #endif 82 83 /******************************************************************************* 84 * 85 * FUNCTION: AcpiDbCloseDebugFile 86 * 87 * PARAMETERS: None 88 * 89 * RETURN: None 90 * 91 * DESCRIPTION: If open, close the current debug output file 92 * 93 ******************************************************************************/ 94 95 void 96 AcpiDbCloseDebugFile ( 97 void) 98 { 99 100 #ifdef ACPI_APPLICATION 101 102 if (AcpiGbl_DebugFile) 103 { 104 fclose (AcpiGbl_DebugFile); 105 AcpiGbl_DebugFile = NULL; 106 AcpiGbl_DbOutputToFile = FALSE; 107 AcpiOsPrintf ("Debug output file %s closed\n", AcpiGbl_DbDebugFilename); 108 } 109 #endif 110 } 111 112 113 /******************************************************************************* 114 * 115 * FUNCTION: AcpiDbOpenDebugFile 116 * 117 * PARAMETERS: Name - Filename to open 118 * 119 * RETURN: None 120 * 121 * DESCRIPTION: Open a file where debug output will be directed. 122 * 123 ******************************************************************************/ 124 125 void 126 AcpiDbOpenDebugFile ( 127 char *Name) 128 { 129 130 #ifdef ACPI_APPLICATION 131 132 AcpiDbCloseDebugFile (); 133 AcpiGbl_DebugFile = fopen (Name, "w+"); 134 if (AcpiGbl_DebugFile) 135 { 136 AcpiOsPrintf ("Debug output file %s opened\n", Name); 137 ACPI_STRCPY (AcpiGbl_DbDebugFilename, Name); 138 AcpiGbl_DbOutputToFile = TRUE; 139 } 140 else 141 { 142 AcpiOsPrintf ("Could not open debug file %s\n", Name); 143 } 144 145 #endif 146 } 147 #endif 148 149 150 #ifdef ACPI_APPLICATION 151 /******************************************************************************* 152 * 153 * FUNCTION: AcpiDbCheckTextModeCorruption 154 * 155 * PARAMETERS: Table - Table buffer 156 * TableLength - Length of table from the table header 157 * FileLength - Length of the file that contains the table 158 * 159 * RETURN: Status 160 * 161 * DESCRIPTION: Check table for text mode file corruption where all linefeed 162 * characters (LF) have been replaced by carriage return linefeed 163 * pairs (CR/LF). 164 * 165 ******************************************************************************/ 166 167 static ACPI_STATUS 168 AcpiDbCheckTextModeCorruption ( 169 UINT8 *Table, 170 UINT32 TableLength, 171 UINT32 FileLength) 172 { 173 UINT32 i; 174 UINT32 Pairs = 0; 175 176 177 if (TableLength != FileLength) 178 { 179 ACPI_WARNING ((AE_INFO, 180 "File length (0x%X) is not the same as the table length (0x%X)", 181 FileLength, TableLength)); 182 } 183 184 /* Scan entire table to determine if each LF has been prefixed with a CR */ 185 186 for (i = 1; i < FileLength; i++) 187 { 188 if (Table[i] == 0x0A) 189 { 190 if (Table[i - 1] != 0x0D) 191 { 192 /* The LF does not have a preceding CR, table not corrupted */ 193 194 return (AE_OK); 195 } 196 else 197 { 198 /* Found a CR/LF pair */ 199 200 Pairs++; 201 } 202 i++; 203 } 204 } 205 206 if (!Pairs) 207 { 208 return (AE_OK); 209 } 210 211 /* 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. 214 * 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. 218 */ 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); 223 } 224 225 226 /******************************************************************************* 227 * 228 * FUNCTION: AcpiDbReadTable 229 * 230 * PARAMETERS: fp - File that contains table 231 * Table - Return value, buffer with table 232 * TableLength - Return value, length of table 233 * 234 * RETURN: Status 235 * 236 * DESCRIPTION: Load the DSDT from the file pointer 237 * 238 ******************************************************************************/ 239 240 static ACPI_STATUS 241 AcpiDbReadTable ( 242 FILE *fp, 243 ACPI_TABLE_HEADER **Table, 244 UINT32 *TableLength) 245 { 246 ACPI_TABLE_HEADER TableHeader; 247 UINT32 Actual; 248 ACPI_STATUS Status; 249 UINT32 FileSize; 250 BOOLEAN StandardHeader = TRUE; 251 252 253 /* Get the file size */ 254 255 fseek (fp, 0, SEEK_END); 256 FileSize = (UINT32) ftell (fp); 257 fseek (fp, 0, SEEK_SET); 258 259 if (FileSize < 4) 260 { 261 return (AE_BAD_HEADER); 262 } 263 264 /* Read the signature */ 265 266 if (fread (&TableHeader, 1, 4, fp) != 4) 267 { 268 AcpiOsPrintf ("Could not read the table signature\n"); 269 return (AE_BAD_HEADER); 270 } 271 272 fseek (fp, 0, SEEK_SET); 273 274 /* The RSDT, FACS and S3PT tables do not have standard ACPI headers */ 275 276 if (ACPI_COMPARE_NAME (TableHeader.Signature, "RSD ") || 277 ACPI_COMPARE_NAME (TableHeader.Signature, "FACS") || 278 ACPI_COMPARE_NAME (TableHeader.Signature, "S3PT")) 279 { 280 *TableLength = FileSize; 281 StandardHeader = FALSE; 282 } 283 else 284 { 285 /* Read the table header */ 286 287 if (fread (&TableHeader, 1, sizeof (TableHeader), fp) != 288 sizeof (ACPI_TABLE_HEADER)) 289 { 290 AcpiOsPrintf ("Could not read the table header\n"); 291 return (AE_BAD_HEADER); 292 } 293 294 #if 0 295 /* Validate the table header/length */ 296 297 Status = AcpiTbValidateTableHeader (&TableHeader); 298 if (ACPI_FAILURE (Status)) 299 { 300 AcpiOsPrintf ("Table header is invalid!\n"); 301 return (Status); 302 } 303 #endif 304 305 /* File size must be at least as long as the Header-specified length */ 306 307 if (TableHeader.Length > FileSize) 308 { 309 AcpiOsPrintf ( 310 "TableHeader length [0x%X] greater than the input file size [0x%X]\n", 311 TableHeader.Length, FileSize); 312 return (AE_BAD_HEADER); 313 } 314 315 #ifdef ACPI_OBSOLETE_CODE 316 /* We only support a limited number of table types */ 317 318 if (ACPI_STRNCMP ((char *) TableHeader.Signature, DSDT_SIG, 4) && 319 ACPI_STRNCMP ((char *) TableHeader.Signature, PSDT_SIG, 4) && 320 ACPI_STRNCMP ((char *) TableHeader.Signature, SSDT_SIG, 4)) 321 { 322 AcpiOsPrintf ("Table signature [%4.4s] is invalid or not supported\n", 323 (char *) TableHeader.Signature); 324 ACPI_DUMP_BUFFER (&TableHeader, sizeof (ACPI_TABLE_HEADER)); 325 return (AE_ERROR); 326 } 327 #endif 328 329 *TableLength = TableHeader.Length; 330 } 331 332 /* Allocate a buffer for the table */ 333 334 *Table = AcpiOsAllocate ((size_t) FileSize); 335 if (!*Table) 336 { 337 AcpiOsPrintf ( 338 "Could not allocate memory for ACPI table %4.4s (size=0x%X)\n", 339 TableHeader.Signature, *TableLength); 340 return (AE_NO_MEMORY); 341 } 342 343 /* Get the rest of the table */ 344 345 fseek (fp, 0, SEEK_SET); 346 Actual = fread (*Table, 1, (size_t) FileSize, fp); 347 if (Actual == FileSize) 348 { 349 if (StandardHeader) 350 { 351 /* Now validate the checksum */ 352 353 Status = AcpiTbVerifyChecksum ((void *) *Table, 354 ACPI_CAST_PTR (ACPI_TABLE_HEADER, *Table)->Length); 355 356 if (Status == AE_BAD_CHECKSUM) 357 { 358 Status = AcpiDbCheckTextModeCorruption ((UINT8 *) *Table, 359 FileSize, (*Table)->Length); 360 return (Status); 361 } 362 } 363 return (AE_OK); 364 } 365 366 if (Actual > 0) 367 { 368 AcpiOsPrintf ("Warning - reading table, asked for %X got %X\n", 369 FileSize, Actual); 370 return (AE_OK); 371 } 372 373 AcpiOsPrintf ("Error - could not read the table file\n"); 374 AcpiOsFree (*Table); 375 *Table = NULL; 376 *TableLength = 0; 377 378 return (AE_ERROR); 379 } 380 381 382 /******************************************************************************* 383 * 384 * FUNCTION: AeLocalLoadTable 385 * 386 * PARAMETERS: Table - pointer to a buffer containing the entire 387 * table to be loaded 388 * 389 * RETURN: Status 390 * 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. 395 * 396 ******************************************************************************/ 397 398 static ACPI_STATUS 399 AeLocalLoadTable ( 400 ACPI_TABLE_HEADER *Table) 401 { 402 ACPI_STATUS Status = AE_OK; 403 /* ACPI_TABLE_DESC TableInfo; */ 404 405 406 ACPI_FUNCTION_TRACE (AeLocalLoadTable); 407 #if 0 408 409 410 if (!Table) 411 { 412 return_ACPI_STATUS (AE_BAD_PARAMETER); 413 } 414 415 TableInfo.Pointer = Table; 416 Status = AcpiTbRecognizeTable (&TableInfo, ACPI_TABLE_ALL); 417 if (ACPI_FAILURE (Status)) 418 { 419 return_ACPI_STATUS (Status); 420 } 421 422 /* Install the new table into the local data structures */ 423 424 Status = AcpiTbInstallTable (&TableInfo); 425 if (ACPI_FAILURE (Status)) 426 { 427 if (Status == AE_ALREADY_EXISTS) 428 { 429 /* Table already exists, no error */ 430 431 Status = AE_OK; 432 } 433 434 /* Free table allocated by AcpiTbGetTable */ 435 436 AcpiTbDeleteSingleTable (&TableInfo); 437 return_ACPI_STATUS (Status); 438 } 439 440 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY)) 441 442 Status = AcpiNsLoadTable (TableInfo.InstalledDesc, AcpiGbl_RootNode); 443 if (ACPI_FAILURE (Status)) 444 { 445 /* Uninstall table and free the buffer */ 446 447 AcpiTbDeleteTablesByType (ACPI_TABLE_ID_DSDT); 448 return_ACPI_STATUS (Status); 449 } 450 #endif 451 #endif 452 453 return_ACPI_STATUS (Status); 454 } 455 456 457 /******************************************************************************* 458 * 459 * FUNCTION: AcpiDbReadTableFromFile 460 * 461 * PARAMETERS: Filename - File where table is located 462 * Table - Where a pointer to the table is returned 463 * 464 * RETURN: Status 465 * 466 * DESCRIPTION: Get an ACPI table from a file 467 * 468 ******************************************************************************/ 469 470 ACPI_STATUS 471 AcpiDbReadTableFromFile ( 472 char *Filename, 473 ACPI_TABLE_HEADER **Table) 474 { 475 FILE *fp; 476 UINT32 TableLength; 477 ACPI_STATUS Status; 478 479 480 /* Open the file */ 481 482 fp = fopen (Filename, "rb"); 483 if (!fp) 484 { 485 AcpiOsPrintf ("Could not open input file %s\n", Filename); 486 return (AE_ERROR); 487 } 488 489 /* Get the entire file */ 490 491 fprintf (stderr, "Loading Acpi table from file %s\n", Filename); 492 Status = AcpiDbReadTable (fp, Table, &TableLength); 493 fclose(fp); 494 495 if (ACPI_FAILURE (Status)) 496 { 497 AcpiOsPrintf ("Could not get table from the file\n"); 498 return (Status); 499 } 500 501 return (AE_OK); 502 } 503 #endif 504 505 506 /******************************************************************************* 507 * 508 * FUNCTION: AcpiDbGetTableFromFile 509 * 510 * PARAMETERS: Filename - File where table is located 511 * ReturnTable - Where a pointer to the table is returned 512 * 513 * RETURN: Status 514 * 515 * DESCRIPTION: Load an ACPI table from a file 516 * 517 ******************************************************************************/ 518 519 ACPI_STATUS 520 AcpiDbGetTableFromFile ( 521 char *Filename, 522 ACPI_TABLE_HEADER **ReturnTable) 523 { 524 #ifdef ACPI_APPLICATION 525 ACPI_STATUS Status; 526 ACPI_TABLE_HEADER *Table; 527 BOOLEAN IsAmlTable = TRUE; 528 529 530 Status = AcpiDbReadTableFromFile (Filename, &Table); 531 if (ACPI_FAILURE (Status)) 532 { 533 return (Status); 534 } 535 536 #ifdef ACPI_DATA_TABLE_DISASSEMBLY 537 IsAmlTable = AcpiUtIsAmlTable (Table); 538 #endif 539 540 if (IsAmlTable) 541 { 542 /* Attempt to recognize and install the table */ 543 544 Status = AeLocalLoadTable (Table); 545 if (ACPI_FAILURE (Status)) 546 { 547 if (Status == AE_ALREADY_EXISTS) 548 { 549 AcpiOsPrintf ("Table %4.4s is already installed\n", 550 Table->Signature); 551 } 552 else 553 { 554 AcpiOsPrintf ("Could not install table, %s\n", 555 AcpiFormatException (Status)); 556 } 557 558 return (Status); 559 } 560 561 fprintf (stderr, 562 "Acpi table [%4.4s] successfully installed and loaded\n", 563 Table->Signature); 564 } 565 566 AcpiGbl_AcpiHardwarePresent = FALSE; 567 if (ReturnTable) 568 { 569 *ReturnTable = Table; 570 } 571 572 573 #endif /* ACPI_APPLICATION */ 574 return (AE_OK); 575 } 576 577 #endif /* ACPI_DEBUGGER */ 578 579