nspredef.c 32 KB

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