nspredef.c 28 KB

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