nsrepair.c 18 KB

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