exconvrt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /******************************************************************************
  2. *
  3. * Module Name: exconvrt - Object conversion routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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 "acinterp.h"
  45. #include "amlcode.h"
  46. #define _COMPONENT ACPI_EXECUTER
  47. ACPI_MODULE_NAME("exconvrt")
  48. /* Local prototypes */
  49. static u32
  50. acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 max_length);
  51. /*******************************************************************************
  52. *
  53. * FUNCTION: acpi_ex_convert_to_integer
  54. *
  55. * PARAMETERS: obj_desc - Object to be converted. Must be an
  56. * Integer, Buffer, or String
  57. * result_desc - Where the new Integer object is returned
  58. * flags - Used for string conversion
  59. *
  60. * RETURN: Status
  61. *
  62. * DESCRIPTION: Convert an ACPI Object to an integer.
  63. *
  64. ******************************************************************************/
  65. acpi_status
  66. acpi_ex_convert_to_integer(union acpi_operand_object *obj_desc,
  67. union acpi_operand_object **result_desc, u32 flags)
  68. {
  69. union acpi_operand_object *return_desc;
  70. u8 *pointer;
  71. u64 result;
  72. u32 i;
  73. u32 count;
  74. acpi_status status;
  75. ACPI_FUNCTION_TRACE_PTR(ex_convert_to_integer, obj_desc);
  76. switch (obj_desc->common.type) {
  77. case ACPI_TYPE_INTEGER:
  78. /* No conversion necessary */
  79. *result_desc = obj_desc;
  80. return_ACPI_STATUS(AE_OK);
  81. case ACPI_TYPE_BUFFER:
  82. case ACPI_TYPE_STRING:
  83. /* Note: Takes advantage of common buffer/string fields */
  84. pointer = obj_desc->buffer.pointer;
  85. count = obj_desc->buffer.length;
  86. break;
  87. default:
  88. return_ACPI_STATUS(AE_TYPE);
  89. }
  90. /*
  91. * Convert the buffer/string to an integer. Note that both buffers and
  92. * strings are treated as raw data - we don't convert ascii to hex for
  93. * strings.
  94. *
  95. * There are two terminating conditions for the loop:
  96. * 1) The size of an integer has been reached, or
  97. * 2) The end of the buffer or string has been reached
  98. */
  99. result = 0;
  100. /* String conversion is different than Buffer conversion */
  101. switch (obj_desc->common.type) {
  102. case ACPI_TYPE_STRING:
  103. /*
  104. * Convert string to an integer - for most cases, the string must be
  105. * hexadecimal as per the ACPI specification. The only exception (as
  106. * of ACPI 3.0) is that the to_integer() operator allows both decimal
  107. * and hexadecimal strings (hex prefixed with "0x").
  108. */
  109. status = acpi_ut_strtoul64((char *)pointer, flags, &result);
  110. if (ACPI_FAILURE(status)) {
  111. return_ACPI_STATUS(status);
  112. }
  113. break;
  114. case ACPI_TYPE_BUFFER:
  115. /* Check for zero-length buffer */
  116. if (!count) {
  117. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  118. }
  119. /* Transfer no more than an integer's worth of data */
  120. if (count > acpi_gbl_integer_byte_width) {
  121. count = acpi_gbl_integer_byte_width;
  122. }
  123. /*
  124. * Convert buffer to an integer - we simply grab enough raw data
  125. * from the buffer to fill an integer
  126. */
  127. for (i = 0; i < count; i++) {
  128. /*
  129. * Get next byte and shift it into the Result.
  130. * Little endian is used, meaning that the first byte of the buffer
  131. * is the LSB of the integer
  132. */
  133. result |= (((u64) pointer[i]) << (i * 8));
  134. }
  135. break;
  136. default:
  137. /* No other types can get here */
  138. break;
  139. }
  140. /* Create a new integer */
  141. return_desc = acpi_ut_create_integer_object(result);
  142. if (!return_desc) {
  143. return_ACPI_STATUS(AE_NO_MEMORY);
  144. }
  145. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
  146. ACPI_FORMAT_UINT64(result)));
  147. /* Save the Result */
  148. (void)acpi_ex_truncate_for32bit_table(return_desc);
  149. *result_desc = return_desc;
  150. return_ACPI_STATUS(AE_OK);
  151. }
  152. /*******************************************************************************
  153. *
  154. * FUNCTION: acpi_ex_convert_to_buffer
  155. *
  156. * PARAMETERS: obj_desc - Object to be converted. Must be an
  157. * Integer, Buffer, or String
  158. * result_desc - Where the new buffer object is returned
  159. *
  160. * RETURN: Status
  161. *
  162. * DESCRIPTION: Convert an ACPI Object to a Buffer
  163. *
  164. ******************************************************************************/
  165. acpi_status
  166. acpi_ex_convert_to_buffer(union acpi_operand_object *obj_desc,
  167. union acpi_operand_object **result_desc)
  168. {
  169. union acpi_operand_object *return_desc;
  170. u8 *new_buf;
  171. ACPI_FUNCTION_TRACE_PTR(ex_convert_to_buffer, obj_desc);
  172. switch (obj_desc->common.type) {
  173. case ACPI_TYPE_BUFFER:
  174. /* No conversion necessary */
  175. *result_desc = obj_desc;
  176. return_ACPI_STATUS(AE_OK);
  177. case ACPI_TYPE_INTEGER:
  178. /*
  179. * Create a new Buffer object.
  180. * Need enough space for one integer
  181. */
  182. return_desc =
  183. acpi_ut_create_buffer_object(acpi_gbl_integer_byte_width);
  184. if (!return_desc) {
  185. return_ACPI_STATUS(AE_NO_MEMORY);
  186. }
  187. /* Copy the integer to the buffer, LSB first */
  188. new_buf = return_desc->buffer.pointer;
  189. memcpy(new_buf,
  190. &obj_desc->integer.value, acpi_gbl_integer_byte_width);
  191. break;
  192. case ACPI_TYPE_STRING:
  193. /*
  194. * Create a new Buffer object
  195. * Size will be the string length
  196. *
  197. * NOTE: Add one to the string length to include the null terminator.
  198. * The ACPI spec is unclear on this subject, but there is existing
  199. * ASL/AML code that depends on the null being transferred to the new
  200. * buffer.
  201. */
  202. return_desc = acpi_ut_create_buffer_object((acpi_size)
  203. obj_desc->string.
  204. length + 1);
  205. if (!return_desc) {
  206. return_ACPI_STATUS(AE_NO_MEMORY);
  207. }
  208. /* Copy the string to the buffer */
  209. new_buf = return_desc->buffer.pointer;
  210. strncpy((char *)new_buf, (char *)obj_desc->string.pointer,
  211. obj_desc->string.length);
  212. break;
  213. default:
  214. return_ACPI_STATUS(AE_TYPE);
  215. }
  216. /* Mark buffer initialized */
  217. return_desc->common.flags |= AOPOBJ_DATA_VALID;
  218. *result_desc = return_desc;
  219. return_ACPI_STATUS(AE_OK);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_ex_convert_to_ascii
  224. *
  225. * PARAMETERS: integer - Value to be converted
  226. * base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
  227. * string - Where the string is returned
  228. * data_width - Size of data item to be converted, in bytes
  229. *
  230. * RETURN: Actual string length
  231. *
  232. * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
  233. *
  234. ******************************************************************************/
  235. static u32
  236. acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 data_width)
  237. {
  238. u64 digit;
  239. u32 i;
  240. u32 j;
  241. u32 k = 0;
  242. u32 hex_length;
  243. u32 decimal_length;
  244. u32 remainder;
  245. u8 supress_zeros;
  246. ACPI_FUNCTION_ENTRY();
  247. switch (base) {
  248. case 10:
  249. /* Setup max length for the decimal number */
  250. switch (data_width) {
  251. case 1:
  252. decimal_length = ACPI_MAX8_DECIMAL_DIGITS;
  253. break;
  254. case 4:
  255. decimal_length = ACPI_MAX32_DECIMAL_DIGITS;
  256. break;
  257. case 8:
  258. default:
  259. decimal_length = ACPI_MAX64_DECIMAL_DIGITS;
  260. break;
  261. }
  262. supress_zeros = TRUE; /* No leading zeros */
  263. remainder = 0;
  264. for (i = decimal_length; i > 0; i--) {
  265. /* Divide by nth factor of 10 */
  266. digit = integer;
  267. for (j = 0; j < i; j++) {
  268. (void)acpi_ut_short_divide(digit, 10, &digit,
  269. &remainder);
  270. }
  271. /* Handle leading zeros */
  272. if (remainder != 0) {
  273. supress_zeros = FALSE;
  274. }
  275. if (!supress_zeros) {
  276. string[k] = (u8) (ACPI_ASCII_ZERO + remainder);
  277. k++;
  278. }
  279. }
  280. break;
  281. case 16:
  282. /* hex_length: 2 ascii hex chars per data byte */
  283. hex_length = ACPI_MUL_2(data_width);
  284. for (i = 0, j = (hex_length - 1); i < hex_length; i++, j--) {
  285. /* Get one hex digit, most significant digits first */
  286. string[k] =
  287. (u8) acpi_ut_hex_to_ascii_char(integer,
  288. ACPI_MUL_4(j));
  289. k++;
  290. }
  291. break;
  292. default:
  293. return (0);
  294. }
  295. /*
  296. * Since leading zeros are suppressed, we must check for the case where
  297. * the integer equals 0
  298. *
  299. * Finally, null terminate the string and return the length
  300. */
  301. if (!k) {
  302. string[0] = ACPI_ASCII_ZERO;
  303. k = 1;
  304. }
  305. string[k] = 0;
  306. return ((u32) k);
  307. }
  308. /*******************************************************************************
  309. *
  310. * FUNCTION: acpi_ex_convert_to_string
  311. *
  312. * PARAMETERS: obj_desc - Object to be converted. Must be an
  313. * Integer, Buffer, or String
  314. * result_desc - Where the string object is returned
  315. * type - String flags (base and conversion type)
  316. *
  317. * RETURN: Status
  318. *
  319. * DESCRIPTION: Convert an ACPI Object to a string
  320. *
  321. ******************************************************************************/
  322. acpi_status
  323. acpi_ex_convert_to_string(union acpi_operand_object * obj_desc,
  324. union acpi_operand_object ** result_desc, u32 type)
  325. {
  326. union acpi_operand_object *return_desc;
  327. u8 *new_buf;
  328. u32 i;
  329. u32 string_length = 0;
  330. u16 base = 16;
  331. u8 separator = ',';
  332. ACPI_FUNCTION_TRACE_PTR(ex_convert_to_string, obj_desc);
  333. switch (obj_desc->common.type) {
  334. case ACPI_TYPE_STRING:
  335. /* No conversion necessary */
  336. *result_desc = obj_desc;
  337. return_ACPI_STATUS(AE_OK);
  338. case ACPI_TYPE_INTEGER:
  339. switch (type) {
  340. case ACPI_EXPLICIT_CONVERT_DECIMAL:
  341. /* Make room for maximum decimal number */
  342. string_length = ACPI_MAX_DECIMAL_DIGITS;
  343. base = 10;
  344. break;
  345. default:
  346. /* Two hex string characters for each integer byte */
  347. string_length = ACPI_MUL_2(acpi_gbl_integer_byte_width);
  348. break;
  349. }
  350. /*
  351. * Create a new String
  352. * Need enough space for one ASCII integer (plus null terminator)
  353. */
  354. return_desc =
  355. acpi_ut_create_string_object((acpi_size) string_length);
  356. if (!return_desc) {
  357. return_ACPI_STATUS(AE_NO_MEMORY);
  358. }
  359. new_buf = return_desc->buffer.pointer;
  360. /* Convert integer to string */
  361. string_length =
  362. acpi_ex_convert_to_ascii(obj_desc->integer.value, base,
  363. new_buf,
  364. acpi_gbl_integer_byte_width);
  365. /* Null terminate at the correct place */
  366. return_desc->string.length = string_length;
  367. new_buf[string_length] = 0;
  368. break;
  369. case ACPI_TYPE_BUFFER:
  370. /* Setup string length, base, and separator */
  371. switch (type) {
  372. case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by to_decimal_string */
  373. /*
  374. * From ACPI: "If Data is a buffer, it is converted to a string of
  375. * decimal values separated by commas."
  376. */
  377. base = 10;
  378. /*
  379. * Calculate the final string length. Individual string values
  380. * are variable length (include separator for each)
  381. */
  382. for (i = 0; i < obj_desc->buffer.length; i++) {
  383. if (obj_desc->buffer.pointer[i] >= 100) {
  384. string_length += 4;
  385. } else if (obj_desc->buffer.pointer[i] >= 10) {
  386. string_length += 3;
  387. } else {
  388. string_length += 2;
  389. }
  390. }
  391. break;
  392. case ACPI_IMPLICIT_CONVERT_HEX:
  393. /*
  394. * From the ACPI spec:
  395. *"The entire contents of the buffer are converted to a string of
  396. * two-character hexadecimal numbers, each separated by a space."
  397. */
  398. separator = ' ';
  399. string_length = (obj_desc->buffer.length * 3);
  400. break;
  401. case ACPI_EXPLICIT_CONVERT_HEX: /* Used by to_hex_string */
  402. /*
  403. * From ACPI: "If Data is a buffer, it is converted to a string of
  404. * hexadecimal values separated by commas."
  405. */
  406. string_length = (obj_desc->buffer.length * 3);
  407. break;
  408. default:
  409. return_ACPI_STATUS(AE_BAD_PARAMETER);
  410. }
  411. /*
  412. * Create a new string object and string buffer
  413. * (-1 because of extra separator included in string_length from above)
  414. * Allow creation of zero-length strings from zero-length buffers.
  415. */
  416. if (string_length) {
  417. string_length--;
  418. }
  419. return_desc =
  420. acpi_ut_create_string_object((acpi_size) string_length);
  421. if (!return_desc) {
  422. return_ACPI_STATUS(AE_NO_MEMORY);
  423. }
  424. new_buf = return_desc->buffer.pointer;
  425. /*
  426. * Convert buffer bytes to hex or decimal values
  427. * (separated by commas or spaces)
  428. */
  429. for (i = 0; i < obj_desc->buffer.length; i++) {
  430. new_buf += acpi_ex_convert_to_ascii((u64) obj_desc->
  431. buffer.pointer[i],
  432. base, new_buf, 1);
  433. *new_buf++ = separator; /* each separated by a comma or space */
  434. }
  435. /*
  436. * Null terminate the string
  437. * (overwrites final comma/space from above)
  438. */
  439. if (obj_desc->buffer.length) {
  440. new_buf--;
  441. }
  442. *new_buf = 0;
  443. break;
  444. default:
  445. return_ACPI_STATUS(AE_TYPE);
  446. }
  447. *result_desc = return_desc;
  448. return_ACPI_STATUS(AE_OK);
  449. }
  450. /*******************************************************************************
  451. *
  452. * FUNCTION: acpi_ex_convert_to_target_type
  453. *
  454. * PARAMETERS: destination_type - Current type of the destination
  455. * source_desc - Source object to be converted.
  456. * result_desc - Where the converted object is returned
  457. * walk_state - Current method state
  458. *
  459. * RETURN: Status
  460. *
  461. * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
  462. *
  463. ******************************************************************************/
  464. acpi_status
  465. acpi_ex_convert_to_target_type(acpi_object_type destination_type,
  466. union acpi_operand_object *source_desc,
  467. union acpi_operand_object **result_desc,
  468. struct acpi_walk_state *walk_state)
  469. {
  470. acpi_status status = AE_OK;
  471. ACPI_FUNCTION_TRACE(ex_convert_to_target_type);
  472. /* Default behavior */
  473. *result_desc = source_desc;
  474. /*
  475. * If required by the target,
  476. * perform implicit conversion on the source before we store it.
  477. */
  478. switch (GET_CURRENT_ARG_TYPE(walk_state->op_info->runtime_args)) {
  479. case ARGI_SIMPLE_TARGET:
  480. case ARGI_FIXED_TARGET:
  481. case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
  482. switch (destination_type) {
  483. case ACPI_TYPE_LOCAL_REGION_FIELD:
  484. /*
  485. * Named field can always handle conversions
  486. */
  487. break;
  488. default:
  489. /* No conversion allowed for these types */
  490. if (destination_type != source_desc->common.type) {
  491. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  492. "Explicit operator, will store (%s) over existing type (%s)\n",
  493. acpi_ut_get_object_type_name
  494. (source_desc),
  495. acpi_ut_get_type_name
  496. (destination_type)));
  497. status = AE_TYPE;
  498. }
  499. }
  500. break;
  501. case ARGI_TARGETREF:
  502. case ARGI_STORE_TARGET:
  503. switch (destination_type) {
  504. case ACPI_TYPE_INTEGER:
  505. case ACPI_TYPE_BUFFER_FIELD:
  506. case ACPI_TYPE_LOCAL_BANK_FIELD:
  507. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  508. /*
  509. * These types require an Integer operand. We can convert
  510. * a Buffer or a String to an Integer if necessary.
  511. */
  512. status =
  513. acpi_ex_convert_to_integer(source_desc, result_desc,
  514. 16);
  515. break;
  516. case ACPI_TYPE_STRING:
  517. /*
  518. * The operand must be a String. We can convert an
  519. * Integer or Buffer if necessary
  520. */
  521. status =
  522. acpi_ex_convert_to_string(source_desc, result_desc,
  523. ACPI_IMPLICIT_CONVERT_HEX);
  524. break;
  525. case ACPI_TYPE_BUFFER:
  526. /*
  527. * The operand must be a Buffer. We can convert an
  528. * Integer or String if necessary
  529. */
  530. status =
  531. acpi_ex_convert_to_buffer(source_desc, result_desc);
  532. break;
  533. default:
  534. ACPI_ERROR((AE_INFO,
  535. "Bad destination type during conversion: 0x%X",
  536. destination_type));
  537. status = AE_AML_INTERNAL;
  538. break;
  539. }
  540. break;
  541. case ARGI_REFERENCE:
  542. /*
  543. * create_xxxx_field cases - we are storing the field object into the name
  544. */
  545. break;
  546. default:
  547. ACPI_ERROR((AE_INFO,
  548. "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
  549. GET_CURRENT_ARG_TYPE(walk_state->op_info->
  550. runtime_args),
  551. walk_state->opcode,
  552. acpi_ut_get_type_name(destination_type)));
  553. status = AE_AML_INTERNAL;
  554. }
  555. /*
  556. * Source-to-Target conversion semantics:
  557. *
  558. * If conversion to the target type cannot be performed, then simply
  559. * overwrite the target with the new object and type.
  560. */
  561. if (status == AE_TYPE) {
  562. status = AE_OK;
  563. }
  564. return_ACPI_STATUS(status);
  565. }