nsrepair.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair - Repair for objects returned by predefined methods
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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. /*
  266. * The original object is a package element. We need to
  267. * decrement the reference count of the original object,
  268. * for removing it from the package.
  269. *
  270. * However, if the original object was just wrapped with a
  271. * package object as part of the repair, we don't need to
  272. * change the reference count.
  273. */
  274. if (!(info->return_flags & ACPI_OBJECT_WRAPPED)) {
  275. new_object->common.reference_count =
  276. return_object->common.reference_count;
  277. if (return_object->common.reference_count > 1) {
  278. return_object->common.reference_count--;
  279. }
  280. }
  281. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  282. "%s: Converted %s to expected %s at Package index %u\n",
  283. info->full_pathname,
  284. acpi_ut_get_object_type_name(return_object),
  285. acpi_ut_get_object_type_name(new_object),
  286. package_index));
  287. } else {
  288. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  289. "%s: Converted %s to expected %s\n",
  290. info->full_pathname,
  291. acpi_ut_get_object_type_name(return_object),
  292. acpi_ut_get_object_type_name(new_object)));
  293. }
  294. /* Delete old object, install the new return object */
  295. acpi_ut_remove_reference(return_object);
  296. *return_object_ptr = new_object;
  297. info->return_flags |= ACPI_OBJECT_REPAIRED;
  298. return (AE_OK);
  299. }
  300. /******************************************************************************
  301. *
  302. * FUNCTION: acpi_ns_match_simple_repair
  303. *
  304. * PARAMETERS: node - Namespace node for the method/object
  305. * return_btype - Object type that was returned
  306. * package_index - Index of object within parent package (if
  307. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  308. * otherwise)
  309. *
  310. * RETURN: Pointer to entry in repair table. NULL indicates not found.
  311. *
  312. * DESCRIPTION: Check an object name against the repairable object list.
  313. *
  314. *****************************************************************************/
  315. static const struct acpi_simple_repair_info *acpi_ns_match_simple_repair(struct
  316. acpi_namespace_node
  317. *node,
  318. u32
  319. return_btype,
  320. u32
  321. package_index)
  322. {
  323. const struct acpi_simple_repair_info *this_name;
  324. /* Search info table for a repairable predefined method/object name */
  325. this_name = acpi_object_repair_info;
  326. while (this_name->object_converter) {
  327. if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
  328. /* Check if we can actually repair this name/type combination */
  329. if ((return_btype & this_name->unexpected_btypes) &&
  330. (this_name->package_index ==
  331. ACPI_ALL_PACKAGE_ELEMENTS
  332. || package_index == this_name->package_index)) {
  333. return (this_name);
  334. }
  335. return (NULL);
  336. }
  337. this_name++;
  338. }
  339. return (NULL); /* Name was not found in the repair table */
  340. }
  341. /*******************************************************************************
  342. *
  343. * FUNCTION: acpi_ns_repair_null_element
  344. *
  345. * PARAMETERS: info - Method execution information block
  346. * expected_btypes - Object types expected
  347. * package_index - Index of object within parent package (if
  348. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  349. * otherwise)
  350. * return_object_ptr - Pointer to the object returned from the
  351. * evaluation of a method or object
  352. *
  353. * RETURN: Status. AE_OK if repair was successful.
  354. *
  355. * DESCRIPTION: Attempt to repair a NULL element of a returned Package object.
  356. *
  357. ******************************************************************************/
  358. acpi_status
  359. acpi_ns_repair_null_element(struct acpi_evaluate_info *info,
  360. u32 expected_btypes,
  361. u32 package_index,
  362. union acpi_operand_object **return_object_ptr)
  363. {
  364. union acpi_operand_object *return_object = *return_object_ptr;
  365. union acpi_operand_object *new_object;
  366. ACPI_FUNCTION_NAME(ns_repair_null_element);
  367. /* No repair needed if return object is non-NULL */
  368. if (return_object) {
  369. return (AE_OK);
  370. }
  371. /*
  372. * Attempt to repair a NULL element of a Package object. This applies to
  373. * predefined names that return a fixed-length package and each element
  374. * is required. It does not apply to variable-length packages where NULL
  375. * elements are allowed, especially at the end of the package.
  376. */
  377. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  378. /* Need an integer - create a zero-value integer */
  379. new_object = acpi_ut_create_integer_object((u64)0);
  380. } else if (expected_btypes & ACPI_RTYPE_STRING) {
  381. /* Need a string - create a NULL string */
  382. new_object = acpi_ut_create_string_object(0);
  383. } else if (expected_btypes & ACPI_RTYPE_BUFFER) {
  384. /* Need a buffer - create a zero-length buffer */
  385. new_object = acpi_ut_create_buffer_object(0);
  386. } else {
  387. /* Error for all other expected types */
  388. return (AE_AML_OPERAND_TYPE);
  389. }
  390. if (!new_object) {
  391. return (AE_NO_MEMORY);
  392. }
  393. /* Set the reference count according to the parent Package object */
  394. new_object->common.reference_count =
  395. info->parent_package->common.reference_count;
  396. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  397. "%s: Converted NULL package element to expected %s at index %u\n",
  398. info->full_pathname,
  399. acpi_ut_get_object_type_name(new_object),
  400. package_index));
  401. *return_object_ptr = new_object;
  402. info->return_flags |= ACPI_OBJECT_REPAIRED;
  403. return (AE_OK);
  404. }
  405. /******************************************************************************
  406. *
  407. * FUNCTION: acpi_ns_remove_null_elements
  408. *
  409. * PARAMETERS: info - Method execution information block
  410. * package_type - An acpi_return_package_types value
  411. * obj_desc - A Package object
  412. *
  413. * RETURN: None.
  414. *
  415. * DESCRIPTION: Remove all NULL package elements from packages that contain
  416. * a variable number of subpackages. For these types of
  417. * packages, NULL elements can be safely removed.
  418. *
  419. *****************************************************************************/
  420. void
  421. acpi_ns_remove_null_elements(struct acpi_evaluate_info *info,
  422. u8 package_type,
  423. union acpi_operand_object *obj_desc)
  424. {
  425. union acpi_operand_object **source;
  426. union acpi_operand_object **dest;
  427. u32 count;
  428. u32 new_count;
  429. u32 i;
  430. ACPI_FUNCTION_NAME(ns_remove_null_elements);
  431. /*
  432. * We can safely remove all NULL elements from these package types:
  433. * PTYPE1_VAR packages contain a variable number of simple data types.
  434. * PTYPE2 packages contain a variable number of subpackages.
  435. */
  436. switch (package_type) {
  437. case ACPI_PTYPE1_VAR:
  438. case ACPI_PTYPE2:
  439. case ACPI_PTYPE2_COUNT:
  440. case ACPI_PTYPE2_PKG_COUNT:
  441. case ACPI_PTYPE2_FIXED:
  442. case ACPI_PTYPE2_MIN:
  443. case ACPI_PTYPE2_REV_FIXED:
  444. case ACPI_PTYPE2_FIX_VAR:
  445. break;
  446. default:
  447. case ACPI_PTYPE2_VAR_VAR:
  448. case ACPI_PTYPE1_FIXED:
  449. case ACPI_PTYPE1_OPTION:
  450. return;
  451. }
  452. count = obj_desc->package.count;
  453. new_count = count;
  454. source = obj_desc->package.elements;
  455. dest = source;
  456. /* Examine all elements of the package object, remove nulls */
  457. for (i = 0; i < count; i++) {
  458. if (!*source) {
  459. new_count--;
  460. } else {
  461. *dest = *source;
  462. dest++;
  463. }
  464. source++;
  465. }
  466. /* Update parent package if any null elements were removed */
  467. if (new_count < count) {
  468. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  469. "%s: Found and removed %u NULL elements\n",
  470. info->full_pathname, (count - new_count)));
  471. /* NULL terminate list and update the package count */
  472. *dest = NULL;
  473. obj_desc->package.count = new_count;
  474. }
  475. }
  476. /*******************************************************************************
  477. *
  478. * FUNCTION: acpi_ns_wrap_with_package
  479. *
  480. * PARAMETERS: info - Method execution information block
  481. * original_object - Pointer to the object to repair.
  482. * obj_desc_ptr - The new package object is returned here
  483. *
  484. * RETURN: Status, new object in *obj_desc_ptr
  485. *
  486. * DESCRIPTION: Repair a common problem with objects that are defined to
  487. * return a variable-length Package of sub-objects. If there is
  488. * only one sub-object, some BIOS code mistakenly simply declares
  489. * the single object instead of a Package with one sub-object.
  490. * This function attempts to repair this error by wrapping a
  491. * Package object around the original object, creating the
  492. * correct and expected Package with one sub-object.
  493. *
  494. * Names that can be repaired in this manner include:
  495. * _ALR, _CSD, _HPX, _MLS, _PLD, _PRT, _PSS, _TRT, _TSS,
  496. * _BCL, _DOD, _FIX, _Sx
  497. *
  498. ******************************************************************************/
  499. acpi_status
  500. acpi_ns_wrap_with_package(struct acpi_evaluate_info *info,
  501. union acpi_operand_object *original_object,
  502. union acpi_operand_object **obj_desc_ptr)
  503. {
  504. union acpi_operand_object *pkg_obj_desc;
  505. ACPI_FUNCTION_NAME(ns_wrap_with_package);
  506. /*
  507. * Create the new outer package and populate it. The new
  508. * package will have a single element, the lone sub-object.
  509. */
  510. pkg_obj_desc = acpi_ut_create_package_object(1);
  511. if (!pkg_obj_desc) {
  512. return (AE_NO_MEMORY);
  513. }
  514. pkg_obj_desc->package.elements[0] = original_object;
  515. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  516. "%s: Wrapped %s with expected Package object\n",
  517. info->full_pathname,
  518. acpi_ut_get_object_type_name(original_object)));
  519. /* Return the new object in the object pointer */
  520. *obj_desc_ptr = pkg_obj_desc;
  521. info->return_flags |= ACPI_OBJECT_REPAIRED | ACPI_OBJECT_WRAPPED;
  522. return (AE_OK);
  523. }