exconvrt.c 18 KB

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