1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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
74 * An incorrect standalone object is wrapped with required outer package
76 * Additional possible repairs:
77 * Required package elements that are NULL replaced by Integer/String/Buffer
79 ******************************************************************************/
80 /* Local prototypes */
82 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
83 union acpi_operand_object **return_object);
86 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
87 union acpi_operand_object **return_object);
90 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
91 union acpi_operand_object **return_object);
93 /*******************************************************************************
95 * FUNCTION: acpi_ns_repair_object
97 * PARAMETERS: data - Pointer to validation data structure
98 * expected_btypes - Object types expected
99 * package_index - Index of object within parent package (if
100 * applicable - ACPI_NOT_PACKAGE_ELEMENT
102 * return_object_ptr - Pointer to the object returned from the
103 * evaluation of a method or object
105 * RETURN: Status. AE_OK if repair was successful.
107 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
110 ******************************************************************************/
113 acpi_ns_repair_object(struct acpi_predefined_data *data,
116 union acpi_operand_object **return_object_ptr)
118 union acpi_operand_object *return_object = *return_object_ptr;
119 union acpi_operand_object *new_object;
122 ACPI_FUNCTION_NAME(ns_repair_object);
125 * At this point, we know that the type of the returned object was not
126 * one of the expected types for this predefined name. Attempt to
127 * repair the object by converting it to one of the expected object
128 * types for this predefined name.
130 if (expected_btypes & ACPI_RTYPE_INTEGER) {
131 status = acpi_ns_convert_to_integer(return_object, &new_object);
132 if (ACPI_SUCCESS(status)) {
133 goto object_repaired;
136 if (expected_btypes & ACPI_RTYPE_STRING) {
137 status = acpi_ns_convert_to_string(return_object, &new_object);
138 if (ACPI_SUCCESS(status)) {
139 goto object_repaired;
142 if (expected_btypes & ACPI_RTYPE_BUFFER) {
143 status = acpi_ns_convert_to_buffer(return_object, &new_object);
144 if (ACPI_SUCCESS(status)) {
145 goto object_repaired;
148 if (expected_btypes & ACPI_RTYPE_PACKAGE) {
150 * A package is expected. We will wrap the existing object with a
151 * new package object. It is often the case that if a variable-length
152 * package is required, but there is only a single object needed, the
153 * BIOS will return that object instead of wrapping it with a Package
154 * object. Note: after the wrapping, the package will be validated
155 * for correct contents (expected object type or types).
158 acpi_ns_wrap_with_package(data, return_object, &new_object);
159 if (ACPI_SUCCESS(status)) {
161 * The original object just had its reference count
162 * incremented for being inserted into the new package.
164 *return_object_ptr = new_object; /* New Package object */
165 data->flags |= ACPI_OBJECT_REPAIRED;
170 /* We cannot repair this object */
172 return (AE_AML_OPERAND_TYPE);
176 /* Object was successfully repaired */
178 if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
180 * The original object is a package element. We need to
181 * decrement the reference count of the original object,
182 * for removing it from the package.
184 * However, if the original object was just wrapped with a
185 * package object as part of the repair, we don't need to
186 * change the reference count.
188 if (!(data->flags & ACPI_OBJECT_WRAPPED)) {
189 new_object->common.reference_count =
190 return_object->common.reference_count;
192 if (return_object->common.reference_count > 1) {
193 return_object->common.reference_count--;
197 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
198 "%s: Converted %s to expected %s at Package index %u\n",
200 acpi_ut_get_object_type_name(return_object),
201 acpi_ut_get_object_type_name(new_object),
204 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
205 "%s: Converted %s to expected %s\n",
207 acpi_ut_get_object_type_name(return_object),
208 acpi_ut_get_object_type_name(new_object)));
211 /* Delete old object, install the new return object */
213 acpi_ut_remove_reference(return_object);
214 *return_object_ptr = new_object;
215 data->flags |= ACPI_OBJECT_REPAIRED;
219 /*******************************************************************************
221 * FUNCTION: acpi_ns_convert_to_integer
223 * PARAMETERS: original_object - Object to be converted
224 * return_object - Where the new converted object is returned
226 * RETURN: Status. AE_OK if conversion was successful.
228 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
230 ******************************************************************************/
233 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
234 union acpi_operand_object **return_object)
236 union acpi_operand_object *new_object;
241 switch (original_object->common.type) {
242 case ACPI_TYPE_STRING:
244 /* String-to-Integer conversion */
246 status = acpi_ut_strtoul64(original_object->string.pointer,
247 ACPI_ANY_BASE, &value);
248 if (ACPI_FAILURE(status)) {
253 case ACPI_TYPE_BUFFER:
255 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
257 if (original_object->buffer.length > 8) {
258 return (AE_AML_OPERAND_TYPE);
261 /* Extract each buffer byte to create the integer */
263 for (i = 0; i < original_object->buffer.length; i++) {
265 ((u64) original_object->buffer.
266 pointer[i] << (i * 8));
271 return (AE_AML_OPERAND_TYPE);
274 new_object = acpi_ut_create_integer_object(value);
276 return (AE_NO_MEMORY);
279 *return_object = new_object;
283 /*******************************************************************************
285 * FUNCTION: acpi_ns_convert_to_string
287 * PARAMETERS: original_object - Object to be converted
288 * return_object - Where the new converted object is returned
290 * RETURN: Status. AE_OK if conversion was successful.
292 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
294 ******************************************************************************/
297 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
298 union acpi_operand_object **return_object)
300 union acpi_operand_object *new_object;
304 switch (original_object->common.type) {
305 case ACPI_TYPE_INTEGER:
307 * Integer-to-String conversion. Commonly, convert
308 * an integer of value 0 to a NULL string. The last element of
309 * _BIF and _BIX packages occasionally need this fix.
311 if (original_object->integer.value == 0) {
313 /* Allocate a new NULL string object */
315 new_object = acpi_ut_create_string_object(0);
317 return (AE_NO_MEMORY);
321 acpi_ex_convert_to_string(original_object,
323 ACPI_IMPLICIT_CONVERT_HEX);
324 if (ACPI_FAILURE(status)) {
330 case ACPI_TYPE_BUFFER:
332 * Buffer-to-String conversion. Use a to_string
333 * conversion, no transform performed on the buffer data. The best
334 * example of this is the _BIF method, where the string data from
335 * the battery is often (incorrectly) returned as buffer object(s).
338 while ((length < original_object->buffer.length) &&
339 (original_object->buffer.pointer[length])) {
343 /* Allocate a new string object */
345 new_object = acpi_ut_create_string_object(length);
347 return (AE_NO_MEMORY);
351 * Copy the raw buffer data with no transform. String is already NULL
352 * terminated at Length+1.
354 ACPI_MEMCPY(new_object->string.pointer,
355 original_object->buffer.pointer, length);
359 return (AE_AML_OPERAND_TYPE);
362 *return_object = new_object;
366 /*******************************************************************************
368 * FUNCTION: acpi_ns_convert_to_buffer
370 * PARAMETERS: original_object - Object to be converted
371 * return_object - Where the new converted object is returned
373 * RETURN: Status. AE_OK if conversion was successful.
375 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
377 ******************************************************************************/
380 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
381 union acpi_operand_object **return_object)
383 union acpi_operand_object *new_object;
385 union acpi_operand_object **elements;
390 switch (original_object->common.type) {
391 case ACPI_TYPE_INTEGER:
393 * Integer-to-Buffer conversion.
394 * Convert the Integer to a packed-byte buffer. _MAT and other
395 * objects need this sometimes, if a read has been performed on a
396 * Field object that is less than or equal to the global integer
397 * size (32 or 64 bits).
400 acpi_ex_convert_to_buffer(original_object, &new_object);
401 if (ACPI_FAILURE(status)) {
406 case ACPI_TYPE_STRING:
408 /* String-to-Buffer conversion. Simple data copy */
411 acpi_ut_create_buffer_object(original_object->string.
414 return (AE_NO_MEMORY);
417 ACPI_MEMCPY(new_object->buffer.pointer,
418 original_object->string.pointer,
419 original_object->string.length);
422 case ACPI_TYPE_PACKAGE:
424 * This case is often seen for predefined names that must return a
425 * Buffer object with multiple DWORD integers within. For example,
426 * _FDE and _GTM. The Package can be converted to a Buffer.
429 /* All elements of the Package must be integers */
431 elements = original_object->package.elements;
432 count = original_object->package.count;
434 for (i = 0; i < count; i++) {
436 ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
437 return (AE_AML_OPERAND_TYPE);
442 /* Create the new buffer object to replace the Package */
444 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
446 return (AE_NO_MEMORY);
449 /* Copy the package elements (integers) to the buffer as DWORDs */
451 elements = original_object->package.elements;
452 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
454 for (i = 0; i < count; i++) {
455 *dword_buffer = (u32) (*elements)->integer.value;
462 return (AE_AML_OPERAND_TYPE);
465 *return_object = new_object;
469 /*******************************************************************************
471 * FUNCTION: acpi_ns_repair_null_element
473 * PARAMETERS: data - Pointer to validation data structure
474 * expected_btypes - Object types expected
475 * package_index - Index of object within parent package (if
476 * applicable - ACPI_NOT_PACKAGE_ELEMENT
478 * return_object_ptr - Pointer to the object returned from the
479 * evaluation of a method or object
481 * RETURN: Status. AE_OK if repair was successful.
483 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
485 ******************************************************************************/
488 acpi_ns_repair_null_element(struct acpi_predefined_data *data,
491 union acpi_operand_object **return_object_ptr)
493 union acpi_operand_object *return_object = *return_object_ptr;
494 union acpi_operand_object *new_object;
496 ACPI_FUNCTION_NAME(ns_repair_null_element);
498 /* No repair needed if return object is non-NULL */
505 * Attempt to repair a NULL element of a Package object. This applies to
506 * predefined names that return a fixed-length package and each element
507 * is required. It does not apply to variable-length packages where NULL
508 * elements are allowed, especially at the end of the package.
510 if (expected_btypes & ACPI_RTYPE_INTEGER) {
512 /* Need an integer - create a zero-value integer */
514 new_object = acpi_ut_create_integer_object((u64)0);
515 } else if (expected_btypes & ACPI_RTYPE_STRING) {
517 /* Need a string - create a NULL string */
519 new_object = acpi_ut_create_string_object(0);
520 } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
522 /* Need a buffer - create a zero-length buffer */
524 new_object = acpi_ut_create_buffer_object(0);
526 /* Error for all other expected types */
528 return (AE_AML_OPERAND_TYPE);
532 return (AE_NO_MEMORY);
535 /* Set the reference count according to the parent Package object */
537 new_object->common.reference_count =
538 data->parent_package->common.reference_count;
540 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
541 "%s: Converted NULL package element to expected %s at index %u\n",
543 acpi_ut_get_object_type_name(new_object),
546 *return_object_ptr = new_object;
547 data->flags |= ACPI_OBJECT_REPAIRED;
551 /******************************************************************************
553 * FUNCTION: acpi_ns_remove_null_elements
555 * PARAMETERS: data - Pointer to validation data structure
556 * package_type - An acpi_return_package_types value
557 * obj_desc - A Package object
561 * DESCRIPTION: Remove all NULL package elements from packages that contain
562 * a variable number of sub-packages. For these types of
563 * packages, NULL elements can be safely removed.
565 *****************************************************************************/
568 acpi_ns_remove_null_elements(struct acpi_predefined_data *data,
570 union acpi_operand_object *obj_desc)
572 union acpi_operand_object **source;
573 union acpi_operand_object **dest;
578 ACPI_FUNCTION_NAME(ns_remove_null_elements);
581 * We can safely remove all NULL elements from these package types:
582 * PTYPE1_VAR packages contain a variable number of simple data types.
583 * PTYPE2 packages contain a variable number of sub-packages.
585 switch (package_type) {
586 case ACPI_PTYPE1_VAR:
588 case ACPI_PTYPE2_COUNT:
589 case ACPI_PTYPE2_PKG_COUNT:
590 case ACPI_PTYPE2_FIXED:
591 case ACPI_PTYPE2_MIN:
592 case ACPI_PTYPE2_REV_FIXED:
593 case ACPI_PTYPE2_FIX_VAR:
597 case ACPI_PTYPE1_FIXED:
598 case ACPI_PTYPE1_OPTION:
602 count = obj_desc->package.count;
605 source = obj_desc->package.elements;
608 /* Examine all elements of the package object, remove nulls */
610 for (i = 0; i < count; i++) {
620 /* Update parent package if any null elements were removed */
622 if (new_count < count) {
623 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
624 "%s: Found and removed %u NULL elements\n",
625 data->pathname, (count - new_count)));
627 /* NULL terminate list and update the package count */
630 obj_desc->package.count = new_count;
634 /*******************************************************************************
636 * FUNCTION: acpi_ns_wrap_with_package
638 * PARAMETERS: data - Pointer to validation data structure
639 * original_object - Pointer to the object to repair.
640 * obj_desc_ptr - The new package object is returned here
642 * RETURN: Status, new object in *obj_desc_ptr
644 * DESCRIPTION: Repair a common problem with objects that are defined to
645 * return a variable-length Package of sub-objects. If there is
646 * only one sub-object, some BIOS code mistakenly simply declares
647 * the single object instead of a Package with one sub-object.
648 * This function attempts to repair this error by wrapping a
649 * Package object around the original object, creating the
650 * correct and expected Package with one sub-object.
652 * Names that can be repaired in this manner include:
653 * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
654 * _BCL, _DOD, _FIX, _Sx
656 ******************************************************************************/
659 acpi_ns_wrap_with_package(struct acpi_predefined_data *data,
660 union acpi_operand_object *original_object,
661 union acpi_operand_object **obj_desc_ptr)
663 union acpi_operand_object *pkg_obj_desc;
665 ACPI_FUNCTION_NAME(ns_wrap_with_package);
668 * Create the new outer package and populate it. The new package will
669 * have a single element, the lone sub-object.
671 pkg_obj_desc = acpi_ut_create_package_object(1);
673 return (AE_NO_MEMORY);
676 pkg_obj_desc->package.elements[0] = original_object;
678 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
679 "%s: Wrapped %s with expected Package object\n",
681 acpi_ut_get_object_type_name(original_object)));
683 /* Return the new object in the object pointer */
685 *obj_desc_ptr = pkg_obj_desc;
686 data->flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;