nsrepair.c 19 KB

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