nsprepkg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /******************************************************************************
  2. *
  3. * Module Name: nsprepkg - Validation of package objects for predefined names
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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 subpackages
  118. */
  119. switch (package->ret_info.type) {
  120. case ACPI_PTYPE1_FIXED:
  121. /*
  122. * The package count is fixed and there are no subpackages
  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 subpackages, 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 subpackages. 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 subpackages */
  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 subpackages 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 subpackages */
  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 subpackages.
  246. *
  247. * First, ensure that the first element is a subpackage. 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 subpackages */
  268. status =
  269. acpi_ns_check_package_list(info, package, elements, count);
  270. break;
  271. case ACPI_PTYPE2_UUID_PAIR:
  272. /* The package must contain pairs of (UUID + type) */
  273. if (count & 1) {
  274. expected_count = count + 1;
  275. goto package_too_small;
  276. }
  277. while (count > 0) {
  278. status = acpi_ns_check_object_type(info, elements,
  279. package->ret_info.
  280. object_type1, 0);
  281. if (ACPI_FAILURE(status)) {
  282. return (status);
  283. }
  284. /* Validate length of the UUID buffer */
  285. if ((*elements)->buffer.length != 16) {
  286. ACPI_WARN_PREDEFINED((AE_INFO,
  287. info->full_pathname,
  288. info->node_flags,
  289. "Invalid length for UUID Buffer"));
  290. return (AE_AML_OPERAND_VALUE);
  291. }
  292. status = acpi_ns_check_object_type(info, elements + 1,
  293. package->ret_info.
  294. object_type2, 0);
  295. if (ACPI_FAILURE(status)) {
  296. return (status);
  297. }
  298. elements += 2;
  299. count -= 2;
  300. }
  301. break;
  302. default:
  303. /* Should not get here if predefined info table is correct */
  304. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  305. info->node_flags,
  306. "Invalid internal return type in table entry: %X",
  307. package->ret_info.type));
  308. return (AE_AML_INTERNAL);
  309. }
  310. return (status);
  311. package_too_small:
  312. /* Error exit for the case with an incorrect package count */
  313. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  314. "Return Package is too small - found %u elements, expected %u",
  315. count, expected_count));
  316. return (AE_AML_OPERAND_VALUE);
  317. }
  318. /*******************************************************************************
  319. *
  320. * FUNCTION: acpi_ns_check_package_list
  321. *
  322. * PARAMETERS: info - Method execution information block
  323. * package - Pointer to package-specific info for method
  324. * elements - Element list of parent package. All elements
  325. * of this list should be of type Package.
  326. * count - Count of subpackages
  327. *
  328. * RETURN: Status
  329. *
  330. * DESCRIPTION: Examine a list of subpackages
  331. *
  332. ******************************************************************************/
  333. static acpi_status
  334. acpi_ns_check_package_list(struct acpi_evaluate_info *info,
  335. const union acpi_predefined_info *package,
  336. union acpi_operand_object **elements, u32 count)
  337. {
  338. union acpi_operand_object *sub_package;
  339. union acpi_operand_object **sub_elements;
  340. acpi_status status;
  341. u32 expected_count;
  342. u32 i;
  343. u32 j;
  344. /*
  345. * Validate each subpackage in the parent Package
  346. *
  347. * NOTE: assumes list of subpackages contains no NULL elements.
  348. * Any NULL elements should have been removed by earlier call
  349. * to acpi_ns_remove_null_elements.
  350. */
  351. for (i = 0; i < count; i++) {
  352. sub_package = *elements;
  353. sub_elements = sub_package->package.elements;
  354. info->parent_package = sub_package;
  355. /* Each sub-object must be of type Package */
  356. status = acpi_ns_check_object_type(info, &sub_package,
  357. ACPI_RTYPE_PACKAGE, i);
  358. if (ACPI_FAILURE(status)) {
  359. return (status);
  360. }
  361. /* Examine the different types of expected subpackages */
  362. info->parent_package = sub_package;
  363. switch (package->ret_info.type) {
  364. case ACPI_PTYPE2:
  365. case ACPI_PTYPE2_PKG_COUNT:
  366. case ACPI_PTYPE2_REV_FIXED:
  367. /* Each subpackage has a fixed number of elements */
  368. expected_count =
  369. package->ret_info.count1 + package->ret_info.count2;
  370. if (sub_package->package.count < expected_count) {
  371. goto package_too_small;
  372. }
  373. status =
  374. acpi_ns_check_package_elements(info, sub_elements,
  375. package->ret_info.
  376. object_type1,
  377. package->ret_info.
  378. count1,
  379. package->ret_info.
  380. object_type2,
  381. package->ret_info.
  382. count2, 0);
  383. if (ACPI_FAILURE(status)) {
  384. return (status);
  385. }
  386. break;
  387. case ACPI_PTYPE2_FIX_VAR:
  388. /*
  389. * Each subpackage has a fixed number of elements and an
  390. * optional element
  391. */
  392. expected_count =
  393. package->ret_info.count1 + package->ret_info.count2;
  394. if (sub_package->package.count < expected_count) {
  395. goto package_too_small;
  396. }
  397. status =
  398. acpi_ns_check_package_elements(info, sub_elements,
  399. package->ret_info.
  400. object_type1,
  401. package->ret_info.
  402. count1,
  403. package->ret_info.
  404. object_type2,
  405. sub_package->package.
  406. count -
  407. package->ret_info.
  408. count1, 0);
  409. if (ACPI_FAILURE(status)) {
  410. return (status);
  411. }
  412. break;
  413. case ACPI_PTYPE2_FIXED:
  414. /* Each subpackage has a fixed length */
  415. expected_count = package->ret_info2.count;
  416. if (sub_package->package.count < expected_count) {
  417. goto package_too_small;
  418. }
  419. /* Check the type of each subpackage element */
  420. for (j = 0; j < expected_count; j++) {
  421. status =
  422. acpi_ns_check_object_type(info,
  423. &sub_elements[j],
  424. package->
  425. ret_info2.
  426. object_type[j],
  427. j);
  428. if (ACPI_FAILURE(status)) {
  429. return (status);
  430. }
  431. }
  432. break;
  433. case ACPI_PTYPE2_MIN:
  434. /* Each subpackage has a variable but minimum length */
  435. expected_count = package->ret_info.count1;
  436. if (sub_package->package.count < expected_count) {
  437. goto package_too_small;
  438. }
  439. /* Check the type of each subpackage element */
  440. status =
  441. acpi_ns_check_package_elements(info, sub_elements,
  442. package->ret_info.
  443. object_type1,
  444. sub_package->package.
  445. count, 0, 0, 0);
  446. if (ACPI_FAILURE(status)) {
  447. return (status);
  448. }
  449. break;
  450. case ACPI_PTYPE2_COUNT:
  451. /*
  452. * First element is the (Integer) count of elements, including
  453. * the count field (the ACPI name is num_elements)
  454. */
  455. status = acpi_ns_check_object_type(info, sub_elements,
  456. ACPI_RTYPE_INTEGER,
  457. 0);
  458. if (ACPI_FAILURE(status)) {
  459. return (status);
  460. }
  461. /*
  462. * Make sure package is large enough for the Count and is
  463. * is as large as the minimum size
  464. */
  465. expected_count = (u32)(*sub_elements)->integer.value;
  466. if (sub_package->package.count < expected_count) {
  467. goto package_too_small;
  468. }
  469. if (sub_package->package.count <
  470. package->ret_info.count1) {
  471. expected_count = package->ret_info.count1;
  472. goto package_too_small;
  473. }
  474. if (expected_count == 0) {
  475. /*
  476. * Either the num_entries element was originally zero or it was
  477. * a NULL element and repaired to an Integer of value zero.
  478. * In either case, repair it by setting num_entries to be the
  479. * actual size of the subpackage.
  480. */
  481. expected_count = sub_package->package.count;
  482. (*sub_elements)->integer.value = expected_count;
  483. }
  484. /* Check the type of each subpackage element */
  485. status =
  486. acpi_ns_check_package_elements(info,
  487. (sub_elements + 1),
  488. package->ret_info.
  489. object_type1,
  490. (expected_count - 1),
  491. 0, 0, 1);
  492. if (ACPI_FAILURE(status)) {
  493. return (status);
  494. }
  495. break;
  496. default: /* Should not get here, type was validated by caller */
  497. return (AE_AML_INTERNAL);
  498. }
  499. elements++;
  500. }
  501. return (AE_OK);
  502. package_too_small:
  503. /* The subpackage count was smaller than required */
  504. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  505. "Return SubPackage[%u] is too small - found %u elements, expected %u",
  506. i, sub_package->package.count, expected_count));
  507. return (AE_AML_OPERAND_VALUE);
  508. }
  509. /*******************************************************************************
  510. *
  511. * FUNCTION: acpi_ns_check_package_elements
  512. *
  513. * PARAMETERS: info - Method execution information block
  514. * elements - Pointer to the package elements array
  515. * type1 - Object type for first group
  516. * count1 - Count for first group
  517. * type2 - Object type for second group
  518. * count2 - Count for second group
  519. * start_index - Start of the first group of elements
  520. *
  521. * RETURN: Status
  522. *
  523. * DESCRIPTION: Check that all elements of a package are of the correct object
  524. * type. Supports up to two groups of different object types.
  525. *
  526. ******************************************************************************/
  527. static acpi_status
  528. acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
  529. union acpi_operand_object **elements,
  530. u8 type1,
  531. u32 count1,
  532. u8 type2, u32 count2, u32 start_index)
  533. {
  534. union acpi_operand_object **this_element = elements;
  535. acpi_status status;
  536. u32 i;
  537. /*
  538. * Up to two groups of package elements are supported by the data
  539. * structure. All elements in each group must be of the same type.
  540. * The second group can have a count of zero.
  541. */
  542. for (i = 0; i < count1; i++) {
  543. status = acpi_ns_check_object_type(info, this_element,
  544. type1, i + start_index);
  545. if (ACPI_FAILURE(status)) {
  546. return (status);
  547. }
  548. this_element++;
  549. }
  550. for (i = 0; i < count2; i++) {
  551. status = acpi_ns_check_object_type(info, this_element,
  552. type2,
  553. (i + count1 + start_index));
  554. if (ACPI_FAILURE(status)) {
  555. return (status);
  556. }
  557. this_element++;
  558. }
  559. return (AE_OK);
  560. }