nsconvert.c revision 1.1 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: nsconvert - Object conversions for objects returned by
4 1.1 christos * predefined methods
5 1.1 christos *
6 1.1 christos *****************************************************************************/
7 1.1 christos
8 1.1 christos /*
9 1.1 christos * Copyright (C) 2000 - 2013, Intel Corp.
10 1.1 christos * All rights reserved.
11 1.1 christos *
12 1.1 christos * Redistribution and use in source and binary forms, with or without
13 1.1 christos * modification, are permitted provided that the following conditions
14 1.1 christos * are met:
15 1.1 christos * 1. Redistributions of source code must retain the above copyright
16 1.1 christos * notice, this list of conditions, and the following disclaimer,
17 1.1 christos * without modification.
18 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
20 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
21 1.1 christos * including a substantially similar Disclaimer requirement for further
22 1.1 christos * binary redistribution.
23 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
24 1.1 christos * of any contributors may be used to endorse or promote products derived
25 1.1 christos * from this software without specific prior written permission.
26 1.1 christos *
27 1.1 christos * Alternatively, this software may be distributed under the terms of the
28 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
29 1.1 christos * Software Foundation.
30 1.1 christos *
31 1.1 christos * NO WARRANTY
32 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
43 1.1 christos */
44 1.1 christos
45 1.1 christos #define __NSCONVERT_C__
46 1.1 christos
47 1.1 christos #include "acpi.h"
48 1.1 christos #include "accommon.h"
49 1.1 christos #include "acnamesp.h"
50 1.1 christos #include "acinterp.h"
51 1.1 christos #include "acpredef.h"
52 1.1 christos #include "amlresrc.h"
53 1.1 christos
54 1.1 christos #define _COMPONENT ACPI_NAMESPACE
55 1.1 christos ACPI_MODULE_NAME ("nsconvert")
56 1.1 christos
57 1.1 christos
58 1.1 christos /*******************************************************************************
59 1.1 christos *
60 1.1 christos * FUNCTION: AcpiNsConvertToInteger
61 1.1 christos *
62 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
63 1.1 christos * ReturnObject - Where the new converted object is returned
64 1.1 christos *
65 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
66 1.1 christos *
67 1.1 christos * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
68 1.1 christos *
69 1.1 christos ******************************************************************************/
70 1.1 christos
71 1.1 christos ACPI_STATUS
72 1.1 christos AcpiNsConvertToInteger (
73 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
74 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
75 1.1 christos {
76 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
77 1.1 christos ACPI_STATUS Status;
78 1.1 christos UINT64 Value = 0;
79 1.1 christos UINT32 i;
80 1.1 christos
81 1.1 christos
82 1.1 christos switch (OriginalObject->Common.Type)
83 1.1 christos {
84 1.1 christos case ACPI_TYPE_STRING:
85 1.1 christos
86 1.1 christos /* String-to-Integer conversion */
87 1.1 christos
88 1.1 christos Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
89 1.1 christos ACPI_ANY_BASE, &Value);
90 1.1 christos if (ACPI_FAILURE (Status))
91 1.1 christos {
92 1.1 christos return (Status);
93 1.1 christos }
94 1.1 christos break;
95 1.1 christos
96 1.1 christos case ACPI_TYPE_BUFFER:
97 1.1 christos
98 1.1 christos /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
99 1.1 christos
100 1.1 christos if (OriginalObject->Buffer.Length > 8)
101 1.1 christos {
102 1.1 christos return (AE_AML_OPERAND_TYPE);
103 1.1 christos }
104 1.1 christos
105 1.1 christos /* Extract each buffer byte to create the integer */
106 1.1 christos
107 1.1 christos for (i = 0; i < OriginalObject->Buffer.Length; i++)
108 1.1 christos {
109 1.1 christos Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
110 1.1 christos }
111 1.1 christos break;
112 1.1 christos
113 1.1 christos default:
114 1.1 christos
115 1.1 christos return (AE_AML_OPERAND_TYPE);
116 1.1 christos }
117 1.1 christos
118 1.1 christos NewObject = AcpiUtCreateIntegerObject (Value);
119 1.1 christos if (!NewObject)
120 1.1 christos {
121 1.1 christos return (AE_NO_MEMORY);
122 1.1 christos }
123 1.1 christos
124 1.1 christos *ReturnObject = NewObject;
125 1.1 christos return (AE_OK);
126 1.1 christos }
127 1.1 christos
128 1.1 christos
129 1.1 christos /*******************************************************************************
130 1.1 christos *
131 1.1 christos * FUNCTION: AcpiNsConvertToString
132 1.1 christos *
133 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
134 1.1 christos * ReturnObject - Where the new converted object is returned
135 1.1 christos *
136 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
137 1.1 christos *
138 1.1 christos * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
139 1.1 christos *
140 1.1 christos ******************************************************************************/
141 1.1 christos
142 1.1 christos ACPI_STATUS
143 1.1 christos AcpiNsConvertToString (
144 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
145 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
146 1.1 christos {
147 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
148 1.1 christos ACPI_SIZE Length;
149 1.1 christos ACPI_STATUS Status;
150 1.1 christos
151 1.1 christos
152 1.1 christos switch (OriginalObject->Common.Type)
153 1.1 christos {
154 1.1 christos case ACPI_TYPE_INTEGER:
155 1.1 christos /*
156 1.1 christos * Integer-to-String conversion. Commonly, convert
157 1.1 christos * an integer of value 0 to a NULL string. The last element of
158 1.1 christos * _BIF and _BIX packages occasionally need this fix.
159 1.1 christos */
160 1.1 christos if (OriginalObject->Integer.Value == 0)
161 1.1 christos {
162 1.1 christos /* Allocate a new NULL string object */
163 1.1 christos
164 1.1 christos NewObject = AcpiUtCreateStringObject (0);
165 1.1 christos if (!NewObject)
166 1.1 christos {
167 1.1 christos return (AE_NO_MEMORY);
168 1.1 christos }
169 1.1 christos }
170 1.1 christos else
171 1.1 christos {
172 1.1 christos Status = AcpiExConvertToString (OriginalObject, &NewObject,
173 1.1 christos ACPI_IMPLICIT_CONVERT_HEX);
174 1.1 christos if (ACPI_FAILURE (Status))
175 1.1 christos {
176 1.1 christos return (Status);
177 1.1 christos }
178 1.1 christos }
179 1.1 christos break;
180 1.1 christos
181 1.1 christos case ACPI_TYPE_BUFFER:
182 1.1 christos /*
183 1.1 christos * Buffer-to-String conversion. Use a ToString
184 1.1 christos * conversion, no transform performed on the buffer data. The best
185 1.1 christos * example of this is the _BIF method, where the string data from
186 1.1 christos * the battery is often (incorrectly) returned as buffer object(s).
187 1.1 christos */
188 1.1 christos Length = 0;
189 1.1 christos while ((Length < OriginalObject->Buffer.Length) &&
190 1.1 christos (OriginalObject->Buffer.Pointer[Length]))
191 1.1 christos {
192 1.1 christos Length++;
193 1.1 christos }
194 1.1 christos
195 1.1 christos /* Allocate a new string object */
196 1.1 christos
197 1.1 christos NewObject = AcpiUtCreateStringObject (Length);
198 1.1 christos if (!NewObject)
199 1.1 christos {
200 1.1 christos return (AE_NO_MEMORY);
201 1.1 christos }
202 1.1 christos
203 1.1 christos /*
204 1.1 christos * Copy the raw buffer data with no transform. String is already NULL
205 1.1 christos * terminated at Length+1.
206 1.1 christos */
207 1.1 christos ACPI_MEMCPY (NewObject->String.Pointer,
208 1.1 christos OriginalObject->Buffer.Pointer, Length);
209 1.1 christos break;
210 1.1 christos
211 1.1 christos default:
212 1.1 christos
213 1.1 christos return (AE_AML_OPERAND_TYPE);
214 1.1 christos }
215 1.1 christos
216 1.1 christos *ReturnObject = NewObject;
217 1.1 christos return (AE_OK);
218 1.1 christos }
219 1.1 christos
220 1.1 christos
221 1.1 christos /*******************************************************************************
222 1.1 christos *
223 1.1 christos * FUNCTION: AcpiNsConvertToBuffer
224 1.1 christos *
225 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
226 1.1 christos * ReturnObject - Where the new converted object is returned
227 1.1 christos *
228 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
229 1.1 christos *
230 1.1 christos * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
231 1.1 christos *
232 1.1 christos ******************************************************************************/
233 1.1 christos
234 1.1 christos ACPI_STATUS
235 1.1 christos AcpiNsConvertToBuffer (
236 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
237 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
238 1.1 christos {
239 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
240 1.1 christos ACPI_STATUS Status;
241 1.1 christos ACPI_OPERAND_OBJECT **Elements;
242 1.1 christos UINT32 *DwordBuffer;
243 1.1 christos UINT32 Count;
244 1.1 christos UINT32 i;
245 1.1 christos
246 1.1 christos
247 1.1 christos switch (OriginalObject->Common.Type)
248 1.1 christos {
249 1.1 christos case ACPI_TYPE_INTEGER:
250 1.1 christos /*
251 1.1 christos * Integer-to-Buffer conversion.
252 1.1 christos * Convert the Integer to a packed-byte buffer. _MAT and other
253 1.1 christos * objects need this sometimes, if a read has been performed on a
254 1.1 christos * Field object that is less than or equal to the global integer
255 1.1 christos * size (32 or 64 bits).
256 1.1 christos */
257 1.1 christos Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
258 1.1 christos if (ACPI_FAILURE (Status))
259 1.1 christos {
260 1.1 christos return (Status);
261 1.1 christos }
262 1.1 christos break;
263 1.1 christos
264 1.1 christos case ACPI_TYPE_STRING:
265 1.1 christos
266 1.1 christos /* String-to-Buffer conversion. Simple data copy */
267 1.1 christos
268 1.1 christos NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
269 1.1 christos if (!NewObject)
270 1.1 christos {
271 1.1 christos return (AE_NO_MEMORY);
272 1.1 christos }
273 1.1 christos
274 1.1 christos ACPI_MEMCPY (NewObject->Buffer.Pointer,
275 1.1 christos OriginalObject->String.Pointer, OriginalObject->String.Length);
276 1.1 christos break;
277 1.1 christos
278 1.1 christos case ACPI_TYPE_PACKAGE:
279 1.1 christos /*
280 1.1 christos * This case is often seen for predefined names that must return a
281 1.1 christos * Buffer object with multiple DWORD integers within. For example,
282 1.1 christos * _FDE and _GTM. The Package can be converted to a Buffer.
283 1.1 christos */
284 1.1 christos
285 1.1 christos /* All elements of the Package must be integers */
286 1.1 christos
287 1.1 christos Elements = OriginalObject->Package.Elements;
288 1.1 christos Count = OriginalObject->Package.Count;
289 1.1 christos
290 1.1 christos for (i = 0; i < Count; i++)
291 1.1 christos {
292 1.1 christos if ((!*Elements) ||
293 1.1 christos ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
294 1.1 christos {
295 1.1 christos return (AE_AML_OPERAND_TYPE);
296 1.1 christos }
297 1.1 christos Elements++;
298 1.1 christos }
299 1.1 christos
300 1.1 christos /* Create the new buffer object to replace the Package */
301 1.1 christos
302 1.1 christos NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
303 1.1 christos if (!NewObject)
304 1.1 christos {
305 1.1 christos return (AE_NO_MEMORY);
306 1.1 christos }
307 1.1 christos
308 1.1 christos /* Copy the package elements (integers) to the buffer as DWORDs */
309 1.1 christos
310 1.1 christos Elements = OriginalObject->Package.Elements;
311 1.1 christos DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
312 1.1 christos
313 1.1 christos for (i = 0; i < Count; i++)
314 1.1 christos {
315 1.1 christos *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
316 1.1 christos DwordBuffer++;
317 1.1 christos Elements++;
318 1.1 christos }
319 1.1 christos break;
320 1.1 christos
321 1.1 christos default:
322 1.1 christos
323 1.1 christos return (AE_AML_OPERAND_TYPE);
324 1.1 christos }
325 1.1 christos
326 1.1 christos *ReturnObject = NewObject;
327 1.1 christos return (AE_OK);
328 1.1 christos }
329 1.1 christos
330 1.1 christos
331 1.1 christos /*******************************************************************************
332 1.1 christos *
333 1.1 christos * FUNCTION: AcpiNsConvertToUnicode
334 1.1 christos *
335 1.1 christos * PARAMETERS: OriginalObject - ASCII String Object to be converted
336 1.1 christos * ReturnObject - Where the new converted object is returned
337 1.1 christos *
338 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
339 1.1 christos *
340 1.1 christos * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
341 1.1 christos *
342 1.1 christos ******************************************************************************/
343 1.1 christos
344 1.1 christos ACPI_STATUS
345 1.1 christos AcpiNsConvertToUnicode (
346 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
347 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
348 1.1 christos {
349 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
350 1.1 christos char *AsciiString;
351 1.1 christos UINT16 *UnicodeBuffer;
352 1.1 christos UINT32 UnicodeLength;
353 1.1 christos UINT32 i;
354 1.1 christos
355 1.1 christos
356 1.1 christos if (!OriginalObject)
357 1.1 christos {
358 1.1 christos return (AE_OK);
359 1.1 christos }
360 1.1 christos
361 1.1 christos /* If a Buffer was returned, it must be at least two bytes long */
362 1.1 christos
363 1.1 christos if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER)
364 1.1 christos {
365 1.1 christos if (OriginalObject->Buffer.Length < 2)
366 1.1 christos {
367 1.1 christos return (AE_AML_OPERAND_VALUE);
368 1.1 christos }
369 1.1 christos
370 1.1 christos *ReturnObject = NULL;
371 1.1 christos return (AE_OK);
372 1.1 christos }
373 1.1 christos
374 1.1 christos /*
375 1.1 christos * The original object is an ASCII string. Convert this string to
376 1.1 christos * a unicode buffer.
377 1.1 christos */
378 1.1 christos AsciiString = OriginalObject->String.Pointer;
379 1.1 christos UnicodeLength = (OriginalObject->String.Length * 2) + 2;
380 1.1 christos
381 1.1 christos /* Create a new buffer object for the Unicode data */
382 1.1 christos
383 1.1 christos NewObject = AcpiUtCreateBufferObject (UnicodeLength);
384 1.1 christos if (!NewObject)
385 1.1 christos {
386 1.1 christos return (AE_NO_MEMORY);
387 1.1 christos }
388 1.1 christos
389 1.1 christos UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer);
390 1.1 christos
391 1.1 christos /* Convert ASCII to Unicode */
392 1.1 christos
393 1.1 christos for (i = 0; i < OriginalObject->String.Length; i++)
394 1.1 christos {
395 1.1 christos UnicodeBuffer[i] = (UINT16) AsciiString[i];
396 1.1 christos }
397 1.1 christos
398 1.1 christos *ReturnObject = NewObject;
399 1.1 christos return (AE_OK);
400 1.1 christos }
401 1.1 christos
402 1.1 christos
403 1.1 christos /*******************************************************************************
404 1.1 christos *
405 1.1 christos * FUNCTION: AcpiNsConvertToResource
406 1.1 christos *
407 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
408 1.1 christos * ReturnObject - Where the new converted object is returned
409 1.1 christos *
410 1.1 christos * RETURN: Status. AE_OK if conversion was successful
411 1.1 christos *
412 1.1 christos * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
413 1.1 christos * Buffer.
414 1.1 christos *
415 1.1 christos ******************************************************************************/
416 1.1 christos
417 1.1 christos ACPI_STATUS
418 1.1 christos AcpiNsConvertToResource (
419 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
420 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
421 1.1 christos {
422 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
423 1.1 christos UINT8 *Buffer;
424 1.1 christos
425 1.1 christos
426 1.1 christos /*
427 1.1 christos * We can fix the following cases for an expected resource template:
428 1.1 christos * 1. No return value (interpreter slack mode is disabled)
429 1.1 christos * 2. A "Return (Zero)" statement
430 1.1 christos * 3. A "Return empty buffer" statement
431 1.1 christos *
432 1.1 christos * We will return a buffer containing a single EndTag
433 1.1 christos * resource descriptor.
434 1.1 christos */
435 1.1 christos if (OriginalObject)
436 1.1 christos {
437 1.1 christos switch (OriginalObject->Common.Type)
438 1.1 christos {
439 1.1 christos case ACPI_TYPE_INTEGER:
440 1.1 christos
441 1.1 christos /* We can only repair an Integer==0 */
442 1.1 christos
443 1.1 christos if (OriginalObject->Integer.Value)
444 1.1 christos {
445 1.1 christos return (AE_AML_OPERAND_TYPE);
446 1.1 christos }
447 1.1 christos break;
448 1.1 christos
449 1.1 christos case ACPI_TYPE_BUFFER:
450 1.1 christos
451 1.1 christos if (OriginalObject->Buffer.Length)
452 1.1 christos {
453 1.1 christos /* Additional checks can be added in the future */
454 1.1 christos
455 1.1 christos *ReturnObject = NULL;
456 1.1 christos return (AE_OK);
457 1.1 christos }
458 1.1 christos break;
459 1.1 christos
460 1.1 christos case ACPI_TYPE_STRING:
461 1.1 christos default:
462 1.1 christos
463 1.1 christos return (AE_AML_OPERAND_TYPE);
464 1.1 christos }
465 1.1 christos }
466 1.1 christos
467 1.1 christos /* Create the new buffer object for the resource descriptor */
468 1.1 christos
469 1.1 christos NewObject = AcpiUtCreateBufferObject (2);
470 1.1 christos if (!NewObject)
471 1.1 christos {
472 1.1 christos return (AE_NO_MEMORY);
473 1.1 christos }
474 1.1 christos
475 1.1 christos Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer);
476 1.1 christos
477 1.1 christos /* Initialize the Buffer with a single EndTag descriptor */
478 1.1 christos
479 1.1 christos Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
480 1.1 christos Buffer[1] = 0x00;
481 1.1 christos
482 1.1 christos *ReturnObject = NewObject;
483 1.1 christos return (AE_OK);
484 1.1 christos }
485