exconvrt.c 18 KB

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