1 /******************************************************************************
3 * Module Name: nsconvert - Object conversions for objects returned by
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2017, 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.
52 #define _COMPONENT ACPI_NAMESPACE
53 ACPI_MODULE_NAME ("nsconvert")
56 /*******************************************************************************
58 * FUNCTION: AcpiNsConvertToInteger
60 * PARAMETERS: OriginalObject - Object to be converted
61 * ReturnObject - Where the new converted object is returned
63 * RETURN: Status. AE_OK if conversion was successful.
65 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
67 ******************************************************************************/
70 AcpiNsConvertToInteger (
71 ACPI_OPERAND_OBJECT
*OriginalObject
,
72 ACPI_OPERAND_OBJECT
**ReturnObject
)
74 ACPI_OPERAND_OBJECT
*NewObject
;
80 switch (OriginalObject
->Common
.Type
)
82 case ACPI_TYPE_STRING
:
84 /* String-to-Integer conversion */
86 Status
= AcpiUtStrtoul64 (OriginalObject
->String
.Pointer
, &Value
);
87 if (ACPI_FAILURE (Status
))
93 case ACPI_TYPE_BUFFER
:
95 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
97 if (OriginalObject
->Buffer
.Length
> 8)
99 return (AE_AML_OPERAND_TYPE
);
102 /* Extract each buffer byte to create the integer */
104 for (i
= 0; i
< OriginalObject
->Buffer
.Length
; i
++)
107 OriginalObject
->Buffer
.Pointer
[i
] << (i
* 8));
113 return (AE_AML_OPERAND_TYPE
);
116 NewObject
= AcpiUtCreateIntegerObject (Value
);
119 return (AE_NO_MEMORY
);
122 *ReturnObject
= NewObject
;
127 /*******************************************************************************
129 * FUNCTION: AcpiNsConvertToString
131 * PARAMETERS: OriginalObject - Object to be converted
132 * ReturnObject - Where the new converted object is returned
134 * RETURN: Status. AE_OK if conversion was successful.
136 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
138 ******************************************************************************/
141 AcpiNsConvertToString (
142 ACPI_OPERAND_OBJECT
*OriginalObject
,
143 ACPI_OPERAND_OBJECT
**ReturnObject
)
145 ACPI_OPERAND_OBJECT
*NewObject
;
150 switch (OriginalObject
->Common
.Type
)
152 case ACPI_TYPE_INTEGER
:
154 * Integer-to-String conversion. Commonly, convert
155 * an integer of value 0 to a NULL string. The last element of
156 * _BIF and _BIX packages occasionally need this fix.
158 if (OriginalObject
->Integer
.Value
== 0)
160 /* Allocate a new NULL string object */
162 NewObject
= AcpiUtCreateStringObject (0);
165 return (AE_NO_MEMORY
);
170 Status
= AcpiExConvertToString (OriginalObject
,
171 &NewObject
, ACPI_IMPLICIT_CONVERT_HEX
);
172 if (ACPI_FAILURE (Status
))
179 case ACPI_TYPE_BUFFER
:
181 * Buffer-to-String conversion. Use a ToString
182 * conversion, no transform performed on the buffer data. The best
183 * example of this is the _BIF method, where the string data from
184 * the battery is often (incorrectly) returned as buffer object(s).
187 while ((Length
< OriginalObject
->Buffer
.Length
) &&
188 (OriginalObject
->Buffer
.Pointer
[Length
]))
193 /* Allocate a new string object */
195 NewObject
= AcpiUtCreateStringObject (Length
);
198 return (AE_NO_MEMORY
);
202 * Copy the raw buffer data with no transform. String is already NULL
203 * terminated at Length+1.
205 memcpy (NewObject
->String
.Pointer
,
206 OriginalObject
->Buffer
.Pointer
, Length
);
211 return (AE_AML_OPERAND_TYPE
);
214 *ReturnObject
= NewObject
;
219 /*******************************************************************************
221 * FUNCTION: AcpiNsConvertToBuffer
223 * PARAMETERS: OriginalObject - Object to be converted
224 * ReturnObject - Where the new converted object is returned
226 * RETURN: Status. AE_OK if conversion was successful.
228 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
230 ******************************************************************************/
233 AcpiNsConvertToBuffer (
234 ACPI_OPERAND_OBJECT
*OriginalObject
,
235 ACPI_OPERAND_OBJECT
**ReturnObject
)
237 ACPI_OPERAND_OBJECT
*NewObject
;
239 ACPI_OPERAND_OBJECT
**Elements
;
245 switch (OriginalObject
->Common
.Type
)
247 case ACPI_TYPE_INTEGER
:
249 * Integer-to-Buffer conversion.
250 * Convert the Integer to a packed-byte buffer. _MAT and other
251 * objects need this sometimes, if a read has been performed on a
252 * Field object that is less than or equal to the global integer
253 * size (32 or 64 bits).
255 Status
= AcpiExConvertToBuffer (OriginalObject
, &NewObject
);
256 if (ACPI_FAILURE (Status
))
262 case ACPI_TYPE_STRING
:
264 /* String-to-Buffer conversion. Simple data copy */
266 NewObject
= AcpiUtCreateBufferObject
267 (OriginalObject
->String
.Length
);
270 return (AE_NO_MEMORY
);
273 memcpy (NewObject
->Buffer
.Pointer
,
274 OriginalObject
->String
.Pointer
, OriginalObject
->String
.Length
);
277 case ACPI_TYPE_PACKAGE
:
279 * This case is often seen for predefined names that must return a
280 * Buffer object with multiple DWORD integers within. For example,
281 * _FDE and _GTM. The Package can be converted to a Buffer.
284 /* All elements of the Package must be integers */
286 Elements
= OriginalObject
->Package
.Elements
;
287 Count
= OriginalObject
->Package
.Count
;
289 for (i
= 0; i
< Count
; i
++)
292 ((*Elements
)->Common
.Type
!= ACPI_TYPE_INTEGER
))
294 return (AE_AML_OPERAND_TYPE
);
299 /* Create the new buffer object to replace the Package */
301 NewObject
= AcpiUtCreateBufferObject (ACPI_MUL_4 (Count
));
304 return (AE_NO_MEMORY
);
307 /* Copy the package elements (integers) to the buffer as DWORDs */
309 Elements
= OriginalObject
->Package
.Elements
;
310 DwordBuffer
= ACPI_CAST_PTR (UINT32
, NewObject
->Buffer
.Pointer
);
312 for (i
= 0; i
< Count
; i
++)
314 *DwordBuffer
= (UINT32
) (*Elements
)->Integer
.Value
;
322 return (AE_AML_OPERAND_TYPE
);
325 *ReturnObject
= NewObject
;
330 /*******************************************************************************
332 * FUNCTION: AcpiNsConvertToUnicode
334 * PARAMETERS: Scope - Namespace node for the method/object
335 * OriginalObject - ASCII String Object to be converted
336 * ReturnObject - Where the new converted object is returned
338 * RETURN: Status. AE_OK if conversion was successful.
340 * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
342 ******************************************************************************/
345 AcpiNsConvertToUnicode (
346 ACPI_NAMESPACE_NODE
*Scope
,
347 ACPI_OPERAND_OBJECT
*OriginalObject
,
348 ACPI_OPERAND_OBJECT
**ReturnObject
)
350 ACPI_OPERAND_OBJECT
*NewObject
;
352 UINT16
*UnicodeBuffer
;
353 UINT32 UnicodeLength
;
362 /* If a Buffer was returned, it must be at least two bytes long */
364 if (OriginalObject
->Common
.Type
== ACPI_TYPE_BUFFER
)
366 if (OriginalObject
->Buffer
.Length
< 2)
368 return (AE_AML_OPERAND_VALUE
);
371 *ReturnObject
= NULL
;
376 * The original object is an ASCII string. Convert this string to
379 AsciiString
= OriginalObject
->String
.Pointer
;
380 UnicodeLength
= (OriginalObject
->String
.Length
* 2) + 2;
382 /* Create a new buffer object for the Unicode data */
384 NewObject
= AcpiUtCreateBufferObject (UnicodeLength
);
387 return (AE_NO_MEMORY
);
390 UnicodeBuffer
= ACPI_CAST_PTR (UINT16
, NewObject
->Buffer
.Pointer
);
392 /* Convert ASCII to Unicode */
394 for (i
= 0; i
< OriginalObject
->String
.Length
; i
++)
396 UnicodeBuffer
[i
] = (UINT16
) AsciiString
[i
];
399 *ReturnObject
= NewObject
;
404 /*******************************************************************************
406 * FUNCTION: AcpiNsConvertToResource
408 * PARAMETERS: Scope - Namespace node for the method/object
409 * OriginalObject - Object to be converted
410 * ReturnObject - Where the new converted object is returned
412 * RETURN: Status. AE_OK if conversion was successful
414 * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
417 ******************************************************************************/
420 AcpiNsConvertToResource (
421 ACPI_NAMESPACE_NODE
*Scope
,
422 ACPI_OPERAND_OBJECT
*OriginalObject
,
423 ACPI_OPERAND_OBJECT
**ReturnObject
)
425 ACPI_OPERAND_OBJECT
*NewObject
;
430 * We can fix the following cases for an expected resource template:
431 * 1. No return value (interpreter slack mode is disabled)
432 * 2. A "Return (Zero)" statement
433 * 3. A "Return empty buffer" statement
435 * We will return a buffer containing a single EndTag
436 * resource descriptor.
440 switch (OriginalObject
->Common
.Type
)
442 case ACPI_TYPE_INTEGER
:
444 /* We can only repair an Integer==0 */
446 if (OriginalObject
->Integer
.Value
)
448 return (AE_AML_OPERAND_TYPE
);
452 case ACPI_TYPE_BUFFER
:
454 if (OriginalObject
->Buffer
.Length
)
456 /* Additional checks can be added in the future */
458 *ReturnObject
= NULL
;
463 case ACPI_TYPE_STRING
:
466 return (AE_AML_OPERAND_TYPE
);
470 /* Create the new buffer object for the resource descriptor */
472 NewObject
= AcpiUtCreateBufferObject (2);
475 return (AE_NO_MEMORY
);
478 Buffer
= ACPI_CAST_PTR (UINT8
, NewObject
->Buffer
.Pointer
);
480 /* Initialize the Buffer with a single EndTag descriptor */
482 Buffer
[0] = (ACPI_RESOURCE_NAME_END_TAG
| ASL_RDESC_END_TAG_SIZE
);
485 *ReturnObject
= NewObject
;
490 /*******************************************************************************
492 * FUNCTION: AcpiNsConvertToReference
494 * PARAMETERS: Scope - Namespace node for the method/object
495 * OriginalObject - Object to be converted
496 * ReturnObject - Where the new converted object is returned
498 * RETURN: Status. AE_OK if conversion was successful
500 * DESCRIPTION: Attempt to convert a Integer object to a ObjectReference.
503 ******************************************************************************/
506 AcpiNsConvertToReference (
507 ACPI_NAMESPACE_NODE
*Scope
,
508 ACPI_OPERAND_OBJECT
*OriginalObject
,
509 ACPI_OPERAND_OBJECT
**ReturnObject
)
511 ACPI_OPERAND_OBJECT
*NewObject
= NULL
;
513 ACPI_NAMESPACE_NODE
*Node
;
514 ACPI_GENERIC_STATE ScopeInfo
;
518 ACPI_FUNCTION_NAME (NsConvertToReference
);
521 /* Convert path into internal presentation */
523 Status
= AcpiNsInternalizeName (OriginalObject
->String
.Pointer
, &Name
);
524 if (ACPI_FAILURE (Status
))
526 return_ACPI_STATUS (Status
);
529 /* Find the namespace node */
531 ScopeInfo
.Scope
.Node
= ACPI_CAST_PTR (ACPI_NAMESPACE_NODE
, Scope
);
532 Status
= AcpiNsLookup (&ScopeInfo
, Name
,
533 ACPI_TYPE_ANY
, ACPI_IMODE_EXECUTE
,
534 ACPI_NS_SEARCH_PARENT
| ACPI_NS_DONT_OPEN_SCOPE
, NULL
, &Node
);
535 if (ACPI_FAILURE (Status
))
537 /* Check if we are resolving a named reference within a package */
539 ACPI_ERROR_NAMESPACE (&ScopeInfo
,
540 OriginalObject
->String
.Pointer
, Status
);
544 /* Create and init a new internal ACPI object */
546 NewObject
= AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE
);
549 Status
= AE_NO_MEMORY
;
552 NewObject
->Reference
.Node
= Node
;
553 NewObject
->Reference
.Object
= Node
->Object
;
554 NewObject
->Reference
.Class
= ACPI_REFCLASS_NAME
;
557 * Increase reference of the object if needed (the object is likely a
558 * null for device nodes).
560 AcpiUtAddReference (Node
->Object
);
564 *ReturnObject
= NewObject
;