exconvrt.c 17.4 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5 6 7
/******************************************************************************
 *
 * Module Name: exconvrt - Object conversion routines
 *
 *****************************************************************************/

/*
8
 * Copyright (C) 2000 - 2016, Intel Corp.
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
Len Brown's avatar
Len Brown committed
45 46 47
#include "accommon.h"
#include "acinterp.h"
#include "amlcode.h"
Linus Torvalds's avatar
Linus Torvalds committed
48 49

#define _COMPONENT          ACPI_EXECUTER
Len Brown's avatar
Len Brown committed
50
ACPI_MODULE_NAME("exconvrt")
Linus Torvalds's avatar
Linus Torvalds committed
51

Robert Moore's avatar
Robert Moore committed
52 53
/* Local prototypes */
static u32
54
acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 max_length);
Linus Torvalds's avatar
Linus Torvalds committed
55 56 57 58 59

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_convert_to_integer
 *
60
 * PARAMETERS:  obj_desc        - Object to be converted. Must be an
Linus Torvalds's avatar
Linus Torvalds committed
61 62
 *                                Integer, Buffer, or String
 *              result_desc     - Where the new Integer object is returned
63
 *              flags           - Used for string conversion
Linus Torvalds's avatar
Linus Torvalds committed
64 65 66 67 68 69 70 71
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert an ACPI Object to an integer.
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
72 73
acpi_ex_convert_to_integer(union acpi_operand_object *obj_desc,
			   union acpi_operand_object **result_desc, u32 flags)
Linus Torvalds's avatar
Linus Torvalds committed
74
{
Len Brown's avatar
Len Brown committed
75 76
	union acpi_operand_object *return_desc;
	u8 *pointer;
77
	u64 result;
Len Brown's avatar
Len Brown committed
78 79 80
	u32 i;
	u32 count;
	acpi_status status;
Linus Torvalds's avatar
Linus Torvalds committed
81

Bob Moore's avatar
Bob Moore committed
82
	ACPI_FUNCTION_TRACE_PTR(ex_convert_to_integer, obj_desc);
Linus Torvalds's avatar
Linus Torvalds committed
83

84
	switch (obj_desc->common.type) {
Linus Torvalds's avatar
Linus Torvalds committed
85 86 87 88 89
	case ACPI_TYPE_INTEGER:

		/* No conversion necessary */

		*result_desc = obj_desc;
Len Brown's avatar
Len Brown committed
90
		return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
91 92 93 94 95 96 97

	case ACPI_TYPE_BUFFER:
	case ACPI_TYPE_STRING:

		/* Note: Takes advantage of common buffer/string fields */

		pointer = obj_desc->buffer.pointer;
Len Brown's avatar
Len Brown committed
98
		count = obj_desc->buffer.length;
Linus Torvalds's avatar
Linus Torvalds committed
99 100 101
		break;

	default:
102

Len Brown's avatar
Len Brown committed
103
		return_ACPI_STATUS(AE_TYPE);
Linus Torvalds's avatar
Linus Torvalds committed
104 105 106
	}

	/*
107
	 * Convert the buffer/string to an integer. Note that both buffers and
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116
	 * strings are treated as raw data - we don't convert ascii to hex for
	 * strings.
	 *
	 * There are two terminating conditions for the loop:
	 * 1) The size of an integer has been reached, or
	 * 2) The end of the buffer or string has been reached
	 */
	result = 0;

Robert Moore's avatar
Robert Moore committed
117 118
	/* String conversion is different than Buffer conversion */

119
	switch (obj_desc->common.type) {
Linus Torvalds's avatar
Linus Torvalds committed
120 121 122
	case ACPI_TYPE_STRING:
		/*
		 * Convert string to an integer - for most cases, the string must be
123
		 * hexadecimal as per the ACPI specification. The only exception (as
Linus Torvalds's avatar
Linus Torvalds committed
124 125 126
		 * of ACPI 3.0) is that the to_integer() operator allows both decimal
		 * and hexadecimal strings (hex prefixed with "0x").
		 */
Len Brown's avatar
Len Brown committed
127 128 129
		status = acpi_ut_strtoul64((char *)pointer, flags, &result);
		if (ACPI_FAILURE(status)) {
			return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
130 131 132 133 134 135 136 137
		}
		break;

	case ACPI_TYPE_BUFFER:

		/* Check for zero-length buffer */

		if (!count) {
Len Brown's avatar
Len Brown committed
138
			return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		}

		/* Transfer no more than an integer's worth of data */

		if (count > acpi_gbl_integer_byte_width) {
			count = acpi_gbl_integer_byte_width;
		}

		/*
		 * Convert buffer to an integer - we simply grab enough raw data
		 * from the buffer to fill an integer
		 */
		for (i = 0; i < count; i++) {
			/*
			 * Get next byte and shift it into the Result.
			 * Little endian is used, meaning that the first byte of the buffer
			 * is the LSB of the integer
			 */
157
			result |= (((u64) pointer[i]) << (i * 8));
Linus Torvalds's avatar
Linus Torvalds committed
158 159 160 161
		}
		break;

	default:
162

Linus Torvalds's avatar
Linus Torvalds committed
163
		/* No other types can get here */
164

Linus Torvalds's avatar
Linus Torvalds committed
165 166 167
		break;
	}

Robert Moore's avatar
Robert Moore committed
168 169
	/* Create a new integer */

170
	return_desc = acpi_ut_create_integer_object(result);
Linus Torvalds's avatar
Linus Torvalds committed
171
	if (!return_desc) {
Len Brown's avatar
Len Brown committed
172
		return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
173 174
	}

Bob Moore's avatar
Bob Moore committed
175 176 177
	ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
			  ACPI_FORMAT_UINT64(result)));

Linus Torvalds's avatar
Linus Torvalds committed
178 179
	/* Save the Result */

180
	(void)acpi_ex_truncate_for32bit_table(return_desc);
Linus Torvalds's avatar
Linus Torvalds committed
181
	*result_desc = return_desc;
Len Brown's avatar
Len Brown committed
182
	return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_convert_to_buffer
 *
189
 * PARAMETERS:  obj_desc        - Object to be converted. Must be an
Linus Torvalds's avatar
Linus Torvalds committed
190 191 192 193 194 195 196 197 198 199
 *                                Integer, Buffer, or String
 *              result_desc     - Where the new buffer object is returned
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert an ACPI Object to a Buffer
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
200 201
acpi_ex_convert_to_buffer(union acpi_operand_object *obj_desc,
			  union acpi_operand_object **result_desc)
Linus Torvalds's avatar
Linus Torvalds committed
202
{
Len Brown's avatar
Len Brown committed
203 204
	union acpi_operand_object *return_desc;
	u8 *new_buf;
Linus Torvalds's avatar
Linus Torvalds committed
205

Bob Moore's avatar
Bob Moore committed
206
	ACPI_FUNCTION_TRACE_PTR(ex_convert_to_buffer, obj_desc);
Linus Torvalds's avatar
Linus Torvalds committed
207

208
	switch (obj_desc->common.type) {
Linus Torvalds's avatar
Linus Torvalds committed
209 210 211 212 213
	case ACPI_TYPE_BUFFER:

		/* No conversion necessary */

		*result_desc = obj_desc;
Len Brown's avatar
Len Brown committed
214
		return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219 220

	case ACPI_TYPE_INTEGER:
		/*
		 * Create a new Buffer object.
		 * Need enough space for one integer
		 */
Len Brown's avatar
Len Brown committed
221 222
		return_desc =
		    acpi_ut_create_buffer_object(acpi_gbl_integer_byte_width);
Linus Torvalds's avatar
Linus Torvalds committed
223
		if (!return_desc) {
Len Brown's avatar
Len Brown committed
224
			return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
225 226 227 228 229
		}

		/* Copy the integer to the buffer, LSB first */

		new_buf = return_desc->buffer.pointer;
230 231
		memcpy(new_buf, &obj_desc->integer.value,
		       acpi_gbl_integer_byte_width);
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236 237 238 239 240 241 242 243
		break;

	case ACPI_TYPE_STRING:
		/*
		 * Create a new Buffer object
		 * Size will be the string length
		 *
		 * NOTE: Add one to the string length to include the null terminator.
		 * The ACPI spec is unclear on this subject, but there is existing
		 * ASL/AML code that depends on the null being transferred to the new
		 * buffer.
		 */
Len Brown's avatar
Len Brown committed
244 245 246
		return_desc = acpi_ut_create_buffer_object((acpi_size)
							   obj_desc->string.
							   length + 1);
Linus Torvalds's avatar
Linus Torvalds committed
247
		if (!return_desc) {
Len Brown's avatar
Len Brown committed
248
			return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
249 250 251 252 253
		}

		/* Copy the string to the buffer */

		new_buf = return_desc->buffer.pointer;
254 255
		strncpy((char *)new_buf, (char *)obj_desc->string.pointer,
			obj_desc->string.length);
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258
		break;

	default:
259

Len Brown's avatar
Len Brown committed
260
		return_ACPI_STATUS(AE_TYPE);
Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266
	}

	/* Mark buffer initialized */

	return_desc->common.flags |= AOPOBJ_DATA_VALID;
	*result_desc = return_desc;
Len Brown's avatar
Len Brown committed
267
	return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
268 269 270 271 272 273
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_convert_to_ascii
 *
274 275 276
 * PARAMETERS:  integer         - Value to be converted
 *              base            - ACPI_STRING_DECIMAL or ACPI_STRING_HEX
 *              string          - Where the string is returned
Linus Torvalds's avatar
Linus Torvalds committed
277 278 279 280 281 282 283 284
 *              data_width      - Size of data item to be converted, in bytes
 *
 * RETURN:      Actual string length
 *
 * DESCRIPTION: Convert an ACPI Integer to a hex or decimal string
 *
 ******************************************************************************/

Robert Moore's avatar
Robert Moore committed
285
static u32
286
acpi_ex_convert_to_ascii(u64 integer, u16 base, u8 *string, u8 data_width)
Linus Torvalds's avatar
Linus Torvalds committed
287
{
288
	u64 digit;
289 290 291 292 293
	u32 i;
	u32 j;
	u32 k = 0;
	u32 hex_length;
	u32 decimal_length;
Len Brown's avatar
Len Brown committed
294 295
	u32 remainder;
	u8 supress_zeros;
Linus Torvalds's avatar
Linus Torvalds committed
296

Len Brown's avatar
Len Brown committed
297
	ACPI_FUNCTION_ENTRY();
Linus Torvalds's avatar
Linus Torvalds committed
298 299 300 301 302 303 304 305

	switch (base) {
	case 10:

		/* Setup max length for the decimal number */

		switch (data_width) {
		case 1:
306

Linus Torvalds's avatar
Linus Torvalds committed
307 308 309 310
			decimal_length = ACPI_MAX8_DECIMAL_DIGITS;
			break;

		case 4:
311

Linus Torvalds's avatar
Linus Torvalds committed
312 313 314 315 316
			decimal_length = ACPI_MAX32_DECIMAL_DIGITS;
			break;

		case 8:
		default:
317

Linus Torvalds's avatar
Linus Torvalds committed
318 319 320 321
			decimal_length = ACPI_MAX64_DECIMAL_DIGITS;
			break;
		}

Len Brown's avatar
Len Brown committed
322
		supress_zeros = TRUE;	/* No leading zeros */
Linus Torvalds's avatar
Linus Torvalds committed
323 324 325
		remainder = 0;

		for (i = decimal_length; i > 0; i--) {
Bob Moore's avatar
Bob Moore committed
326

Linus Torvalds's avatar
Linus Torvalds committed
327 328 329 330
			/* Divide by nth factor of 10 */

			digit = integer;
			for (j = 0; j < i; j++) {
Len Brown's avatar
Len Brown committed
331 332
				(void)acpi_ut_short_divide(digit, 10, &digit,
							   &remainder);
Linus Torvalds's avatar
Linus Torvalds committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			}

			/* Handle leading zeros */

			if (remainder != 0) {
				supress_zeros = FALSE;
			}

			if (!supress_zeros) {
				string[k] = (u8) (ACPI_ASCII_ZERO + remainder);
				k++;
			}
		}
		break;

	case 16:

Robert Moore's avatar
Robert Moore committed
350
		/* hex_length: 2 ascii hex chars per data byte */
Linus Torvalds's avatar
Linus Torvalds committed
351

352
		hex_length = ACPI_MUL_2(data_width);
Len Brown's avatar
Len Brown committed
353
		for (i = 0, j = (hex_length - 1); i < hex_length; i++, j--) {
Bob Moore's avatar
Bob Moore committed
354

Linus Torvalds's avatar
Linus Torvalds committed
355 356
			/* Get one hex digit, most significant digits first */

357 358
			string[k] = (u8)
			    acpi_ut_hex_to_ascii_char(integer, ACPI_MUL_4(j));
Linus Torvalds's avatar
Linus Torvalds committed
359 360 361 362 363 364 365 366 367
			k++;
		}
		break;

	default:
		return (0);
	}

	/*
368
	 * Since leading zeros are suppressed, we must check for the case where
Linus Torvalds's avatar
Linus Torvalds committed
369 370 371 372 373
	 * the integer equals 0
	 *
	 * Finally, null terminate the string and return the length
	 */
	if (!k) {
Len Brown's avatar
Len Brown committed
374
		string[0] = ACPI_ASCII_ZERO;
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377
		k = 1;
	}

Len Brown's avatar
Len Brown committed
378
	string[k] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
379 380 381 382 383 384 385
	return ((u32) k);
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_convert_to_string
 *
386
 * PARAMETERS:  obj_desc        - Object to be converted. Must be an
Linus Torvalds's avatar
Linus Torvalds committed
387 388
 *                                Integer, Buffer, or String
 *              result_desc     - Where the string object is returned
389
 *              type            - String flags (base and conversion type)
Linus Torvalds's avatar
Linus Torvalds committed
390 391 392 393 394 395 396 397
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert an ACPI Object to a string
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
398 399
acpi_ex_convert_to_string(union acpi_operand_object * obj_desc,
			  union acpi_operand_object ** result_desc, u32 type)
Linus Torvalds's avatar
Linus Torvalds committed
400
{
Len Brown's avatar
Len Brown committed
401 402 403 404 405 406
	union acpi_operand_object *return_desc;
	u8 *new_buf;
	u32 i;
	u32 string_length = 0;
	u16 base = 16;
	u8 separator = ',';
Linus Torvalds's avatar
Linus Torvalds committed
407

Bob Moore's avatar
Bob Moore committed
408
	ACPI_FUNCTION_TRACE_PTR(ex_convert_to_string, obj_desc);
Linus Torvalds's avatar
Linus Torvalds committed
409

410
	switch (obj_desc->common.type) {
Linus Torvalds's avatar
Linus Torvalds committed
411 412 413 414 415
	case ACPI_TYPE_STRING:

		/* No conversion necessary */

		*result_desc = obj_desc;
Len Brown's avatar
Len Brown committed
416
		return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

	case ACPI_TYPE_INTEGER:

		switch (type) {
		case ACPI_EXPLICIT_CONVERT_DECIMAL:

			/* Make room for maximum decimal number */

			string_length = ACPI_MAX_DECIMAL_DIGITS;
			base = 10;
			break;

		default:

			/* Two hex string characters for each integer byte */

Len Brown's avatar
Len Brown committed
433
			string_length = ACPI_MUL_2(acpi_gbl_integer_byte_width);
Linus Torvalds's avatar
Linus Torvalds committed
434 435 436 437 438 439 440
			break;
		}

		/*
		 * Create a new String
		 * Need enough space for one ASCII integer (plus null terminator)
		 */
Len Brown's avatar
Len Brown committed
441 442
		return_desc =
		    acpi_ut_create_string_object((acpi_size) string_length);
Linus Torvalds's avatar
Linus Torvalds committed
443
		if (!return_desc) {
Len Brown's avatar
Len Brown committed
444
			return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
445 446 447 448 449 450
		}

		new_buf = return_desc->buffer.pointer;

		/* Convert integer to string */

Len Brown's avatar
Len Brown committed
451 452 453 454
		string_length =
		    acpi_ex_convert_to_ascii(obj_desc->integer.value, base,
					     new_buf,
					     acpi_gbl_integer_byte_width);
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457 458

		/* Null terminate at the correct place */

		return_desc->string.length = string_length;
Len Brown's avatar
Len Brown committed
459
		new_buf[string_length] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464 465 466
		break;

	case ACPI_TYPE_BUFFER:

		/* Setup string length, base, and separator */

		switch (type) {
Len Brown's avatar
Len Brown committed
467
		case ACPI_EXPLICIT_CONVERT_DECIMAL:	/* Used by to_decimal_string */
Linus Torvalds's avatar
Linus Torvalds committed
468 469 470 471 472 473 474
			/*
			 * From ACPI: "If Data is a buffer, it is converted to a string of
			 * decimal values separated by commas."
			 */
			base = 10;

			/*
475
			 * Calculate the final string length. Individual string values
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478 479 480
			 * are variable length (include separator for each)
			 */
			for (i = 0; i < obj_desc->buffer.length; i++) {
				if (obj_desc->buffer.pointer[i] >= 100) {
					string_length += 4;
Len Brown's avatar
Len Brown committed
481
				} else if (obj_desc->buffer.pointer[i] >= 10) {
Linus Torvalds's avatar
Linus Torvalds committed
482
					string_length += 3;
Len Brown's avatar
Len Brown committed
483
				} else {
Linus Torvalds's avatar
Linus Torvalds committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
					string_length += 2;
				}
			}
			break;

		case ACPI_IMPLICIT_CONVERT_HEX:
			/*
			 * From the ACPI spec:
			 *"The entire contents of the buffer are converted to a string of
			 * two-character hexadecimal numbers, each separated by a space."
			 */
			separator = ' ';
			string_length = (obj_desc->buffer.length * 3);
			break;

Len Brown's avatar
Len Brown committed
499
		case ACPI_EXPLICIT_CONVERT_HEX:	/* Used by to_hex_string */
Linus Torvalds's avatar
Linus Torvalds committed
500 501 502 503 504 505 506 507
			/*
			 * From ACPI: "If Data is a buffer, it is converted to a string of
			 * hexadecimal values separated by commas."
			 */
			string_length = (obj_desc->buffer.length * 3);
			break;

		default:
Len Brown's avatar
Len Brown committed
508
			return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds's avatar
Linus Torvalds committed
509 510 511
		}

		/*
Bob Moore's avatar
Bob Moore committed
512
		 * Create a new string object and string buffer
Linus Torvalds's avatar
Linus Torvalds committed
513
		 * (-1 because of extra separator included in string_length from above)
514
		 * Allow creation of zero-length strings from zero-length buffers.
Linus Torvalds's avatar
Linus Torvalds committed
515
		 */
516 517 518 519
		if (string_length) {
			string_length--;
		}

520 521
		return_desc =
		    acpi_ut_create_string_object((acpi_size) string_length);
Linus Torvalds's avatar
Linus Torvalds committed
522
		if (!return_desc) {
Len Brown's avatar
Len Brown committed
523
			return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds's avatar
Linus Torvalds committed
524 525 526 527 528 529 530 531 532
		}

		new_buf = return_desc->buffer.pointer;

		/*
		 * Convert buffer bytes to hex or decimal values
		 * (separated by commas or spaces)
		 */
		for (i = 0; i < obj_desc->buffer.length; i++) {
533 534 535
			new_buf += acpi_ex_convert_to_ascii((u64) obj_desc->
							    buffer.pointer[i],
							    base, new_buf, 1);
Len Brown's avatar
Len Brown committed
536
			*new_buf++ = separator;	/* each separated by a comma or space */
Linus Torvalds's avatar
Linus Torvalds committed
537 538
		}

Robert Moore's avatar
Robert Moore committed
539 540 541 542
		/*
		 * Null terminate the string
		 * (overwrites final comma/space from above)
		 */
543 544 545
		if (obj_desc->buffer.length) {
			new_buf--;
		}
Linus Torvalds's avatar
Linus Torvalds committed
546 547 548 549
		*new_buf = 0;
		break;

	default:
550

Len Brown's avatar
Len Brown committed
551
		return_ACPI_STATUS(AE_TYPE);
Linus Torvalds's avatar
Linus Torvalds committed
552 553 554
	}

	*result_desc = return_desc;
Len Brown's avatar
Len Brown committed
555
	return_ACPI_STATUS(AE_OK);
Linus Torvalds's avatar
Linus Torvalds committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_convert_to_target_type
 *
 * PARAMETERS:  destination_type    - Current type of the destination
 *              source_desc         - Source object to be converted.
 *              result_desc         - Where the converted object is returned
 *              walk_state          - Current method state
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Implements "implicit conversion" rules for storing an object.
 *
 ******************************************************************************/

acpi_status
Len Brown's avatar
Len Brown committed
574 575 576 577
acpi_ex_convert_to_target_type(acpi_object_type destination_type,
			       union acpi_operand_object *source_desc,
			       union acpi_operand_object **result_desc,
			       struct acpi_walk_state *walk_state)
Linus Torvalds's avatar
Linus Torvalds committed
578
{
Len Brown's avatar
Len Brown committed
579
	acpi_status status = AE_OK;
Linus Torvalds's avatar
Linus Torvalds committed
580

Bob Moore's avatar
Bob Moore committed
581
	ACPI_FUNCTION_TRACE(ex_convert_to_target_type);
Linus Torvalds's avatar
Linus Torvalds committed
582 583 584 585 586 587 588 589 590

	/* Default behavior */

	*result_desc = source_desc;

	/*
	 * If required by the target,
	 * perform implicit conversion on the source before we store it.
	 */
Len Brown's avatar
Len Brown committed
591
	switch (GET_CURRENT_ARG_TYPE(walk_state->op_info->runtime_args)) {
Linus Torvalds's avatar
Linus Torvalds committed
592 593
	case ARGI_SIMPLE_TARGET:
	case ARGI_FIXED_TARGET:
Len Brown's avatar
Len Brown committed
594
	case ARGI_INTEGER_REF:	/* Handles Increment, Decrement cases */
Linus Torvalds's avatar
Linus Torvalds committed
595 596 597 598 599 600 601 602 603

		switch (destination_type) {
		case ACPI_TYPE_LOCAL_REGION_FIELD:
			/*
			 * Named field can always handle conversions
			 */
			break;

		default:
604

Linus Torvalds's avatar
Linus Torvalds committed
605 606
			/* No conversion allowed for these types */

607
			if (destination_type != source_desc->common.type) {
Len Brown's avatar
Len Brown committed
608 609 610 611 612 613
				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
						  "Explicit operator, will store (%s) over existing type (%s)\n",
						  acpi_ut_get_object_type_name
						  (source_desc),
						  acpi_ut_get_type_name
						  (destination_type)));
Linus Torvalds's avatar
Linus Torvalds committed
614 615 616 617 618 619
				status = AE_TYPE;
			}
		}
		break;

	case ARGI_TARGETREF:
620
	case ARGI_STORE_TARGET:
Linus Torvalds's avatar
Linus Torvalds committed
621 622 623 624 625 626 627

		switch (destination_type) {
		case ACPI_TYPE_INTEGER:
		case ACPI_TYPE_BUFFER_FIELD:
		case ACPI_TYPE_LOCAL_BANK_FIELD:
		case ACPI_TYPE_LOCAL_INDEX_FIELD:
			/*
628
			 * These types require an Integer operand. We can convert
Linus Torvalds's avatar
Linus Torvalds committed
629 630
			 * a Buffer or a String to an Integer if necessary.
			 */
Len Brown's avatar
Len Brown committed
631 632 633
			status =
			    acpi_ex_convert_to_integer(source_desc, result_desc,
						       16);
Linus Torvalds's avatar
Linus Torvalds committed
634 635 636 637
			break;

		case ACPI_TYPE_STRING:
			/*
638
			 * The operand must be a String. We can convert an
Linus Torvalds's avatar
Linus Torvalds committed
639 640
			 * Integer or Buffer if necessary
			 */
Len Brown's avatar
Len Brown committed
641 642 643
			status =
			    acpi_ex_convert_to_string(source_desc, result_desc,
						      ACPI_IMPLICIT_CONVERT_HEX);
Linus Torvalds's avatar
Linus Torvalds committed
644 645 646 647
			break;

		case ACPI_TYPE_BUFFER:
			/*
648
			 * The operand must be a Buffer. We can convert an
Linus Torvalds's avatar
Linus Torvalds committed
649 650
			 * Integer or String if necessary
			 */
Len Brown's avatar
Len Brown committed
651 652
			status =
			    acpi_ex_convert_to_buffer(source_desc, result_desc);
Linus Torvalds's avatar
Linus Torvalds committed
653 654 655
			break;

		default:
656

Bob Moore's avatar
Bob Moore committed
657
			ACPI_ERROR((AE_INFO,
658
				    "Bad destination type during conversion: 0x%X",
Bob Moore's avatar
Bob Moore committed
659
				    destination_type));
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662 663 664 665 666 667 668 669 670 671
			status = AE_AML_INTERNAL;
			break;
		}
		break;

	case ARGI_REFERENCE:
		/*
		 * create_xxxx_field cases - we are storing the field object into the name
		 */
		break;

	default:
672

Bob Moore's avatar
Bob Moore committed
673
		ACPI_ERROR((AE_INFO,
674
			    "Unknown Target type ID 0x%X AmlOpcode 0x%X DestType %s",
Bob Moore's avatar
Bob Moore committed
675 676 677 678
			    GET_CURRENT_ARG_TYPE(walk_state->op_info->
						 runtime_args),
			    walk_state->opcode,
			    acpi_ut_get_type_name(destination_type)));
Bob Moore's avatar
Bob Moore committed
679
		status = AE_AML_INTERNAL;
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682 683 684 685 686 687 688 689 690 691
	}

	/*
	 * Source-to-Target conversion semantics:
	 *
	 * If conversion to the target type cannot be performed, then simply
	 * overwrite the target with the new object and type.
	 */
	if (status == AE_TYPE) {
		status = AE_OK;
	}

Len Brown's avatar
Len Brown committed
692
	return_ACPI_STATUS(status);
Linus Torvalds's avatar
Linus Torvalds committed
693
}