1 /******************************************************************************
3 * Module Name: nsconvert - Object conversions for objects returned by
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2016, 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
,
87 ACPI_ANY_BASE
, AcpiGbl_IntegerByteWidth
, &Value
);
88 if (ACPI_FAILURE (Status
))
94 case ACPI_TYPE_BUFFER
:
96 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
98 if (OriginalObject
->Buffer
.Length
> 8)
100 return (AE_AML_OPERAND_TYPE
);
103 /* Extract each buffer byte to create the integer */
105 for (i
= 0; i
< OriginalObject
->Buffer
.Length
; i
++)
108 OriginalObject
->Buffer
.Pointer
[i
] << (i
* 8));
114 return (AE_AML_OPERAND_TYPE
);
117 NewObject
= AcpiUtCreateIntegerObject (Value
);
120 return (AE_NO_MEMORY
);
123 *ReturnObject
= NewObject
;
128 /*******************************************************************************
130 * FUNCTION: AcpiNsConvertToString
132 * PARAMETERS: OriginalObject - Object to be converted
133 * ReturnObject - Where the new converted object is returned
135 * RETURN: Status. AE_OK if conversion was successful.
137 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
139 ******************************************************************************/
142 AcpiNsConvertToString (
143 ACPI_OPERAND_OBJECT
*OriginalObject
,
144 ACPI_OPERAND_OBJECT
**ReturnObject
)
146 ACPI_OPERAND_OBJECT
*NewObject
;
151 switch (OriginalObject
->Common
.Type
)
153 case ACPI_TYPE_INTEGER
:
155 * Integer-to-String conversion. Commonly, convert
156 * an integer of value 0 to a NULL string. The last element of
157 * _BIF and _BIX packages occasionally need this fix.
159 if (OriginalObject
->Integer
.Value
== 0)
161 /* Allocate a new NULL string object */
163 NewObject
= AcpiUtCreateStringObject (0);
166 return (AE_NO_MEMORY
);
171 Status
= AcpiExConvertToString (OriginalObject
,
172 &NewObject
, ACPI_IMPLICIT_CONVERT_HEX
);
173 if (ACPI_FAILURE (Status
))
180 case ACPI_TYPE_BUFFER
:
182 * Buffer-to-String conversion. Use a ToString
183 * conversion, no transform performed on the buffer data. The best
184 * example of this is the _BIF method, where the string data from
185 * the battery is often (incorrectly) returned as buffer object(s).
188 while ((Length
< OriginalObject
->Buffer
.Length
) &&
189 (OriginalObject
->Buffer
.Pointer
[Length
]))
194 /* Allocate a new string object */
196 NewObject
= AcpiUtCreateStringObject (Length
);
199 return (AE_NO_MEMORY
);
203 * Copy the raw buffer data with no transform. String is already NULL
204 * terminated at Length+1.
206 memcpy (NewObject
->String
.Pointer
,
207 OriginalObject
->Buffer
.Pointer
, Length
);
212 return (AE_AML_OPERAND_TYPE
);
215 *ReturnObject
= NewObject
;
220 /*******************************************************************************
222 * FUNCTION: AcpiNsConvertToBuffer
224 * PARAMETERS: OriginalObject - Object to be converted
225 * ReturnObject - Where the new converted object is returned
227 * RETURN: Status. AE_OK if conversion was successful.
229 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
231 ******************************************************************************/
234 AcpiNsConvertToBuffer (
235 ACPI_OPERAND_OBJECT
*OriginalObject
,
236 ACPI_OPERAND_OBJECT
**ReturnObject
)
238 ACPI_OPERAND_OBJECT
*NewObject
;
240 ACPI_OPERAND_OBJECT
**Elements
;
246 switch (OriginalObject
->Common
.Type
)
248 case ACPI_TYPE_INTEGER
:
250 * Integer-to-Buffer conversion.
251 * Convert the Integer to a packed-byte buffer. _MAT and other
252 * objects need this sometimes, if a read has been performed on a
253 * Field object that is less than or equal to the global integer
254 * size (32 or 64 bits).
256 Status
= AcpiExConvertToBuffer (OriginalObject
, &NewObject
);
257 if (ACPI_FAILURE (Status
))
263 case ACPI_TYPE_STRING
:
265 /* String-to-Buffer conversion. Simple data copy */
267 NewObject
= AcpiUtCreateBufferObject
268 (OriginalObject
->String
.Length
);
271 return (AE_NO_MEMORY
);
274 memcpy (NewObject
->Buffer
.Pointer
,
275 OriginalObject
->String
.Pointer
, OriginalObject
->String
.Length
);
278 case ACPI_TYPE_PACKAGE
:
280 * This case is often seen for predefined names that must return a
281 * Buffer object with multiple DWORD integers within. For example,
282 * _FDE and _GTM. The Package can be converted to a Buffer.
285 /* All elements of the Package must be integers */
287 Elements
= OriginalObject
->Package
.Elements
;
288 Count
= OriginalObject
->Package
.Count
;
290 for (i
= 0; i
< Count
; i
++)
293 ((*Elements
)->Common
.Type
!= ACPI_TYPE_INTEGER
))
295 return (AE_AML_OPERAND_TYPE
);
300 /* Create the new buffer object to replace the Package */
302 NewObject
= AcpiUtCreateBufferObject (ACPI_MUL_4 (Count
));
305 return (AE_NO_MEMORY
);
308 /* Copy the package elements (integers) to the buffer as DWORDs */
310 Elements
= OriginalObject
->Package
.Elements
;
311 DwordBuffer
= ACPI_CAST_PTR (UINT32
, NewObject
->Buffer
.Pointer
);
313 for (i
= 0; i
< Count
; i
++)
315 *DwordBuffer
= (UINT32
) (*Elements
)->Integer
.Value
;
323 return (AE_AML_OPERAND_TYPE
);
326 *ReturnObject
= NewObject
;
331 /*******************************************************************************
333 * FUNCTION: AcpiNsConvertToUnicode
335 * PARAMETERS: Scope - Namespace node for the method/object
336 * OriginalObject - ASCII String Object to be converted
337 * ReturnObject - Where the new converted object is returned
339 * RETURN: Status. AE_OK if conversion was successful.
341 * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
343 ******************************************************************************/
346 AcpiNsConvertToUnicode (
347 ACPI_NAMESPACE_NODE
*Scope
,
348 ACPI_OPERAND_OBJECT
*OriginalObject
,
349 ACPI_OPERAND_OBJECT
**ReturnObject
)
351 ACPI_OPERAND_OBJECT
*NewObject
;
353 UINT16
*UnicodeBuffer
;
354 UINT32 UnicodeLength
;
363 /* If a Buffer was returned, it must be at least two bytes long */
365 if (OriginalObject
->Common
.Type
== ACPI_TYPE_BUFFER
)
367 if (OriginalObject
->Buffer
.Length
< 2)
369 return (AE_AML_OPERAND_VALUE
);
372 *ReturnObject
= NULL
;
377 * The original object is an ASCII string. Convert this string to
380 AsciiString
= OriginalObject
->String
.Pointer
;
381 UnicodeLength
= (OriginalObject
->String
.Length
* 2) + 2;
383 /* Create a new buffer object for the Unicode data */
385 NewObject
= AcpiUtCreateBufferObject (UnicodeLength
);
388 return (AE_NO_MEMORY
);
391 UnicodeBuffer
= ACPI_CAST_PTR (UINT16
, NewObject
->Buffer
.Pointer
);
393 /* Convert ASCII to Unicode */
395 for (i
= 0; i
< OriginalObject
->String
.Length
; i
++)
397 UnicodeBuffer
[i
] = (UINT16
) AsciiString
[i
];
400 *ReturnObject
= NewObject
;
405 /*******************************************************************************
407 * FUNCTION: AcpiNsConvertToResource
409 * PARAMETERS: Scope - Namespace node for the method/object
410 * OriginalObject - Object to be converted
411 * ReturnObject - Where the new converted object is returned
413 * RETURN: Status. AE_OK if conversion was successful
415 * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
418 ******************************************************************************/
421 AcpiNsConvertToResource (
422 ACPI_NAMESPACE_NODE
*Scope
,
423 ACPI_OPERAND_OBJECT
*OriginalObject
,
424 ACPI_OPERAND_OBJECT
**ReturnObject
)
426 ACPI_OPERAND_OBJECT
*NewObject
;
431 * We can fix the following cases for an expected resource template:
432 * 1. No return value (interpreter slack mode is disabled)
433 * 2. A "Return (Zero)" statement
434 * 3. A "Return empty buffer" statement
436 * We will return a buffer containing a single EndTag
437 * resource descriptor.
441 switch (OriginalObject
->Common
.Type
)
443 case ACPI_TYPE_INTEGER
:
445 /* We can only repair an Integer==0 */
447 if (OriginalObject
->Integer
.Value
)
449 return (AE_AML_OPERAND_TYPE
);
453 case ACPI_TYPE_BUFFER
:
455 if (OriginalObject
->Buffer
.Length
)
457 /* Additional checks can be added in the future */
459 *ReturnObject
= NULL
;
464 case ACPI_TYPE_STRING
:
467 return (AE_AML_OPERAND_TYPE
);
471 /* Create the new buffer object for the resource descriptor */
473 NewObject
= AcpiUtCreateBufferObject (2);
476 return (AE_NO_MEMORY
);
479 Buffer
= ACPI_CAST_PTR (UINT8
, NewObject
->Buffer
.Pointer
);
481 /* Initialize the Buffer with a single EndTag descriptor */
483 Buffer
[0] = (ACPI_RESOURCE_NAME_END_TAG
| ASL_RDESC_END_TAG_SIZE
);
486 *ReturnObject
= NewObject
;
491 /*******************************************************************************
493 * FUNCTION: AcpiNsConvertToReference
495 * PARAMETERS: Scope - Namespace node for the method/object
496 * OriginalObject - Object to be converted
497 * ReturnObject - Where the new converted object is returned
499 * RETURN: Status. AE_OK if conversion was successful
501 * DESCRIPTION: Attempt to convert a Integer object to a ObjectReference.
504 ******************************************************************************/
507 AcpiNsConvertToReference (
508 ACPI_NAMESPACE_NODE
*Scope
,
509 ACPI_OPERAND_OBJECT
*OriginalObject
,
510 ACPI_OPERAND_OBJECT
**ReturnObject
)
512 ACPI_OPERAND_OBJECT
*NewObject
= NULL
;
514 ACPI_NAMESPACE_NODE
*Node
;
515 ACPI_GENERIC_STATE ScopeInfo
;
519 ACPI_FUNCTION_NAME (NsConvertToReference
);
522 /* Convert path into internal presentation */
524 Status
= AcpiNsInternalizeName (OriginalObject
->String
.Pointer
, &Name
);
525 if (ACPI_FAILURE (Status
))
527 return_ACPI_STATUS (Status
);
530 /* Find the namespace node */
532 ScopeInfo
.Scope
.Node
= ACPI_CAST_PTR (ACPI_NAMESPACE_NODE
, Scope
);
533 Status
= AcpiNsLookup (&ScopeInfo
, Name
,
534 ACPI_TYPE_ANY
, ACPI_IMODE_EXECUTE
,
535 ACPI_NS_SEARCH_PARENT
| ACPI_NS_DONT_OPEN_SCOPE
, NULL
, &Node
);
536 if (ACPI_FAILURE (Status
))
538 /* Check if we are resolving a named reference within a package */
540 ACPI_ERROR_NAMESPACE (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
;