nspredef.c 34 KB

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