1 /******************************************************************************
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2009, 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>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair")
52 /*******************************************************************************
54 * This module attempts to repair or convert objects returned by the
55 * predefined methods to an object type that is expected, as per the ACPI
56 * specification. The need for this code is dictated by the many machines that
57 * return incorrect types for the standard predefined methods. Performing these
58 * conversions here, in one place, eliminates the need for individual ACPI
59 * device drivers to do the same. Note: Most of these conversions are different
60 * than the internal object conversion routines used for implicit object
63 * The following conversions can be performed as necessary:
71 * Buffer -> Package of Integers
72 * Package -> Package of one Package
74 ******************************************************************************/
75 /* Local prototypes */
77 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
78 union acpi_operand_object **return_object);
81 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
82 union acpi_operand_object **return_object);
85 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
86 union acpi_operand_object **return_object);
89 acpi_ns_convert_to_package(union acpi_operand_object *original_object,
90 union acpi_operand_object **return_object);
92 /*******************************************************************************
94 * FUNCTION: acpi_ns_repair_object
96 * PARAMETERS: Data - Pointer to validation data structure
97 * expected_btypes - Object types expected
98 * package_index - Index of object within parent package (if
99 * applicable - ACPI_NOT_PACKAGE_ELEMENT
101 * return_object_ptr - Pointer to the object returned from the
102 * evaluation of a method or object
104 * RETURN: Status. AE_OK if repair was successful.
106 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
109 ******************************************************************************/
112 acpi_ns_repair_object(struct acpi_predefined_data *data,
115 union acpi_operand_object **return_object_ptr)
117 union acpi_operand_object *return_object = *return_object_ptr;
118 union acpi_operand_object *new_object;
122 * At this point, we know that the type of the returned object was not
123 * one of the expected types for this predefined name. Attempt to
124 * repair the object by converting it to one of the expected object
125 * types for this predefined name.
127 if (expected_btypes & ACPI_RTYPE_INTEGER) {
128 status = acpi_ns_convert_to_integer(return_object, &new_object);
129 if (ACPI_SUCCESS(status)) {
130 goto object_repaired;
133 if (expected_btypes & ACPI_RTYPE_STRING) {
134 status = acpi_ns_convert_to_string(return_object, &new_object);
135 if (ACPI_SUCCESS(status)) {
136 goto object_repaired;
139 if (expected_btypes & ACPI_RTYPE_BUFFER) {
140 status = acpi_ns_convert_to_buffer(return_object, &new_object);
141 if (ACPI_SUCCESS(status)) {
142 goto object_repaired;
145 if (expected_btypes & ACPI_RTYPE_PACKAGE) {
146 status = acpi_ns_convert_to_package(return_object, &new_object);
147 if (ACPI_SUCCESS(status)) {
148 goto object_repaired;
152 /* We cannot repair this object */
154 return (AE_AML_OPERAND_TYPE);
158 /* Object was successfully repaired */
161 * If the original object is a package element, we need to:
162 * 1. Set the reference count of the new object to match the
163 * reference count of the old object.
164 * 2. Decrement the reference count of the original object.
166 if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
167 new_object->common.reference_count =
168 return_object->common.reference_count;
170 if (return_object->common.reference_count > 1) {
171 return_object->common.reference_count--;
174 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
175 "Converted %s to expected %s at index %u",
176 acpi_ut_get_object_type_name
178 acpi_ut_get_object_type_name(new_object),
181 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
182 "Converted %s to expected %s",
183 acpi_ut_get_object_type_name
185 acpi_ut_get_object_type_name
189 /* Delete old object, install the new return object */
191 acpi_ut_remove_reference(return_object);
192 *return_object_ptr = new_object;
193 data->flags |= ACPI_OBJECT_REPAIRED;
197 /*******************************************************************************
199 * FUNCTION: acpi_ns_convert_to_integer
201 * PARAMETERS: original_object - Object to be converted
202 * return_object - Where the new converted object is returned
204 * RETURN: Status. AE_OK if conversion was successful.
206 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
208 ******************************************************************************/
211 acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
212 union acpi_operand_object **return_object)
214 union acpi_operand_object *new_object;
219 switch (original_object->common.type) {
220 case ACPI_TYPE_STRING:
222 /* String-to-Integer conversion */
224 status = acpi_ut_strtoul64(original_object->string.pointer,
225 ACPI_ANY_BASE, &value);
226 if (ACPI_FAILURE(status)) {
231 case ACPI_TYPE_BUFFER:
233 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
235 if (original_object->buffer.length > 8) {
236 return (AE_AML_OPERAND_TYPE);
239 /* Extract each buffer byte to create the integer */
241 for (i = 0; i < original_object->buffer.length; i++) {
243 ((u64) original_object->buffer.
244 pointer[i] << (i * 8));
249 return (AE_AML_OPERAND_TYPE);
252 new_object = acpi_ut_create_integer_object(value);
254 return (AE_NO_MEMORY);
257 *return_object = new_object;
261 /*******************************************************************************
263 * FUNCTION: acpi_ns_convert_to_string
265 * PARAMETERS: original_object - Object to be converted
266 * return_object - Where the new converted object is returned
268 * RETURN: Status. AE_OK if conversion was successful.
270 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
272 ******************************************************************************/
275 acpi_ns_convert_to_string(union acpi_operand_object *original_object,
276 union acpi_operand_object **return_object)
278 union acpi_operand_object *new_object;
282 switch (original_object->common.type) {
283 case ACPI_TYPE_INTEGER:
285 * Integer-to-String conversion. Commonly, convert
286 * an integer of value 0 to a NULL string. The last element of
287 * _BIF and _BIX packages occasionally need this fix.
289 if (original_object->integer.value == 0) {
291 /* Allocate a new NULL string object */
293 new_object = acpi_ut_create_string_object(0);
295 return (AE_NO_MEMORY);
299 acpi_ex_convert_to_string(original_object,
301 ACPI_IMPLICIT_CONVERT_HEX);
302 if (ACPI_FAILURE(status)) {
308 case ACPI_TYPE_BUFFER:
310 * Buffer-to-String conversion. Use a to_string
311 * conversion, no transform performed on the buffer data. The best
312 * example of this is the _BIF method, where the string data from
313 * the battery is often (incorrectly) returned as buffer object(s).
316 while ((length < original_object->buffer.length) &&
317 (original_object->buffer.pointer[length])) {
321 /* Allocate a new string object */
323 new_object = acpi_ut_create_string_object(length);
325 return (AE_NO_MEMORY);
329 * Copy the raw buffer data with no transform. String is already NULL
330 * terminated at Length+1.
332 ACPI_MEMCPY(new_object->string.pointer,
333 original_object->buffer.pointer, length);
337 return (AE_AML_OPERAND_TYPE);
340 *return_object = new_object;
344 /*******************************************************************************
346 * FUNCTION: acpi_ns_convert_to_buffer
348 * PARAMETERS: original_object - Object to be converted
349 * return_object - Where the new converted object is returned
351 * RETURN: Status. AE_OK if conversion was successful.
353 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
355 ******************************************************************************/
358 acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
359 union acpi_operand_object **return_object)
361 union acpi_operand_object *new_object;
363 union acpi_operand_object **elements;
368 switch (original_object->common.type) {
369 case ACPI_TYPE_INTEGER:
371 * Integer-to-Buffer conversion.
372 * Convert the Integer to a packed-byte buffer. _MAT and other
373 * objects need this sometimes, if a read has been performed on a
374 * Field object that is less than or equal to the global integer
375 * size (32 or 64 bits).
378 acpi_ex_convert_to_buffer(original_object, &new_object);
379 if (ACPI_FAILURE(status)) {
384 case ACPI_TYPE_STRING:
386 /* String-to-Buffer conversion. Simple data copy */
389 acpi_ut_create_buffer_object(original_object->string.
392 return (AE_NO_MEMORY);
395 ACPI_MEMCPY(new_object->buffer.pointer,
396 original_object->string.pointer,
397 original_object->string.length);
400 case ACPI_TYPE_PACKAGE:
402 /* All elements of the Package must be integers */
404 elements = original_object->package.elements;
405 count = original_object->package.count;
407 for (i = 0; i < count; i++) {
409 ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
410 return (AE_AML_OPERAND_TYPE);
415 /* Create the new buffer object to replace the Package */
417 new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
419 return (AE_NO_MEMORY);
422 /* Copy the package elements (integers) to the buffer as DWORDs */
424 elements = original_object->package.elements;
425 dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
427 for (i = 0; i < count; i++) {
428 *dword_buffer = (u32) (*elements)->integer.value;
435 return (AE_AML_OPERAND_TYPE);
438 *return_object = new_object;
442 /*******************************************************************************
444 * FUNCTION: acpi_ns_convert_to_package
446 * PARAMETERS: original_object - Object to be converted
447 * return_object - Where the new converted object is returned
449 * RETURN: Status. AE_OK if conversion was successful.
451 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
452 * the buffer is converted to a single integer package element.
454 ******************************************************************************/
457 acpi_ns_convert_to_package(union acpi_operand_object *original_object,
458 union acpi_operand_object **return_object)
460 union acpi_operand_object *new_object;
461 union acpi_operand_object **elements;
465 switch (original_object->common.type) {
466 case ACPI_TYPE_BUFFER:
468 /* Buffer-to-Package conversion */
470 length = original_object->buffer.length;
471 new_object = acpi_ut_create_package_object(length);
473 return (AE_NO_MEMORY);
476 /* Convert each buffer byte to an integer package element */
478 elements = new_object->package.elements;
479 buffer = original_object->buffer.pointer;
483 acpi_ut_create_integer_object((u64) *buffer);
485 acpi_ut_remove_reference(new_object);
486 return (AE_NO_MEMORY);
494 return (AE_AML_OPERAND_TYPE);
497 *return_object = new_object;
501 /*******************************************************************************
503 * FUNCTION: acpi_ns_repair_package_list
505 * PARAMETERS: Data - Pointer to validation data structure
506 * obj_desc_ptr - Pointer to the object to repair. The new
507 * package object is returned here,
508 * overwriting the old object.
510 * RETURN: Status, new object in *obj_desc_ptr
512 * DESCRIPTION: Repair a common problem with objects that are defined to return
513 * a variable-length Package of Packages. If the variable-length
514 * is one, some BIOS code mistakenly simply declares a single
515 * Package instead of a Package with one sub-Package. This
516 * function attempts to repair this error by wrapping a Package
517 * object around the original Package, creating the correct
518 * Package with one sub-Package.
520 * Names that can be repaired in this manner include:
521 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
523 ******************************************************************************/
526 acpi_ns_repair_package_list(struct acpi_predefined_data *data,
527 union acpi_operand_object **obj_desc_ptr)
529 union acpi_operand_object *pkg_obj_desc;
532 * Create the new outer package and populate it. The new package will
533 * have a single element, the lone subpackage.
535 pkg_obj_desc = acpi_ut_create_package_object(1);
537 return (AE_NO_MEMORY);
540 pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
542 /* Return the new object in the object pointer */
544 *obj_desc_ptr = pkg_obj_desc;
545 data->flags |= ACPI_OBJECT_REPAIRED;
547 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
548 "Repaired Incorrectly formed Package"));