nsrepair.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. /*
  112. * At this point, we know that the type of the returned object was not
  113. * one of the expected types for this predefined name. Attempt to
  114. * repair the object by converting it to one of the expected object
  115. * types for this predefined name.
  116. */
  117. if (expected_btypes & ACPI_RTYPE_INTEGER) {
  118. status = acpi_ns_convert_to_integer(return_object, &new_object);
  119. if (ACPI_SUCCESS(status)) {
  120. goto object_repaired;
  121. }
  122. }
  123. if (expected_btypes & ACPI_RTYPE_STRING) {
  124. status = acpi_ns_convert_to_string(return_object, &new_object);
  125. if (ACPI_SUCCESS(status)) {
  126. goto object_repaired;
  127. }
  128. }
  129. if (expected_btypes & ACPI_RTYPE_BUFFER) {
  130. status = acpi_ns_convert_to_buffer(return_object, &new_object);
  131. if (ACPI_SUCCESS(status)) {
  132. goto object_repaired;
  133. }
  134. }
  135. if (expected_btypes & ACPI_RTYPE_PACKAGE) {
  136. status = acpi_ns_convert_to_package(return_object, &new_object);
  137. if (ACPI_SUCCESS(status)) {
  138. goto object_repaired;
  139. }
  140. }
  141. /* We cannot repair this object */
  142. return (AE_AML_OPERAND_TYPE);
  143. object_repaired:
  144. /* Object was successfully repaired */
  145. /*
  146. * If the original object is a package element, we need to:
  147. * 1. Set the reference count of the new object to match the
  148. * reference count of the old object.
  149. * 2. Decrement the reference count of the original object.
  150. */
  151. if (package_index != ACPI_NOT_PACKAGE_ELEMENT) {
  152. new_object->common.reference_count =
  153. return_object->common.reference_count;
  154. if (return_object->common.reference_count > 1) {
  155. return_object->common.reference_count--;
  156. }
  157. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  158. "Converted %s to expected %s at index %u",
  159. acpi_ut_get_object_type_name
  160. (return_object),
  161. acpi_ut_get_object_type_name(new_object),
  162. package_index));
  163. } else {
  164. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  165. "Converted %s to expected %s",
  166. acpi_ut_get_object_type_name
  167. (return_object),
  168. acpi_ut_get_object_type_name
  169. (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. /* All elements of the Package must be integers */
  351. elements = original_object->package.elements;
  352. count = original_object->package.count;
  353. for (i = 0; i < count; i++) {
  354. if ((!*elements) ||
  355. ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
  356. return (AE_AML_OPERAND_TYPE);
  357. }
  358. elements++;
  359. }
  360. /* Create the new buffer object to replace the Package */
  361. new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
  362. if (!new_object) {
  363. return (AE_NO_MEMORY);
  364. }
  365. /* Copy the package elements (integers) to the buffer as DWORDs */
  366. elements = original_object->package.elements;
  367. dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
  368. for (i = 0; i < count; i++) {
  369. *dword_buffer = (u32) (*elements)->integer.value;
  370. dword_buffer++;
  371. elements++;
  372. }
  373. break;
  374. default:
  375. return (AE_AML_OPERAND_TYPE);
  376. }
  377. *return_object = new_object;
  378. return (AE_OK);
  379. }
  380. /*******************************************************************************
  381. *
  382. * FUNCTION: acpi_ns_convert_to_package
  383. *
  384. * PARAMETERS: original_object - Object to be converted
  385. * return_object - Where the new converted object is returned
  386. *
  387. * RETURN: Status. AE_OK if conversion was successful.
  388. *
  389. * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
  390. * the buffer is converted to a single integer package element.
  391. *
  392. ******************************************************************************/
  393. static acpi_status
  394. acpi_ns_convert_to_package(union acpi_operand_object *original_object,
  395. union acpi_operand_object **return_object)
  396. {
  397. union acpi_operand_object *new_object;
  398. union acpi_operand_object **elements;
  399. u32 length;
  400. u8 *buffer;
  401. switch (original_object->common.type) {
  402. case ACPI_TYPE_BUFFER:
  403. /* Buffer-to-Package conversion */
  404. length = original_object->buffer.length;
  405. new_object = acpi_ut_create_package_object(length);
  406. if (!new_object) {
  407. return (AE_NO_MEMORY);
  408. }
  409. /* Convert each buffer byte to an integer package element */
  410. elements = new_object->package.elements;
  411. buffer = original_object->buffer.pointer;
  412. while (length--) {
  413. *elements =
  414. acpi_ut_create_integer_object((u64) *buffer);
  415. if (!*elements) {
  416. acpi_ut_remove_reference(new_object);
  417. return (AE_NO_MEMORY);
  418. }
  419. elements++;
  420. buffer++;
  421. }
  422. break;
  423. default:
  424. return (AE_AML_OPERAND_TYPE);
  425. }
  426. *return_object = new_object;
  427. return (AE_OK);
  428. }
  429. /*******************************************************************************
  430. *
  431. * FUNCTION: acpi_ns_repair_package_list
  432. *
  433. * PARAMETERS: Data - Pointer to validation data structure
  434. * obj_desc_ptr - Pointer to the object to repair. The new
  435. * package object is returned here,
  436. * overwriting the old object.
  437. *
  438. * RETURN: Status, new object in *obj_desc_ptr
  439. *
  440. * DESCRIPTION: Repair a common problem with objects that are defined to return
  441. * a variable-length Package of Packages. If the variable-length
  442. * is one, some BIOS code mistakenly simply declares a single
  443. * Package instead of a Package with one sub-Package. This
  444. * function attempts to repair this error by wrapping a Package
  445. * object around the original Package, creating the correct
  446. * Package with one sub-Package.
  447. *
  448. * Names that can be repaired in this manner include:
  449. * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
  450. *
  451. ******************************************************************************/
  452. acpi_status
  453. acpi_ns_repair_package_list(struct acpi_predefined_data *data,
  454. union acpi_operand_object **obj_desc_ptr)
  455. {
  456. union acpi_operand_object *pkg_obj_desc;
  457. /*
  458. * Create the new outer package and populate it. The new package will
  459. * have a single element, the lone subpackage.
  460. */
  461. pkg_obj_desc = acpi_ut_create_package_object(1);
  462. if (!pkg_obj_desc) {
  463. return (AE_NO_MEMORY);
  464. }
  465. pkg_obj_desc->package.elements[0] = *obj_desc_ptr;
  466. /* Return the new object in the object pointer */
  467. *obj_desc_ptr = pkg_obj_desc;
  468. data->flags |= ACPI_OBJECT_REPAIRED;
  469. ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
  470. "Repaired Incorrectly formed Package"));
  471. return (AE_OK);
  472. }