exconvrt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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. /*******************************************************************************
  48. *
  49. * FUNCTION: acpi_ex_convert_to_integer
  50. *
  51. * PARAMETERS: obj_desc - Object to be converted. Must be an
  52. * Integer, Buffer, or String
  53. * result_desc - Where the new Integer object is returned
  54. * Flags - Used for string conversion
  55. *
  56. * RETURN: Status
  57. *
  58. * DESCRIPTION: Convert an ACPI Object to an integer.
  59. *
  60. ******************************************************************************/
  61. acpi_status
  62. acpi_ex_convert_to_integer (
  63. union acpi_operand_object *obj_desc,
  64. union acpi_operand_object **result_desc,
  65. u32 flags)
  66. {
  67. union acpi_operand_object *return_desc;
  68. u8 *pointer;
  69. acpi_integer result;
  70. u32 i;
  71. u32 count;
  72. acpi_status status;
  73. ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_integer", obj_desc);
  74. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  75. case ACPI_TYPE_INTEGER:
  76. /* No conversion necessary */
  77. *result_desc = obj_desc;
  78. return_ACPI_STATUS (AE_OK);
  79. case ACPI_TYPE_BUFFER:
  80. case ACPI_TYPE_STRING:
  81. /* Note: Takes advantage of common buffer/string fields */
  82. pointer = obj_desc->buffer.pointer;
  83. count = obj_desc->buffer.length;
  84. break;
  85. default:
  86. return_ACPI_STATUS (AE_TYPE);
  87. }
  88. /*
  89. * Convert the buffer/string to an integer. Note that both buffers and
  90. * strings are treated as raw data - we don't convert ascii to hex for
  91. * strings.
  92. *
  93. * There are two terminating conditions for the loop:
  94. * 1) The size of an integer has been reached, or
  95. * 2) The end of the buffer or string has been reached
  96. */
  97. result = 0;
  98. /*
  99. * String conversion is different than Buffer conversion
  100. */
  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. /*
  141. * Create a new integer
  142. */
  143. return_desc = acpi_ut_create_internal_object (ACPI_TYPE_INTEGER);
  144. if (!return_desc) {
  145. return_ACPI_STATUS (AE_NO_MEMORY);
  146. }
  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 (
  168. 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 (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  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 = 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) obj_desc->string.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. u32
  236. acpi_ex_convert_to_ascii (
  237. acpi_integer integer,
  238. u16 base,
  239. u8 *string,
  240. u8 data_width)
  241. {
  242. acpi_integer digit;
  243. acpi_native_uint i;
  244. acpi_native_uint j;
  245. acpi_native_uint k = 0;
  246. acpi_native_uint hex_length;
  247. acpi_native_uint 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, &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 = ACPI_MUL_2 (data_width); /* 2 ascii hex chars per data byte */
  286. for (i = 0, j = (hex_length-1); i < hex_length; i++, j--) {
  287. /* Get one hex digit, most significant digits first */
  288. string[k] = (u8) acpi_ut_hex_to_ascii_char (integer, ACPI_MUL_4 (j));
  289. k++;
  290. }
  291. break;
  292. default:
  293. return (0);
  294. }
  295. /*
  296. * Since leading zeros are supressed, we must check for the case where
  297. * the integer equals 0
  298. *
  299. * Finally, null terminate the string and return the length
  300. */
  301. if (!k) {
  302. string [0] = ACPI_ASCII_ZERO;
  303. k = 1;
  304. }
  305. string [k] = 0;
  306. return ((u32) k);
  307. }
  308. /*******************************************************************************
  309. *
  310. * FUNCTION: acpi_ex_convert_to_string
  311. *
  312. * PARAMETERS: obj_desc - Object to be converted. Must be an
  313. * Integer, Buffer, or String
  314. * result_desc - Where the string object is returned
  315. * Type - String flags (base and conversion type)
  316. *
  317. * RETURN: Status
  318. *
  319. * DESCRIPTION: Convert an ACPI Object to a string
  320. *
  321. ******************************************************************************/
  322. acpi_status
  323. acpi_ex_convert_to_string (
  324. union acpi_operand_object *obj_desc,
  325. union acpi_operand_object **result_desc,
  326. u32 type)
  327. {
  328. union acpi_operand_object *return_desc;
  329. u8 *new_buf;
  330. u32 i;
  331. u32 string_length = 0;
  332. u16 base = 16;
  333. u8 separator = ',';
  334. ACPI_FUNCTION_TRACE_PTR ("ex_convert_to_string", obj_desc);
  335. switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
  336. case ACPI_TYPE_STRING:
  337. /* No conversion necessary */
  338. *result_desc = obj_desc;
  339. return_ACPI_STATUS (AE_OK);
  340. case ACPI_TYPE_INTEGER:
  341. switch (type) {
  342. case ACPI_EXPLICIT_CONVERT_DECIMAL:
  343. /* Make room for maximum decimal number */
  344. string_length = ACPI_MAX_DECIMAL_DIGITS;
  345. base = 10;
  346. break;
  347. default:
  348. /* Two hex string characters for each integer byte */
  349. string_length = ACPI_MUL_2 (acpi_gbl_integer_byte_width);
  350. break;
  351. }
  352. /*
  353. * Create a new String
  354. * Need enough space for one ASCII integer (plus null terminator)
  355. */
  356. return_desc = 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 = acpi_ex_convert_to_ascii (obj_desc->integer.value, base,
  363. new_buf, acpi_gbl_integer_byte_width);
  364. /* Null terminate at the correct place */
  365. return_desc->string.length = string_length;
  366. new_buf [string_length] = 0;
  367. break;
  368. case ACPI_TYPE_BUFFER:
  369. /* Setup string length, base, and separator */
  370. switch (type) {
  371. case ACPI_EXPLICIT_CONVERT_DECIMAL: /* Used by to_decimal_string operator */
  372. /*
  373. * From ACPI: "If Data is a buffer, it is converted to a string of
  374. * decimal values separated by commas."
  375. */
  376. base = 10;
  377. /*
  378. * Calculate the final string length. Individual string values
  379. * are variable length (include separator for each)
  380. */
  381. for (i = 0; i < obj_desc->buffer.length; i++) {
  382. if (obj_desc->buffer.pointer[i] >= 100) {
  383. string_length += 4;
  384. }
  385. else if (obj_desc->buffer.pointer[i] >= 10) {
  386. string_length += 3;
  387. }
  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 operator */
  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. * Perform the conversion.
  414. * (-1 because of extra separator included in string_length from above)
  415. */
  416. string_length--;
  417. if (string_length > ACPI_MAX_STRING_CONVERSION) /* ACPI limit */ {
  418. return_ACPI_STATUS (AE_AML_STRING_LIMIT);
  419. }
  420. /*
  421. * Create a new string object and string buffer
  422. */
  423. return_desc = acpi_ut_create_string_object ((acpi_size) string_length);
  424. if (!return_desc) {
  425. return_ACPI_STATUS (AE_NO_MEMORY);
  426. }
  427. new_buf = return_desc->buffer.pointer;
  428. /*
  429. * Convert buffer bytes to hex or decimal values
  430. * (separated by commas or spaces)
  431. */
  432. for (i = 0; i < obj_desc->buffer.length; i++) {
  433. new_buf += acpi_ex_convert_to_ascii (
  434. (acpi_integer) obj_desc->buffer.pointer[i], base,
  435. new_buf, 1);
  436. *new_buf++ = separator; /* each separated by a comma or space */
  437. }
  438. /* Null terminate the string (overwrites final comma/space from above) */
  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 (
  464. acpi_object_type destination_type,
  465. union acpi_operand_object *source_desc,
  466. union acpi_operand_object **result_desc,
  467. struct acpi_walk_state *walk_state)
  468. {
  469. acpi_status status = AE_OK;
  470. ACPI_FUNCTION_TRACE ("ex_convert_to_target_type");
  471. /* Default behavior */
  472. *result_desc = source_desc;
  473. /*
  474. * If required by the target,
  475. * perform implicit conversion on the source before we store it.
  476. */
  477. switch (GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args)) {
  478. case ARGI_SIMPLE_TARGET:
  479. case ARGI_FIXED_TARGET:
  480. case ARGI_INTEGER_REF: /* Handles Increment, Decrement cases */
  481. switch (destination_type) {
  482. case ACPI_TYPE_LOCAL_REGION_FIELD:
  483. /*
  484. * Named field can always handle conversions
  485. */
  486. break;
  487. default:
  488. /* No conversion allowed for these types */
  489. if (destination_type != 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 (source_desc),
  493. acpi_ut_get_type_name (destination_type)));
  494. status = AE_TYPE;
  495. }
  496. }
  497. break;
  498. case ARGI_TARGETREF:
  499. switch (destination_type) {
  500. case ACPI_TYPE_INTEGER:
  501. case ACPI_TYPE_BUFFER_FIELD:
  502. case ACPI_TYPE_LOCAL_BANK_FIELD:
  503. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  504. /*
  505. * These types require an Integer operand. We can convert
  506. * a Buffer or a String to an Integer if necessary.
  507. */
  508. status = acpi_ex_convert_to_integer (source_desc, result_desc,
  509. 16);
  510. break;
  511. case ACPI_TYPE_STRING:
  512. /*
  513. * The operand must be a String. We can convert an
  514. * Integer or Buffer if necessary
  515. */
  516. status = acpi_ex_convert_to_string (source_desc, result_desc,
  517. ACPI_IMPLICIT_CONVERT_HEX);
  518. break;
  519. case ACPI_TYPE_BUFFER:
  520. /*
  521. * The operand must be a Buffer. We can convert an
  522. * Integer or String if necessary
  523. */
  524. status = acpi_ex_convert_to_buffer (source_desc, result_desc);
  525. break;
  526. default:
  527. ACPI_REPORT_ERROR (("Bad destination type during conversion: %X\n",
  528. destination_type));
  529. status = AE_AML_INTERNAL;
  530. break;
  531. }
  532. break;
  533. case ARGI_REFERENCE:
  534. /*
  535. * create_xxxx_field cases - we are storing the field object into the name
  536. */
  537. break;
  538. default:
  539. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  540. "Unknown Target type ID 0x%X Op %s dest_type %s\n",
  541. GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args),
  542. walk_state->op_info->name, acpi_ut_get_type_name (destination_type)));
  543. ACPI_REPORT_ERROR (("Bad Target Type (ARGI): %X\n",
  544. GET_CURRENT_ARG_TYPE (walk_state->op_info->runtime_args)))
  545. status = AE_AML_INTERNAL;
  546. }
  547. /*
  548. * Source-to-Target conversion semantics:
  549. *
  550. * If conversion to the target type cannot be performed, then simply
  551. * overwrite the target with the new object and type.
  552. */
  553. if (status == AE_TYPE) {
  554. status = AE_OK;
  555. }
  556. return_ACPI_STATUS (status);
  557. }