acpica.library: Initial import of Intel ACPICA, v20131115
[AROS.git] / arch / all-pc / acpica / source / components / namespace / nsconvert.c
blob11aca36a5a6f9f8cb2d7b12d42a5f9b0a6fa3ebe
1 /******************************************************************************
3 * Module Name: nsconvert - Object conversions for objects returned by
4 * predefined methods
6 *****************************************************************************/
8 /*
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
14 * are met:
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.
31 * NO WARRANTY
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 #define __NSCONVERT_C__
47 #include "acpi.h"
48 #include "accommon.h"
49 #include "acnamesp.h"
50 #include "acinterp.h"
51 #include "acpredef.h"
52 #include "amlresrc.h"
54 #define _COMPONENT ACPI_NAMESPACE
55 ACPI_MODULE_NAME ("nsconvert")
58 /*******************************************************************************
60 * FUNCTION: AcpiNsConvertToInteger
62 * PARAMETERS: OriginalObject - Object to be converted
63 * ReturnObject - Where the new converted object is returned
65 * RETURN: Status. AE_OK if conversion was successful.
67 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
69 ******************************************************************************/
71 ACPI_STATUS
72 AcpiNsConvertToInteger (
73 ACPI_OPERAND_OBJECT *OriginalObject,
74 ACPI_OPERAND_OBJECT **ReturnObject)
76 ACPI_OPERAND_OBJECT *NewObject;
77 ACPI_STATUS Status;
78 UINT64 Value = 0;
79 UINT32 i;
82 switch (OriginalObject->Common.Type)
84 case ACPI_TYPE_STRING:
86 /* String-to-Integer conversion */
88 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
89 ACPI_ANY_BASE, &Value);
90 if (ACPI_FAILURE (Status))
92 return (Status);
94 break;
96 case ACPI_TYPE_BUFFER:
98 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
100 if (OriginalObject->Buffer.Length > 8)
102 return (AE_AML_OPERAND_TYPE);
105 /* Extract each buffer byte to create the integer */
107 for (i = 0; i < OriginalObject->Buffer.Length; i++)
109 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
111 break;
113 default:
115 return (AE_AML_OPERAND_TYPE);
118 NewObject = AcpiUtCreateIntegerObject (Value);
119 if (!NewObject)
121 return (AE_NO_MEMORY);
124 *ReturnObject = NewObject;
125 return (AE_OK);
129 /*******************************************************************************
131 * FUNCTION: AcpiNsConvertToString
133 * PARAMETERS: OriginalObject - Object to be converted
134 * ReturnObject - Where the new converted object is returned
136 * RETURN: Status. AE_OK if conversion was successful.
138 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
140 ******************************************************************************/
142 ACPI_STATUS
143 AcpiNsConvertToString (
144 ACPI_OPERAND_OBJECT *OriginalObject,
145 ACPI_OPERAND_OBJECT **ReturnObject)
147 ACPI_OPERAND_OBJECT *NewObject;
148 ACPI_SIZE Length;
149 ACPI_STATUS Status;
152 switch (OriginalObject->Common.Type)
154 case ACPI_TYPE_INTEGER:
156 * Integer-to-String conversion. Commonly, convert
157 * an integer of value 0 to a NULL string. The last element of
158 * _BIF and _BIX packages occasionally need this fix.
160 if (OriginalObject->Integer.Value == 0)
162 /* Allocate a new NULL string object */
164 NewObject = AcpiUtCreateStringObject (0);
165 if (!NewObject)
167 return (AE_NO_MEMORY);
170 else
172 Status = AcpiExConvertToString (OriginalObject, &NewObject,
173 ACPI_IMPLICIT_CONVERT_HEX);
174 if (ACPI_FAILURE (Status))
176 return (Status);
179 break;
181 case ACPI_TYPE_BUFFER:
183 * Buffer-to-String conversion. Use a ToString
184 * conversion, no transform performed on the buffer data. The best
185 * example of this is the _BIF method, where the string data from
186 * the battery is often (incorrectly) returned as buffer object(s).
188 Length = 0;
189 while ((Length < OriginalObject->Buffer.Length) &&
190 (OriginalObject->Buffer.Pointer[Length]))
192 Length++;
195 /* Allocate a new string object */
197 NewObject = AcpiUtCreateStringObject (Length);
198 if (!NewObject)
200 return (AE_NO_MEMORY);
204 * Copy the raw buffer data with no transform. String is already NULL
205 * terminated at Length+1.
207 ACPI_MEMCPY (NewObject->String.Pointer,
208 OriginalObject->Buffer.Pointer, Length);
209 break;
211 default:
213 return (AE_AML_OPERAND_TYPE);
216 *ReturnObject = NewObject;
217 return (AE_OK);
221 /*******************************************************************************
223 * FUNCTION: AcpiNsConvertToBuffer
225 * PARAMETERS: OriginalObject - Object to be converted
226 * ReturnObject - Where the new converted object is returned
228 * RETURN: Status. AE_OK if conversion was successful.
230 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
232 ******************************************************************************/
234 ACPI_STATUS
235 AcpiNsConvertToBuffer (
236 ACPI_OPERAND_OBJECT *OriginalObject,
237 ACPI_OPERAND_OBJECT **ReturnObject)
239 ACPI_OPERAND_OBJECT *NewObject;
240 ACPI_STATUS Status;
241 ACPI_OPERAND_OBJECT **Elements;
242 UINT32 *DwordBuffer;
243 UINT32 Count;
244 UINT32 i;
247 switch (OriginalObject->Common.Type)
249 case ACPI_TYPE_INTEGER:
251 * Integer-to-Buffer conversion.
252 * Convert the Integer to a packed-byte buffer. _MAT and other
253 * objects need this sometimes, if a read has been performed on a
254 * Field object that is less than or equal to the global integer
255 * size (32 or 64 bits).
257 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
258 if (ACPI_FAILURE (Status))
260 return (Status);
262 break;
264 case ACPI_TYPE_STRING:
266 /* String-to-Buffer conversion. Simple data copy */
268 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
269 if (!NewObject)
271 return (AE_NO_MEMORY);
274 ACPI_MEMCPY (NewObject->Buffer.Pointer,
275 OriginalObject->String.Pointer, OriginalObject->String.Length);
276 break;
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++)
292 if ((!*Elements) ||
293 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
295 return (AE_AML_OPERAND_TYPE);
297 Elements++;
300 /* Create the new buffer object to replace the Package */
302 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
303 if (!NewObject)
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;
316 DwordBuffer++;
317 Elements++;
319 break;
321 default:
323 return (AE_AML_OPERAND_TYPE);
326 *ReturnObject = NewObject;
327 return (AE_OK);
331 /*******************************************************************************
333 * FUNCTION: AcpiNsConvertToUnicode
335 * PARAMETERS: 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 ******************************************************************************/
344 ACPI_STATUS
345 AcpiNsConvertToUnicode (
346 ACPI_OPERAND_OBJECT *OriginalObject,
347 ACPI_OPERAND_OBJECT **ReturnObject)
349 ACPI_OPERAND_OBJECT *NewObject;
350 char *AsciiString;
351 UINT16 *UnicodeBuffer;
352 UINT32 UnicodeLength;
353 UINT32 i;
356 if (!OriginalObject)
358 return (AE_OK);
361 /* If a Buffer was returned, it must be at least two bytes long */
363 if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER)
365 if (OriginalObject->Buffer.Length < 2)
367 return (AE_AML_OPERAND_VALUE);
370 *ReturnObject = NULL;
371 return (AE_OK);
375 * The original object is an ASCII string. Convert this string to
376 * a unicode buffer.
378 AsciiString = OriginalObject->String.Pointer;
379 UnicodeLength = (OriginalObject->String.Length * 2) + 2;
381 /* Create a new buffer object for the Unicode data */
383 NewObject = AcpiUtCreateBufferObject (UnicodeLength);
384 if (!NewObject)
386 return (AE_NO_MEMORY);
389 UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer);
391 /* Convert ASCII to Unicode */
393 for (i = 0; i < OriginalObject->String.Length; i++)
395 UnicodeBuffer[i] = (UINT16) AsciiString[i];
398 *ReturnObject = NewObject;
399 return (AE_OK);
403 /*******************************************************************************
405 * FUNCTION: AcpiNsConvertToResource
407 * PARAMETERS: OriginalObject - Object to be converted
408 * ReturnObject - Where the new converted object is returned
410 * RETURN: Status. AE_OK if conversion was successful
412 * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
413 * Buffer.
415 ******************************************************************************/
417 ACPI_STATUS
418 AcpiNsConvertToResource (
419 ACPI_OPERAND_OBJECT *OriginalObject,
420 ACPI_OPERAND_OBJECT **ReturnObject)
422 ACPI_OPERAND_OBJECT *NewObject;
423 UINT8 *Buffer;
427 * We can fix the following cases for an expected resource template:
428 * 1. No return value (interpreter slack mode is disabled)
429 * 2. A "Return (Zero)" statement
430 * 3. A "Return empty buffer" statement
432 * We will return a buffer containing a single EndTag
433 * resource descriptor.
435 if (OriginalObject)
437 switch (OriginalObject->Common.Type)
439 case ACPI_TYPE_INTEGER:
441 /* We can only repair an Integer==0 */
443 if (OriginalObject->Integer.Value)
445 return (AE_AML_OPERAND_TYPE);
447 break;
449 case ACPI_TYPE_BUFFER:
451 if (OriginalObject->Buffer.Length)
453 /* Additional checks can be added in the future */
455 *ReturnObject = NULL;
456 return (AE_OK);
458 break;
460 case ACPI_TYPE_STRING:
461 default:
463 return (AE_AML_OPERAND_TYPE);
467 /* Create the new buffer object for the resource descriptor */
469 NewObject = AcpiUtCreateBufferObject (2);
470 if (!NewObject)
472 return (AE_NO_MEMORY);
475 Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer);
477 /* Initialize the Buffer with a single EndTag descriptor */
479 Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
480 Buffer[1] = 0x00;
482 *ReturnObject = NewObject;
483 return (AE_OK);