nsconvert.c revision 1.1.1.3 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.1.3 christos * Copyright (C) 2000 - 2015, 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 #include "acpi.h"
46 1.1 christos #include "accommon.h"
47 1.1 christos #include "acnamesp.h"
48 1.1 christos #include "acinterp.h"
49 1.1 christos #include "acpredef.h"
50 1.1 christos #include "amlresrc.h"
51 1.1 christos
52 1.1 christos #define _COMPONENT ACPI_NAMESPACE
53 1.1 christos ACPI_MODULE_NAME ("nsconvert")
54 1.1 christos
55 1.1 christos
56 1.1 christos /*******************************************************************************
57 1.1 christos *
58 1.1 christos * FUNCTION: AcpiNsConvertToInteger
59 1.1 christos *
60 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
61 1.1 christos * ReturnObject - Where the new converted object is returned
62 1.1 christos *
63 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
64 1.1 christos *
65 1.1 christos * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
66 1.1 christos *
67 1.1 christos ******************************************************************************/
68 1.1 christos
69 1.1 christos ACPI_STATUS
70 1.1 christos AcpiNsConvertToInteger (
71 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
72 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
73 1.1 christos {
74 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
75 1.1 christos ACPI_STATUS Status;
76 1.1 christos UINT64 Value = 0;
77 1.1 christos UINT32 i;
78 1.1 christos
79 1.1 christos
80 1.1 christos switch (OriginalObject->Common.Type)
81 1.1 christos {
82 1.1 christos case ACPI_TYPE_STRING:
83 1.1 christos
84 1.1 christos /* String-to-Integer conversion */
85 1.1 christos
86 1.1 christos Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
87 1.1 christos ACPI_ANY_BASE, &Value);
88 1.1 christos if (ACPI_FAILURE (Status))
89 1.1 christos {
90 1.1 christos return (Status);
91 1.1 christos }
92 1.1 christos break;
93 1.1 christos
94 1.1 christos case ACPI_TYPE_BUFFER:
95 1.1 christos
96 1.1 christos /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
97 1.1 christos
98 1.1 christos if (OriginalObject->Buffer.Length > 8)
99 1.1 christos {
100 1.1 christos return (AE_AML_OPERAND_TYPE);
101 1.1 christos }
102 1.1 christos
103 1.1 christos /* Extract each buffer byte to create the integer */
104 1.1 christos
105 1.1 christos for (i = 0; i < OriginalObject->Buffer.Length; i++)
106 1.1 christos {
107 1.1 christos Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
108 1.1 christos }
109 1.1 christos break;
110 1.1 christos
111 1.1 christos default:
112 1.1 christos
113 1.1 christos return (AE_AML_OPERAND_TYPE);
114 1.1 christos }
115 1.1 christos
116 1.1 christos NewObject = AcpiUtCreateIntegerObject (Value);
117 1.1 christos if (!NewObject)
118 1.1 christos {
119 1.1 christos return (AE_NO_MEMORY);
120 1.1 christos }
121 1.1 christos
122 1.1 christos *ReturnObject = NewObject;
123 1.1 christos return (AE_OK);
124 1.1 christos }
125 1.1 christos
126 1.1 christos
127 1.1 christos /*******************************************************************************
128 1.1 christos *
129 1.1 christos * FUNCTION: AcpiNsConvertToString
130 1.1 christos *
131 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
132 1.1 christos * ReturnObject - Where the new converted object is returned
133 1.1 christos *
134 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
135 1.1 christos *
136 1.1 christos * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
137 1.1 christos *
138 1.1 christos ******************************************************************************/
139 1.1 christos
140 1.1 christos ACPI_STATUS
141 1.1 christos AcpiNsConvertToString (
142 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
143 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
144 1.1 christos {
145 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
146 1.1 christos ACPI_SIZE Length;
147 1.1 christos ACPI_STATUS Status;
148 1.1 christos
149 1.1 christos
150 1.1 christos switch (OriginalObject->Common.Type)
151 1.1 christos {
152 1.1 christos case ACPI_TYPE_INTEGER:
153 1.1 christos /*
154 1.1 christos * Integer-to-String conversion. Commonly, convert
155 1.1 christos * an integer of value 0 to a NULL string. The last element of
156 1.1 christos * _BIF and _BIX packages occasionally need this fix.
157 1.1 christos */
158 1.1 christos if (OriginalObject->Integer.Value == 0)
159 1.1 christos {
160 1.1 christos /* Allocate a new NULL string object */
161 1.1 christos
162 1.1 christos NewObject = AcpiUtCreateStringObject (0);
163 1.1 christos if (!NewObject)
164 1.1 christos {
165 1.1 christos return (AE_NO_MEMORY);
166 1.1 christos }
167 1.1 christos }
168 1.1 christos else
169 1.1 christos {
170 1.1 christos Status = AcpiExConvertToString (OriginalObject, &NewObject,
171 1.1 christos ACPI_IMPLICIT_CONVERT_HEX);
172 1.1 christos if (ACPI_FAILURE (Status))
173 1.1 christos {
174 1.1 christos return (Status);
175 1.1 christos }
176 1.1 christos }
177 1.1 christos break;
178 1.1 christos
179 1.1 christos case ACPI_TYPE_BUFFER:
180 1.1 christos /*
181 1.1 christos * Buffer-to-String conversion. Use a ToString
182 1.1 christos * conversion, no transform performed on the buffer data. The best
183 1.1 christos * example of this is the _BIF method, where the string data from
184 1.1 christos * the battery is often (incorrectly) returned as buffer object(s).
185 1.1 christos */
186 1.1 christos Length = 0;
187 1.1 christos while ((Length < OriginalObject->Buffer.Length) &&
188 1.1 christos (OriginalObject->Buffer.Pointer[Length]))
189 1.1 christos {
190 1.1 christos Length++;
191 1.1 christos }
192 1.1 christos
193 1.1 christos /* Allocate a new string object */
194 1.1 christos
195 1.1 christos NewObject = AcpiUtCreateStringObject (Length);
196 1.1 christos if (!NewObject)
197 1.1 christos {
198 1.1 christos return (AE_NO_MEMORY);
199 1.1 christos }
200 1.1 christos
201 1.1 christos /*
202 1.1 christos * Copy the raw buffer data with no transform. String is already NULL
203 1.1 christos * terminated at Length+1.
204 1.1 christos */
205 1.1 christos ACPI_MEMCPY (NewObject->String.Pointer,
206 1.1 christos OriginalObject->Buffer.Pointer, Length);
207 1.1 christos break;
208 1.1 christos
209 1.1 christos default:
210 1.1 christos
211 1.1 christos return (AE_AML_OPERAND_TYPE);
212 1.1 christos }
213 1.1 christos
214 1.1 christos *ReturnObject = NewObject;
215 1.1 christos return (AE_OK);
216 1.1 christos }
217 1.1 christos
218 1.1 christos
219 1.1 christos /*******************************************************************************
220 1.1 christos *
221 1.1 christos * FUNCTION: AcpiNsConvertToBuffer
222 1.1 christos *
223 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
224 1.1 christos * ReturnObject - Where the new converted object is returned
225 1.1 christos *
226 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
227 1.1 christos *
228 1.1 christos * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
229 1.1 christos *
230 1.1 christos ******************************************************************************/
231 1.1 christos
232 1.1 christos ACPI_STATUS
233 1.1 christos AcpiNsConvertToBuffer (
234 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
235 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
236 1.1 christos {
237 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
238 1.1 christos ACPI_STATUS Status;
239 1.1 christos ACPI_OPERAND_OBJECT **Elements;
240 1.1 christos UINT32 *DwordBuffer;
241 1.1 christos UINT32 Count;
242 1.1 christos UINT32 i;
243 1.1 christos
244 1.1 christos
245 1.1 christos switch (OriginalObject->Common.Type)
246 1.1 christos {
247 1.1 christos case ACPI_TYPE_INTEGER:
248 1.1 christos /*
249 1.1 christos * Integer-to-Buffer conversion.
250 1.1 christos * Convert the Integer to a packed-byte buffer. _MAT and other
251 1.1 christos * objects need this sometimes, if a read has been performed on a
252 1.1 christos * Field object that is less than or equal to the global integer
253 1.1 christos * size (32 or 64 bits).
254 1.1 christos */
255 1.1 christos Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
256 1.1 christos if (ACPI_FAILURE (Status))
257 1.1 christos {
258 1.1 christos return (Status);
259 1.1 christos }
260 1.1 christos break;
261 1.1 christos
262 1.1 christos case ACPI_TYPE_STRING:
263 1.1 christos
264 1.1 christos /* String-to-Buffer conversion. Simple data copy */
265 1.1 christos
266 1.1 christos NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
267 1.1 christos if (!NewObject)
268 1.1 christos {
269 1.1 christos return (AE_NO_MEMORY);
270 1.1 christos }
271 1.1 christos
272 1.1 christos ACPI_MEMCPY (NewObject->Buffer.Pointer,
273 1.1 christos OriginalObject->String.Pointer, OriginalObject->String.Length);
274 1.1 christos break;
275 1.1 christos
276 1.1 christos case ACPI_TYPE_PACKAGE:
277 1.1 christos /*
278 1.1 christos * This case is often seen for predefined names that must return a
279 1.1 christos * Buffer object with multiple DWORD integers within. For example,
280 1.1 christos * _FDE and _GTM. The Package can be converted to a Buffer.
281 1.1 christos */
282 1.1 christos
283 1.1 christos /* All elements of the Package must be integers */
284 1.1 christos
285 1.1 christos Elements = OriginalObject->Package.Elements;
286 1.1 christos Count = OriginalObject->Package.Count;
287 1.1 christos
288 1.1 christos for (i = 0; i < Count; i++)
289 1.1 christos {
290 1.1 christos if ((!*Elements) ||
291 1.1 christos ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
292 1.1 christos {
293 1.1 christos return (AE_AML_OPERAND_TYPE);
294 1.1 christos }
295 1.1 christos Elements++;
296 1.1 christos }
297 1.1 christos
298 1.1 christos /* Create the new buffer object to replace the Package */
299 1.1 christos
300 1.1 christos NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
301 1.1 christos if (!NewObject)
302 1.1 christos {
303 1.1 christos return (AE_NO_MEMORY);
304 1.1 christos }
305 1.1 christos
306 1.1 christos /* Copy the package elements (integers) to the buffer as DWORDs */
307 1.1 christos
308 1.1 christos Elements = OriginalObject->Package.Elements;
309 1.1 christos DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
310 1.1 christos
311 1.1 christos for (i = 0; i < Count; i++)
312 1.1 christos {
313 1.1 christos *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
314 1.1 christos DwordBuffer++;
315 1.1 christos Elements++;
316 1.1 christos }
317 1.1 christos break;
318 1.1 christos
319 1.1 christos default:
320 1.1 christos
321 1.1 christos return (AE_AML_OPERAND_TYPE);
322 1.1 christos }
323 1.1 christos
324 1.1 christos *ReturnObject = NewObject;
325 1.1 christos return (AE_OK);
326 1.1 christos }
327 1.1 christos
328 1.1 christos
329 1.1 christos /*******************************************************************************
330 1.1 christos *
331 1.1 christos * FUNCTION: AcpiNsConvertToUnicode
332 1.1 christos *
333 1.1 christos * PARAMETERS: OriginalObject - ASCII String Object to be converted
334 1.1 christos * ReturnObject - Where the new converted object is returned
335 1.1 christos *
336 1.1 christos * RETURN: Status. AE_OK if conversion was successful.
337 1.1 christos *
338 1.1 christos * DESCRIPTION: Attempt to convert a String object to a Unicode string Buffer.
339 1.1 christos *
340 1.1 christos ******************************************************************************/
341 1.1 christos
342 1.1 christos ACPI_STATUS
343 1.1 christos AcpiNsConvertToUnicode (
344 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
345 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
346 1.1 christos {
347 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
348 1.1 christos char *AsciiString;
349 1.1 christos UINT16 *UnicodeBuffer;
350 1.1 christos UINT32 UnicodeLength;
351 1.1 christos UINT32 i;
352 1.1 christos
353 1.1 christos
354 1.1 christos if (!OriginalObject)
355 1.1 christos {
356 1.1 christos return (AE_OK);
357 1.1 christos }
358 1.1 christos
359 1.1 christos /* If a Buffer was returned, it must be at least two bytes long */
360 1.1 christos
361 1.1 christos if (OriginalObject->Common.Type == ACPI_TYPE_BUFFER)
362 1.1 christos {
363 1.1 christos if (OriginalObject->Buffer.Length < 2)
364 1.1 christos {
365 1.1 christos return (AE_AML_OPERAND_VALUE);
366 1.1 christos }
367 1.1 christos
368 1.1 christos *ReturnObject = NULL;
369 1.1 christos return (AE_OK);
370 1.1 christos }
371 1.1 christos
372 1.1 christos /*
373 1.1 christos * The original object is an ASCII string. Convert this string to
374 1.1 christos * a unicode buffer.
375 1.1 christos */
376 1.1 christos AsciiString = OriginalObject->String.Pointer;
377 1.1 christos UnicodeLength = (OriginalObject->String.Length * 2) + 2;
378 1.1 christos
379 1.1 christos /* Create a new buffer object for the Unicode data */
380 1.1 christos
381 1.1 christos NewObject = AcpiUtCreateBufferObject (UnicodeLength);
382 1.1 christos if (!NewObject)
383 1.1 christos {
384 1.1 christos return (AE_NO_MEMORY);
385 1.1 christos }
386 1.1 christos
387 1.1 christos UnicodeBuffer = ACPI_CAST_PTR (UINT16, NewObject->Buffer.Pointer);
388 1.1 christos
389 1.1 christos /* Convert ASCII to Unicode */
390 1.1 christos
391 1.1 christos for (i = 0; i < OriginalObject->String.Length; i++)
392 1.1 christos {
393 1.1 christos UnicodeBuffer[i] = (UINT16) AsciiString[i];
394 1.1 christos }
395 1.1 christos
396 1.1 christos *ReturnObject = NewObject;
397 1.1 christos return (AE_OK);
398 1.1 christos }
399 1.1 christos
400 1.1 christos
401 1.1 christos /*******************************************************************************
402 1.1 christos *
403 1.1 christos * FUNCTION: AcpiNsConvertToResource
404 1.1 christos *
405 1.1 christos * PARAMETERS: OriginalObject - Object to be converted
406 1.1 christos * ReturnObject - Where the new converted object is returned
407 1.1 christos *
408 1.1 christos * RETURN: Status. AE_OK if conversion was successful
409 1.1 christos *
410 1.1 christos * DESCRIPTION: Attempt to convert a Integer object to a ResourceTemplate
411 1.1 christos * Buffer.
412 1.1 christos *
413 1.1 christos ******************************************************************************/
414 1.1 christos
415 1.1 christos ACPI_STATUS
416 1.1 christos AcpiNsConvertToResource (
417 1.1 christos ACPI_OPERAND_OBJECT *OriginalObject,
418 1.1 christos ACPI_OPERAND_OBJECT **ReturnObject)
419 1.1 christos {
420 1.1 christos ACPI_OPERAND_OBJECT *NewObject;
421 1.1 christos UINT8 *Buffer;
422 1.1 christos
423 1.1 christos
424 1.1 christos /*
425 1.1 christos * We can fix the following cases for an expected resource template:
426 1.1 christos * 1. No return value (interpreter slack mode is disabled)
427 1.1 christos * 2. A "Return (Zero)" statement
428 1.1 christos * 3. A "Return empty buffer" statement
429 1.1 christos *
430 1.1 christos * We will return a buffer containing a single EndTag
431 1.1 christos * resource descriptor.
432 1.1 christos */
433 1.1 christos if (OriginalObject)
434 1.1 christos {
435 1.1 christos switch (OriginalObject->Common.Type)
436 1.1 christos {
437 1.1 christos case ACPI_TYPE_INTEGER:
438 1.1 christos
439 1.1 christos /* We can only repair an Integer==0 */
440 1.1 christos
441 1.1 christos if (OriginalObject->Integer.Value)
442 1.1 christos {
443 1.1 christos return (AE_AML_OPERAND_TYPE);
444 1.1 christos }
445 1.1 christos break;
446 1.1 christos
447 1.1 christos case ACPI_TYPE_BUFFER:
448 1.1 christos
449 1.1 christos if (OriginalObject->Buffer.Length)
450 1.1 christos {
451 1.1 christos /* Additional checks can be added in the future */
452 1.1 christos
453 1.1 christos *ReturnObject = NULL;
454 1.1 christos return (AE_OK);
455 1.1 christos }
456 1.1 christos break;
457 1.1 christos
458 1.1 christos case ACPI_TYPE_STRING:
459 1.1 christos default:
460 1.1 christos
461 1.1 christos return (AE_AML_OPERAND_TYPE);
462 1.1 christos }
463 1.1 christos }
464 1.1 christos
465 1.1 christos /* Create the new buffer object for the resource descriptor */
466 1.1 christos
467 1.1 christos NewObject = AcpiUtCreateBufferObject (2);
468 1.1 christos if (!NewObject)
469 1.1 christos {
470 1.1 christos return (AE_NO_MEMORY);
471 1.1 christos }
472 1.1 christos
473 1.1 christos Buffer = ACPI_CAST_PTR (UINT8, NewObject->Buffer.Pointer);
474 1.1 christos
475 1.1 christos /* Initialize the Buffer with a single EndTag descriptor */
476 1.1 christos
477 1.1 christos Buffer[0] = (ACPI_RESOURCE_NAME_END_TAG | ASL_RDESC_END_TAG_SIZE);
478 1.1 christos Buffer[1] = 0x00;
479 1.1 christos
480 1.1 christos *ReturnObject = NewObject;
481 1.1 christos return (AE_OK);
482 1.1 christos }
483