1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, 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 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME("nsrepair")
53 /*******************************************************************************
55 * This module attempts to repair or convert objects returned by the
56 * predefined methods to an object type that is expected, as per the ACPI
57 * specification. The need for this code is dictated by the many machines that
58 * return incorrect types for the standard predefined methods. Performing these
59 * conversions here, in one place, eliminates the need for individual ACPI
60 * device drivers to do the same. Note: Most of these conversions are different
61 * than the internal object conversion routines used for implicit object
64 * The following conversions can be performed as necessary:
72 * Buffer -> Package of Integers
73 * Package -> Package of one Package
75 * Additional possible repairs:
77 * Required package elements that are NULL replaced by Integer/String/Buffer
78 * Incorrect standalone package wrapped with required outer package
80 ******************************************************************************/
81 /* Local prototypes */
83 acpi_ns_convert_to_integer(union acpi_operand_object
*original_object
,
84 union acpi_operand_object
**return_object
);
87 acpi_ns_convert_to_string(union acpi_operand_object
*original_object
,
88 union acpi_operand_object
**return_object
);
91 acpi_ns_convert_to_buffer(union acpi_operand_object
*original_object
,
92 union acpi_operand_object
**return_object
);
95 acpi_ns_convert_to_package(union acpi_operand_object
*original_object
,
96 union acpi_operand_object
**return_object
);
98 /*******************************************************************************
100 * FUNCTION: acpi_ns_repair_object
102 * PARAMETERS: Data - Pointer to validation data structure
103 * expected_btypes - Object types expected
104 * package_index - Index of object within parent package (if
105 * applicable - ACPI_NOT_PACKAGE_ELEMENT
107 * return_object_ptr - Pointer to the object returned from the
108 * evaluation of a method or object
110 * RETURN: Status. AE_OK if repair was successful.
112 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
115 ******************************************************************************/
118 acpi_ns_repair_object(struct acpi_predefined_data
*data
,
121 union acpi_operand_object
**return_object_ptr
)
123 union acpi_operand_object
*return_object
= *return_object_ptr
;
124 union acpi_operand_object
*new_object
;
127 ACPI_FUNCTION_NAME(ns_repair_object
);
130 * At this point, we know that the type of the returned object was not
131 * one of the expected types for this predefined name. Attempt to
132 * repair the object by converting it to one of the expected object
133 * types for this predefined name.
135 if (expected_btypes
& ACPI_RTYPE_INTEGER
) {
136 status
= acpi_ns_convert_to_integer(return_object
, &new_object
);
137 if (ACPI_SUCCESS(status
)) {
138 goto object_repaired
;
141 if (expected_btypes
& ACPI_RTYPE_STRING
) {
142 status
= acpi_ns_convert_to_string(return_object
, &new_object
);
143 if (ACPI_SUCCESS(status
)) {
144 goto object_repaired
;
147 if (expected_btypes
& ACPI_RTYPE_BUFFER
) {
148 status
= acpi_ns_convert_to_buffer(return_object
, &new_object
);
149 if (ACPI_SUCCESS(status
)) {
150 goto object_repaired
;
153 if (expected_btypes
& ACPI_RTYPE_PACKAGE
) {
154 status
= acpi_ns_convert_to_package(return_object
, &new_object
);
155 if (ACPI_SUCCESS(status
)) {
156 goto object_repaired
;
160 /* We cannot repair this object */
162 return (AE_AML_OPERAND_TYPE
);
166 /* Object was successfully repaired */
169 * If the original object is a package element, we need to:
170 * 1. Set the reference count of the new object to match the
171 * reference count of the old object.
172 * 2. Decrement the reference count of the original object.
174 if (package_index
!= ACPI_NOT_PACKAGE_ELEMENT
) {
175 new_object
->common
.reference_count
=
176 return_object
->common
.reference_count
;
178 if (return_object
->common
.reference_count
> 1) {
179 return_object
->common
.reference_count
--;
182 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
183 "%s: Converted %s to expected %s at index %u\n",
185 acpi_ut_get_object_type_name(return_object
),
186 acpi_ut_get_object_type_name(new_object
),
189 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
190 "%s: Converted %s to expected %s\n",
192 acpi_ut_get_object_type_name(return_object
),
193 acpi_ut_get_object_type_name(new_object
)));
196 /* Delete old object, install the new return object */
198 acpi_ut_remove_reference(return_object
);
199 *return_object_ptr
= new_object
;
200 data
->flags
|= ACPI_OBJECT_REPAIRED
;
204 /*******************************************************************************
206 * FUNCTION: acpi_ns_convert_to_integer
208 * PARAMETERS: original_object - Object to be converted
209 * return_object - Where the new converted object is returned
211 * RETURN: Status. AE_OK if conversion was successful.
213 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
215 ******************************************************************************/
218 acpi_ns_convert_to_integer(union acpi_operand_object
*original_object
,
219 union acpi_operand_object
**return_object
)
221 union acpi_operand_object
*new_object
;
226 switch (original_object
->common
.type
) {
227 case ACPI_TYPE_STRING
:
229 /* String-to-Integer conversion */
231 status
= acpi_ut_strtoul64(original_object
->string
.pointer
,
232 ACPI_ANY_BASE
, &value
);
233 if (ACPI_FAILURE(status
)) {
238 case ACPI_TYPE_BUFFER
:
240 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
242 if (original_object
->buffer
.length
> 8) {
243 return (AE_AML_OPERAND_TYPE
);
246 /* Extract each buffer byte to create the integer */
248 for (i
= 0; i
< original_object
->buffer
.length
; i
++) {
250 ((u64
) original_object
->buffer
.
251 pointer
[i
] << (i
* 8));
256 return (AE_AML_OPERAND_TYPE
);
259 new_object
= acpi_ut_create_integer_object(value
);
261 return (AE_NO_MEMORY
);
264 *return_object
= new_object
;
268 /*******************************************************************************
270 * FUNCTION: acpi_ns_convert_to_string
272 * PARAMETERS: original_object - Object to be converted
273 * return_object - Where the new converted object is returned
275 * RETURN: Status. AE_OK if conversion was successful.
277 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
279 ******************************************************************************/
282 acpi_ns_convert_to_string(union acpi_operand_object
*original_object
,
283 union acpi_operand_object
**return_object
)
285 union acpi_operand_object
*new_object
;
289 switch (original_object
->common
.type
) {
290 case ACPI_TYPE_INTEGER
:
292 * Integer-to-String conversion. Commonly, convert
293 * an integer of value 0 to a NULL string. The last element of
294 * _BIF and _BIX packages occasionally need this fix.
296 if (original_object
->integer
.value
== 0) {
298 /* Allocate a new NULL string object */
300 new_object
= acpi_ut_create_string_object(0);
302 return (AE_NO_MEMORY
);
306 acpi_ex_convert_to_string(original_object
,
308 ACPI_IMPLICIT_CONVERT_HEX
);
309 if (ACPI_FAILURE(status
)) {
315 case ACPI_TYPE_BUFFER
:
317 * Buffer-to-String conversion. Use a to_string
318 * conversion, no transform performed on the buffer data. The best
319 * example of this is the _BIF method, where the string data from
320 * the battery is often (incorrectly) returned as buffer object(s).
323 while ((length
< original_object
->buffer
.length
) &&
324 (original_object
->buffer
.pointer
[length
])) {
328 /* Allocate a new string object */
330 new_object
= acpi_ut_create_string_object(length
);
332 return (AE_NO_MEMORY
);
336 * Copy the raw buffer data with no transform. String is already NULL
337 * terminated at Length+1.
339 ACPI_MEMCPY(new_object
->string
.pointer
,
340 original_object
->buffer
.pointer
, length
);
344 return (AE_AML_OPERAND_TYPE
);
347 *return_object
= new_object
;
351 /*******************************************************************************
353 * FUNCTION: acpi_ns_convert_to_buffer
355 * PARAMETERS: original_object - Object to be converted
356 * return_object - Where the new converted object is returned
358 * RETURN: Status. AE_OK if conversion was successful.
360 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
362 ******************************************************************************/
365 acpi_ns_convert_to_buffer(union acpi_operand_object
*original_object
,
366 union acpi_operand_object
**return_object
)
368 union acpi_operand_object
*new_object
;
370 union acpi_operand_object
**elements
;
375 switch (original_object
->common
.type
) {
376 case ACPI_TYPE_INTEGER
:
378 * Integer-to-Buffer conversion.
379 * Convert the Integer to a packed-byte buffer. _MAT and other
380 * objects need this sometimes, if a read has been performed on a
381 * Field object that is less than or equal to the global integer
382 * size (32 or 64 bits).
385 acpi_ex_convert_to_buffer(original_object
, &new_object
);
386 if (ACPI_FAILURE(status
)) {
391 case ACPI_TYPE_STRING
:
393 /* String-to-Buffer conversion. Simple data copy */
396 acpi_ut_create_buffer_object(original_object
->string
.
399 return (AE_NO_MEMORY
);
402 ACPI_MEMCPY(new_object
->buffer
.pointer
,
403 original_object
->string
.pointer
,
404 original_object
->string
.length
);
407 case ACPI_TYPE_PACKAGE
:
409 * This case is often seen for predefined names that must return a
410 * Buffer object with multiple DWORD integers within. For example,
411 * _FDE and _GTM. The Package can be converted to a Buffer.
414 /* All elements of the Package must be integers */
416 elements
= original_object
->package
.elements
;
417 count
= original_object
->package
.count
;
419 for (i
= 0; i
< count
; i
++) {
421 ((*elements
)->common
.type
!= ACPI_TYPE_INTEGER
)) {
422 return (AE_AML_OPERAND_TYPE
);
427 /* Create the new buffer object to replace the Package */
429 new_object
= acpi_ut_create_buffer_object(ACPI_MUL_4(count
));
431 return (AE_NO_MEMORY
);
434 /* Copy the package elements (integers) to the buffer as DWORDs */
436 elements
= original_object
->package
.elements
;
437 dword_buffer
= ACPI_CAST_PTR(u32
, new_object
->buffer
.pointer
);
439 for (i
= 0; i
< count
; i
++) {
440 *dword_buffer
= (u32
) (*elements
)->integer
.value
;
447 return (AE_AML_OPERAND_TYPE
);
450 *return_object
= new_object
;
454 /*******************************************************************************
456 * FUNCTION: acpi_ns_convert_to_package
458 * PARAMETERS: original_object - Object to be converted
459 * return_object - Where the new converted object is returned
461 * RETURN: Status. AE_OK if conversion was successful.
463 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
464 * the buffer is converted to a single integer package element.
466 ******************************************************************************/
469 acpi_ns_convert_to_package(union acpi_operand_object
*original_object
,
470 union acpi_operand_object
**return_object
)
472 union acpi_operand_object
*new_object
;
473 union acpi_operand_object
**elements
;
477 switch (original_object
->common
.type
) {
478 case ACPI_TYPE_BUFFER
:
480 /* Buffer-to-Package conversion */
482 length
= original_object
->buffer
.length
;
483 new_object
= acpi_ut_create_package_object(length
);
485 return (AE_NO_MEMORY
);
488 /* Convert each buffer byte to an integer package element */
490 elements
= new_object
->package
.elements
;
491 buffer
= original_object
->buffer
.pointer
;
495 acpi_ut_create_integer_object((u64
) *buffer
);
497 acpi_ut_remove_reference(new_object
);
498 return (AE_NO_MEMORY
);
506 return (AE_AML_OPERAND_TYPE
);
509 *return_object
= new_object
;
513 /*******************************************************************************
515 * FUNCTION: acpi_ns_repair_null_element
517 * PARAMETERS: Data - Pointer to validation data structure
518 * expected_btypes - Object types expected
519 * package_index - Index of object within parent package (if
520 * applicable - ACPI_NOT_PACKAGE_ELEMENT
522 * return_object_ptr - Pointer to the object returned from the
523 * evaluation of a method or object
525 * RETURN: Status. AE_OK if repair was successful.
527 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
529 ******************************************************************************/
532 acpi_ns_repair_null_element(struct acpi_predefined_data
*data
,
535 union acpi_operand_object
**return_object_ptr
)
537 union acpi_operand_object
*return_object
= *return_object_ptr
;
538 union acpi_operand_object
*new_object
;
540 ACPI_FUNCTION_NAME(ns_repair_null_element
);
542 /* No repair needed if return object is non-NULL */
549 * Attempt to repair a NULL element of a Package object. This applies to
550 * predefined names that return a fixed-length package and each element
551 * is required. It does not apply to variable-length packages where NULL
552 * elements are allowed, especially at the end of the package.
554 if (expected_btypes
& ACPI_RTYPE_INTEGER
) {
556 /* Need an Integer - create a zero-value integer */
558 new_object
= acpi_ut_create_integer_object((u64
)0);
559 } else if (expected_btypes
& ACPI_RTYPE_STRING
) {
561 /* Need a String - create a NULL string */
563 new_object
= acpi_ut_create_string_object(0);
564 } else if (expected_btypes
& ACPI_RTYPE_BUFFER
) {
566 /* Need a Buffer - create a zero-length buffer */
568 new_object
= acpi_ut_create_buffer_object(0);
570 /* Error for all other expected types */
572 return (AE_AML_OPERAND_TYPE
);
576 return (AE_NO_MEMORY
);
579 /* Set the reference count according to the parent Package object */
581 new_object
->common
.reference_count
=
582 data
->parent_package
->common
.reference_count
;
584 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
585 "%s: Converted NULL package element to expected %s at index %u\n",
587 acpi_ut_get_object_type_name(new_object
),
590 *return_object_ptr
= new_object
;
591 data
->flags
|= ACPI_OBJECT_REPAIRED
;
595 /******************************************************************************
597 * FUNCTION: acpi_ns_remove_null_elements
599 * PARAMETERS: Data - Pointer to validation data structure
600 * package_type - An acpi_return_package_types value
601 * obj_desc - A Package object
605 * DESCRIPTION: Remove all NULL package elements from packages that contain
606 * a variable number of sub-packages. For these types of
607 * packages, NULL elements can be safely removed.
609 *****************************************************************************/
612 acpi_ns_remove_null_elements(struct acpi_predefined_data
*data
,
614 union acpi_operand_object
*obj_desc
)
616 union acpi_operand_object
**source
;
617 union acpi_operand_object
**dest
;
622 ACPI_FUNCTION_NAME(ns_remove_null_elements
);
625 * We can safely remove all NULL elements from these package types:
626 * PTYPE1_VAR packages contain a variable number of simple data types.
627 * PTYPE2 packages contain a variable number of sub-packages.
629 switch (package_type
) {
630 case ACPI_PTYPE1_VAR
:
632 case ACPI_PTYPE2_COUNT
:
633 case ACPI_PTYPE2_PKG_COUNT
:
634 case ACPI_PTYPE2_FIXED
:
635 case ACPI_PTYPE2_MIN
:
636 case ACPI_PTYPE2_REV_FIXED
:
637 case ACPI_PTYPE2_FIX_VAR
:
641 case ACPI_PTYPE1_FIXED
:
642 case ACPI_PTYPE1_OPTION
:
646 count
= obj_desc
->package
.count
;
649 source
= obj_desc
->package
.elements
;
652 /* Examine all elements of the package object, remove nulls */
654 for (i
= 0; i
< count
; i
++) {
664 /* Update parent package if any null elements were removed */
666 if (new_count
< count
) {
667 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
668 "%s: Found and removed %u NULL elements\n",
669 data
->pathname
, (count
- new_count
)));
671 /* NULL terminate list and update the package count */
674 obj_desc
->package
.count
= new_count
;
678 /*******************************************************************************
680 * FUNCTION: acpi_ns_repair_package_list
682 * PARAMETERS: Data - Pointer to validation data structure
683 * obj_desc_ptr - Pointer to the object to repair. The new
684 * package object is returned here,
685 * overwriting the old object.
687 * RETURN: Status, new object in *obj_desc_ptr
689 * DESCRIPTION: Repair a common problem with objects that are defined to return
690 * a variable-length Package of Packages. If the variable-length
691 * is one, some BIOS code mistakenly simply declares a single
692 * Package instead of a Package with one sub-Package. This
693 * function attempts to repair this error by wrapping a Package
694 * object around the original Package, creating the correct
695 * Package with one sub-Package.
697 * Names that can be repaired in this manner include:
698 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
700 ******************************************************************************/
703 acpi_ns_repair_package_list(struct acpi_predefined_data
*data
,
704 union acpi_operand_object
**obj_desc_ptr
)
706 union acpi_operand_object
*pkg_obj_desc
;
708 ACPI_FUNCTION_NAME(ns_repair_package_list
);
711 * Create the new outer package and populate it. The new package will
712 * have a single element, the lone subpackage.
714 pkg_obj_desc
= acpi_ut_create_package_object(1);
716 return (AE_NO_MEMORY
);
719 pkg_obj_desc
->package
.elements
[0] = *obj_desc_ptr
;
721 /* Return the new object in the object pointer */
723 *obj_desc_ptr
= pkg_obj_desc
;
724 data
->flags
|= ACPI_OBJECT_REPAIRED
;
726 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
727 "%s: Repaired incorrectly formed Package\n",