1/******************************************************************************
2 *
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
4 *
5 *****************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116#define __NSREPAIR_C__
117
118#include <contrib/dev/acpica/include/acpi.h>
119#include <contrib/dev/acpica/include/accommon.h>
120#include <contrib/dev/acpica/include/acnamesp.h>
121#include <contrib/dev/acpica/include/acinterp.h>
122
123#define _COMPONENT ACPI_NAMESPACE
124 ACPI_MODULE_NAME ("nsrepair")
125
126
127/*******************************************************************************
128 *
129 * This module attempts to repair or convert objects returned by the
130 * predefined methods to an object type that is expected, as per the ACPI
131 * specification. The need for this code is dictated by the many machines that
132 * return incorrect types for the standard predefined methods. Performing these
133 * conversions here, in one place, eliminates the need for individual ACPI
134 * device drivers to do the same. Note: Most of these conversions are different
135 * than the internal object conversion routines used for implicit object
136 * conversion.
137 *
138 * The following conversions can be performed as necessary:
139 *
140 * Integer -> String
141 * Integer -> Buffer
142 * String -> Integer
143 * String -> Buffer
144 * Buffer -> Integer
145 * Buffer -> String
146 * Buffer -> Package of Integers
147 * Package -> Package of one Package
148 *
149 ******************************************************************************/
150
151
152/* Local prototypes */
153
154static ACPI_STATUS
155AcpiNsConvertToInteger (
156 ACPI_OPERAND_OBJECT *OriginalObject,
157 ACPI_OPERAND_OBJECT **ReturnObject);
158
159static ACPI_STATUS
160AcpiNsConvertToString (
161 ACPI_OPERAND_OBJECT *OriginalObject,
162 ACPI_OPERAND_OBJECT **ReturnObject);
163
164static ACPI_STATUS
165AcpiNsConvertToBuffer (
166 ACPI_OPERAND_OBJECT *OriginalObject,
167 ACPI_OPERAND_OBJECT **ReturnObject);
168
169static ACPI_STATUS
170AcpiNsConvertToPackage (
171 ACPI_OPERAND_OBJECT *OriginalObject,
172 ACPI_OPERAND_OBJECT **ReturnObject);
173
174
175/*******************************************************************************
176 *
177 * FUNCTION: AcpiNsRepairObject
178 *
179 * PARAMETERS: Data - Pointer to validation data structure
180 * ExpectedBtypes - Object types expected
181 * PackageIndex - Index of object within parent package (if
182 * applicable - ACPI_NOT_PACKAGE_ELEMENT
183 * otherwise)
184 * ReturnObjectPtr - Pointer to the object returned from the
185 * evaluation of a method or object
186 *
187 * RETURN: Status. AE_OK if repair was successful.
188 *
189 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
190 * not expected.
191 *
192 ******************************************************************************/
193
194ACPI_STATUS
195AcpiNsRepairObject (
196 ACPI_PREDEFINED_DATA *Data,
197 UINT32 ExpectedBtypes,
198 UINT32 PackageIndex,
199 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
200{
201 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
202 ACPI_OPERAND_OBJECT *NewObject;
203 ACPI_STATUS Status;
204
205
206 ACPI_FUNCTION_NAME (NsRepairObject);
207
208
209 /*
210 * At this point, we know that the type of the returned object was not
211 * one of the expected types for this predefined name. Attempt to
212 * repair the object by converting it to one of the expected object
213 * types for this predefined name.
214 */
215 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
216 {
217 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject);
218 if (ACPI_SUCCESS (Status))
219 {
220 goto ObjectRepaired;
221 }
222 }
223 if (ExpectedBtypes & ACPI_RTYPE_STRING)
224 {
225 Status = AcpiNsConvertToString (ReturnObject, &NewObject);
226 if (ACPI_SUCCESS (Status))
227 {
228 goto ObjectRepaired;
229 }
230 }
231 if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
232 {
233 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject);
234 if (ACPI_SUCCESS (Status))
235 {
236 goto ObjectRepaired;
237 }
238 }
239 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE)
240 {
241 Status = AcpiNsConvertToPackage (ReturnObject, &NewObject);
242 if (ACPI_SUCCESS (Status))
243 {
244 goto ObjectRepaired;
245 }
246 }
247
248 /* We cannot repair this object */
249
250 return (AE_AML_OPERAND_TYPE);
251
252
253ObjectRepaired:
254
255 /* Object was successfully repaired */
256
257 /*
258 * If the original object is a package element, we need to:
259 * 1. Set the reference count of the new object to match the
260 * reference count of the old object.
261 * 2. Decrement the reference count of the original object.
262 */
263 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
264 {
265 NewObject->Common.ReferenceCount =
266 ReturnObject->Common.ReferenceCount;
267
268 if (ReturnObject->Common.ReferenceCount > 1)
269 {
270 ReturnObject->Common.ReferenceCount--;
271 }
272
273 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
274 "%s: Converted %s to expected %s at index %u\n",
275 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
276 AcpiUtGetObjectTypeName (NewObject), PackageIndex));
277 }
278 else
279 {
280 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
281 "%s: Converted %s to expected %s\n",
282 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
283 AcpiUtGetObjectTypeName (NewObject)));
284 }
285
286 /* Delete old object, install the new return object */
287
288 AcpiUtRemoveReference (ReturnObject);
289 *ReturnObjectPtr = NewObject;
290 Data->Flags |= ACPI_OBJECT_REPAIRED;
291 return (AE_OK);
292}
293
294
295/*******************************************************************************
296 *
297 * FUNCTION: AcpiNsConvertToInteger
298 *
299 * PARAMETERS: OriginalObject - Object to be converted
300 * ReturnObject - Where the new converted object is returned
301 *
302 * RETURN: Status. AE_OK if conversion was successful.
303 *
304 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
305 *
306 ******************************************************************************/
307
308static ACPI_STATUS
309AcpiNsConvertToInteger (
310 ACPI_OPERAND_OBJECT *OriginalObject,
311 ACPI_OPERAND_OBJECT **ReturnObject)
312{
313 ACPI_OPERAND_OBJECT *NewObject;
314 ACPI_STATUS Status;
315 UINT64 Value = 0;
316 UINT32 i;
317
318
319 switch (OriginalObject->Common.Type)
320 {
321 case ACPI_TYPE_STRING:
322
323 /* String-to-Integer conversion */
324
325 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
326 ACPI_ANY_BASE, &Value);
327 if (ACPI_FAILURE (Status))
328 {
329 return (Status);
330 }
331 break;
332
333 case ACPI_TYPE_BUFFER:
334
335 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
336
337 if (OriginalObject->Buffer.Length > 8)
338 {
339 return (AE_AML_OPERAND_TYPE);
340 }
341
342 /* Extract each buffer byte to create the integer */
343
344 for (i = 0; i < OriginalObject->Buffer.Length; i++)
345 {
346 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
347 }
348 break;
349
350 default:
351 return (AE_AML_OPERAND_TYPE);
352 }
353
354 NewObject = AcpiUtCreateIntegerObject (Value);
355 if (!NewObject)
356 {
357 return (AE_NO_MEMORY);
358 }
359
360 *ReturnObject = NewObject;
361 return (AE_OK);
362}
363
364
365/*******************************************************************************
366 *
367 * FUNCTION: AcpiNsConvertToString
368 *
369 * PARAMETERS: OriginalObject - Object to be converted
370 * ReturnObject - Where the new converted object is returned
371 *
372 * RETURN: Status. AE_OK if conversion was successful.
373 *
374 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
375 *
376 ******************************************************************************/
377
378static ACPI_STATUS
379AcpiNsConvertToString (
380 ACPI_OPERAND_OBJECT *OriginalObject,
381 ACPI_OPERAND_OBJECT **ReturnObject)
382{
383 ACPI_OPERAND_OBJECT *NewObject;
384 ACPI_SIZE Length;
385 ACPI_STATUS Status;
386
387
388 switch (OriginalObject->Common.Type)
389 {
390 case ACPI_TYPE_INTEGER:
391 /*
392 * Integer-to-String conversion. Commonly, convert
393 * an integer of value 0 to a NULL string. The last element of
394 * _BIF and _BIX packages occasionally need this fix.
395 */
396 if (OriginalObject->Integer.Value == 0)
397 {
398 /* Allocate a new NULL string object */
399
400 NewObject = AcpiUtCreateStringObject (0);
401 if (!NewObject)
402 {
403 return (AE_NO_MEMORY);
404 }
405 }
406 else
407 {
408 Status = AcpiExConvertToString (OriginalObject, &NewObject,
409 ACPI_IMPLICIT_CONVERT_HEX);
410 if (ACPI_FAILURE (Status))
411 {
412 return (Status);
413 }
414 }
415 break;
416
417 case ACPI_TYPE_BUFFER:
418 /*
419 * Buffer-to-String conversion. Use a ToString
420 * conversion, no transform performed on the buffer data. The best
421 * example of this is the _BIF method, where the string data from
422 * the battery is often (incorrectly) returned as buffer object(s).
423 */
424 Length = 0;
425 while ((Length < OriginalObject->Buffer.Length) &&
426 (OriginalObject->Buffer.Pointer[Length]))
427 {
428 Length++;
429 }
430
431 /* Allocate a new string object */
432
433 NewObject = AcpiUtCreateStringObject (Length);
434 if (!NewObject)
435 {
436 return (AE_NO_MEMORY);
437 }
438
439 /*
440 * Copy the raw buffer data with no transform. String is already NULL
441 * terminated at Length+1.
442 */
443 ACPI_MEMCPY (NewObject->String.Pointer,
444 OriginalObject->Buffer.Pointer, Length);
445 break;
446
447 default:
448 return (AE_AML_OPERAND_TYPE);
449 }
450
451 *ReturnObject = NewObject;
452 return (AE_OK);
453}
454
455
456/*******************************************************************************
457 *
458 * FUNCTION: AcpiNsConvertToBuffer
459 *
460 * PARAMETERS: OriginalObject - Object to be converted
461 * ReturnObject - Where the new converted object is returned
462 *
463 * RETURN: Status. AE_OK if conversion was successful.
464 *
465 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
466 *
467 ******************************************************************************/
468
469static ACPI_STATUS
470AcpiNsConvertToBuffer (
471 ACPI_OPERAND_OBJECT *OriginalObject,
472 ACPI_OPERAND_OBJECT **ReturnObject)
473{
474 ACPI_OPERAND_OBJECT *NewObject;
475 ACPI_STATUS Status;
476 ACPI_OPERAND_OBJECT **Elements;
477 UINT32 *DwordBuffer;
478 UINT32 Count;
479 UINT32 i;
480
481
482 switch (OriginalObject->Common.Type)
483 {
484 case ACPI_TYPE_INTEGER:
485 /*
486 * Integer-to-Buffer conversion.
487 * Convert the Integer to a packed-byte buffer. _MAT and other
488 * objects need this sometimes, if a read has been performed on a
489 * Field object that is less than or equal to the global integer
490 * size (32 or 64 bits).
491 */
492 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
493 if (ACPI_FAILURE (Status))
494 {
495 return (Status);
496 }
497 break;
498
499 case ACPI_TYPE_STRING:
500
501 /* String-to-Buffer conversion. Simple data copy */
502
503 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
504 if (!NewObject)
505 {
506 return (AE_NO_MEMORY);
507 }
508
509 ACPI_MEMCPY (NewObject->Buffer.Pointer,
510 OriginalObject->String.Pointer, OriginalObject->String.Length);
511 break;
512
513 case ACPI_TYPE_PACKAGE:
514 /*
515 * This case is often seen for predefined names that must return a
516 * Buffer object with multiple DWORD integers within. For example,
517 * _FDE and _GTM. The Package can be converted to a Buffer.
518 */
519
520 /* All elements of the Package must be integers */
521
522 Elements = OriginalObject->Package.Elements;
523 Count = OriginalObject->Package.Count;
524
525 for (i = 0; i < Count; i++)
526 {
527 if ((!*Elements) ||
528 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
529 {
530 return (AE_AML_OPERAND_TYPE);
531 }
532 Elements++;
533 }
534
535 /* Create the new buffer object to replace the Package */
536
537 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
538 if (!NewObject)
539 {
540 return (AE_NO_MEMORY);
541 }
542
543 /* Copy the package elements (integers) to the buffer as DWORDs */
544
545 Elements = OriginalObject->Package.Elements;
546 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
547
548 for (i = 0; i < Count; i++)
549 {
550 *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
551 DwordBuffer++;
552 Elements++;
553 }
554 break;
555
556 default:
557 return (AE_AML_OPERAND_TYPE);
558 }
559
560 *ReturnObject = NewObject;
561 return (AE_OK);
562}
563
564
565/*******************************************************************************
566 *
567 * FUNCTION: AcpiNsConvertToPackage
568 *
569 * PARAMETERS: OriginalObject - Object to be converted
570 * ReturnObject - Where the new converted object is returned
571 *
572 * RETURN: Status. AE_OK if conversion was successful.
573 *
574 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
575 * the buffer is converted to a single integer package element.
576 *
577 ******************************************************************************/
578
579static ACPI_STATUS
580AcpiNsConvertToPackage (
581 ACPI_OPERAND_OBJECT *OriginalObject,
582 ACPI_OPERAND_OBJECT **ReturnObject)
583{
584 ACPI_OPERAND_OBJECT *NewObject;
585 ACPI_OPERAND_OBJECT **Elements;
586 UINT32 Length;
587 UINT8 *Buffer;
588
589
590 switch (OriginalObject->Common.Type)
591 {
592 case ACPI_TYPE_BUFFER:
593
594 /* Buffer-to-Package conversion */
595
596 Length = OriginalObject->Buffer.Length;
597 NewObject = AcpiUtCreatePackageObject (Length);
598 if (!NewObject)
599 {
600 return (AE_NO_MEMORY);
601 }
602
603 /* Convert each buffer byte to an integer package element */
604
605 Elements = NewObject->Package.Elements;
606 Buffer = OriginalObject->Buffer.Pointer;
607
608 while (Length--)
609 {
610 *Elements = AcpiUtCreateIntegerObject ((UINT64) *Buffer);
611 if (!*Elements)
612 {
613 AcpiUtRemoveReference (NewObject);
614 return (AE_NO_MEMORY);
615 }
616 Elements++;
617 Buffer++;
618 }
619 break;
620
621 default:
622 return (AE_AML_OPERAND_TYPE);
623 }
624
625 *ReturnObject = NewObject;
626 return (AE_OK);
627}
628
629
630/*******************************************************************************
631 *
632 * FUNCTION: AcpiNsRepairPackageList
633 *
634 * PARAMETERS: Data - Pointer to validation data structure
635 * ObjDescPtr - Pointer to the object to repair. The new
636 * package object is returned here,
637 * overwriting the old object.
638 *
639 * RETURN: Status, new object in *ObjDescPtr
640 *
641 * DESCRIPTION: Repair a common problem with objects that are defined to return
642 * a variable-length Package of Packages. If the variable-length
643 * is one, some BIOS code mistakenly simply declares a single
644 * Package instead of a Package with one sub-Package. This
645 * function attempts to repair this error by wrapping a Package
646 * object around the original Package, creating the correct
647 * Package with one sub-Package.
648 *
649 * Names that can be repaired in this manner include:
650 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
651 *
652 ******************************************************************************/
653
654ACPI_STATUS
655AcpiNsRepairPackageList (
656 ACPI_PREDEFINED_DATA *Data,
657 ACPI_OPERAND_OBJECT **ObjDescPtr)
658{
659 ACPI_OPERAND_OBJECT *PkgObjDesc;
660
661
662 ACPI_FUNCTION_NAME (NsRepairPackageList);
663
664
665 /*
666 * Create the new outer package and populate it. The new package will
667 * have a single element, the lone subpackage.
668 */
669 PkgObjDesc = AcpiUtCreatePackageObject (1);
670 if (!PkgObjDesc)
671 {
672 return (AE_NO_MEMORY);
673 }
674
675 PkgObjDesc->Package.Elements[0] = *ObjDescPtr;
676
677 /* Return the new object in the object pointer */
678
679 *ObjDescPtr = PkgObjDesc;
680 Data->Flags |= ACPI_OBJECT_REPAIRED;
681
682 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
683 "%s: Repaired incorrectly formed Package\n", Data->Pathname));
684
685 return (AE_OK);
686}
2 *
3 * Module Name: nsrepair - Repair for objects returned by predefined methods
4 *
5 *****************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116#define __NSREPAIR_C__
117
118#include <contrib/dev/acpica/include/acpi.h>
119#include <contrib/dev/acpica/include/accommon.h>
120#include <contrib/dev/acpica/include/acnamesp.h>
121#include <contrib/dev/acpica/include/acinterp.h>
122
123#define _COMPONENT ACPI_NAMESPACE
124 ACPI_MODULE_NAME ("nsrepair")
125
126
127/*******************************************************************************
128 *
129 * This module attempts to repair or convert objects returned by the
130 * predefined methods to an object type that is expected, as per the ACPI
131 * specification. The need for this code is dictated by the many machines that
132 * return incorrect types for the standard predefined methods. Performing these
133 * conversions here, in one place, eliminates the need for individual ACPI
134 * device drivers to do the same. Note: Most of these conversions are different
135 * than the internal object conversion routines used for implicit object
136 * conversion.
137 *
138 * The following conversions can be performed as necessary:
139 *
140 * Integer -> String
141 * Integer -> Buffer
142 * String -> Integer
143 * String -> Buffer
144 * Buffer -> Integer
145 * Buffer -> String
146 * Buffer -> Package of Integers
147 * Package -> Package of one Package
148 *
149 ******************************************************************************/
150
151
152/* Local prototypes */
153
154static ACPI_STATUS
155AcpiNsConvertToInteger (
156 ACPI_OPERAND_OBJECT *OriginalObject,
157 ACPI_OPERAND_OBJECT **ReturnObject);
158
159static ACPI_STATUS
160AcpiNsConvertToString (
161 ACPI_OPERAND_OBJECT *OriginalObject,
162 ACPI_OPERAND_OBJECT **ReturnObject);
163
164static ACPI_STATUS
165AcpiNsConvertToBuffer (
166 ACPI_OPERAND_OBJECT *OriginalObject,
167 ACPI_OPERAND_OBJECT **ReturnObject);
168
169static ACPI_STATUS
170AcpiNsConvertToPackage (
171 ACPI_OPERAND_OBJECT *OriginalObject,
172 ACPI_OPERAND_OBJECT **ReturnObject);
173
174
175/*******************************************************************************
176 *
177 * FUNCTION: AcpiNsRepairObject
178 *
179 * PARAMETERS: Data - Pointer to validation data structure
180 * ExpectedBtypes - Object types expected
181 * PackageIndex - Index of object within parent package (if
182 * applicable - ACPI_NOT_PACKAGE_ELEMENT
183 * otherwise)
184 * ReturnObjectPtr - Pointer to the object returned from the
185 * evaluation of a method or object
186 *
187 * RETURN: Status. AE_OK if repair was successful.
188 *
189 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
190 * not expected.
191 *
192 ******************************************************************************/
193
194ACPI_STATUS
195AcpiNsRepairObject (
196 ACPI_PREDEFINED_DATA *Data,
197 UINT32 ExpectedBtypes,
198 UINT32 PackageIndex,
199 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
200{
201 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
202 ACPI_OPERAND_OBJECT *NewObject;
203 ACPI_STATUS Status;
204
205
206 ACPI_FUNCTION_NAME (NsRepairObject);
207
208
209 /*
210 * At this point, we know that the type of the returned object was not
211 * one of the expected types for this predefined name. Attempt to
212 * repair the object by converting it to one of the expected object
213 * types for this predefined name.
214 */
215 if (ExpectedBtypes & ACPI_RTYPE_INTEGER)
216 {
217 Status = AcpiNsConvertToInteger (ReturnObject, &NewObject);
218 if (ACPI_SUCCESS (Status))
219 {
220 goto ObjectRepaired;
221 }
222 }
223 if (ExpectedBtypes & ACPI_RTYPE_STRING)
224 {
225 Status = AcpiNsConvertToString (ReturnObject, &NewObject);
226 if (ACPI_SUCCESS (Status))
227 {
228 goto ObjectRepaired;
229 }
230 }
231 if (ExpectedBtypes & ACPI_RTYPE_BUFFER)
232 {
233 Status = AcpiNsConvertToBuffer (ReturnObject, &NewObject);
234 if (ACPI_SUCCESS (Status))
235 {
236 goto ObjectRepaired;
237 }
238 }
239 if (ExpectedBtypes & ACPI_RTYPE_PACKAGE)
240 {
241 Status = AcpiNsConvertToPackage (ReturnObject, &NewObject);
242 if (ACPI_SUCCESS (Status))
243 {
244 goto ObjectRepaired;
245 }
246 }
247
248 /* We cannot repair this object */
249
250 return (AE_AML_OPERAND_TYPE);
251
252
253ObjectRepaired:
254
255 /* Object was successfully repaired */
256
257 /*
258 * If the original object is a package element, we need to:
259 * 1. Set the reference count of the new object to match the
260 * reference count of the old object.
261 * 2. Decrement the reference count of the original object.
262 */
263 if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
264 {
265 NewObject->Common.ReferenceCount =
266 ReturnObject->Common.ReferenceCount;
267
268 if (ReturnObject->Common.ReferenceCount > 1)
269 {
270 ReturnObject->Common.ReferenceCount--;
271 }
272
273 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
274 "%s: Converted %s to expected %s at index %u\n",
275 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
276 AcpiUtGetObjectTypeName (NewObject), PackageIndex));
277 }
278 else
279 {
280 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
281 "%s: Converted %s to expected %s\n",
282 Data->Pathname, AcpiUtGetObjectTypeName (ReturnObject),
283 AcpiUtGetObjectTypeName (NewObject)));
284 }
285
286 /* Delete old object, install the new return object */
287
288 AcpiUtRemoveReference (ReturnObject);
289 *ReturnObjectPtr = NewObject;
290 Data->Flags |= ACPI_OBJECT_REPAIRED;
291 return (AE_OK);
292}
293
294
295/*******************************************************************************
296 *
297 * FUNCTION: AcpiNsConvertToInteger
298 *
299 * PARAMETERS: OriginalObject - Object to be converted
300 * ReturnObject - Where the new converted object is returned
301 *
302 * RETURN: Status. AE_OK if conversion was successful.
303 *
304 * DESCRIPTION: Attempt to convert a String/Buffer object to an Integer.
305 *
306 ******************************************************************************/
307
308static ACPI_STATUS
309AcpiNsConvertToInteger (
310 ACPI_OPERAND_OBJECT *OriginalObject,
311 ACPI_OPERAND_OBJECT **ReturnObject)
312{
313 ACPI_OPERAND_OBJECT *NewObject;
314 ACPI_STATUS Status;
315 UINT64 Value = 0;
316 UINT32 i;
317
318
319 switch (OriginalObject->Common.Type)
320 {
321 case ACPI_TYPE_STRING:
322
323 /* String-to-Integer conversion */
324
325 Status = AcpiUtStrtoul64 (OriginalObject->String.Pointer,
326 ACPI_ANY_BASE, &Value);
327 if (ACPI_FAILURE (Status))
328 {
329 return (Status);
330 }
331 break;
332
333 case ACPI_TYPE_BUFFER:
334
335 /* Buffer-to-Integer conversion. Max buffer size is 64 bits. */
336
337 if (OriginalObject->Buffer.Length > 8)
338 {
339 return (AE_AML_OPERAND_TYPE);
340 }
341
342 /* Extract each buffer byte to create the integer */
343
344 for (i = 0; i < OriginalObject->Buffer.Length; i++)
345 {
346 Value |= ((UINT64) OriginalObject->Buffer.Pointer[i] << (i * 8));
347 }
348 break;
349
350 default:
351 return (AE_AML_OPERAND_TYPE);
352 }
353
354 NewObject = AcpiUtCreateIntegerObject (Value);
355 if (!NewObject)
356 {
357 return (AE_NO_MEMORY);
358 }
359
360 *ReturnObject = NewObject;
361 return (AE_OK);
362}
363
364
365/*******************************************************************************
366 *
367 * FUNCTION: AcpiNsConvertToString
368 *
369 * PARAMETERS: OriginalObject - Object to be converted
370 * ReturnObject - Where the new converted object is returned
371 *
372 * RETURN: Status. AE_OK if conversion was successful.
373 *
374 * DESCRIPTION: Attempt to convert a Integer/Buffer object to a String.
375 *
376 ******************************************************************************/
377
378static ACPI_STATUS
379AcpiNsConvertToString (
380 ACPI_OPERAND_OBJECT *OriginalObject,
381 ACPI_OPERAND_OBJECT **ReturnObject)
382{
383 ACPI_OPERAND_OBJECT *NewObject;
384 ACPI_SIZE Length;
385 ACPI_STATUS Status;
386
387
388 switch (OriginalObject->Common.Type)
389 {
390 case ACPI_TYPE_INTEGER:
391 /*
392 * Integer-to-String conversion. Commonly, convert
393 * an integer of value 0 to a NULL string. The last element of
394 * _BIF and _BIX packages occasionally need this fix.
395 */
396 if (OriginalObject->Integer.Value == 0)
397 {
398 /* Allocate a new NULL string object */
399
400 NewObject = AcpiUtCreateStringObject (0);
401 if (!NewObject)
402 {
403 return (AE_NO_MEMORY);
404 }
405 }
406 else
407 {
408 Status = AcpiExConvertToString (OriginalObject, &NewObject,
409 ACPI_IMPLICIT_CONVERT_HEX);
410 if (ACPI_FAILURE (Status))
411 {
412 return (Status);
413 }
414 }
415 break;
416
417 case ACPI_TYPE_BUFFER:
418 /*
419 * Buffer-to-String conversion. Use a ToString
420 * conversion, no transform performed on the buffer data. The best
421 * example of this is the _BIF method, where the string data from
422 * the battery is often (incorrectly) returned as buffer object(s).
423 */
424 Length = 0;
425 while ((Length < OriginalObject->Buffer.Length) &&
426 (OriginalObject->Buffer.Pointer[Length]))
427 {
428 Length++;
429 }
430
431 /* Allocate a new string object */
432
433 NewObject = AcpiUtCreateStringObject (Length);
434 if (!NewObject)
435 {
436 return (AE_NO_MEMORY);
437 }
438
439 /*
440 * Copy the raw buffer data with no transform. String is already NULL
441 * terminated at Length+1.
442 */
443 ACPI_MEMCPY (NewObject->String.Pointer,
444 OriginalObject->Buffer.Pointer, Length);
445 break;
446
447 default:
448 return (AE_AML_OPERAND_TYPE);
449 }
450
451 *ReturnObject = NewObject;
452 return (AE_OK);
453}
454
455
456/*******************************************************************************
457 *
458 * FUNCTION: AcpiNsConvertToBuffer
459 *
460 * PARAMETERS: OriginalObject - Object to be converted
461 * ReturnObject - Where the new converted object is returned
462 *
463 * RETURN: Status. AE_OK if conversion was successful.
464 *
465 * DESCRIPTION: Attempt to convert a Integer/String/Package object to a Buffer.
466 *
467 ******************************************************************************/
468
469static ACPI_STATUS
470AcpiNsConvertToBuffer (
471 ACPI_OPERAND_OBJECT *OriginalObject,
472 ACPI_OPERAND_OBJECT **ReturnObject)
473{
474 ACPI_OPERAND_OBJECT *NewObject;
475 ACPI_STATUS Status;
476 ACPI_OPERAND_OBJECT **Elements;
477 UINT32 *DwordBuffer;
478 UINT32 Count;
479 UINT32 i;
480
481
482 switch (OriginalObject->Common.Type)
483 {
484 case ACPI_TYPE_INTEGER:
485 /*
486 * Integer-to-Buffer conversion.
487 * Convert the Integer to a packed-byte buffer. _MAT and other
488 * objects need this sometimes, if a read has been performed on a
489 * Field object that is less than or equal to the global integer
490 * size (32 or 64 bits).
491 */
492 Status = AcpiExConvertToBuffer (OriginalObject, &NewObject);
493 if (ACPI_FAILURE (Status))
494 {
495 return (Status);
496 }
497 break;
498
499 case ACPI_TYPE_STRING:
500
501 /* String-to-Buffer conversion. Simple data copy */
502
503 NewObject = AcpiUtCreateBufferObject (OriginalObject->String.Length);
504 if (!NewObject)
505 {
506 return (AE_NO_MEMORY);
507 }
508
509 ACPI_MEMCPY (NewObject->Buffer.Pointer,
510 OriginalObject->String.Pointer, OriginalObject->String.Length);
511 break;
512
513 case ACPI_TYPE_PACKAGE:
514 /*
515 * This case is often seen for predefined names that must return a
516 * Buffer object with multiple DWORD integers within. For example,
517 * _FDE and _GTM. The Package can be converted to a Buffer.
518 */
519
520 /* All elements of the Package must be integers */
521
522 Elements = OriginalObject->Package.Elements;
523 Count = OriginalObject->Package.Count;
524
525 for (i = 0; i < Count; i++)
526 {
527 if ((!*Elements) ||
528 ((*Elements)->Common.Type != ACPI_TYPE_INTEGER))
529 {
530 return (AE_AML_OPERAND_TYPE);
531 }
532 Elements++;
533 }
534
535 /* Create the new buffer object to replace the Package */
536
537 NewObject = AcpiUtCreateBufferObject (ACPI_MUL_4 (Count));
538 if (!NewObject)
539 {
540 return (AE_NO_MEMORY);
541 }
542
543 /* Copy the package elements (integers) to the buffer as DWORDs */
544
545 Elements = OriginalObject->Package.Elements;
546 DwordBuffer = ACPI_CAST_PTR (UINT32, NewObject->Buffer.Pointer);
547
548 for (i = 0; i < Count; i++)
549 {
550 *DwordBuffer = (UINT32) (*Elements)->Integer.Value;
551 DwordBuffer++;
552 Elements++;
553 }
554 break;
555
556 default:
557 return (AE_AML_OPERAND_TYPE);
558 }
559
560 *ReturnObject = NewObject;
561 return (AE_OK);
562}
563
564
565/*******************************************************************************
566 *
567 * FUNCTION: AcpiNsConvertToPackage
568 *
569 * PARAMETERS: OriginalObject - Object to be converted
570 * ReturnObject - Where the new converted object is returned
571 *
572 * RETURN: Status. AE_OK if conversion was successful.
573 *
574 * DESCRIPTION: Attempt to convert a Buffer object to a Package. Each byte of
575 * the buffer is converted to a single integer package element.
576 *
577 ******************************************************************************/
578
579static ACPI_STATUS
580AcpiNsConvertToPackage (
581 ACPI_OPERAND_OBJECT *OriginalObject,
582 ACPI_OPERAND_OBJECT **ReturnObject)
583{
584 ACPI_OPERAND_OBJECT *NewObject;
585 ACPI_OPERAND_OBJECT **Elements;
586 UINT32 Length;
587 UINT8 *Buffer;
588
589
590 switch (OriginalObject->Common.Type)
591 {
592 case ACPI_TYPE_BUFFER:
593
594 /* Buffer-to-Package conversion */
595
596 Length = OriginalObject->Buffer.Length;
597 NewObject = AcpiUtCreatePackageObject (Length);
598 if (!NewObject)
599 {
600 return (AE_NO_MEMORY);
601 }
602
603 /* Convert each buffer byte to an integer package element */
604
605 Elements = NewObject->Package.Elements;
606 Buffer = OriginalObject->Buffer.Pointer;
607
608 while (Length--)
609 {
610 *Elements = AcpiUtCreateIntegerObject ((UINT64) *Buffer);
611 if (!*Elements)
612 {
613 AcpiUtRemoveReference (NewObject);
614 return (AE_NO_MEMORY);
615 }
616 Elements++;
617 Buffer++;
618 }
619 break;
620
621 default:
622 return (AE_AML_OPERAND_TYPE);
623 }
624
625 *ReturnObject = NewObject;
626 return (AE_OK);
627}
628
629
630/*******************************************************************************
631 *
632 * FUNCTION: AcpiNsRepairPackageList
633 *
634 * PARAMETERS: Data - Pointer to validation data structure
635 * ObjDescPtr - Pointer to the object to repair. The new
636 * package object is returned here,
637 * overwriting the old object.
638 *
639 * RETURN: Status, new object in *ObjDescPtr
640 *
641 * DESCRIPTION: Repair a common problem with objects that are defined to return
642 * a variable-length Package of Packages. If the variable-length
643 * is one, some BIOS code mistakenly simply declares a single
644 * Package instead of a Package with one sub-Package. This
645 * function attempts to repair this error by wrapping a Package
646 * object around the original Package, creating the correct
647 * Package with one sub-Package.
648 *
649 * Names that can be repaired in this manner include:
650 * _ALR, _CSD, _HPX, _MLS, _PRT, _PSS, _TRT, TSS
651 *
652 ******************************************************************************/
653
654ACPI_STATUS
655AcpiNsRepairPackageList (
656 ACPI_PREDEFINED_DATA *Data,
657 ACPI_OPERAND_OBJECT **ObjDescPtr)
658{
659 ACPI_OPERAND_OBJECT *PkgObjDesc;
660
661
662 ACPI_FUNCTION_NAME (NsRepairPackageList);
663
664
665 /*
666 * Create the new outer package and populate it. The new package will
667 * have a single element, the lone subpackage.
668 */
669 PkgObjDesc = AcpiUtCreatePackageObject (1);
670 if (!PkgObjDesc)
671 {
672 return (AE_NO_MEMORY);
673 }
674
675 PkgObjDesc->Package.Elements[0] = *ObjDescPtr;
676
677 /* Return the new object in the object pointer */
678
679 *ObjDescPtr = PkgObjDesc;
680 Data->Flags |= ACPI_OBJECT_REPAIRED;
681
682 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
683 "%s: Repaired incorrectly formed Package\n", Data->Pathname));
684
685 return (AE_OK);
686}