nsprepkg.c 17 KB

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