nsprepkg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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_predefined_data *data,
  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_predefined_data *data,
  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: data - Pointer to validation data structure
  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_predefined_data *data,
  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 = data->predefined + 1;
  87. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  88. "%s Validating return Package of Type %X, Count %X\n",
  89. data->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(data, 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, data->pathname, data->node_flags,
  109. "Return Package has no elements (empty)"));
  110. return (AE_AML_OPERAND_VALUE);
  111. }
  112. /*
  113. * Decode the type of the expected package contents
  114. *
  115. * PTYPE1 packages contain no subpackages
  116. * PTYPE2 packages contain sub-packages
  117. */
  118. switch (package->ret_info.type) {
  119. case ACPI_PTYPE1_FIXED:
  120. /*
  121. * The package count is fixed and there are no sub-packages
  122. *
  123. * If package is too small, exit.
  124. * If package is larger than expected, issue warning but continue
  125. */
  126. expected_count =
  127. package->ret_info.count1 + package->ret_info.count2;
  128. if (count < expected_count) {
  129. goto package_too_small;
  130. } else if (count > expected_count) {
  131. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  132. "%s: Return Package is larger than needed - "
  133. "found %u, expected %u\n",
  134. data->pathname, count,
  135. expected_count));
  136. }
  137. /* Validate all elements of the returned package */
  138. status = acpi_ns_check_package_elements(data, elements,
  139. package->ret_info.
  140. object_type1,
  141. package->ret_info.
  142. count1,
  143. package->ret_info.
  144. object_type2,
  145. package->ret_info.
  146. count2, 0);
  147. break;
  148. case ACPI_PTYPE1_VAR:
  149. /*
  150. * The package count is variable, there are no sub-packages, and all
  151. * elements must be of the same type
  152. */
  153. for (i = 0; i < count; i++) {
  154. status = acpi_ns_check_object_type(data, elements,
  155. package->ret_info.
  156. object_type1, i);
  157. if (ACPI_FAILURE(status)) {
  158. return (status);
  159. }
  160. elements++;
  161. }
  162. break;
  163. case ACPI_PTYPE1_OPTION:
  164. /*
  165. * The package count is variable, there are no sub-packages. There are
  166. * a fixed number of required elements, and a variable number of
  167. * optional elements.
  168. *
  169. * Check if package is at least as large as the minimum required
  170. */
  171. expected_count = package->ret_info3.count;
  172. if (count < expected_count) {
  173. goto package_too_small;
  174. }
  175. /* Variable number of sub-objects */
  176. for (i = 0; i < count; i++) {
  177. if (i < package->ret_info3.count) {
  178. /* These are the required package elements (0, 1, or 2) */
  179. status =
  180. acpi_ns_check_object_type(data, elements,
  181. package->
  182. ret_info3.
  183. object_type[i],
  184. i);
  185. if (ACPI_FAILURE(status)) {
  186. return (status);
  187. }
  188. } else {
  189. /* These are the optional package elements */
  190. status =
  191. acpi_ns_check_object_type(data, elements,
  192. package->
  193. ret_info3.
  194. tail_object_type,
  195. i);
  196. if (ACPI_FAILURE(status)) {
  197. return (status);
  198. }
  199. }
  200. elements++;
  201. }
  202. break;
  203. case ACPI_PTYPE2_REV_FIXED:
  204. /* First element is the (Integer) revision */
  205. status = acpi_ns_check_object_type(data, elements,
  206. ACPI_RTYPE_INTEGER, 0);
  207. if (ACPI_FAILURE(status)) {
  208. return (status);
  209. }
  210. elements++;
  211. count--;
  212. /* Examine the sub-packages */
  213. status =
  214. acpi_ns_check_package_list(data, package, elements, count);
  215. break;
  216. case ACPI_PTYPE2_PKG_COUNT:
  217. /* First element is the (Integer) count of sub-packages to follow */
  218. status = acpi_ns_check_object_type(data, elements,
  219. ACPI_RTYPE_INTEGER, 0);
  220. if (ACPI_FAILURE(status)) {
  221. return (status);
  222. }
  223. /*
  224. * Count cannot be larger than the parent package length, but allow it
  225. * to be smaller. The >= accounts for the Integer above.
  226. */
  227. expected_count = (u32)(*elements)->integer.value;
  228. if (expected_count >= count) {
  229. goto package_too_small;
  230. }
  231. count = expected_count;
  232. elements++;
  233. /* Examine the sub-packages */
  234. status =
  235. acpi_ns_check_package_list(data, package, elements, count);
  236. break;
  237. case ACPI_PTYPE2:
  238. case ACPI_PTYPE2_FIXED:
  239. case ACPI_PTYPE2_MIN:
  240. case ACPI_PTYPE2_COUNT:
  241. case ACPI_PTYPE2_FIX_VAR:
  242. /*
  243. * These types all return a single Package that consists of a
  244. * variable number of sub-Packages.
  245. *
  246. * First, ensure that the first element is a sub-Package. If not,
  247. * the BIOS may have incorrectly returned the object as a single
  248. * package instead of a Package of Packages (a common error if
  249. * there is only one entry). We may be able to repair this by
  250. * wrapping the returned Package with a new outer Package.
  251. */
  252. if (*elements
  253. && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
  254. /* Create the new outer package and populate it */
  255. status =
  256. acpi_ns_wrap_with_package(data, return_object,
  257. return_object_ptr);
  258. if (ACPI_FAILURE(status)) {
  259. return (status);
  260. }
  261. /* Update locals to point to the new package (of 1 element) */
  262. return_object = *return_object_ptr;
  263. elements = return_object->package.elements;
  264. count = 1;
  265. }
  266. /* Examine the sub-packages */
  267. status =
  268. acpi_ns_check_package_list(data, package, elements, count);
  269. break;
  270. default:
  271. /* Should not get here if predefined info table is correct */
  272. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  273. "Invalid internal return type in table entry: %X",
  274. package->ret_info.type));
  275. return (AE_AML_INTERNAL);
  276. }
  277. return (status);
  278. package_too_small:
  279. /* Error exit for the case with an incorrect package count */
  280. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  281. "Return Package is too small - found %u elements, expected %u",
  282. count, expected_count));
  283. return (AE_AML_OPERAND_VALUE);
  284. }
  285. /*******************************************************************************
  286. *
  287. * FUNCTION: acpi_ns_check_package_list
  288. *
  289. * PARAMETERS: data - Pointer to validation data structure
  290. * package - Pointer to package-specific info for method
  291. * elements - Element list of parent package. All elements
  292. * of this list should be of type Package.
  293. * count - Count of subpackages
  294. *
  295. * RETURN: Status
  296. *
  297. * DESCRIPTION: Examine a list of subpackages
  298. *
  299. ******************************************************************************/
  300. static acpi_status
  301. acpi_ns_check_package_list(struct acpi_predefined_data *data,
  302. const union acpi_predefined_info *package,
  303. union acpi_operand_object **elements, u32 count)
  304. {
  305. union acpi_operand_object *sub_package;
  306. union acpi_operand_object **sub_elements;
  307. acpi_status status;
  308. u32 expected_count;
  309. u32 i;
  310. u32 j;
  311. /*
  312. * Validate each sub-Package in the parent Package
  313. *
  314. * NOTE: assumes list of sub-packages contains no NULL elements.
  315. * Any NULL elements should have been removed by earlier call
  316. * to acpi_ns_remove_null_elements.
  317. */
  318. for (i = 0; i < count; i++) {
  319. sub_package = *elements;
  320. sub_elements = sub_package->package.elements;
  321. data->parent_package = sub_package;
  322. /* Each sub-object must be of type Package */
  323. status = acpi_ns_check_object_type(data, &sub_package,
  324. ACPI_RTYPE_PACKAGE, i);
  325. if (ACPI_FAILURE(status)) {
  326. return (status);
  327. }
  328. /* Examine the different types of expected sub-packages */
  329. data->parent_package = sub_package;
  330. switch (package->ret_info.type) {
  331. case ACPI_PTYPE2:
  332. case ACPI_PTYPE2_PKG_COUNT:
  333. case ACPI_PTYPE2_REV_FIXED:
  334. /* Each subpackage has a fixed number of elements */
  335. expected_count =
  336. package->ret_info.count1 + package->ret_info.count2;
  337. if (sub_package->package.count < expected_count) {
  338. goto package_too_small;
  339. }
  340. status =
  341. acpi_ns_check_package_elements(data, sub_elements,
  342. package->ret_info.
  343. object_type1,
  344. package->ret_info.
  345. count1,
  346. package->ret_info.
  347. object_type2,
  348. package->ret_info.
  349. count2, 0);
  350. if (ACPI_FAILURE(status)) {
  351. return (status);
  352. }
  353. break;
  354. case ACPI_PTYPE2_FIX_VAR:
  355. /*
  356. * Each subpackage has a fixed number of elements and an
  357. * optional element
  358. */
  359. expected_count =
  360. package->ret_info.count1 + package->ret_info.count2;
  361. if (sub_package->package.count < expected_count) {
  362. goto package_too_small;
  363. }
  364. status =
  365. acpi_ns_check_package_elements(data, sub_elements,
  366. package->ret_info.
  367. object_type1,
  368. package->ret_info.
  369. count1,
  370. package->ret_info.
  371. object_type2,
  372. sub_package->package.
  373. count -
  374. package->ret_info.
  375. count1, 0);
  376. if (ACPI_FAILURE(status)) {
  377. return (status);
  378. }
  379. break;
  380. case ACPI_PTYPE2_FIXED:
  381. /* Each sub-package has a fixed length */
  382. expected_count = package->ret_info2.count;
  383. if (sub_package->package.count < expected_count) {
  384. goto package_too_small;
  385. }
  386. /* Check the type of each sub-package element */
  387. for (j = 0; j < expected_count; j++) {
  388. status =
  389. acpi_ns_check_object_type(data,
  390. &sub_elements[j],
  391. package->
  392. ret_info2.
  393. object_type[j],
  394. j);
  395. if (ACPI_FAILURE(status)) {
  396. return (status);
  397. }
  398. }
  399. break;
  400. case ACPI_PTYPE2_MIN:
  401. /* Each sub-package has a variable but minimum length */
  402. expected_count = package->ret_info.count1;
  403. if (sub_package->package.count < expected_count) {
  404. goto package_too_small;
  405. }
  406. /* Check the type of each sub-package element */
  407. status =
  408. acpi_ns_check_package_elements(data, sub_elements,
  409. package->ret_info.
  410. object_type1,
  411. sub_package->package.
  412. count, 0, 0, 0);
  413. if (ACPI_FAILURE(status)) {
  414. return (status);
  415. }
  416. break;
  417. case ACPI_PTYPE2_COUNT:
  418. /*
  419. * First element is the (Integer) count of elements, including
  420. * the count field (the ACPI name is num_elements)
  421. */
  422. status = acpi_ns_check_object_type(data, sub_elements,
  423. ACPI_RTYPE_INTEGER,
  424. 0);
  425. if (ACPI_FAILURE(status)) {
  426. return (status);
  427. }
  428. /*
  429. * Make sure package is large enough for the Count and is
  430. * is as large as the minimum size
  431. */
  432. expected_count = (u32)(*sub_elements)->integer.value;
  433. if (sub_package->package.count < expected_count) {
  434. goto package_too_small;
  435. }
  436. if (sub_package->package.count <
  437. package->ret_info.count1) {
  438. expected_count = package->ret_info.count1;
  439. goto package_too_small;
  440. }
  441. if (expected_count == 0) {
  442. /*
  443. * Either the num_entries element was originally zero or it was
  444. * a NULL element and repaired to an Integer of value zero.
  445. * In either case, repair it by setting num_entries to be the
  446. * actual size of the subpackage.
  447. */
  448. expected_count = sub_package->package.count;
  449. (*sub_elements)->integer.value = expected_count;
  450. }
  451. /* Check the type of each sub-package element */
  452. status =
  453. acpi_ns_check_package_elements(data,
  454. (sub_elements + 1),
  455. package->ret_info.
  456. object_type1,
  457. (expected_count - 1),
  458. 0, 0, 1);
  459. if (ACPI_FAILURE(status)) {
  460. return (status);
  461. }
  462. break;
  463. default: /* Should not get here, type was validated by caller */
  464. return (AE_AML_INTERNAL);
  465. }
  466. elements++;
  467. }
  468. return (AE_OK);
  469. package_too_small:
  470. /* The sub-package count was smaller than required */
  471. ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  472. "Return Sub-Package[%u] is too small - found %u elements, expected %u",
  473. i, sub_package->package.count, expected_count));
  474. return (AE_AML_OPERAND_VALUE);
  475. }
  476. /*******************************************************************************
  477. *
  478. * FUNCTION: acpi_ns_check_package_elements
  479. *
  480. * PARAMETERS: data - Pointer to validation data structure
  481. * elements - Pointer to the package elements array
  482. * type1 - Object type for first group
  483. * count1 - Count for first group
  484. * type2 - Object type for second group
  485. * count2 - Count for second group
  486. * start_index - Start of the first group of elements
  487. *
  488. * RETURN: Status
  489. *
  490. * DESCRIPTION: Check that all elements of a package are of the correct object
  491. * type. Supports up to two groups of different object types.
  492. *
  493. ******************************************************************************/
  494. static acpi_status
  495. acpi_ns_check_package_elements(struct acpi_predefined_data *data,
  496. union acpi_operand_object **elements,
  497. u8 type1,
  498. u32 count1,
  499. u8 type2, u32 count2, u32 start_index)
  500. {
  501. union acpi_operand_object **this_element = elements;
  502. acpi_status status;
  503. u32 i;
  504. /*
  505. * Up to two groups of package elements are supported by the data
  506. * structure. All elements in each group must be of the same type.
  507. * The second group can have a count of zero.
  508. */
  509. for (i = 0; i < count1; i++) {
  510. status = acpi_ns_check_object_type(data, this_element,
  511. type1, i + start_index);
  512. if (ACPI_FAILURE(status)) {
  513. return (status);
  514. }
  515. this_element++;
  516. }
  517. for (i = 0; i < count2; i++) {
  518. status = acpi_ns_check_object_type(data, this_element,
  519. type2,
  520. (i + count1 + start_index));
  521. if (ACPI_FAILURE(status)) {
  522. return (status);
  523. }
  524. this_element++;
  525. }
  526. return (AE_OK);
  527. }