nspredef.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /******************************************************************************
  2. *
  3. * Module Name: nspredef - Validation of ACPI predefined methods and objects
  4. * $Revision: 1.1 $
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2008, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #define ACPI_CREATE_PREDEFINED_TABLE
  44. #include <acpi/acpi.h>
  45. #include "accommon.h"
  46. #include "acnamesp.h"
  47. #include "acpredef.h"
  48. #define _COMPONENT ACPI_NAMESPACE
  49. ACPI_MODULE_NAME("nspredef")
  50. /*******************************************************************************
  51. *
  52. * This module validates predefined ACPI objects that appear in the namespace,
  53. * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
  54. * validation is to detect problems with BIOS-exposed predefined ACPI objects
  55. * before the results are returned to the ACPI-related drivers.
  56. *
  57. * There are several areas that are validated:
  58. *
  59. * 1) The number of input arguments as defined by the method/object in the
  60. * ASL is validated against the ACPI specification.
  61. * 2) The type of the return object (if any) is validated against the ACPI
  62. * specification.
  63. * 3) For returned package objects, the count of package elements is
  64. * validated, as well as the type of each package element. Nested
  65. * packages are supported.
  66. *
  67. * For any problems found, a warning message is issued.
  68. *
  69. ******************************************************************************/
  70. /* Local prototypes */
  71. static acpi_status
  72. acpi_ns_check_package(struct acpi_predefined_data *data,
  73. union acpi_operand_object **return_object_ptr);
  74. static acpi_status
  75. acpi_ns_check_package_list(struct acpi_predefined_data *data,
  76. const union acpi_predefined_info *package,
  77. union acpi_operand_object **elements, u32 count);
  78. static acpi_status
  79. acpi_ns_check_package_elements(struct acpi_predefined_data *data,
  80. union acpi_operand_object **elements,
  81. u8 type1,
  82. u32 count1,
  83. u8 type2, u32 count2, u32 start_index);
  84. static acpi_status
  85. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  86. union acpi_operand_object **return_object_ptr,
  87. u32 expected_btypes, u32 package_index);
  88. static acpi_status
  89. acpi_ns_check_reference(struct acpi_predefined_data *data,
  90. union acpi_operand_object *return_object);
  91. static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes);
  92. /*
  93. * Names for the types that can be returned by the predefined objects.
  94. * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
  95. */
  96. static const char *acpi_rtype_names[] = {
  97. "/Integer",
  98. "/String",
  99. "/Buffer",
  100. "/Package",
  101. "/Reference",
  102. };
  103. /*******************************************************************************
  104. *
  105. * FUNCTION: acpi_ns_check_predefined_names
  106. *
  107. * PARAMETERS: Node - Namespace node for the method/object
  108. * user_param_count - Number of parameters actually passed
  109. * return_status - Status from the object evaluation
  110. * return_object_ptr - Pointer to the object returned from the
  111. * evaluation of a method or object
  112. *
  113. * RETURN: Status
  114. *
  115. * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
  116. *
  117. ******************************************************************************/
  118. acpi_status
  119. acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
  120. u32 user_param_count,
  121. acpi_status return_status,
  122. union acpi_operand_object **return_object_ptr)
  123. {
  124. union acpi_operand_object *return_object = *return_object_ptr;
  125. acpi_status status = AE_OK;
  126. const union acpi_predefined_info *predefined;
  127. char *pathname;
  128. struct acpi_predefined_data *data;
  129. /* Match the name for this method/object against the predefined list */
  130. predefined = acpi_ns_check_for_predefined_name(node);
  131. /* Get the full pathname to the object, for use in warning messages */
  132. pathname = acpi_ns_get_external_pathname(node);
  133. if (!pathname) {
  134. return AE_OK; /* Could not get pathname, ignore */
  135. }
  136. /*
  137. * Check that the parameter count for this method matches the ASL
  138. * definition. For predefined names, ensure that both the caller and
  139. * the method itself are in accordance with the ACPI specification.
  140. */
  141. acpi_ns_check_parameter_count(pathname, node, user_param_count,
  142. predefined);
  143. /* If not a predefined name, we cannot validate the return object */
  144. if (!predefined) {
  145. goto cleanup;
  146. }
  147. /*
  148. * If the method failed or did not actually return an object, we cannot
  149. * validate the return object
  150. */
  151. if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
  152. goto cleanup;
  153. }
  154. /*
  155. * If there is no return value, check if we require a return value for
  156. * this predefined name. Either one return value is expected, or none,
  157. * for both methods and other objects.
  158. *
  159. * Exit now if there is no return object. Warning if one was expected.
  160. */
  161. if (!return_object) {
  162. if ((predefined->info.expected_btypes) &&
  163. (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
  164. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  165. ACPI_WARN_ALWAYS,
  166. "Missing expected return value"));
  167. status = AE_AML_NO_RETURN_VALUE;
  168. }
  169. goto cleanup;
  170. }
  171. /*
  172. * We have a return value, but if one wasn't expected, just exit, this is
  173. * not a problem. For example, if the "Implicit Return" feature is
  174. * enabled, methods will always return a value.
  175. */
  176. if (!predefined->info.expected_btypes) {
  177. goto cleanup;
  178. }
  179. /* Create the parameter data block for object validation */
  180. data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
  181. if (!data) {
  182. goto cleanup;
  183. }
  184. data->predefined = predefined;
  185. data->node_flags = node->flags;
  186. data->pathname = pathname;
  187. /*
  188. * Check that the type of the return object is what is expected for
  189. * this predefined name
  190. */
  191. status = acpi_ns_check_object_type(data, return_object_ptr,
  192. predefined->info.expected_btypes,
  193. ACPI_NOT_PACKAGE_ELEMENT);
  194. if (ACPI_FAILURE(status)) {
  195. goto check_validation_status;
  196. }
  197. /* For returned Package objects, check the type of all sub-objects */
  198. if (return_object->common.type == ACPI_TYPE_PACKAGE) {
  199. status = acpi_ns_check_package(data, return_object_ptr);
  200. }
  201. check_validation_status:
  202. /*
  203. * If the object validation failed or if we successfully repaired one
  204. * or more objects, mark the parent node to suppress further warning
  205. * messages during the next evaluation of the same method/object.
  206. */
  207. if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
  208. node->flags |= ANOBJ_EVALUATED;
  209. }
  210. ACPI_FREE(data);
  211. cleanup:
  212. ACPI_FREE(pathname);
  213. return (status);
  214. }
  215. /*******************************************************************************
  216. *
  217. * FUNCTION: acpi_ns_check_parameter_count
  218. *
  219. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  220. * Node - Namespace node for the method/object
  221. * user_param_count - Number of args passed in by the caller
  222. * Predefined - Pointer to entry in predefined name table
  223. *
  224. * RETURN: None
  225. *
  226. * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
  227. * predefined name is what is expected (i.e., what is defined in
  228. * the ACPI specification for this predefined name.)
  229. *
  230. ******************************************************************************/
  231. void
  232. acpi_ns_check_parameter_count(char *pathname,
  233. struct acpi_namespace_node *node,
  234. u32 user_param_count,
  235. const union acpi_predefined_info *predefined)
  236. {
  237. u32 param_count;
  238. u32 required_params_current;
  239. u32 required_params_old;
  240. /* Methods have 0-7 parameters. All other types have zero. */
  241. param_count = 0;
  242. if (node->type == ACPI_TYPE_METHOD) {
  243. param_count = node->object->method.param_count;
  244. }
  245. if (!predefined) {
  246. /*
  247. * Check the parameter count for non-predefined methods/objects.
  248. *
  249. * Warning if too few or too many arguments have been passed by the
  250. * caller. An incorrect number of arguments may not cause the method
  251. * to fail. However, the method will fail if there are too few
  252. * arguments and the method attempts to use one of the missing ones.
  253. */
  254. if (user_param_count < param_count) {
  255. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  256. ACPI_WARN_ALWAYS,
  257. "Insufficient arguments - needs %u, found %u",
  258. param_count, user_param_count));
  259. } else if (user_param_count > param_count) {
  260. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  261. ACPI_WARN_ALWAYS,
  262. "Excess arguments - needs %u, found %u",
  263. param_count, user_param_count));
  264. }
  265. return;
  266. }
  267. /*
  268. * Validate the user-supplied parameter count.
  269. * Allow two different legal argument counts (_SCP, etc.)
  270. */
  271. required_params_current = predefined->info.param_count & 0x0F;
  272. required_params_old = predefined->info.param_count >> 4;
  273. if (user_param_count != ACPI_UINT32_MAX) {
  274. if ((user_param_count != required_params_current) &&
  275. (user_param_count != required_params_old)) {
  276. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  277. ACPI_WARN_ALWAYS,
  278. "Parameter count mismatch - "
  279. "caller passed %u, ACPI requires %u",
  280. user_param_count,
  281. required_params_current));
  282. }
  283. }
  284. /*
  285. * Check that the ASL-defined parameter count is what is expected for
  286. * this predefined name (parameter count as defined by the ACPI
  287. * specification)
  288. */
  289. if ((param_count != required_params_current) &&
  290. (param_count != required_params_old)) {
  291. ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
  292. "Parameter count mismatch - ASL declared %u, ACPI requires %u",
  293. param_count, required_params_current));
  294. }
  295. }
  296. /*******************************************************************************
  297. *
  298. * FUNCTION: acpi_ns_check_for_predefined_name
  299. *
  300. * PARAMETERS: Node - Namespace node for the method/object
  301. *
  302. * RETURN: Pointer to entry in predefined table. NULL indicates not found.
  303. *
  304. * DESCRIPTION: Check an object name against the predefined object list.
  305. *
  306. ******************************************************************************/
  307. const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
  308. acpi_namespace_node
  309. *node)
  310. {
  311. const union acpi_predefined_info *this_name;
  312. /* Quick check for a predefined name, first character must be underscore */
  313. if (node->name.ascii[0] != '_') {
  314. return (NULL);
  315. }
  316. /* Search info table for a predefined method/object name */
  317. this_name = predefined_names;
  318. while (this_name->info.name[0]) {
  319. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
  320. return (this_name);
  321. }
  322. /*
  323. * Skip next entry in the table if this name returns a Package
  324. * (next entry contains the package info)
  325. */
  326. if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
  327. this_name++;
  328. }
  329. this_name++;
  330. }
  331. return (NULL); /* Not found */
  332. }
  333. /*******************************************************************************
  334. *
  335. * FUNCTION: acpi_ns_check_package
  336. *
  337. * PARAMETERS: Data - Pointer to validation data structure
  338. * return_object_ptr - Pointer to the object returned from the
  339. * evaluation of a method or object
  340. *
  341. * RETURN: Status
  342. *
  343. * DESCRIPTION: Check a returned package object for the correct count and
  344. * correct type of all sub-objects.
  345. *
  346. ******************************************************************************/
  347. static acpi_status
  348. acpi_ns_check_package(struct acpi_predefined_data *data,
  349. union acpi_operand_object **return_object_ptr)
  350. {
  351. union acpi_operand_object *return_object = *return_object_ptr;
  352. const union acpi_predefined_info *package;
  353. union acpi_operand_object **elements;
  354. acpi_status status = AE_OK;
  355. u32 expected_count;
  356. u32 count;
  357. u32 i;
  358. ACPI_FUNCTION_NAME(ns_check_package);
  359. /* The package info for this name is in the next table entry */
  360. package = data->predefined + 1;
  361. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  362. "%s Validating return Package of Type %X, Count %X\n",
  363. data->pathname, package->ret_info.type,
  364. return_object->package.count));
  365. /* Extract package count and elements array */
  366. elements = return_object->package.elements;
  367. count = return_object->package.count;
  368. /* The package must have at least one element, else invalid */
  369. if (!count) {
  370. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  371. "Return Package has no elements (empty)"));
  372. return (AE_AML_OPERAND_VALUE);
  373. }
  374. /*
  375. * Decode the type of the expected package contents
  376. *
  377. * PTYPE1 packages contain no subpackages
  378. * PTYPE2 packages contain sub-packages
  379. */
  380. switch (package->ret_info.type) {
  381. case ACPI_PTYPE1_FIXED:
  382. /*
  383. * The package count is fixed and there are no sub-packages
  384. *
  385. * If package is too small, exit.
  386. * If package is larger than expected, issue warning but continue
  387. */
  388. expected_count =
  389. package->ret_info.count1 + package->ret_info.count2;
  390. if (count < expected_count) {
  391. goto package_too_small;
  392. } else if (count > expected_count) {
  393. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  394. data->node_flags,
  395. "Return Package is larger than needed - "
  396. "found %u, expected %u", count,
  397. expected_count));
  398. }
  399. /* Validate all elements of the returned package */
  400. status = acpi_ns_check_package_elements(data, elements,
  401. package->ret_info.
  402. object_type1,
  403. package->ret_info.
  404. count1,
  405. package->ret_info.
  406. object_type2,
  407. package->ret_info.
  408. count2, 0);
  409. break;
  410. case ACPI_PTYPE1_VAR:
  411. /*
  412. * The package count is variable, there are no sub-packages, and all
  413. * elements must be of the same type
  414. */
  415. for (i = 0; i < count; i++) {
  416. status = acpi_ns_check_object_type(data, elements,
  417. package->ret_info.
  418. object_type1, i);
  419. if (ACPI_FAILURE(status)) {
  420. return (status);
  421. }
  422. elements++;
  423. }
  424. break;
  425. case ACPI_PTYPE1_OPTION:
  426. /*
  427. * The package count is variable, there are no sub-packages. There are
  428. * a fixed number of required elements, and a variable number of
  429. * optional elements.
  430. *
  431. * Check if package is at least as large as the minimum required
  432. */
  433. expected_count = package->ret_info3.count;
  434. if (count < expected_count) {
  435. goto package_too_small;
  436. }
  437. /* Variable number of sub-objects */
  438. for (i = 0; i < count; i++) {
  439. if (i < package->ret_info3.count) {
  440. /* These are the required package elements (0, 1, or 2) */
  441. status =
  442. acpi_ns_check_object_type(data, elements,
  443. package->
  444. ret_info3.
  445. object_type[i],
  446. i);
  447. if (ACPI_FAILURE(status)) {
  448. return (status);
  449. }
  450. } else {
  451. /* These are the optional package elements */
  452. status =
  453. acpi_ns_check_object_type(data, elements,
  454. package->
  455. ret_info3.
  456. tail_object_type,
  457. i);
  458. if (ACPI_FAILURE(status)) {
  459. return (status);
  460. }
  461. }
  462. elements++;
  463. }
  464. break;
  465. case ACPI_PTYPE2_REV_FIXED:
  466. /* First element is the (Integer) revision */
  467. status = acpi_ns_check_object_type(data, elements,
  468. ACPI_RTYPE_INTEGER, 0);
  469. if (ACPI_FAILURE(status)) {
  470. return (status);
  471. }
  472. elements++;
  473. count--;
  474. /* Examine the sub-packages */
  475. status =
  476. acpi_ns_check_package_list(data, package, elements, count);
  477. break;
  478. case ACPI_PTYPE2_PKG_COUNT:
  479. /* First element is the (Integer) count of sub-packages to follow */
  480. status = acpi_ns_check_object_type(data, elements,
  481. ACPI_RTYPE_INTEGER, 0);
  482. if (ACPI_FAILURE(status)) {
  483. return (status);
  484. }
  485. /*
  486. * Count cannot be larger than the parent package length, but allow it
  487. * to be smaller. The >= accounts for the Integer above.
  488. */
  489. expected_count = (u32) (*elements)->integer.value;
  490. if (expected_count >= count) {
  491. goto package_too_small;
  492. }
  493. count = expected_count;
  494. elements++;
  495. /* Examine the sub-packages */
  496. status =
  497. acpi_ns_check_package_list(data, package, elements, count);
  498. break;
  499. case ACPI_PTYPE2:
  500. case ACPI_PTYPE2_FIXED:
  501. case ACPI_PTYPE2_MIN:
  502. case ACPI_PTYPE2_COUNT:
  503. /*
  504. * These types all return a single Package that consists of a
  505. * variable number of sub-Packages.
  506. *
  507. * First, ensure that the first element is a sub-Package. If not,
  508. * the BIOS may have incorrectly returned the object as a single
  509. * package instead of a Package of Packages (a common error if
  510. * there is only one entry). We may be able to repair this by
  511. * wrapping the returned Package with a new outer Package.
  512. */
  513. if ((*elements)->common.type != ACPI_TYPE_PACKAGE) {
  514. /* Create the new outer package and populate it */
  515. status =
  516. acpi_ns_repair_package_list(data,
  517. return_object_ptr);
  518. if (ACPI_FAILURE(status)) {
  519. return (status);
  520. }
  521. /* Update locals to point to the new package (of 1 element) */
  522. return_object = *return_object_ptr;
  523. elements = return_object->package.elements;
  524. count = 1;
  525. }
  526. /* Examine the sub-packages */
  527. status =
  528. acpi_ns_check_package_list(data, package, elements, count);
  529. break;
  530. default:
  531. /* Should not get here if predefined info table is correct */
  532. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  533. "Invalid internal return type in table entry: %X",
  534. package->ret_info.type));
  535. return (AE_AML_INTERNAL);
  536. }
  537. return (status);
  538. package_too_small:
  539. /* Error exit for the case with an incorrect package count */
  540. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  541. "Return Package is too small - found %u elements, expected %u",
  542. count, expected_count));
  543. return (AE_AML_OPERAND_VALUE);
  544. }
  545. /*******************************************************************************
  546. *
  547. * FUNCTION: acpi_ns_check_package_list
  548. *
  549. * PARAMETERS: Data - Pointer to validation data structure
  550. * Package - Pointer to package-specific info for method
  551. * Elements - Element list of parent package. All elements
  552. * of this list should be of type Package.
  553. * Count - Count of subpackages
  554. *
  555. * RETURN: Status
  556. *
  557. * DESCRIPTION: Examine a list of subpackages
  558. *
  559. ******************************************************************************/
  560. static acpi_status
  561. acpi_ns_check_package_list(struct acpi_predefined_data *data,
  562. const union acpi_predefined_info *package,
  563. union acpi_operand_object **elements, u32 count)
  564. {
  565. union acpi_operand_object *sub_package;
  566. union acpi_operand_object **sub_elements;
  567. acpi_status status;
  568. u32 expected_count;
  569. u32 i;
  570. u32 j;
  571. /* Validate each sub-Package in the parent Package */
  572. for (i = 0; i < count; i++) {
  573. sub_package = *elements;
  574. sub_elements = sub_package->package.elements;
  575. /* Each sub-object must be of type Package */
  576. status = acpi_ns_check_object_type(data, &sub_package,
  577. ACPI_RTYPE_PACKAGE, i);
  578. if (ACPI_FAILURE(status)) {
  579. return (status);
  580. }
  581. /* Examine the different types of expected sub-packages */
  582. switch (package->ret_info.type) {
  583. case ACPI_PTYPE2:
  584. case ACPI_PTYPE2_PKG_COUNT:
  585. case ACPI_PTYPE2_REV_FIXED:
  586. /* Each subpackage has a fixed number of elements */
  587. expected_count =
  588. package->ret_info.count1 + package->ret_info.count2;
  589. if (sub_package->package.count < expected_count) {
  590. goto package_too_small;
  591. }
  592. status =
  593. acpi_ns_check_package_elements(data, sub_elements,
  594. package->ret_info.
  595. object_type1,
  596. package->ret_info.
  597. count1,
  598. package->ret_info.
  599. object_type2,
  600. package->ret_info.
  601. count2, 0);
  602. if (ACPI_FAILURE(status)) {
  603. return (status);
  604. }
  605. break;
  606. case ACPI_PTYPE2_FIXED:
  607. /* Each sub-package has a fixed length */
  608. expected_count = package->ret_info2.count;
  609. if (sub_package->package.count < expected_count) {
  610. goto package_too_small;
  611. }
  612. /* Check the type of each sub-package element */
  613. for (j = 0; j < expected_count; j++) {
  614. status =
  615. acpi_ns_check_object_type(data,
  616. &sub_elements[j],
  617. package->
  618. ret_info2.
  619. object_type[j],
  620. j);
  621. if (ACPI_FAILURE(status)) {
  622. return (status);
  623. }
  624. }
  625. break;
  626. case ACPI_PTYPE2_MIN:
  627. /* Each sub-package has a variable but minimum length */
  628. expected_count = package->ret_info.count1;
  629. if (sub_package->package.count < expected_count) {
  630. goto package_too_small;
  631. }
  632. /* Check the type of each sub-package element */
  633. status =
  634. acpi_ns_check_package_elements(data, sub_elements,
  635. package->ret_info.
  636. object_type1,
  637. sub_package->package.
  638. count, 0, 0, 0);
  639. if (ACPI_FAILURE(status)) {
  640. return (status);
  641. }
  642. break;
  643. case ACPI_PTYPE2_COUNT:
  644. /*
  645. * First element is the (Integer) count of elements, including
  646. * the count field.
  647. */
  648. status = acpi_ns_check_object_type(data, sub_elements,
  649. ACPI_RTYPE_INTEGER,
  650. 0);
  651. if (ACPI_FAILURE(status)) {
  652. return (status);
  653. }
  654. /*
  655. * Make sure package is large enough for the Count and is
  656. * is as large as the minimum size
  657. */
  658. expected_count = (u32)(*sub_elements)->integer.value;
  659. if (sub_package->package.count < expected_count) {
  660. goto package_too_small;
  661. }
  662. if (sub_package->package.count <
  663. package->ret_info.count1) {
  664. expected_count = package->ret_info.count1;
  665. goto package_too_small;
  666. }
  667. /* Check the type of each sub-package element */
  668. status =
  669. acpi_ns_check_package_elements(data,
  670. (sub_elements + 1),
  671. package->ret_info.
  672. object_type1,
  673. (expected_count - 1),
  674. 0, 0, 1);
  675. if (ACPI_FAILURE(status)) {
  676. return (status);
  677. }
  678. break;
  679. default: /* Should not get here, type was validated by caller */
  680. return (AE_AML_INTERNAL);
  681. }
  682. elements++;
  683. }
  684. return (AE_OK);
  685. package_too_small:
  686. /* The sub-package count was smaller than required */
  687. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  688. "Return Sub-Package[%u] is too small - found %u elements, expected %u",
  689. i, sub_package->package.count, expected_count));
  690. return (AE_AML_OPERAND_VALUE);
  691. }
  692. /*******************************************************************************
  693. *
  694. * FUNCTION: acpi_ns_check_package_elements
  695. *
  696. * PARAMETERS: Data - Pointer to validation data structure
  697. * Elements - Pointer to the package elements array
  698. * Type1 - Object type for first group
  699. * Count1 - Count for first group
  700. * Type2 - Object type for second group
  701. * Count2 - Count for second group
  702. * start_index - Start of the first group of elements
  703. *
  704. * RETURN: Status
  705. *
  706. * DESCRIPTION: Check that all elements of a package are of the correct object
  707. * type. Supports up to two groups of different object types.
  708. *
  709. ******************************************************************************/
  710. static acpi_status
  711. acpi_ns_check_package_elements(struct acpi_predefined_data *data,
  712. union acpi_operand_object **elements,
  713. u8 type1,
  714. u32 count1,
  715. u8 type2, u32 count2, u32 start_index)
  716. {
  717. union acpi_operand_object **this_element = elements;
  718. acpi_status status;
  719. u32 i;
  720. /*
  721. * Up to two groups of package elements are supported by the data
  722. * structure. All elements in each group must be of the same type.
  723. * The second group can have a count of zero.
  724. */
  725. for (i = 0; i < count1; i++) {
  726. status = acpi_ns_check_object_type(data, this_element,
  727. type1, i + start_index);
  728. if (ACPI_FAILURE(status)) {
  729. return (status);
  730. }
  731. this_element++;
  732. }
  733. for (i = 0; i < count2; i++) {
  734. status = acpi_ns_check_object_type(data, this_element,
  735. type2,
  736. (i + count1 + start_index));
  737. if (ACPI_FAILURE(status)) {
  738. return (status);
  739. }
  740. this_element++;
  741. }
  742. return (AE_OK);
  743. }
  744. /*******************************************************************************
  745. *
  746. * FUNCTION: acpi_ns_check_object_type
  747. *
  748. * PARAMETERS: Data - Pointer to validation data structure
  749. * return_object_ptr - Pointer to the object returned from the
  750. * evaluation of a method or object
  751. * expected_btypes - Bitmap of expected return type(s)
  752. * package_index - Index of object within parent package (if
  753. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  754. * otherwise)
  755. *
  756. * RETURN: Status
  757. *
  758. * DESCRIPTION: Check the type of the return object against the expected object
  759. * type(s). Use of Btype allows multiple expected object types.
  760. *
  761. ******************************************************************************/
  762. static acpi_status
  763. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  764. union acpi_operand_object **return_object_ptr,
  765. u32 expected_btypes, u32 package_index)
  766. {
  767. union acpi_operand_object *return_object = *return_object_ptr;
  768. acpi_status status = AE_OK;
  769. u32 return_btype;
  770. char type_buffer[48]; /* Room for 5 types */
  771. /*
  772. * If we get a NULL return_object here, it is a NULL package element,
  773. * and this is always an error.
  774. */
  775. if (!return_object) {
  776. goto type_error_exit;
  777. }
  778. /* A Namespace node should not get here, but make sure */
  779. if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  780. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  781. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  782. return_object->node.name.ascii,
  783. acpi_ut_get_type_name(return_object->node.
  784. type)));
  785. return (AE_AML_OPERAND_TYPE);
  786. }
  787. /*
  788. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  789. * The bitmapped type allows multiple possible return types.
  790. *
  791. * Note, the cases below must handle all of the possible types returned
  792. * from all of the predefined names (including elements of returned
  793. * packages)
  794. */
  795. switch (return_object->common.type) {
  796. case ACPI_TYPE_INTEGER:
  797. return_btype = ACPI_RTYPE_INTEGER;
  798. break;
  799. case ACPI_TYPE_BUFFER:
  800. return_btype = ACPI_RTYPE_BUFFER;
  801. break;
  802. case ACPI_TYPE_STRING:
  803. return_btype = ACPI_RTYPE_STRING;
  804. break;
  805. case ACPI_TYPE_PACKAGE:
  806. return_btype = ACPI_RTYPE_PACKAGE;
  807. break;
  808. case ACPI_TYPE_LOCAL_REFERENCE:
  809. return_btype = ACPI_RTYPE_REFERENCE;
  810. break;
  811. default:
  812. /* Not one of the supported objects, must be incorrect */
  813. goto type_error_exit;
  814. }
  815. /* Is the object one of the expected types? */
  816. if (!(return_btype & expected_btypes)) {
  817. /* Type mismatch -- attempt repair of the returned object */
  818. status = acpi_ns_repair_object(data, expected_btypes,
  819. package_index,
  820. return_object_ptr);
  821. if (ACPI_SUCCESS(status)) {
  822. return (AE_OK); /* Repair was successful */
  823. }
  824. goto type_error_exit;
  825. }
  826. /* For reference objects, check that the reference type is correct */
  827. if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  828. status = acpi_ns_check_reference(data, return_object);
  829. }
  830. return (status);
  831. type_error_exit:
  832. /* Create a string with all expected types for this predefined object */
  833. acpi_ns_get_expected_types(type_buffer, expected_btypes);
  834. if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  835. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  836. "Return type mismatch - found %s, expected %s",
  837. acpi_ut_get_object_type_name
  838. (return_object), type_buffer));
  839. } else {
  840. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  841. "Return Package type mismatch at index %u - "
  842. "found %s, expected %s", package_index,
  843. acpi_ut_get_object_type_name
  844. (return_object), type_buffer));
  845. }
  846. return (AE_AML_OPERAND_TYPE);
  847. }
  848. /*******************************************************************************
  849. *
  850. * FUNCTION: acpi_ns_check_reference
  851. *
  852. * PARAMETERS: Data - Pointer to validation data structure
  853. * return_object - Object returned from the evaluation of a
  854. * method or object
  855. *
  856. * RETURN: Status
  857. *
  858. * DESCRIPTION: Check a returned reference object for the correct reference
  859. * type. The only reference type that can be returned from a
  860. * predefined method is a named reference. All others are invalid.
  861. *
  862. ******************************************************************************/
  863. static acpi_status
  864. acpi_ns_check_reference(struct acpi_predefined_data *data,
  865. union acpi_operand_object *return_object)
  866. {
  867. /*
  868. * Check the reference object for the correct reference type (opcode).
  869. * The only type of reference that can be converted to an union acpi_object is
  870. * a reference to a named object (reference class: NAME)
  871. */
  872. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  873. return (AE_OK);
  874. }
  875. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  876. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  877. acpi_ut_get_reference_name(return_object),
  878. return_object->reference.class));
  879. return (AE_AML_OPERAND_TYPE);
  880. }
  881. /*******************************************************************************
  882. *
  883. * FUNCTION: acpi_ns_get_expected_types
  884. *
  885. * PARAMETERS: Buffer - Pointer to where the string is returned
  886. * expected_btypes - Bitmap of expected return type(s)
  887. *
  888. * RETURN: Buffer is populated with type names.
  889. *
  890. * DESCRIPTION: Translate the expected types bitmap into a string of ascii
  891. * names of expected types, for use in warning messages.
  892. *
  893. ******************************************************************************/
  894. static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
  895. {
  896. u32 this_rtype;
  897. u32 i;
  898. u32 j;
  899. j = 1;
  900. buffer[0] = 0;
  901. this_rtype = ACPI_RTYPE_INTEGER;
  902. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  903. /* If one of the expected types, concatenate the name of this type */
  904. if (expected_btypes & this_rtype) {
  905. ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
  906. j = 0; /* Use name separator from now on */
  907. }
  908. this_rtype <<= 1; /* Next Rtype */
  909. }
  910. }