nsrepair.c 19 KB

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