nspredef.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /******************************************************************************
  2. *
  3. * Module Name: nspredef - Validation of ACPI predefined methods and objects
  4. * $Revision: 1.1 $
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2010, 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. * 1) 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. * 2) If the return value can be of any type, then we cannot perform any
  177. * validation, exit.
  178. */
  179. if ((!predefined->info.expected_btypes) ||
  180. (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
  181. goto cleanup;
  182. }
  183. /* Create the parameter data block for object validation */
  184. data = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_predefined_data));
  185. if (!data) {
  186. goto cleanup;
  187. }
  188. data->predefined = predefined;
  189. data->node_flags = node->flags;
  190. data->pathname = pathname;
  191. /*
  192. * Check that the type of the main return object is what is expected
  193. * for this predefined name
  194. */
  195. status = acpi_ns_check_object_type(data, return_object_ptr,
  196. predefined->info.expected_btypes,
  197. ACPI_NOT_PACKAGE_ELEMENT);
  198. if (ACPI_FAILURE(status)) {
  199. goto exit;
  200. }
  201. /*
  202. * For returned Package objects, check the type of all sub-objects.
  203. * Note: Package may have been newly created by call above.
  204. */
  205. if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
  206. data->parent_package = *return_object_ptr;
  207. status = acpi_ns_check_package(data, return_object_ptr);
  208. if (ACPI_FAILURE(status)) {
  209. goto exit;
  210. }
  211. }
  212. /*
  213. * The return object was OK, or it was successfully repaired above.
  214. * Now make some additional checks such as verifying that package
  215. * objects are sorted correctly (if required) or buffer objects have
  216. * the correct data width (bytes vs. dwords). These repairs are
  217. * performed on a per-name basis, i.e., the code is specific to
  218. * particular predefined names.
  219. */
  220. status = acpi_ns_complex_repairs(data, node, status, return_object_ptr);
  221. exit:
  222. /*
  223. * If the object validation failed or if we successfully repaired one
  224. * or more objects, mark the parent node to suppress further warning
  225. * messages during the next evaluation of the same method/object.
  226. */
  227. if (ACPI_FAILURE(status) || (data->flags & ACPI_OBJECT_REPAIRED)) {
  228. node->flags |= ANOBJ_EVALUATED;
  229. }
  230. ACPI_FREE(data);
  231. cleanup:
  232. ACPI_FREE(pathname);
  233. return (status);
  234. }
  235. /*******************************************************************************
  236. *
  237. * FUNCTION: acpi_ns_check_parameter_count
  238. *
  239. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  240. * Node - Namespace node for the method/object
  241. * user_param_count - Number of args passed in by the caller
  242. * Predefined - Pointer to entry in predefined name table
  243. *
  244. * RETURN: None
  245. *
  246. * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
  247. * predefined name is what is expected (i.e., what is defined in
  248. * the ACPI specification for this predefined name.)
  249. *
  250. ******************************************************************************/
  251. void
  252. acpi_ns_check_parameter_count(char *pathname,
  253. struct acpi_namespace_node *node,
  254. u32 user_param_count,
  255. const union acpi_predefined_info *predefined)
  256. {
  257. u32 param_count;
  258. u32 required_params_current;
  259. u32 required_params_old;
  260. /* Methods have 0-7 parameters. All other types have zero. */
  261. param_count = 0;
  262. if (node->type == ACPI_TYPE_METHOD) {
  263. param_count = node->object->method.param_count;
  264. }
  265. if (!predefined) {
  266. /*
  267. * Check the parameter count for non-predefined methods/objects.
  268. *
  269. * Warning if too few or too many arguments have been passed by the
  270. * caller. An incorrect number of arguments may not cause the method
  271. * to fail. However, the method will fail if there are too few
  272. * arguments and the method attempts to use one of the missing ones.
  273. */
  274. if (user_param_count < param_count) {
  275. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  276. ACPI_WARN_ALWAYS,
  277. "Insufficient arguments - needs %u, found %u",
  278. param_count, user_param_count));
  279. } else if (user_param_count > param_count) {
  280. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  281. ACPI_WARN_ALWAYS,
  282. "Excess arguments - needs %u, found %u",
  283. param_count, user_param_count));
  284. }
  285. return;
  286. }
  287. /*
  288. * Validate the user-supplied parameter count.
  289. * Allow two different legal argument counts (_SCP, etc.)
  290. */
  291. required_params_current = predefined->info.param_count & 0x0F;
  292. required_params_old = predefined->info.param_count >> 4;
  293. if (user_param_count != ACPI_UINT32_MAX) {
  294. if ((user_param_count != required_params_current) &&
  295. (user_param_count != required_params_old)) {
  296. ACPI_WARN_PREDEFINED((AE_INFO, pathname,
  297. ACPI_WARN_ALWAYS,
  298. "Parameter count mismatch - "
  299. "caller passed %u, ACPI requires %u",
  300. user_param_count,
  301. required_params_current));
  302. }
  303. }
  304. /*
  305. * Check that the ASL-defined parameter count is what is expected for
  306. * this predefined name (parameter count as defined by the ACPI
  307. * specification)
  308. */
  309. if ((param_count != required_params_current) &&
  310. (param_count != required_params_old)) {
  311. ACPI_WARN_PREDEFINED((AE_INFO, pathname, node->flags,
  312. "Parameter count mismatch - ASL declared %u, ACPI requires %u",
  313. param_count, required_params_current));
  314. }
  315. }
  316. /*******************************************************************************
  317. *
  318. * FUNCTION: acpi_ns_check_for_predefined_name
  319. *
  320. * PARAMETERS: Node - Namespace node for the method/object
  321. *
  322. * RETURN: Pointer to entry in predefined table. NULL indicates not found.
  323. *
  324. * DESCRIPTION: Check an object name against the predefined object list.
  325. *
  326. ******************************************************************************/
  327. const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
  328. acpi_namespace_node
  329. *node)
  330. {
  331. const union acpi_predefined_info *this_name;
  332. /* Quick check for a predefined name, first character must be underscore */
  333. if (node->name.ascii[0] != '_') {
  334. return (NULL);
  335. }
  336. /* Search info table for a predefined method/object name */
  337. this_name = predefined_names;
  338. while (this_name->info.name[0]) {
  339. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
  340. return (this_name);
  341. }
  342. /*
  343. * Skip next entry in the table if this name returns a Package
  344. * (next entry contains the package info)
  345. */
  346. if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
  347. this_name++;
  348. }
  349. this_name++;
  350. }
  351. return (NULL); /* Not found */
  352. }
  353. /*******************************************************************************
  354. *
  355. * FUNCTION: acpi_ns_check_package
  356. *
  357. * PARAMETERS: Data - Pointer to validation data structure
  358. * return_object_ptr - Pointer to the object returned from the
  359. * evaluation of a method or object
  360. *
  361. * RETURN: Status
  362. *
  363. * DESCRIPTION: Check a returned package object for the correct count and
  364. * correct type of all sub-objects.
  365. *
  366. ******************************************************************************/
  367. static acpi_status
  368. acpi_ns_check_package(struct acpi_predefined_data *data,
  369. union acpi_operand_object **return_object_ptr)
  370. {
  371. union acpi_operand_object *return_object = *return_object_ptr;
  372. const union acpi_predefined_info *package;
  373. union acpi_operand_object **elements;
  374. acpi_status status = AE_OK;
  375. u32 expected_count;
  376. u32 count;
  377. u32 i;
  378. ACPI_FUNCTION_NAME(ns_check_package);
  379. /* The package info for this name is in the next table entry */
  380. package = data->predefined + 1;
  381. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  382. "%s Validating return Package of Type %X, Count %X\n",
  383. data->pathname, package->ret_info.type,
  384. return_object->package.count));
  385. /*
  386. * For variable-length Packages, we can safely remove all embedded
  387. * and trailing NULL package elements
  388. */
  389. acpi_ns_remove_null_elements(data, package->ret_info.type,
  390. return_object);
  391. /* Extract package count and elements array */
  392. elements = return_object->package.elements;
  393. count = return_object->package.count;
  394. /* The package must have at least one element, else invalid */
  395. if (!count) {
  396. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  397. "Return Package has no elements (empty)"));
  398. return (AE_AML_OPERAND_VALUE);
  399. }
  400. /*
  401. * Decode the type of the expected package contents
  402. *
  403. * PTYPE1 packages contain no subpackages
  404. * PTYPE2 packages contain sub-packages
  405. */
  406. switch (package->ret_info.type) {
  407. case ACPI_PTYPE1_FIXED:
  408. /*
  409. * The package count is fixed and there are no sub-packages
  410. *
  411. * If package is too small, exit.
  412. * If package is larger than expected, issue warning but continue
  413. */
  414. expected_count =
  415. package->ret_info.count1 + package->ret_info.count2;
  416. if (count < expected_count) {
  417. goto package_too_small;
  418. } else if (count > expected_count) {
  419. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  420. "%s: Return Package is larger than needed - "
  421. "found %u, expected %u\n",
  422. data->pathname, count,
  423. expected_count));
  424. }
  425. /* Validate all elements of the returned package */
  426. status = acpi_ns_check_package_elements(data, elements,
  427. package->ret_info.
  428. object_type1,
  429. package->ret_info.
  430. count1,
  431. package->ret_info.
  432. object_type2,
  433. package->ret_info.
  434. count2, 0);
  435. break;
  436. case ACPI_PTYPE1_VAR:
  437. /*
  438. * The package count is variable, there are no sub-packages, and all
  439. * elements must be of the same type
  440. */
  441. for (i = 0; i < count; i++) {
  442. status = acpi_ns_check_object_type(data, elements,
  443. package->ret_info.
  444. object_type1, i);
  445. if (ACPI_FAILURE(status)) {
  446. return (status);
  447. }
  448. elements++;
  449. }
  450. break;
  451. case ACPI_PTYPE1_OPTION:
  452. /*
  453. * The package count is variable, there are no sub-packages. There are
  454. * a fixed number of required elements, and a variable number of
  455. * optional elements.
  456. *
  457. * Check if package is at least as large as the minimum required
  458. */
  459. expected_count = package->ret_info3.count;
  460. if (count < expected_count) {
  461. goto package_too_small;
  462. }
  463. /* Variable number of sub-objects */
  464. for (i = 0; i < count; i++) {
  465. if (i < package->ret_info3.count) {
  466. /* These are the required package elements (0, 1, or 2) */
  467. status =
  468. acpi_ns_check_object_type(data, elements,
  469. package->
  470. ret_info3.
  471. object_type[i],
  472. i);
  473. if (ACPI_FAILURE(status)) {
  474. return (status);
  475. }
  476. } else {
  477. /* These are the optional package elements */
  478. status =
  479. acpi_ns_check_object_type(data, elements,
  480. package->
  481. ret_info3.
  482. tail_object_type,
  483. i);
  484. if (ACPI_FAILURE(status)) {
  485. return (status);
  486. }
  487. }
  488. elements++;
  489. }
  490. break;
  491. case ACPI_PTYPE2_REV_FIXED:
  492. /* First element is the (Integer) revision */
  493. status = acpi_ns_check_object_type(data, elements,
  494. ACPI_RTYPE_INTEGER, 0);
  495. if (ACPI_FAILURE(status)) {
  496. return (status);
  497. }
  498. elements++;
  499. count--;
  500. /* Examine the sub-packages */
  501. status =
  502. acpi_ns_check_package_list(data, package, elements, count);
  503. break;
  504. case ACPI_PTYPE2_PKG_COUNT:
  505. /* First element is the (Integer) count of sub-packages to follow */
  506. status = acpi_ns_check_object_type(data, elements,
  507. ACPI_RTYPE_INTEGER, 0);
  508. if (ACPI_FAILURE(status)) {
  509. return (status);
  510. }
  511. /*
  512. * Count cannot be larger than the parent package length, but allow it
  513. * to be smaller. The >= accounts for the Integer above.
  514. */
  515. expected_count = (u32) (*elements)->integer.value;
  516. if (expected_count >= count) {
  517. goto package_too_small;
  518. }
  519. count = expected_count;
  520. elements++;
  521. /* Examine the sub-packages */
  522. status =
  523. acpi_ns_check_package_list(data, package, elements, count);
  524. break;
  525. case ACPI_PTYPE2:
  526. case ACPI_PTYPE2_FIXED:
  527. case ACPI_PTYPE2_MIN:
  528. case ACPI_PTYPE2_COUNT:
  529. /*
  530. * These types all return a single Package that consists of a
  531. * variable number of sub-Packages.
  532. *
  533. * First, ensure that the first element is a sub-Package. If not,
  534. * the BIOS may have incorrectly returned the object as a single
  535. * package instead of a Package of Packages (a common error if
  536. * there is only one entry). We may be able to repair this by
  537. * wrapping the returned Package with a new outer Package.
  538. */
  539. if (*elements
  540. && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
  541. /* Create the new outer package and populate it */
  542. status =
  543. acpi_ns_repair_package_list(data,
  544. return_object_ptr);
  545. if (ACPI_FAILURE(status)) {
  546. return (status);
  547. }
  548. /* Update locals to point to the new package (of 1 element) */
  549. return_object = *return_object_ptr;
  550. elements = return_object->package.elements;
  551. count = 1;
  552. }
  553. /* Examine the sub-packages */
  554. status =
  555. acpi_ns_check_package_list(data, package, elements, count);
  556. break;
  557. default:
  558. /* Should not get here if predefined info table is correct */
  559. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  560. "Invalid internal return type in table entry: %X",
  561. package->ret_info.type));
  562. return (AE_AML_INTERNAL);
  563. }
  564. return (status);
  565. package_too_small:
  566. /* Error exit for the case with an incorrect package count */
  567. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  568. "Return Package is too small - found %u elements, expected %u",
  569. count, expected_count));
  570. return (AE_AML_OPERAND_VALUE);
  571. }
  572. /*******************************************************************************
  573. *
  574. * FUNCTION: acpi_ns_check_package_list
  575. *
  576. * PARAMETERS: Data - Pointer to validation data structure
  577. * Package - Pointer to package-specific info for method
  578. * Elements - Element list of parent package. All elements
  579. * of this list should be of type Package.
  580. * Count - Count of subpackages
  581. *
  582. * RETURN: Status
  583. *
  584. * DESCRIPTION: Examine a list of subpackages
  585. *
  586. ******************************************************************************/
  587. static acpi_status
  588. acpi_ns_check_package_list(struct acpi_predefined_data *data,
  589. const union acpi_predefined_info *package,
  590. union acpi_operand_object **elements, u32 count)
  591. {
  592. union acpi_operand_object *sub_package;
  593. union acpi_operand_object **sub_elements;
  594. acpi_status status;
  595. u32 expected_count;
  596. u32 i;
  597. u32 j;
  598. /*
  599. * Validate each sub-Package in the parent Package
  600. *
  601. * NOTE: assumes list of sub-packages contains no NULL elements.
  602. * Any NULL elements should have been removed by earlier call
  603. * to acpi_ns_remove_null_elements.
  604. */
  605. for (i = 0; i < count; i++) {
  606. sub_package = *elements;
  607. sub_elements = sub_package->package.elements;
  608. data->parent_package = sub_package;
  609. /* Each sub-object must be of type Package */
  610. status = acpi_ns_check_object_type(data, &sub_package,
  611. ACPI_RTYPE_PACKAGE, i);
  612. if (ACPI_FAILURE(status)) {
  613. return (status);
  614. }
  615. /* Examine the different types of expected sub-packages */
  616. data->parent_package = sub_package;
  617. switch (package->ret_info.type) {
  618. case ACPI_PTYPE2:
  619. case ACPI_PTYPE2_PKG_COUNT:
  620. case ACPI_PTYPE2_REV_FIXED:
  621. /* Each subpackage has a fixed number of elements */
  622. expected_count =
  623. package->ret_info.count1 + package->ret_info.count2;
  624. if (sub_package->package.count < expected_count) {
  625. goto package_too_small;
  626. }
  627. status =
  628. acpi_ns_check_package_elements(data, sub_elements,
  629. package->ret_info.
  630. object_type1,
  631. package->ret_info.
  632. count1,
  633. package->ret_info.
  634. object_type2,
  635. package->ret_info.
  636. count2, 0);
  637. if (ACPI_FAILURE(status)) {
  638. return (status);
  639. }
  640. break;
  641. case ACPI_PTYPE2_FIXED:
  642. /* Each sub-package has a fixed length */
  643. expected_count = package->ret_info2.count;
  644. if (sub_package->package.count < expected_count) {
  645. goto package_too_small;
  646. }
  647. /* Check the type of each sub-package element */
  648. for (j = 0; j < expected_count; j++) {
  649. status =
  650. acpi_ns_check_object_type(data,
  651. &sub_elements[j],
  652. package->
  653. ret_info2.
  654. object_type[j],
  655. j);
  656. if (ACPI_FAILURE(status)) {
  657. return (status);
  658. }
  659. }
  660. break;
  661. case ACPI_PTYPE2_MIN:
  662. /* Each sub-package has a variable but minimum length */
  663. expected_count = package->ret_info.count1;
  664. if (sub_package->package.count < expected_count) {
  665. goto package_too_small;
  666. }
  667. /* Check the type of each sub-package element */
  668. status =
  669. acpi_ns_check_package_elements(data, sub_elements,
  670. package->ret_info.
  671. object_type1,
  672. sub_package->package.
  673. count, 0, 0, 0);
  674. if (ACPI_FAILURE(status)) {
  675. return (status);
  676. }
  677. break;
  678. case ACPI_PTYPE2_COUNT:
  679. /*
  680. * First element is the (Integer) count of elements, including
  681. * the count field (the ACPI name is num_elements)
  682. */
  683. status = acpi_ns_check_object_type(data, sub_elements,
  684. ACPI_RTYPE_INTEGER,
  685. 0);
  686. if (ACPI_FAILURE(status)) {
  687. return (status);
  688. }
  689. /*
  690. * Make sure package is large enough for the Count and is
  691. * is as large as the minimum size
  692. */
  693. expected_count = (u32)(*sub_elements)->integer.value;
  694. if (sub_package->package.count < expected_count) {
  695. goto package_too_small;
  696. }
  697. if (sub_package->package.count <
  698. package->ret_info.count1) {
  699. expected_count = package->ret_info.count1;
  700. goto package_too_small;
  701. }
  702. if (expected_count == 0) {
  703. /*
  704. * Either the num_entries element was originally zero or it was
  705. * a NULL element and repaired to an Integer of value zero.
  706. * In either case, repair it by setting num_entries to be the
  707. * actual size of the subpackage.
  708. */
  709. expected_count = sub_package->package.count;
  710. (*sub_elements)->integer.value = expected_count;
  711. }
  712. /* Check the type of each sub-package element */
  713. status =
  714. acpi_ns_check_package_elements(data,
  715. (sub_elements + 1),
  716. package->ret_info.
  717. object_type1,
  718. (expected_count - 1),
  719. 0, 0, 1);
  720. if (ACPI_FAILURE(status)) {
  721. return (status);
  722. }
  723. break;
  724. default: /* Should not get here, type was validated by caller */
  725. return (AE_AML_INTERNAL);
  726. }
  727. elements++;
  728. }
  729. return (AE_OK);
  730. package_too_small:
  731. /* The sub-package count was smaller than required */
  732. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  733. "Return Sub-Package[%u] is too small - found %u elements, expected %u",
  734. i, sub_package->package.count, expected_count));
  735. return (AE_AML_OPERAND_VALUE);
  736. }
  737. /*******************************************************************************
  738. *
  739. * FUNCTION: acpi_ns_check_package_elements
  740. *
  741. * PARAMETERS: Data - Pointer to validation data structure
  742. * Elements - Pointer to the package elements array
  743. * Type1 - Object type for first group
  744. * Count1 - Count for first group
  745. * Type2 - Object type for second group
  746. * Count2 - Count for second group
  747. * start_index - Start of the first group of elements
  748. *
  749. * RETURN: Status
  750. *
  751. * DESCRIPTION: Check that all elements of a package are of the correct object
  752. * type. Supports up to two groups of different object types.
  753. *
  754. ******************************************************************************/
  755. static acpi_status
  756. acpi_ns_check_package_elements(struct acpi_predefined_data *data,
  757. union acpi_operand_object **elements,
  758. u8 type1,
  759. u32 count1,
  760. u8 type2, u32 count2, u32 start_index)
  761. {
  762. union acpi_operand_object **this_element = elements;
  763. acpi_status status;
  764. u32 i;
  765. /*
  766. * Up to two groups of package elements are supported by the data
  767. * structure. All elements in each group must be of the same type.
  768. * The second group can have a count of zero.
  769. */
  770. for (i = 0; i < count1; i++) {
  771. status = acpi_ns_check_object_type(data, this_element,
  772. type1, i + start_index);
  773. if (ACPI_FAILURE(status)) {
  774. return (status);
  775. }
  776. this_element++;
  777. }
  778. for (i = 0; i < count2; i++) {
  779. status = acpi_ns_check_object_type(data, this_element,
  780. type2,
  781. (i + count1 + start_index));
  782. if (ACPI_FAILURE(status)) {
  783. return (status);
  784. }
  785. this_element++;
  786. }
  787. return (AE_OK);
  788. }
  789. /*******************************************************************************
  790. *
  791. * FUNCTION: acpi_ns_check_object_type
  792. *
  793. * PARAMETERS: Data - Pointer to validation data structure
  794. * return_object_ptr - Pointer to the object returned from the
  795. * evaluation of a method or object
  796. * expected_btypes - Bitmap of expected return type(s)
  797. * package_index - Index of object within parent package (if
  798. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  799. * otherwise)
  800. *
  801. * RETURN: Status
  802. *
  803. * DESCRIPTION: Check the type of the return object against the expected object
  804. * type(s). Use of Btype allows multiple expected object types.
  805. *
  806. ******************************************************************************/
  807. static acpi_status
  808. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  809. union acpi_operand_object **return_object_ptr,
  810. u32 expected_btypes, u32 package_index)
  811. {
  812. union acpi_operand_object *return_object = *return_object_ptr;
  813. acpi_status status = AE_OK;
  814. u32 return_btype;
  815. char type_buffer[48]; /* Room for 5 types */
  816. /*
  817. * If we get a NULL return_object here, it is a NULL package element.
  818. * Since all extraneous NULL package elements were removed earlier by a
  819. * call to acpi_ns_remove_null_elements, this is an unexpected NULL element.
  820. * We will attempt to repair it.
  821. */
  822. if (!return_object) {
  823. status = acpi_ns_repair_null_element(data, expected_btypes,
  824. package_index,
  825. return_object_ptr);
  826. if (ACPI_SUCCESS(status)) {
  827. return (AE_OK); /* Repair was successful */
  828. }
  829. goto type_error_exit;
  830. }
  831. /* A Namespace node should not get here, but make sure */
  832. if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  833. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  834. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  835. return_object->node.name.ascii,
  836. acpi_ut_get_type_name(return_object->node.
  837. type)));
  838. return (AE_AML_OPERAND_TYPE);
  839. }
  840. /*
  841. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  842. * The bitmapped type allows multiple possible return types.
  843. *
  844. * Note, the cases below must handle all of the possible types returned
  845. * from all of the predefined names (including elements of returned
  846. * packages)
  847. */
  848. switch (return_object->common.type) {
  849. case ACPI_TYPE_INTEGER:
  850. return_btype = ACPI_RTYPE_INTEGER;
  851. break;
  852. case ACPI_TYPE_BUFFER:
  853. return_btype = ACPI_RTYPE_BUFFER;
  854. break;
  855. case ACPI_TYPE_STRING:
  856. return_btype = ACPI_RTYPE_STRING;
  857. break;
  858. case ACPI_TYPE_PACKAGE:
  859. return_btype = ACPI_RTYPE_PACKAGE;
  860. break;
  861. case ACPI_TYPE_LOCAL_REFERENCE:
  862. return_btype = ACPI_RTYPE_REFERENCE;
  863. break;
  864. default:
  865. /* Not one of the supported objects, must be incorrect */
  866. goto type_error_exit;
  867. }
  868. /* Is the object one of the expected types? */
  869. if (return_btype & expected_btypes) {
  870. /* For reference objects, check that the reference type is correct */
  871. if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  872. status = acpi_ns_check_reference(data, return_object);
  873. }
  874. return (status);
  875. }
  876. /* Type mismatch -- attempt repair of the returned object */
  877. status = acpi_ns_repair_object(data, expected_btypes,
  878. package_index, return_object_ptr);
  879. if (ACPI_SUCCESS(status)) {
  880. return (AE_OK); /* Repair was successful */
  881. }
  882. type_error_exit:
  883. /* Create a string with all expected types for this predefined object */
  884. acpi_ns_get_expected_types(type_buffer, expected_btypes);
  885. if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  886. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  887. "Return type mismatch - found %s, expected %s",
  888. acpi_ut_get_object_type_name
  889. (return_object), type_buffer));
  890. } else {
  891. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  892. "Return Package type mismatch at index %u - "
  893. "found %s, expected %s", package_index,
  894. acpi_ut_get_object_type_name
  895. (return_object), type_buffer));
  896. }
  897. return (AE_AML_OPERAND_TYPE);
  898. }
  899. /*******************************************************************************
  900. *
  901. * FUNCTION: acpi_ns_check_reference
  902. *
  903. * PARAMETERS: Data - Pointer to validation data structure
  904. * return_object - Object returned from the evaluation of a
  905. * method or object
  906. *
  907. * RETURN: Status
  908. *
  909. * DESCRIPTION: Check a returned reference object for the correct reference
  910. * type. The only reference type that can be returned from a
  911. * predefined method is a named reference. All others are invalid.
  912. *
  913. ******************************************************************************/
  914. static acpi_status
  915. acpi_ns_check_reference(struct acpi_predefined_data *data,
  916. union acpi_operand_object *return_object)
  917. {
  918. /*
  919. * Check the reference object for the correct reference type (opcode).
  920. * The only type of reference that can be converted to an union acpi_object is
  921. * a reference to a named object (reference class: NAME)
  922. */
  923. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  924. return (AE_OK);
  925. }
  926. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  927. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  928. acpi_ut_get_reference_name(return_object),
  929. return_object->reference.class));
  930. return (AE_AML_OPERAND_TYPE);
  931. }
  932. /*******************************************************************************
  933. *
  934. * FUNCTION: acpi_ns_get_expected_types
  935. *
  936. * PARAMETERS: Buffer - Pointer to where the string is returned
  937. * expected_btypes - Bitmap of expected return type(s)
  938. *
  939. * RETURN: Buffer is populated with type names.
  940. *
  941. * DESCRIPTION: Translate the expected types bitmap into a string of ascii
  942. * names of expected types, for use in warning messages.
  943. *
  944. ******************************************************************************/
  945. static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
  946. {
  947. u32 this_rtype;
  948. u32 i;
  949. u32 j;
  950. j = 1;
  951. buffer[0] = 0;
  952. this_rtype = ACPI_RTYPE_INTEGER;
  953. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  954. /* If one of the expected types, concatenate the name of this type */
  955. if (expected_btypes & this_rtype) {
  956. ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
  957. j = 0; /* Use name separator from now on */
  958. }
  959. this_rtype <<= 1; /* Next Rtype */
  960. }
  961. }