exconvrt.c 18 KB

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