nspredef.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /******************************************************************************
  2. *
  3. * Module Name: nspredef - Validation of ACPI predefined methods and objects
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, 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. #define ACPI_CREATE_PREDEFINED_TABLE
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #include "acpredef.h"
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME("nspredef")
  49. /*******************************************************************************
  50. *
  51. * This module validates predefined ACPI objects that appear in the namespace,
  52. * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
  53. * validation is to detect problems with BIOS-exposed predefined ACPI objects
  54. * before the results are returned to the ACPI-related drivers.
  55. *
  56. * There are several areas that are validated:
  57. *
  58. * 1) The number of input arguments as defined by the method/object in the
  59. * ASL is validated against the ACPI specification.
  60. * 2) The type of the return object (if any) is validated against the ACPI
  61. * specification.
  62. * 3) For returned package objects, the count of package elements is
  63. * validated, as well as the type of each package element. Nested
  64. * packages are supported.
  65. *
  66. * For any problems found, a warning message is issued.
  67. *
  68. ******************************************************************************/
  69. /* Local prototypes */
  70. static acpi_status
  71. acpi_ns_check_reference(struct acpi_predefined_data *data,
  72. union acpi_operand_object *return_object);
  73. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object);
  74. /*******************************************************************************
  75. *
  76. * FUNCTION: acpi_ns_check_predefined_names
  77. *
  78. * PARAMETERS: node - Namespace node for the method/object
  79. * user_param_count - Number of parameters actually passed
  80. * return_status - Status from the object evaluation
  81. * return_object_ptr - Pointer to the object returned from the
  82. * evaluation of a method or object
  83. *
  84. * RETURN: Status
  85. *
  86. * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
  87. *
  88. ******************************************************************************/
  89. acpi_status
  90. acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
  91. u32 user_param_count,
  92. acpi_status return_status,
  93. union acpi_operand_object **return_object_ptr)
  94. {
  95. acpi_status status = AE_OK;
  96. const union acpi_predefined_info *predefined;
  97. char *pathname;
  98. struct acpi_predefined_data *data;
  99. /* Match the name for this method/object against the predefined list */
  100. predefined = acpi_ut_match_predefined_method(node->name.ascii);
  101. /* Get the full pathname to the object, for use in warning messages */
  102. pathname = acpi_ns_get_external_pathname(node);
  103. if (!pathname) {
  104. return (AE_OK); /* Could not get pathname, ignore */
  105. }
  106. /*
  107. * Check that the parameter count for this method matches the ASL
  108. * definition. For predefined names, ensure that both the caller and
  109. * the method itself are in accordance with the ACPI specification.
  110. */
  111. acpi_ns_check_parameter_count(pathname, node, user_param_count,
  112. predefined);
  113. /* If not a predefined name, we cannot validate the return object */
  114. if (!predefined) {
  115. goto cleanup;
  116. }
  117. /*
  118. * If the method failed or did not actually return an object, we cannot
  119. * validate the return object
  120. */
  121. if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
  122. goto cleanup;
  123. }
  124. /*
  125. * Return value validation and possible repair.
  126. *
  127. * 1) Don't perform return value validation/repair if this feature
  128. * has been disabled via a global option.
  129. *
  130. * 2) We have a return value, but if one wasn't expected, just exit,
  131. * this is not a problem. For example, if the "Implicit Return"
  132. * feature is enabled, methods will always return a value.
  133. *
  134. * 3) If the return value can be of any type, then we cannot perform
  135. * any validation, just exit.
  136. */
  137. if (acpi_gbl_disable_auto_repair ||
  138. (!predefined->info.expected_btypes) ||
  139. (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
  140. goto cleanup;
  141. }
  142. /* Create the parameter data block for object validation */
  143. data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
  144. if (!data) {
  145. goto cleanup;
  146. }
  147. data->predefined = predefined;
  148. data->node = node;
  149. data->node_flags = node->flags;
  150. data->pathname = pathname;
  151. /*
  152. * Check that the type of the main return object is what is expected
  153. * for this predefined name
  154. */
  155. status = acpi_ns_check_object_type(data, return_object_ptr,
  156. predefined->info.expected_btypes,
  157. ACPI_NOT_PACKAGE_ELEMENT);
  158. if (ACPI_FAILURE(status)) {
  159. goto exit;
  160. }
  161. /*
  162. * For returned Package objects, check the type of all sub-objects.
  163. * Note: Package may have been newly created by call above.
  164. */
  165. if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
  166. data->parent_package = *return_object_ptr;
  167. status = acpi_ns_check_package(data, return_object_ptr);
  168. if (ACPI_FAILURE(status)) {
  169. goto exit;
  170. }
  171. }
  172. /*
  173. * The return object was OK, or it was successfully repaired above.
  174. * Now make some additional checks such as verifying that package
  175. * objects are sorted correctly (if required) or buffer objects have
  176. * the correct data width (bytes vs. dwords). These repairs are
  177. * performed on a per-name basis, i.e., the code is specific to
  178. * particular predefined names.
  179. */
  180. status = acpi_ns_complex_repairs(data, node, status, return_object_ptr);
  181. exit:
  182. /*
  183. * If the object validation failed or if we successfully repaired one
  184. * or more objects, mark the parent node to suppress further warning
  185. * messages during the next evaluation of the same method/object.
  186. */
  187. if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
  188. node->flags |= ANOBJ_EVALUATED;
  189. }
  190. ACPI_FREE(data);
  191. cleanup:
  192. ACPI_FREE(pathname);
  193. return (status);
  194. }
  195. /*******************************************************************************
  196. *
  197. * FUNCTION: acpi_ns_check_parameter_count
  198. *
  199. * PARAMETERS: pathname - Full pathname to the node (for error msgs)
  200. * node - Namespace node for the method/object
  201. * user_param_count - Number of args passed in by the caller
  202. * predefined - Pointer to entry in predefined name table
  203. *
  204. * RETURN: None
  205. *
  206. * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
  207. * predefined name is what is expected (i.e., what is defined in
  208. * the ACPI specification for this predefined name.)
  209. *
  210. ******************************************************************************/
  211. void
  212. acpi_ns_check_parameter_count(char *pathname,
  213. struct acpi_namespace_node *node,
  214. u32 user_param_count,
  215. const union acpi_predefined_info *predefined)
  216. {
  217. u32 param_count;
  218. u32 required_params_current;
  219. u32 required_params_old;
  220. /* Methods have 0-7 parameters. All other types have zero. */
  221. param_count = 0;
  222. if (node->type == ACPI_TYPE_METHOD) {
  223. param_count = node->object->method.param_count;
  224. }
  225. if (!predefined) {
  226. /*
  227. * Check the parameter count for non-predefined methods/objects.
  228. *
  229. * Warning if too few or too many arguments have been passed by the
  230. * caller. An incorrect number of arguments may not cause the method
  231. * to fail. However, the method will fail if there are too few
  232. * arguments and the method attempts to use one of the missing ones.
  233. */
  234. if (user_param_count < param_count) {
  235. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  236. ACPI_WARN_ALWAYS,
  237. "Insufficient arguments - needs %u, found %u",
  238. param_count, user_param_count));
  239. } else if (user_param_count > param_count) {
  240. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  241. ACPI_WARN_ALWAYS,
  242. "Excess arguments - needs %u, found %u",
  243. param_count, user_param_count));
  244. }
  245. return;
  246. }
  247. /*
  248. * Validate the user-supplied parameter count.
  249. * Allow two different legal argument counts (_SCP, etc.)
  250. */
  251. required_params_current =
  252. predefined->info.argument_list & METHOD_ARG_MASK;
  253. required_params_old =
  254. predefined->info.argument_list >> METHOD_ARG_BIT_WIDTH;
  255. if (user_param_count != ACPI_UINT32_MAX) {
  256. if ((user_param_count != required_params_current) &&
  257. (user_param_count != required_params_old)) {
  258. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  259. ACPI_WARN_ALWAYS,
  260. "Parameter count mismatch - "
  261. "caller passed %u, ACPI requires %u",
  262. user_param_count,
  263. required_params_current));
  264. }
  265. }
  266. /*
  267. * Check that the ASL-defined parameter count is what is expected for
  268. * this predefined name (parameter count as defined by the ACPI
  269. * specification)
  270. */
  271. if ((param_count != required_params_current) &&
  272. (param_count != required_params_old)) {
  273. ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
  274. "Parameter count mismatch - ASL declared %u, ACPI requires %u",
  275. param_count, required_params_current));
  276. }
  277. }
  278. /*******************************************************************************
  279. *
  280. * FUNCTION: acpi_ns_check_object_type
  281. *
  282. * PARAMETERS: data - Pointer to validation data structure
  283. * return_object_ptr - Pointer to the object returned from the
  284. * evaluation of a method or object
  285. * expected_btypes - Bitmap of expected return type(s)
  286. * package_index - Index of object within parent package (if
  287. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  288. * otherwise)
  289. *
  290. * RETURN: Status
  291. *
  292. * DESCRIPTION: Check the type of the return object against the expected object
  293. * type(s). Use of Btype allows multiple expected object types.
  294. *
  295. ******************************************************************************/
  296. acpi_status
  297. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  298. union acpi_operand_object **return_object_ptr,
  299. u32 expected_btypes, u32 package_index)
  300. {
  301. union acpi_operand_object *return_object = *return_object_ptr;
  302. acpi_status status = AE_OK;
  303. char type_buffer[48]; /* Room for 5 types */
  304. /* A Namespace node should not get here, but make sure */
  305. if (return_object &&
  306. ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  307. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  308. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  309. return_object->node.name.ascii,
  310. acpi_ut_get_type_name(return_object->node.
  311. type)));
  312. return (AE_AML_OPERAND_TYPE);
  313. }
  314. /*
  315. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  316. * The bitmapped type allows multiple possible return types.
  317. *
  318. * Note, the cases below must handle all of the possible types returned
  319. * from all of the predefined names (including elements of returned
  320. * packages)
  321. */
  322. data->return_btype = acpi_ns_get_bitmapped_type(return_object);
  323. if (data->return_btype == ACPI_RTYPE_ANY) {
  324. /* Not one of the supported objects, must be incorrect */
  325. goto type_error_exit;
  326. }
  327. /* For reference objects, check that the reference type is correct */
  328. if ((data->return_btype & expected_btypes) == ACPI_RTYPE_REFERENCE) {
  329. status = acpi_ns_check_reference(data, return_object);
  330. return (status);
  331. }
  332. /* Attempt simple repair of the returned object if necessary */
  333. status = acpi_ns_simple_repair(data, expected_btypes,
  334. package_index, return_object_ptr);
  335. return (status);
  336. type_error_exit:
  337. /* Create a string with all expected types for this predefined object */
  338. acpi_ut_get_expected_return_types(type_buffer, expected_btypes);
  339. if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  340. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  341. "Return type mismatch - found %s, expected %s",
  342. acpi_ut_get_object_type_name
  343. (return_object), type_buffer));
  344. } else {
  345. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  346. "Return Package type mismatch at index %u - "
  347. "found %s, expected %s", package_index,
  348. acpi_ut_get_object_type_name
  349. (return_object), type_buffer));
  350. }
  351. return (AE_AML_OPERAND_TYPE);
  352. }
  353. /*******************************************************************************
  354. *
  355. * FUNCTION: acpi_ns_check_reference
  356. *
  357. * PARAMETERS: data - Pointer to validation data structure
  358. * return_object - Object returned from the evaluation of a
  359. * method or object
  360. *
  361. * RETURN: Status
  362. *
  363. * DESCRIPTION: Check a returned reference object for the correct reference
  364. * type. The only reference type that can be returned from a
  365. * predefined method is a named reference. All others are invalid.
  366. *
  367. ******************************************************************************/
  368. static acpi_status
  369. acpi_ns_check_reference(struct acpi_predefined_data *data,
  370. union acpi_operand_object *return_object)
  371. {
  372. /*
  373. * Check the reference object for the correct reference type (opcode).
  374. * The only type of reference that can be converted to an union acpi_object is
  375. * a reference to a named object (reference class: NAME)
  376. */
  377. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  378. return (AE_OK);
  379. }
  380. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  381. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  382. acpi_ut_get_reference_name(return_object),
  383. return_object->reference.class));
  384. return (AE_AML_OPERAND_TYPE);
  385. }
  386. /*******************************************************************************
  387. *
  388. * FUNCTION: acpi_ns_get_bitmapped_type
  389. *
  390. * PARAMETERS: return_object - Object returned from method/obj evaluation
  391. *
  392. * RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object
  393. * type is not supported. ACPI_RTYPE_NONE indicates that no
  394. * object was returned (return_object is NULL).
  395. *
  396. * DESCRIPTION: Convert object type into a bitmapped object return type.
  397. *
  398. ******************************************************************************/
  399. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object)
  400. {
  401. u32 return_btype;
  402. if (!return_object) {
  403. return (ACPI_RTYPE_NONE);
  404. }
  405. /* Map acpi_object_type to internal bitmapped type */
  406. switch (return_object->common.type) {
  407. case ACPI_TYPE_INTEGER:
  408. return_btype = ACPI_RTYPE_INTEGER;
  409. break;
  410. case ACPI_TYPE_BUFFER:
  411. return_btype = ACPI_RTYPE_BUFFER;
  412. break;
  413. case ACPI_TYPE_STRING:
  414. return_btype = ACPI_RTYPE_STRING;
  415. break;
  416. case ACPI_TYPE_PACKAGE:
  417. return_btype = ACPI_RTYPE_PACKAGE;
  418. break;
  419. case ACPI_TYPE_LOCAL_REFERENCE:
  420. return_btype = ACPI_RTYPE_REFERENCE;
  421. break;
  422. default:
  423. /* Not one of the supported objects, must be incorrect */
  424. return_btype = ACPI_RTYPE_ANY;
  425. break;
  426. }
  427. return (return_btype);
  428. }