exconvrt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /******************************************************************************
  2. *
  3. * Module Name: exconvrt - Object conversion routines
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, 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 (
  50. acpi_integer integer,
  51. u16 base,
  52. u8 *string,
  53. u8 max_length);
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ex_convert_to_integer
  57. *
  58. * PARAMETERS: obj_desc - Object to be converted. Must be an
  59. * Integer, Buffer, or String
  60. * result_desc - Where the new Integer object is returned
  61. * Flags - Used for string conversion
  62. *
  63. * RETURN: Status
  64. *
  65. * DESCRIPTION: Convert an ACPI Object to an integer.
  66. *
  67. ******************************************************************************/
  68. acpi_status
  69. acpi_ex_convert_to_integer (
  70. union acpi_operand_object *obj_desc,
  71. union acpi_operand_object **result_desc,
  72. u32 flags)
  73. {
  74. union acpi_operand_object *return_desc;
  75. u8 *pointer;
  76. acpi_integer result;
  77. u32 i;
  78. u32 count;
  79. acpi_status status;
  80. ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_integer", obj_desc);
  81. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  82. case ACPI_TYPE_INTEGER:
  83. /* No conversion necessary */
  84. *result_desc = obj_desc;
  85. return_ACPI_STATUS (AE_OK);
  86. case ACPI_TYPE_BUFFER:
  87. case ACPI_TYPE_STRING:
  88. /* Note: Takes advantage of common buffer/string fields */
  89. pointer = obj_desc->buffer.pointer;
  90. count = obj_desc->buffer.length;
  91. break;
  92. default:
  93. return_ACPI_STATUS (AE_TYPE);
  94. }
  95. /*
  96. * Convert the buffer/string to an integer. Note that both buffers and
  97. * strings are treated as raw data - we don't convert ascii to hex for
  98. * strings.
  99. *
  100. * There are two terminating conditions for the loop:
  101. * 1) The size of an integer has been reached, or
  102. * 2) The end of the buffer or string has been reached
  103. */
  104. result = 0;
  105. /* String conversion is different than Buffer conversion */
  106. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  107. case ACPI_TYPE_STRING:
  108. /*
  109. * Convert string to an integer - for most cases, the string must be
  110. * hexadecimal as per the ACPI specification. The only exception (as
  111. * of ACPI 3.0) is that the to_integer() operator allows both decimal
  112. * and hexadecimal strings (hex prefixed with "0x").
  113. */
  114. status = acpi_ut_strtoul64 ((char *) pointer, flags, &result);
  115. if (ACPI_FAILURE (status)) {
  116. return_ACPI_STATUS (status);
  117. }
  118. break;
  119. case ACPI_TYPE_BUFFER:
  120. /* Check for zero-length buffer */
  121. if (!count) {
  122. return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
  123. }
  124. /* Transfer no more than an integer's worth of data */
  125. if (count > acpi_gbl_integer_byte_width) {
  126. count = acpi_gbl_integer_byte_width;
  127. }
  128. /*
  129. * Convert buffer to an integer - we simply grab enough raw data
  130. * from the buffer to fill an integer
  131. */
  132. for (i = 0; i < count; i++) {
  133. /*
  134. * Get next byte and shift it into the Result.
  135. * Little endian is used, meaning that the first byte of the buffer
  136. * is the LSB of the integer
  137. */
  138. result |= (((acpi_integer) pointer[i]) << (i * 8));
  139. }
  140. break;
  141. default:
  142. /* No other types can get here */
  143. break;
  144. }
  145. /* Create a new integer */
  146. return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
  147. if (!return_desc) {
  148. return_ACPI_STATUS (AE_NO_MEMORY);
  149. }
  150. /* Save the Result */
  151. return_desc->integer.value = result;
  152. acpi_ex_truncate_for32bit_table (return_desc);
  153. *result_desc = return_desc;
  154. return_ACPI_STATUS (AE_OK);
  155. }
  156. /*******************************************************************************
  157. *
  158. * FUNCTION: acpi_ex_convert_to_buffer
  159. *
  160. * PARAMETERS: obj_desc - Object to be converted. Must be an
  161. * Integer, Buffer, or String
  162. * result_desc - Where the new buffer object is returned
  163. *
  164. * RETURN: Status
  165. *
  166. * DESCRIPTION: Convert an ACPI Object to a Buffer
  167. *
  168. ******************************************************************************/
  169. acpi_status
  170. acpi_ex_convert_to_buffer (
  171. union acpi_operand_object *obj_desc,
  172. union acpi_operand_object **result_desc)
  173. {
  174. union acpi_operand_object *return_desc;
  175. u8 *new_buf;
  176. ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_buffer", obj_desc);
  177. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  178. case ACPI_TYPE_BUFFER:
  179. /* No conversion necessary */
  180. *result_desc = obj_desc;
  181. return_ACPI_STATUS (AE_OK);
  182. case ACPI_TYPE_INTEGER:
  183. /*
  184. * Create a new Buffer object.
  185. * Need enough space for one integer
  186. */
  187. return_desc = acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width);
  188. if (!return_desc) {
  189. return_ACPI_STATUS (AE_NO_MEMORY);
  190. }
  191. /* Copy the integer to the buffer, LSB first */
  192. new_buf = return_desc->buffer.pointer;
  193. ACPI_MEMCPY (new_buf,
  194. &obj_desc->integer.value,
  195. acpi_gbl_integer_byte_width);
  196. break;
  197. case ACPI_TYPE_STRING:
  198. /*
  199. * Create a new Buffer object
  200. * Size will be the string length
  201. *
  202. * NOTE: Add one to the string length to include the null terminator.
  203. * The ACPI spec is unclear on this subject, but there is existing
  204. * ASL/AML code that depends on the null being transferred to the new
  205. * buffer.
  206. */
  207. return_desc = acpi_ut_create_buffer_object (
  208. (acpi_size) obj_desc->string.length + 1);
  209. if (!return_desc) {
  210. return_ACPI_STATUS (AE_NO_MEMORY);
  211. }
  212. /* Copy the string to the buffer */
  213. new_buf = return_desc->buffer.pointer;
  214. ACPI_STRNCPY ((char *) new_buf, (char *) obj_desc->string.pointer,
  215. obj_desc->string.length);
  216. break;
  217. default:
  218. return_ACPI_STATUS (AE_TYPE);
  219. }
  220. /* Mark buffer initialized */
  221. return_desc->common.flags |= AOPOBJ_DATA_VALID;
  222. *result_desc = return_desc;
  223. return_ACPI_STATUS (AE_OK);
  224. }
  225. /*******************************************************************************
  226. *
  227. * FUNCTION: acpi_ex_convert_to_ascii
  228. *
  229. * PARAMETERS: Integer - Value to be converted
  230. * Base - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
  231. * String - Where the string is returned
  232. * data_width - Size of data item to be converted, in bytes
  233. *
  234. * RETURN: Actual string length
  235. *
  236. * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
  237. *
  238. ******************************************************************************/
  239. static u32
  240. acpi_ex_convert_to_ascii (
  241. acpi_integer integer,
  242. u16 base,
  243. u8 *string,
  244. u8 data_width)
  245. {
  246. acpi_integer digit;
  247. acpi_native_uint i;
  248. acpi_native_uint j;
  249. acpi_native_uint k = 0;
  250. acpi_native_uint hex_length;
  251. acpi_native_uint 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, &remainder);
  277. }
  278. /* Handle leading zeros */
  279. if (remainder != 0) {
  280. supress_zeros = FALSE;
  281. }
  282. if (!supress_zeros) {
  283. string[k] = (u8) (ACPI_ASCII_ZERO + remainder);
  284. k++;
  285. }
  286. }
  287. break;
  288. case 16:
  289. /* hex_length: 2 ascii hex chars per data byte */
  290. hex_length = ACPI_MUL_2 (data_width);
  291. for (i = 0, j = (hex_length-1); i < hex_length; i++, j--) {
  292. /* Get one hex digit, most significant digits first */
  293. string[k] = (u8) acpi_ut_hex_to_ascii_char (integer, ACPI_MUL_4 (j));
  294. k++;
  295. }
  296. break;
  297. default:
  298. return (0);
  299. }
  300. /*
  301. * Since leading zeros are supressed, we must check for the case where
  302. * the integer equals 0
  303. *
  304. * Finally, null terminate the string and return the length
  305. */
  306. if (!k) {
  307. string [0] = ACPI_ASCII_ZERO;
  308. k = 1;
  309. }
  310. string [k] = 0;
  311. return ((u32) k);
  312. }
  313. /*******************************************************************************
  314. *
  315. * FUNCTION: acpi_ex_convert_to_string
  316. *
  317. * PARAMETERS: obj_desc - Object to be converted. Must be an
  318. * Integer, Buffer, or String
  319. * result_desc - Where the string object is returned
  320. * Type - String flags (base and conversion type)
  321. *
  322. * RETURN: Status
  323. *
  324. * DESCRIPTION: Convert an ACPI Object to a string
  325. *
  326. ******************************************************************************/
  327. acpi_status
  328. acpi_ex_convert_to_string (
  329. union acpi_operand_object *obj_desc,
  330. union acpi_operand_object **result_desc,
  331. 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 (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  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 = acpi_ut_create_string_object ((acpi_size) string_length);
  362. if (!return_desc) {
  363. return_ACPI_STATUS (AE_NO_MEMORY);
  364. }
  365. new_buf = return_desc->buffer.pointer;
  366. /* Convert integer to string */
  367. string_length = acpi_ex_convert_to_ascii (obj_desc->integer.value, base,
  368. new_buf, 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. }
  390. else if (obj_desc->buffer.pointer[i] >= 10) {
  391. string_length += 3;
  392. }
  393. else {
  394. string_length += 2;
  395. }
  396. }
  397. break;
  398. case ACPI_IMPLICIT_CONVERT_HEX:
  399. /*
  400. * From the ACPI spec:
  401. *"The entire contents of the buffer are converted to a string of
  402. * two-character hexadecimal numbers, each separated by a space."
  403. */
  404. separator = ' ';
  405. string_length = (obj_desc->buffer.length * 3);
  406. break;
  407. case ACPI_EXPLICIT_CONVERT_HEX: /* Used by to_hex_string */
  408. /*
  409. * From ACPI: "If Data is a buffer, it is converted to a string of
  410. * hexadecimal values separated by commas."
  411. */
  412. string_length = (obj_desc->buffer.length * 3);
  413. break;
  414. default:
  415. return_ACPI_STATUS (AE_BAD_PARAMETER);
  416. }
  417. /*
  418. * Perform the conversion.
  419. * (-1 because of extra separator included in string_length from above)
  420. */
  421. string_length--;
  422. if (string_length > ACPI_MAX_STRING_CONVERSION) /* ACPI limit */ {
  423. return_ACPI_STATUS (AE_AML_STRING_LIMIT);
  424. }
  425. /* Create a new string object and string buffer */
  426. return_desc = acpi_ut_create_string_object ((acpi_size) string_length);
  427. if (!return_desc) {
  428. return_ACPI_STATUS (AE_NO_MEMORY);
  429. }
  430. new_buf = return_desc->buffer.pointer;
  431. /*
  432. * Convert buffer bytes to hex or decimal values
  433. * (separated by commas or spaces)
  434. */
  435. for (i = 0; i < obj_desc->buffer.length; i++) {
  436. new_buf += acpi_ex_convert_to_ascii (
  437. (acpi_integer) obj_desc->buffer.pointer[i], base,
  438. new_buf, 1);
  439. *new_buf++ = separator; /* each separated by a comma or space */
  440. }
  441. /*
  442. * Null terminate the string
  443. * (overwrites final comma/space from above)
  444. */
  445. new_buf--;
  446. *new_buf = 0;
  447. break;
  448. default:
  449. return_ACPI_STATUS (AE_TYPE);
  450. }
  451. *result_desc = return_desc;
  452. return_ACPI_STATUS (AE_OK);
  453. }
  454. /*******************************************************************************
  455. *
  456. * FUNCTION: acpi_ex_convert_to_target_type
  457. *
  458. * PARAMETERS: destination_type - Current type of the destination
  459. * source_desc - Source object to be converted.
  460. * result_desc - Where the converted object is returned
  461. * walk_state - Current method state
  462. *
  463. * RETURN: Status
  464. *
  465. * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
  466. *
  467. ******************************************************************************/
  468. acpi_status
  469. acpi_ex_convert_to_target_type (
  470. 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 != ACPI_GET_OBJECT_TYPE (source_desc)) {
  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 (source_desc),
  499. acpi_ut_get_type_name (destination_type)));
  500. status = AE_TYPE;
  501. }
  502. }
  503. break;
  504. case ARGI_TARGETREF:
  505. switch (destination_type) {
  506. case ACPI_TYPE_INTEGER:
  507. case ACPI_TYPE_BUFFER_FIELD:
  508. case ACPI_TYPE_LOCAL_BANK_FIELD:
  509. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  510. /*
  511. * These types require an Integer operand. We can convert
  512. * a Buffer or a String to an Integer if necessary.
  513. */
  514. status = acpi_ex_convert_to_integer (source_desc, result_desc,
  515. 16);
  516. break;
  517. case ACPI_TYPE_STRING:
  518. /*
  519. * The operand must be a String. We can convert an
  520. * Integer or Buffer if necessary
  521. */
  522. status = 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 = acpi_ex_convert_to_buffer (source_desc, result_desc);
  531. break;
  532. default:
  533. ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
  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_DEBUG_PRINT ((ACPI_DB_ERROR,
  546. "Unknown Target type ID 0x%X Op %s dest_type %s\n",
  547. GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args),
  548. walk_state->op_info->name, acpi_ut_get_type_name (destination_type)));
  549. ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
  550. GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args)))
  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. }