1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #define __NSREPAIR_C__
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <contrib/dev/acpica/include/accommon.h>
48 #include <contrib/dev/acpica/include/acnamesp.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/acpredef.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsrepair")
56 /*******************************************************************************
58 * This module attempts to repair or convert objects returned by the
59 * predefined methods to an object type that is expected, as per the ACPI
60 * specification. The need for this code is dictated by the many machines that
61 * return incorrect types for the standard predefined methods. Performing these
62 * conversions here, in one place, eliminates the need for individual ACPI
63 * device drivers to do the same. Note: Most of these conversions are different
64 * than the internal object conversion routines used for implicit object
67 * The following conversions can be performed as necessary:
75 * Buffer -> Package of Integers
76 * Package -> Package of one Package
78 * Additional possible repairs:
80 * Required package elements that are NULL replaced by Integer/String/Buffer
81 * Incorrect standalone package wrapped with required outer package
83 ******************************************************************************/
86 /* Local prototypes */
89 AcpiNsConvertToInteger (
90 ACPI_OPERAND_OBJECT *OriginalObject,
91 ACPI_OPERAND_OBJECT **ReturnObject);
94 AcpiNsConvertToString (
95 ACPI_OPERAND_OBJECT *OriginalObject,
96 ACPI_OPERAND_OBJECT **ReturnObject);
99 AcpiNsConvertToBuffer (
100 ACPI_OPERAND_OBJECT *OriginalObject,
101 ACPI_OPERAND_OBJECT **ReturnObject);
104 AcpiNsConvertToPackage (
105 ACPI_OPERAND_OBJECT *OriginalObject,
106 ACPI_OPERAND_OBJECT **ReturnObject);
109 /*******************************************************************************
111 * FUNCTION: AcpiNsRepairObject
113 * PARAMETERS: Data - Pointer to validation data structure
114 * ExpectedBtypes - Object types expected
115 * PackageIndex - Index of object within parent package (if
116 * applicable - ACPI_NOT_PACKAGE_ELEMENT
118 * ReturnObjectPtr - Pointer to the object returned from the
119 * evaluation of a method or object
121 * RETURN: Status. AE_OK if repair was successful.
123 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
126 ******************************************************************************/
130 ACPI_PREDEFINED_DATA *Data,
131 UINT32 ExpectedBtypes,
133 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
135 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
136 ACPI_OPERAND_OBJECT *NewObject;
140 ACPI_FUNCTION_NAME (NsRepairObject);
144 * At this point, we know that the type of the returned object was not
145 * one of the expected types for this predefined name. Attempt to
146 * repair the object by converting it to one of the expected object
147 * types for this predefined name.
149 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
151 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject);
152 if (ACPI_SUCCESS (Status))
157 if (ExpectedBtypes & ACPI_RTYPE_STRING)
159 Status = AcpiNsConvertToString (ReturnObject, &NewObject);
160 if (ACPI_SUCCESS (Status))
165 if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
167 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject);
168 if (ACPI_SUCCESS (Status))
173 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE)
175 Status = AcpiNsConvertToPackage (ReturnObject, &NewObject);
176 if (ACPI_SUCCESS (Status))
182 /* We cannot repair this object */
184 return (AE_AML_OPERAND_TYPE);
189 /* Object was successfully repaired */
192 * If the original object is a package element, we need to:
193 * 1. Set the reference count of the new object to match the
194 * reference count of the old object.
195 * 2. Decrement the reference count of the original object.
197 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
199 NewObject->Common.ReferenceCount =
200 ReturnObject->Common.ReferenceCount;
202 if (ReturnObject->Common.ReferenceCount > 1)
204 ReturnObject->Common.ReferenceCount--;
207 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
208 "%s: Converted %s to expected %s at index %u\n",
209 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
210 AcpiUtGetObjectTypeName (NewObject), PackageIndex));
214 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
215 "%s: Converted %s to expected %s\n",
216 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
217 AcpiUtGetObjectTypeName (NewObject)));
220 /* Delete old object, install the new return object */
222 AcpiUtRemoveReference (ReturnObject);
223 *ReturnObjectPtr = NewObject;
224 Data->Flags |= ACPI_OBJECT_REPAIRED;
229 /*******************************************************************************
231 * FUNCTION: AcpiNsConvertToInteger
233 * PARAMETERS: OriginalObject - Object to be converted
234 * ReturnObject - Where the new converted object is returned
236 * RETURN: Status. AE_OK if conversion was successful.
238 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
240 ******************************************************************************/
243 AcpiNsConvertToInteger (
244 ACPI_OPERAND_OBJECT *OriginalObject,
245 ACPI_OPERAND_OBJECT **ReturnObject)
247 ACPI_OPERAND_OBJECT *NewObject;
253 switch (OriginalObject->Common.Type)
255 case ACPI_TYPE_STRING:
257 /* String-to-Integer conversion */
259 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
260 ACPI_ANY_BASE, &Value);
261 if (ACPI_FAILURE (Status))
267 case ACPI_TYPE_BUFFER:
269 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
271 if (OriginalObject->Buffer.Length > 8)
273 return (AE_AML_OPERAND_TYPE);
276 /* Extract each buffer byte to create the integer */
278 for (i = 0; i < OriginalObject->Buffer.Length; i++)
280 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
285 return (AE_AML_OPERAND_TYPE);
288 NewObject = AcpiUtCreateIntegerObject (Value);
291 return (AE_NO_MEMORY);
294 *ReturnObject = NewObject;
299 /*******************************************************************************
301 * FUNCTION: AcpiNsConvertToString
303 * PARAMETERS: OriginalObject - Object to be converted
304 * ReturnObject - Where the new converted object is returned
306 * RETURN: Status. AE_OK if conversion was successful.
308 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
310 ******************************************************************************/
313 AcpiNsConvertToString (
314 ACPI_OPERAND_OBJECT *OriginalObject,
315 ACPI_OPERAND_OBJECT **ReturnObject)
317 ACPI_OPERAND_OBJECT *NewObject;
322 switch (OriginalObject->Common.Type)
324 case ACPI_TYPE_INTEGER:
326 * Integer-to-String conversion. Commonly, convert
327 * an integer of value 0 to a NULL string. The last element of
328 * _BIF and _BIX packages occasionally need this fix.
330 if (OriginalObject->Integer.Value == 0)
332 /* Allocate a new NULL string object */
334 NewObject = AcpiUtCreateStringObject (0);
337 return (AE_NO_MEMORY);
342 Status = AcpiExConvertToString (OriginalObject, &NewObject,
343 ACPI_IMPLICIT_CONVERT_HEX);
344 if (ACPI_FAILURE (Status))
351 case ACPI_TYPE_BUFFER:
353 * Buffer-to-String conversion. Use a ToString
354 * conversion, no transform performed on the buffer data. The best
355 * example of this is the _BIF method, where the string data from
356 * the battery is often (incorrectly) returned as buffer object(s).
359 while ((Length < OriginalObject->Buffer.Length) &&
360 (OriginalObject->Buffer.Pointer[Length]))
365 /* Allocate a new string object */
367 NewObject = AcpiUtCreateStringObject (Length);
370 return (AE_NO_MEMORY);
374 * Copy the raw buffer data with no transform. String is already NULL
375 * terminated at Length+1.
377 ACPI_MEMCPY (NewObject->String.Pointer,
378 OriginalObject->Buffer.Pointer, Length);
382 return (AE_AML_OPERAND_TYPE);
385 *ReturnObject = NewObject;
390 /*******************************************************************************
392 * FUNCTION: AcpiNsConvertToBuffer
394 * PARAMETERS: OriginalObject - Object to be converted
395 * ReturnObject - Where the new converted object is returned
397 * RETURN: Status. AE_OK if conversion was successful.
399 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
401 ******************************************************************************/
404 AcpiNsConvertToBuffer (
405 ACPI_OPERAND_OBJECT *OriginalObject,
406 ACPI_OPERAND_OBJECT **ReturnObject)
408 ACPI_OPERAND_OBJECT *NewObject;
410 ACPI_OPERAND_OBJECT **Elements;
416 switch (OriginalObject->Common.Type)
418 case ACPI_TYPE_INTEGER:
420 * Integer-to-Buffer conversion.
421 * Convert the Integer to a packed-byte buffer. _MAT and other
422 * objects need this sometimes, if a read has been performed on a
423 * Field object that is less than or equal to the global integer
424 * size (32 or 64 bits).
426 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
427 if (ACPI_FAILURE (Status))
433 case ACPI_TYPE_STRING:
435 /* String-to-Buffer conversion. Simple data copy */
437 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
440 return (AE_NO_MEMORY);
443 ACPI_MEMCPY (NewObject->Buffer.Pointer,
444 OriginalObject->String.Pointer, OriginalObject->String.Length);
447 case ACPI_TYPE_PACKAGE:
449 * This case is often seen for predefined names that must return a
450 * Buffer object with multiple DWORD integers within. For example,
451 * _FDE and _GTM. The Package can be converted to a Buffer.
454 /* All elements of the Package must be integers */
456 Elements = OriginalObject->Package.Elements;
457 Count = OriginalObject->Package.Count;
459 for (i = 0; i < Count; i++)
462 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
464 return (AE_AML_OPERAND_TYPE);
469 /* Create the new buffer object to replace the Package */
471 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
474 return (AE_NO_MEMORY);
477 /* Copy the package elements (integers) to the buffer as DWORDs */
479 Elements = OriginalObject->Package.Elements;
480 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
482 for (i = 0; i < Count; i++)
484 *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
491 return (AE_AML_OPERAND_TYPE);
494 *ReturnObject = NewObject;
499 /*******************************************************************************
501 * FUNCTION: AcpiNsConvertToPackage
503 * PARAMETERS: OriginalObject - Object to be converted
504 * ReturnObject - Where the new converted object is returned
506 * RETURN: Status. AE_OK if conversion was successful.
508 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
509 * the buffer is converted to a single integer package element.
511 ******************************************************************************/
514 AcpiNsConvertToPackage (
515 ACPI_OPERAND_OBJECT *OriginalObject,
516 ACPI_OPERAND_OBJECT **ReturnObject)
518 ACPI_OPERAND_OBJECT *NewObject;
519 ACPI_OPERAND_OBJECT **Elements;
524 switch (OriginalObject->Common.Type)
526 case ACPI_TYPE_BUFFER:
528 /* Buffer-to-Package conversion */
530 Length = OriginalObject->Buffer.Length;
531 NewObject = AcpiUtCreatePackageObject (Length);
534 return (AE_NO_MEMORY);
537 /* Convert each buffer byte to an integer package element */
539 Elements = NewObject->Package.Elements;
540 Buffer = OriginalObject->Buffer.Pointer;
544 *Elements = AcpiUtCreateIntegerObject ((UINT64) *Buffer);
547 AcpiUtRemoveReference (NewObject);
548 return (AE_NO_MEMORY);
556 return (AE_AML_OPERAND_TYPE);
559 *ReturnObject = NewObject;
564 /*******************************************************************************
566 * FUNCTION: AcpiNsRepairNullElement
568 * PARAMETERS: Data - Pointer to validation data structure
569 * ExpectedBtypes - Object types expected
570 * PackageIndex - Index of object within parent package (if
571 * applicable - ACPI_NOT_PACKAGE_ELEMENT
573 * ReturnObjectPtr - Pointer to the object returned from the
574 * evaluation of a method or object
576 * RETURN: Status. AE_OK if repair was successful.
578 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
580 ******************************************************************************/
583 AcpiNsRepairNullElement (
584 ACPI_PREDEFINED_DATA *Data,
585 UINT32 ExpectedBtypes,
587 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
589 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
590 ACPI_OPERAND_OBJECT *NewObject;
593 ACPI_FUNCTION_NAME (NsRepairNullElement);
596 /* No repair needed if return object is non-NULL */
604 * Attempt to repair a NULL element of a Package object. This applies to
605 * predefined names that return a fixed-length package and each element
606 * is required. It does not apply to variable-length packages where NULL
607 * elements are allowed, especially at the end of the package.
609 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
611 /* Need an Integer - create a zero-value integer */
613 NewObject = AcpiUtCreateIntegerObject ((UINT64) 0);
615 else if (ExpectedBtypes & ACPI_RTYPE_STRING)
617 /* Need a String - create a NULL string */
619 NewObject = AcpiUtCreateStringObject (0);
621 else if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
623 /* Need a Buffer - create a zero-length buffer */
625 NewObject = AcpiUtCreateBufferObject (0);
629 /* Error for all other expected types */
631 return (AE_AML_OPERAND_TYPE);
636 return (AE_NO_MEMORY);
639 /* Set the reference count according to the parent Package object */
641 NewObject->Common.ReferenceCount = Data->ParentPackage->Common.ReferenceCount;
643 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
644 "%s: Converted NULL package element to expected %s at index %u\n",
645 Data->Pathname, AcpiUtGetObjectTypeName (NewObject), PackageIndex));
647 *ReturnObjectPtr = NewObject;
648 Data->Flags |= ACPI_OBJECT_REPAIRED;
653 /******************************************************************************
655 * FUNCTION: AcpiNsRemoveNullElements
657 * PARAMETERS: Data - Pointer to validation data structure
658 * PackageType - An AcpiReturnPackageTypes value
659 * ObjDesc - A Package object
663 * DESCRIPTION: Remove all NULL package elements from packages that contain
664 * a variable number of sub-packages. For these types of
665 * packages, NULL elements can be safely removed.
667 *****************************************************************************/
670 AcpiNsRemoveNullElements (
671 ACPI_PREDEFINED_DATA *Data,
673 ACPI_OPERAND_OBJECT *ObjDesc)
675 ACPI_OPERAND_OBJECT **Source;
676 ACPI_OPERAND_OBJECT **Dest;
682 ACPI_FUNCTION_NAME (NsRemoveNullElements);
686 * We can safely remove all NULL elements from these package types:
687 * PTYPE1_VAR packages contain a variable number of simple data types.
688 * PTYPE2 packages contain a variable number of sub-packages.
692 case ACPI_PTYPE1_VAR:
694 case ACPI_PTYPE2_COUNT:
695 case ACPI_PTYPE2_PKG_COUNT:
696 case ACPI_PTYPE2_FIXED:
697 case ACPI_PTYPE2_MIN:
698 case ACPI_PTYPE2_REV_FIXED:
702 case ACPI_PTYPE1_FIXED:
703 case ACPI_PTYPE1_OPTION:
707 Count = ObjDesc->Package.Count;
710 Source = ObjDesc->Package.Elements;
713 /* Examine all elements of the package object, remove nulls */
715 for (i = 0; i < Count; i++)
729 /* Update parent package if any null elements were removed */
731 if (NewCount < Count)
733 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
734 "%s: Found and removed %u NULL elements\n",
735 Data->Pathname, (Count - NewCount)));
737 /* NULL terminate list and update the package count */
740 ObjDesc->Package.Count = NewCount;
745 /*******************************************************************************
747 * FUNCTION: AcpiNsRepairPackageList
749 * PARAMETERS: Data - Pointer to validation data structure
750 * ObjDescPtr - Pointer to the object to repair. The new
751 * package object is returned here,
752 * overwriting the old object.
754 * RETURN: Status, new object in *ObjDescPtr
756 * DESCRIPTION: Repair a common problem with objects that are defined to return
757 * a variable-length Package of Packages. If the variable-length
758 * is one, some BIOS code mistakenly simply declares a single
759 * Package instead of a Package with one sub-Package. This
760 * function attempts to repair this error by wrapping a Package
761 * object around the original Package, creating the correct
762 * Package with one sub-Package.
764 * Names that can be repaired in this manner include:
765 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
767 ******************************************************************************/
770 AcpiNsRepairPackageList (
771 ACPI_PREDEFINED_DATA *Data,
772 ACPI_OPERAND_OBJECT **ObjDescPtr)
774 ACPI_OPERAND_OBJECT *PkgObjDesc;
777 ACPI_FUNCTION_NAME (NsRepairPackageList);
781 * Create the new outer package and populate it. The new package will
782 * have a single element, the lone subpackage.
784 PkgObjDesc = AcpiUtCreatePackageObject (1);
787 return (AE_NO_MEMORY);
790 PkgObjDesc->Package.Elements[0] = *ObjDescPtr;
792 /* Return the new object in the object pointer */
794 *ObjDescPtr = PkgObjDesc;
795 Data->Flags |= ACPI_OBJECT_REPAIRED;
797 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
798 "%s: Repaired incorrectly formed Package\n", Data->Pathname));