nsrepair.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair - Repair for objects returned by predefined methods
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2009, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "acinterp.h"
  46. #include "acpredef.h"
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME("nsrepair")
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ns_repair_object
  52. *
  53. * PARAMETERS: Data - Pointer to validation data structure
  54. * expected_btypes - Object types expected
  55. * package_index - Index of object within parent package (if
  56. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  57. * otherwise)
  58. * return_object_ptr - Pointer to the object returned from the
  59. * evaluation of a method or object
  60. *
  61. * RETURN: Status. AE_OK if repair was successful.
  62. *
  63. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  64. * not expected.
  65. *
  66. ******************************************************************************/
  67. acpi_status
  68. acpi_ns_repair_object(struct acpi_predefined_data *data,
  69. u32 expected_btypes,
  70. u32 package_index,
  71. union acpi_operand_object **return_object_ptr)
  72. {
  73. union acpi_operand_object *return_object = *return_object_ptr;
  74. union acpi_operand_object *new_object;
  75. acpi_size length;
  76. acpi_status status;
  77. /*
  78. * At this point, we know that the type of the returned object was not
  79. * one of the expected types for this predefined name. Attempt to
  80. * repair the object. Only a limited number of repairs are possible.
  81. */
  82. switch (return_object->common.type) {
  83. case ACPI_TYPE_BUFFER:
  84. /* Does the method/object legally return a string? */
  85. if (!(expected_btypes & ACPI_RTYPE_STRING)) {
  86. return (AE_AML_OPERAND_TYPE);
  87. }
  88. /*
  89. * Have a Buffer, expected a String, convert. Use a to_string
  90. * conversion, no transform performed on the buffer data. The best
  91. * example of this is the _BIF method, where the string data from
  92. * the battery is often (incorrectly) returned as buffer object(s).
  93. */
  94. length = 0;
  95. while ((length < return_object->buffer.length) &&
  96. (return_object->buffer.pointer[length])) {
  97. length++;
  98. }
  99. /* Allocate a new string object */
  100. new_object = acpi_ut_create_string_object(length);
  101. if (!new_object) {
  102. return (AE_NO_MEMORY);
  103. }
  104. /*
  105. * Copy the raw buffer data with no transform. String is already NULL
  106. * terminated at Length+1.
  107. */
  108. ACPI_MEMCPY(new_object->string.pointer,
  109. return_object->buffer.pointer, length);
  110. break;
  111. case ACPI_TYPE_INTEGER:
  112. /* 1) Does the method/object legally return a buffer? */
  113. if (expected_btypes & ACPI_RTYPE_BUFFER) {
  114. /*
  115. * Convert the Integer to a packed-byte buffer. _MAT needs
  116. * this sometimes, if a read has been performed on a Field
  117. * object that is less than or equal to the global integer
  118. * size (32 or 64 bits).
  119. */
  120. status =
  121. acpi_ex_convert_to_buffer(return_object,
  122. &new_object);
  123. if (ACPI_FAILURE(status)) {
  124. return (status);
  125. }
  126. }
  127. /* 2) Does the method/object legally return a string? */
  128. else if (expected_btypes & ACPI_RTYPE_STRING) {
  129. /*
  130. * The only supported Integer-to-String conversion is to convert
  131. * an integer of value 0 to a NULL string. The last element of
  132. * _BIF and _BIX packages occasionally need this fix.
  133. */
  134. if (return_object->integer.value != 0) {
  135. return (AE_AML_OPERAND_TYPE);
  136. }
  137. /* Allocate a new NULL string object */
  138. new_object = acpi_ut_create_string_object(0);
  139. if (!new_object) {
  140. return (AE_NO_MEMORY);
  141. }
  142. } else {
  143. return (AE_AML_OPERAND_TYPE);
  144. }
  145. break;
  146. default:
  147. /* We cannot repair this object */
  148. return (AE_AML_OPERAND_TYPE);
  149. }
  150. /* Object was successfully repaired */
  151. /*
  152. * If the original object is a package element, we need to:
  153. * 1. Set the reference count of the new object to match the
  154. * reference count of the old object.
  155. * 2. Decrement the reference count of the original object.
  156. */
  157. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  158. new_object->common.reference_count =
  159. return_object->common.reference_count;
  160. if (return_object->common.reference_count > 1) {
  161. return_object->common.reference_count--;
  162. }
  163. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  164. "Converted %s to expected %s at index %u",
  165. acpi_ut_get_object_type_name
  166. (return_object),
  167. acpi_ut_get_object_type_name(new_object),
  168. package_index));
  169. } else {
  170. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  171. "Converted %s to expected %s",
  172. acpi_ut_get_object_type_name
  173. (return_object),
  174. acpi_ut_get_object_type_name
  175. (new_object)));
  176. }
  177. /* Delete old object, install the new return object */
  178. acpi_ut_remove_reference(return_object);
  179. *return_object_ptr = new_object;
  180. data->flags |= ACPI_OBJECT_REPAIRED;
  181. return (AE_OK);
  182. }
  183. /*******************************************************************************
  184. *
  185. * FUNCTION: acpi_ns_repair_package_list
  186. *
  187. * PARAMETERS: Data - Pointer to validation data structure
  188. * obj_desc_ptr - Pointer to the object to repair. The new
  189. * package object is returned here,
  190. * overwriting the old object.
  191. *
  192. * RETURN: Status, new object in *obj_desc_ptr
  193. *
  194. * DESCRIPTION: Repair a common problem with objects that are defined to return
  195. * a variable-length Package of Packages. If the variable-length
  196. * is one, some BIOS code mistakenly simply declares a single
  197. * Package instead of a Package with one sub-Package. This
  198. * function attempts to repair this error by wrapping a Package
  199. * object around the original Package, creating the correct
  200. * Package with one sub-Package.
  201. *
  202. * Names that can be repaired in this manner include:
  203. * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
  204. *
  205. ******************************************************************************/
  206. acpi_status
  207. acpi_ns_repair_package_list(struct acpi_predefined_data *data,
  208. union acpi_operand_object **obj_desc_ptr)
  209. {
  210. union acpi_operand_object *pkg_obj_desc;
  211. /*
  212. * Create the new outer package and populate it. The new package will
  213. * have a single element, the lone subpackage.
  214. */
  215. pkg_obj_desc = acpi_ut_create_package_object(1);
  216. if (!pkg_obj_desc) {
  217. return (AE_NO_MEMORY);
  218. }
  219. pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
  220. /* Return the new object in the object pointer */
  221. *obj_desc_ptr = pkg_obj_desc;
  222. data->flags |= ACPI_OBJECT_REPAIRED;
  223. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  224. "Repaired Incorrectly formed Package"));
  225. return (AE_OK);
  226. }