nspredef.c 33 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 - 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. * 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 return object is what is expected for
  193. * 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 check_validation_status;
  200. }
  201. /* For returned Package objects, check the type of all sub-objects */
  202. if (return_object->common.type == ACPI_TYPE_PACKAGE) {
  203. status = acpi_ns_check_package(data, return_object_ptr);
  204. }
  205. /*
  206. * Perform additional, more complicated repairs on a per-name
  207. * basis.
  208. */
  209. status = acpi_ns_complex_repairs(data, node, status, return_object_ptr);
  210. check_validation_status:
  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_package
  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. *
  350. * RETURN: Status
  351. *
  352. * DESCRIPTION: Check a returned package object for the correct count and
  353. * correct type of all sub-objects.
  354. *
  355. ******************************************************************************/
  356. static acpi_status
  357. acpi_ns_check_package(struct acpi_predefined_data *data,
  358. union acpi_operand_object **return_object_ptr)
  359. {
  360. union acpi_operand_object *return_object = *return_object_ptr;
  361. const union acpi_predefined_info *package;
  362. union acpi_operand_object **elements;
  363. acpi_status status = AE_OK;
  364. u32 expected_count;
  365. u32 count;
  366. u32 i;
  367. ACPI_FUNCTION_NAME(ns_check_package);
  368. /* The package info for this name is in the next table entry */
  369. package = data->predefined + 1;
  370. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  371. "%s Validating return Package of Type %X, Count %X\n",
  372. data->pathname, package->ret_info.type,
  373. return_object->package.count));
  374. /* Extract package count and elements array */
  375. elements = return_object->package.elements;
  376. count = return_object->package.count;
  377. /* The package must have at least one element, else invalid */
  378. if (!count) {
  379. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  380. "Return Package has no elements (empty)"));
  381. return (AE_AML_OPERAND_VALUE);
  382. }
  383. /*
  384. * Decode the type of the expected package contents
  385. *
  386. * PTYPE1 packages contain no subpackages
  387. * PTYPE2 packages contain sub-packages
  388. */
  389. switch (package->ret_info.type) {
  390. case ACPI_PTYPE1_FIXED:
  391. /*
  392. * The package count is fixed and there are no sub-packages
  393. *
  394. * If package is too small, exit.
  395. * If package is larger than expected, issue warning but continue
  396. */
  397. expected_count =
  398. package->ret_info.count1 + package->ret_info.count2;
  399. if (count < expected_count) {
  400. goto package_too_small;
  401. } else if (count > expected_count) {
  402. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  403. data->node_flags,
  404. "Return Package is larger than needed - "
  405. "found %u, expected %u", count,
  406. expected_count));
  407. }
  408. /* Validate all elements of the returned package */
  409. status = acpi_ns_check_package_elements(data, elements,
  410. package->ret_info.
  411. object_type1,
  412. package->ret_info.
  413. count1,
  414. package->ret_info.
  415. object_type2,
  416. package->ret_info.
  417. count2, 0);
  418. break;
  419. case ACPI_PTYPE1_VAR:
  420. /*
  421. * The package count is variable, there are no sub-packages, and all
  422. * elements must be of the same type
  423. */
  424. for (i = 0; i < count; i++) {
  425. status = acpi_ns_check_object_type(data, elements,
  426. package->ret_info.
  427. object_type1, i);
  428. if (ACPI_FAILURE(status)) {
  429. return (status);
  430. }
  431. elements++;
  432. }
  433. break;
  434. case ACPI_PTYPE1_OPTION:
  435. /*
  436. * The package count is variable, there are no sub-packages. There are
  437. * a fixed number of required elements, and a variable number of
  438. * optional elements.
  439. *
  440. * Check if package is at least as large as the minimum required
  441. */
  442. expected_count = package->ret_info3.count;
  443. if (count < expected_count) {
  444. goto package_too_small;
  445. }
  446. /* Variable number of sub-objects */
  447. for (i = 0; i < count; i++) {
  448. if (i < package->ret_info3.count) {
  449. /* These are the required package elements (0, 1, or 2) */
  450. status =
  451. acpi_ns_check_object_type(data, elements,
  452. package->
  453. ret_info3.
  454. object_type[i],
  455. i);
  456. if (ACPI_FAILURE(status)) {
  457. return (status);
  458. }
  459. } else {
  460. /* These are the optional package elements */
  461. status =
  462. acpi_ns_check_object_type(data, elements,
  463. package->
  464. ret_info3.
  465. tail_object_type,
  466. i);
  467. if (ACPI_FAILURE(status)) {
  468. return (status);
  469. }
  470. }
  471. elements++;
  472. }
  473. break;
  474. case ACPI_PTYPE2_REV_FIXED:
  475. /* First element is the (Integer) revision */
  476. status = acpi_ns_check_object_type(data, elements,
  477. ACPI_RTYPE_INTEGER, 0);
  478. if (ACPI_FAILURE(status)) {
  479. return (status);
  480. }
  481. elements++;
  482. count--;
  483. /* Examine the sub-packages */
  484. status =
  485. acpi_ns_check_package_list(data, package, elements, count);
  486. break;
  487. case ACPI_PTYPE2_PKG_COUNT:
  488. /* First element is the (Integer) count of sub-packages to follow */
  489. status = acpi_ns_check_object_type(data, elements,
  490. ACPI_RTYPE_INTEGER, 0);
  491. if (ACPI_FAILURE(status)) {
  492. return (status);
  493. }
  494. /*
  495. * Count cannot be larger than the parent package length, but allow it
  496. * to be smaller. The >= accounts for the Integer above.
  497. */
  498. expected_count = (u32) (*elements)->integer.value;
  499. if (expected_count >= count) {
  500. goto package_too_small;
  501. }
  502. count = expected_count;
  503. elements++;
  504. /* Examine the sub-packages */
  505. status =
  506. acpi_ns_check_package_list(data, package, elements, count);
  507. break;
  508. case ACPI_PTYPE2:
  509. case ACPI_PTYPE2_FIXED:
  510. case ACPI_PTYPE2_MIN:
  511. case ACPI_PTYPE2_COUNT:
  512. /*
  513. * These types all return a single Package that consists of a
  514. * variable number of sub-Packages.
  515. *
  516. * First, ensure that the first element is a sub-Package. If not,
  517. * the BIOS may have incorrectly returned the object as a single
  518. * package instead of a Package of Packages (a common error if
  519. * there is only one entry). We may be able to repair this by
  520. * wrapping the returned Package with a new outer Package.
  521. */
  522. if (*elements
  523. && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
  524. /* Create the new outer package and populate it */
  525. status =
  526. acpi_ns_repair_package_list(data,
  527. return_object_ptr);
  528. if (ACPI_FAILURE(status)) {
  529. return (status);
  530. }
  531. /* Update locals to point to the new package (of 1 element) */
  532. return_object = *return_object_ptr;
  533. elements = return_object->package.elements;
  534. count = 1;
  535. }
  536. /* Examine the sub-packages */
  537. status =
  538. acpi_ns_check_package_list(data, package, elements, count);
  539. break;
  540. default:
  541. /* Should not get here if predefined info table is correct */
  542. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  543. "Invalid internal return type in table entry: %X",
  544. package->ret_info.type));
  545. return (AE_AML_INTERNAL);
  546. }
  547. return (status);
  548. package_too_small:
  549. /* Error exit for the case with an incorrect package count */
  550. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  551. "Return Package is too small - found %u elements, expected %u",
  552. count, expected_count));
  553. return (AE_AML_OPERAND_VALUE);
  554. }
  555. /*******************************************************************************
  556. *
  557. * FUNCTION: acpi_ns_check_package_list
  558. *
  559. * PARAMETERS: Data - Pointer to validation data structure
  560. * Package - Pointer to package-specific info for method
  561. * Elements - Element list of parent package. All elements
  562. * of this list should be of type Package.
  563. * Count - Count of subpackages
  564. *
  565. * RETURN: Status
  566. *
  567. * DESCRIPTION: Examine a list of subpackages
  568. *
  569. ******************************************************************************/
  570. static acpi_status
  571. acpi_ns_check_package_list(struct acpi_predefined_data *data,
  572. const union acpi_predefined_info *package,
  573. union acpi_operand_object **elements, u32 count)
  574. {
  575. union acpi_operand_object *sub_package;
  576. union acpi_operand_object **sub_elements;
  577. acpi_status status;
  578. u8 non_trailing_null = FALSE;
  579. u32 expected_count;
  580. u32 i;
  581. u32 j;
  582. /* Validate each sub-Package in the parent Package */
  583. for (i = 0; i < count; i++) {
  584. /*
  585. * Handling for NULL package elements. For now, we will simply allow
  586. * a parent package with trailing NULL elements. This can happen if
  587. * the package was defined to be longer than the initializer list.
  588. * This is legal as per the ACPI specification. It is often used
  589. * to allow for dynamic initialization of a Package.
  590. *
  591. * A future enhancement may be to simply truncate the package to
  592. * remove the trailing NULL elements.
  593. */
  594. if (!(*elements)) {
  595. if (!non_trailing_null) {
  596. /* Ensure the remaining elements are all NULL */
  597. for (j = 1; j < (count - i + 1); j++) {
  598. if (elements[j]) {
  599. non_trailing_null = TRUE;
  600. }
  601. }
  602. if (!non_trailing_null) {
  603. /* Ignore the trailing NULL elements */
  604. return (AE_OK);
  605. }
  606. }
  607. /* There are trailing non-null elements, issue warning */
  608. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
  609. data->node_flags,
  610. "Found NULL element at package index %u",
  611. i));
  612. elements++;
  613. continue;
  614. }
  615. sub_package = *elements;
  616. sub_elements = sub_package->package.elements;
  617. /* Each sub-object must be of type Package */
  618. status = acpi_ns_check_object_type(data, &sub_package,
  619. ACPI_RTYPE_PACKAGE, i);
  620. if (ACPI_FAILURE(status)) {
  621. return (status);
  622. }
  623. /* Examine the different types of expected sub-packages */
  624. switch (package->ret_info.type) {
  625. case ACPI_PTYPE2:
  626. case ACPI_PTYPE2_PKG_COUNT:
  627. case ACPI_PTYPE2_REV_FIXED:
  628. /* Each subpackage has a fixed number of elements */
  629. expected_count =
  630. package->ret_info.count1 + package->ret_info.count2;
  631. if (sub_package->package.count < expected_count) {
  632. goto package_too_small;
  633. }
  634. status =
  635. acpi_ns_check_package_elements(data, sub_elements,
  636. package->ret_info.
  637. object_type1,
  638. package->ret_info.
  639. count1,
  640. package->ret_info.
  641. object_type2,
  642. package->ret_info.
  643. count2, 0);
  644. if (ACPI_FAILURE(status)) {
  645. return (status);
  646. }
  647. break;
  648. case ACPI_PTYPE2_FIXED:
  649. /* Each sub-package has a fixed length */
  650. expected_count = package->ret_info2.count;
  651. if (sub_package->package.count < expected_count) {
  652. goto package_too_small;
  653. }
  654. /* Check the type of each sub-package element */
  655. for (j = 0; j < expected_count; j++) {
  656. status =
  657. acpi_ns_check_object_type(data,
  658. &sub_elements[j],
  659. package->
  660. ret_info2.
  661. object_type[j],
  662. j);
  663. if (ACPI_FAILURE(status)) {
  664. return (status);
  665. }
  666. }
  667. break;
  668. case ACPI_PTYPE2_MIN:
  669. /* Each sub-package has a variable but minimum length */
  670. expected_count = package->ret_info.count1;
  671. if (sub_package->package.count < expected_count) {
  672. goto package_too_small;
  673. }
  674. /* Check the type of each sub-package element */
  675. status =
  676. acpi_ns_check_package_elements(data, sub_elements,
  677. package->ret_info.
  678. object_type1,
  679. sub_package->package.
  680. count, 0, 0, 0);
  681. if (ACPI_FAILURE(status)) {
  682. return (status);
  683. }
  684. break;
  685. case ACPI_PTYPE2_COUNT:
  686. /*
  687. * First element is the (Integer) count of elements, including
  688. * the count field.
  689. */
  690. status = acpi_ns_check_object_type(data, sub_elements,
  691. ACPI_RTYPE_INTEGER,
  692. 0);
  693. if (ACPI_FAILURE(status)) {
  694. return (status);
  695. }
  696. /*
  697. * Make sure package is large enough for the Count and is
  698. * is as large as the minimum size
  699. */
  700. expected_count = (u32)(*sub_elements)->integer.value;
  701. if (sub_package->package.count < expected_count) {
  702. goto package_too_small;
  703. }
  704. if (sub_package->package.count <
  705. package->ret_info.count1) {
  706. expected_count = package->ret_info.count1;
  707. goto package_too_small;
  708. }
  709. /* Check the type of each sub-package element */
  710. status =
  711. acpi_ns_check_package_elements(data,
  712. (sub_elements + 1),
  713. package->ret_info.
  714. object_type1,
  715. (expected_count - 1),
  716. 0, 0, 1);
  717. if (ACPI_FAILURE(status)) {
  718. return (status);
  719. }
  720. break;
  721. default: /* Should not get here, type was validated by caller */
  722. return (AE_AML_INTERNAL);
  723. }
  724. elements++;
  725. }
  726. return (AE_OK);
  727. package_too_small:
  728. /* The sub-package count was smaller than required */
  729. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  730. "Return Sub-Package[%u] is too small - found %u elements, expected %u",
  731. i, sub_package->package.count, expected_count));
  732. return (AE_AML_OPERAND_VALUE);
  733. }
  734. /*******************************************************************************
  735. *
  736. * FUNCTION: acpi_ns_check_package_elements
  737. *
  738. * PARAMETERS: Data - Pointer to validation data structure
  739. * Elements - Pointer to the package elements array
  740. * Type1 - Object type for first group
  741. * Count1 - Count for first group
  742. * Type2 - Object type for second group
  743. * Count2 - Count for second group
  744. * start_index - Start of the first group of elements
  745. *
  746. * RETURN: Status
  747. *
  748. * DESCRIPTION: Check that all elements of a package are of the correct object
  749. * type. Supports up to two groups of different object types.
  750. *
  751. ******************************************************************************/
  752. static acpi_status
  753. acpi_ns_check_package_elements(struct acpi_predefined_data *data,
  754. union acpi_operand_object **elements,
  755. u8 type1,
  756. u32 count1,
  757. u8 type2, u32 count2, u32 start_index)
  758. {
  759. union acpi_operand_object **this_element = elements;
  760. acpi_status status;
  761. u32 i;
  762. /*
  763. * Up to two groups of package elements are supported by the data
  764. * structure. All elements in each group must be of the same type.
  765. * The second group can have a count of zero.
  766. */
  767. for (i = 0; i < count1; i++) {
  768. status = acpi_ns_check_object_type(data, this_element,
  769. type1, i + start_index);
  770. if (ACPI_FAILURE(status)) {
  771. return (status);
  772. }
  773. this_element++;
  774. }
  775. for (i = 0; i < count2; i++) {
  776. status = acpi_ns_check_object_type(data, this_element,
  777. type2,
  778. (i + count1 + start_index));
  779. if (ACPI_FAILURE(status)) {
  780. return (status);
  781. }
  782. this_element++;
  783. }
  784. return (AE_OK);
  785. }
  786. /*******************************************************************************
  787. *
  788. * FUNCTION: acpi_ns_check_object_type
  789. *
  790. * PARAMETERS: Data - Pointer to validation data structure
  791. * return_object_ptr - Pointer to the object returned from the
  792. * evaluation of a method or object
  793. * expected_btypes - Bitmap of expected return type(s)
  794. * package_index - Index of object within parent package (if
  795. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  796. * otherwise)
  797. *
  798. * RETURN: Status
  799. *
  800. * DESCRIPTION: Check the type of the return object against the expected object
  801. * type(s). Use of Btype allows multiple expected object types.
  802. *
  803. ******************************************************************************/
  804. static acpi_status
  805. acpi_ns_check_object_type(struct acpi_predefined_data *data,
  806. union acpi_operand_object **return_object_ptr,
  807. u32 expected_btypes, u32 package_index)
  808. {
  809. union acpi_operand_object *return_object = *return_object_ptr;
  810. acpi_status status = AE_OK;
  811. u32 return_btype;
  812. char type_buffer[48]; /* Room for 5 types */
  813. /*
  814. * If we get a NULL return_object here, it is a NULL package element,
  815. * and this is always an error.
  816. */
  817. if (!return_object) {
  818. goto type_error_exit;
  819. }
  820. /* A Namespace node should not get here, but make sure */
  821. if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  822. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  823. "Invalid return type - Found a Namespace node [%4.4s] type %s",
  824. return_object->node.name.ascii,
  825. acpi_ut_get_type_name(return_object->node.
  826. type)));
  827. return (AE_AML_OPERAND_TYPE);
  828. }
  829. /*
  830. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  831. * The bitmapped type allows multiple possible return types.
  832. *
  833. * Note, the cases below must handle all of the possible types returned
  834. * from all of the predefined names (including elements of returned
  835. * packages)
  836. */
  837. switch (return_object->common.type) {
  838. case ACPI_TYPE_INTEGER:
  839. return_btype = ACPI_RTYPE_INTEGER;
  840. break;
  841. case ACPI_TYPE_BUFFER:
  842. return_btype = ACPI_RTYPE_BUFFER;
  843. break;
  844. case ACPI_TYPE_STRING:
  845. return_btype = ACPI_RTYPE_STRING;
  846. break;
  847. case ACPI_TYPE_PACKAGE:
  848. return_btype = ACPI_RTYPE_PACKAGE;
  849. break;
  850. case ACPI_TYPE_LOCAL_REFERENCE:
  851. return_btype = ACPI_RTYPE_REFERENCE;
  852. break;
  853. default:
  854. /* Not one of the supported objects, must be incorrect */
  855. goto type_error_exit;
  856. }
  857. /* Is the object one of the expected types? */
  858. if (!(return_btype & expected_btypes)) {
  859. /* Type mismatch -- attempt repair of the returned object */
  860. status = acpi_ns_repair_object(data, expected_btypes,
  861. package_index,
  862. return_object_ptr);
  863. if (ACPI_SUCCESS(status)) {
  864. return (AE_OK); /* Repair was successful */
  865. }
  866. goto type_error_exit;
  867. }
  868. /* For reference objects, check that the reference type is correct */
  869. if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  870. status = acpi_ns_check_reference(data, return_object);
  871. }
  872. return (status);
  873. type_error_exit:
  874. /* Create a string with all expected types for this predefined object */
  875. acpi_ns_get_expected_types(type_buffer, expected_btypes);
  876. if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
  877. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  878. "Return type mismatch - found %s, expected %s",
  879. acpi_ut_get_object_type_name
  880. (return_object), type_buffer));
  881. } else {
  882. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  883. "Return Package type mismatch at index %u - "
  884. "found %s, expected %s", package_index,
  885. acpi_ut_get_object_type_name
  886. (return_object), type_buffer));
  887. }
  888. return (AE_AML_OPERAND_TYPE);
  889. }
  890. /*******************************************************************************
  891. *
  892. * FUNCTION: acpi_ns_check_reference
  893. *
  894. * PARAMETERS: Data - Pointer to validation data structure
  895. * return_object - Object returned from the evaluation of a
  896. * method or object
  897. *
  898. * RETURN: Status
  899. *
  900. * DESCRIPTION: Check a returned reference object for the correct reference
  901. * type. The only reference type that can be returned from a
  902. * predefined method is a named reference. All others are invalid.
  903. *
  904. ******************************************************************************/
  905. static acpi_status
  906. acpi_ns_check_reference(struct acpi_predefined_data *data,
  907. union acpi_operand_object *return_object)
  908. {
  909. /*
  910. * Check the reference object for the correct reference type (opcode).
  911. * The only type of reference that can be converted to an union acpi_object is
  912. * a reference to a named object (reference class: NAME)
  913. */
  914. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  915. return (AE_OK);
  916. }
  917. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  918. "Return type mismatch - unexpected reference object type [%s] %2.2X",
  919. acpi_ut_get_reference_name(return_object),
  920. return_object->reference.class));
  921. return (AE_AML_OPERAND_TYPE);
  922. }
  923. /*******************************************************************************
  924. *
  925. * FUNCTION: acpi_ns_get_expected_types
  926. *
  927. * PARAMETERS: Buffer - Pointer to where the string is returned
  928. * expected_btypes - Bitmap of expected return type(s)
  929. *
  930. * RETURN: Buffer is populated with type names.
  931. *
  932. * DESCRIPTION: Translate the expected types bitmap into a string of ascii
  933. * names of expected types, for use in warning messages.
  934. *
  935. ******************************************************************************/
  936. static void acpi_ns_get_expected_types(char *buffer, u32 expected_btypes)
  937. {
  938. u32 this_rtype;
  939. u32 i;
  940. u32 j;
  941. j = 1;
  942. buffer[0] = 0;
  943. this_rtype = ACPI_RTYPE_INTEGER;
  944. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  945. /* If one of the expected types, concatenate the name of this type */
  946. if (expected_btypes & this_rtype) {
  947. ACPI_STRCAT(buffer, &acpi_rtype_names[i][j]);
  948. j = 0; /* Use name separator from now on */
  949. }
  950. this_rtype <<= 1; /* Next Rtype */
  951. }
  952. }