nspredef.c 30 KB

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