nsconvert.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_ANY_BASE,
  76. acpi_gbl_integer_byte_width, &value);
  77. if (ACPI_FAILURE(status)) {
  78. return (status);
  79. }
  80. break;
  81. case ACPI_TYPE_BUFFER:
  82. /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
  83. if (original_object->buffer.length > 8) {
  84. return (AE_AML_OPERAND_TYPE);
  85. }
  86. /* Extract each buffer byte to create the integer */
  87. for (i = 0; i < original_object->buffer.length; i++) {
  88. value |= ((u64)
  89. original_object->buffer.pointer[i] << (i *
  90. 8));
  91. }
  92. break;
  93. default:
  94. return (AE_AML_OPERAND_TYPE);
  95. }
  96. new_object = acpi_ut_create_integer_object(value);
  97. if (!new_object) {
  98. return (AE_NO_MEMORY);
  99. }
  100. *return_object = new_object;
  101. return (AE_OK);
  102. }
  103. /*******************************************************************************
  104. *
  105. * FUNCTION: acpi_ns_convert_to_string
  106. *
  107. * PARAMETERS: original_object - Object to be converted
  108. * return_object - Where the new converted object is returned
  109. *
  110. * RETURN: Status. AE_OK if conversion was successful.
  111. *
  112. * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
  113. *
  114. ******************************************************************************/
  115. acpi_status
  116. acpi_ns_convert_to_string(union acpi_operand_object *original_object,
  117. union acpi_operand_object **return_object)
  118. {
  119. union acpi_operand_object *new_object;
  120. acpi_size length;
  121. acpi_status status;
  122. switch (original_object->common.type) {
  123. case ACPI_TYPE_INTEGER:
  124. /*
  125. * Integer-to-String conversion. Commonly, convert
  126. * an integer of value 0 to a NULL string. The last element of
  127. * _BIF and _BIX packages occasionally need this fix.
  128. */
  129. if (original_object->integer.value == 0) {
  130. /* Allocate a new NULL string object */
  131. new_object = acpi_ut_create_string_object(0);
  132. if (!new_object) {
  133. return (AE_NO_MEMORY);
  134. }
  135. } else {
  136. status = 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. 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 = acpi_ut_create_buffer_object
  214. (original_object->string.length);
  215. if (!new_object) {
  216. return (AE_NO_MEMORY);
  217. }
  218. memcpy(new_object->buffer.pointer,
  219. original_object->string.pointer,
  220. original_object->string.length);
  221. break;
  222. case ACPI_TYPE_PACKAGE:
  223. /*
  224. * This case is often seen for predefined names that must return a
  225. * Buffer object with multiple DWORD integers within. For example,
  226. * _FDE and _GTM. The Package can be converted to a Buffer.
  227. */
  228. /* All elements of the Package must be integers */
  229. elements = original_object->package.elements;
  230. count = original_object->package.count;
  231. for (i = 0; i < count; i++) {
  232. if ((!*elements) ||
  233. ((*elements)->common.type != ACPI_TYPE_INTEGER)) {
  234. return (AE_AML_OPERAND_TYPE);
  235. }
  236. elements++;
  237. }
  238. /* Create the new buffer object to replace the Package */
  239. new_object = acpi_ut_create_buffer_object(ACPI_MUL_4(count));
  240. if (!new_object) {
  241. return (AE_NO_MEMORY);
  242. }
  243. /* Copy the package elements (integers) to the buffer as DWORDs */
  244. elements = original_object->package.elements;
  245. dword_buffer = ACPI_CAST_PTR(u32, new_object->buffer.pointer);
  246. for (i = 0; i < count; i++) {
  247. *dword_buffer = (u32)(*elements)->integer.value;
  248. dword_buffer++;
  249. elements++;
  250. }
  251. break;
  252. default:
  253. return (AE_AML_OPERAND_TYPE);
  254. }
  255. *return_object = new_object;
  256. return (AE_OK);
  257. }
  258. /*******************************************************************************
  259. *
  260. * FUNCTION: acpi_ns_convert_to_unicode
  261. *
  262. * PARAMETERS: scope - Namespace node for the method/object
  263. * 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(struct acpi_namespace_node * scope,
  273. union acpi_operand_object *original_object,
  274. union acpi_operand_object **return_object)
  275. {
  276. union acpi_operand_object *new_object;
  277. char *ascii_string;
  278. u16 *unicode_buffer;
  279. u32 unicode_length;
  280. u32 i;
  281. if (!original_object) {
  282. return (AE_OK);
  283. }
  284. /* If a Buffer was returned, it must be at least two bytes long */
  285. if (original_object->common.type == ACPI_TYPE_BUFFER) {
  286. if (original_object->buffer.length < 2) {
  287. return (AE_AML_OPERAND_VALUE);
  288. }
  289. *return_object = NULL;
  290. return (AE_OK);
  291. }
  292. /*
  293. * The original object is an ASCII string. Convert this string to
  294. * a unicode buffer.
  295. */
  296. ascii_string = original_object->string.pointer;
  297. unicode_length = (original_object->string.length * 2) + 2;
  298. /* Create a new buffer object for the Unicode data */
  299. new_object = acpi_ut_create_buffer_object(unicode_length);
  300. if (!new_object) {
  301. return (AE_NO_MEMORY);
  302. }
  303. unicode_buffer = ACPI_CAST_PTR(u16, new_object->buffer.pointer);
  304. /* Convert ASCII to Unicode */
  305. for (i = 0; i < original_object->string.length; i++) {
  306. unicode_buffer[i] = (u16)ascii_string[i];
  307. }
  308. *return_object = new_object;
  309. return (AE_OK);
  310. }
  311. /*******************************************************************************
  312. *
  313. * FUNCTION: acpi_ns_convert_to_resource
  314. *
  315. * PARAMETERS: scope - Namespace node for the method/object
  316. * original_object - Object to be converted
  317. * return_object - Where the new converted object is returned
  318. *
  319. * RETURN: Status. AE_OK if conversion was successful
  320. *
  321. * DESCRIPTION: Attempt to convert a Integer object to a resource_template
  322. * Buffer.
  323. *
  324. ******************************************************************************/
  325. acpi_status
  326. acpi_ns_convert_to_resource(struct acpi_namespace_node * scope,
  327. union acpi_operand_object *original_object,
  328. union acpi_operand_object **return_object)
  329. {
  330. union acpi_operand_object *new_object;
  331. u8 *buffer;
  332. /*
  333. * We can fix the following cases for an expected resource template:
  334. * 1. No return value (interpreter slack mode is disabled)
  335. * 2. A "Return (Zero)" statement
  336. * 3. A "Return empty buffer" statement
  337. *
  338. * We will return a buffer containing a single end_tag
  339. * resource descriptor.
  340. */
  341. if (original_object) {
  342. switch (original_object->common.type) {
  343. case ACPI_TYPE_INTEGER:
  344. /* We can only repair an Integer==0 */
  345. if (original_object->integer.value) {
  346. return (AE_AML_OPERAND_TYPE);
  347. }
  348. break;
  349. case ACPI_TYPE_BUFFER:
  350. if (original_object->buffer.length) {
  351. /* Additional checks can be added in the future */
  352. *return_object = NULL;
  353. return (AE_OK);
  354. }
  355. break;
  356. case ACPI_TYPE_STRING:
  357. default:
  358. return (AE_AML_OPERAND_TYPE);
  359. }
  360. }
  361. /* Create the new buffer object for the resource descriptor */
  362. new_object = acpi_ut_create_buffer_object(2);
  363. if (!new_object) {
  364. return (AE_NO_MEMORY);
  365. }
  366. buffer = ACPI_CAST_PTR(u8, new_object->buffer.pointer);
  367. /* Initialize the Buffer with a single end_tag descriptor */
  368. buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
  369. buffer[1] = 0x00;
  370. *return_object = new_object;
  371. return (AE_OK);
  372. }
  373. /*******************************************************************************
  374. *
  375. * FUNCTION: acpi_ns_convert_to_reference
  376. *
  377. * PARAMETERS: scope - Namespace node for the method/object
  378. * original_object - Object to be converted
  379. * return_object - Where the new converted object is returned
  380. *
  381. * RETURN: Status. AE_OK if conversion was successful
  382. *
  383. * DESCRIPTION: Attempt to convert a Integer object to a object_reference.
  384. * Buffer.
  385. *
  386. ******************************************************************************/
  387. acpi_status
  388. acpi_ns_convert_to_reference(struct acpi_namespace_node * scope,
  389. union acpi_operand_object *original_object,
  390. union acpi_operand_object **return_object)
  391. {
  392. union acpi_operand_object *new_object = NULL;
  393. acpi_status status;
  394. struct acpi_namespace_node *node;
  395. union acpi_generic_state scope_info;
  396. char *name;
  397. ACPI_FUNCTION_NAME(ns_convert_to_reference);
  398. /* Convert path into internal presentation */
  399. status =
  400. acpi_ns_internalize_name(original_object->string.pointer, &name);
  401. if (ACPI_FAILURE(status)) {
  402. return_ACPI_STATUS(status);
  403. }
  404. /* Find the namespace node */
  405. scope_info.scope.node =
  406. ACPI_CAST_PTR(struct acpi_namespace_node, scope);
  407. status =
  408. acpi_ns_lookup(&scope_info, name, ACPI_TYPE_ANY, ACPI_IMODE_EXECUTE,
  409. ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
  410. NULL, &node);
  411. if (ACPI_FAILURE(status)) {
  412. /* Check if we are resolving a named reference within a package */
  413. ACPI_ERROR_NAMESPACE(original_object->string.pointer, status);
  414. goto error_exit;
  415. }
  416. /* Create and init a new internal ACPI object */
  417. new_object = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  418. if (!new_object) {
  419. status = AE_NO_MEMORY;
  420. goto error_exit;
  421. }
  422. new_object->reference.node = node;
  423. new_object->reference.object = node->object;
  424. new_object->reference.class = ACPI_REFCLASS_NAME;
  425. /*
  426. * Increase reference of the object if needed (the object is likely a
  427. * null for device nodes).
  428. */
  429. acpi_ut_add_reference(node->object);
  430. error_exit:
  431. ACPI_FREE(name);
  432. *return_object = new_object;
  433. return (AE_OK);
  434. }