nsconvert.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /******************************************************************************
  2. *
  3. * Module Name: nsconvert - Object conversions for objects returned by
  4. * predefined methods
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2013, Intel Corp.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions, and the following disclaimer,
  16. * without modification.
  17. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  18. * substantially similar to the "NO WARRANTY" disclaimer below
  19. * ("Disclaimer") and any redistribution must be conditioned upon
  20. * including a substantially similar Disclaimer requirement for further
  21. * binary redistribution.
  22. * 3. Neither the names of the above-listed copyright holders nor the names
  23. * of any contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * Alternatively, this software may be distributed under the terms of the
  27. * GNU General Public License ("GPL") version 2 as published by the Free
  28. * Software Foundation.
  29. *
  30. * NO WARRANTY
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  40. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGES.
  42. */
  43. #include <acpi/acpi.h>
  44. #include "accommon.h"
  45. #include "acnamesp.h"
  46. #include "acinterp.h"
  47. #include "acpredef.h"
  48. #include "amlresrc.h"
  49. #define _COMPONENT ACPI_NAMESPACE
  50. ACPI_MODULE_NAME("nsconvert")
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ns_convert_to_integer
  54. *
  55. * PARAMETERS: original_object - Object to be converted
  56. * return_object - Where the new converted object is returned
  57. *
  58. * RETURN: Status. AE_OK if conversion was successful.
  59. *
  60. * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
  61. *
  62. ******************************************************************************/
  63. acpi_status
  64. acpi_ns_convert_to_integer(union acpi_operand_object *original_object,
  65. union acpi_operand_object **return_object)
  66. {
  67. union acpi_operand_object *new_object;
  68. acpi_status status;
  69. u64 value = 0;
  70. u32 i;
  71. switch (original_object->common.type) {
  72. case ACPI_TYPE_STRING:
  73. /* String-to-Integer conversion */
  74. status = acpi_ut_strtoul64(original_object->string.pointer,
  75. ACPI_ANY_BASE, &value);
  76. if (ACPI_FAILURE(status)) {
  77. return (status);
  78. }
  79. break;
  80. case ACPI_TYPE_BUFFER:
  81. /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
  82. if (original_object->buffer.length > 8) {
  83. return (AE_AML_OPERAND_TYPE);
  84. }
  85. /* Extract each buffer byte to create the integer */
  86. for (i = 0; i < original_object->buffer.length; i++) {
  87. value |=
  88. ((u64)original_object->buffer.
  89. pointer[i] << (i * 8));
  90. }
  91. break;
  92. default:
  93. return (AE_AML_OPERAND_TYPE);
  94. }
  95. new_object = acpi_ut_create_integer_object(value);
  96. if (!new_object) {
  97. return (AE_NO_MEMORY);
  98. }
  99. *return_object = new_object;
  100. return (AE_OK);
  101. }
  102. /*******************************************************************************
  103. *
  104. * FUNCTION: acpi_ns_convert_to_string
  105. *
  106. * PARAMETERS: original_object - Object to be converted
  107. * return_object - Where the new converted object is returned
  108. *
  109. * RETURN: Status. AE_OK if conversion was successful.
  110. *
  111. * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
  112. *
  113. ******************************************************************************/
  114. acpi_status
  115. acpi_ns_convert_to_string(union acpi_operand_object *original_object,
  116. union acpi_operand_object **return_object)
  117. {
  118. union acpi_operand_object *new_object;
  119. acpi_size length;
  120. acpi_status status;
  121. switch (original_object->common.type) {
  122. case ACPI_TYPE_INTEGER:
  123. /*
  124. * Integer-to-String conversion. Commonly, convert
  125. * an integer of value 0 to a NULL string. The last element of
  126. * _BIF and _BIX packages occasionally need this fix.
  127. */
  128. if (original_object->integer.value == 0) {
  129. /* Allocate a new NULL string object */
  130. new_object = acpi_ut_create_string_object(0);
  131. if (!new_object) {
  132. return (AE_NO_MEMORY);
  133. }
  134. } else {
  135. status =
  136. acpi_ex_convert_to_string(original_object,
  137. &new_object,
  138. ACPI_IMPLICIT_CONVERT_HEX);
  139. if (ACPI_FAILURE(status)) {
  140. return (status);
  141. }
  142. }
  143. break;
  144. case ACPI_TYPE_BUFFER:
  145. /*
  146. * Buffer-to-String conversion. Use a to_string
  147. * conversion, no transform performed on the buffer data. The best
  148. * example of this is the _BIF method, where the string data from
  149. * the battery is often (incorrectly) returned as buffer object(s).
  150. */
  151. length = 0;
  152. while ((length < original_object->buffer.length) &&
  153. (original_object->buffer.pointer[length])) {
  154. length++;
  155. }
  156. /* Allocate a new string object */
  157. new_object = acpi_ut_create_string_object(length);
  158. if (!new_object) {
  159. return (AE_NO_MEMORY);
  160. }
  161. /*
  162. * Copy the raw buffer data with no transform. String is already NULL
  163. * terminated at Length+1.
  164. */
  165. ACPI_MEMCPY(new_object->string.pointer,
  166. original_object->buffer.pointer, length);
  167. break;
  168. default:
  169. return (AE_AML_OPERAND_TYPE);
  170. }
  171. *return_object = new_object;
  172. return (AE_OK);
  173. }
  174. /*******************************************************************************
  175. *
  176. * FUNCTION: acpi_ns_convert_to_buffer
  177. *
  178. * PARAMETERS: original_object - Object to be converted
  179. * return_object - Where the new converted object is returned
  180. *
  181. * RETURN: Status. AE_OK if conversion was successful.
  182. *
  183. * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
  184. *
  185. ******************************************************************************/
  186. acpi_status
  187. acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
  188. union acpi_operand_object **return_object)
  189. {
  190. union acpi_operand_object *new_object;
  191. acpi_status status;
  192. union acpi_operand_object **elements;
  193. u32 *dword_buffer;
  194. u32 count;
  195. u32 i;
  196. switch (original_object->common.type) {
  197. case ACPI_TYPE_INTEGER:
  198. /*
  199. * Integer-to-Buffer conversion.
  200. * Convert the Integer to a packed-byte buffer. _MAT and other
  201. * objects need this sometimes, if a read has been performed on a
  202. * Field object that is less than or equal to the global integer
  203. * size (32 or 64 bits).
  204. */
  205. status =
  206. acpi_ex_convert_to_buffer(original_object, &new_object);
  207. if (ACPI_FAILURE(status)) {
  208. return (status);
  209. }
  210. break;
  211. case ACPI_TYPE_STRING:
  212. /* String-to-Buffer conversion. Simple data copy */
  213. new_object =
  214. acpi_ut_create_buffer_object(original_object->string.
  215. length);
  216. if (!new_object) {
  217. return (AE_NO_MEMORY);
  218. }
  219. ACPI_MEMCPY(new_object->buffer.pointer,
  220. original_object->string.pointer,
  221. original_object->string.length);
  222. break;
  223. case ACPI_TYPE_PACKAGE:
  224. /*
  225. * This case is often seen for predefined names that must return a
  226. * Buffer object with multiple DWORD integers within. For example,
  227. * _FDE and _GTM. The Package can be converted to a Buffer.
  228. */
  229. /* All elements of the Package must be integers */
  230. elements = original_object->package.elements;
  231. count = original_object->package.count;
  232. for (i = 0; i < count; i++) {
  233. if ((!*elements) ||
  234. ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
  235. return (AE_AML_OPERAND_TYPE);
  236. }
  237. elements++;
  238. }
  239. /* Create the new buffer object to replace the Package */
  240. new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
  241. if (!new_object) {
  242. return (AE_NO_MEMORY);
  243. }
  244. /* Copy the package elements (integers) to the buffer as DWORDs */
  245. elements = original_object->package.elements;
  246. dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
  247. for (i = 0; i < count; i++) {
  248. *dword_buffer = (u32)(*elements)->integer.value;
  249. dword_buffer++;
  250. elements++;
  251. }
  252. break;
  253. default:
  254. return (AE_AML_OPERAND_TYPE);
  255. }
  256. *return_object = new_object;
  257. return (AE_OK);
  258. }
  259. /*******************************************************************************
  260. *
  261. * FUNCTION: acpi_ns_convert_to_unicode
  262. *
  263. * PARAMETERS: original_object - ASCII String Object to be converted
  264. * return_object - Where the new converted object is returned
  265. *
  266. * RETURN: Status. AE_OK if conversion was successful.
  267. *
  268. * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
  269. *
  270. ******************************************************************************/
  271. acpi_status
  272. acpi_ns_convert_to_unicode(union acpi_operand_object *original_object,
  273. union acpi_operand_object **return_object)
  274. {
  275. union acpi_operand_object *new_object;
  276. char *ascii_string;
  277. u16 *unicode_buffer;
  278. u32 unicode_length;
  279. u32 i;
  280. if (!original_object) {
  281. return (AE_OK);
  282. }
  283. /* If a Buffer was returned, it must be at least two bytes long */
  284. if (original_object->common.type == ACPI_TYPE_BUFFER) {
  285. if (original_object->buffer.length < 2) {
  286. return (AE_AML_OPERAND_VALUE);
  287. }
  288. *return_object = NULL;
  289. return (AE_OK);
  290. }
  291. /*
  292. * The original object is an ASCII string. Convert this string to
  293. * a unicode buffer.
  294. */
  295. ascii_string = original_object->string.pointer;
  296. unicode_length = (original_object->string.length * 2) + 2;
  297. /* Create a new buffer object for the Unicode data */
  298. new_object = acpi_ut_create_buffer_object(unicode_length);
  299. if (!new_object) {
  300. return (AE_NO_MEMORY);
  301. }
  302. unicode_buffer = ACPI_CAST_PTR(u16, new_object->buffer.pointer);
  303. /* Convert ASCII to Unicode */
  304. for (i = 0; i < original_object->string.length; i++) {
  305. unicode_buffer[i] = (u16)ascii_string[i];
  306. }
  307. *return_object = new_object;
  308. return (AE_OK);
  309. }
  310. /*******************************************************************************
  311. *
  312. * FUNCTION: acpi_ns_convert_to_resource
  313. *
  314. * PARAMETERS: original_object - Object to be converted
  315. * return_object - Where the new converted object is returned
  316. *
  317. * RETURN: Status. AE_OK if conversion was successful
  318. *
  319. * DESCRIPTION: Attempt to convert a Integer object to a resource_template
  320. * Buffer.
  321. *
  322. ******************************************************************************/
  323. acpi_status
  324. acpi_ns_convert_to_resource(union acpi_operand_object *original_object,
  325. union acpi_operand_object **return_object)
  326. {
  327. union acpi_operand_object *new_object;
  328. u8 *buffer;
  329. /*
  330. * We can fix the following cases for an expected resource template:
  331. * 1. No return value (interpreter slack mode is disabled)
  332. * 2. A "Return (Zero)" statement
  333. * 3. A "Return empty buffer" statement
  334. *
  335. * We will return a buffer containing a single end_tag
  336. * resource descriptor.
  337. */
  338. if (original_object) {
  339. switch (original_object->common.type) {
  340. case ACPI_TYPE_INTEGER:
  341. /* We can only repair an Integer==0 */
  342. if (original_object->integer.value) {
  343. return (AE_AML_OPERAND_TYPE);
  344. }
  345. break;
  346. case ACPI_TYPE_BUFFER:
  347. if (original_object->buffer.length) {
  348. /* Additional checks can be added in the future */
  349. *return_object = NULL;
  350. return (AE_OK);
  351. }
  352. break;
  353. case ACPI_TYPE_STRING:
  354. default:
  355. return (AE_AML_OPERAND_TYPE);
  356. }
  357. }
  358. /* Create the new buffer object for the resource descriptor */
  359. new_object = acpi_ut_create_buffer_object(2);
  360. if (!new_object) {
  361. return (AE_NO_MEMORY);
  362. }
  363. buffer = ACPI_CAST_PTR(u8, new_object->buffer.pointer);
  364. /* Initialize the Buffer with a single end_tag descriptor */
  365. buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
  366. buffer[1] = 0x00;
  367. *return_object = new_object;
  368. return (AE_OK);
  369. }