nspredef.c 30 KB

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