nspredef.c 34 KB

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