exconvrt.c 17 KB

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