exconvrt.c 17 KB

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