nspredef.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /******************************************************************************
  2. *
  3. * Module Name: nspredef - Validation of ACPI predefined methods and objects
  4. * $Revision: 1.1 $
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2008, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #include "acpredef.h"
  47. #define _COMPONENT ACPI_NAMESPACE
  48. ACPI_MODULE_NAME("nspredef")
  49. /*******************************************************************************
  50. *
  51. * This module validates predefined ACPI objects that appear in the namespace,
  52. * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
  53. * validation is to detect problems with BIOS-exposed predefined ACPI objects
  54. * before the results are returned to the ACPI-related drivers.
  55. *
  56. * There are several areas that are validated:
  57. *
  58. * 1) The number of input arguments as defined by the method/object in the
  59. * ASL is validated against the ACPI specification.
  60. * 2) The type of the return object (if any) is validated against the ACPI
  61. * specification.
  62. * 3) For returned package objects, the count of package elements is
  63. * validated, as well as the type of each package element. Nested
  64. * packages are supported.
  65. *
  66. * For any problems found, a warning message is issued.
  67. *
  68. ******************************************************************************/
  69. /* Local prototypes */
  70. static acpi_status
  71. acpi_ns_check_package(char *pathname,
  72. union acpi_operand_object **return_object_ptr,
  73. const union acpi_predefined_info *predefined);
  74. static acpi_status
  75. acpi_ns_check_package_elements(char *pathname,
  76. union acpi_operand_object **elements,
  77. u8 type1, u32 count1, u8 type2, u32 count2);
  78. static acpi_status
  79. acpi_ns_check_object_type(char *pathname,
  80. union acpi_operand_object **return_object_ptr,
  81. u32 expected_btypes, u32 package_index);
  82. static acpi_status
  83. acpi_ns_check_reference(char *pathname,
  84. union acpi_operand_object *return_object);
  85. static acpi_status
  86. acpi_ns_repair_object(u32 expected_btypes,
  87. u32 package_index,
  88. union acpi_operand_object **return_object_ptr);
  89. /*
  90. * Names for the types that can be returned by the predefined objects.
  91. * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
  92. */
  93. static const char *acpi_rtype_names[] = {
  94. "/Integer",
  95. "/String",
  96. "/Buffer",
  97. "/Package",
  98. "/Reference",
  99. };
  100. #define ACPI_NOT_PACKAGE ACPI_UINT32_MAX
  101. /*******************************************************************************
  102. *
  103. * FUNCTION: acpi_ns_check_predefined_names
  104. *
  105. * PARAMETERS: Node - Namespace node for the method/object
  106. * return_object_ptr - Pointer to the object returned from the
  107. * evaluation of a method or object
  108. *
  109. * RETURN: Status
  110. *
  111. * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
  112. *
  113. ******************************************************************************/
  114. acpi_status
  115. acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
  116. u32 user_param_count,
  117. acpi_status return_status,
  118. union acpi_operand_object **return_object_ptr)
  119. {
  120. union acpi_operand_object *return_object = *return_object_ptr;
  121. acpi_status status = AE_OK;
  122. const union acpi_predefined_info *predefined;
  123. char *pathname;
  124. /* Match the name for this method/object against the predefined list */
  125. predefined = acpi_ns_check_for_predefined_name(node);
  126. /* Get the full pathname to the object, for use in error messages */
  127. pathname = acpi_ns_get_external_pathname(node);
  128. if (!pathname) {
  129. pathname = ACPI_CAST_PTR(char, predefined->info.name);
  130. }
  131. /*
  132. * Check that the parameter count for this method matches the ASL
  133. * definition. For predefined names, ensure that both the caller and
  134. * the method itself are in accordance with the ACPI specification.
  135. */
  136. acpi_ns_check_parameter_count(pathname, node, user_param_count,
  137. predefined);
  138. /* If not a predefined name, we cannot validate the return object */
  139. if (!predefined) {
  140. goto exit;
  141. }
  142. /* If the method failed, we cannot validate the return object */
  143. if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
  144. goto exit;
  145. }
  146. /*
  147. * Only validate the return value on the first successful evaluation of
  148. * the method. This ensures that any warnings will only be emitted during
  149. * the very first evaluation of the method/object.
  150. */
  151. if (node->flags & ANOBJ_EVALUATED) {
  152. goto exit;
  153. }
  154. /* Mark the node as having been successfully evaluated */
  155. node->flags |= ANOBJ_EVALUATED;
  156. /*
  157. * If there is no return value, check if we require a return value for
  158. * this predefined name. Either one return value is expected, or none,
  159. * for both methods and other objects.
  160. *
  161. * Exit now if there is no return object. Warning if one was expected.
  162. */
  163. if (!return_object) {
  164. if ((predefined->info.expected_btypes) &&
  165. (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
  166. ACPI_ERROR((AE_INFO,
  167. "%s: Missing expected return value",
  168. pathname));
  169. status = AE_AML_NO_RETURN_VALUE;
  170. }
  171. goto exit;
  172. }
  173. /*
  174. * We have a return value, but if one wasn't expected, just exit, this is
  175. * not a problem
  176. *
  177. * For example, if the "Implicit Return" feature is enabled, methods will
  178. * always return a value
  179. */
  180. if (!predefined->info.expected_btypes) {
  181. goto exit;
  182. }
  183. /*
  184. * Check that the type of the return object is what is expected for
  185. * this predefined name
  186. */
  187. status = acpi_ns_check_object_type(pathname, return_object_ptr,
  188. predefined->info.expected_btypes,
  189. ACPI_NOT_PACKAGE);
  190. if (ACPI_FAILURE(status)) {
  191. goto exit;
  192. }
  193. /* For returned Package objects, check the type of all sub-objects */
  194. if (return_object->common.type == ACPI_TYPE_PACKAGE) {
  195. status =
  196. acpi_ns_check_package(pathname, return_object_ptr,
  197. predefined);
  198. }
  199. exit:
  200. if (pathname != predefined->info.name) {
  201. ACPI_FREE(pathname);
  202. }
  203. return (status);
  204. }
  205. /*******************************************************************************
  206. *
  207. * FUNCTION: acpi_ns_check_parameter_count
  208. *
  209. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  210. * Node - Namespace node for the method/object
  211. * user_param_count - Number of args passed in by the caller
  212. * Predefined - Pointer to entry in predefined name table
  213. *
  214. * RETURN: None
  215. *
  216. * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
  217. * predefined name is what is expected (i.e., what is defined in
  218. * the ACPI specification for this predefined name.)
  219. *
  220. ******************************************************************************/
  221. void
  222. acpi_ns_check_parameter_count(char *pathname,
  223. struct acpi_namespace_node *node,
  224. u32 user_param_count,
  225. const union acpi_predefined_info *predefined)
  226. {
  227. u32 param_count;
  228. u32 required_params_current;
  229. u32 required_params_old;
  230. /* Methods have 0-7 parameters. All other types have zero. */
  231. param_count = 0;
  232. if (node->type == ACPI_TYPE_METHOD) {
  233. param_count = node->object->method.param_count;
  234. }
  235. /* Argument count check for non-predefined methods/objects */
  236. if (!predefined) {
  237. /*
  238. * Warning if too few or too many arguments have been passed by the
  239. * caller. An incorrect number of arguments may not cause the method
  240. * to fail. However, the method will fail if there are too few
  241. * arguments and the method attempts to use one of the missing ones.
  242. */
  243. if (user_param_count < param_count) {
  244. ACPI_WARNING((AE_INFO,
  245. "%s: Insufficient arguments - needs %d, found %d",
  246. pathname, param_count, user_param_count));
  247. } else if (user_param_count > param_count) {
  248. ACPI_WARNING((AE_INFO,
  249. "%s: Excess arguments - needs %d, found %d",
  250. pathname, param_count, user_param_count));
  251. }
  252. return;
  253. }
  254. /* Allow two different legal argument counts (_SCP, etc.) */
  255. required_params_current = predefined->info.param_count & 0x0F;
  256. required_params_old = predefined->info.param_count >> 4;
  257. if (user_param_count != ACPI_UINT32_MAX) {
  258. /* Validate the user-supplied parameter count */
  259. if ((user_param_count != required_params_current) &&
  260. (user_param_count != required_params_old)) {
  261. ACPI_WARNING((AE_INFO,
  262. "%s: Parameter count mismatch - "
  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);
  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);
  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. if (ACPI_FAILURE(status)) {
  572. return (status);
  573. }
  574. break;
  575. case ACPI_PTYPE2_COUNT:
  576. /* First element is the (Integer) count of elements to follow */
  577. status =
  578. acpi_ns_check_object_type(pathname,
  579. sub_elements,
  580. ACPI_RTYPE_INTEGER,
  581. 0);
  582. if (ACPI_FAILURE(status)) {
  583. return (status);
  584. }
  585. /* Make sure package is large enough for the Count */
  586. expected_count =
  587. (u32) (*sub_elements)->integer.value;
  588. if (sub_package->package.count < expected_count) {
  589. count = sub_package->package.count;
  590. goto package_too_small;
  591. }
  592. /* Check the type of each sub-package element */
  593. status =
  594. acpi_ns_check_package_elements(pathname,
  595. (sub_elements
  596. + 1),
  597. package->
  598. ret_info.
  599. object_type1,
  600. (expected_count
  601. - 1), 0, 0);
  602. if (ACPI_FAILURE(status)) {
  603. return (status);
  604. }
  605. break;
  606. default:
  607. break;
  608. }
  609. elements++;
  610. }
  611. break;
  612. default:
  613. /* Should not get here if predefined info table is correct */
  614. ACPI_WARNING((AE_INFO,
  615. "%s: Invalid internal return type in table entry: %X",
  616. pathname, package->ret_info.type));
  617. return (AE_AML_INTERNAL);
  618. }
  619. return (AE_OK);
  620. package_too_small:
  621. /* Error exit for the case with an incorrect package count */
  622. ACPI_WARNING((AE_INFO, "%s: Return Package is too small - "
  623. "found %u, expected %u", pathname, count,
  624. expected_count));
  625. return (AE_AML_OPERAND_VALUE);
  626. }
  627. /*******************************************************************************
  628. *
  629. * FUNCTION: acpi_ns_check_package_elements
  630. *
  631. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  632. * Elements - Pointer to the package elements array
  633. * Type1 - Object type for first group
  634. * Count1 - Count for first group
  635. * Type2 - Object type for second group
  636. * Count2 - Count for second group
  637. *
  638. * RETURN: Status
  639. *
  640. * DESCRIPTION: Check that all elements of a package are of the correct object
  641. * type. Supports up to two groups of different object types.
  642. *
  643. ******************************************************************************/
  644. static acpi_status
  645. acpi_ns_check_package_elements(char *pathname,
  646. union acpi_operand_object **elements,
  647. u8 type1, u32 count1, u8 type2, u32 count2)
  648. {
  649. union acpi_operand_object **this_element = elements;
  650. acpi_status status;
  651. u32 i;
  652. /*
  653. * Up to two groups of package elements are supported by the data
  654. * structure. All elements in each group must be of the same type.
  655. * The second group can have a count of zero.
  656. */
  657. for (i = 0; i < count1; i++) {
  658. status = acpi_ns_check_object_type(pathname, this_element,
  659. type1, i);
  660. if (ACPI_FAILURE(status)) {
  661. return (status);
  662. }
  663. this_element++;
  664. }
  665. for (i = 0; i < count2; i++) {
  666. status = acpi_ns_check_object_type(pathname, this_element,
  667. type2, (i + count1));
  668. if (ACPI_FAILURE(status)) {
  669. return (status);
  670. }
  671. this_element++;
  672. }
  673. return (AE_OK);
  674. }
  675. /*******************************************************************************
  676. *
  677. * FUNCTION: acpi_ns_check_object_type
  678. *
  679. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  680. * return_object_ptr - Pointer to the object returned from the
  681. * evaluation of a method or object
  682. * expected_btypes - Bitmap of expected return type(s)
  683. * package_index - Index of object within parent package (if
  684. * applicable - ACPI_NOT_PACKAGE otherwise)
  685. *
  686. * RETURN: Status
  687. *
  688. * DESCRIPTION: Check the type of the return object against the expected object
  689. * type(s). Use of Btype allows multiple expected object types.
  690. *
  691. ******************************************************************************/
  692. static acpi_status
  693. acpi_ns_check_object_type(char *pathname,
  694. union acpi_operand_object **return_object_ptr,
  695. u32 expected_btypes, u32 package_index)
  696. {
  697. union acpi_operand_object *return_object = *return_object_ptr;
  698. acpi_status status = AE_OK;
  699. u32 return_btype;
  700. char type_buffer[48]; /* Room for 5 types */
  701. u32 this_rtype;
  702. u32 i;
  703. u32 j;
  704. /*
  705. * If we get a NULL return_object here, it is a NULL package element,
  706. * and this is always an error.
  707. */
  708. if (!return_object) {
  709. goto type_error_exit;
  710. }
  711. /* A Namespace node should not get here, but make sure */
  712. if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
  713. ACPI_WARNING((AE_INFO,
  714. "%s: Invalid return type - Found a Namespace node [%4.4s] type %s",
  715. pathname, return_object->node.name.ascii,
  716. acpi_ut_get_type_name(return_object->node.type)));
  717. return (AE_AML_OPERAND_TYPE);
  718. }
  719. /*
  720. * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
  721. * The bitmapped type allows multiple possible return types.
  722. *
  723. * Note, the cases below must handle all of the possible types returned
  724. * from all of the predefined names (including elements of returned
  725. * packages)
  726. */
  727. switch (return_object->common.type) {
  728. case ACPI_TYPE_INTEGER:
  729. return_btype = ACPI_RTYPE_INTEGER;
  730. break;
  731. case ACPI_TYPE_BUFFER:
  732. return_btype = ACPI_RTYPE_BUFFER;
  733. break;
  734. case ACPI_TYPE_STRING:
  735. return_btype = ACPI_RTYPE_STRING;
  736. break;
  737. case ACPI_TYPE_PACKAGE:
  738. return_btype = ACPI_RTYPE_PACKAGE;
  739. break;
  740. case ACPI_TYPE_LOCAL_REFERENCE:
  741. return_btype = ACPI_RTYPE_REFERENCE;
  742. break;
  743. default:
  744. /* Not one of the supported objects, must be incorrect */
  745. goto type_error_exit;
  746. }
  747. /* Is the object one of the expected types? */
  748. if (!(return_btype & expected_btypes)) {
  749. /* Type mismatch -- attempt repair of the returned object */
  750. status = acpi_ns_repair_object(expected_btypes, package_index,
  751. return_object_ptr);
  752. if (ACPI_SUCCESS(status)) {
  753. return (status);
  754. }
  755. goto type_error_exit;
  756. }
  757. /* For reference objects, check that the reference type is correct */
  758. if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
  759. status = acpi_ns_check_reference(pathname, return_object);
  760. }
  761. return (status);
  762. type_error_exit:
  763. /* Create a string with all expected types for this predefined object */
  764. j = 1;
  765. type_buffer[0] = 0;
  766. this_rtype = ACPI_RTYPE_INTEGER;
  767. for (i = 0; i < ACPI_NUM_RTYPES; i++) {
  768. /* If one of the expected types, concatenate the name of this type */
  769. if (expected_btypes & this_rtype) {
  770. ACPI_STRCAT(type_buffer, &acpi_rtype_names[i][j]);
  771. j = 0; /* Use name separator from now on */
  772. }
  773. this_rtype <<= 1; /* Next Rtype */
  774. }
  775. if (package_index == ACPI_NOT_PACKAGE) {
  776. ACPI_WARNING((AE_INFO,
  777. "%s: Return type mismatch - found %s, expected %s",
  778. pathname,
  779. acpi_ut_get_object_type_name(return_object),
  780. type_buffer));
  781. } else {
  782. ACPI_WARNING((AE_INFO,
  783. "%s: Return Package type mismatch at index %u - "
  784. "found %s, expected %s", pathname, package_index,
  785. acpi_ut_get_object_type_name(return_object),
  786. type_buffer));
  787. }
  788. return (AE_AML_OPERAND_TYPE);
  789. }
  790. /*******************************************************************************
  791. *
  792. * FUNCTION: acpi_ns_check_reference
  793. *
  794. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  795. * return_object - Object returned from the evaluation of a
  796. * method or object
  797. *
  798. * RETURN: Status
  799. *
  800. * DESCRIPTION: Check a returned reference object for the correct reference
  801. * type. The only reference type that can be returned from a
  802. * predefined method is a named reference. All others are invalid.
  803. *
  804. ******************************************************************************/
  805. static acpi_status
  806. acpi_ns_check_reference(char *pathname,
  807. union acpi_operand_object *return_object)
  808. {
  809. /*
  810. * Check the reference object for the correct reference type (opcode).
  811. * The only type of reference that can be converted to an union acpi_object is
  812. * a reference to a named object (reference class: NAME)
  813. */
  814. if (return_object->reference.class == ACPI_REFCLASS_NAME) {
  815. return (AE_OK);
  816. }
  817. ACPI_WARNING((AE_INFO,
  818. "%s: Return type mismatch - "
  819. "unexpected reference object type [%s] %2.2X",
  820. pathname, acpi_ut_get_reference_name(return_object),
  821. return_object->reference.class));
  822. return (AE_AML_OPERAND_TYPE);
  823. }
  824. /*******************************************************************************
  825. *
  826. * FUNCTION: acpi_ns_repair_object
  827. *
  828. * PARAMETERS: Pathname - Full pathname to the node (for error msgs)
  829. * package_index - Used to determine if target is in a package
  830. * return_object_ptr - Pointer to the object returned from the
  831. * evaluation of a method or object
  832. *
  833. * RETURN: Status. AE_OK if repair was successful.
  834. *
  835. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  836. * not expected.
  837. *
  838. ******************************************************************************/
  839. static acpi_status
  840. acpi_ns_repair_object(u32 expected_btypes,
  841. u32 package_index,
  842. union acpi_operand_object **return_object_ptr)
  843. {
  844. union acpi_operand_object *return_object = *return_object_ptr;
  845. union acpi_operand_object *new_object;
  846. acpi_size length;
  847. switch (return_object->common.type) {
  848. case ACPI_TYPE_BUFFER:
  849. if (!(expected_btypes & ACPI_RTYPE_STRING)) {
  850. return (AE_AML_OPERAND_TYPE);
  851. }
  852. /*
  853. * Have a Buffer, expected a String, convert. Use a to_string
  854. * conversion, no transform performed on the buffer data. The best
  855. * example of this is the _BIF method, where the string data from
  856. * the battery is often (incorrectly) returned as buffer object(s).
  857. */
  858. length = 0;
  859. while ((length < return_object->buffer.length) &&
  860. (return_object->buffer.pointer[length])) {
  861. length++;
  862. }
  863. /* Allocate a new string object */
  864. new_object = acpi_ut_create_string_object(length);
  865. if (!new_object) {
  866. return (AE_NO_MEMORY);
  867. }
  868. /*
  869. * Copy the raw buffer data with no transform. String is already NULL
  870. * terminated at Length+1.
  871. */
  872. ACPI_MEMCPY(new_object->string.pointer,
  873. return_object->buffer.pointer, length);
  874. /* Install the new return object */
  875. acpi_ut_remove_reference(return_object);
  876. *return_object_ptr = new_object;
  877. /*
  878. * If the object is a package element, we need to:
  879. * 1. Decrement the reference count of the orignal object, it was
  880. * incremented when building the package
  881. * 2. Increment the reference count of the new object, it will be
  882. * decremented when releasing the package
  883. */
  884. if (package_index != ACPI_NOT_PACKAGE) {
  885. acpi_ut_remove_reference(return_object);
  886. acpi_ut_add_reference(new_object);
  887. }
  888. return (AE_OK);
  889. default:
  890. break;
  891. }
  892. return (AE_AML_OPERAND_TYPE);
  893. }