nsrepair.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /******************************************************************************
  2. *
  3. * Module Name: nsrepair - Repair for objects returned by predefined methods
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2009, 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. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsrepair")
  48. /*******************************************************************************
  49. *
  50. * This module attempts to repair or convert objects returned by the
  51. * predefined methods to an object type that is expected, as per the ACPI
  52. * specification. The need for this code is dictated by the many machines that
  53. * return incorrect types for the standard predefined methods. Performing these
  54. * conversions here, in one place, eliminates the need for individual ACPI
  55. * device drivers to do the same. Note: Most of these conversions are different
  56. * than the internal object conversion routines used for implicit object
  57. * conversion.
  58. *
  59. * The following conversions can be performed as necessary:
  60. *
  61. * Integer -> String
  62. * Integer -> Buffer
  63. * String -> Integer
  64. * String -> Buffer
  65. * Buffer -> Integer
  66. * Buffer -> String
  67. * Buffer -> Package of Integers
  68. * Package -> Package of one Package
  69. *
  70. ******************************************************************************/
  71. /* Local prototypes */
  72. static acpi_status
  73. acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
  74. union acpi_operand_object **return_object);
  75. static acpi_status
  76. acpi_ns_convert_to_string(union acpi_operand_object *original_object,
  77. union acpi_operand_object **return_object);
  78. static acpi_status
  79. acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
  80. union acpi_operand_object **return_object);
  81. static acpi_status
  82. acpi_ns_convert_to_package(union acpi_operand_object *original_object,
  83. union acpi_operand_object **return_object);
  84. /*******************************************************************************
  85. *
  86. * FUNCTION: acpi_ns_repair_object
  87. *
  88. * PARAMETERS: Data - Pointer to validation data structure
  89. * expected_btypes - Object types expected
  90. * package_index - Index of object within parent package (if
  91. * applicable - ACPI_NOT_PACKAGE_ELEMENT
  92. * otherwise)
  93. * return_object_ptr - Pointer to the object returned from the
  94. * evaluation of a method or object
  95. *
  96. * RETURN: Status. AE_OK if repair was successful.
  97. *
  98. * DESCRIPTION: Attempt to repair/convert a return object of a type that was
  99. * not expected.
  100. *
  101. ******************************************************************************/
  102. acpi_status
  103. acpi_ns_repair_object(struct acpi_predefined_data *data,
  104. u32 expected_btypes,
  105. u32 package_index,
  106. union acpi_operand_object **return_object_ptr)
  107. {
  108. union acpi_operand_object *return_object = *return_object_ptr;
  109. union acpi_operand_object *new_object;
  110. acpi_status status;
  111. ACPI_FUNCTION_NAME(ns_repair_object);
  112. /*
  113. * At this point, we know that the type of the returned object was not
  114. * one of the expected types for this predefined name. Attempt to
  115. * repair the object by converting it to one of the expected object
  116. * types for this predefined name.
  117. */
  118. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  119. status = acpi_ns_convert_to_integer(return_object, &new_object);
  120. if (ACPI_SUCCESS(status)) {
  121. goto object_repaired;
  122. }
  123. }
  124. if (expected_btypes & ACPI_RTYPE_STRING) {
  125. status = acpi_ns_convert_to_string(return_object, &new_object);
  126. if (ACPI_SUCCESS(status)) {
  127. goto object_repaired;
  128. }
  129. }
  130. if (expected_btypes & ACPI_RTYPE_BUFFER) {
  131. status = acpi_ns_convert_to_buffer(return_object, &new_object);
  132. if (ACPI_SUCCESS(status)) {
  133. goto object_repaired;
  134. }
  135. }
  136. if (expected_btypes & ACPI_RTYPE_PACKAGE) {
  137. status = acpi_ns_convert_to_package(return_object, &new_object);
  138. if (ACPI_SUCCESS(status)) {
  139. goto object_repaired;
  140. }
  141. }
  142. /* We cannot repair this object */
  143. return (AE_AML_OPERAND_TYPE);
  144. object_repaired:
  145. /* Object was successfully repaired */
  146. /*
  147. * If the original object is a package element, we need to:
  148. * 1. Set the reference count of the new object to match the
  149. * reference count of the old object.
  150. * 2. Decrement the reference count of the original object.
  151. */
  152. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  153. new_object->common.reference_count =
  154. return_object->common.reference_count;
  155. if (return_object->common.reference_count > 1) {
  156. return_object->common.reference_count--;
  157. }
  158. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  159. "%s: Converted %s to expected %s at index %u\n",
  160. data->pathname,
  161. acpi_ut_get_object_type_name(return_object),
  162. acpi_ut_get_object_type_name(new_object),
  163. package_index));
  164. } else {
  165. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  166. "%s: Converted %s to expected %s\n",
  167. data->pathname,
  168. acpi_ut_get_object_type_name(return_object),
  169. acpi_ut_get_object_type_name(new_object)));
  170. }
  171. /* Delete old object, install the new return object */
  172. acpi_ut_remove_reference(return_object);
  173. *return_object_ptr = new_object;
  174. data->flags |= ACPI_OBJECT_REPAIRED;
  175. return (AE_OK);
  176. }
  177. /*******************************************************************************
  178. *
  179. * FUNCTION: acpi_ns_convert_to_integer
  180. *
  181. * PARAMETERS: original_object - Object to be converted
  182. * return_object - Where the new converted object is returned
  183. *
  184. * RETURN: Status. AE_OK if conversion was successful.
  185. *
  186. * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
  187. *
  188. ******************************************************************************/
  189. static acpi_status
  190. acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
  191. union acpi_operand_object **return_object)
  192. {
  193. union acpi_operand_object *new_object;
  194. acpi_status status;
  195. u64 value = 0;
  196. u32 i;
  197. switch (original_object->common.type) {
  198. case ACPI_TYPE_STRING:
  199. /* String-to-Integer conversion */
  200. status = acpi_ut_strtoul64(original_object->string.pointer,
  201. ACPI_ANY_BASE, &value);
  202. if (ACPI_FAILURE(status)) {
  203. return (status);
  204. }
  205. break;
  206. case ACPI_TYPE_BUFFER:
  207. /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
  208. if (original_object->buffer.length > 8) {
  209. return (AE_AML_OPERAND_TYPE);
  210. }
  211. /* Extract each buffer byte to create the integer */
  212. for (i = 0; i < original_object->buffer.length; i++) {
  213. value |=
  214. ((u64) original_object->buffer.
  215. pointer[i] << (i * 8));
  216. }
  217. break;
  218. default:
  219. return (AE_AML_OPERAND_TYPE);
  220. }
  221. new_object = acpi_ut_create_integer_object(value);
  222. if (!new_object) {
  223. return (AE_NO_MEMORY);
  224. }
  225. *return_object = new_object;
  226. return (AE_OK);
  227. }
  228. /*******************************************************************************
  229. *
  230. * FUNCTION: acpi_ns_convert_to_string
  231. *
  232. * PARAMETERS: original_object - Object to be converted
  233. * return_object - Where the new converted object is returned
  234. *
  235. * RETURN: Status. AE_OK if conversion was successful.
  236. *
  237. * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
  238. *
  239. ******************************************************************************/
  240. static acpi_status
  241. acpi_ns_convert_to_string(union acpi_operand_object *original_object,
  242. union acpi_operand_object **return_object)
  243. {
  244. union acpi_operand_object *new_object;
  245. acpi_size length;
  246. acpi_status status;
  247. switch (original_object->common.type) {
  248. case ACPI_TYPE_INTEGER:
  249. /*
  250. * Integer-to-String conversion. Commonly, convert
  251. * an integer of value 0 to a NULL string. The last element of
  252. * _BIF and _BIX packages occasionally need this fix.
  253. */
  254. if (original_object->integer.value == 0) {
  255. /* Allocate a new NULL string object */
  256. new_object = acpi_ut_create_string_object(0);
  257. if (!new_object) {
  258. return (AE_NO_MEMORY);
  259. }
  260. } else {
  261. status =
  262. acpi_ex_convert_to_string(original_object,
  263. &new_object,
  264. ACPI_IMPLICIT_CONVERT_HEX);
  265. if (ACPI_FAILURE(status)) {
  266. return (status);
  267. }
  268. }
  269. break;
  270. case ACPI_TYPE_BUFFER:
  271. /*
  272. * Buffer-to-String conversion. Use a to_string
  273. * conversion, no transform performed on the buffer data. The best
  274. * example of this is the _BIF method, where the string data from
  275. * the battery is often (incorrectly) returned as buffer object(s).
  276. */
  277. length = 0;
  278. while ((length < original_object->buffer.length) &&
  279. (original_object->buffer.pointer[length])) {
  280. length++;
  281. }
  282. /* Allocate a new string object */
  283. new_object = acpi_ut_create_string_object(length);
  284. if (!new_object) {
  285. return (AE_NO_MEMORY);
  286. }
  287. /*
  288. * Copy the raw buffer data with no transform. String is already NULL
  289. * terminated at Length+1.
  290. */
  291. ACPI_MEMCPY(new_object->string.pointer,
  292. original_object->buffer.pointer, length);
  293. break;
  294. default:
  295. return (AE_AML_OPERAND_TYPE);
  296. }
  297. *return_object = new_object;
  298. return (AE_OK);
  299. }
  300. /*******************************************************************************
  301. *
  302. * FUNCTION: acpi_ns_convert_to_buffer
  303. *
  304. * PARAMETERS: original_object - Object to be converted
  305. * return_object - Where the new converted object is returned
  306. *
  307. * RETURN: Status. AE_OK if conversion was successful.
  308. *
  309. * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
  310. *
  311. ******************************************************************************/
  312. static acpi_status
  313. acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
  314. union acpi_operand_object **return_object)
  315. {
  316. union acpi_operand_object *new_object;
  317. acpi_status status;
  318. union acpi_operand_object **elements;
  319. u32 *dword_buffer;
  320. u32 count;
  321. u32 i;
  322. switch (original_object->common.type) {
  323. case ACPI_TYPE_INTEGER:
  324. /*
  325. * Integer-to-Buffer conversion.
  326. * Convert the Integer to a packed-byte buffer. _MAT and other
  327. * objects need this sometimes, if a read has been performed on a
  328. * Field object that is less than or equal to the global integer
  329. * size (32 or 64 bits).
  330. */
  331. status =
  332. acpi_ex_convert_to_buffer(original_object, &new_object);
  333. if (ACPI_FAILURE(status)) {
  334. return (status);
  335. }
  336. break;
  337. case ACPI_TYPE_STRING:
  338. /* String-to-Buffer conversion. Simple data copy */
  339. new_object =
  340. acpi_ut_create_buffer_object(original_object->string.
  341. length);
  342. if (!new_object) {
  343. return (AE_NO_MEMORY);
  344. }
  345. ACPI_MEMCPY(new_object->buffer.pointer,
  346. original_object->string.pointer,
  347. original_object->string.length);
  348. break;
  349. case ACPI_TYPE_PACKAGE:
  350. /*
  351. * This case is often seen for predefined names that must return a
  352. * Buffer object with multiple DWORD integers within. For example,
  353. * _FDE and _GTM. The Package can be converted to a Buffer.
  354. */
  355. /* All elements of the Package must be integers */
  356. elements = original_object->package.elements;
  357. count = original_object->package.count;
  358. for (i = 0; i < count; i++) {
  359. if ((!*elements) ||
  360. ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
  361. return (AE_AML_OPERAND_TYPE);
  362. }
  363. elements++;
  364. }
  365. /* Create the new buffer object to replace the Package */
  366. new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
  367. if (!new_object) {
  368. return (AE_NO_MEMORY);
  369. }
  370. /* Copy the package elements (integers) to the buffer as DWORDs */
  371. elements = original_object->package.elements;
  372. dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
  373. for (i = 0; i < count; i++) {
  374. *dword_buffer = (u32) (*elements)->integer.value;
  375. dword_buffer++;
  376. elements++;
  377. }
  378. break;
  379. default:
  380. return (AE_AML_OPERAND_TYPE);
  381. }
  382. *return_object = new_object;
  383. return (AE_OK);
  384. }
  385. /*******************************************************************************
  386. *
  387. * FUNCTION: acpi_ns_convert_to_package
  388. *
  389. * PARAMETERS: original_object - Object to be converted
  390. * return_object - Where the new converted object is returned
  391. *
  392. * RETURN: Status. AE_OK if conversion was successful.
  393. *
  394. * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
  395. * the buffer is converted to a single integer package element.
  396. *
  397. ******************************************************************************/
  398. static acpi_status
  399. acpi_ns_convert_to_package(union acpi_operand_object *original_object,
  400. union acpi_operand_object **return_object)
  401. {
  402. union acpi_operand_object *new_object;
  403. union acpi_operand_object **elements;
  404. u32 length;
  405. u8 *buffer;
  406. switch (original_object->common.type) {
  407. case ACPI_TYPE_BUFFER:
  408. /* Buffer-to-Package conversion */
  409. length = original_object->buffer.length;
  410. new_object = acpi_ut_create_package_object(length);
  411. if (!new_object) {
  412. return (AE_NO_MEMORY);
  413. }
  414. /* Convert each buffer byte to an integer package element */
  415. elements = new_object->package.elements;
  416. buffer = original_object->buffer.pointer;
  417. while (length--) {
  418. *elements =
  419. acpi_ut_create_integer_object((u64) *buffer);
  420. if (!*elements) {
  421. acpi_ut_remove_reference(new_object);
  422. return (AE_NO_MEMORY);
  423. }
  424. elements++;
  425. buffer++;
  426. }
  427. break;
  428. default:
  429. return (AE_AML_OPERAND_TYPE);
  430. }
  431. *return_object = new_object;
  432. return (AE_OK);
  433. }
  434. /*******************************************************************************
  435. *
  436. * FUNCTION: acpi_ns_repair_package_list
  437. *
  438. * PARAMETERS: Data - Pointer to validation data structure
  439. * obj_desc_ptr - Pointer to the object to repair. The new
  440. * package object is returned here,
  441. * overwriting the old object.
  442. *
  443. * RETURN: Status, new object in *obj_desc_ptr
  444. *
  445. * DESCRIPTION: Repair a common problem with objects that are defined to return
  446. * a variable-length Package of Packages. If the variable-length
  447. * is one, some BIOS code mistakenly simply declares a single
  448. * Package instead of a Package with one sub-Package. This
  449. * function attempts to repair this error by wrapping a Package
  450. * object around the original Package, creating the correct
  451. * Package with one sub-Package.
  452. *
  453. * Names that can be repaired in this manner include:
  454. * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
  455. *
  456. ******************************************************************************/
  457. acpi_status
  458. acpi_ns_repair_package_list(struct acpi_predefined_data *data,
  459. union acpi_operand_object **obj_desc_ptr)
  460. {
  461. union acpi_operand_object *pkg_obj_desc;
  462. ACPI_FUNCTION_NAME(ns_repair_package_list);
  463. /*
  464. * Create the new outer package and populate it. The new package will
  465. * have a single element, the lone subpackage.
  466. */
  467. pkg_obj_desc = acpi_ut_create_package_object(1);
  468. if (!pkg_obj_desc) {
  469. return (AE_NO_MEMORY);
  470. }
  471. pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
  472. /* Return the new object in the object pointer */
  473. *obj_desc_ptr = pkg_obj_desc;
  474. data->flags |= ACPI_OBJECT_REPAIRED;
  475. ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
  476. "%s: Repaired incorrectly formed Package\n",
  477. data->pathname));
  478. return (AE_OK);
  479. }