exconvrt.c 17 KB

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