1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2010, 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"
122 #include "acpredef.h"
124 #define _COMPONENT ACPI_NAMESPACE
125 ACPI_MODULE_NAME ("nsrepair")
128 /*******************************************************************************
130 * This module attempts to repair or convert objects returned by the
131 * predefined methods to an object type that is expected, as per the ACPI
132 * specification. The need for this code is dictated by the many machines that
133 * return incorrect types for the standard predefined methods. Performing these
134 * conversions here, in one place, eliminates the need for individual ACPI
135 * device drivers to do the same. Note: Most of these conversions are different
136 * than the internal object conversion routines used for implicit object
139 * The following conversions can be performed as necessary:
147 * Buffer -> Package of Integers
148 * Package -> Package of one Package
150 * Additional possible repairs:
152 * Optional/unnecessary NULL package elements removed
153 * Required package elements that are NULL replaced by Integer/String/Buffer
154 * Incorrect standalone package wrapped with required outer package
156 ******************************************************************************/
159 /* Local prototypes */
162 AcpiNsConvertToInteger (
163 ACPI_OPERAND_OBJECT *OriginalObject,
164 ACPI_OPERAND_OBJECT **ReturnObject);
167 AcpiNsConvertToString (
168 ACPI_OPERAND_OBJECT *OriginalObject,
169 ACPI_OPERAND_OBJECT **ReturnObject);
172 AcpiNsConvertToBuffer (
173 ACPI_OPERAND_OBJECT *OriginalObject,
174 ACPI_OPERAND_OBJECT **ReturnObject);
177 AcpiNsConvertToPackage (
178 ACPI_OPERAND_OBJECT *OriginalObject,
179 ACPI_OPERAND_OBJECT **ReturnObject);
182 /*******************************************************************************
184 * FUNCTION: AcpiNsRepairObject
186 * PARAMETERS: Data - Pointer to validation data structure
187 * ExpectedBtypes - Object types expected
188 * PackageIndex - Index of object within parent package (if
189 * applicable - ACPI_NOT_PACKAGE_ELEMENT
191 * ReturnObjectPtr - Pointer to the object returned from the
192 * evaluation of a method or object
194 * RETURN: Status. AE_OK if repair was successful.
196 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
199 ******************************************************************************/
203 ACPI_PREDEFINED_DATA *Data,
204 UINT32 ExpectedBtypes,
206 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
208 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
209 ACPI_OPERAND_OBJECT *NewObject;
213 ACPI_FUNCTION_NAME (NsRepairObject);
217 * At this point, we know that the type of the returned object was not
218 * one of the expected types for this predefined name. Attempt to
219 * repair the object by converting it to one of the expected object
220 * types for this predefined name.
222 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
224 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject);
225 if (ACPI_SUCCESS (Status))
230 if (ExpectedBtypes & ACPI_RTYPE_STRING)
232 Status = AcpiNsConvertToString (ReturnObject, &NewObject);
233 if (ACPI_SUCCESS (Status))
238 if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
240 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject);
241 if (ACPI_SUCCESS (Status))
246 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE)
248 Status = AcpiNsConvertToPackage (ReturnObject, &NewObject);
249 if (ACPI_SUCCESS (Status))
255 /* We cannot repair this object */
257 return (AE_AML_OPERAND_TYPE);
262 /* Object was successfully repaired */
265 * If the original object is a package element, we need to:
266 * 1. Set the reference count of the new object to match the
267 * reference count of the old object.
268 * 2. Decrement the reference count of the original object.
270 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
272 NewObject->Common.ReferenceCount =
273 ReturnObject->Common.ReferenceCount;
275 if (ReturnObject->Common.ReferenceCount > 1)
277 ReturnObject->Common.ReferenceCount--;
280 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
281 "%s: Converted %s to expected %s at index %u\n",
282 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
283 AcpiUtGetObjectTypeName (NewObject), PackageIndex));
287 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
288 "%s: Converted %s to expected %s\n",
289 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
290 AcpiUtGetObjectTypeName (NewObject)));
293 /* Delete old object, install the new return object */
295 AcpiUtRemoveReference (ReturnObject);
296 *ReturnObjectPtr = NewObject;
297 Data->Flags |= ACPI_OBJECT_REPAIRED;
302 /*******************************************************************************
304 * FUNCTION: AcpiNsConvertToInteger
306 * PARAMETERS: OriginalObject - Object to be converted
307 * ReturnObject - Where the new converted object is returned
309 * RETURN: Status. AE_OK if conversion was successful.
311 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
313 ******************************************************************************/
316 AcpiNsConvertToInteger (
317 ACPI_OPERAND_OBJECT *OriginalObject,
318 ACPI_OPERAND_OBJECT **ReturnObject)
320 ACPI_OPERAND_OBJECT *NewObject;
326 switch (OriginalObject->Common.Type)
328 case ACPI_TYPE_STRING:
330 /* String-to-Integer conversion */
332 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
333 ACPI_ANY_BASE, &Value);
334 if (ACPI_FAILURE (Status))
340 case ACPI_TYPE_BUFFER:
342 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
344 if (OriginalObject->Buffer.Length > 8)
346 return (AE_AML_OPERAND_TYPE);
349 /* Extract each buffer byte to create the integer */
351 for (i = 0; i < OriginalObject->Buffer.Length; i++)
353 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
358 return (AE_AML_OPERAND_TYPE);
361 NewObject = AcpiUtCreateIntegerObject (Value);
364 return (AE_NO_MEMORY);
367 *ReturnObject = NewObject;
372 /*******************************************************************************
374 * FUNCTION: AcpiNsConvertToString
376 * PARAMETERS: OriginalObject - Object to be converted
377 * ReturnObject - Where the new converted object is returned
379 * RETURN: Status. AE_OK if conversion was successful.
381 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
383 ******************************************************************************/
386 AcpiNsConvertToString (
387 ACPI_OPERAND_OBJECT *OriginalObject,
388 ACPI_OPERAND_OBJECT **ReturnObject)
390 ACPI_OPERAND_OBJECT *NewObject;
395 switch (OriginalObject->Common.Type)
397 case ACPI_TYPE_INTEGER:
399 * Integer-to-String conversion. Commonly, convert
400 * an integer of value 0 to a NULL string. The last element of
401 * _BIF and _BIX packages occasionally need this fix.
403 if (OriginalObject->Integer.Value == 0)
405 /* Allocate a new NULL string object */
407 NewObject = AcpiUtCreateStringObject (0);
410 return (AE_NO_MEMORY);
415 Status = AcpiExConvertToString (OriginalObject, &NewObject,
416 ACPI_IMPLICIT_CONVERT_HEX);
417 if (ACPI_FAILURE (Status))
424 case ACPI_TYPE_BUFFER:
426 * Buffer-to-String conversion. Use a ToString
427 * conversion, no transform performed on the buffer data. The best
428 * example of this is the _BIF method, where the string data from
429 * the battery is often (incorrectly) returned as buffer object(s).
432 while ((Length < OriginalObject->Buffer.Length) &&
433 (OriginalObject->Buffer.Pointer[Length]))
438 /* Allocate a new string object */
440 NewObject = AcpiUtCreateStringObject (Length);
443 return (AE_NO_MEMORY);
447 * Copy the raw buffer data with no transform. String is already NULL
448 * terminated at Length+1.
450 ACPI_MEMCPY (NewObject->String.Pointer,
451 OriginalObject->Buffer.Pointer, Length);
455 return (AE_AML_OPERAND_TYPE);
458 *ReturnObject = NewObject;
463 /*******************************************************************************
465 * FUNCTION: AcpiNsConvertToBuffer
467 * PARAMETERS: OriginalObject - Object to be converted
468 * ReturnObject - Where the new converted object is returned
470 * RETURN: Status. AE_OK if conversion was successful.
472 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
474 ******************************************************************************/
477 AcpiNsConvertToBuffer (
478 ACPI_OPERAND_OBJECT *OriginalObject,
479 ACPI_OPERAND_OBJECT **ReturnObject)
481 ACPI_OPERAND_OBJECT *NewObject;
483 ACPI_OPERAND_OBJECT **Elements;
489 switch (OriginalObject->Common.Type)
491 case ACPI_TYPE_INTEGER:
493 * Integer-to-Buffer conversion.
494 * Convert the Integer to a packed-byte buffer. _MAT and other
495 * objects need this sometimes, if a read has been performed on a
496 * Field object that is less than or equal to the global integer
497 * size (32 or 64 bits).
499 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
500 if (ACPI_FAILURE (Status))
506 case ACPI_TYPE_STRING:
508 /* String-to-Buffer conversion. Simple data copy */
510 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
513 return (AE_NO_MEMORY);
516 ACPI_MEMCPY (NewObject->Buffer.Pointer,
517 OriginalObject->String.Pointer, OriginalObject->String.Length);
520 case ACPI_TYPE_PACKAGE:
522 * This case is often seen for predefined names that must return a
523 * Buffer object with multiple DWORD integers within. For example,
524 * _FDE and _GTM. The Package can be converted to a Buffer.
527 /* All elements of the Package must be integers */
529 Elements = OriginalObject->Package.Elements;
530 Count = OriginalObject->Package.Count;
532 for (i = 0; i < Count; i++)
535 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
537 return (AE_AML_OPERAND_TYPE);
542 /* Create the new buffer object to replace the Package */
544 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
547 return (AE_NO_MEMORY);
550 /* Copy the package elements (integers) to the buffer as DWORDs */
552 Elements = OriginalObject->Package.Elements;
553 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
555 for (i = 0; i < Count; i++)
557 *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
564 return (AE_AML_OPERAND_TYPE);
567 *ReturnObject = NewObject;
572 /*******************************************************************************
574 * FUNCTION: AcpiNsConvertToPackage
576 * PARAMETERS: OriginalObject - Object to be converted
577 * ReturnObject - Where the new converted object is returned
579 * RETURN: Status. AE_OK if conversion was successful.
581 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
582 * the buffer is converted to a single integer package element.
584 ******************************************************************************/
587 AcpiNsConvertToPackage (
588 ACPI_OPERAND_OBJECT *OriginalObject,
589 ACPI_OPERAND_OBJECT **ReturnObject)
591 ACPI_OPERAND_OBJECT *NewObject;
592 ACPI_OPERAND_OBJECT **Elements;
597 switch (OriginalObject->Common.Type)
599 case ACPI_TYPE_BUFFER:
601 /* Buffer-to-Package conversion */
603 Length = OriginalObject->Buffer.Length;
604 NewObject = AcpiUtCreatePackageObject (Length);
607 return (AE_NO_MEMORY);
610 /* Convert each buffer byte to an integer package element */
612 Elements = NewObject->Package.Elements;
613 Buffer = OriginalObject->Buffer.Pointer;
617 *Elements = AcpiUtCreateIntegerObject ((UINT64) *Buffer);
620 AcpiUtRemoveReference (NewObject);
621 return (AE_NO_MEMORY);
629 return (AE_AML_OPERAND_TYPE);
632 *ReturnObject = NewObject;
637 /*******************************************************************************
639 * FUNCTION: AcpiNsRepairNullElement
641 * PARAMETERS: Data - Pointer to validation data structure
642 * ExpectedBtypes - Object types expected
643 * PackageIndex - Index of object within parent package (if
644 * applicable - ACPI_NOT_PACKAGE_ELEMENT
646 * ReturnObjectPtr - Pointer to the object returned from the
647 * evaluation of a method or object
649 * RETURN: Status. AE_OK if repair was successful.
651 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
653 ******************************************************************************/
656 AcpiNsRepairNullElement (
657 ACPI_PREDEFINED_DATA *Data,
658 UINT32 ExpectedBtypes,
660 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
662 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
663 ACPI_OPERAND_OBJECT *NewObject;
666 ACPI_FUNCTION_NAME (NsRepairNullElement);
669 /* No repair needed if return object is non-NULL */
677 * Attempt to repair a NULL element of a Package object. This applies to
678 * predefined names that return a fixed-length package and each element
679 * is required. It does not apply to variable-length packages where NULL
680 * elements are allowed, especially at the end of the package.
682 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
684 /* Need an Integer - create a zero-value integer */
686 NewObject = AcpiUtCreateIntegerObject ((UINT64) 0);
688 else if (ExpectedBtypes & ACPI_RTYPE_STRING)
690 /* Need a String - create a NULL string */
692 NewObject = AcpiUtCreateStringObject (0);
694 else if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
696 /* Need a Buffer - create a zero-length buffer */
698 NewObject = AcpiUtCreateBufferObject (0);
702 /* Error for all other expected types */
704 return (AE_AML_OPERAND_TYPE);
709 return (AE_NO_MEMORY);
712 /* Set the reference count according to the parent Package object */
714 NewObject->Common.ReferenceCount = Data->ParentPackage->Common.ReferenceCount;
716 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
717 "%s: Converted NULL package element to expected %s at index %u\n",
718 Data->Pathname, AcpiUtGetObjectTypeName (NewObject), PackageIndex));
720 *ReturnObjectPtr = NewObject;
721 Data->Flags |= ACPI_OBJECT_REPAIRED;
726 /******************************************************************************
728 * FUNCTION: AcpiNsRemoveNullElements
730 * PARAMETERS: Data - Pointer to validation data structure
731 * PackageType - An AcpiReturnPackageTypes value
732 * ObjDesc - A Package object
736 * DESCRIPTION: Remove all NULL package elements from packages that contain
737 * a variable number of sub-packages. For these types of
738 * packages, NULL elements can be safely removed.
740 *****************************************************************************/
743 AcpiNsRemoveNullElements (
744 ACPI_PREDEFINED_DATA *Data,
746 ACPI_OPERAND_OBJECT *ObjDesc)
748 ACPI_OPERAND_OBJECT **Source;
749 ACPI_OPERAND_OBJECT **Dest;
755 ACPI_FUNCTION_NAME (NsRemoveNullElements);
759 * PTYPE1 packages contain no subpackages.
760 * PTYPE2 packages contain a variable number of sub-packages. We can
761 * safely remove all NULL elements from the PTYPE2 packages.
765 case ACPI_PTYPE1_FIXED:
766 case ACPI_PTYPE1_VAR:
767 case ACPI_PTYPE1_OPTION:
771 case ACPI_PTYPE2_COUNT:
772 case ACPI_PTYPE2_PKG_COUNT:
773 case ACPI_PTYPE2_FIXED:
774 case ACPI_PTYPE2_MIN:
775 case ACPI_PTYPE2_REV_FIXED:
782 Count = ObjDesc->Package.Count;
785 Source = ObjDesc->Package.Elements;
788 /* Examine all elements of the package object, remove nulls */
790 for (i = 0; i < Count; i++)
804 /* Update parent package if any null elements were removed */
806 if (NewCount < Count)
808 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
809 "%s: Found and removed %u NULL elements\n",
810 Data->Pathname, (Count - NewCount)));
812 /* NULL terminate list and update the package count */
815 ObjDesc->Package.Count = NewCount;
820 /*******************************************************************************
822 * FUNCTION: AcpiNsRepairPackageList
824 * PARAMETERS: Data - Pointer to validation data structure
825 * ObjDescPtr - Pointer to the object to repair. The new
826 * package object is returned here,
827 * overwriting the old object.
829 * RETURN: Status, new object in *ObjDescPtr
831 * DESCRIPTION: Repair a common problem with objects that are defined to return
832 * a variable-length Package of Packages. If the variable-length
833 * is one, some BIOS code mistakenly simply declares a single
834 * Package instead of a Package with one sub-Package. This
835 * function attempts to repair this error by wrapping a Package
836 * object around the original Package, creating the correct
837 * Package with one sub-Package.
839 * Names that can be repaired in this manner include:
840 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
842 ******************************************************************************/
845 AcpiNsRepairPackageList (
846 ACPI_PREDEFINED_DATA *Data,
847 ACPI_OPERAND_OBJECT **ObjDescPtr)
849 ACPI_OPERAND_OBJECT *PkgObjDesc;
852 ACPI_FUNCTION_NAME (NsRepairPackageList);
856 * Create the new outer package and populate it. The new package will
857 * have a single element, the lone subpackage.
859 PkgObjDesc = AcpiUtCreatePackageObject (1);
862 return (AE_NO_MEMORY);
865 PkgObjDesc->Package.Elements[0] = *ObjDescPtr;
867 /* Return the new object in the object pointer */
869 *ObjDescPtr = PkgObjDesc;
870 Data->Flags |= ACPI_OBJECT_REPAIRED;
872 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
873 "%s: Repaired incorrectly formed Package\n", Data->Pathname));