nspredef.c 30 KB

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