nsrepair.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 - 2018, 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 && (!(expected_btypes & ACPI_RTYPE_NONE))) {
  166. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  167. ACPI_WARN_PREDEFINED((AE_INFO,
  168. info->full_pathname,
  169. ACPI_WARN_ALWAYS,
  170. "Found unexpected NULL package element"));
  171. status =
  172. acpi_ns_repair_null_element(info,
  173. expected_btypes,
  174. package_index,
  175. return_object_ptr);
  176. if (ACPI_SUCCESS(status)) {
  177. return (AE_OK); /* Repair was successful */
  178. }
  179. } else {
  180. ACPI_WARN_PREDEFINED((AE_INFO,
  181. info->full_pathname,
  182. ACPI_WARN_ALWAYS,
  183. "Missing expected return value"));
  184. }
  185. return (AE_AML_NO_RETURN_VALUE);
  186. }
  187. }
  188. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  189. status = acpi_ns_convert_to_integer(return_object, &new_object);
  190. if (ACPI_SUCCESS(status)) {
  191. goto object_repaired;
  192. }
  193. }
  194. if (expected_btypes & ACPI_RTYPE_STRING) {
  195. status = acpi_ns_convert_to_string(return_object, &new_object);
  196. if (ACPI_SUCCESS(status)) {
  197. goto object_repaired;
  198. }
  199. }
  200. if (expected_btypes & ACPI_RTYPE_BUFFER) {
  201. status = acpi_ns_convert_to_buffer(return_object, &new_object);
  202. if (ACPI_SUCCESS(status)) {
  203. goto object_repaired;
  204. }
  205. }
  206. if (expected_btypes & ACPI_RTYPE_PACKAGE) {
  207. /*
  208. * A package is expected. We will wrap the existing object with a
  209. * new package object. It is often the case that if a variable-length
  210. * package is required, but there is only a single object needed, the
  211. * BIOS will return that object instead of wrapping it with a Package
  212. * object. Note: after the wrapping, the package will be validated
  213. * for correct contents (expected object type or types).
  214. */
  215. status =
  216. acpi_ns_wrap_with_package(info, return_object, &new_object);
  217. if (ACPI_SUCCESS(status)) {
  218. /*
  219. * The original object just had its reference count
  220. * incremented for being inserted into the new package.
  221. */
  222. *return_object_ptr = new_object; /* New Package object */
  223. info->return_flags |= ACPI_OBJECT_REPAIRED;
  224. return (AE_OK);
  225. }
  226. }
  227. /* We cannot repair this object */
  228. return (AE_AML_OPERAND_TYPE);
  229. object_repaired:
  230. /* Object was successfully repaired */
  231. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  232. /* Update reference count of new object */
  233. if (!(info->return_flags & ACPI_OBJECT_WRAPPED)) {
  234. new_object->common.reference_count =
  235. return_object->common.reference_count;
  236. }
  237. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  238. "%s: Converted %s to expected %s at Package index %u\n",
  239. info->full_pathname,
  240. acpi_ut_get_object_type_name(return_object),
  241. acpi_ut_get_object_type_name(new_object),
  242. package_index));
  243. } else {
  244. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  245. "%s: Converted %s to expected %s\n",
  246. info->full_pathname,
  247. acpi_ut_get_object_type_name(return_object),
  248. acpi_ut_get_object_type_name(new_object)));
  249. }
  250. /* Delete old object, install the new return object */
  251. acpi_ut_remove_reference(return_object);
  252. *return_object_ptr = new_object;
  253. info->return_flags |= ACPI_OBJECT_REPAIRED;
  254. return (AE_OK);
  255. }
  256. /******************************************************************************
  257. *
  258. * FUNCTION: acpi_ns_match_simple_repair
  259. *
  260. * PARAMETERS: node - Namespace node for the method/object
  261. * return_btype - Object type that was returned
  262. * package_index - Index of object within parent package (if
  263. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  264. * otherwise)
  265. *
  266. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  267. *
  268. * DESCRIPTION: Check an object name against the repairable object list.
  269. *
  270. *****************************************************************************/
  271. static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
  272. acpi_namespace_node
  273. *node,
  274. u32
  275. return_btype,
  276. u32
  277. package_index)
  278. {
  279. const struct acpi_simple_repair_info *this_name;
  280. /* Search info table for a repairable predefined method/object name */
  281. this_name = acpi_object_repair_info;
  282. while (this_name->object_converter) {
  283. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
  284. /* Check if we can actually repair this name/type combination */
  285. if ((return_btype & this_name->unexpected_btypes) &&
  286. (this_name->package_index ==
  287. ACPI_ALL_PACKAGE_ELEMENTS
  288. || package_index == this_name->package_index)) {
  289. return (this_name);
  290. }
  291. return (NULL);
  292. }
  293. this_name++;
  294. }
  295. return (NULL); /* Name was not found in the repair table */
  296. }
  297. /*******************************************************************************
  298. *
  299. * FUNCTION: acpi_ns_repair_null_element
  300. *
  301. * PARAMETERS: info - Method execution information block
  302. * expected_btypes - Object types expected
  303. * package_index - Index of object within parent package (if
  304. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  305. * otherwise)
  306. * return_object_ptr - Pointer to the object returned from the
  307. * evaluation of a method or object
  308. *
  309. * RETURN: Status. AE_OK if repair was successful.
  310. *
  311. * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
  312. *
  313. ******************************************************************************/
  314. acpi_status
  315. acpi_ns_repair_null_element(struct acpi_evaluate_info *info,
  316. u32 expected_btypes,
  317. u32 package_index,
  318. union acpi_operand_object **return_object_ptr)
  319. {
  320. union acpi_operand_object *return_object = *return_object_ptr;
  321. union acpi_operand_object *new_object;
  322. ACPI_FUNCTION_NAME(ns_repair_null_element);
  323. /* No repair needed if return object is non-NULL */
  324. if (return_object) {
  325. return (AE_OK);
  326. }
  327. /*
  328. * Attempt to repair a NULL element of a Package object. This applies to
  329. * predefined names that return a fixed-length package and each element
  330. * is required. It does not apply to variable-length packages where NULL
  331. * elements are allowed, especially at the end of the package.
  332. */
  333. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  334. /* Need an integer - create a zero-value integer */
  335. new_object = acpi_ut_create_integer_object((u64)0);
  336. } else if (expected_btypes & ACPI_RTYPE_STRING) {
  337. /* Need a string - create a NULL string */
  338. new_object = acpi_ut_create_string_object(0);
  339. } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
  340. /* Need a buffer - create a zero-length buffer */
  341. new_object = acpi_ut_create_buffer_object(0);
  342. } else {
  343. /* Error for all other expected types */
  344. return (AE_AML_OPERAND_TYPE);
  345. }
  346. if (!new_object) {
  347. return (AE_NO_MEMORY);
  348. }
  349. /* Set the reference count according to the parent Package object */
  350. new_object->common.reference_count =
  351. info->parent_package->common.reference_count;
  352. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  353. "%s: Converted NULL package element to expected %s at index %u\n",
  354. info->full_pathname,
  355. acpi_ut_get_object_type_name(new_object),
  356. package_index));
  357. *return_object_ptr = new_object;
  358. info->return_flags |= ACPI_OBJECT_REPAIRED;
  359. return (AE_OK);
  360. }
  361. /******************************************************************************
  362. *
  363. * FUNCTION: acpi_ns_remove_null_elements
  364. *
  365. * PARAMETERS: info - Method execution information block
  366. * package_type - An acpi_return_package_types value
  367. * obj_desc - A Package object
  368. *
  369. * RETURN: None.
  370. *
  371. * DESCRIPTION: Remove all NULL package elements from packages that contain
  372. * a variable number of subpackages. For these types of
  373. * packages, NULL elements can be safely removed.
  374. *
  375. *****************************************************************************/
  376. void
  377. acpi_ns_remove_null_elements(struct acpi_evaluate_info *info,
  378. u8 package_type,
  379. union acpi_operand_object *obj_desc)
  380. {
  381. union acpi_operand_object **source;
  382. union acpi_operand_object **dest;
  383. u32 count;
  384. u32 new_count;
  385. u32 i;
  386. ACPI_FUNCTION_NAME(ns_remove_null_elements);
  387. /*
  388. * We can safely remove all NULL elements from these package types:
  389. * PTYPE1_VAR packages contain a variable number of simple data types.
  390. * PTYPE2 packages contain a variable number of subpackages.
  391. */
  392. switch (package_type) {
  393. case ACPI_PTYPE1_VAR:
  394. case ACPI_PTYPE2:
  395. case ACPI_PTYPE2_COUNT:
  396. case ACPI_PTYPE2_PKG_COUNT:
  397. case ACPI_PTYPE2_FIXED:
  398. case ACPI_PTYPE2_MIN:
  399. case ACPI_PTYPE2_REV_FIXED:
  400. case ACPI_PTYPE2_FIX_VAR:
  401. break;
  402. default:
  403. case ACPI_PTYPE2_VAR_VAR:
  404. case ACPI_PTYPE1_FIXED:
  405. case ACPI_PTYPE1_OPTION:
  406. return;
  407. }
  408. count = obj_desc->package.count;
  409. new_count = count;
  410. source = obj_desc->package.elements;
  411. dest = source;
  412. /* Examine all elements of the package object, remove nulls */
  413. for (i = 0; i < count; i++) {
  414. if (!*source) {
  415. new_count--;
  416. } else {
  417. *dest = *source;
  418. dest++;
  419. }
  420. source++;
  421. }
  422. /* Update parent package if any null elements were removed */
  423. if (new_count < count) {
  424. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  425. "%s: Found and removed %u NULL elements\n",
  426. info->full_pathname, (count - new_count)));
  427. /* NULL terminate list and update the package count */
  428. *dest = NULL;
  429. obj_desc->package.count = new_count;
  430. }
  431. }
  432. /*******************************************************************************
  433. *
  434. * FUNCTION: acpi_ns_wrap_with_package
  435. *
  436. * PARAMETERS: info - Method execution information block
  437. * original_object - Pointer to the object to repair.
  438. * obj_desc_ptr - The new package object is returned here
  439. *
  440. * RETURN: Status, new object in *obj_desc_ptr
  441. *
  442. * DESCRIPTION: Repair a common problem with objects that are defined to
  443. * return a variable-length Package of sub-objects. If there is
  444. * only one sub-object, some BIOS code mistakenly simply declares
  445. * the single object instead of a Package with one sub-object.
  446. * This function attempts to repair this error by wrapping a
  447. * Package object around the original object, creating the
  448. * correct and expected Package with one sub-object.
  449. *
  450. * Names that can be repaired in this manner include:
  451. * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
  452. * _BCL, _DOD, _FIX, _Sx
  453. *
  454. ******************************************************************************/
  455. acpi_status
  456. acpi_ns_wrap_with_package(struct acpi_evaluate_info *info,
  457. union acpi_operand_object *original_object,
  458. union acpi_operand_object **obj_desc_ptr)
  459. {
  460. union acpi_operand_object *pkg_obj_desc;
  461. ACPI_FUNCTION_NAME(ns_wrap_with_package);
  462. /*
  463. * Create the new outer package and populate it. The new
  464. * package will have a single element, the lone sub-object.
  465. */
  466. pkg_obj_desc = acpi_ut_create_package_object(1);
  467. if (!pkg_obj_desc) {
  468. return (AE_NO_MEMORY);
  469. }
  470. pkg_obj_desc->package.elements[0] = original_object;
  471. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  472. "%s: Wrapped %s with expected Package object\n",
  473. info->full_pathname,
  474. acpi_ut_get_object_type_name(original_object)));
  475. /* Return the new object in the object pointer */
  476. *obj_desc_ptr = pkg_obj_desc;
  477. info->return_flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
  478. return (AE_OK);
  479. }