1 /****************************************************************************** 2 * 3 * Module Name: acfileio - Get ACPI tables from file 4 * 5 *****************************************************************************/ 6 7 /****************************************************************************** 8 * 9 * 1. Copyright Notice 10 * 11 * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp. 12 * All rights reserved. 13 * 14 * 2. License 15 * 16 * 2.1. This is your license from Intel Corp. under its intellectual property 17 * rights. You may have additional license terms from the party that provided 18 * you this software, covering your right to use that party's intellectual 19 * property rights. 20 * 21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 22 * copy of the source code appearing in this file ("Covered Code") an 23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 24 * base code distributed originally by Intel ("Original Intel Code") to copy, 25 * make derivatives, distribute, use and display any portion of the Covered 26 * Code in any form, with the right to sublicense such rights; and 27 * 28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 29 * license (with the right to sublicense), under only those claims of Intel 30 * patents that are infringed by the Original Intel Code, to make, use, sell, 31 * offer to sell, and import the Covered Code and derivative works thereof 32 * solely to the minimum extent necessary to exercise the above copyright 33 * license, and in no event shall the patent license extend to any additions 34 * to or modifications of the Original Intel Code. No other license or right 35 * is granted directly or by implication, estoppel or otherwise; 36 * 37 * The above copyright and patent license is granted only if the following 38 * conditions are met: 39 * 40 * 3. Conditions 41 * 42 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 43 * Redistribution of source code of any substantial portion of the Covered 44 * Code or modification with rights to further distribute source must include 45 * the above Copyright Notice, the above License, this list of Conditions, 46 * and the following Disclaimer and Export Compliance provision. In addition, 47 * Licensee must cause all Covered Code to which Licensee contributes to 48 * contain a file documenting the changes Licensee made to create that Covered 49 * Code and the date of any change. Licensee must include in that file the 50 * documentation of any changes made by any predecessor Licensee. Licensee 51 * must include a prominent statement that the modification is derived, 52 * directly or indirectly, from Original Intel Code. 53 * 54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 55 * Redistribution of source code of any substantial portion of the Covered 56 * Code or modification without rights to further distribute source must 57 * include the following Disclaimer and Export Compliance provision in the 58 * documentation and/or other materials provided with distribution. In 59 * addition, Licensee may not authorize further sublicense of source of any 60 * portion of the Covered Code, and must include terms to the effect that the 61 * license from Licensee to its licensee is limited to the intellectual 62 * property embodied in the software Licensee provides to its licensee, and 63 * not to intellectual property embodied in modifications its licensee may 64 * make. 65 * 66 * 3.3. Redistribution of Executable. Redistribution in executable form of any 67 * substantial portion of the Covered Code or modification must reproduce the 68 * above Copyright Notice, and the following Disclaimer and Export Compliance 69 * provision in the documentation and/or other materials provided with the 70 * distribution. 71 * 72 * 3.4. Intel retains all right, title, and interest in and to the Original 73 * Intel Code. 74 * 75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 76 * Intel shall be used in advertising or otherwise to promote the sale, use or 77 * other dealings in products derived from or relating to the Covered Code 78 * without prior written authorization from Intel. 79 * 80 * 4. Disclaimer and Export Compliance 81 * 82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 88 * PARTICULAR PURPOSE. 89 * 90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 97 * LIMITED REMEDY. 98 * 99 * 4.3. Licensee shall not export, either directly or indirectly, any of this 100 * software or system incorporating such software without first obtaining any 101 * required license or other approval from the U. S. Department of Commerce or 102 * any other agency or department of the United States Government. In the 103 * event Licensee exports any such software from the United States or 104 * re-exports any such software from a foreign destination, Licensee shall 105 * ensure that the distribution and export/re-export of the software is in 106 * compliance with all laws, regulations, orders, or other restrictions of the 107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 108 * any of its subsidiaries will export/re-export any technical data, process, 109 * software, or service, directly or indirectly, to any country for which the 110 * United States government or any agency thereof requires an export license, 111 * other governmental approval, or letter of assurance, without first obtaining 112 * such license, approval or letter. 113 * 114 ***************************************************************************** 115 * 116 * Alternatively, you may choose to be licensed under the terms of the 117 * following license: 118 * 119 * Redistribution and use in source and binary forms, with or without 120 * modification, are permitted provided that the following conditions 121 * are met: 122 * 1. Redistributions of source code must retain the above copyright 123 * notice, this list of conditions, and the following disclaimer, 124 * without modification. 125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 126 * substantially similar to the "NO WARRANTY" disclaimer below 127 * ("Disclaimer") and any redistribution must be conditioned upon 128 * including a substantially similar Disclaimer requirement for further 129 * binary redistribution. 130 * 3. Neither the names of the above-listed copyright holders nor the names 131 * of any contributors may be used to endorse or promote products derived 132 * from this software without specific prior written permission. 133 * 134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 145 * 146 * Alternatively, you may choose to be licensed under the terms of the 147 * GNU General Public License ("GPL") version 2 as published by the Free 148 * Software Foundation. 149 * 150 *****************************************************************************/ 151 152 #include <contrib/dev/acpica/include/acpi.h> 153 #include <contrib/dev/acpica/include/accommon.h> 154 #include <contrib/dev/acpica/include/actables.h> 155 #include <contrib/dev/acpica/include/acutils.h> 156 #include <contrib/dev/acpica/include/acapps.h> 157 158 #define _COMPONENT ACPI_UTILITIES 159 ACPI_MODULE_NAME ("acfileio") 160 161 162 /* Local prototypes */ 163 164 static ACPI_STATUS 165 AcGetOneTableFromFile ( 166 char *Filename, 167 FILE *File, 168 UINT8 GetOnlyAmlTables, 169 ACPI_TABLE_HEADER **Table); 170 171 static ACPI_STATUS 172 AcCheckTextModeCorruption ( 173 ACPI_TABLE_HEADER *Table); 174 175 176 /******************************************************************************* 177 * 178 * FUNCTION: AcGetAllTablesFromFile 179 * 180 * PARAMETERS: Filename - Table filename 181 * GetOnlyAmlTables - TRUE if the tables must be AML tables 182 * ReturnListHead - Where table list is returned 183 * 184 * RETURN: Status 185 * 186 * DESCRIPTION: Get all ACPI tables from within a single file. 187 * 188 ******************************************************************************/ 189 190 ACPI_STATUS 191 AcGetAllTablesFromFile ( 192 char *Filename, 193 UINT8 GetOnlyAmlTables, 194 ACPI_NEW_TABLE_DESC **ReturnListHead) 195 { 196 ACPI_NEW_TABLE_DESC *ListHead = NULL; 197 ACPI_NEW_TABLE_DESC *ListTail = NULL; 198 ACPI_NEW_TABLE_DESC *TableDesc; 199 FILE *File; 200 ACPI_TABLE_HEADER *Table = NULL; 201 UINT32 FileSize; 202 ACPI_STATUS Status = AE_OK; 203 204 205 File = fopen (Filename, "rb"); 206 if (!File) 207 { 208 fprintf (stderr, "Could not open input file: %s\n", Filename); 209 if (errno == ENOENT) 210 { 211 return (AE_NOT_EXIST); 212 } 213 214 return (AE_ERROR); 215 } 216 217 /* Get the file size */ 218 219 FileSize = CmGetFileSize (File); 220 if (FileSize == ACPI_UINT32_MAX) 221 { 222 Status = AE_ERROR; 223 goto Exit; 224 } 225 226 fprintf (stderr, 227 "Input file %s, Length 0x%X (%u) bytes\n", 228 Filename, FileSize, FileSize); 229 230 /* We must have at least one ACPI table header */ 231 232 if (FileSize < sizeof (ACPI_TABLE_HEADER)) 233 { 234 Status = AE_BAD_HEADER; 235 goto Exit; 236 } 237 238 /* Check for an non-binary file */ 239 240 if (!AcIsFileBinary (File)) 241 { 242 fprintf (stderr, 243 " %s: File does not appear to contain a valid AML table\n", 244 Filename); 245 Status = AE_TYPE; 246 goto Exit; 247 } 248 249 /* Read all tables within the file */ 250 251 while (ACPI_SUCCESS (Status)) 252 { 253 /* Get one entire ACPI table */ 254 255 Status = AcGetOneTableFromFile ( 256 Filename, File, GetOnlyAmlTables, &Table); 257 258 if (Status == AE_CTRL_TERMINATE) 259 { 260 Status = AE_OK; 261 break; 262 } 263 else if (Status == AE_TYPE) 264 { 265 Status = AE_OK; 266 goto Exit; 267 } 268 else if (ACPI_FAILURE (Status)) 269 { 270 goto Exit; 271 } 272 273 /* Print table header for iASL/disassembler only */ 274 275 #ifdef ACPI_ASL_COMPILER 276 277 AcpiTbPrintTableHeader (0, Table); 278 #endif 279 280 /* Allocate and link a table descriptor */ 281 282 TableDesc = AcpiOsAllocate (sizeof (ACPI_NEW_TABLE_DESC)); 283 if (!TableDesc) 284 { 285 AcpiOsFree (Table); 286 Status = AE_NO_MEMORY; 287 goto Exit; 288 } 289 290 TableDesc->Table = Table; 291 TableDesc->Next = NULL; 292 293 /* Link at the end of the local table list */ 294 295 if (!ListHead) 296 { 297 ListHead = TableDesc; 298 ListTail = TableDesc; 299 } 300 else 301 { 302 ListTail->Next = TableDesc; 303 ListTail = TableDesc; 304 } 305 } 306 307 /* Add the local table list to the end of the global list */ 308 309 if (*ReturnListHead) 310 { 311 ListTail = *ReturnListHead; 312 while (ListTail->Next) 313 { 314 ListTail = ListTail->Next; 315 } 316 317 ListTail->Next = ListHead; 318 } 319 else 320 { 321 *ReturnListHead = ListHead; 322 } 323 324 Exit: 325 fclose(File); 326 return (Status); 327 } 328 329 330 /******************************************************************************* 331 * 332 * FUNCTION: AcGetOneTableFromFile 333 * 334 * PARAMETERS: Filename - File where table is located 335 * File - Open FILE pointer to Filename 336 * GetOnlyAmlTables - TRUE if the tables must be AML tables. 337 * ReturnTable - Where a pointer to the table is returned 338 * 339 * RETURN: Status 340 * 341 * DESCRIPTION: Read the next ACPI table from a file. Implements support 342 * for multiple tables within a single file. File must already 343 * be open. 344 * 345 * Note: Loading an RSDP is not supported. 346 * 347 ******************************************************************************/ 348 349 static ACPI_STATUS 350 AcGetOneTableFromFile ( 351 char *Filename, 352 FILE *File, 353 UINT8 GetOnlyAmlTables, 354 ACPI_TABLE_HEADER **ReturnTable) 355 { 356 ACPI_STATUS Status = AE_OK; 357 ACPI_TABLE_HEADER TableHeader; 358 ACPI_TABLE_HEADER *Table; 359 INT32 Count; 360 long TableOffset; 361 362 363 *ReturnTable = NULL; 364 365 /* Get the table header to examine signature and length */ 366 367 TableOffset = ftell (File); 368 Count = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File); 369 if (Count != sizeof (ACPI_TABLE_HEADER)) 370 { 371 return (AE_CTRL_TERMINATE); 372 } 373 374 /* Validate the table signature/header (limited ASCII chars) */ 375 376 Status = AcValidateTableHeader (File, TableOffset); 377 if (ACPI_FAILURE (Status)) 378 { 379 return (Status); 380 } 381 382 if (GetOnlyAmlTables) 383 { 384 /* 385 * Table must be an AML table (DSDT/SSDT). 386 * Used for iASL -e option only. 387 */ 388 if (!AcpiUtIsAmlTable (&TableHeader)) 389 { 390 fprintf (stderr, 391 " %s: Table [%4.4s] is not an AML table - ignoring\n", 392 Filename, TableHeader.Signature); 393 394 return (AE_TYPE); 395 } 396 } 397 398 /* Allocate a buffer for the entire table */ 399 400 Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length); 401 if (!Table) 402 { 403 return (AE_NO_MEMORY); 404 } 405 406 /* Read the entire ACPI table, including header */ 407 408 fseek (File, TableOffset, SEEK_SET); 409 410 Count = fread (Table, 1, TableHeader.Length, File); 411 if (Count != (INT32) TableHeader.Length) 412 { 413 Status = AE_ERROR; 414 goto ErrorExit; 415 } 416 417 /* Validate the checksum (just issue a warning) */ 418 419 Status = AcpiTbVerifyChecksum (Table, TableHeader.Length); 420 if (ACPI_FAILURE (Status)) 421 { 422 Status = AcCheckTextModeCorruption (Table); 423 if (ACPI_FAILURE (Status)) 424 { 425 goto ErrorExit; 426 } 427 } 428 429 *ReturnTable = Table; 430 return (AE_OK); 431 432 433 ErrorExit: 434 AcpiOsFree (Table); 435 return (Status); 436 } 437 438 439 /******************************************************************************* 440 * 441 * FUNCTION: AcIsFileBinary 442 * 443 * PARAMETERS: File - Open input file 444 * 445 * RETURN: TRUE if file appears to be binary 446 * 447 * DESCRIPTION: Scan a file for any non-ASCII bytes. 448 * 449 * Note: Maintains current file position. 450 * 451 ******************************************************************************/ 452 453 BOOLEAN 454 AcIsFileBinary ( 455 FILE *File) 456 { 457 UINT8 Byte; 458 BOOLEAN IsBinary = FALSE; 459 long FileOffset; 460 461 462 /* Scan entire file for any non-ASCII bytes */ 463 464 FileOffset = ftell (File); 465 while (fread (&Byte, 1, 1, File) == 1) 466 { 467 if (!isprint (Byte) && !isspace (Byte)) 468 { 469 IsBinary = TRUE; 470 goto Exit; 471 } 472 } 473 474 Exit: 475 fseek (File, FileOffset, SEEK_SET); 476 return (IsBinary); 477 } 478 479 480 /******************************************************************************* 481 * 482 * FUNCTION: AcValidateTableHeader 483 * 484 * PARAMETERS: File - Open input file 485 * 486 * RETURN: Status 487 * 488 * DESCRIPTION: Determine if a file seems to contain one or more binary ACPI 489 * tables, via the 490 * following checks on what would be the table header: 491 * 1) File must be at least as long as an ACPI_TABLE_HEADER 492 * 2) There must be enough room in the file to hold entire table 493 * 3) Signature, OemId, OemTableId, AslCompilerId must be ASCII 494 * 495 * Note: There can be multiple definition blocks per file, so we cannot 496 * expect/compare the file size to be equal to the table length. 12/2015. 497 * 498 * Note: Maintains current file position. 499 * 500 ******************************************************************************/ 501 502 ACPI_STATUS 503 AcValidateTableHeader ( 504 FILE *File, 505 long TableOffset) 506 { 507 ACPI_TABLE_HEADER TableHeader; 508 ACPI_SIZE Actual; 509 long OriginalOffset; 510 UINT32 FileSize; 511 UINT32 i; 512 513 514 ACPI_FUNCTION_TRACE (AcValidateTableHeader); 515 516 517 /* Read a potential table header */ 518 519 OriginalOffset = ftell (File); 520 fseek (File, TableOffset, SEEK_SET); 521 522 Actual = fread (&TableHeader, 1, sizeof (ACPI_TABLE_HEADER), File); 523 fseek (File, OriginalOffset, SEEK_SET); 524 525 if (Actual < sizeof (ACPI_TABLE_HEADER)) 526 { 527 return (AE_ERROR); 528 } 529 530 /* Validate the signature (limited ASCII chars) */ 531 532 if (!AcpiUtValidNameseg (TableHeader.Signature)) 533 { 534 return (AE_BAD_SIGNATURE); 535 } 536 537 /* Validate table length against bytes remaining in the file */ 538 539 FileSize = CmGetFileSize (File); 540 if (TableHeader.Length > (UINT32) (FileSize - TableOffset)) 541 { 542 fprintf (stderr, "Table [%4.4s] is too long for file - " 543 "needs: 0x%.2X, remaining in file: 0x%.2X\n", 544 TableHeader.Signature, TableHeader.Length, 545 (UINT32) (FileSize - TableOffset)); 546 return (AE_BAD_HEADER); 547 } 548 549 /* 550 * These fields must be ASCII: OemId, OemTableId, AslCompilerId. 551 * We allow a NULL terminator in OemId and OemTableId. 552 */ 553 for (i = 0; i < ACPI_NAME_SIZE; i++) 554 { 555 if (!ACPI_IS_ASCII ((UINT8) TableHeader.AslCompilerId[i])) 556 { 557 goto BadCharacters; 558 } 559 } 560 561 for (i = 0; (i < ACPI_OEM_ID_SIZE) && (TableHeader.OemId[i]); i++) 562 { 563 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemId[i])) 564 { 565 goto BadCharacters; 566 } 567 } 568 569 for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (TableHeader.OemTableId[i]); i++) 570 { 571 if (!ACPI_IS_ASCII ((UINT8) TableHeader.OemTableId[i])) 572 { 573 goto BadCharacters; 574 } 575 } 576 577 return (AE_OK); 578 579 580 BadCharacters: 581 582 ACPI_WARNING ((AE_INFO, 583 "Table header for [%4.4s] has invalid ASCII character(s)", 584 TableHeader.Signature)); 585 return (AE_OK); 586 } 587 588 589 /******************************************************************************* 590 * 591 * FUNCTION: AcCheckTextModeCorruption 592 * 593 * PARAMETERS: Table - Table buffer starting with table header 594 * 595 * RETURN: Status 596 * 597 * DESCRIPTION: Check table for text mode file corruption where all linefeed 598 * characters (LF) have been replaced by carriage return linefeed 599 * pairs (CR/LF). 600 * 601 ******************************************************************************/ 602 603 static ACPI_STATUS 604 AcCheckTextModeCorruption ( 605 ACPI_TABLE_HEADER *Table) 606 { 607 UINT32 i; 608 UINT32 Pairs = 0; 609 UINT8 *Buffer = ACPI_CAST_PTR (UINT8, Table); 610 611 612 /* Scan entire table to determine if each LF has been prefixed with a CR */ 613 614 for (i = 1; i < Table->Length; i++) 615 { 616 if (Buffer[i] == 0x0A) 617 { 618 if (Buffer[i - 1] != 0x0D) 619 { 620 /* The LF does not have a preceding CR, table not corrupted */ 621 622 return (AE_OK); 623 } 624 else 625 { 626 /* Found a CR/LF pair */ 627 628 Pairs++; 629 } 630 631 i++; 632 } 633 } 634 635 if (!Pairs) 636 { 637 return (AE_OK); 638 } 639 640 /* 641 * Entire table scanned, each CR is part of a CR/LF pair -- 642 * meaning that the table was treated as a text file somewhere. 643 * 644 * NOTE: We can't "fix" the table, because any existing CR/LF pairs in the 645 * original table are left untouched by the text conversion process -- 646 * meaning that we cannot simply replace CR/LF pairs with LFs. 647 */ 648 AcpiOsPrintf ("Table has been corrupted by text mode conversion\n"); 649 AcpiOsPrintf ("All LFs (%u) were changed to CR/LF pairs\n", Pairs); 650 AcpiOsPrintf ("Table cannot be repaired!\n"); 651 652 return (AE_BAD_VALUE); 653 } 654