nsprepkg.c 18 KB

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