1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12 * All rights reserved.
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
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
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;
37 * The above copyright and patent license is granted only if the following
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.
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
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
72 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
80 * 4. Disclaimer and Export Compliance
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
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
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.
114 *****************************************************************************/
116 #define __NSREPAIR_C__
119 #include "accommon.h"
120 #include "acnamesp.h"
121 #include "acinterp.h"
123 #define _COMPONENT ACPI_NAMESPACE
124 ACPI_MODULE_NAME ("nsrepair")
127 /*******************************************************************************
129 * This module attempts to repair or convert objects returned by the
130 * predefined methods to an object type that is expected, as per the ACPI
131 * specification. The need for this code is dictated by the many machines that
132 * return incorrect types for the standard predefined methods. Performing these
133 * conversions here, in one place, eliminates the need for individual ACPI
134 * device drivers to do the same. Note: Most of these conversions are different
135 * than the internal object conversion routines used for implicit object
138 * The following conversions can be performed as necessary:
146 * Buffer -> Package of Integers
147 * Package -> Package of one Package
149 ******************************************************************************/
152 /* Local prototypes */
155 AcpiNsConvertToInteger (
156 ACPI_OPERAND_OBJECT *OriginalObject,
157 ACPI_OPERAND_OBJECT **ReturnObject);
160 AcpiNsConvertToString (
161 ACPI_OPERAND_OBJECT *OriginalObject,
162 ACPI_OPERAND_OBJECT **ReturnObject);
165 AcpiNsConvertToBuffer (
166 ACPI_OPERAND_OBJECT *OriginalObject,
167 ACPI_OPERAND_OBJECT **ReturnObject);
170 AcpiNsConvertToPackage (
171 ACPI_OPERAND_OBJECT *OriginalObject,
172 ACPI_OPERAND_OBJECT **ReturnObject);
175 /*******************************************************************************
177 * FUNCTION: AcpiNsRepairObject
179 * PARAMETERS: Data - Pointer to validation data structure
180 * ExpectedBtypes - Object types expected
181 * PackageIndex - Index of object within parent package (if
182 * applicable - ACPI_NOT_PACKAGE_ELEMENT
184 * ReturnObjectPtr - Pointer to the object returned from the
185 * evaluation of a method or object
187 * RETURN: Status. AE_OK if repair was successful.
189 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
192 ******************************************************************************/
196 ACPI_PREDEFINED_DATA *Data,
197 UINT32 ExpectedBtypes,
199 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
201 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
202 ACPI_OPERAND_OBJECT *NewObject;
206 ACPI_FUNCTION_NAME (NsRepairObject);
210 * At this point, we know that the type of the returned object was not
211 * one of the expected types for this predefined name. Attempt to
212 * repair the object by converting it to one of the expected object
213 * types for this predefined name.
215 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
217 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject);
218 if (ACPI_SUCCESS (Status))
223 if (ExpectedBtypes & ACPI_RTYPE_STRING)
225 Status = AcpiNsConvertToString (ReturnObject, &NewObject);
226 if (ACPI_SUCCESS (Status))
231 if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
233 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject);
234 if (ACPI_SUCCESS (Status))
239 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE)
241 Status = AcpiNsConvertToPackage (ReturnObject, &NewObject);
242 if (ACPI_SUCCESS (Status))
248 /* We cannot repair this object */
250 return (AE_AML_OPERAND_TYPE);
255 /* Object was successfully repaired */
258 * If the original object is a package element, we need to:
259 * 1. Set the reference count of the new object to match the
260 * reference count of the old object.
261 * 2. Decrement the reference count of the original object.
263 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
265 NewObject->Common.ReferenceCount =
266 ReturnObject->Common.ReferenceCount;
268 if (ReturnObject->Common.ReferenceCount > 1)
270 ReturnObject->Common.ReferenceCount--;
273 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
274 "%s: Converted %s to expected %s at index %u\n",
275 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
276 AcpiUtGetObjectTypeName (NewObject), PackageIndex));
280 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
281 "%s: Converted %s to expected %s\n",
282 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
283 AcpiUtGetObjectTypeName (NewObject)));
286 /* Delete old object, install the new return object */
288 AcpiUtRemoveReference (ReturnObject);
289 *ReturnObjectPtr = NewObject;
290 Data->Flags |= ACPI_OBJECT_REPAIRED;
295 /*******************************************************************************
297 * FUNCTION: AcpiNsConvertToInteger
299 * PARAMETERS: OriginalObject - Object to be converted
300 * ReturnObject - Where the new converted object is returned
302 * RETURN: Status. AE_OK if conversion was successful.
304 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
306 ******************************************************************************/
309 AcpiNsConvertToInteger (
310 ACPI_OPERAND_OBJECT *OriginalObject,
311 ACPI_OPERAND_OBJECT **ReturnObject)
313 ACPI_OPERAND_OBJECT *NewObject;
319 switch (OriginalObject->Common.Type)
321 case ACPI_TYPE_STRING:
323 /* String-to-Integer conversion */
325 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
326 ACPI_ANY_BASE, &Value);
327 if (ACPI_FAILURE (Status))
333 case ACPI_TYPE_BUFFER:
335 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
337 if (OriginalObject->Buffer.Length > 8)
339 return (AE_AML_OPERAND_TYPE);
342 /* Extract each buffer byte to create the integer */
344 for (i = 0; i < OriginalObject->Buffer.Length; i++)
346 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
351 return (AE_AML_OPERAND_TYPE);
354 NewObject = AcpiUtCreateIntegerObject (Value);
357 return (AE_NO_MEMORY);
360 *ReturnObject = NewObject;
365 /*******************************************************************************
367 * FUNCTION: AcpiNsConvertToString
369 * PARAMETERS: OriginalObject - Object to be converted
370 * ReturnObject - Where the new converted object is returned
372 * RETURN: Status. AE_OK if conversion was successful.
374 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
376 ******************************************************************************/
379 AcpiNsConvertToString (
380 ACPI_OPERAND_OBJECT *OriginalObject,
381 ACPI_OPERAND_OBJECT **ReturnObject)
383 ACPI_OPERAND_OBJECT *NewObject;
388 switch (OriginalObject->Common.Type)
390 case ACPI_TYPE_INTEGER:
392 * Integer-to-String conversion. Commonly, convert
393 * an integer of value 0 to a NULL string. The last element of
394 * _BIF and _BIX packages occasionally need this fix.
396 if (OriginalObject->Integer.Value == 0)
398 /* Allocate a new NULL string object */
400 NewObject = AcpiUtCreateStringObject (0);
403 return (AE_NO_MEMORY);
408 Status = AcpiExConvertToString (OriginalObject, &NewObject,
409 ACPI_IMPLICIT_CONVERT_HEX);
410 if (ACPI_FAILURE (Status))
417 case ACPI_TYPE_BUFFER:
419 * Buffer-to-String conversion. Use a ToString
420 * conversion, no transform performed on the buffer data. The best
421 * example of this is the _BIF method, where the string data from
422 * the battery is often (incorrectly) returned as buffer object(s).
425 while ((Length < OriginalObject->Buffer.Length) &&
426 (OriginalObject->Buffer.Pointer[Length]))
431 /* Allocate a new string object */
433 NewObject = AcpiUtCreateStringObject (Length);
436 return (AE_NO_MEMORY);
440 * Copy the raw buffer data with no transform. String is already NULL
441 * terminated at Length+1.
443 ACPI_MEMCPY (NewObject->String.Pointer,
444 OriginalObject->Buffer.Pointer, Length);
448 return (AE_AML_OPERAND_TYPE);
451 *ReturnObject = NewObject;
456 /*******************************************************************************
458 * FUNCTION: AcpiNsConvertToBuffer
460 * PARAMETERS: OriginalObject - Object to be converted
461 * ReturnObject - Where the new converted object is returned
463 * RETURN: Status. AE_OK if conversion was successful.
465 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
467 ******************************************************************************/
470 AcpiNsConvertToBuffer (
471 ACPI_OPERAND_OBJECT *OriginalObject,
472 ACPI_OPERAND_OBJECT **ReturnObject)
474 ACPI_OPERAND_OBJECT *NewObject;
476 ACPI_OPERAND_OBJECT **Elements;
482 switch (OriginalObject->Common.Type)
484 case ACPI_TYPE_INTEGER:
486 * Integer-to-Buffer conversion.
487 * Convert the Integer to a packed-byte buffer. _MAT and other
488 * objects need this sometimes, if a read has been performed on a
489 * Field object that is less than or equal to the global integer
490 * size (32 or 64 bits).
492 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
493 if (ACPI_FAILURE (Status))
499 case ACPI_TYPE_STRING:
501 /* String-to-Buffer conversion. Simple data copy */
503 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
506 return (AE_NO_MEMORY);
509 ACPI_MEMCPY (NewObject->Buffer.Pointer,
510 OriginalObject->String.Pointer, OriginalObject->String.Length);
513 case ACPI_TYPE_PACKAGE:
515 * This case is often seen for predefined names that must return a
516 * Buffer object with multiple DWORD integers within. For example,
517 * _FDE and _GTM. The Package can be converted to a Buffer.
520 /* All elements of the Package must be integers */
522 Elements = OriginalObject->Package.Elements;
523 Count = OriginalObject->Package.Count;
525 for (i = 0; i < Count; i++)
528 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
530 return (AE_AML_OPERAND_TYPE);
535 /* Create the new buffer object to replace the Package */
537 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
540 return (AE_NO_MEMORY);
543 /* Copy the package elements (integers) to the buffer as DWORDs */
545 Elements = OriginalObject->Package.Elements;
546 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
548 for (i = 0; i < Count; i++)
550 *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
557 return (AE_AML_OPERAND_TYPE);
560 *ReturnObject = NewObject;
565 /*******************************************************************************
567 * FUNCTION: AcpiNsConvertToPackage
569 * PARAMETERS: OriginalObject - Object to be converted
570 * ReturnObject - Where the new converted object is returned
572 * RETURN: Status. AE_OK if conversion was successful.
574 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
575 * the buffer is converted to a single integer package element.
577 ******************************************************************************/
580 AcpiNsConvertToPackage (
581 ACPI_OPERAND_OBJECT *OriginalObject,
582 ACPI_OPERAND_OBJECT **ReturnObject)
584 ACPI_OPERAND_OBJECT *NewObject;
585 ACPI_OPERAND_OBJECT **Elements;
590 switch (OriginalObject->Common.Type)
592 case ACPI_TYPE_BUFFER:
594 /* Buffer-to-Package conversion */
596 Length = OriginalObject->Buffer.Length;
597 NewObject = AcpiUtCreatePackageObject (Length);
600 return (AE_NO_MEMORY);
603 /* Convert each buffer byte to an integer package element */
605 Elements = NewObject->Package.Elements;
606 Buffer = OriginalObject->Buffer.Pointer;
610 *Elements = AcpiUtCreateIntegerObject ((UINT64) *Buffer);
613 AcpiUtRemoveReference (NewObject);
614 return (AE_NO_MEMORY);
622 return (AE_AML_OPERAND_TYPE);
625 *ReturnObject = NewObject;
630 /*******************************************************************************
632 * FUNCTION: AcpiNsRepairPackageList
634 * PARAMETERS: Data - Pointer to validation data structure
635 * ObjDescPtr - Pointer to the object to repair. The new
636 * package object is returned here,
637 * overwriting the old object.
639 * RETURN: Status, new object in *ObjDescPtr
641 * DESCRIPTION: Repair a common problem with objects that are defined to return
642 * a variable-length Package of Packages. If the variable-length
643 * is one, some BIOS code mistakenly simply declares a single
644 * Package instead of a Package with one sub-Package. This
645 * function attempts to repair this error by wrapping a Package
646 * object around the original Package, creating the correct
647 * Package with one sub-Package.
649 * Names that can be repaired in this manner include:
650 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
652 ******************************************************************************/
655 AcpiNsRepairPackageList (
656 ACPI_PREDEFINED_DATA *Data,
657 ACPI_OPERAND_OBJECT **ObjDescPtr)
659 ACPI_OPERAND_OBJECT *PkgObjDesc;
662 ACPI_FUNCTION_NAME (NsRepairPackageList);
666 * Create the new outer package and populate it. The new package will
667 * have a single element, the lone subpackage.
669 PkgObjDesc = AcpiUtCreatePackageObject (1);
672 return (AE_NO_MEMORY);
675 PkgObjDesc->Package.Elements[0] = *ObjDescPtr;
677 /* Return the new object in the object pointer */
679 *ObjDescPtr = PkgObjDesc;
680 Data->Flags |= ACPI_OBJECT_REPAIRED;
682 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
683 "%s: Repaired incorrectly formed Package\n", Data->Pathname));