1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: nsrepair - Repair for objects returned by predefined methods
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
17 #define _COMPONENT ACPI_NAMESPACE
18 ACPI_MODULE_NAME("nsrepair")
20 /*******************************************************************************
22 * This module attempts to repair or convert objects returned by the
23 * predefined methods to an object type that is expected, as per the ACPI
24 * specification. The need for this code is dictated by the many machines that
25 * return incorrect types for the standard predefined methods. Performing these
26 * conversions here, in one place, eliminates the need for individual ACPI
27 * device drivers to do the same. Note: Most of these conversions are different
28 * than the internal object conversion routines used for implicit object
31 * The following conversions can be performed as necessary:
39 * Buffer -> Package of Integers
40 * Package -> Package of one Package
42 * Additional conversions that are available:
43 * Convert a null return or zero return value to an end_tag descriptor
44 * Convert an ASCII string to a Unicode buffer
46 * An incorrect standalone object is wrapped with required outer package
48 * Additional possible repairs:
49 * Required package elements that are NULL replaced by Integer/String/Buffer
51 ******************************************************************************/
52 /* Local prototypes */
53 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
62 * Special but simple repairs for some names.
64 * 2nd argument: Unexpected types that can be repaired
66 static const struct acpi_simple_repair_info acpi_object_repair_info[] = {
67 /* Resource descriptor conversions */
70 ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
72 ACPI_NOT_PACKAGE_ELEMENT,
73 acpi_ns_convert_to_resource},
75 ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
77 ACPI_NOT_PACKAGE_ELEMENT,
78 acpi_ns_convert_to_resource},
80 ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
82 ACPI_NOT_PACKAGE_ELEMENT,
83 acpi_ns_convert_to_resource},
85 /* Object reference conversions */
87 {"_DEP", ACPI_RTYPE_STRING, ACPI_ALL_PACKAGE_ELEMENTS,
88 acpi_ns_convert_to_reference},
90 /* Unicode conversions */
92 {"_MLS", ACPI_RTYPE_STRING, 1,
93 acpi_ns_convert_to_unicode},
94 {"_STR", ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER,
95 ACPI_NOT_PACKAGE_ELEMENT,
96 acpi_ns_convert_to_unicode},
97 {{0, 0, 0, 0}, 0, 0, NULL} /* Table terminator */
100 /*******************************************************************************
102 * FUNCTION: acpi_ns_simple_repair
104 * PARAMETERS: info - Method execution information block
105 * expected_btypes - Object types expected
106 * package_index - Index of object within parent package (if
107 * applicable - ACPI_NOT_PACKAGE_ELEMENT
109 * return_object_ptr - Pointer to the object returned from the
110 * evaluation of a method or object
112 * RETURN: Status. AE_OK if repair was successful.
114 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
117 ******************************************************************************/
120 acpi_ns_simple_repair(struct acpi_evaluate_info *info,
123 union acpi_operand_object **return_object_ptr)
125 union acpi_operand_object *return_object = *return_object_ptr;
126 union acpi_operand_object *new_object = NULL;
128 const struct acpi_simple_repair_info *predefined;
130 ACPI_FUNCTION_NAME(ns_simple_repair);
133 * Special repairs for certain names that are in the repair table.
134 * Check if this name is in the list of repairable names.
136 predefined = acpi_ns_match_simple_repair(info->node,
140 if (!return_object) {
141 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
143 "Missing expected return value"));
146 status = predefined->object_converter(info->node, return_object,
148 if (ACPI_FAILURE(status)) {
150 /* A fatal error occurred during a conversion */
152 ACPI_EXCEPTION((AE_INFO, status,
153 "During return object analysis"));
157 goto object_repaired;
162 * Do not perform simple object repair unless the return type is not
165 if (info->return_btype & expected_btypes) {
170 * At this point, we know that the type of the returned object was not
171 * one of the expected types for this predefined name. Attempt to
172 * repair the object by converting it to one of the expected object
173 * types for this predefined name.
177 * If there is no return value, check if we require a return value for
178 * this predefined name. Either one return value is expected, or none,
179 * for both methods and other objects.
181 * Try to fix if there was no return object. Warning if failed to fix.
183 if (!return_object) {
184 if (expected_btypes) {
185 if (!(expected_btypes & ACPI_RTYPE_NONE) &&
186 package_index != ACPI_NOT_PACKAGE_ELEMENT) {
187 ACPI_WARN_PREDEFINED((AE_INFO,
190 "Found unexpected NULL package element"));
193 acpi_ns_repair_null_element(info,
197 if (ACPI_SUCCESS(status)) {
198 return (AE_OK); /* Repair was successful */
202 if (expected_btypes != ACPI_RTYPE_NONE) {
203 ACPI_WARN_PREDEFINED((AE_INFO,
206 "Missing expected return value"));
207 return (AE_AML_NO_RETURN_VALUE);
212 if (expected_btypes & ACPI_RTYPE_INTEGER) {
213 status = acpi_ns_convert_to_integer(return_object, &new_object);
214 if (ACPI_SUCCESS(status)) {
215 goto object_repaired;
218 if (expected_btypes & ACPI_RTYPE_STRING) {
219 status = acpi_ns_convert_to_string(return_object, &new_object);
220 if (ACPI_SUCCESS(status)) {
221 goto object_repaired;
224 if (expected_btypes & ACPI_RTYPE_BUFFER) {
225 status = acpi_ns_convert_to_buffer(return_object, &new_object);
226 if (ACPI_SUCCESS(status)) {
227 goto object_repaired;
230 if (expected_btypes & ACPI_RTYPE_PACKAGE) {
232 * A package is expected. We will wrap the existing object with a
233 * new package object. It is often the case that if a variable-length
234 * package is required, but there is only a single object needed, the
235 * BIOS will return that object instead of wrapping it with a Package
236 * object. Note: after the wrapping, the package will be validated
237 * for correct contents (expected object type or types).
240 acpi_ns_wrap_with_package(info, return_object, &new_object);
241 if (ACPI_SUCCESS(status)) {
243 * The original object just had its reference count
244 * incremented for being inserted into the new package.
246 *return_object_ptr = new_object; /* New Package object */
247 info->return_flags |= ACPI_OBJECT_REPAIRED;
252 /* We cannot repair this object */
254 return (AE_AML_OPERAND_TYPE);
258 /* Object was successfully repaired */
260 if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
262 /* Update reference count of new object */
264 if (!(info->return_flags & ACPI_OBJECT_WRAPPED)) {
265 new_object->common.reference_count =
266 return_object->common.reference_count;
269 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
270 "%s: Converted %s to expected %s at Package index %u\n",
272 acpi_ut_get_object_type_name(return_object),
273 acpi_ut_get_object_type_name(new_object),
276 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
277 "%s: Converted %s to expected %s\n",
279 acpi_ut_get_object_type_name(return_object),
280 acpi_ut_get_object_type_name(new_object)));
283 /* Delete old object, install the new return object */
285 acpi_ut_remove_reference(return_object);
286 *return_object_ptr = new_object;
287 info->return_flags |= ACPI_OBJECT_REPAIRED;
291 /******************************************************************************
293 * FUNCTION: acpi_ns_match_simple_repair
295 * PARAMETERS: node - Namespace node for the method/object
296 * return_btype - Object type that was returned
297 * package_index - Index of object within parent package (if
298 * applicable - ACPI_NOT_PACKAGE_ELEMENT
301 * RETURN: Pointer to entry in repair table. NULL indicates not found.
303 * DESCRIPTION: Check an object name against the repairable object list.
305 *****************************************************************************/
307 static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
315 const struct acpi_simple_repair_info *this_name;
317 /* Search info table for a repairable predefined method/object name */
319 this_name = acpi_object_repair_info;
320 while (this_name->object_converter) {
321 if (ACPI_COMPARE_NAMESEG(node->name.ascii, this_name->name)) {
323 /* Check if we can actually repair this name/type combination */
325 if ((return_btype & this_name->unexpected_btypes) &&
326 (this_name->package_index ==
327 ACPI_ALL_PACKAGE_ELEMENTS
328 || package_index == this_name->package_index)) {
338 return (NULL); /* Name was not found in the repair table */
341 /*******************************************************************************
343 * FUNCTION: acpi_ns_repair_null_element
345 * PARAMETERS: info - Method execution information block
346 * expected_btypes - Object types expected
347 * package_index - Index of object within parent package (if
348 * applicable - ACPI_NOT_PACKAGE_ELEMENT
350 * return_object_ptr - Pointer to the object returned from the
351 * evaluation of a method or object
353 * RETURN: Status. AE_OK if repair was successful.
355 * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
357 ******************************************************************************/
360 acpi_ns_repair_null_element(struct acpi_evaluate_info *info,
363 union acpi_operand_object **return_object_ptr)
365 union acpi_operand_object *return_object = *return_object_ptr;
366 union acpi_operand_object *new_object;
368 ACPI_FUNCTION_NAME(ns_repair_null_element);
370 /* No repair needed if return object is non-NULL */
377 * Attempt to repair a NULL element of a Package object. This applies to
378 * predefined names that return a fixed-length package and each element
379 * is required. It does not apply to variable-length packages where NULL
380 * elements are allowed, especially at the end of the package.
382 if (expected_btypes & ACPI_RTYPE_INTEGER) {
384 /* Need an integer - create a zero-value integer */
386 new_object = acpi_ut_create_integer_object((u64)0);
387 } else if (expected_btypes & ACPI_RTYPE_STRING) {
389 /* Need a string - create a NULL string */
391 new_object = acpi_ut_create_string_object(0);
392 } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
394 /* Need a buffer - create a zero-length buffer */
396 new_object = acpi_ut_create_buffer_object(0);
398 /* Error for all other expected types */
400 return (AE_AML_OPERAND_TYPE);
404 return (AE_NO_MEMORY);
407 /* Set the reference count according to the parent Package object */
409 new_object->common.reference_count =
410 info->parent_package->common.reference_count;
412 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
413 "%s: Converted NULL package element to expected %s at index %u\n",
415 acpi_ut_get_object_type_name(new_object),
418 *return_object_ptr = new_object;
419 info->return_flags |= ACPI_OBJECT_REPAIRED;
423 /******************************************************************************
425 * FUNCTION: acpi_ns_remove_null_elements
427 * PARAMETERS: info - Method execution information block
428 * package_type - An acpi_return_package_types value
429 * obj_desc - A Package object
433 * DESCRIPTION: Remove all NULL package elements from packages that contain
434 * a variable number of subpackages. For these types of
435 * packages, NULL elements can be safely removed.
437 *****************************************************************************/
440 acpi_ns_remove_null_elements(struct acpi_evaluate_info *info,
442 union acpi_operand_object *obj_desc)
444 union acpi_operand_object **source;
445 union acpi_operand_object **dest;
450 ACPI_FUNCTION_NAME(ns_remove_null_elements);
453 * We can safely remove all NULL elements from these package types:
454 * PTYPE1_VAR packages contain a variable number of simple data types.
455 * PTYPE2 packages contain a variable number of subpackages.
457 switch (package_type) {
458 case ACPI_PTYPE1_VAR:
460 case ACPI_PTYPE2_COUNT:
461 case ACPI_PTYPE2_PKG_COUNT:
462 case ACPI_PTYPE2_FIXED:
463 case ACPI_PTYPE2_MIN:
464 case ACPI_PTYPE2_REV_FIXED:
465 case ACPI_PTYPE2_FIX_VAR:
469 case ACPI_PTYPE2_VAR_VAR:
470 case ACPI_PTYPE1_FIXED:
471 case ACPI_PTYPE1_OPTION:
475 count = obj_desc->package.count;
478 source = obj_desc->package.elements;
481 /* Examine all elements of the package object, remove nulls */
483 for (i = 0; i < count; i++) {
494 /* Update parent package if any null elements were removed */
496 if (new_count < count) {
497 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
498 "%s: Found and removed %u NULL elements\n",
499 info->full_pathname, (count - new_count)));
501 /* NULL terminate list and update the package count */
504 obj_desc->package.count = new_count;
508 /*******************************************************************************
510 * FUNCTION: acpi_ns_wrap_with_package
512 * PARAMETERS: info - Method execution information block
513 * original_object - Pointer to the object to repair.
514 * obj_desc_ptr - The new package object is returned here
516 * RETURN: Status, new object in *obj_desc_ptr
518 * DESCRIPTION: Repair a common problem with objects that are defined to
519 * return a variable-length Package of sub-objects. If there is
520 * only one sub-object, some BIOS code mistakenly simply declares
521 * the single object instead of a Package with one sub-object.
522 * This function attempts to repair this error by wrapping a
523 * Package object around the original object, creating the
524 * correct and expected Package with one sub-object.
526 * Names that can be repaired in this manner include:
527 * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
528 * _BCL, _DOD, _FIX, _Sx
530 ******************************************************************************/
533 acpi_ns_wrap_with_package(struct acpi_evaluate_info *info,
534 union acpi_operand_object *original_object,
535 union acpi_operand_object **obj_desc_ptr)
537 union acpi_operand_object *pkg_obj_desc;
539 ACPI_FUNCTION_NAME(ns_wrap_with_package);
542 * Create the new outer package and populate it. The new
543 * package will have a single element, the lone sub-object.
545 pkg_obj_desc = acpi_ut_create_package_object(1);
547 return (AE_NO_MEMORY);
550 pkg_obj_desc->package.elements[0] = original_object;
552 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
553 "%s: Wrapped %s with expected Package object\n",
555 acpi_ut_get_object_type_name(original_object)));
557 /* Return the new object in the object pointer */
559 *obj_desc_ptr = pkg_obj_desc;
560 info->return_flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;