nsrepair.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: nsrepair - Repair for objects returned by predefined methods
  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 "acinterp.h"
  13. #include "acpredef.h"
  14. #include "amlresrc.h"
  15. #define _COMPONENT ACPI_NAMESPACE
  16. ACPI_MODULE_NAME("nsrepair")
  17. /*******************************************************************************
  18. *
  19. * This module attempts to repair or convert objects returned by the
  20. * predefined methods to an object type that is expected, as per the ACPI
  21. * specification. The need for this code is dictated by the many machines that
  22. * return incorrect types for the standard predefined methods. Performing these
  23. * conversions here, in one place, eliminates the need for individual ACPI
  24. * device drivers to do the same. Note: Most of these conversions are different
  25. * than the internal object conversion routines used for implicit object
  26. * conversion.
  27. *
  28. * The following conversions can be performed as necessary:
  29. *
  30. * Integer -> String
  31. * Integer -> Buffer
  32. * String -> Integer
  33. * String -> Buffer
  34. * Buffer -> Integer
  35. * Buffer -> String
  36. * Buffer -> Package of Integers
  37. * Package -> Package of one Package
  38. *
  39. * Additional conversions that are available:
  40. * Convert a null return or zero return value to an end_tag descriptor
  41. * Convert an ASCII string to a Unicode buffer
  42. *
  43. * An incorrect standalone object is wrapped with required outer package
  44. *
  45. * Additional possible repairs:
  46. * Required package elements that are NULL replaced by Integer/String/Buffer
  47. *
  48. ******************************************************************************/
  49. /* Local prototypes */
  50. static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
  51. acpi_namespace_node
  52. *node,
  53. u32
  54. return_btype,
  55. u32
  56. package_index);
  57. /*
  58. * Special but simple repairs for some names.
  59. *
  60. * 2nd argument: Unexpected types that can be repaired
  61. */
  62. static const struct acpi_simple_repair_info acpi_object_repair_info[] = {
  63. /* Resource descriptor conversions */
  64. {"_CRS",
  65. ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
  66. ACPI_RTYPE_NONE,
  67. ACPI_NOT_PACKAGE_ELEMENT,
  68. acpi_ns_convert_to_resource},
  69. {"_DMA",
  70. ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
  71. ACPI_RTYPE_NONE,
  72. ACPI_NOT_PACKAGE_ELEMENT,
  73. acpi_ns_convert_to_resource},
  74. {"_PRS",
  75. ACPI_RTYPE_INTEGER | ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER |
  76. ACPI_RTYPE_NONE,
  77. ACPI_NOT_PACKAGE_ELEMENT,
  78. acpi_ns_convert_to_resource},
  79. /* Object reference conversions */
  80. {"_DEP", ACPI_RTYPE_STRING, ACPI_ALL_PACKAGE_ELEMENTS,
  81. acpi_ns_convert_to_reference},
  82. /* Unicode conversions */
  83. {"_MLS", ACPI_RTYPE_STRING, 1,
  84. acpi_ns_convert_to_unicode},
  85. {"_STR", ACPI_RTYPE_STRING | ACPI_RTYPE_BUFFER,
  86. ACPI_NOT_PACKAGE_ELEMENT,
  87. acpi_ns_convert_to_unicode},
  88. {{0, 0, 0, 0}, 0, 0, NULL} /* Table terminator */
  89. };
  90. /*******************************************************************************
  91. *
  92. * FUNCTION: acpi_ns_simple_repair
  93. *
  94. * PARAMETERS: info - Method execution information block
  95. * expected_btypes - Object types expected
  96. * package_index - Index of object within parent package (if
  97. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  98. * otherwise)
  99. * return_object_ptr - Pointer to the object returned from the
  100. * evaluation of a method or object
  101. *
  102. * RETURN: Status. AE_OK if repair was successful.
  103. *
  104. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  105. * not expected.
  106. *
  107. ******************************************************************************/
  108. acpi_status
  109. acpi_ns_simple_repair(struct acpi_evaluate_info *info,
  110. u32 expected_btypes,
  111. u32 package_index,
  112. union acpi_operand_object **return_object_ptr)
  113. {
  114. union acpi_operand_object *return_object = *return_object_ptr;
  115. union acpi_operand_object *new_object = NULL;
  116. acpi_status status;
  117. const struct acpi_simple_repair_info *predefined;
  118. ACPI_FUNCTION_NAME(ns_simple_repair);
  119. /*
  120. * Special repairs for certain names that are in the repair table.
  121. * Check if this name is in the list of repairable names.
  122. */
  123. predefined = acpi_ns_match_simple_repair(info->node,
  124. info->return_btype,
  125. package_index);
  126. if (predefined) {
  127. if (!return_object) {
  128. ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
  129. ACPI_WARN_ALWAYS,
  130. "Missing expected return value"));
  131. }
  132. status = predefined->object_converter(info->node, return_object,
  133. &new_object);
  134. if (ACPI_FAILURE(status)) {
  135. /* A fatal error occurred during a conversion */
  136. ACPI_EXCEPTION((AE_INFO, status,
  137. "During return object analysis"));
  138. return (status);
  139. }
  140. if (new_object) {
  141. goto object_repaired;
  142. }
  143. }
  144. /*
  145. * Do not perform simple object repair unless the return type is not
  146. * expected.
  147. */
  148. if (info->return_btype & expected_btypes) {
  149. return (AE_OK);
  150. }
  151. /*
  152. * At this point, we know that the type of the returned object was not
  153. * one of the expected types for this predefined name. Attempt to
  154. * repair the object by converting it to one of the expected object
  155. * types for this predefined name.
  156. */
  157. /*
  158. * If there is no return value, check if we require a return value for
  159. * this predefined name. Either one return value is expected, or none,
  160. * for both methods and other objects.
  161. *
  162. * Try to fix if there was no return object. Warning if failed to fix.
  163. */
  164. if (!return_object) {
  165. if (expected_btypes) {
  166. if (!(expected_btypes & ACPI_RTYPE_NONE) &&
  167. package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  168. ACPI_WARN_PREDEFINED((AE_INFO,
  169. info->full_pathname,
  170. ACPI_WARN_ALWAYS,
  171. "Found unexpected NULL package element"));
  172. status =
  173. acpi_ns_repair_null_element(info,
  174. expected_btypes,
  175. package_index,
  176. return_object_ptr);
  177. if (ACPI_SUCCESS(status)) {
  178. return (AE_OK); /* Repair was successful */
  179. }
  180. }
  181. if (expected_btypes != ACPI_RTYPE_NONE) {
  182. ACPI_WARN_PREDEFINED((AE_INFO,
  183. info->full_pathname,
  184. ACPI_WARN_ALWAYS,
  185. "Missing expected return value"));
  186. return (AE_AML_NO_RETURN_VALUE);
  187. }
  188. }
  189. }
  190. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  191. status = acpi_ns_convert_to_integer(return_object, &new_object);
  192. if (ACPI_SUCCESS(status)) {
  193. goto object_repaired;
  194. }
  195. }
  196. if (expected_btypes & ACPI_RTYPE_STRING) {
  197. status = acpi_ns_convert_to_string(return_object, &new_object);
  198. if (ACPI_SUCCESS(status)) {
  199. goto object_repaired;
  200. }
  201. }
  202. if (expected_btypes & ACPI_RTYPE_BUFFER) {
  203. status = acpi_ns_convert_to_buffer(return_object, &new_object);
  204. if (ACPI_SUCCESS(status)) {
  205. goto object_repaired;
  206. }
  207. }
  208. if (expected_btypes & ACPI_RTYPE_PACKAGE) {
  209. /*
  210. * A package is expected. We will wrap the existing object with a
  211. * new package object. It is often the case that if a variable-length
  212. * package is required, but there is only a single object needed, the
  213. * BIOS will return that object instead of wrapping it with a Package
  214. * object. Note: after the wrapping, the package will be validated
  215. * for correct contents (expected object type or types).
  216. */
  217. status =
  218. acpi_ns_wrap_with_package(info, return_object, &new_object);
  219. if (ACPI_SUCCESS(status)) {
  220. /*
  221. * The original object just had its reference count
  222. * incremented for being inserted into the new package.
  223. */
  224. *return_object_ptr = new_object; /* New Package object */
  225. info->return_flags |= ACPI_OBJECT_REPAIRED;
  226. return (AE_OK);
  227. }
  228. }
  229. /* We cannot repair this object */
  230. return (AE_AML_OPERAND_TYPE);
  231. object_repaired:
  232. /* Object was successfully repaired */
  233. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  234. /* Update reference count of new object */
  235. if (!(info->return_flags & ACPI_OBJECT_WRAPPED)) {
  236. new_object->common.reference_count =
  237. return_object->common.reference_count;
  238. }
  239. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  240. "%s: Converted %s to expected %s at Package index %u\n",
  241. info->full_pathname,
  242. acpi_ut_get_object_type_name(return_object),
  243. acpi_ut_get_object_type_name(new_object),
  244. package_index));
  245. } else {
  246. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  247. "%s: Converted %s to expected %s\n",
  248. info->full_pathname,
  249. acpi_ut_get_object_type_name(return_object),
  250. acpi_ut_get_object_type_name(new_object)));
  251. }
  252. /* Delete old object, install the new return object */
  253. acpi_ut_remove_reference(return_object);
  254. *return_object_ptr = new_object;
  255. info->return_flags |= ACPI_OBJECT_REPAIRED;
  256. return (AE_OK);
  257. }
  258. /******************************************************************************
  259. *
  260. * FUNCTION: acpi_ns_match_simple_repair
  261. *
  262. * PARAMETERS: node - Namespace node for the method/object
  263. * return_btype - Object type that was returned
  264. * package_index - Index of object within parent package (if
  265. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  266. * otherwise)
  267. *
  268. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  269. *
  270. * DESCRIPTION: Check an object name against the repairable object list.
  271. *
  272. *****************************************************************************/
  273. static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
  274. acpi_namespace_node
  275. *node,
  276. u32
  277. return_btype,
  278. u32
  279. package_index)
  280. {
  281. const struct acpi_simple_repair_info *this_name;
  282. /* Search info table for a repairable predefined method/object name */
  283. this_name = acpi_object_repair_info;
  284. while (this_name->object_converter) {
  285. if (ACPI_COMPARE_NAMESEG(node->name.ascii, this_name->name)) {
  286. /* Check if we can actually repair this name/type combination */
  287. if ((return_btype & this_name->unexpected_btypes) &&
  288. (this_name->package_index ==
  289. ACPI_ALL_PACKAGE_ELEMENTS
  290. || package_index == this_name->package_index)) {
  291. return (this_name);
  292. }
  293. return (NULL);
  294. }
  295. this_name++;
  296. }
  297. return (NULL); /* Name was not found in the repair table */
  298. }
  299. /*******************************************************************************
  300. *
  301. * FUNCTION: acpi_ns_repair_null_element
  302. *
  303. * PARAMETERS: info - Method execution information block
  304. * expected_btypes - Object types expected
  305. * package_index - Index of object within parent package (if
  306. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  307. * otherwise)
  308. * return_object_ptr - Pointer to the object returned from the
  309. * evaluation of a method or object
  310. *
  311. * RETURN: Status. AE_OK if repair was successful.
  312. *
  313. * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
  314. *
  315. ******************************************************************************/
  316. acpi_status
  317. acpi_ns_repair_null_element(struct acpi_evaluate_info *info,
  318. u32 expected_btypes,
  319. u32 package_index,
  320. union acpi_operand_object **return_object_ptr)
  321. {
  322. union acpi_operand_object *return_object = *return_object_ptr;
  323. union acpi_operand_object *new_object;
  324. ACPI_FUNCTION_NAME(ns_repair_null_element);
  325. /* No repair needed if return object is non-NULL */
  326. if (return_object) {
  327. return (AE_OK);
  328. }
  329. /*
  330. * Attempt to repair a NULL element of a Package object. This applies to
  331. * predefined names that return a fixed-length package and each element
  332. * is required. It does not apply to variable-length packages where NULL
  333. * elements are allowed, especially at the end of the package.
  334. */
  335. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  336. /* Need an integer - create a zero-value integer */
  337. new_object = acpi_ut_create_integer_object((u64)0);
  338. } else if (expected_btypes & ACPI_RTYPE_STRING) {
  339. /* Need a string - create a NULL string */
  340. new_object = acpi_ut_create_string_object(0);
  341. } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
  342. /* Need a buffer - create a zero-length buffer */
  343. new_object = acpi_ut_create_buffer_object(0);
  344. } else {
  345. /* Error for all other expected types */
  346. return (AE_AML_OPERAND_TYPE);
  347. }
  348. if (!new_object) {
  349. return (AE_NO_MEMORY);
  350. }
  351. /* Set the reference count according to the parent Package object */
  352. new_object->common.reference_count =
  353. info->parent_package->common.reference_count;
  354. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  355. "%s: Converted NULL package element to expected %s at index %u\n",
  356. info->full_pathname,
  357. acpi_ut_get_object_type_name(new_object),
  358. package_index));
  359. *return_object_ptr = new_object;
  360. info->return_flags |= ACPI_OBJECT_REPAIRED;
  361. return (AE_OK);
  362. }
  363. /******************************************************************************
  364. *
  365. * FUNCTION: acpi_ns_remove_null_elements
  366. *
  367. * PARAMETERS: info - Method execution information block
  368. * package_type - An acpi_return_package_types value
  369. * obj_desc - A Package object
  370. *
  371. * RETURN: None.
  372. *
  373. * DESCRIPTION: Remove all NULL package elements from packages that contain
  374. * a variable number of subpackages. For these types of
  375. * packages, NULL elements can be safely removed.
  376. *
  377. *****************************************************************************/
  378. void
  379. acpi_ns_remove_null_elements(struct acpi_evaluate_info *info,
  380. u8 package_type,
  381. union acpi_operand_object *obj_desc)
  382. {
  383. union acpi_operand_object **source;
  384. union acpi_operand_object **dest;
  385. u32 count;
  386. u32 new_count;
  387. u32 i;
  388. ACPI_FUNCTION_NAME(ns_remove_null_elements);
  389. /*
  390. * We can safely remove all NULL elements from these package types:
  391. * PTYPE1_VAR packages contain a variable number of simple data types.
  392. * PTYPE2 packages contain a variable number of subpackages.
  393. */
  394. switch (package_type) {
  395. case ACPI_PTYPE1_VAR:
  396. case ACPI_PTYPE2:
  397. case ACPI_PTYPE2_COUNT:
  398. case ACPI_PTYPE2_PKG_COUNT:
  399. case ACPI_PTYPE2_FIXED:
  400. case ACPI_PTYPE2_MIN:
  401. case ACPI_PTYPE2_REV_FIXED:
  402. case ACPI_PTYPE2_FIX_VAR:
  403. break;
  404. default:
  405. case ACPI_PTYPE2_VAR_VAR:
  406. case ACPI_PTYPE1_FIXED:
  407. case ACPI_PTYPE1_OPTION:
  408. return;
  409. }
  410. count = obj_desc->package.count;
  411. new_count = count;
  412. source = obj_desc->package.elements;
  413. dest = source;
  414. /* Examine all elements of the package object, remove nulls */
  415. for (i = 0; i < count; i++) {
  416. if (!*source) {
  417. new_count--;
  418. } else {
  419. *dest = *source;
  420. dest++;
  421. }
  422. source++;
  423. }
  424. /* Update parent package if any null elements were removed */
  425. if (new_count < count) {
  426. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  427. "%s: Found and removed %u NULL elements\n",
  428. info->full_pathname, (count - new_count)));
  429. /* NULL terminate list and update the package count */
  430. *dest = NULL;
  431. obj_desc->package.count = new_count;
  432. }
  433. }
  434. /*******************************************************************************
  435. *
  436. * FUNCTION: acpi_ns_wrap_with_package
  437. *
  438. * PARAMETERS: info - Method execution information block
  439. * original_object - Pointer to the object to repair.
  440. * obj_desc_ptr - The new package object is returned here
  441. *
  442. * RETURN: Status, new object in *obj_desc_ptr
  443. *
  444. * DESCRIPTION: Repair a common problem with objects that are defined to
  445. * return a variable-length Package of sub-objects. If there is
  446. * only one sub-object, some BIOS code mistakenly simply declares
  447. * the single object instead of a Package with one sub-object.
  448. * This function attempts to repair this error by wrapping a
  449. * Package object around the original object, creating the
  450. * correct and expected Package with one sub-object.
  451. *
  452. * Names that can be repaired in this manner include:
  453. * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
  454. * _BCL, _DOD, _FIX, _Sx
  455. *
  456. ******************************************************************************/
  457. acpi_status
  458. acpi_ns_wrap_with_package(struct acpi_evaluate_info *info,
  459. union acpi_operand_object *original_object,
  460. union acpi_operand_object **obj_desc_ptr)
  461. {
  462. union acpi_operand_object *pkg_obj_desc;
  463. ACPI_FUNCTION_NAME(ns_wrap_with_package);
  464. /*
  465. * Create the new outer package and populate it. The new
  466. * package will have a single element, the lone sub-object.
  467. */
  468. pkg_obj_desc = acpi_ut_create_package_object(1);
  469. if (!pkg_obj_desc) {
  470. return (AE_NO_MEMORY);
  471. }
  472. pkg_obj_desc->package.elements[0] = original_object;
  473. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  474. "%s: Wrapped %s with expected Package object\n",
  475. info->full_pathname,
  476. acpi_ut_get_object_type_name(original_object)));
  477. /* Return the new object in the object pointer */
  478. *obj_desc_ptr = pkg_obj_desc;
  479. info->return_flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
  480. return (AE_OK);
  481. }