exconvrt.c 18 KB

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