1 /****************************************************************************** 2 * 3 * Module Name: aslutils -- compiler utilities 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2018, Intel Corp. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions, and the following disclaimer, 16 * without modification. 17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18 * substantially similar to the "NO WARRANTY" disclaimer below 19 * ("Disclaimer") and any redistribution must be conditioned upon 20 * including a substantially similar Disclaimer requirement for further 21 * binary redistribution. 22 * 3. Neither the names of the above-listed copyright holders nor the names 23 * of any contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * Alternatively, this software may be distributed under the terms of the 27 * GNU General Public License ("GPL") version 2 as published by the Free 28 * Software Foundation. 29 * 30 * NO WARRANTY 31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGES. 42 */ 43 44 #include "aslcompiler.h" 45 #include "aslcompiler.y.h" 46 #include "acdisasm.h" 47 #include "acnamesp.h" 48 #include "amlcode.h" 49 #include "acapps.h" 50 #include <sys/stat.h> 51 52 53 #define _COMPONENT ACPI_COMPILER 54 ACPI_MODULE_NAME ("aslutils") 55 56 57 /* Local prototypes */ 58 59 static void 60 UtPadNameWithUnderscores ( 61 char *NameSeg, 62 char *PaddedNameSeg); 63 64 static void 65 UtAttachNameseg ( 66 ACPI_PARSE_OBJECT *Op, 67 char *Name); 68 69 70 /******************************************************************************* 71 * 72 * FUNCTION: UtIsBigEndianMachine 73 * 74 * PARAMETERS: None 75 * 76 * RETURN: TRUE if machine is big endian 77 * FALSE if machine is little endian 78 * 79 * DESCRIPTION: Detect whether machine is little endian or big endian. 80 * 81 ******************************************************************************/ 82 83 UINT8 84 UtIsBigEndianMachine ( 85 void) 86 { 87 union { 88 UINT32 Integer; 89 UINT8 Bytes[4]; 90 } Overlay = {0xFF000000}; 91 92 93 return (Overlay.Bytes[0]); /* Returns 0xFF (TRUE) for big endian */ 94 } 95 96 97 /****************************************************************************** 98 * 99 * FUNCTION: UtQueryForOverwrite 100 * 101 * PARAMETERS: Pathname - Output filename 102 * 103 * RETURN: TRUE if file does not exist or overwrite is authorized 104 * 105 * DESCRIPTION: Query for file overwrite if it already exists. 106 * 107 ******************************************************************************/ 108 109 BOOLEAN 110 UtQueryForOverwrite ( 111 char *Pathname) 112 { 113 struct stat StatInfo; 114 115 116 if (!stat (Pathname, &StatInfo)) 117 { 118 fprintf (stderr, "Target file \"%s\" already exists, overwrite? [y|n] ", 119 Pathname); 120 121 if (getchar () != 'y') 122 { 123 return (FALSE); 124 } 125 } 126 127 return (TRUE); 128 } 129 130 131 /******************************************************************************* 132 * 133 * FUNCTION: UtNodeIsDescendantOf 134 * 135 * PARAMETERS: Node1 - Child node 136 * Node2 - Possible parent node 137 * 138 * RETURN: Boolean 139 * 140 * DESCRIPTION: Returns TRUE if Node1 is a descendant of Node2. Otherwise, 141 * return FALSE. Note, we assume a NULL Node2 element to be the 142 * topmost (root) scope. All nodes are descendants of the root. 143 * Note: Nodes at the same level (siblings) are not considered 144 * descendants. 145 * 146 ******************************************************************************/ 147 148 BOOLEAN 149 UtNodeIsDescendantOf ( 150 ACPI_NAMESPACE_NODE *Node1, 151 ACPI_NAMESPACE_NODE *Node2) 152 { 153 154 if (Node1 == Node2) 155 { 156 return (FALSE); 157 } 158 159 if (!Node2) 160 { 161 return (TRUE); /* All nodes descend from the root */ 162 } 163 164 /* Walk upward until the root is reached or parent is found */ 165 166 while (Node1) 167 { 168 if (Node1 == Node2) 169 { 170 return (TRUE); 171 } 172 173 Node1 = Node1->Parent; 174 } 175 176 return (FALSE); 177 } 178 179 180 /******************************************************************************* 181 * 182 * FUNCTION: UtGetParentMethod 183 * 184 * PARAMETERS: Node - Namespace node for any object 185 * 186 * RETURN: Namespace node for the parent method 187 * NULL - object is not within a method 188 * 189 * DESCRIPTION: Find the parent (owning) method node for a namespace object 190 * 191 ******************************************************************************/ 192 193 void * 194 UtGetParentMethod ( 195 ACPI_NAMESPACE_NODE *Node) 196 { 197 ACPI_NAMESPACE_NODE *ParentNode; 198 199 200 if (!Node) 201 { 202 return (NULL); 203 } 204 205 /* Walk upward until a method is found, or the root is reached */ 206 207 ParentNode = Node->Parent; 208 while (ParentNode) 209 { 210 if (ParentNode->Type == ACPI_TYPE_METHOD) 211 { 212 return (ParentNode); 213 } 214 215 ParentNode = ParentNode->Parent; 216 } 217 218 return (NULL); /* Object is not within a control method */ 219 } 220 221 222 /******************************************************************************* 223 * 224 * FUNCTION: UtDisplaySupportedTables 225 * 226 * PARAMETERS: None 227 * 228 * RETURN: None 229 * 230 * DESCRIPTION: Print all supported ACPI table names. 231 * 232 ******************************************************************************/ 233 234 void 235 UtDisplaySupportedTables ( 236 void) 237 { 238 const AH_TABLE *TableData; 239 UINT32 i; 240 241 242 printf ("\nACPI tables supported by iASL version %8.8X:\n" 243 " (Compiler, Disassembler, Template Generator)\n\n", 244 ACPI_CA_VERSION); 245 246 /* All ACPI tables with the common table header */ 247 248 printf ("\n Supported ACPI tables:\n"); 249 for (TableData = Gbl_AcpiSupportedTables, i = 1; 250 TableData->Signature; TableData++, i++) 251 { 252 printf ("%8u) %s %s\n", i, 253 TableData->Signature, TableData->Description); 254 } 255 } 256 257 258 /******************************************************************************* 259 * 260 * FUNCTION: UtDisplayConstantOpcodes 261 * 262 * PARAMETERS: None 263 * 264 * RETURN: None 265 * 266 * DESCRIPTION: Print AML opcodes that can be used in constant expressions. 267 * 268 ******************************************************************************/ 269 270 void 271 UtDisplayConstantOpcodes ( 272 void) 273 { 274 UINT32 i; 275 276 277 printf ("Constant expression opcode information\n\n"); 278 279 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++) 280 { 281 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT) 282 { 283 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name); 284 } 285 } 286 } 287 288 289 /******************************************************************************* 290 * 291 * FUNCTION: UtBeginEvent 292 * 293 * PARAMETERS: Name - Ascii name of this event 294 * 295 * RETURN: Event number (integer index) 296 * 297 * DESCRIPTION: Saves the current time with this event 298 * 299 ******************************************************************************/ 300 301 UINT8 302 UtBeginEvent ( 303 char *Name) 304 { 305 306 if (AslGbl_NextEvent >= ASL_NUM_EVENTS) 307 { 308 AcpiOsPrintf ("Ran out of compiler event structs!\n"); 309 return (AslGbl_NextEvent); 310 } 311 312 /* Init event with current (start) time */ 313 314 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer (); 315 AslGbl_Events[AslGbl_NextEvent].EventName = Name; 316 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE; 317 return (AslGbl_NextEvent++); 318 } 319 320 321 /******************************************************************************* 322 * 323 * FUNCTION: UtEndEvent 324 * 325 * PARAMETERS: Event - Event number (integer index) 326 * 327 * RETURN: None 328 * 329 * DESCRIPTION: Saves the current time (end time) with this event 330 * 331 ******************************************************************************/ 332 333 void 334 UtEndEvent ( 335 UINT8 Event) 336 { 337 338 if (Event >= ASL_NUM_EVENTS) 339 { 340 return; 341 } 342 343 /* Insert end time for event */ 344 345 AslGbl_Events[Event].EndTime = AcpiOsGetTimer (); 346 } 347 348 349 /******************************************************************************* 350 * 351 * FUNCTION: DbgPrint 352 * 353 * PARAMETERS: Type - Type of output 354 * Fmt - Printf format string 355 * ... - variable printf list 356 * 357 * RETURN: None 358 * 359 * DESCRIPTION: Conditional print statement. Prints to stderr only if the 360 * debug flag is set. 361 * 362 ******************************************************************************/ 363 364 void 365 DbgPrint ( 366 UINT32 Type, 367 char *Fmt, 368 ...) 369 { 370 va_list Args; 371 372 373 if (!Gbl_DebugFlag) 374 { 375 return; 376 } 377 378 if ((Type == ASL_PARSE_OUTPUT) && 379 (!(AslCompilerdebug))) 380 { 381 return; 382 } 383 384 va_start (Args, Fmt); 385 (void) vfprintf (stderr, Fmt, Args); 386 va_end (Args); 387 return; 388 } 389 390 391 /******************************************************************************* 392 * 393 * FUNCTION: UtSetParseOpName 394 * 395 * PARAMETERS: Op - Parse op to be named. 396 * 397 * RETURN: None 398 * 399 * DESCRIPTION: Insert the ascii name of the parse opcode 400 * 401 ******************************************************************************/ 402 403 void 404 UtSetParseOpName ( 405 ACPI_PARSE_OBJECT *Op) 406 { 407 408 AcpiUtSafeStrncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode), 409 ACPI_MAX_PARSEOP_NAME); 410 } 411 412 413 /******************************************************************************* 414 * 415 * FUNCTION: UtDisplaySummary 416 * 417 * PARAMETERS: FileID - ID of outpout file 418 * 419 * RETURN: None 420 * 421 * DESCRIPTION: Display compilation statistics 422 * 423 ******************************************************************************/ 424 425 void 426 UtDisplaySummary ( 427 UINT32 FileId) 428 { 429 UINT32 i; 430 431 432 if (FileId != ASL_FILE_STDOUT) 433 { 434 /* Compiler name and version number */ 435 436 FlPrintFile (FileId, "%s version %X [%s]\n\n", 437 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, "2017-01-19"); 438 } 439 440 /* Summary of main input and output files */ 441 442 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA) 443 { 444 FlPrintFile (FileId, 445 "%-14s %s - %u lines, %u bytes, %u fields\n", 446 "Table Input:", 447 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber, 448 Gbl_InputByteCount, Gbl_InputFieldCount); 449 450 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors)) 451 { 452 FlPrintFile (FileId, 453 "%-14s %s - %u bytes\n", 454 "Binary Output:", 455 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength); 456 } 457 } 458 else 459 { 460 FlPrintFile (FileId, 461 "%-14s %s - %u lines, %u bytes, %u keywords\n", 462 "ASL Input:", 463 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber, 464 Gbl_OriginalInputFileSize, TotalKeywords); 465 466 /* AML summary */ 467 468 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors)) 469 { 470 if (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle) 471 { 472 FlPrintFile (FileId, 473 "%-14s %s - %u bytes, %u named objects, " 474 "%u executable opcodes\n", 475 "AML Output:", 476 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, 477 FlGetFileSize (ASL_FILE_AML_OUTPUT), 478 TotalNamedObjects, TotalExecutableOpcodes); 479 } 480 } 481 } 482 483 /* Display summary of any optional files */ 484 485 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++) 486 { 487 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle) 488 { 489 continue; 490 } 491 492 /* .SRC is a temp file unless specifically requested */ 493 494 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag)) 495 { 496 continue; 497 } 498 499 /* .PRE is the preprocessor intermediate file */ 500 501 if ((i == ASL_FILE_PREPROCESSOR) && (!Gbl_KeepPreprocessorTempFile)) 502 { 503 continue; 504 } 505 506 FlPrintFile (FileId, "%14s %s - %u bytes\n", 507 Gbl_Files[i].ShortDescription, 508 Gbl_Files[i].Filename, FlGetFileSize (i)); 509 } 510 511 /* Error summary */ 512 513 FlPrintFile (FileId, 514 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks", 515 Gbl_ExceptionCount[ASL_ERROR], 516 Gbl_ExceptionCount[ASL_WARNING] + 517 Gbl_ExceptionCount[ASL_WARNING2] + 518 Gbl_ExceptionCount[ASL_WARNING3], 519 Gbl_ExceptionCount[ASL_REMARK]); 520 521 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA) 522 { 523 FlPrintFile (FileId, ", %u Optimizations", 524 Gbl_ExceptionCount[ASL_OPTIMIZATION]); 525 526 if (TotalFolds) 527 { 528 FlPrintFile (FileId, ", %u Constants Folded", TotalFolds); 529 } 530 } 531 532 FlPrintFile (FileId, "\n"); 533 } 534 535 536 /******************************************************************************* 537 * 538 * FUNCTION: UtCheckIntegerRange 539 * 540 * PARAMETERS: Op - Integer parse node 541 * LowValue - Smallest allowed value 542 * HighValue - Largest allowed value 543 * 544 * RETURN: Op if OK, otherwise NULL 545 * 546 * DESCRIPTION: Check integer for an allowable range 547 * 548 ******************************************************************************/ 549 550 ACPI_PARSE_OBJECT * 551 UtCheckIntegerRange ( 552 ACPI_PARSE_OBJECT *Op, 553 UINT32 LowValue, 554 UINT32 HighValue) 555 { 556 557 if (!Op) 558 { 559 return (NULL); 560 } 561 562 if ((Op->Asl.Value.Integer < LowValue) || 563 (Op->Asl.Value.Integer > HighValue)) 564 { 565 snprintf (MsgBuffer, sizeof(MsgBuffer), "0x%X, allowable: 0x%X-0x%X", 566 (UINT32) Op->Asl.Value.Integer, LowValue, HighValue); 567 568 AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer); 569 return (NULL); 570 } 571 572 return (Op); 573 } 574 575 576 /******************************************************************************* 577 * 578 * FUNCTION: UtInternalizeName 579 * 580 * PARAMETERS: ExternalName - Name to convert 581 * ConvertedName - Where the converted name is returned 582 * 583 * RETURN: Status 584 * 585 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name 586 * 587 ******************************************************************************/ 588 589 ACPI_STATUS 590 UtInternalizeName ( 591 char *ExternalName, 592 char **ConvertedName) 593 { 594 ACPI_NAMESTRING_INFO Info; 595 ACPI_STATUS Status; 596 597 598 if (!ExternalName) 599 { 600 return (AE_OK); 601 } 602 603 /* Get the length of the new internal name */ 604 605 Info.ExternalName = ExternalName; 606 AcpiNsGetInternalNameLength (&Info); 607 608 /* We need a segment to store the internal name */ 609 610 Info.InternalName = UtLocalCacheCalloc (Info.Length); 611 612 /* Build the name */ 613 614 Status = AcpiNsBuildInternalName (&Info); 615 if (ACPI_FAILURE (Status)) 616 { 617 return (Status); 618 } 619 620 *ConvertedName = Info.InternalName; 621 return (AE_OK); 622 } 623 624 625 /******************************************************************************* 626 * 627 * FUNCTION: UtPadNameWithUnderscores 628 * 629 * PARAMETERS: NameSeg - Input nameseg 630 * PaddedNameSeg - Output padded nameseg 631 * 632 * RETURN: Padded nameseg. 633 * 634 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full 635 * ACPI_NAME. 636 * 637 ******************************************************************************/ 638 639 static void 640 UtPadNameWithUnderscores ( 641 char *NameSeg, 642 char *PaddedNameSeg) 643 { 644 UINT32 i; 645 646 647 for (i = 0; (i < ACPI_NAME_SIZE); i++) 648 { 649 if (*NameSeg) 650 { 651 *PaddedNameSeg = *NameSeg; 652 NameSeg++; 653 } 654 else 655 { 656 *PaddedNameSeg = '_'; 657 } 658 659 PaddedNameSeg++; 660 } 661 } 662 663 664 /******************************************************************************* 665 * 666 * FUNCTION: UtAttachNameseg 667 * 668 * PARAMETERS: Op - Parent parse node 669 * Name - Full ExternalName 670 * 671 * RETURN: None; Sets the NameSeg field in parent node 672 * 673 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it 674 * in the NameSeg field of the Op. 675 * 676 ******************************************************************************/ 677 678 static void 679 UtAttachNameseg ( 680 ACPI_PARSE_OBJECT *Op, 681 char *Name) 682 { 683 char *NameSeg; 684 char PaddedNameSeg[4]; 685 686 687 if (!Name) 688 { 689 return; 690 } 691 692 /* Look for the last dot in the namepath */ 693 694 NameSeg = strrchr (Name, '.'); 695 if (NameSeg) 696 { 697 /* Found last dot, we have also found the final nameseg */ 698 699 NameSeg++; 700 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg); 701 } 702 else 703 { 704 /* No dots in the namepath, there is only a single nameseg. */ 705 /* Handle prefixes */ 706 707 while (ACPI_IS_ROOT_PREFIX (*Name) || 708 ACPI_IS_PARENT_PREFIX (*Name)) 709 { 710 Name++; 711 } 712 713 /* Remaining string should be one single nameseg */ 714 715 UtPadNameWithUnderscores (Name, PaddedNameSeg); 716 } 717 718 ACPI_MOVE_NAME (Op->Asl.NameSeg, PaddedNameSeg); 719 } 720 721 722 /******************************************************************************* 723 * 724 * FUNCTION: UtAttachNamepathToOwner 725 * 726 * PARAMETERS: Op - Parent parse node 727 * NameOp - Node that contains the name 728 * 729 * RETURN: Sets the ExternalName and Namepath in the parent node 730 * 731 * DESCRIPTION: Store the name in two forms in the parent node: The original 732 * (external) name, and the internalized name that is used within 733 * the ACPI namespace manager. 734 * 735 ******************************************************************************/ 736 737 void 738 UtAttachNamepathToOwner ( 739 ACPI_PARSE_OBJECT *Op, 740 ACPI_PARSE_OBJECT *NameOp) 741 { 742 ACPI_STATUS Status; 743 744 745 /* Full external path */ 746 747 Op->Asl.ExternalName = NameOp->Asl.Value.String; 748 749 /* Save the NameOp for possible error reporting later */ 750 751 Op->Asl.ParentMethod = (void *) NameOp; 752 753 /* Last nameseg of the path */ 754 755 UtAttachNameseg (Op, Op->Asl.ExternalName); 756 757 /* Create internalized path */ 758 759 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath); 760 if (ACPI_FAILURE (Status)) 761 { 762 /* TBD: abort on no memory */ 763 } 764 } 765 766 767 /******************************************************************************* 768 * 769 * FUNCTION: UtDoConstant 770 * 771 * PARAMETERS: String - Hex/Decimal/Octal 772 * 773 * RETURN: Converted Integer 774 * 775 * DESCRIPTION: Convert a string to an integer, with overflow/error checking. 776 * 777 ******************************************************************************/ 778 779 UINT64 780 UtDoConstant ( 781 char *String) 782 { 783 ACPI_STATUS Status; 784 UINT64 ConvertedInteger; 785 char ErrBuf[64]; 786 787 788 Status = AcpiUtStrtoul64 (String, &ConvertedInteger); 789 if (ACPI_FAILURE (Status)) 790 { 791 snprintf (ErrBuf, sizeof(ErrBuf), "While creating 64-bit constant: %s\n", 792 AcpiFormatException (Status)); 793 794 AslCommonError (ASL_ERROR, ASL_MSG_SYNTAX, Gbl_CurrentLineNumber, 795 Gbl_LogicalLineNumber, Gbl_CurrentLineOffset, 796 Gbl_CurrentColumn, Gbl_Files[ASL_FILE_INPUT].Filename, ErrBuf); 797 } 798 799 return (ConvertedInteger); 800 } 801