1 /****************************************************************************** 2 * 3 * Module Name: nsrepair - Repair for objects returned by predefined methods 4 * 5 *****************************************************************************/ 6 7 /* 8 * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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 "acpi.h" 45 #include "accommon.h" 46 #include "acnamesp.h" 47 #include "acinterp.h" 48 #include "acpredef.h" 49 #include "amlresrc.h" 50 51 #define _COMPONENT ACPI_NAMESPACE 52 ACPI_MODULE_NAME ("nsrepair") 53 54 55 /******************************************************************************* 56 * 57 * This module attempts to repair or convert objects returned by the 58 * predefined methods to an object type that is expected, as per the ACPI 59 * specification. The need for this code is dictated by the many machines that 60 * return incorrect types for the standard predefined methods. Performing these 61 * conversions here, in one place, eliminates the need for individual ACPI 62 * device drivers to do the same. Note: Most of these conversions are different 63 * than the internal object conversion routines used for implicit object 64 * conversion. 65 * 66 * The following conversions can be performed as necessary: 67 * 68 * Integer -> String 69 * Integer -> Buffer 70 * String -> Integer 71 * String -> Buffer 72 * Buffer -> Integer 73 * Buffer -> String 74 * Buffer -> Package of Integers 75 * Package -> Package of one Package 76 * 77 * Additional conversions that are available: 78 * Convert a null return or zero return value to an EndTag descriptor 79 * Convert an ASCII string to a Unicode buffer 80 * 81 * An incorrect standalone object is wrapped with required outer package 82 * 83 * Additional possible repairs: 84 * Required package elements that are NULL replaced by Integer/String/Buffer 85 * 86 ******************************************************************************/ 87 88 89 /* Local prototypes */ 90 91 static const ACPI_SIMPLE_REPAIR_INFO * 92 AcpiNsMatchSimpleRepair ( 93 ACPI_NAMESPACE_NODE *Node, 94 UINT32 ReturnBtype, 95 UINT32 PackageIndex); 96 97 98 /* 99 * Special but simple repairs for some names. 100 * 101 * 2nd argument: Unexpected types that can be repaired 102 */ 103 static const ACPI_SIMPLE_REPAIR_INFO AcpiObjectRepairInfo[] = 104 { 105 /* Resource descriptor conversions */ 106 107 { "_CRS", ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER | ACPI_RTYPE_NONE, 108 ACPI_NOT_PACKAGE_ELEMENT, 109 AcpiNsConvertToResource }, 110 { "_DMA", ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER | ACPI_RTYPE_NONE, 111 ACPI_NOT_PACKAGE_ELEMENT, 112 AcpiNsConvertToResource }, 113 { "_PRS", ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER | ACPI_RTYPE_NONE, 114 ACPI_NOT_PACKAGE_ELEMENT, 115 AcpiNsConvertToResource }, 116 117 /* Object reference conversions */ 118 119 { "_DEP", ACPI_RTYPE_STRING, ACPI_ALL_PACKAGE_ELEMENTS, 120 AcpiNsConvertToReference }, 121 122 /* Unicode conversions */ 123 124 { "_MLS", ACPI_RTYPE_STRING, 1, 125 AcpiNsConvertToUnicode }, 126 { "_STR", ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER, 127 ACPI_NOT_PACKAGE_ELEMENT, 128 AcpiNsConvertToUnicode }, 129 { {0,0,0,0}, 0, 0, NULL } /* Table terminator */ 130 }; 131 132 133 /******************************************************************************* 134 * 135 * FUNCTION: AcpiNsSimpleRepair 136 * 137 * PARAMETERS: Info - Method execution information block 138 * ExpectedBtypes - Object types expected 139 * PackageIndex - Index of object within parent package (if 140 * applicable - ACPI_NOT_PACKAGE_ELEMENT 141 * otherwise) 142 * ReturnObjectPtr - Pointer to the object returned from the 143 * evaluation of a method or object 144 * 145 * RETURN: Status. AE_OK if repair was successful. 146 * 147 * DESCRIPTION: Attempt to repair/convert a return object of a type that was 148 * not expected. 149 * 150 ******************************************************************************/ 151 152 ACPI_STATUS 153 AcpiNsSimpleRepair ( 154 ACPI_EVALUATE_INFO *Info, 155 UINT32 ExpectedBtypes, 156 UINT32 PackageIndex, 157 ACPI_OPERAND_OBJECT **ReturnObjectPtr) 158 { 159 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr; 160 ACPI_OPERAND_OBJECT *NewObject = NULL; 161 ACPI_STATUS Status; 162 const ACPI_SIMPLE_REPAIR_INFO *Predefined; 163 164 165 ACPI_FUNCTION_NAME (NsSimpleRepair); 166 167 168 /* 169 * Special repairs for certain names that are in the repair table. 170 * Check if this name is in the list of repairable names. 171 */ 172 Predefined = AcpiNsMatchSimpleRepair (Info->Node, 173 Info->ReturnBtype, PackageIndex); 174 if (Predefined) 175 { 176 if (!ReturnObject) 177 { 178 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, 179 ACPI_WARN_ALWAYS, "Missing expected return value")); 180 } 181 182 Status = Predefined->ObjectConverter (Info->Node, ReturnObject, 183 &NewObject); 184 if (ACPI_FAILURE (Status)) 185 { 186 /* A fatal error occurred during a conversion */ 187 188 ACPI_EXCEPTION ((AE_INFO, Status, 189 "During return object analysis")); 190 return (Status); 191 } 192 if (NewObject) 193 { 194 goto ObjectRepaired; 195 } 196 } 197 198 /* 199 * Do not perform simple object repair unless the return type is not 200 * expected. 201 */ 202 if (Info->ReturnBtype & ExpectedBtypes) 203 { 204 return (AE_OK); 205 } 206 207 /* 208 * At this point, we know that the type of the returned object was not 209 * one of the expected types for this predefined name. Attempt to 210 * repair the object by converting it to one of the expected object 211 * types for this predefined name. 212 */ 213 214 /* 215 * If there is no return value, check if we require a return value for 216 * this predefined name. Either one return value is expected, or none, 217 * for both methods and other objects. 218 * 219 * Try to fix if there was no return object. Warning if failed to fix. 220 */ 221 if (!ReturnObject) 222 { 223 if (ExpectedBtypes) 224 { 225 if (!(ExpectedBtypes & ACPI_RTYPE_NONE) && 226 PackageIndex != ACPI_NOT_PACKAGE_ELEMENT) 227 { 228 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, 229 ACPI_WARN_ALWAYS, "Found unexpected NULL package element")); 230 231 Status = AcpiNsRepairNullElement (Info, ExpectedBtypes, 232 PackageIndex, ReturnObjectPtr); 233 if (ACPI_SUCCESS (Status)) 234 { 235 return (AE_OK); /* Repair was successful */ 236 } 237 } 238 239 if (ExpectedBtypes != ACPI_RTYPE_NONE) 240 { 241 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, 242 ACPI_WARN_ALWAYS, 243 "Missing expected return value")); 244 return (AE_AML_NO_RETURN_VALUE); 245 } 246 } 247 } 248 249 if (ExpectedBtypes & ACPI_RTYPE_INTEGER) 250 { 251 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject); 252 if (ACPI_SUCCESS (Status)) 253 { 254 goto ObjectRepaired; 255 } 256 } 257 if (ExpectedBtypes & ACPI_RTYPE_STRING) 258 { 259 Status = AcpiNsConvertToString (ReturnObject, &NewObject); 260 if (ACPI_SUCCESS (Status)) 261 { 262 goto ObjectRepaired; 263 } 264 } 265 if (ExpectedBtypes & ACPI_RTYPE_BUFFER) 266 { 267 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject); 268 if (ACPI_SUCCESS (Status)) 269 { 270 goto ObjectRepaired; 271 } 272 } 273 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE) 274 { 275 /* 276 * A package is expected. We will wrap the existing object with a 277 * new package object. It is often the case that if a variable-length 278 * package is required, but there is only a single object needed, the 279 * BIOS will return that object instead of wrapping it with a Package 280 * object. Note: after the wrapping, the package will be validated 281 * for correct contents (expected object type or types). 282 */ 283 Status = AcpiNsWrapWithPackage (Info, ReturnObject, &NewObject); 284 if (ACPI_SUCCESS (Status)) 285 { 286 /* 287 * The original object just had its reference count 288 * incremented for being inserted into the new package. 289 */ 290 *ReturnObjectPtr = NewObject; /* New Package object */ 291 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED; 292 return (AE_OK); 293 } 294 } 295 296 /* We cannot repair this object */ 297 298 return (AE_AML_OPERAND_TYPE); 299 300 301 ObjectRepaired: 302 303 /* Object was successfully repaired */ 304 305 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT) 306 { 307 /* Update reference count of new object */ 308 309 if (!(Info->ReturnFlags & ACPI_OBJECT_WRAPPED)) 310 { 311 NewObject->Common.ReferenceCount = 312 ReturnObject->Common.ReferenceCount; 313 } 314 315 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR, 316 "%s: Converted %s to expected %s at Package index %u\n", 317 Info->FullPathname, AcpiUtGetObjectTypeName (ReturnObject), 318 AcpiUtGetObjectTypeName (NewObject), PackageIndex)); 319 } 320 else 321 { 322 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR, 323 "%s: Converted %s to expected %s\n", 324 Info->FullPathname, AcpiUtGetObjectTypeName (ReturnObject), 325 AcpiUtGetObjectTypeName (NewObject))); 326 } 327 328 /* Delete old object, install the new return object */ 329 330 AcpiUtRemoveReference (ReturnObject); 331 *ReturnObjectPtr = NewObject; 332 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED; 333 return (AE_OK); 334 } 335 336 337 /****************************************************************************** 338 * 339 * FUNCTION: AcpiNsMatchSimpleRepair 340 * 341 * PARAMETERS: Node - Namespace node for the method/object 342 * ReturnBtype - Object type that was returned 343 * PackageIndex - Index of object within parent package (if 344 * applicable - ACPI_NOT_PACKAGE_ELEMENT 345 * otherwise) 346 * 347 * RETURN: Pointer to entry in repair table. NULL indicates not found. 348 * 349 * DESCRIPTION: Check an object name against the repairable object list. 350 * 351 *****************************************************************************/ 352 353 static const ACPI_SIMPLE_REPAIR_INFO * 354 AcpiNsMatchSimpleRepair ( 355 ACPI_NAMESPACE_NODE *Node, 356 UINT32 ReturnBtype, 357 UINT32 PackageIndex) 358 { 359 const ACPI_SIMPLE_REPAIR_INFO *ThisName; 360 361 362 /* Search info table for a repairable predefined method/object name */ 363 364 ThisName = AcpiObjectRepairInfo; 365 while (ThisName->ObjectConverter) 366 { 367 if (ACPI_COMPARE_NAMESEG (Node->Name.Ascii, ThisName->Name)) 368 { 369 /* Check if we can actually repair this name/type combination */ 370 371 if ((ReturnBtype & ThisName->UnexpectedBtypes) && 372 (ThisName->PackageIndex == ACPI_ALL_PACKAGE_ELEMENTS || 373 PackageIndex == ThisName->PackageIndex)) 374 { 375 return (ThisName); 376 } 377 378 return (NULL); 379 } 380 381 ThisName++; 382 } 383 384 return (NULL); /* Name was not found in the repair table */ 385 } 386 387 388 /******************************************************************************* 389 * 390 * FUNCTION: AcpiNsRepairNullElement 391 * 392 * PARAMETERS: Info - Method execution information block 393 * ExpectedBtypes - Object types expected 394 * PackageIndex - Index of object within parent package (if 395 * applicable - ACPI_NOT_PACKAGE_ELEMENT 396 * otherwise) 397 * ReturnObjectPtr - Pointer to the object returned from the 398 * evaluation of a method or object 399 * 400 * RETURN: Status. AE_OK if repair was successful. 401 * 402 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object. 403 * 404 ******************************************************************************/ 405 406 ACPI_STATUS 407 AcpiNsRepairNullElement ( 408 ACPI_EVALUATE_INFO *Info, 409 UINT32 ExpectedBtypes, 410 UINT32 PackageIndex, 411 ACPI_OPERAND_OBJECT **ReturnObjectPtr) 412 { 413 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr; 414 ACPI_OPERAND_OBJECT *NewObject; 415 416 417 ACPI_FUNCTION_NAME (NsRepairNullElement); 418 419 420 /* No repair needed if return object is non-NULL */ 421 422 if (ReturnObject) 423 { 424 return (AE_OK); 425 } 426 427 /* 428 * Attempt to repair a NULL element of a Package object. This applies to 429 * predefined names that return a fixed-length package and each element 430 * is required. It does not apply to variable-length packages where NULL 431 * elements are allowed, especially at the end of the package. 432 */ 433 if (ExpectedBtypes & ACPI_RTYPE_INTEGER) 434 { 435 /* Need an Integer - create a zero-value integer */ 436 437 NewObject = AcpiUtCreateIntegerObject ((UINT64) 0); 438 } 439 else if (ExpectedBtypes & ACPI_RTYPE_STRING) 440 { 441 /* Need a String - create a NULL string */ 442 443 NewObject = AcpiUtCreateStringObject (0); 444 } 445 else if (ExpectedBtypes & ACPI_RTYPE_BUFFER) 446 { 447 /* Need a Buffer - create a zero-length buffer */ 448 449 NewObject = AcpiUtCreateBufferObject (0); 450 } 451 else 452 { 453 /* Error for all other expected types */ 454 455 return (AE_AML_OPERAND_TYPE); 456 } 457 458 if (!NewObject) 459 { 460 return (AE_NO_MEMORY); 461 } 462 463 /* Set the reference count according to the parent Package object */ 464 465 NewObject->Common.ReferenceCount = 466 Info->ParentPackage->Common.ReferenceCount; 467 468 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR, 469 "%s: Converted NULL package element to expected %s at index %u\n", 470 Info->FullPathname, AcpiUtGetObjectTypeName (NewObject), 471 PackageIndex)); 472 473 *ReturnObjectPtr = NewObject; 474 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED; 475 return (AE_OK); 476 } 477 478 479 /****************************************************************************** 480 * 481 * FUNCTION: AcpiNsRemoveNullElements 482 * 483 * PARAMETERS: Info - Method execution information block 484 * PackageType - An AcpiReturnPackageTypes value 485 * ObjDesc - A Package object 486 * 487 * RETURN: None. 488 * 489 * DESCRIPTION: Remove all NULL package elements from packages that contain 490 * a variable number of subpackages. For these types of 491 * packages, NULL elements can be safely removed. 492 * 493 *****************************************************************************/ 494 495 void 496 AcpiNsRemoveNullElements ( 497 ACPI_EVALUATE_INFO *Info, 498 UINT8 PackageType, 499 ACPI_OPERAND_OBJECT *ObjDesc) 500 { 501 ACPI_OPERAND_OBJECT **Source; 502 ACPI_OPERAND_OBJECT **Dest; 503 UINT32 Count; 504 UINT32 NewCount; 505 UINT32 i; 506 507 508 ACPI_FUNCTION_NAME (NsRemoveNullElements); 509 510 511 /* 512 * We can safely remove all NULL elements from these package types: 513 * PTYPE1_VAR packages contain a variable number of simple data types. 514 * PTYPE2 packages contain a variable number of subpackages. 515 */ 516 switch (PackageType) 517 { 518 case ACPI_PTYPE1_VAR: 519 case ACPI_PTYPE2: 520 case ACPI_PTYPE2_COUNT: 521 case ACPI_PTYPE2_PKG_COUNT: 522 case ACPI_PTYPE2_FIXED: 523 case ACPI_PTYPE2_MIN: 524 case ACPI_PTYPE2_REV_FIXED: 525 case ACPI_PTYPE2_FIX_VAR: 526 break; 527 528 default: 529 case ACPI_PTYPE2_VAR_VAR: 530 case ACPI_PTYPE1_FIXED: 531 case ACPI_PTYPE1_OPTION: 532 return; 533 } 534 535 Count = ObjDesc->Package.Count; 536 NewCount = Count; 537 538 Source = ObjDesc->Package.Elements; 539 Dest = Source; 540 541 /* Examine all elements of the package object, remove nulls */ 542 543 for (i = 0; i < Count; i++) 544 { 545 if (!*Source) 546 { 547 NewCount--; 548 } 549 else 550 { 551 *Dest = *Source; 552 Dest++; 553 } 554 555 Source++; 556 } 557 558 /* Update parent package if any null elements were removed */ 559 560 if (NewCount < Count) 561 { 562 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR, 563 "%s: Found and removed %u NULL elements\n", 564 Info->FullPathname, (Count - NewCount))); 565 566 /* NULL terminate list and update the package count */ 567 568 *Dest = NULL; 569 ObjDesc->Package.Count = NewCount; 570 } 571 } 572 573 574 /******************************************************************************* 575 * 576 * FUNCTION: AcpiNsWrapWithPackage 577 * 578 * PARAMETERS: Info - Method execution information block 579 * OriginalObject - Pointer to the object to repair. 580 * ObjDescPtr - The new package object is returned here 581 * 582 * RETURN: Status, new object in *ObjDescPtr 583 * 584 * DESCRIPTION: Repair a common problem with objects that are defined to 585 * return a variable-length Package of sub-objects. If there is 586 * only one sub-object, some BIOS code mistakenly simply declares 587 * the single object instead of a Package with one sub-object. 588 * This function attempts to repair this error by wrapping a 589 * Package object around the original object, creating the 590 * correct and expected Package with one sub-object. 591 * 592 * Names that can be repaired in this manner include: 593 * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS, 594 * _BCL, _DOD, _FIX, _Sx 595 * 596 ******************************************************************************/ 597 598 ACPI_STATUS 599 AcpiNsWrapWithPackage ( 600 ACPI_EVALUATE_INFO *Info, 601 ACPI_OPERAND_OBJECT *OriginalObject, 602 ACPI_OPERAND_OBJECT **ObjDescPtr) 603 { 604 ACPI_OPERAND_OBJECT *PkgObjDesc; 605 606 607 ACPI_FUNCTION_NAME (NsWrapWithPackage); 608 609 610 /* 611 * Create the new outer package and populate it. The new 612 * package will have a single element, the lone sub-object. 613 */ 614 PkgObjDesc = AcpiUtCreatePackageObject (1); 615 if (!PkgObjDesc) 616 { 617 return (AE_NO_MEMORY); 618 } 619 620 PkgObjDesc->Package.Elements[0] = OriginalObject; 621 622 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR, 623 "%s: Wrapped %s with expected Package object\n", 624 Info->FullPathname, AcpiUtGetObjectTypeName (OriginalObject))); 625 626 /* Return the new object in the object pointer */ 627 628 *ObjDescPtr = PkgObjDesc; 629 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED; 630 return (AE_OK); 631 } 632