exconvrt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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((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, &obj_desc->integer.value,
  190. 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] = (u8)
  287. acpi_ut_hex_to_ascii_char(integer, ACPI_MUL_4(j));
  288. k++;
  289. }
  290. break;
  291. default:
  292. return (0);
  293. }
  294. /*
  295. * Since leading zeros are suppressed, we must check for the case where
  296. * the integer equals 0
  297. *
  298. * Finally, null terminate the string and return the length
  299. */
  300. if (!k) {
  301. string[0] = ACPI_ASCII_ZERO;
  302. k = 1;
  303. }
  304. string[k] = 0;
  305. return ((u32) k);
  306. }
  307. /*******************************************************************************
  308. *
  309. * FUNCTION: acpi_ex_convert_to_string
  310. *
  311. * PARAMETERS: obj_desc - Object to be converted. Must be an
  312. * Integer, Buffer, or String
  313. * result_desc - Where the string object is returned
  314. * type - String flags (base and conversion type)
  315. *
  316. * RETURN: Status
  317. *
  318. * DESCRIPTION: Convert an ACPI Object to a string
  319. *
  320. ******************************************************************************/
  321. acpi_status
  322. acpi_ex_convert_to_string(union acpi_operand_object * obj_desc,
  323. union acpi_operand_object ** result_desc, u32 type)
  324. {
  325. union acpi_operand_object *return_desc;
  326. u8 *new_buf;
  327. u32 i;
  328. u32 string_length = 0;
  329. u16 base = 16;
  330. u8 separator = ',';
  331. ACPI_FUNCTION_TRACE_PTR(ex_convert_to_string, obj_desc);
  332. switch (obj_desc->common.type) {
  333. case ACPI_TYPE_STRING:
  334. /* No conversion necessary */
  335. *result_desc = obj_desc;
  336. return_ACPI_STATUS(AE_OK);
  337. case ACPI_TYPE_INTEGER:
  338. switch (type) {
  339. case ACPI_EXPLICIT_CONVERT_DECIMAL:
  340. /* Make room for maximum decimal number */
  341. string_length = ACPI_MAX_DECIMAL_DIGITS;
  342. base = 10;
  343. break;
  344. default:
  345. /* Two hex string characters for each integer byte */
  346. string_length = ACPI_MUL_2(acpi_gbl_integer_byte_width);
  347. break;
  348. }
  349. /*
  350. * Create a new String
  351. * Need enough space for one ASCII integer (plus null terminator)
  352. */
  353. return_desc =
  354. acpi_ut_create_string_object((acpi_size) string_length);
  355. if (!return_desc) {
  356. return_ACPI_STATUS(AE_NO_MEMORY);
  357. }
  358. new_buf = return_desc->buffer.pointer;
  359. /* Convert integer to string */
  360. string_length =
  361. acpi_ex_convert_to_ascii(obj_desc->integer.value, base,
  362. new_buf,
  363. acpi_gbl_integer_byte_width);
  364. /* Null terminate at the correct place */
  365. return_desc->string.length = string_length;
  366. new_buf[string_length] = 0;
  367. break;
  368. case ACPI_TYPE_BUFFER:
  369. /* Setup string length, base, and separator */
  370. switch (type) {
  371. case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by to_decimal_string */
  372. /*
  373. * From ACPI: "If Data is a buffer, it is converted to a string of
  374. * decimal values separated by commas."
  375. */
  376. base = 10;
  377. /*
  378. * Calculate the final string length. Individual string values
  379. * are variable length (include separator for each)
  380. */
  381. for (i = 0; i < obj_desc->buffer.length; i++) {
  382. if (obj_desc->buffer.pointer[i] >= 100) {
  383. string_length += 4;
  384. } else if (obj_desc->buffer.pointer[i] >= 10) {
  385. string_length += 3;
  386. } else {
  387. string_length += 2;
  388. }
  389. }
  390. break;
  391. case ACPI_IMPLICIT_CONVERT_HEX:
  392. /*
  393. * From the ACPI spec:
  394. *"The entire contents of the buffer are converted to a string of
  395. * two-character hexadecimal numbers, each separated by a space."
  396. */
  397. separator = ' ';
  398. string_length = (obj_desc->buffer.length * 3);
  399. break;
  400. case ACPI_EXPLICIT_CONVERT_HEX: /* Used by to_hex_string */
  401. /*
  402. * From ACPI: "If Data is a buffer, it is converted to a string of
  403. * hexadecimal values separated by commas."
  404. */
  405. string_length = (obj_desc->buffer.length * 3);
  406. break;
  407. default:
  408. return_ACPI_STATUS(AE_BAD_PARAMETER);
  409. }
  410. /*
  411. * Create a new string object and string buffer
  412. * (-1 because of extra separator included in string_length from above)
  413. * Allow creation of zero-length strings from zero-length buffers.
  414. */
  415. if (string_length) {
  416. string_length--;
  417. }
  418. return_desc =
  419. acpi_ut_create_string_object((acpi_size) string_length);
  420. if (!return_desc) {
  421. return_ACPI_STATUS(AE_NO_MEMORY);
  422. }
  423. new_buf = return_desc->buffer.pointer;
  424. /*
  425. * Convert buffer bytes to hex or decimal values
  426. * (separated by commas or spaces)
  427. */
  428. for (i = 0; i < obj_desc->buffer.length; i++) {
  429. new_buf += acpi_ex_convert_to_ascii((u64) obj_desc->
  430. buffer.pointer[i],
  431. base, new_buf, 1);
  432. *new_buf++ = separator; /* each separated by a comma or space */
  433. }
  434. /*
  435. * Null terminate the string
  436. * (overwrites final comma/space from above)
  437. */
  438. if (obj_desc->buffer.length) {
  439. new_buf--;
  440. }
  441. *new_buf = 0;
  442. break;
  443. default:
  444. return_ACPI_STATUS(AE_TYPE);
  445. }
  446. *result_desc = return_desc;
  447. return_ACPI_STATUS(AE_OK);
  448. }
  449. /*******************************************************************************
  450. *
  451. * FUNCTION: acpi_ex_convert_to_target_type
  452. *
  453. * PARAMETERS: destination_type - Current type of the destination
  454. * source_desc - Source object to be converted.
  455. * result_desc - Where the converted object is returned
  456. * walk_state - Current method state
  457. *
  458. * RETURN: Status
  459. *
  460. * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
  461. *
  462. ******************************************************************************/
  463. acpi_status
  464. acpi_ex_convert_to_target_type(acpi_object_type destination_type,
  465. union acpi_operand_object *source_desc,
  466. union acpi_operand_object **result_desc,
  467. struct acpi_walk_state *walk_state)
  468. {
  469. acpi_status status = AE_OK;
  470. ACPI_FUNCTION_TRACE(ex_convert_to_target_type);
  471. /* Default behavior */
  472. *result_desc = source_desc;
  473. /*
  474. * If required by the target,
  475. * perform implicit conversion on the source before we store it.
  476. */
  477. switch (GET_CURRENT_ARG_TYPE(walk_state->op_info->runtime_args)) {
  478. case ARGI_SIMPLE_TARGET:
  479. case ARGI_FIXED_TARGET:
  480. case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
  481. switch (destination_type) {
  482. case ACPI_TYPE_LOCAL_REGION_FIELD:
  483. /*
  484. * Named field can always handle conversions
  485. */
  486. break;
  487. default:
  488. /* No conversion allowed for these types */
  489. if (destination_type != source_desc->common.type) {
  490. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  491. "Explicit operator, will store (%s) over existing type (%s)\n",
  492. acpi_ut_get_object_type_name
  493. (source_desc),
  494. acpi_ut_get_type_name
  495. (destination_type)));
  496. status = AE_TYPE;
  497. }
  498. }
  499. break;
  500. case ARGI_TARGETREF:
  501. case ARGI_STORE_TARGET:
  502. switch (destination_type) {
  503. case ACPI_TYPE_INTEGER:
  504. case ACPI_TYPE_BUFFER_FIELD:
  505. case ACPI_TYPE_LOCAL_BANK_FIELD:
  506. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  507. /*
  508. * These types require an Integer operand. We can convert
  509. * a Buffer or a String to an Integer if necessary.
  510. */
  511. status =
  512. acpi_ex_convert_to_integer(source_desc, result_desc,
  513. 16);
  514. break;
  515. case ACPI_TYPE_STRING:
  516. /*
  517. * The operand must be a String. We can convert an
  518. * Integer or Buffer if necessary
  519. */
  520. status =
  521. acpi_ex_convert_to_string(source_desc, result_desc,
  522. ACPI_IMPLICIT_CONVERT_HEX);
  523. break;
  524. case ACPI_TYPE_BUFFER:
  525. /*
  526. * The operand must be a Buffer. We can convert an
  527. * Integer or String if necessary
  528. */
  529. status =
  530. acpi_ex_convert_to_buffer(source_desc, result_desc);
  531. break;
  532. default:
  533. ACPI_ERROR((AE_INFO,
  534. "Bad destination type during conversion: 0x%X",
  535. destination_type));
  536. status = AE_AML_INTERNAL;
  537. break;
  538. }
  539. break;
  540. case ARGI_REFERENCE:
  541. /*
  542. * create_xxxx_field cases - we are storing the field object into the name
  543. */
  544. break;
  545. default:
  546. ACPI_ERROR((AE_INFO,
  547. "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
  548. GET_CURRENT_ARG_TYPE(walk_state->op_info->
  549. runtime_args),
  550. walk_state->opcode,
  551. acpi_ut_get_type_name(destination_type)));
  552. status = AE_AML_INTERNAL;
  553. }
  554. /*
  555. * Source-to-Target conversion semantics:
  556. *
  557. * If conversion to the target type cannot be performed, then simply
  558. * overwrite the target with the new object and type.
  559. */
  560. if (status == AE_TYPE) {
  561. status = AE_OK;
  562. }
  563. return_ACPI_STATUS(status);
  564. }