exconvrt.c 16 KB

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