nsprepkg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /******************************************************************************
  2. *
  3. * Module Name: nsprepkg - Validation of package objects for predefined names
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2013, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acnamesp.h"
  45. #include "acpredef.h"
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsprepkg")
  48. /* Local prototypes */
  49. static acpi_status
  50. acpi_ns_check_package_list(struct acpi_evaluate_info *info,
  51. const union acpi_predefined_info *package,
  52. union acpi_operand_object **elements, u32 count);
  53. static acpi_status
  54. acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
  55. union acpi_operand_object **elements,
  56. u8 type1,
  57. u32 count1,
  58. u8 type2, u32 count2, u32 start_index);
  59. /*******************************************************************************
  60. *
  61. * FUNCTION: acpi_ns_check_package
  62. *
  63. * PARAMETERS: info - Method execution information block
  64. * return_object_ptr - Pointer to the object returned from the
  65. * evaluation of a method or object
  66. *
  67. * RETURN: Status
  68. *
  69. * DESCRIPTION: Check a returned package object for the correct count and
  70. * correct type of all sub-objects.
  71. *
  72. ******************************************************************************/
  73. acpi_status
  74. acpi_ns_check_package(struct acpi_evaluate_info *info,
  75. union acpi_operand_object **return_object_ptr)
  76. {
  77. union acpi_operand_object *return_object = *return_object_ptr;
  78. const union acpi_predefined_info *package;
  79. union acpi_operand_object **elements;
  80. acpi_status status = AE_OK;
  81. u32 expected_count;
  82. u32 count;
  83. u32 i;
  84. ACPI_FUNCTION_NAME(ns_check_package);
  85. /* The package info for this name is in the next table entry */
  86. package = info->predefined + 1;
  87. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  88. "%s Validating return Package of Type %X, Count %X\n",
  89. info->full_pathname, package->ret_info.type,
  90. return_object->package.count));
  91. /*
  92. * For variable-length Packages, we can safely remove all embedded
  93. * and trailing NULL package elements
  94. */
  95. acpi_ns_remove_null_elements(info, package->ret_info.type,
  96. return_object);
  97. /* Extract package count and elements array */
  98. elements = return_object->package.elements;
  99. count = return_object->package.count;
  100. /*
  101. * Most packages must have at least one element. The only exception
  102. * is the variable-length package (ACPI_PTYPE1_VAR).
  103. */
  104. if (!count) {
  105. if (package->ret_info.type == ACPI_PTYPE1_VAR) {
  106. return (AE_OK);
  107. }
  108. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  109. info->node_flags,
  110. "Return Package has no elements (empty)"));
  111. return (AE_AML_OPERAND_VALUE);
  112. }
  113. /*
  114. * Decode the type of the expected package contents
  115. *
  116. * PTYPE1 packages contain no subpackages
  117. * PTYPE2 packages contain sub-packages
  118. */
  119. switch (package->ret_info.type) {
  120. case ACPI_PTYPE1_FIXED:
  121. /*
  122. * The package count is fixed and there are no sub-packages
  123. *
  124. * If package is too small, exit.
  125. * If package is larger than expected, issue warning but continue
  126. */
  127. expected_count =
  128. package->ret_info.count1 + package->ret_info.count2;
  129. if (count < expected_count) {
  130. goto package_too_small;
  131. } else if (count > expected_count) {
  132. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  133. "%s: Return Package is larger than needed - "
  134. "found %u, expected %u\n",
  135. info->full_pathname, count,
  136. expected_count));
  137. }
  138. /* Validate all elements of the returned package */
  139. status = acpi_ns_check_package_elements(info, elements,
  140. package->ret_info.
  141. object_type1,
  142. package->ret_info.
  143. count1,
  144. package->ret_info.
  145. object_type2,
  146. package->ret_info.
  147. count2, 0);
  148. break;
  149. case ACPI_PTYPE1_VAR:
  150. /*
  151. * The package count is variable, there are no sub-packages, and all
  152. * elements must be of the same type
  153. */
  154. for (i = 0; i < count; i++) {
  155. status = acpi_ns_check_object_type(info, elements,
  156. package->ret_info.
  157. object_type1, i);
  158. if (ACPI_FAILURE(status)) {
  159. return (status);
  160. }
  161. elements++;
  162. }
  163. break;
  164. case ACPI_PTYPE1_OPTION:
  165. /*
  166. * The package count is variable, there are no sub-packages. There are
  167. * a fixed number of required elements, and a variable number of
  168. * optional elements.
  169. *
  170. * Check if package is at least as large as the minimum required
  171. */
  172. expected_count = package->ret_info3.count;
  173. if (count < expected_count) {
  174. goto package_too_small;
  175. }
  176. /* Variable number of sub-objects */
  177. for (i = 0; i < count; i++) {
  178. if (i < package->ret_info3.count) {
  179. /* These are the required package elements (0, 1, or 2) */
  180. status =
  181. acpi_ns_check_object_type(info, elements,
  182. package->
  183. ret_info3.
  184. object_type[i],
  185. i);
  186. if (ACPI_FAILURE(status)) {
  187. return (status);
  188. }
  189. } else {
  190. /* These are the optional package elements */
  191. status =
  192. acpi_ns_check_object_type(info, elements,
  193. package->
  194. ret_info3.
  195. tail_object_type,
  196. i);
  197. if (ACPI_FAILURE(status)) {
  198. return (status);
  199. }
  200. }
  201. elements++;
  202. }
  203. break;
  204. case ACPI_PTYPE2_REV_FIXED:
  205. /* First element is the (Integer) revision */
  206. status = acpi_ns_check_object_type(info, elements,
  207. ACPI_RTYPE_INTEGER, 0);
  208. if (ACPI_FAILURE(status)) {
  209. return (status);
  210. }
  211. elements++;
  212. count--;
  213. /* Examine the sub-packages */
  214. status =
  215. acpi_ns_check_package_list(info, package, elements, count);
  216. break;
  217. case ACPI_PTYPE2_PKG_COUNT:
  218. /* First element is the (Integer) count of sub-packages to follow */
  219. status = acpi_ns_check_object_type(info, elements,
  220. ACPI_RTYPE_INTEGER, 0);
  221. if (ACPI_FAILURE(status)) {
  222. return (status);
  223. }
  224. /*
  225. * Count cannot be larger than the parent package length, but allow it
  226. * to be smaller. The >= accounts for the Integer above.
  227. */
  228. expected_count = (u32)(*elements)->integer.value;
  229. if (expected_count >= count) {
  230. goto package_too_small;
  231. }
  232. count = expected_count;
  233. elements++;
  234. /* Examine the sub-packages */
  235. status =
  236. acpi_ns_check_package_list(info, package, elements, count);
  237. break;
  238. case ACPI_PTYPE2:
  239. case ACPI_PTYPE2_FIXED:
  240. case ACPI_PTYPE2_MIN:
  241. case ACPI_PTYPE2_COUNT:
  242. case ACPI_PTYPE2_FIX_VAR:
  243. /*
  244. * These types all return a single Package that consists of a
  245. * variable number of sub-Packages.
  246. *
  247. * First, ensure that the first element is a sub-Package. If not,
  248. * the BIOS may have incorrectly returned the object as a single
  249. * package instead of a Package of Packages (a common error if
  250. * there is only one entry). We may be able to repair this by
  251. * wrapping the returned Package with a new outer Package.
  252. */
  253. if (*elements
  254. && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
  255. /* Create the new outer package and populate it */
  256. status =
  257. acpi_ns_wrap_with_package(info, return_object,
  258. return_object_ptr);
  259. if (ACPI_FAILURE(status)) {
  260. return (status);
  261. }
  262. /* Update locals to point to the new package (of 1 element) */
  263. return_object = *return_object_ptr;
  264. elements = return_object->package.elements;
  265. count = 1;
  266. }
  267. /* Examine the sub-packages */
  268. status =
  269. acpi_ns_check_package_list(info, package, elements, count);
  270. break;
  271. default:
  272. /* Should not get here if predefined info table is correct */
  273. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  274. info->node_flags,
  275. "Invalid internal return type in table entry: %X",
  276. package->ret_info.type));
  277. return (AE_AML_INTERNAL);
  278. }
  279. return (status);
  280. package_too_small:
  281. /* Error exit for the case with an incorrect package count */
  282. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  283. "Return Package is too small - found %u elements, expected %u",
  284. count, expected_count));
  285. return (AE_AML_OPERAND_VALUE);
  286. }
  287. /*******************************************************************************
  288. *
  289. * FUNCTION: acpi_ns_check_package_list
  290. *
  291. * PARAMETERS: info - Method execution information block
  292. * package - Pointer to package-specific info for method
  293. * elements - Element list of parent package. All elements
  294. * of this list should be of type Package.
  295. * count - Count of subpackages
  296. *
  297. * RETURN: Status
  298. *
  299. * DESCRIPTION: Examine a list of subpackages
  300. *
  301. ******************************************************************************/
  302. static acpi_status
  303. acpi_ns_check_package_list(struct acpi_evaluate_info *info,
  304. const union acpi_predefined_info *package,
  305. union acpi_operand_object **elements, u32 count)
  306. {
  307. union acpi_operand_object *sub_package;
  308. union acpi_operand_object **sub_elements;
  309. acpi_status status;
  310. u32 expected_count;
  311. u32 i;
  312. u32 j;
  313. /*
  314. * Validate each sub-Package in the parent Package
  315. *
  316. * NOTE: assumes list of sub-packages contains no NULL elements.
  317. * Any NULL elements should have been removed by earlier call
  318. * to acpi_ns_remove_null_elements.
  319. */
  320. for (i = 0; i < count; i++) {
  321. sub_package = *elements;
  322. sub_elements = sub_package->package.elements;
  323. info->parent_package = sub_package;
  324. /* Each sub-object must be of type Package */
  325. status = acpi_ns_check_object_type(info, &sub_package,
  326. ACPI_RTYPE_PACKAGE, i);
  327. if (ACPI_FAILURE(status)) {
  328. return (status);
  329. }
  330. /* Examine the different types of expected sub-packages */
  331. info->parent_package = sub_package;
  332. switch (package->ret_info.type) {
  333. case ACPI_PTYPE2:
  334. case ACPI_PTYPE2_PKG_COUNT:
  335. case ACPI_PTYPE2_REV_FIXED:
  336. /* Each subpackage has a fixed number of elements */
  337. expected_count =
  338. package->ret_info.count1 + package->ret_info.count2;
  339. if (sub_package->package.count < expected_count) {
  340. goto package_too_small;
  341. }
  342. status =
  343. acpi_ns_check_package_elements(info, sub_elements,
  344. package->ret_info.
  345. object_type1,
  346. package->ret_info.
  347. count1,
  348. package->ret_info.
  349. object_type2,
  350. package->ret_info.
  351. count2, 0);
  352. if (ACPI_FAILURE(status)) {
  353. return (status);
  354. }
  355. break;
  356. case ACPI_PTYPE2_FIX_VAR:
  357. /*
  358. * Each subpackage has a fixed number of elements and an
  359. * optional element
  360. */
  361. expected_count =
  362. package->ret_info.count1 + package->ret_info.count2;
  363. if (sub_package->package.count < expected_count) {
  364. goto package_too_small;
  365. }
  366. status =
  367. acpi_ns_check_package_elements(info, sub_elements,
  368. package->ret_info.
  369. object_type1,
  370. package->ret_info.
  371. count1,
  372. package->ret_info.
  373. object_type2,
  374. sub_package->package.
  375. count -
  376. package->ret_info.
  377. count1, 0);
  378. if (ACPI_FAILURE(status)) {
  379. return (status);
  380. }
  381. break;
  382. case ACPI_PTYPE2_FIXED:
  383. /* Each sub-package has a fixed length */
  384. expected_count = package->ret_info2.count;
  385. if (sub_package->package.count < expected_count) {
  386. goto package_too_small;
  387. }
  388. /* Check the type of each sub-package element */
  389. for (j = 0; j < expected_count; j++) {
  390. status =
  391. acpi_ns_check_object_type(info,
  392. &sub_elements[j],
  393. package->
  394. ret_info2.
  395. object_type[j],
  396. j);
  397. if (ACPI_FAILURE(status)) {
  398. return (status);
  399. }
  400. }
  401. break;
  402. case ACPI_PTYPE2_MIN:
  403. /* Each sub-package has a variable but minimum length */
  404. expected_count = package->ret_info.count1;
  405. if (sub_package->package.count < expected_count) {
  406. goto package_too_small;
  407. }
  408. /* Check the type of each sub-package element */
  409. status =
  410. acpi_ns_check_package_elements(info, sub_elements,
  411. package->ret_info.
  412. object_type1,
  413. sub_package->package.
  414. count, 0, 0, 0);
  415. if (ACPI_FAILURE(status)) {
  416. return (status);
  417. }
  418. break;
  419. case ACPI_PTYPE2_COUNT:
  420. /*
  421. * First element is the (Integer) count of elements, including
  422. * the count field (the ACPI name is num_elements)
  423. */
  424. status = acpi_ns_check_object_type(info, sub_elements,
  425. ACPI_RTYPE_INTEGER,
  426. 0);
  427. if (ACPI_FAILURE(status)) {
  428. return (status);
  429. }
  430. /*
  431. * Make sure package is large enough for the Count and is
  432. * is as large as the minimum size
  433. */
  434. expected_count = (u32)(*sub_elements)->integer.value;
  435. if (sub_package->package.count < expected_count) {
  436. goto package_too_small;
  437. }
  438. if (sub_package->package.count <
  439. package->ret_info.count1) {
  440. expected_count = package->ret_info.count1;
  441. goto package_too_small;
  442. }
  443. if (expected_count == 0) {
  444. /*
  445. * Either the num_entries element was originally zero or it was
  446. * a NULL element and repaired to an Integer of value zero.
  447. * In either case, repair it by setting num_entries to be the
  448. * actual size of the subpackage.
  449. */
  450. expected_count = sub_package->package.count;
  451. (*sub_elements)->integer.value = expected_count;
  452. }
  453. /* Check the type of each sub-package element */
  454. status =
  455. acpi_ns_check_package_elements(info,
  456. (sub_elements + 1),
  457. package->ret_info.
  458. object_type1,
  459. (expected_count - 1),
  460. 0, 0, 1);
  461. if (ACPI_FAILURE(status)) {
  462. return (status);
  463. }
  464. break;
  465. default: /* Should not get here, type was validated by caller */
  466. return (AE_AML_INTERNAL);
  467. }
  468. elements++;
  469. }
  470. return (AE_OK);
  471. package_too_small:
  472. /* The sub-package count was smaller than required */
  473. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  474. "Return Sub-Package[%u] is too small - found %u elements, expected %u",
  475. i, sub_package->package.count, expected_count));
  476. return (AE_AML_OPERAND_VALUE);
  477. }
  478. /*******************************************************************************
  479. *
  480. * FUNCTION: acpi_ns_check_package_elements
  481. *
  482. * PARAMETERS: info - Method execution information block
  483. * elements - Pointer to the package elements array
  484. * type1 - Object type for first group
  485. * count1 - Count for first group
  486. * type2 - Object type for second group
  487. * count2 - Count for second group
  488. * start_index - Start of the first group of elements
  489. *
  490. * RETURN: Status
  491. *
  492. * DESCRIPTION: Check that all elements of a package are of the correct object
  493. * type. Supports up to two groups of different object types.
  494. *
  495. ******************************************************************************/
  496. static acpi_status
  497. acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
  498. union acpi_operand_object **elements,
  499. u8 type1,
  500. u32 count1,
  501. u8 type2, u32 count2, u32 start_index)
  502. {
  503. union acpi_operand_object **this_element = elements;
  504. acpi_status status;
  505. u32 i;
  506. /*
  507. * Up to two groups of package elements are supported by the data
  508. * structure. All elements in each group must be of the same type.
  509. * The second group can have a count of zero.
  510. */
  511. for (i = 0; i < count1; i++) {
  512. status = acpi_ns_check_object_type(info, this_element,
  513. type1, i + start_index);
  514. if (ACPI_FAILURE(status)) {
  515. return (status);
  516. }
  517. this_element++;
  518. }
  519. for (i = 0; i < count2; i++) {
  520. status = acpi_ns_check_object_type(info, this_element,
  521. type2,
  522. (i + count1 + start_index));
  523. if (ACPI_FAILURE(status)) {
  524. return (status);
  525. }
  526. this_element++;
  527. }
  528. return (AE_OK);
  529. }