nspredef.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes);
  74. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object);
  75. /*
  76. * Names for the types that can be returned by the predefined objects.
  77. * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
  78. */
  79. static const char *acpi_rtype_names[] = {
  80. "/Integer",
  81. "/String",
  82. "/Buffer",
  83. "/Package",
  84. "/Reference",
  85. };
  86. /*******************************************************************************
  87. *
  88. * FUNCTION: acpi_ns_check_predefined_names
  89. *
  90. * PARAMETERS: node - Namespace node for the method/object
  91. * user_param_count - Number of parameters actually passed
  92. * return_status - Status from the object evaluation
  93. * return_object_ptr - Pointer to the object returned from the
  94. * evaluation of a method or object
  95. *
  96. * RETURN: Status
  97. *
  98. * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
  99. *
  100. ******************************************************************************/
  101. acpi_status
  102. acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
  103. u32 user_param_count,
  104. acpi_status return_status,
  105. union acpi_operand_object **return_object_ptr)
  106. {
  107. acpi_status status = AE_OK;
  108. const union acpi_predefined_info *predefined;
  109. char *pathname;
  110. struct acpi_predefined_data *data;
  111. /* Match the name for this method/object against the predefined list */
  112. predefined = acpi_ns_check_for_predefined_name(node);
  113. /* Get the full pathname to the object, for use in warning messages */
  114. pathname = acpi_ns_get_external_pathname(node);
  115. if (!pathname) {
  116. return (AE_OK); /* Could not get pathname, ignore */
  117. }
  118. /*
  119. * Check that the parameter count for this method matches the ASL
  120. * definition. For predefined names, ensure that both the caller and
  121. * the method itself are in accordance with the ACPI specification.
  122. */
  123. acpi_ns_check_parameter_count(pathname, node, user_param_count,
  124. predefined);
  125. /* If not a predefined name, we cannot validate the return object */
  126. if (!predefined) {
  127. goto cleanup;
  128. }
  129. /*
  130. * If the method failed or did not actually return an object, we cannot
  131. * validate the return object
  132. */
  133. if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
  134. goto cleanup;
  135. }
  136. /*
  137. * Return value validation and possible repair.
  138. *
  139. * 1) Don't perform return value validation/repair if this feature
  140. * has been disabled via a global option.
  141. *
  142. * 2) We have a return value, but if one wasn't expected, just exit,
  143. * this is not a problem. For example, if the "Implicit Return"
  144. * feature is enabled, methods will always return a value.
  145. *
  146. * 3) If the return value can be of any type, then we cannot perform
  147. * any validation, just exit.
  148. */
  149. if (acpi_gbl_disable_auto_repair ||
  150. (!predefined->info.expected_btypes) ||
  151. (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
  152. goto cleanup;
  153. }
  154. /* Create the parameter data block for object validation */
  155. data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
  156. if (!data) {
  157. goto cleanup;
  158. }
  159. data->predefined = predefined;
  160. data->node = node;
  161. data->node_flags = node->flags;
  162. data->pathname = pathname;
  163. /*
  164. * Check that the type of the main return object is what is expected
  165. * for this predefined name
  166. */
  167. status = acpi_ns_check_object_type(data, return_object_ptr,
  168. predefined->info.expected_btypes,
  169. ACPI_NOT_PACKAGE_ELEMENT);
  170. if (ACPI_FAILURE(status)) {
  171. goto exit;
  172. }
  173. /*
  174. * For returned Package objects, check the type of all sub-objects.
  175. * Note: Package may have been newly created by call above.
  176. */
  177. if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
  178. data->parent_package = *return_object_ptr;
  179. status = acpi_ns_check_package(data, return_object_ptr);
  180. if (ACPI_FAILURE(status)) {
  181. goto exit;
  182. }
  183. }
  184. /*
  185. * The return object was OK, or it was successfully repaired above.
  186. * Now make some additional checks such as verifying that package
  187. * objects are sorted correctly (if required) or buffer objects have
  188. * the correct data width (bytes vs. dwords). These repairs are
  189. * performed on a per-name basis, i.e., the code is specific to
  190. * particular predefined names.
  191. */
  192. status = acpi_ns_complex_repairs(data, node, status, return_object_ptr);
  193. exit:
  194. /*
  195. * If the object validation failed or if we successfully repaired one
  196. * or more objects, mark the parent node to suppress further warning
  197. * messages during the next evaluation of the same method/object.
  198. */
  199. if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
  200. node->flags |= ANOBJ_EVALUATED;
  201. }
  202. ACPI_FREE(data);
  203. cleanup:
  204. ACPI_FREE(pathname);
  205. return (status);
  206. }
  207. /*******************************************************************************
  208. *
  209. * FUNCTION: acpi_ns_check_parameter_count
  210. *
  211. * PARAMETERS: pathname - Full pathname to the node (for error msgs)
  212. * node - Namespace node for the method/object
  213. * user_param_count - Number of args passed in by the caller
  214. * predefined - Pointer to entry in predefined name table
  215. *
  216. * RETURN: None
  217. *
  218. * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
  219. * predefined name is what is expected (i.e., what is defined in
  220. * the ACPI specification for this predefined name.)
  221. *
  222. ******************************************************************************/
  223. void
  224. acpi_ns_check_parameter_count(char *pathname,
  225. struct acpi_namespace_node *node,
  226. u32 user_param_count,
  227. const union acpi_predefined_info *predefined)
  228. {
  229. u32 param_count;
  230. u32 required_params_current;
  231. u32 required_params_old;
  232. /* Methods have 0-7 parameters. All other types have zero. */
  233. param_count = 0;
  234. if (node->type == ACPI_TYPE_METHOD) {
  235. param_count = node->object->method.param_count;
  236. }
  237. if (!predefined) {
  238. /*
  239. * Check the parameter count for non-predefined methods/objects.
  240. *
  241. * Warning if too few or too many arguments have been passed by the
  242. * caller. An incorrect number of arguments may not cause the method
  243. * to fail. However, the method will fail if there are too few
  244. * arguments and the method attempts to use one of the missing ones.
  245. */
  246. if (user_param_count < param_count) {
  247. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  248. ACPI_WARN_ALWAYS,
  249. "Insufficient arguments - needs %u, found %u",
  250. param_count, user_param_count));
  251. } else if (user_param_count > param_count) {
  252. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  253. ACPI_WARN_ALWAYS,
  254. "Excess arguments - needs %u, found %u",
  255. param_count, user_param_count));
  256. }
  257. return;
  258. }
  259. /*
  260. * Validate the user-supplied parameter count.
  261. * Allow two different legal argument counts (_SCP, etc.)
  262. */
  263. required_params_current = predefined->info.param_count & 0x0F;
  264. required_params_old = predefined->info.param_count >> 4;
  265. if (user_param_count != ACPI_UINT32_MAX) {
  266. if ((user_param_count != required_params_current) &&
  267. (user_param_count != required_params_old)) {
  268. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  269. ACPI_WARN_ALWAYS,
  270. "Parameter count mismatch - "
  271. "caller passed %u, ACPI requires %u",
  272. user_param_count,
  273. required_params_current));
  274. }
  275. }
  276. /*
  277. * Check that the ASL-defined parameter count is what is expected for
  278. * this predefined name (parameter count as defined by the ACPI
  279. * specification)
  280. */
  281. if ((param_count != required_params_current) &&
  282. (param_count != required_params_old)) {
  283. ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
  284. "Parameter count mismatch - ASL declared %u, ACPI requires %u",
  285. param_count, required_params_current));
  286. }
  287. }
  288. /*******************************************************************************
  289. *
  290. * FUNCTION: acpi_ns_check_for_predefined_name
  291. *
  292. * PARAMETERS: node - Namespace node for the method/object
  293. *
  294. * RETURN: Pointer to entry in predefined table. NULL indicates not found.
  295. *
  296. * DESCRIPTION: Check an object name against the predefined object list.
  297. *
  298. ******************************************************************************/
  299. const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
  300. acpi_namespace_node
  301. *node)
  302. {
  303. const union acpi_predefined_info *this_name;
  304. /* Quick check for a predefined name, first character must be underscore */
  305. if (node->name.ascii[0] != '_') {
  306. return (NULL);
  307. }
  308. /* Search info table for a predefined method/object name */
  309. this_name = predefined_names;
  310. while (this_name->info.name[0]) {
  311. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
  312. return (this_name);
  313. }
  314. /*
  315. * Skip next entry in the table if this name returns a Package
  316. * (next entry contains the package info)
  317. */
  318. if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
  319. this_name++;
  320. }
  321. this_name++;
  322. }
  323. return (NULL); /* Not found */
  324. }
  325. /*******************************************************************************
  326. *
  327. * FUNCTION: acpi_ns_check_object_type
  328. *
  329. * PARAMETERS: data - Pointer to validation data structure
  330. * return_object_ptr - Pointer to the object returned from the
  331. * evaluation of a method or object
  332. * expected_btypes - Bitmap of expected return type(s)
  333. * package_index - Index of object within parent package (if
  334. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  335. * otherwise)
  336. *
  337. * RETURN: Status
  338. *
  339. * DESCRIPTION: Check the type of the return object against the expected object
  340. * type(s). Use of Btype allows multiple expected object types.
  341. *
  342. ******************************************************************************/
  343. acpi_status
  344. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  345. union acpi_operand_object **return_object_ptr,
  346. u32 expected_btypes, u32 package_index)
  347. {
  348. union acpi_operand_object *return_object = *return_object_ptr;
  349. acpi_status status = AE_OK;
  350. char type_buffer[48]; /* Room for 5 types */
  351. /* A Namespace node should not get here, but make sure */
  352. if (return_object &&
  353. ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  354. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  355. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  356. return_object->node.name.ascii,
  357. acpi_ut_get_type_name(return_object->node.
  358. type)));
  359. return (AE_AML_OPERAND_TYPE);
  360. }
  361. /*
  362. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  363. * The bitmapped type allows multiple possible return types.
  364. *
  365. * Note, the cases below must handle all of the possible types returned
  366. * from all of the predefined names (including elements of returned
  367. * packages)
  368. */
  369. data->return_btype = acpi_ns_get_bitmapped_type(return_object);
  370. if (data->return_btype == ACPI_RTYPE_ANY) {
  371. /* Not one of the supported objects, must be incorrect */
  372. goto type_error_exit;
  373. }
  374. /* For reference objects, check that the reference type is correct */
  375. if ((data->return_btype & expected_btypes) == ACPI_RTYPE_REFERENCE) {
  376. status = acpi_ns_check_reference(data, return_object);
  377. return (status);
  378. }
  379. /* Attempt simple repair of the returned object if necessary */
  380. status = acpi_ns_simple_repair(data, expected_btypes,
  381. package_index, return_object_ptr);
  382. return (status);
  383. type_error_exit:
  384. /* Create a string with all expected types for this predefined object */
  385. acpi_ns_get_expected_types(type_buffer, expected_btypes);
  386. if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  387. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  388. "Return type mismatch - found %s, expected %s",
  389. acpi_ut_get_object_type_name
  390. (return_object), type_buffer));
  391. } else {
  392. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  393. "Return Package type mismatch at index %u - "
  394. "found %s, expected %s", package_index,
  395. acpi_ut_get_object_type_name
  396. (return_object), type_buffer));
  397. }
  398. return (AE_AML_OPERAND_TYPE);
  399. }
  400. /*******************************************************************************
  401. *
  402. * FUNCTION: acpi_ns_check_reference
  403. *
  404. * PARAMETERS: data - Pointer to validation data structure
  405. * return_object - Object returned from the evaluation of a
  406. * method or object
  407. *
  408. * RETURN: Status
  409. *
  410. * DESCRIPTION: Check a returned reference object for the correct reference
  411. * type. The only reference type that can be returned from a
  412. * predefined method is a named reference. All others are invalid.
  413. *
  414. ******************************************************************************/
  415. static acpi_status
  416. acpi_ns_check_reference(struct acpi_predefined_data *data,
  417. union acpi_operand_object *return_object)
  418. {
  419. /*
  420. * Check the reference object for the correct reference type (opcode).
  421. * The only type of reference that can be converted to an union acpi_object is
  422. * a reference to a named object (reference class: NAME)
  423. */
  424. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  425. return (AE_OK);
  426. }
  427. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  428. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  429. acpi_ut_get_reference_name(return_object),
  430. return_object->reference.class));
  431. return (AE_AML_OPERAND_TYPE);
  432. }
  433. /*******************************************************************************
  434. *
  435. * FUNCTION: acpi_ns_get_bitmapped_type
  436. *
  437. * PARAMETERS: return_object - Object returned from method/obj evaluation
  438. *
  439. * RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object
  440. * type is not supported. ACPI_RTYPE_NONE indicates that no
  441. * object was returned (return_object is NULL).
  442. *
  443. * DESCRIPTION: Convert object type into a bitmapped object return type.
  444. *
  445. ******************************************************************************/
  446. static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object)
  447. {
  448. u32 return_btype;
  449. if (!return_object) {
  450. return (ACPI_RTYPE_NONE);
  451. }
  452. /* Map acpi_object_type to internal bitmapped type */
  453. switch (return_object->common.type) {
  454. case ACPI_TYPE_INTEGER:
  455. return_btype = ACPI_RTYPE_INTEGER;
  456. break;
  457. case ACPI_TYPE_BUFFER:
  458. return_btype = ACPI_RTYPE_BUFFER;
  459. break;
  460. case ACPI_TYPE_STRING:
  461. return_btype = ACPI_RTYPE_STRING;
  462. break;
  463. case ACPI_TYPE_PACKAGE:
  464. return_btype = ACPI_RTYPE_PACKAGE;
  465. break;
  466. case ACPI_TYPE_LOCAL_REFERENCE:
  467. return_btype = ACPI_RTYPE_REFERENCE;
  468. break;
  469. default:
  470. /* Not one of the supported objects, must be incorrect */
  471. return_btype = ACPI_RTYPE_ANY;
  472. break;
  473. }
  474. return (return_btype);
  475. }
  476. /*******************************************************************************
  477. *
  478. * FUNCTION: acpi_ns_get_expected_types
  479. *
  480. * PARAMETERS: buffer - Pointer to where the string is returned
  481. * expected_btypes - Bitmap of expected return type(s)
  482. *
  483. * RETURN: Buffer is populated with type names.
  484. *
  485. * DESCRIPTION: Translate the expected types bitmap into a string of ascii
  486. * names of expected types, for use in warning messages.
  487. *
  488. ******************************************************************************/
  489. static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
  490. {
  491. u32 this_rtype;
  492. u32 i;
  493. u32 j;
  494. j = 1;
  495. buffer[0] = 0;
  496. this_rtype = ACPI_RTYPE_INTEGER;
  497. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  498. /* If one of the expected types, concatenate the name of this type */
  499. if (expected_btypes & this_rtype) {
  500. ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
  501. j = 0; /* Use name separator from now on */
  502. }
  503. this_rtype <<= 1; /* Next Rtype */
  504. }
  505. }