nspredef.c 19 KB

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