nsprepkg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsprepkg - Validation of package objects for predefined names
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acnamesp.h"
  12. #include "acpredef.h"
  13. #define _COMPONENT ACPI_NAMESPACE
  14. ACPI_MODULE_NAME("nsprepkg")
  15. /* Local prototypes */
  16. static acpi_status
  17. acpi_ns_check_package_list(struct acpi_evaluate_info *info,
  18. const union acpi_predefined_info *package,
  19. union acpi_operand_object **elements, u32 count);
  20. static acpi_status
  21. acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
  22. union acpi_operand_object **elements,
  23. u8 type1,
  24. u32 count1,
  25. u8 type2, u32 count2, u32 start_index);
  26. static acpi_status
  27. acpi_ns_custom_package(struct acpi_evaluate_info *info,
  28. union acpi_operand_object **elements, u32 count);
  29. /*******************************************************************************
  30. *
  31. * FUNCTION: acpi_ns_check_package
  32. *
  33. * PARAMETERS: info - Method execution information block
  34. * return_object_ptr - Pointer to the object returned from the
  35. * evaluation of a method or object
  36. *
  37. * RETURN: Status
  38. *
  39. * DESCRIPTION: Check a returned package object for the correct count and
  40. * correct type of all sub-objects.
  41. *
  42. ******************************************************************************/
  43. acpi_status
  44. acpi_ns_check_package(struct acpi_evaluate_info *info,
  45. union acpi_operand_object **return_object_ptr)
  46. {
  47. union acpi_operand_object *return_object = *return_object_ptr;
  48. const union acpi_predefined_info *package;
  49. union acpi_operand_object **elements;
  50. acpi_status status = AE_OK;
  51. u32 expected_count;
  52. u32 count;
  53. u32 i;
  54. ACPI_FUNCTION_TRACE(ns_check_package);
  55. /* The package info for this name is in the next table entry */
  56. package = info->predefined + 1;
  57. ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
  58. "%s Validating return Package of Type %X, Count %X\n",
  59. info->full_pathname, package->ret_info.type,
  60. return_object->package.count));
  61. /*
  62. * For variable-length Packages, we can safely remove all embedded
  63. * and trailing NULL package elements
  64. */
  65. acpi_ns_remove_null_elements(info, package->ret_info.type,
  66. return_object);
  67. /* Extract package count and elements array */
  68. elements = return_object->package.elements;
  69. count = return_object->package.count;
  70. /*
  71. * Most packages must have at least one element. The only exception
  72. * is the variable-length package (ACPI_PTYPE1_VAR).
  73. */
  74. if (!count) {
  75. if (package->ret_info.type == ACPI_PTYPE1_VAR) {
  76. return_ACPI_STATUS(AE_OK);
  77. }
  78. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  79. info->node_flags,
  80. "Return Package has no elements (empty)"));
  81. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  82. }
  83. /*
  84. * Decode the type of the expected package contents
  85. *
  86. * PTYPE1 packages contain no subpackages
  87. * PTYPE2 packages contain subpackages
  88. */
  89. switch (package->ret_info.type) {
  90. case ACPI_PTYPE_CUSTOM:
  91. status = acpi_ns_custom_package(info, elements, count);
  92. break;
  93. case ACPI_PTYPE1_FIXED:
  94. /*
  95. * The package count is fixed and there are no subpackages
  96. *
  97. * If package is too small, exit.
  98. * If package is larger than expected, issue warning but continue
  99. */
  100. expected_count =
  101. package->ret_info.count1 + package->ret_info.count2;
  102. if (count < expected_count) {
  103. goto package_too_small;
  104. } else if (count > expected_count) {
  105. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  106. "%s: Return Package is larger than needed - "
  107. "found %u, expected %u\n",
  108. info->full_pathname, count,
  109. expected_count));
  110. }
  111. /* Validate all elements of the returned package */
  112. status = acpi_ns_check_package_elements(info, elements,
  113. package->ret_info.
  114. object_type1,
  115. package->ret_info.
  116. count1,
  117. package->ret_info.
  118. object_type2,
  119. package->ret_info.
  120. count2, 0);
  121. break;
  122. case ACPI_PTYPE1_VAR:
  123. /*
  124. * The package count is variable, there are no subpackages, and all
  125. * elements must be of the same type
  126. */
  127. for (i = 0; i < count; i++) {
  128. status = acpi_ns_check_object_type(info, elements,
  129. package->ret_info.
  130. object_type1, i);
  131. if (ACPI_FAILURE(status)) {
  132. return_ACPI_STATUS(status);
  133. }
  134. elements++;
  135. }
  136. break;
  137. case ACPI_PTYPE1_OPTION:
  138. /*
  139. * The package count is variable, there are no subpackages. There are
  140. * a fixed number of required elements, and a variable number of
  141. * optional elements.
  142. *
  143. * Check if package is at least as large as the minimum required
  144. */
  145. expected_count = package->ret_info3.count;
  146. if (count < expected_count) {
  147. goto package_too_small;
  148. }
  149. /* Variable number of sub-objects */
  150. for (i = 0; i < count; i++) {
  151. if (i < package->ret_info3.count) {
  152. /* These are the required package elements (0, 1, or 2) */
  153. status =
  154. acpi_ns_check_object_type(info, elements,
  155. package->
  156. ret_info3.
  157. object_type[i],
  158. i);
  159. if (ACPI_FAILURE(status)) {
  160. return_ACPI_STATUS(status);
  161. }
  162. } else {
  163. /* These are the optional package elements */
  164. status =
  165. acpi_ns_check_object_type(info, elements,
  166. package->
  167. ret_info3.
  168. tail_object_type,
  169. i);
  170. if (ACPI_FAILURE(status)) {
  171. return_ACPI_STATUS(status);
  172. }
  173. }
  174. elements++;
  175. }
  176. break;
  177. case ACPI_PTYPE2_REV_FIXED:
  178. /* First element is the (Integer) revision */
  179. status =
  180. acpi_ns_check_object_type(info, elements,
  181. ACPI_RTYPE_INTEGER, 0);
  182. if (ACPI_FAILURE(status)) {
  183. return_ACPI_STATUS(status);
  184. }
  185. elements++;
  186. count--;
  187. /* Examine the subpackages */
  188. status =
  189. acpi_ns_check_package_list(info, package, elements, count);
  190. break;
  191. case ACPI_PTYPE2_PKG_COUNT:
  192. /* First element is the (Integer) count of subpackages to follow */
  193. status =
  194. acpi_ns_check_object_type(info, elements,
  195. ACPI_RTYPE_INTEGER, 0);
  196. if (ACPI_FAILURE(status)) {
  197. return_ACPI_STATUS(status);
  198. }
  199. /*
  200. * Count cannot be larger than the parent package length, but allow it
  201. * to be smaller. The >= accounts for the Integer above.
  202. */
  203. expected_count = (u32)(*elements)->integer.value;
  204. if (expected_count >= count) {
  205. goto package_too_small;
  206. }
  207. count = expected_count;
  208. elements++;
  209. /* Examine the subpackages */
  210. status =
  211. acpi_ns_check_package_list(info, package, elements, count);
  212. break;
  213. case ACPI_PTYPE2:
  214. case ACPI_PTYPE2_FIXED:
  215. case ACPI_PTYPE2_MIN:
  216. case ACPI_PTYPE2_COUNT:
  217. case ACPI_PTYPE2_FIX_VAR:
  218. /*
  219. * These types all return a single Package that consists of a
  220. * variable number of subpackages.
  221. *
  222. * First, ensure that the first element is a subpackage. If not,
  223. * the BIOS may have incorrectly returned the object as a single
  224. * package instead of a Package of Packages (a common error if
  225. * there is only one entry). We may be able to repair this by
  226. * wrapping the returned Package with a new outer Package.
  227. */
  228. if (*elements
  229. && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
  230. /* Create the new outer package and populate it */
  231. status =
  232. acpi_ns_wrap_with_package(info, return_object,
  233. return_object_ptr);
  234. if (ACPI_FAILURE(status)) {
  235. return_ACPI_STATUS(status);
  236. }
  237. /* Update locals to point to the new package (of 1 element) */
  238. return_object = *return_object_ptr;
  239. elements = return_object->package.elements;
  240. count = 1;
  241. }
  242. /* Examine the subpackages */
  243. status =
  244. acpi_ns_check_package_list(info, package, elements, count);
  245. break;
  246. case ACPI_PTYPE2_VAR_VAR:
  247. /*
  248. * Returns a variable list of packages, each with a variable list
  249. * of objects.
  250. */
  251. break;
  252. case ACPI_PTYPE2_UUID_PAIR:
  253. /* The package must contain pairs of (UUID + type) */
  254. if (count & 1) {
  255. expected_count = count + 1;
  256. goto package_too_small;
  257. }
  258. while (count > 0) {
  259. status = acpi_ns_check_object_type(info, elements,
  260. package->ret_info.
  261. object_type1, 0);
  262. if (ACPI_FAILURE(status)) {
  263. return_ACPI_STATUS(status);
  264. }
  265. /* Validate length of the UUID buffer */
  266. if ((*elements)->buffer.length != 16) {
  267. ACPI_WARN_PREDEFINED((AE_INFO,
  268. info->full_pathname,
  269. info->node_flags,
  270. "Invalid length for UUID Buffer"));
  271. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  272. }
  273. status = acpi_ns_check_object_type(info, elements + 1,
  274. package->ret_info.
  275. object_type2, 0);
  276. if (ACPI_FAILURE(status)) {
  277. return_ACPI_STATUS(status);
  278. }
  279. elements += 2;
  280. count -= 2;
  281. }
  282. break;
  283. default:
  284. /* Should not get here if predefined info table is correct */
  285. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  286. info->node_flags,
  287. "Invalid internal return type in table entry: %X",
  288. package->ret_info.type));
  289. return_ACPI_STATUS(AE_AML_INTERNAL);
  290. }
  291. return_ACPI_STATUS(status);
  292. package_too_small:
  293. /* Error exit for the case with an incorrect package count */
  294. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  295. "Return Package is too small - found %u elements, expected %u",
  296. count, expected_count));
  297. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  298. }
  299. /*******************************************************************************
  300. *
  301. * FUNCTION: acpi_ns_check_package_list
  302. *
  303. * PARAMETERS: info - Method execution information block
  304. * package - Pointer to package-specific info for method
  305. * elements - Element list of parent package. All elements
  306. * of this list should be of type Package.
  307. * count - Count of subpackages
  308. *
  309. * RETURN: Status
  310. *
  311. * DESCRIPTION: Examine a list of subpackages
  312. *
  313. ******************************************************************************/
  314. static acpi_status
  315. acpi_ns_check_package_list(struct acpi_evaluate_info *info,
  316. const union acpi_predefined_info *package,
  317. union acpi_operand_object **elements, u32 count)
  318. {
  319. union acpi_operand_object *sub_package;
  320. union acpi_operand_object **sub_elements;
  321. acpi_status status;
  322. u32 expected_count;
  323. u32 i;
  324. u32 j;
  325. /*
  326. * Validate each subpackage in the parent Package
  327. *
  328. * NOTE: assumes list of subpackages contains no NULL elements.
  329. * Any NULL elements should have been removed by earlier call
  330. * to acpi_ns_remove_null_elements.
  331. */
  332. for (i = 0; i < count; i++) {
  333. sub_package = *elements;
  334. sub_elements = sub_package->package.elements;
  335. info->parent_package = sub_package;
  336. /* Each sub-object must be of type Package */
  337. status = acpi_ns_check_object_type(info, &sub_package,
  338. ACPI_RTYPE_PACKAGE, i);
  339. if (ACPI_FAILURE(status)) {
  340. return (status);
  341. }
  342. /* Examine the different types of expected subpackages */
  343. info->parent_package = sub_package;
  344. switch (package->ret_info.type) {
  345. case ACPI_PTYPE2:
  346. case ACPI_PTYPE2_PKG_COUNT:
  347. case ACPI_PTYPE2_REV_FIXED:
  348. /* Each subpackage has a fixed number of elements */
  349. expected_count =
  350. package->ret_info.count1 + package->ret_info.count2;
  351. if (sub_package->package.count < expected_count) {
  352. goto package_too_small;
  353. }
  354. status =
  355. acpi_ns_check_package_elements(info, sub_elements,
  356. package->ret_info.
  357. object_type1,
  358. package->ret_info.
  359. count1,
  360. package->ret_info.
  361. object_type2,
  362. package->ret_info.
  363. count2, 0);
  364. if (ACPI_FAILURE(status)) {
  365. return (status);
  366. }
  367. break;
  368. case ACPI_PTYPE2_FIX_VAR:
  369. /*
  370. * Each subpackage has a fixed number of elements and an
  371. * optional element
  372. */
  373. expected_count =
  374. package->ret_info.count1 + package->ret_info.count2;
  375. if (sub_package->package.count < expected_count) {
  376. goto package_too_small;
  377. }
  378. status =
  379. acpi_ns_check_package_elements(info, sub_elements,
  380. package->ret_info.
  381. object_type1,
  382. package->ret_info.
  383. count1,
  384. package->ret_info.
  385. object_type2,
  386. sub_package->package.
  387. count -
  388. package->ret_info.
  389. count1, 0);
  390. if (ACPI_FAILURE(status)) {
  391. return (status);
  392. }
  393. break;
  394. case ACPI_PTYPE2_VAR_VAR:
  395. /*
  396. * Each subpackage has a fixed or variable number of elements
  397. */
  398. break;
  399. case ACPI_PTYPE2_FIXED:
  400. /* Each subpackage has a fixed length */
  401. expected_count = package->ret_info2.count;
  402. if (sub_package->package.count < expected_count) {
  403. goto package_too_small;
  404. }
  405. /* Check the type of each subpackage element */
  406. for (j = 0; j < expected_count; j++) {
  407. status =
  408. acpi_ns_check_object_type(info,
  409. &sub_elements[j],
  410. package->
  411. ret_info2.
  412. object_type[j],
  413. j);
  414. if (ACPI_FAILURE(status)) {
  415. return (status);
  416. }
  417. }
  418. break;
  419. case ACPI_PTYPE2_MIN:
  420. /* Each subpackage has a variable but minimum length */
  421. expected_count = package->ret_info.count1;
  422. if (sub_package->package.count < expected_count) {
  423. goto package_too_small;
  424. }
  425. /* Check the type of each subpackage element */
  426. status =
  427. acpi_ns_check_package_elements(info, sub_elements,
  428. package->ret_info.
  429. object_type1,
  430. sub_package->package.
  431. count, 0, 0, 0);
  432. if (ACPI_FAILURE(status)) {
  433. return (status);
  434. }
  435. break;
  436. case ACPI_PTYPE2_COUNT:
  437. /*
  438. * First element is the (Integer) count of elements, including
  439. * the count field (the ACPI name is num_elements)
  440. */
  441. status = acpi_ns_check_object_type(info, sub_elements,
  442. ACPI_RTYPE_INTEGER,
  443. 0);
  444. if (ACPI_FAILURE(status)) {
  445. return (status);
  446. }
  447. /*
  448. * Make sure package is large enough for the Count and is
  449. * is as large as the minimum size
  450. */
  451. expected_count = (u32)(*sub_elements)->integer.value;
  452. if (sub_package->package.count < expected_count) {
  453. goto package_too_small;
  454. }
  455. if (sub_package->package.count <
  456. package->ret_info.count1) {
  457. expected_count = package->ret_info.count1;
  458. goto package_too_small;
  459. }
  460. if (expected_count == 0) {
  461. /*
  462. * Either the num_entries element was originally zero or it was
  463. * a NULL element and repaired to an Integer of value zero.
  464. * In either case, repair it by setting num_entries to be the
  465. * actual size of the subpackage.
  466. */
  467. expected_count = sub_package->package.count;
  468. (*sub_elements)->integer.value = expected_count;
  469. }
  470. /* Check the type of each subpackage element */
  471. status =
  472. acpi_ns_check_package_elements(info,
  473. (sub_elements + 1),
  474. package->ret_info.
  475. object_type1,
  476. (expected_count - 1),
  477. 0, 0, 1);
  478. if (ACPI_FAILURE(status)) {
  479. return (status);
  480. }
  481. break;
  482. default: /* Should not get here, type was validated by caller */
  483. ACPI_ERROR((AE_INFO, "Invalid Package type: %X",
  484. package->ret_info.type));
  485. return (AE_AML_INTERNAL);
  486. }
  487. elements++;
  488. }
  489. return (AE_OK);
  490. package_too_small:
  491. /* The subpackage count was smaller than required */
  492. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
  493. "Return SubPackage[%u] is too small - found %u elements, expected %u",
  494. i, sub_package->package.count, expected_count));
  495. return (AE_AML_OPERAND_VALUE);
  496. }
  497. /*******************************************************************************
  498. *
  499. * FUNCTION: acpi_ns_custom_package
  500. *
  501. * PARAMETERS: info - Method execution information block
  502. * elements - Pointer to the package elements array
  503. * count - Element count for the package
  504. *
  505. * RETURN: Status
  506. *
  507. * DESCRIPTION: Check a returned package object for the correct count and
  508. * correct type of all sub-objects.
  509. *
  510. * NOTE: Currently used for the _BIX method only. When needed for two or more
  511. * methods, probably a detect/dispatch mechanism will be required.
  512. *
  513. ******************************************************************************/
  514. static acpi_status
  515. acpi_ns_custom_package(struct acpi_evaluate_info *info,
  516. union acpi_operand_object **elements, u32 count)
  517. {
  518. u32 expected_count;
  519. u32 version;
  520. acpi_status status = AE_OK;
  521. ACPI_FUNCTION_NAME(ns_custom_package);
  522. /* Get version number, must be Integer */
  523. if ((*elements)->common.type != ACPI_TYPE_INTEGER) {
  524. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  525. info->node_flags,
  526. "Return Package has invalid object type for version number"));
  527. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  528. }
  529. version = (u32)(*elements)->integer.value;
  530. expected_count = 21; /* Version 1 */
  531. if (version == 0) {
  532. expected_count = 20; /* Version 0 */
  533. }
  534. if (count < expected_count) {
  535. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  536. info->node_flags,
  537. "Return Package is too small - found %u elements, expected %u",
  538. count, expected_count));
  539. return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
  540. } else if (count > expected_count) {
  541. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  542. "%s: Return Package is larger than needed - "
  543. "found %u, expected %u\n",
  544. info->full_pathname, count, expected_count));
  545. }
  546. /* Validate all elements of the returned package */
  547. status = acpi_ns_check_package_elements(info, elements,
  548. ACPI_RTYPE_INTEGER, 16,
  549. ACPI_RTYPE_STRING, 4, 0);
  550. if (ACPI_FAILURE(status)) {
  551. return_ACPI_STATUS(status);
  552. }
  553. /* Version 1 has a single trailing integer */
  554. if (version > 0) {
  555. status = acpi_ns_check_package_elements(info, elements + 20,
  556. ACPI_RTYPE_INTEGER, 1,
  557. 0, 0, 20);
  558. }
  559. return_ACPI_STATUS(status);
  560. }
  561. /*******************************************************************************
  562. *
  563. * FUNCTION: acpi_ns_check_package_elements
  564. *
  565. * PARAMETERS: info - Method execution information block
  566. * elements - Pointer to the package elements array
  567. * type1 - Object type for first group
  568. * count1 - Count for first group
  569. * type2 - Object type for second group
  570. * count2 - Count for second group
  571. * start_index - Start of the first group of elements
  572. *
  573. * RETURN: Status
  574. *
  575. * DESCRIPTION: Check that all elements of a package are of the correct object
  576. * type. Supports up to two groups of different object types.
  577. *
  578. ******************************************************************************/
  579. static acpi_status
  580. acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
  581. union acpi_operand_object **elements,
  582. u8 type1,
  583. u32 count1,
  584. u8 type2, u32 count2, u32 start_index)
  585. {
  586. union acpi_operand_object **this_element = elements;
  587. acpi_status status;
  588. u32 i;
  589. ACPI_FUNCTION_TRACE(ns_check_package_elements);
  590. /*
  591. * Up to two groups of package elements are supported by the data
  592. * structure. All elements in each group must be of the same type.
  593. * The second group can have a count of zero.
  594. */
  595. for (i = 0; i < count1; i++) {
  596. status = acpi_ns_check_object_type(info, this_element,
  597. type1, i + start_index);
  598. if (ACPI_FAILURE(status)) {
  599. return_ACPI_STATUS(status);
  600. }
  601. this_element++;
  602. }
  603. for (i = 0; i < count2; i++) {
  604. status = acpi_ns_check_object_type(info, this_element,
  605. type2,
  606. (i + count1 + start_index));
  607. if (ACPI_FAILURE(status)) {
  608. return_ACPI_STATUS(status);
  609. }
  610. this_element++;
  611. }
  612. return_ACPI_STATUS(AE_OK);
  613. }