1 /******************************************************************************
3 * Module Name: nsconvert - Object conversions for objects returned by
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2013, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME("nsconvert")
55 /*******************************************************************************
57 * FUNCTION: acpi_ns_convert_to_integer
59 * PARAMETERS: original_object - Object to be converted
60 * return_object - Where the new converted object is returned
62 * RETURN: Status. AE_OK if conversion was successful.
64 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
66 ******************************************************************************/
68 acpi_ns_convert_to_integer(union acpi_operand_object
*original_object
,
69 union acpi_operand_object
**return_object
)
71 union acpi_operand_object
*new_object
;
76 switch (original_object
->common
.type
) {
77 case ACPI_TYPE_STRING
:
79 /* String-to-Integer conversion */
81 status
= acpi_ut_strtoul64(original_object
->string
.pointer
,
82 ACPI_ANY_BASE
, &value
);
83 if (ACPI_FAILURE(status
)) {
88 case ACPI_TYPE_BUFFER
:
90 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
92 if (original_object
->buffer
.length
> 8) {
93 return (AE_AML_OPERAND_TYPE
);
96 /* Extract each buffer byte to create the integer */
98 for (i
= 0; i
< original_object
->buffer
.length
; i
++) {
100 ((u64
)original_object
->buffer
.
101 pointer
[i
] << (i
* 8));
106 return (AE_AML_OPERAND_TYPE
);
109 new_object
= acpi_ut_create_integer_object(value
);
111 return (AE_NO_MEMORY
);
114 *return_object
= new_object
;
118 /*******************************************************************************
120 * FUNCTION: acpi_ns_convert_to_string
122 * PARAMETERS: original_object - Object to be converted
123 * return_object - Where the new converted object is returned
125 * RETURN: Status. AE_OK if conversion was successful.
127 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
129 ******************************************************************************/
132 acpi_ns_convert_to_string(union acpi_operand_object
*original_object
,
133 union acpi_operand_object
**return_object
)
135 union acpi_operand_object
*new_object
;
139 switch (original_object
->common
.type
) {
140 case ACPI_TYPE_INTEGER
:
142 * Integer-to-String conversion. Commonly, convert
143 * an integer of value 0 to a NULL string. The last element of
144 * _BIF and _BIX packages occasionally need this fix.
146 if (original_object
->integer
.value
== 0) {
148 /* Allocate a new NULL string object */
150 new_object
= acpi_ut_create_string_object(0);
152 return (AE_NO_MEMORY
);
156 acpi_ex_convert_to_string(original_object
,
158 ACPI_IMPLICIT_CONVERT_HEX
);
159 if (ACPI_FAILURE(status
)) {
165 case ACPI_TYPE_BUFFER
:
167 * Buffer-to-String conversion. Use a to_string
168 * conversion, no transform performed on the buffer data. The best
169 * example of this is the _BIF method, where the string data from
170 * the battery is often (incorrectly) returned as buffer object(s).
173 while ((length
< original_object
->buffer
.length
) &&
174 (original_object
->buffer
.pointer
[length
])) {
178 /* Allocate a new string object */
180 new_object
= acpi_ut_create_string_object(length
);
182 return (AE_NO_MEMORY
);
186 * Copy the raw buffer data with no transform. String is already NULL
187 * terminated at Length+1.
189 ACPI_MEMCPY(new_object
->string
.pointer
,
190 original_object
->buffer
.pointer
, length
);
194 return (AE_AML_OPERAND_TYPE
);
197 *return_object
= new_object
;
201 /*******************************************************************************
203 * FUNCTION: acpi_ns_convert_to_buffer
205 * PARAMETERS: original_object - Object to be converted
206 * return_object - Where the new converted object is returned
208 * RETURN: Status. AE_OK if conversion was successful.
210 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
212 ******************************************************************************/
215 acpi_ns_convert_to_buffer(union acpi_operand_object
*original_object
,
216 union acpi_operand_object
**return_object
)
218 union acpi_operand_object
*new_object
;
220 union acpi_operand_object
**elements
;
225 switch (original_object
->common
.type
) {
226 case ACPI_TYPE_INTEGER
:
228 * Integer-to-Buffer conversion.
229 * Convert the Integer to a packed-byte buffer. _MAT and other
230 * objects need this sometimes, if a read has been performed on a
231 * Field object that is less than or equal to the global integer
232 * size (32 or 64 bits).
235 acpi_ex_convert_to_buffer(original_object
, &new_object
);
236 if (ACPI_FAILURE(status
)) {
241 case ACPI_TYPE_STRING
:
243 /* String-to-Buffer conversion. Simple data copy */
246 acpi_ut_create_buffer_object(original_object
->string
.
249 return (AE_NO_MEMORY
);
252 ACPI_MEMCPY(new_object
->buffer
.pointer
,
253 original_object
->string
.pointer
,
254 original_object
->string
.length
);
257 case ACPI_TYPE_PACKAGE
:
259 * This case is often seen for predefined names that must return a
260 * Buffer object with multiple DWORD integers within. For example,
261 * _FDE and _GTM. The Package can be converted to a Buffer.
264 /* All elements of the Package must be integers */
266 elements
= original_object
->package
.elements
;
267 count
= original_object
->package
.count
;
269 for (i
= 0; i
< count
; i
++) {
271 ((*elements
)->common
.type
!= ACPI_TYPE_INTEGER
)) {
272 return (AE_AML_OPERAND_TYPE
);
277 /* Create the new buffer object to replace the Package */
279 new_object
= acpi_ut_create_buffer_object(ACPI_MUL_4(count
));
281 return (AE_NO_MEMORY
);
284 /* Copy the package elements (integers) to the buffer as DWORDs */
286 elements
= original_object
->package
.elements
;
287 dword_buffer
= ACPI_CAST_PTR(u32
, new_object
->buffer
.pointer
);
289 for (i
= 0; i
< count
; i
++) {
290 *dword_buffer
= (u32
)(*elements
)->integer
.value
;
297 return (AE_AML_OPERAND_TYPE
);
300 *return_object
= new_object
;
304 /*******************************************************************************
306 * FUNCTION: acpi_ns_convert_to_unicode
308 * PARAMETERS: original_object - ASCII String Object to be converted
309 * return_object - Where the new converted object is returned
311 * RETURN: Status. AE_OK if conversion was successful.
313 * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
315 ******************************************************************************/
318 acpi_ns_convert_to_unicode(union acpi_operand_object
*original_object
,
319 union acpi_operand_object
**return_object
)
321 union acpi_operand_object
*new_object
;
327 if (!original_object
) {
331 /* If a Buffer was returned, it must be at least two bytes long */
333 if (original_object
->common
.type
== ACPI_TYPE_BUFFER
) {
334 if (original_object
->buffer
.length
< 2) {
335 return (AE_AML_OPERAND_VALUE
);
338 *return_object
= NULL
;
343 * The original object is an ASCII string. Convert this string to
346 ascii_string
= original_object
->string
.pointer
;
347 unicode_length
= (original_object
->string
.length
* 2) + 2;
349 /* Create a new buffer object for the Unicode data */
351 new_object
= acpi_ut_create_buffer_object(unicode_length
);
353 return (AE_NO_MEMORY
);
356 unicode_buffer
= ACPI_CAST_PTR(u16
, new_object
->buffer
.pointer
);
358 /* Convert ASCII to Unicode */
360 for (i
= 0; i
< original_object
->string
.length
; i
++) {
361 unicode_buffer
[i
] = (u16
)ascii_string
[i
];
364 *return_object
= new_object
;
368 /*******************************************************************************
370 * FUNCTION: acpi_ns_convert_to_resource
372 * PARAMETERS: original_object - Object to be converted
373 * return_object - Where the new converted object is returned
375 * RETURN: Status. AE_OK if conversion was successful
377 * DESCRIPTION: Attempt to convert a Integer object to a resource_template
380 ******************************************************************************/
383 acpi_ns_convert_to_resource(union acpi_operand_object
*original_object
,
384 union acpi_operand_object
**return_object
)
386 union acpi_operand_object
*new_object
;
390 * We can fix the following cases for an expected resource template:
391 * 1. No return value (interpreter slack mode is disabled)
392 * 2. A "Return (Zero)" statement
393 * 3. A "Return empty buffer" statement
395 * We will return a buffer containing a single end_tag
396 * resource descriptor.
398 if (original_object
) {
399 switch (original_object
->common
.type
) {
400 case ACPI_TYPE_INTEGER
:
402 /* We can only repair an Integer==0 */
404 if (original_object
->integer
.value
) {
405 return (AE_AML_OPERAND_TYPE
);
409 case ACPI_TYPE_BUFFER
:
411 if (original_object
->buffer
.length
) {
413 /* Additional checks can be added in the future */
415 *return_object
= NULL
;
420 case ACPI_TYPE_STRING
:
423 return (AE_AML_OPERAND_TYPE
);
427 /* Create the new buffer object for the resource descriptor */
429 new_object
= acpi_ut_create_buffer_object(2);
431 return (AE_NO_MEMORY
);
434 buffer
= ACPI_CAST_PTR(u8
, new_object
->buffer
.pointer
);
436 /* Initialize the Buffer with a single end_tag descriptor */
438 buffer
[0] = (ACPI_RESOURCE_NAME_END_TAG
| ASL_RDESC_END_TAG_SIZE
);
441 *return_object
= new_object
;