nsconvert.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /******************************************************************************
  2. *
  3. * Module Name: nsconvert - Object conversions for objects returned by
  4. * predefined methods
  5. *
  6. *****************************************************************************/
  7. /*
  8. * Copyright (C) 2000 - 2016, 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_gbl_integer_byte_width, &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 |= ((u64)
  88. original_object->buffer.pointer[i] << (i *
  89. 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 = acpi_ex_convert_to_string(original_object,
  136. &new_object,
  137. ACPI_IMPLICIT_CONVERT_HEX);
  138. if (ACPI_FAILURE(status)) {
  139. return (status);
  140. }
  141. }
  142. break;
  143. case ACPI_TYPE_BUFFER:
  144. /*
  145. * Buffer-to-String conversion. Use a to_string
  146. * conversion, no transform performed on the buffer data. The best
  147. * example of this is the _BIF method, where the string data from
  148. * the battery is often (incorrectly) returned as buffer object(s).
  149. */
  150. length = 0;
  151. while ((length < original_object->buffer.length) &&
  152. (original_object->buffer.pointer[length])) {
  153. length++;
  154. }
  155. /* Allocate a new string object */
  156. new_object = acpi_ut_create_string_object(length);
  157. if (!new_object) {
  158. return (AE_NO_MEMORY);
  159. }
  160. /*
  161. * Copy the raw buffer data with no transform. String is already NULL
  162. * terminated at Length+1.
  163. */
  164. memcpy(new_object->string.pointer,
  165. original_object->buffer.pointer, length);
  166. break;
  167. default:
  168. return (AE_AML_OPERAND_TYPE);
  169. }
  170. *return_object = new_object;
  171. return (AE_OK);
  172. }
  173. /*******************************************************************************
  174. *
  175. * FUNCTION: acpi_ns_convert_to_buffer
  176. *
  177. * PARAMETERS: original_object - Object to be converted
  178. * return_object - Where the new converted object is returned
  179. *
  180. * RETURN: Status. AE_OK if conversion was successful.
  181. *
  182. * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
  183. *
  184. ******************************************************************************/
  185. acpi_status
  186. acpi_ns_convert_to_buffer(union acpi_operand_object *original_object,
  187. union acpi_operand_object **return_object)
  188. {
  189. union acpi_operand_object *new_object;
  190. acpi_status status;
  191. union acpi_operand_object **elements;
  192. u32 *dword_buffer;
  193. u32 count;
  194. u32 i;
  195. switch (original_object->common.type) {
  196. case ACPI_TYPE_INTEGER:
  197. /*
  198. * Integer-to-Buffer conversion.
  199. * Convert the Integer to a packed-byte buffer. _MAT and other
  200. * objects need this sometimes, if a read has been performed on a
  201. * Field object that is less than or equal to the global integer
  202. * size (32 or 64 bits).
  203. */
  204. status =
  205. acpi_ex_convert_to_buffer(original_object, &new_object);
  206. if (ACPI_FAILURE(status)) {
  207. return (status);
  208. }
  209. break;
  210. case ACPI_TYPE_STRING:
  211. /* String-to-Buffer conversion. Simple data copy */
  212. new_object = acpi_ut_create_buffer_object
  213. (original_object->string.length);
  214. if (!new_object) {
  215. return (AE_NO_MEMORY);
  216. }
  217. memcpy(new_object->buffer.pointer,
  218. original_object->string.pointer,
  219. original_object->string.length);
  220. break;
  221. case ACPI_TYPE_PACKAGE:
  222. /*
  223. * This case is often seen for predefined names that must return a
  224. * Buffer object with multiple DWORD integers within. For example,
  225. * _FDE and _GTM. The Package can be converted to a Buffer.
  226. */
  227. /* All elements of the Package must be integers */
  228. elements = original_object->package.elements;
  229. count = original_object->package.count;
  230. for (i = 0; i < count; i++) {
  231. if ((!*elements) ||
  232. ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
  233. return (AE_AML_OPERAND_TYPE);
  234. }
  235. elements++;
  236. }
  237. /* Create the new buffer object to replace the Package */
  238. new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
  239. if (!new_object) {
  240. return (AE_NO_MEMORY);
  241. }
  242. /* Copy the package elements (integers) to the buffer as DWORDs */
  243. elements = original_object->package.elements;
  244. dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
  245. for (i = 0; i < count; i++) {
  246. *dword_buffer = (u32)(*elements)->integer.value;
  247. dword_buffer++;
  248. elements++;
  249. }
  250. break;
  251. default:
  252. return (AE_AML_OPERAND_TYPE);
  253. }
  254. *return_object = new_object;
  255. return (AE_OK);
  256. }
  257. /*******************************************************************************
  258. *
  259. * FUNCTION: acpi_ns_convert_to_unicode
  260. *
  261. * PARAMETERS: scope - Namespace node for the method/object
  262. * original_object - ASCII String Object to be converted
  263. * return_object - Where the new converted object is returned
  264. *
  265. * RETURN: Status. AE_OK if conversion was successful.
  266. *
  267. * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
  268. *
  269. ******************************************************************************/
  270. acpi_status
  271. acpi_ns_convert_to_unicode(struct acpi_namespace_node *scope,
  272. 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: scope - Namespace node for the method/object
  315. * original_object - Object to be converted
  316. * return_object - Where the new converted object is returned
  317. *
  318. * RETURN: Status. AE_OK if conversion was successful
  319. *
  320. * DESCRIPTION: Attempt to convert a Integer object to a resource_template
  321. * Buffer.
  322. *
  323. ******************************************************************************/
  324. acpi_status
  325. acpi_ns_convert_to_resource(struct acpi_namespace_node *scope,
  326. union acpi_operand_object *original_object,
  327. union acpi_operand_object **return_object)
  328. {
  329. union acpi_operand_object *new_object;
  330. u8 *buffer;
  331. /*
  332. * We can fix the following cases for an expected resource template:
  333. * 1. No return value (interpreter slack mode is disabled)
  334. * 2. A "Return (Zero)" statement
  335. * 3. A "Return empty buffer" statement
  336. *
  337. * We will return a buffer containing a single end_tag
  338. * resource descriptor.
  339. */
  340. if (original_object) {
  341. switch (original_object->common.type) {
  342. case ACPI_TYPE_INTEGER:
  343. /* We can only repair an Integer==0 */
  344. if (original_object->integer.value) {
  345. return (AE_AML_OPERAND_TYPE);
  346. }
  347. break;
  348. case ACPI_TYPE_BUFFER:
  349. if (original_object->buffer.length) {
  350. /* Additional checks can be added in the future */
  351. *return_object = NULL;
  352. return (AE_OK);
  353. }
  354. break;
  355. case ACPI_TYPE_STRING:
  356. default:
  357. return (AE_AML_OPERAND_TYPE);
  358. }
  359. }
  360. /* Create the new buffer object for the resource descriptor */
  361. new_object = acpi_ut_create_buffer_object(2);
  362. if (!new_object) {
  363. return (AE_NO_MEMORY);
  364. }
  365. buffer = ACPI_CAST_PTR(u8, new_object->buffer.pointer);
  366. /* Initialize the Buffer with a single end_tag descriptor */
  367. buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
  368. buffer[1] = 0x00;
  369. *return_object = new_object;
  370. return (AE_OK);
  371. }
  372. /*******************************************************************************
  373. *
  374. * FUNCTION: acpi_ns_convert_to_reference
  375. *
  376. * PARAMETERS: scope - Namespace node for the method/object
  377. * original_object - Object to be converted
  378. * return_object - Where the new converted object is returned
  379. *
  380. * RETURN: Status. AE_OK if conversion was successful
  381. *
  382. * DESCRIPTION: Attempt to convert a Integer object to a object_reference.
  383. * Buffer.
  384. *
  385. ******************************************************************************/
  386. acpi_status
  387. acpi_ns_convert_to_reference(struct acpi_namespace_node *scope,
  388. union acpi_operand_object *original_object,
  389. union acpi_operand_object **return_object)
  390. {
  391. union acpi_operand_object *new_object = NULL;
  392. acpi_status status;
  393. struct acpi_namespace_node *node;
  394. union acpi_generic_state scope_info;
  395. char *name;
  396. ACPI_FUNCTION_NAME(ns_convert_to_reference);
  397. /* Convert path into internal presentation */
  398. status =
  399. acpi_ns_internalize_name(original_object->string.pointer, &name);
  400. if (ACPI_FAILURE(status)) {
  401. return_ACPI_STATUS(status);
  402. }
  403. /* Find the namespace node */
  404. scope_info.scope.node =
  405. ACPI_CAST_PTR(struct acpi_namespace_node, scope);
  406. status =
  407. acpi_ns_lookup(&scope_info, name, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
  408. ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
  409. NULL, &node);
  410. if (ACPI_FAILURE(status)) {
  411. /* Check if we are resolving a named reference within a package */
  412. ACPI_ERROR_NAMESPACE(original_object->string.pointer, status);
  413. goto error_exit;
  414. }
  415. /* Create and init a new internal ACPI object */
  416. new_object = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  417. if (!new_object) {
  418. status = AE_NO_MEMORY;
  419. goto error_exit;
  420. }
  421. new_object->reference.node = node;
  422. new_object->reference.object = node->object;
  423. new_object->reference.class = ACPI_REFCLASS_NAME;
  424. /*
  425. * Increase reference of the object if needed (the object is likely a
  426. * null for device nodes).
  427. */
  428. acpi_ut_add_reference(node->object);
  429. error_exit:
  430. ACPI_FREE(name);
  431. *return_object = new_object;
  432. return (AE_OK);
  433. }