nspredef.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nspredef - Validation of ACPI predefined methods and objects
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #define ACPI_CREATE_PREDEFINED_TABLE
  10. #include <acpi/acpi.h>
  11. #include "accommon.h"
  12. #include "acnamesp.h"
  13. #include "acpredef.h"
  14. #define _COMPONENT ACPI_NAMESPACE
  15. ACPI_MODULE_NAME("nspredef")
  16. /*******************************************************************************
  17. *
  18. * This module validates predefined ACPI objects that appear in the namespace,
  19. * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
  20. * validation is to detect problems with BIOS-exposed predefined ACPI objects
  21. * before the results are returned to the ACPI-related drivers.
  22. *
  23. * There are several areas that are validated:
  24. *
  25. * 1) The number of input arguments as defined by the method/object in the
  26. * ASL is validated against the ACPI specification.
  27. * 2) The type of the return object (if any) is validated against the ACPI
  28. * specification.
  29. * 3) For returned package objects, the count of package elements is
  30. * validated, as well as the type of each package element. Nested
  31. * packages are supported.
  32. *
  33. * For any problems found, a warning message is issued.
  34. *
  35. ******************************************************************************/
  36. /* Local prototypes */
  37. static acpi_status
  38. acpi_ns_check_reference(struct acpi_evaluate_info *info,
  39. union acpi_operand_object *return_object);
  40. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object);
  41. /*******************************************************************************
  42. *
  43. * FUNCTION: acpi_ns_check_return_value
  44. *
  45. * PARAMETERS: node - Namespace node for the method/object
  46. * info - Method execution information block
  47. * user_param_count - Number of parameters actually passed
  48. * return_status - Status from the object evaluation
  49. * return_object_ptr - Pointer to the object returned from the
  50. * evaluation of a method or object
  51. *
  52. * RETURN: Status
  53. *
  54. * DESCRIPTION: Check the value returned from a predefined name.
  55. *
  56. ******************************************************************************/
  57. acpi_status
  58. acpi_ns_check_return_value(struct acpi_namespace_node *node,
  59. struct acpi_evaluate_info *info,
  60. u32 user_param_count,
  61. acpi_status return_status,
  62. union acpi_operand_object **return_object_ptr)
  63. {
  64. acpi_status status;
  65. const union acpi_predefined_info *predefined;
  66. ACPI_FUNCTION_TRACE(ns_check_return_value);
  67. /* If not a predefined name, we cannot validate the return object */
  68. predefined = info->predefined;
  69. if (!predefined) {
  70. return_ACPI_STATUS(AE_OK);
  71. }
  72. /*
  73. * If the method failed or did not actually return an object, we cannot
  74. * validate the return object
  75. */
  76. if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
  77. return_ACPI_STATUS(AE_OK);
  78. }
  79. /*
  80. * Return value validation and possible repair.
  81. *
  82. * 1) Don't perform return value validation/repair if this feature
  83. * has been disabled via a global option.
  84. *
  85. * 2) We have a return value, but if one wasn't expected, just exit,
  86. * this is not a problem. For example, if the "Implicit Return"
  87. * feature is enabled, methods will always return a value.
  88. *
  89. * 3) If the return value can be of any type, then we cannot perform
  90. * any validation, just exit.
  91. */
  92. if (acpi_gbl_disable_auto_repair ||
  93. (!predefined->info.expected_btypes) ||
  94. (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
  95. return_ACPI_STATUS(AE_OK);
  96. }
  97. /*
  98. * Check that the type of the main return object is what is expected
  99. * for this predefined name
  100. */
  101. status = acpi_ns_check_object_type(info, return_object_ptr,
  102. predefined->info.expected_btypes,
  103. ACPI_NOT_PACKAGE_ELEMENT);
  104. if (ACPI_FAILURE(status)) {
  105. goto exit;
  106. }
  107. /*
  108. *
  109. * 4) If there is no return value and it is optional, just return
  110. * AE_OK (_WAK).
  111. */
  112. if (!(*return_object_ptr)) {
  113. goto exit;
  114. }
  115. /*
  116. * For returned Package objects, check the type of all sub-objects.
  117. * Note: Package may have been newly created by call above.
  118. */
  119. if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
  120. info->parent_package = *return_object_ptr;
  121. status = acpi_ns_check_package(info, return_object_ptr);
  122. if (ACPI_FAILURE(status)) {
  123. /* We might be able to fix some errors */
  124. if ((status != AE_AML_OPERAND_TYPE) &&
  125. (status != AE_AML_OPERAND_VALUE)) {
  126. goto exit;
  127. }
  128. }
  129. }
  130. /*
  131. * The return object was OK, or it was successfully repaired above.
  132. * Now make some additional checks such as verifying that package
  133. * objects are sorted correctly (if required) or buffer objects have
  134. * the correct data width (bytes vs. dwords). These repairs are
  135. * performed on a per-name basis, i.e., the code is specific to
  136. * particular predefined names.
  137. */
  138. status = acpi_ns_complex_repairs(info, node, status, return_object_ptr);
  139. exit:
  140. /*
  141. * If the object validation failed or if we successfully repaired one
  142. * or more objects, mark the parent node to suppress further warning
  143. * messages during the next evaluation of the same method/object.
  144. */
  145. if (ACPI_FAILURE(status) || (info->return_flags & ACPI_OBJECT_REPAIRED)) {
  146. node->flags |= ANOBJ_EVALUATED;
  147. }
  148. return_ACPI_STATUS(status);
  149. }
  150. /*******************************************************************************
  151. *
  152. * FUNCTION: acpi_ns_check_object_type
  153. *
  154. * PARAMETERS: info - Method execution information block
  155. * return_object_ptr - Pointer to the object returned from the
  156. * evaluation of a method or object
  157. * expected_btypes - Bitmap of expected return type(s)
  158. * package_index - Index of object within parent package (if
  159. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  160. * otherwise)
  161. *
  162. * RETURN: Status
  163. *
  164. * DESCRIPTION: Check the type of the return object against the expected object
  165. * type(s). Use of Btype allows multiple expected object types.
  166. *
  167. ******************************************************************************/
  168. acpi_status
  169. acpi_ns_check_object_type(struct acpi_evaluate_info *info,
  170. union acpi_operand_object **return_object_ptr,
  171. u32 expected_btypes, u32 package_index)
  172. {
  173. union acpi_operand_object *return_object = *return_object_ptr;
  174. acpi_status status = AE_OK;
  175. char type_buffer[96]; /* Room for 10 types */
  176. /* A Namespace node should not get here, but make sure */
  177. if (return_object &&
  178. ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  179. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  180. info->node_flags,
  181. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  182. return_object->node.name.ascii,
  183. acpi_ut_get_type_name(return_object->node.
  184. type)));
  185. return (AE_AML_OPERAND_TYPE);
  186. }
  187. /*
  188. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  189. * The bitmapped type allows multiple possible return types.
  190. *
  191. * Note, the cases below must handle all of the possible types returned
  192. * from all of the predefined names (including elements of returned
  193. * packages)
  194. */
  195. info->return_btype = acpi_ns_get_bitmapped_type(return_object);
  196. if (info->return_btype == ACPI_RTYPE_ANY) {
  197. /* Not one of the supported objects, must be incorrect */
  198. goto type_error_exit;
  199. }
  200. /* For reference objects, check that the reference type is correct */
  201. if ((info->return_btype & expected_btypes) == ACPI_RTYPE_REFERENCE) {
  202. status = acpi_ns_check_reference(info, return_object);
  203. return (status);
  204. }
  205. /* Attempt simple repair of the returned object if necessary */
  206. status = acpi_ns_simple_repair(info, expected_btypes,
  207. package_index, return_object_ptr);
  208. if (ACPI_SUCCESS(status)) {
  209. return (AE_OK); /* Successful repair */
  210. }
  211. type_error_exit:
  212. /* Create a string with all expected types for this predefined object */
  213. acpi_ut_get_expected_return_types(type_buffer, expected_btypes);
  214. if (!return_object) {
  215. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  216. info->node_flags,
  217. "Expected return object of type %s",
  218. type_buffer));
  219. } else if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  220. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  221. info->node_flags,
  222. "Return type mismatch - found %s, expected %s",
  223. acpi_ut_get_object_type_name
  224. (return_object), type_buffer));
  225. } else {
  226. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  227. info->node_flags,
  228. "Return Package type mismatch at index %u - "
  229. "found %s, expected %s", package_index,
  230. acpi_ut_get_object_type_name
  231. (return_object), type_buffer));
  232. }
  233. return (AE_AML_OPERAND_TYPE);
  234. }
  235. /*******************************************************************************
  236. *
  237. * FUNCTION: acpi_ns_check_reference
  238. *
  239. * PARAMETERS: info - Method execution information block
  240. * return_object - Object returned from the evaluation of a
  241. * method or object
  242. *
  243. * RETURN: Status
  244. *
  245. * DESCRIPTION: Check a returned reference object for the correct reference
  246. * type. The only reference type that can be returned from a
  247. * predefined method is a named reference. All others are invalid.
  248. *
  249. ******************************************************************************/
  250. static acpi_status
  251. acpi_ns_check_reference(struct acpi_evaluate_info *info,
  252. union acpi_operand_object *return_object)
  253. {
  254. /*
  255. * Check the reference object for the correct reference type (opcode).
  256. * The only type of reference that can be converted to a union acpi_object is
  257. * a reference to a named object (reference class: NAME)
  258. */
  259. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  260. return (AE_OK);
  261. }
  262. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  263. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  264. acpi_ut_get_reference_name(return_object),
  265. return_object->reference.class));
  266. return (AE_AML_OPERAND_TYPE);
  267. }
  268. /*******************************************************************************
  269. *
  270. * FUNCTION: acpi_ns_get_bitmapped_type
  271. *
  272. * PARAMETERS: return_object - Object returned from method/obj evaluation
  273. *
  274. * RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object
  275. * type is not supported. ACPI_RTYPE_NONE indicates that no
  276. * object was returned (return_object is NULL).
  277. *
  278. * DESCRIPTION: Convert object type into a bitmapped object return type.
  279. *
  280. ******************************************************************************/
  281. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object)
  282. {
  283. u32 return_btype;
  284. if (!return_object) {
  285. return (ACPI_RTYPE_NONE);
  286. }
  287. /* Map acpi_object_type to internal bitmapped type */
  288. switch (return_object->common.type) {
  289. case ACPI_TYPE_INTEGER:
  290. return_btype = ACPI_RTYPE_INTEGER;
  291. break;
  292. case ACPI_TYPE_BUFFER:
  293. return_btype = ACPI_RTYPE_BUFFER;
  294. break;
  295. case ACPI_TYPE_STRING:
  296. return_btype = ACPI_RTYPE_STRING;
  297. break;
  298. case ACPI_TYPE_PACKAGE:
  299. return_btype = ACPI_RTYPE_PACKAGE;
  300. break;
  301. case ACPI_TYPE_LOCAL_REFERENCE:
  302. return_btype = ACPI_RTYPE_REFERENCE;
  303. break;
  304. default:
  305. /* Not one of the supported objects, must be incorrect */
  306. return_btype = ACPI_RTYPE_ANY;
  307. break;
  308. }
  309. return (return_btype);
  310. }