exconvrt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /******************************************************************************
  2. *
  3. * Module Name: exconvrt - Object conversion routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2006, R. Byron Moore
  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. /* Save the Result */
  146. return_desc->integer.value = result;
  147. acpi_ex_truncate_for32bit_table(return_desc);
  148. *result_desc = return_desc;
  149. return_ACPI_STATUS(AE_OK);
  150. }
  151. /*******************************************************************************
  152. *
  153. * FUNCTION: acpi_ex_convert_to_buffer
  154. *
  155. * PARAMETERS: obj_desc - Object to be converted. Must be an
  156. * Integer, Buffer, or String
  157. * result_desc - Where the new buffer object is returned
  158. *
  159. * RETURN: Status
  160. *
  161. * DESCRIPTION: Convert an ACPI Object to a Buffer
  162. *
  163. ******************************************************************************/
  164. acpi_status
  165. acpi_ex_convert_to_buffer(union acpi_operand_object *obj_desc,
  166. union acpi_operand_object **result_desc)
  167. {
  168. union acpi_operand_object *return_desc;
  169. u8 *new_buf;
  170. ACPI_FUNCTION_TRACE_PTR("ex_convert_to_buffer", obj_desc);
  171. switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
  172. case ACPI_TYPE_BUFFER:
  173. /* No conversion necessary */
  174. *result_desc = obj_desc;
  175. return_ACPI_STATUS(AE_OK);
  176. case ACPI_TYPE_INTEGER:
  177. /*
  178. * Create a new Buffer object.
  179. * Need enough space for one integer
  180. */
  181. return_desc =
  182. acpi_ut_create_buffer_object(acpi_gbl_integer_byte_width);
  183. if (!return_desc) {
  184. return_ACPI_STATUS(AE_NO_MEMORY);
  185. }
  186. /* Copy the integer to the buffer, LSB first */
  187. new_buf = return_desc->buffer.pointer;
  188. ACPI_MEMCPY(new_buf,
  189. &obj_desc->integer.value,
  190. acpi_gbl_integer_byte_width);
  191. break;
  192. case ACPI_TYPE_STRING:
  193. /*
  194. * Create a new Buffer object
  195. * Size will be the string length
  196. *
  197. * NOTE: Add one to the string length to include the null terminator.
  198. * The ACPI spec is unclear on this subject, but there is existing
  199. * ASL/AML code that depends on the null being transferred to the new
  200. * buffer.
  201. */
  202. return_desc = acpi_ut_create_buffer_object((acpi_size)
  203. obj_desc->string.
  204. length + 1);
  205. if (!return_desc) {
  206. return_ACPI_STATUS(AE_NO_MEMORY);
  207. }
  208. /* Copy the string to the buffer */
  209. new_buf = return_desc->buffer.pointer;
  210. ACPI_STRNCPY((char *)new_buf, (char *)obj_desc->string.pointer,
  211. obj_desc->string.length);
  212. break;
  213. default:
  214. return_ACPI_STATUS(AE_TYPE);
  215. }
  216. /* Mark buffer initialized */
  217. return_desc->common.flags |= AOPOBJ_DATA_VALID;
  218. *result_desc = return_desc;
  219. return_ACPI_STATUS(AE_OK);
  220. }
  221. /*******************************************************************************
  222. *
  223. * FUNCTION: acpi_ex_convert_to_ascii
  224. *
  225. * PARAMETERS: Integer - Value to be converted
  226. * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
  227. * String - Where the string is returned
  228. * data_width - Size of data item to be converted, in bytes
  229. *
  230. * RETURN: Actual string length
  231. *
  232. * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
  233. *
  234. ******************************************************************************/
  235. static u32
  236. acpi_ex_convert_to_ascii(acpi_integer integer,
  237. u16 base, u8 * string, u8 data_width)
  238. {
  239. acpi_integer digit;
  240. acpi_native_uint i;
  241. acpi_native_uint j;
  242. acpi_native_uint k = 0;
  243. acpi_native_uint hex_length;
  244. acpi_native_uint 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_native_uint) 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 supressed, 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 (ACPI_GET_OBJECT_TYPE(obj_desc)) {
  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. */
  416. return_desc =
  417. acpi_ut_create_string_object((acpi_size)
  418. (string_length - 1));
  419. if (!return_desc) {
  420. return_ACPI_STATUS(AE_NO_MEMORY);
  421. }
  422. new_buf = return_desc->buffer.pointer;
  423. /*
  424. * Convert buffer bytes to hex or decimal values
  425. * (separated by commas or spaces)
  426. */
  427. for (i = 0; i < obj_desc->buffer.length; i++) {
  428. new_buf += acpi_ex_convert_to_ascii((acpi_integer)
  429. obj_desc->buffer.
  430. pointer[i], base,
  431. new_buf, 1);
  432. *new_buf++ = separator; /* each separated by a comma or space */
  433. }
  434. /*
  435. * Null terminate the string
  436. * (overwrites final comma/space from above)
  437. */
  438. new_buf--;
  439. *new_buf = 0;
  440. break;
  441. default:
  442. return_ACPI_STATUS(AE_TYPE);
  443. }
  444. *result_desc = return_desc;
  445. return_ACPI_STATUS(AE_OK);
  446. }
  447. /*******************************************************************************
  448. *
  449. * FUNCTION: acpi_ex_convert_to_target_type
  450. *
  451. * PARAMETERS: destination_type - Current type of the destination
  452. * source_desc - Source object to be converted.
  453. * result_desc - Where the converted object is returned
  454. * walk_state - Current method state
  455. *
  456. * RETURN: Status
  457. *
  458. * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
  459. *
  460. ******************************************************************************/
  461. acpi_status
  462. acpi_ex_convert_to_target_type(acpi_object_type destination_type,
  463. union acpi_operand_object *source_desc,
  464. union acpi_operand_object **result_desc,
  465. struct acpi_walk_state *walk_state)
  466. {
  467. acpi_status status = AE_OK;
  468. ACPI_FUNCTION_TRACE("ex_convert_to_target_type");
  469. /* Default behavior */
  470. *result_desc = source_desc;
  471. /*
  472. * If required by the target,
  473. * perform implicit conversion on the source before we store it.
  474. */
  475. switch (GET_CURRENT_ARG_TYPE(walk_state->op_info->runtime_args)) {
  476. case ARGI_SIMPLE_TARGET:
  477. case ARGI_FIXED_TARGET:
  478. case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
  479. switch (destination_type) {
  480. case ACPI_TYPE_LOCAL_REGION_FIELD:
  481. /*
  482. * Named field can always handle conversions
  483. */
  484. break;
  485. default:
  486. /* No conversion allowed for these types */
  487. if (destination_type !=
  488. ACPI_GET_OBJECT_TYPE(source_desc)) {
  489. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  490. "Explicit operator, will store (%s) over existing type (%s)\n",
  491. acpi_ut_get_object_type_name
  492. (source_desc),
  493. acpi_ut_get_type_name
  494. (destination_type)));
  495. status = AE_TYPE;
  496. }
  497. }
  498. break;
  499. case ARGI_TARGETREF:
  500. switch (destination_type) {
  501. case ACPI_TYPE_INTEGER:
  502. case ACPI_TYPE_BUFFER_FIELD:
  503. case ACPI_TYPE_LOCAL_BANK_FIELD:
  504. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  505. /*
  506. * These types require an Integer operand. We can convert
  507. * a Buffer or a String to an Integer if necessary.
  508. */
  509. status =
  510. acpi_ex_convert_to_integer(source_desc, result_desc,
  511. 16);
  512. break;
  513. case ACPI_TYPE_STRING:
  514. /*
  515. * The operand must be a String. We can convert an
  516. * Integer or Buffer if necessary
  517. */
  518. status =
  519. acpi_ex_convert_to_string(source_desc, result_desc,
  520. ACPI_IMPLICIT_CONVERT_HEX);
  521. break;
  522. case ACPI_TYPE_BUFFER:
  523. /*
  524. * The operand must be a Buffer. We can convert an
  525. * Integer or String if necessary
  526. */
  527. status =
  528. acpi_ex_convert_to_buffer(source_desc, result_desc);
  529. break;
  530. default:
  531. ACPI_ERROR((AE_INFO,
  532. "Bad destination type during conversion: %X",
  533. destination_type));
  534. status = AE_AML_INTERNAL;
  535. break;
  536. }
  537. break;
  538. case ARGI_REFERENCE:
  539. /*
  540. * create_xxxx_field cases - we are storing the field object into the name
  541. */
  542. break;
  543. default:
  544. ACPI_ERROR((AE_INFO,
  545. "Unknown Target type ID 0x%X aml_opcode %X dest_type %s",
  546. GET_CURRENT_ARG_TYPE(walk_state->op_info->
  547. runtime_args),
  548. walk_state->opcode,
  549. acpi_ut_get_type_name(destination_type)));
  550. status = AE_AML_INTERNAL;
  551. }
  552. /*
  553. * Source-to-Target conversion semantics:
  554. *
  555. * If conversion to the target type cannot be performed, then simply
  556. * overwrite the target with the new object and type.
  557. */
  558. if (status == AE_TYPE) {
  559. status = AE_OK;
  560. }
  561. return_ACPI_STATUS(status);
  562. }