utmisc.c revision 209746
1/*******************************************************************************
2 *
3 * Module Name: utmisc - common utility procedures
4 *
5 ******************************************************************************/
6
7/******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2010, 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
117#define __UTMISC_C__
118
119#include <contrib/dev/acpica/include/acpi.h>
120#include <contrib/dev/acpica/include/accommon.h>
121#include <contrib/dev/acpica/include/acnamesp.h>
122
123
124#define _COMPONENT          ACPI_UTILITIES
125        ACPI_MODULE_NAME    ("utmisc")
126
127/*
128 * Common suffix for messages
129 */
130#define ACPI_COMMON_MSG_SUFFIX \
131    AcpiOsPrintf (" (%8.8X/%s-%u)\n", ACPI_CA_VERSION, ModuleName, LineNumber)
132
133
134/*******************************************************************************
135 *
136 * FUNCTION:    AcpiUtValidateException
137 *
138 * PARAMETERS:  Status       - The ACPI_STATUS code to be formatted
139 *
140 * RETURN:      A string containing the exception text. NULL if exception is
141 *              not valid.
142 *
143 * DESCRIPTION: This function validates and translates an ACPI exception into
144 *              an ASCII string.
145 *
146 ******************************************************************************/
147
148const char *
149AcpiUtValidateException (
150    ACPI_STATUS             Status)
151{
152    UINT32                  SubStatus;
153    const char              *Exception = NULL;
154
155
156    ACPI_FUNCTION_ENTRY ();
157
158
159    /*
160     * Status is composed of two parts, a "type" and an actual code
161     */
162    SubStatus = (Status & ~AE_CODE_MASK);
163
164    switch (Status & AE_CODE_MASK)
165    {
166    case AE_CODE_ENVIRONMENTAL:
167
168        if (SubStatus <= AE_CODE_ENV_MAX)
169        {
170            Exception = AcpiGbl_ExceptionNames_Env [SubStatus];
171        }
172        break;
173
174    case AE_CODE_PROGRAMMER:
175
176        if (SubStatus <= AE_CODE_PGM_MAX)
177        {
178            Exception = AcpiGbl_ExceptionNames_Pgm [SubStatus];
179        }
180        break;
181
182    case AE_CODE_ACPI_TABLES:
183
184        if (SubStatus <= AE_CODE_TBL_MAX)
185        {
186            Exception = AcpiGbl_ExceptionNames_Tbl [SubStatus];
187        }
188        break;
189
190    case AE_CODE_AML:
191
192        if (SubStatus <= AE_CODE_AML_MAX)
193        {
194            Exception = AcpiGbl_ExceptionNames_Aml [SubStatus];
195        }
196        break;
197
198    case AE_CODE_CONTROL:
199
200        if (SubStatus <= AE_CODE_CTRL_MAX)
201        {
202            Exception = AcpiGbl_ExceptionNames_Ctrl [SubStatus];
203        }
204        break;
205
206    default:
207        break;
208    }
209
210    return (ACPI_CAST_PTR (const char, Exception));
211}
212
213
214/*******************************************************************************
215 *
216 * FUNCTION:    AcpiUtIsPciRootBridge
217 *
218 * PARAMETERS:  Id              - The HID/CID in string format
219 *
220 * RETURN:      TRUE if the Id is a match for a PCI/PCI-Express Root Bridge
221 *
222 * DESCRIPTION: Determine if the input ID is a PCI Root Bridge ID.
223 *
224 ******************************************************************************/
225
226BOOLEAN
227AcpiUtIsPciRootBridge (
228    char                    *Id)
229{
230
231    /*
232     * Check if this is a PCI root bridge.
233     * ACPI 3.0+: check for a PCI Express root also.
234     */
235    if (!(ACPI_STRCMP (Id,
236            PCI_ROOT_HID_STRING)) ||
237
238        !(ACPI_STRCMP (Id,
239            PCI_EXPRESS_ROOT_HID_STRING)))
240    {
241        return (TRUE);
242    }
243
244    return (FALSE);
245}
246
247
248/*******************************************************************************
249 *
250 * FUNCTION:    AcpiUtIsAmlTable
251 *
252 * PARAMETERS:  Table               - An ACPI table
253 *
254 * RETURN:      TRUE if table contains executable AML; FALSE otherwise
255 *
256 * DESCRIPTION: Check ACPI Signature for a table that contains AML code.
257 *              Currently, these are DSDT,SSDT,PSDT. All other table types are
258 *              data tables that do not contain AML code.
259 *
260 ******************************************************************************/
261
262BOOLEAN
263AcpiUtIsAmlTable (
264    ACPI_TABLE_HEADER       *Table)
265{
266
267    /* These are the only tables that contain executable AML */
268
269    if (ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_DSDT) ||
270        ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_PSDT) ||
271        ACPI_COMPARE_NAME (Table->Signature, ACPI_SIG_SSDT))
272    {
273        return (TRUE);
274    }
275
276    return (FALSE);
277}
278
279
280/*******************************************************************************
281 *
282 * FUNCTION:    AcpiUtAllocateOwnerId
283 *
284 * PARAMETERS:  OwnerId         - Where the new owner ID is returned
285 *
286 * RETURN:      Status
287 *
288 * DESCRIPTION: Allocate a table or method owner ID. The owner ID is used to
289 *              track objects created by the table or method, to be deleted
290 *              when the method exits or the table is unloaded.
291 *
292 ******************************************************************************/
293
294ACPI_STATUS
295AcpiUtAllocateOwnerId (
296    ACPI_OWNER_ID           *OwnerId)
297{
298    UINT32                  i;
299    UINT32                  j;
300    UINT32                  k;
301    ACPI_STATUS             Status;
302
303
304    ACPI_FUNCTION_TRACE (UtAllocateOwnerId);
305
306
307    /* Guard against multiple allocations of ID to the same location */
308
309    if (*OwnerId)
310    {
311        ACPI_ERROR ((AE_INFO, "Owner ID [0x%2.2X] already exists", *OwnerId));
312        return_ACPI_STATUS (AE_ALREADY_EXISTS);
313    }
314
315    /* Mutex for the global ID mask */
316
317    Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
318    if (ACPI_FAILURE (Status))
319    {
320        return_ACPI_STATUS (Status);
321    }
322
323    /*
324     * Find a free owner ID, cycle through all possible IDs on repeated
325     * allocations. (ACPI_NUM_OWNERID_MASKS + 1) because first index may have
326     * to be scanned twice.
327     */
328    for (i = 0, j = AcpiGbl_LastOwnerIdIndex;
329         i < (ACPI_NUM_OWNERID_MASKS + 1);
330         i++, j++)
331    {
332        if (j >= ACPI_NUM_OWNERID_MASKS)
333        {
334            j = 0;  /* Wraparound to start of mask array */
335        }
336
337        for (k = AcpiGbl_NextOwnerIdOffset; k < 32; k++)
338        {
339            if (AcpiGbl_OwnerIdMask[j] == ACPI_UINT32_MAX)
340            {
341                /* There are no free IDs in this mask */
342
343                break;
344            }
345
346            if (!(AcpiGbl_OwnerIdMask[j] & (1 << k)))
347            {
348                /*
349                 * Found a free ID. The actual ID is the bit index plus one,
350                 * making zero an invalid Owner ID. Save this as the last ID
351                 * allocated and update the global ID mask.
352                 */
353                AcpiGbl_OwnerIdMask[j] |= (1 << k);
354
355                AcpiGbl_LastOwnerIdIndex = (UINT8) j;
356                AcpiGbl_NextOwnerIdOffset = (UINT8) (k + 1);
357
358                /*
359                 * Construct encoded ID from the index and bit position
360                 *
361                 * Note: Last [j].k (bit 255) is never used and is marked
362                 * permanently allocated (prevents +1 overflow)
363                 */
364                *OwnerId = (ACPI_OWNER_ID) ((k + 1) + ACPI_MUL_32 (j));
365
366                ACPI_DEBUG_PRINT ((ACPI_DB_VALUES,
367                    "Allocated OwnerId: %2.2X\n", (unsigned int) *OwnerId));
368                goto Exit;
369            }
370        }
371
372        AcpiGbl_NextOwnerIdOffset = 0;
373    }
374
375    /*
376     * All OwnerIds have been allocated. This typically should
377     * not happen since the IDs are reused after deallocation. The IDs are
378     * allocated upon table load (one per table) and method execution, and
379     * they are released when a table is unloaded or a method completes
380     * execution.
381     *
382     * If this error happens, there may be very deep nesting of invoked control
383     * methods, or there may be a bug where the IDs are not released.
384     */
385    Status = AE_OWNER_ID_LIMIT;
386    ACPI_ERROR ((AE_INFO,
387        "Could not allocate new OwnerId (255 max), AE_OWNER_ID_LIMIT"));
388
389Exit:
390    (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
391    return_ACPI_STATUS (Status);
392}
393
394
395/*******************************************************************************
396 *
397 * FUNCTION:    AcpiUtReleaseOwnerId
398 *
399 * PARAMETERS:  OwnerIdPtr          - Pointer to a previously allocated OwnerID
400 *
401 * RETURN:      None. No error is returned because we are either exiting a
402 *              control method or unloading a table. Either way, we would
403 *              ignore any error anyway.
404 *
405 * DESCRIPTION: Release a table or method owner ID.  Valid IDs are 1 - 255
406 *
407 ******************************************************************************/
408
409void
410AcpiUtReleaseOwnerId (
411    ACPI_OWNER_ID           *OwnerIdPtr)
412{
413    ACPI_OWNER_ID           OwnerId = *OwnerIdPtr;
414    ACPI_STATUS             Status;
415    UINT32                  Index;
416    UINT32                  Bit;
417
418
419    ACPI_FUNCTION_TRACE_U32 (UtReleaseOwnerId, OwnerId);
420
421
422    /* Always clear the input OwnerId (zero is an invalid ID) */
423
424    *OwnerIdPtr = 0;
425
426    /* Zero is not a valid OwnerID */
427
428    if (OwnerId == 0)
429    {
430        ACPI_ERROR ((AE_INFO, "Invalid OwnerId: 0x%2.2X", OwnerId));
431        return_VOID;
432    }
433
434    /* Mutex for the global ID mask */
435
436    Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
437    if (ACPI_FAILURE (Status))
438    {
439        return_VOID;
440    }
441
442    /* Normalize the ID to zero */
443
444    OwnerId--;
445
446    /* Decode ID to index/offset pair */
447
448    Index = ACPI_DIV_32 (OwnerId);
449    Bit = 1 << ACPI_MOD_32 (OwnerId);
450
451    /* Free the owner ID only if it is valid */
452
453    if (AcpiGbl_OwnerIdMask[Index] & Bit)
454    {
455        AcpiGbl_OwnerIdMask[Index] ^= Bit;
456    }
457    else
458    {
459        ACPI_ERROR ((AE_INFO,
460            "Release of non-allocated OwnerId: 0x%2.2X", OwnerId + 1));
461    }
462
463    (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
464    return_VOID;
465}
466
467
468/*******************************************************************************
469 *
470 * FUNCTION:    AcpiUtStrupr (strupr)
471 *
472 * PARAMETERS:  SrcString       - The source string to convert
473 *
474 * RETURN:      None
475 *
476 * DESCRIPTION: Convert string to uppercase
477 *
478 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
479 *
480 ******************************************************************************/
481
482void
483AcpiUtStrupr (
484    char                    *SrcString)
485{
486    char                    *String;
487
488
489    ACPI_FUNCTION_ENTRY ();
490
491
492    if (!SrcString)
493    {
494        return;
495    }
496
497    /* Walk entire string, uppercasing the letters */
498
499    for (String = SrcString; *String; String++)
500    {
501        *String = (char) ACPI_TOUPPER (*String);
502    }
503
504    return;
505}
506
507
508#ifdef ACPI_ASL_COMPILER
509/*******************************************************************************
510 *
511 * FUNCTION:    AcpiUtStrlwr (strlwr)
512 *
513 * PARAMETERS:  SrcString       - The source string to convert
514 *
515 * RETURN:      None
516 *
517 * DESCRIPTION: Convert string to lowercase
518 *
519 * NOTE: This is not a POSIX function, so it appears here, not in utclib.c
520 *
521 ******************************************************************************/
522
523void
524AcpiUtStrlwr (
525    char                    *SrcString)
526{
527    char                    *String;
528
529
530    ACPI_FUNCTION_ENTRY ();
531
532
533    if (!SrcString)
534    {
535        return;
536    }
537
538    /* Walk entire string, lowercasing the letters */
539
540    for (String = SrcString; *String; String++)
541    {
542        *String = (char) ACPI_TOLOWER (*String);
543    }
544
545    return;
546}
547#endif
548
549
550/*******************************************************************************
551 *
552 * FUNCTION:    AcpiUtPrintString
553 *
554 * PARAMETERS:  String          - Null terminated ASCII string
555 *              MaxLength       - Maximum output length
556 *
557 * RETURN:      None
558 *
559 * DESCRIPTION: Dump an ASCII string with support for ACPI-defined escape
560 *              sequences.
561 *
562 ******************************************************************************/
563
564void
565AcpiUtPrintString (
566    char                    *String,
567    UINT8                   MaxLength)
568{
569    UINT32                  i;
570
571
572    if (!String)
573    {
574        AcpiOsPrintf ("<\"NULL STRING PTR\">");
575        return;
576    }
577
578    AcpiOsPrintf ("\"");
579    for (i = 0; String[i] && (i < MaxLength); i++)
580    {
581        /* Escape sequences */
582
583        switch (String[i])
584        {
585        case 0x07:
586            AcpiOsPrintf ("\\a");       /* BELL */
587            break;
588
589        case 0x08:
590            AcpiOsPrintf ("\\b");       /* BACKSPACE */
591            break;
592
593        case 0x0C:
594            AcpiOsPrintf ("\\f");       /* FORMFEED */
595            break;
596
597        case 0x0A:
598            AcpiOsPrintf ("\\n");       /* LINEFEED */
599            break;
600
601        case 0x0D:
602            AcpiOsPrintf ("\\r");       /* CARRIAGE RETURN*/
603            break;
604
605        case 0x09:
606            AcpiOsPrintf ("\\t");       /* HORIZONTAL TAB */
607            break;
608
609        case 0x0B:
610            AcpiOsPrintf ("\\v");       /* VERTICAL TAB */
611            break;
612
613        case '\'':                      /* Single Quote */
614        case '\"':                      /* Double Quote */
615        case '\\':                      /* Backslash */
616            AcpiOsPrintf ("\\%c", (int) String[i]);
617            break;
618
619        default:
620
621            /* Check for printable character or hex escape */
622
623            if (ACPI_IS_PRINT (String[i]))
624            {
625                /* This is a normal character */
626
627                AcpiOsPrintf ("%c", (int) String[i]);
628            }
629            else
630            {
631                /* All others will be Hex escapes */
632
633                AcpiOsPrintf ("\\x%2.2X", (INT32) String[i]);
634            }
635            break;
636        }
637    }
638    AcpiOsPrintf ("\"");
639
640    if (i == MaxLength && String[i])
641    {
642        AcpiOsPrintf ("...");
643    }
644}
645
646
647/*******************************************************************************
648 *
649 * FUNCTION:    AcpiUtDwordByteSwap
650 *
651 * PARAMETERS:  Value           - Value to be converted
652 *
653 * RETURN:      UINT32 integer with bytes swapped
654 *
655 * DESCRIPTION: Convert a 32-bit value to big-endian (swap the bytes)
656 *
657 ******************************************************************************/
658
659UINT32
660AcpiUtDwordByteSwap (
661    UINT32                  Value)
662{
663    union
664    {
665        UINT32              Value;
666        UINT8               Bytes[4];
667    } Out;
668    union
669    {
670        UINT32              Value;
671        UINT8               Bytes[4];
672    } In;
673
674
675    ACPI_FUNCTION_ENTRY ();
676
677
678    In.Value = Value;
679
680    Out.Bytes[0] = In.Bytes[3];
681    Out.Bytes[1] = In.Bytes[2];
682    Out.Bytes[2] = In.Bytes[1];
683    Out.Bytes[3] = In.Bytes[0];
684
685    return (Out.Value);
686}
687
688
689/*******************************************************************************
690 *
691 * FUNCTION:    AcpiUtSetIntegerWidth
692 *
693 * PARAMETERS:  Revision            From DSDT header
694 *
695 * RETURN:      None
696 *
697 * DESCRIPTION: Set the global integer bit width based upon the revision
698 *              of the DSDT.  For Revision 1 and 0, Integers are 32 bits.
699 *              For Revision 2 and above, Integers are 64 bits.  Yes, this
700 *              makes a difference.
701 *
702 ******************************************************************************/
703
704void
705AcpiUtSetIntegerWidth (
706    UINT8                   Revision)
707{
708
709    if (Revision < 2)
710    {
711        /* 32-bit case */
712
713        AcpiGbl_IntegerBitWidth    = 32;
714        AcpiGbl_IntegerNybbleWidth = 8;
715        AcpiGbl_IntegerByteWidth   = 4;
716    }
717    else
718    {
719        /* 64-bit case (ACPI 2.0+) */
720
721        AcpiGbl_IntegerBitWidth    = 64;
722        AcpiGbl_IntegerNybbleWidth = 16;
723        AcpiGbl_IntegerByteWidth   = 8;
724    }
725}
726
727
728#ifdef ACPI_DEBUG_OUTPUT
729/*******************************************************************************
730 *
731 * FUNCTION:    AcpiUtDisplayInitPathname
732 *
733 * PARAMETERS:  Type                - Object type of the node
734 *              ObjHandle           - Handle whose pathname will be displayed
735 *              Path                - Additional path string to be appended.
736 *                                      (NULL if no extra path)
737 *
738 * RETURN:      ACPI_STATUS
739 *
740 * DESCRIPTION: Display full pathname of an object, DEBUG ONLY
741 *
742 ******************************************************************************/
743
744void
745AcpiUtDisplayInitPathname (
746    UINT8                   Type,
747    ACPI_NAMESPACE_NODE     *ObjHandle,
748    char                    *Path)
749{
750    ACPI_STATUS             Status;
751    ACPI_BUFFER             Buffer;
752
753
754    ACPI_FUNCTION_ENTRY ();
755
756
757    /* Only print the path if the appropriate debug level is enabled */
758
759    if (!(AcpiDbgLevel & ACPI_LV_INIT_NAMES))
760    {
761        return;
762    }
763
764    /* Get the full pathname to the node */
765
766    Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
767    Status = AcpiNsHandleToPathname (ObjHandle, &Buffer);
768    if (ACPI_FAILURE (Status))
769    {
770        return;
771    }
772
773    /* Print what we're doing */
774
775    switch (Type)
776    {
777    case ACPI_TYPE_METHOD:
778        AcpiOsPrintf ("Executing    ");
779        break;
780
781    default:
782        AcpiOsPrintf ("Initializing ");
783        break;
784    }
785
786    /* Print the object type and pathname */
787
788    AcpiOsPrintf ("%-12s  %s",
789        AcpiUtGetTypeName (Type), (char *) Buffer.Pointer);
790
791    /* Extra path is used to append names like _STA, _INI, etc. */
792
793    if (Path)
794    {
795        AcpiOsPrintf (".%s", Path);
796    }
797    AcpiOsPrintf ("\n");
798
799    ACPI_FREE (Buffer.Pointer);
800}
801#endif
802
803
804/*******************************************************************************
805 *
806 * FUNCTION:    AcpiUtValidAcpiChar
807 *
808 * PARAMETERS:  Char            - The character to be examined
809 *              Position        - Byte position (0-3)
810 *
811 * RETURN:      TRUE if the character is valid, FALSE otherwise
812 *
813 * DESCRIPTION: Check for a valid ACPI character. Must be one of:
814 *              1) Upper case alpha
815 *              2) numeric
816 *              3) underscore
817 *
818 *              We allow a '!' as the last character because of the ASF! table
819 *
820 ******************************************************************************/
821
822BOOLEAN
823AcpiUtValidAcpiChar (
824    char                    Character,
825    UINT32                  Position)
826{
827
828    if (!((Character >= 'A' && Character <= 'Z') ||
829          (Character >= '0' && Character <= '9') ||
830          (Character == '_')))
831    {
832        /* Allow a '!' in the last position */
833
834        if (Character == '!' && Position == 3)
835        {
836            return (TRUE);
837        }
838
839        return (FALSE);
840    }
841
842    return (TRUE);
843}
844
845
846/*******************************************************************************
847 *
848 * FUNCTION:    AcpiUtValidAcpiName
849 *
850 * PARAMETERS:  Name            - The name to be examined
851 *
852 * RETURN:      TRUE if the name is valid, FALSE otherwise
853 *
854 * DESCRIPTION: Check for a valid ACPI name.  Each character must be one of:
855 *              1) Upper case alpha
856 *              2) numeric
857 *              3) underscore
858 *
859 ******************************************************************************/
860
861BOOLEAN
862AcpiUtValidAcpiName (
863    UINT32                  Name)
864{
865    UINT32                  i;
866
867
868    ACPI_FUNCTION_ENTRY ();
869
870
871    for (i = 0; i < ACPI_NAME_SIZE; i++)
872    {
873        if (!AcpiUtValidAcpiChar ((ACPI_CAST_PTR (char, &Name))[i], i))
874        {
875            return (FALSE);
876        }
877    }
878
879    return (TRUE);
880}
881
882
883/*******************************************************************************
884 *
885 * FUNCTION:    AcpiUtRepairName
886 *
887 * PARAMETERS:  Name            - The ACPI name to be repaired
888 *
889 * RETURN:      Repaired version of the name
890 *
891 * DESCRIPTION: Repair an ACPI name: Change invalid characters to '*' and
892 *              return the new name. NOTE: the Name parameter must reside in
893 *              read/write memory, cannot be a const.
894 *
895 * An ACPI Name must consist of valid ACPI characters. We will repair the name
896 * if necessary because we don't want to abort because of this, but we want
897 * all namespace names to be printable. A warning message is appropriate.
898 *
899 * This issue came up because there are in fact machines that exhibit
900 * this problem, and we want to be able to enable ACPI support for them,
901 * even though there are a few bad names.
902 *
903 ******************************************************************************/
904
905void
906AcpiUtRepairName (
907    char                    *Name)
908{
909    UINT32                  i;
910    BOOLEAN                 FoundBadChar = FALSE;
911
912
913    ACPI_FUNCTION_NAME (UtRepairName);
914
915
916    /* Check each character in the name */
917
918    for (i = 0; i < ACPI_NAME_SIZE; i++)
919    {
920        if (AcpiUtValidAcpiChar (Name[i], i))
921        {
922            continue;
923        }
924
925        /*
926         * Replace a bad character with something printable, yet technically
927         * still invalid. This prevents any collisions with existing "good"
928         * names in the namespace.
929         */
930        Name[i] = '*';
931        FoundBadChar = TRUE;
932    }
933
934    if (FoundBadChar)
935    {
936        /* Report warning only if in strict mode or debug mode */
937
938        if (!AcpiGbl_EnableInterpreterSlack)
939        {
940            ACPI_WARNING ((AE_INFO,
941                "Found bad character(s) in name, repaired: [%4.4s]\n", Name));
942        }
943        else
944        {
945            ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
946                "Found bad character(s) in name, repaired: [%4.4s]\n", Name));
947        }
948    }
949}
950
951
952/*******************************************************************************
953 *
954 * FUNCTION:    AcpiUtStrtoul64
955 *
956 * PARAMETERS:  String          - Null terminated string
957 *              Base            - Radix of the string: 16 or ACPI_ANY_BASE;
958 *                                ACPI_ANY_BASE means 'in behalf of ToInteger'
959 *              RetInteger      - Where the converted integer is returned
960 *
961 * RETURN:      Status and Converted value
962 *
963 * DESCRIPTION: Convert a string into an unsigned value. Performs either a
964 *              32-bit or 64-bit conversion, depending on the current mode
965 *              of the interpreter.
966 *              NOTE: Does not support Octal strings, not needed.
967 *
968 ******************************************************************************/
969
970ACPI_STATUS
971AcpiUtStrtoul64 (
972    char                    *String,
973    UINT32                  Base,
974    UINT64                  *RetInteger)
975{
976    UINT32                  ThisDigit = 0;
977    UINT64                  ReturnValue = 0;
978    UINT64                  Quotient;
979    UINT64                  Dividend;
980    UINT32                  ToIntegerOp = (Base == ACPI_ANY_BASE);
981    UINT32                  Mode32 = (AcpiGbl_IntegerByteWidth == 4);
982    UINT8                   ValidDigits = 0;
983    UINT8                   SignOf0x = 0;
984    UINT8                   Term = 0;
985
986
987    ACPI_FUNCTION_TRACE_STR (UtStroul64, String);
988
989
990    switch (Base)
991    {
992    case ACPI_ANY_BASE:
993    case 16:
994        break;
995
996    default:
997        /* Invalid Base */
998        return_ACPI_STATUS (AE_BAD_PARAMETER);
999    }
1000
1001    if (!String)
1002    {
1003        goto ErrorExit;
1004    }
1005
1006    /* Skip over any white space in the buffer */
1007
1008    while ((*String) && (ACPI_IS_SPACE (*String) || *String == '\t'))
1009    {
1010        String++;
1011    }
1012
1013    if (ToIntegerOp)
1014    {
1015        /*
1016         * Base equal to ACPI_ANY_BASE means 'ToInteger operation case'.
1017         * We need to determine if it is decimal or hexadecimal.
1018         */
1019        if ((*String == '0') && (ACPI_TOLOWER (*(String + 1)) == 'x'))
1020        {
1021            SignOf0x = 1;
1022            Base = 16;
1023
1024            /* Skip over the leading '0x' */
1025            String += 2;
1026        }
1027        else
1028        {
1029            Base = 10;
1030        }
1031    }
1032
1033    /* Any string left? Check that '0x' is not followed by white space. */
1034
1035    if (!(*String) || ACPI_IS_SPACE (*String) || *String == '\t')
1036    {
1037        if (ToIntegerOp)
1038        {
1039            goto ErrorExit;
1040        }
1041        else
1042        {
1043            goto AllDone;
1044        }
1045    }
1046
1047    /*
1048     * Perform a 32-bit or 64-bit conversion, depending upon the current
1049     * execution mode of the interpreter
1050     */
1051    Dividend = (Mode32) ? ACPI_UINT32_MAX : ACPI_UINT64_MAX;
1052
1053    /* Main loop: convert the string to a 32- or 64-bit integer */
1054
1055    while (*String)
1056    {
1057        if (ACPI_IS_DIGIT (*String))
1058        {
1059            /* Convert ASCII 0-9 to Decimal value */
1060
1061            ThisDigit = ((UINT8) *String) - '0';
1062        }
1063        else if (Base == 10)
1064        {
1065            /* Digit is out of range; possible in ToInteger case only */
1066
1067            Term = 1;
1068        }
1069        else
1070        {
1071            ThisDigit = (UINT8) ACPI_TOUPPER (*String);
1072            if (ACPI_IS_XDIGIT ((char) ThisDigit))
1073            {
1074                /* Convert ASCII Hex char to value */
1075
1076                ThisDigit = ThisDigit - 'A' + 10;
1077            }
1078            else
1079            {
1080                Term = 1;
1081            }
1082        }
1083
1084        if (Term)
1085        {
1086            if (ToIntegerOp)
1087            {
1088                goto ErrorExit;
1089            }
1090            else
1091            {
1092                break;
1093            }
1094        }
1095        else if ((ValidDigits == 0) && (ThisDigit == 0) && !SignOf0x)
1096        {
1097            /* Skip zeros */
1098            String++;
1099            continue;
1100        }
1101
1102        ValidDigits++;
1103
1104        if (SignOf0x && ((ValidDigits > 16) || ((ValidDigits > 8) && Mode32)))
1105        {
1106            /*
1107             * This is ToInteger operation case.
1108             * No any restrictions for string-to-integer conversion,
1109             * see ACPI spec.
1110             */
1111            goto ErrorExit;
1112        }
1113
1114        /* Divide the digit into the correct position */
1115
1116        (void) AcpiUtShortDivide ((Dividend - (UINT64) ThisDigit),
1117                    Base, &Quotient, NULL);
1118
1119        if (ReturnValue > Quotient)
1120        {
1121            if (ToIntegerOp)
1122            {
1123                goto ErrorExit;
1124            }
1125            else
1126            {
1127                break;
1128            }
1129        }
1130
1131        ReturnValue *= Base;
1132        ReturnValue += ThisDigit;
1133        String++;
1134    }
1135
1136    /* All done, normal exit */
1137
1138AllDone:
1139
1140    ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Converted value: %8.8X%8.8X\n",
1141        ACPI_FORMAT_UINT64 (ReturnValue)));
1142
1143    *RetInteger = ReturnValue;
1144    return_ACPI_STATUS (AE_OK);
1145
1146
1147ErrorExit:
1148    /* Base was set/validated above */
1149
1150    if (Base == 10)
1151    {
1152        return_ACPI_STATUS (AE_BAD_DECIMAL_CONSTANT);
1153    }
1154    else
1155    {
1156        return_ACPI_STATUS (AE_BAD_HEX_CONSTANT);
1157    }
1158}
1159
1160
1161/*******************************************************************************
1162 *
1163 * FUNCTION:    AcpiUtCreateUpdateStateAndPush
1164 *
1165 * PARAMETERS:  Object          - Object to be added to the new state
1166 *              Action          - Increment/Decrement
1167 *              StateList       - List the state will be added to
1168 *
1169 * RETURN:      Status
1170 *
1171 * DESCRIPTION: Create a new state and push it
1172 *
1173 ******************************************************************************/
1174
1175ACPI_STATUS
1176AcpiUtCreateUpdateStateAndPush (
1177    ACPI_OPERAND_OBJECT     *Object,
1178    UINT16                  Action,
1179    ACPI_GENERIC_STATE      **StateList)
1180{
1181    ACPI_GENERIC_STATE       *State;
1182
1183
1184    ACPI_FUNCTION_ENTRY ();
1185
1186
1187    /* Ignore null objects; these are expected */
1188
1189    if (!Object)
1190    {
1191        return (AE_OK);
1192    }
1193
1194    State = AcpiUtCreateUpdateState (Object, Action);
1195    if (!State)
1196    {
1197        return (AE_NO_MEMORY);
1198    }
1199
1200    AcpiUtPushGenericState (StateList, State);
1201    return (AE_OK);
1202}
1203
1204
1205/*******************************************************************************
1206 *
1207 * FUNCTION:    AcpiUtWalkPackageTree
1208 *
1209 * PARAMETERS:  SourceObject        - The package to walk
1210 *              TargetObject        - Target object (if package is being copied)
1211 *              WalkCallback        - Called once for each package element
1212 *              Context             - Passed to the callback function
1213 *
1214 * RETURN:      Status
1215 *
1216 * DESCRIPTION: Walk through a package
1217 *
1218 ******************************************************************************/
1219
1220ACPI_STATUS
1221AcpiUtWalkPackageTree (
1222    ACPI_OPERAND_OBJECT     *SourceObject,
1223    void                    *TargetObject,
1224    ACPI_PKG_CALLBACK       WalkCallback,
1225    void                    *Context)
1226{
1227    ACPI_STATUS             Status = AE_OK;
1228    ACPI_GENERIC_STATE      *StateList = NULL;
1229    ACPI_GENERIC_STATE      *State;
1230    UINT32                  ThisIndex;
1231    ACPI_OPERAND_OBJECT     *ThisSourceObj;
1232
1233
1234    ACPI_FUNCTION_TRACE (UtWalkPackageTree);
1235
1236
1237    State = AcpiUtCreatePkgState (SourceObject, TargetObject, 0);
1238    if (!State)
1239    {
1240        return_ACPI_STATUS (AE_NO_MEMORY);
1241    }
1242
1243    while (State)
1244    {
1245        /* Get one element of the package */
1246
1247        ThisIndex     = State->Pkg.Index;
1248        ThisSourceObj = (ACPI_OPERAND_OBJECT *)
1249                        State->Pkg.SourceObject->Package.Elements[ThisIndex];
1250
1251        /*
1252         * Check for:
1253         * 1) An uninitialized package element.  It is completely
1254         *    legal to declare a package and leave it uninitialized
1255         * 2) Not an internal object - can be a namespace node instead
1256         * 3) Any type other than a package.  Packages are handled in else
1257         *    case below.
1258         */
1259        if ((!ThisSourceObj) ||
1260            (ACPI_GET_DESCRIPTOR_TYPE (ThisSourceObj) != ACPI_DESC_TYPE_OPERAND) ||
1261            (ThisSourceObj->Common.Type != ACPI_TYPE_PACKAGE))
1262        {
1263            Status = WalkCallback (ACPI_COPY_TYPE_SIMPLE, ThisSourceObj,
1264                                    State, Context);
1265            if (ACPI_FAILURE (Status))
1266            {
1267                return_ACPI_STATUS (Status);
1268            }
1269
1270            State->Pkg.Index++;
1271            while (State->Pkg.Index >= State->Pkg.SourceObject->Package.Count)
1272            {
1273                /*
1274                 * We've handled all of the objects at this level,  This means
1275                 * that we have just completed a package.  That package may
1276                 * have contained one or more packages itself.
1277                 *
1278                 * Delete this state and pop the previous state (package).
1279                 */
1280                AcpiUtDeleteGenericState (State);
1281                State = AcpiUtPopGenericState (&StateList);
1282
1283                /* Finished when there are no more states */
1284
1285                if (!State)
1286                {
1287                    /*
1288                     * We have handled all of the objects in the top level
1289                     * package just add the length of the package objects
1290                     * and exit
1291                     */
1292                    return_ACPI_STATUS (AE_OK);
1293                }
1294
1295                /*
1296                 * Go back up a level and move the index past the just
1297                 * completed package object.
1298                 */
1299                State->Pkg.Index++;
1300            }
1301        }
1302        else
1303        {
1304            /* This is a subobject of type package */
1305
1306            Status = WalkCallback (ACPI_COPY_TYPE_PACKAGE, ThisSourceObj,
1307                                        State, Context);
1308            if (ACPI_FAILURE (Status))
1309            {
1310                return_ACPI_STATUS (Status);
1311            }
1312
1313            /*
1314             * Push the current state and create a new one
1315             * The callback above returned a new target package object.
1316             */
1317            AcpiUtPushGenericState (&StateList, State);
1318            State = AcpiUtCreatePkgState (ThisSourceObj,
1319                                            State->Pkg.ThisTargetObj, 0);
1320            if (!State)
1321            {
1322                /* Free any stacked Update State objects */
1323
1324                while (StateList)
1325                {
1326                    State = AcpiUtPopGenericState (&StateList);
1327                    AcpiUtDeleteGenericState (State);
1328                }
1329                return_ACPI_STATUS (AE_NO_MEMORY);
1330            }
1331        }
1332    }
1333
1334    /* We should never get here */
1335
1336    return_ACPI_STATUS (AE_AML_INTERNAL);
1337}
1338
1339
1340/*******************************************************************************
1341 *
1342 * FUNCTION:    AcpiError, AcpiException, AcpiWarning, AcpiInfo
1343 *
1344 * PARAMETERS:  ModuleName          - Caller's module name (for error output)
1345 *              LineNumber          - Caller's line number (for error output)
1346 *              Format              - Printf format string + additional args
1347 *
1348 * RETURN:      None
1349 *
1350 * DESCRIPTION: Print message with module/line/version info
1351 *
1352 ******************************************************************************/
1353
1354void  ACPI_INTERNAL_VAR_XFACE
1355AcpiError (
1356    const char              *ModuleName,
1357    UINT32                  LineNumber,
1358    const char              *Format,
1359    ...)
1360{
1361    va_list                 args;
1362
1363
1364    AcpiOsPrintf ("ACPI Error: ");
1365
1366    va_start (args, Format);
1367    AcpiOsVprintf (Format, args);
1368    ACPI_COMMON_MSG_SUFFIX;
1369    va_end (args);
1370}
1371
1372void  ACPI_INTERNAL_VAR_XFACE
1373AcpiException (
1374    const char              *ModuleName,
1375    UINT32                  LineNumber,
1376    ACPI_STATUS             Status,
1377    const char              *Format,
1378    ...)
1379{
1380    va_list                 args;
1381
1382
1383    AcpiOsPrintf ("ACPI Exception: %s, ", AcpiFormatException (Status));
1384
1385    va_start (args, Format);
1386    AcpiOsVprintf (Format, args);
1387    ACPI_COMMON_MSG_SUFFIX;
1388    va_end (args);
1389}
1390
1391void  ACPI_INTERNAL_VAR_XFACE
1392AcpiWarning (
1393    const char              *ModuleName,
1394    UINT32                  LineNumber,
1395    const char              *Format,
1396    ...)
1397{
1398    va_list                 args;
1399
1400
1401    AcpiOsPrintf ("ACPI Warning: ");
1402
1403    va_start (args, Format);
1404    AcpiOsVprintf (Format, args);
1405    ACPI_COMMON_MSG_SUFFIX;
1406    va_end (args);
1407}
1408
1409void  ACPI_INTERNAL_VAR_XFACE
1410AcpiInfo (
1411    const char              *ModuleName,
1412    UINT32                  LineNumber,
1413    const char              *Format,
1414    ...)
1415{
1416    va_list                 args;
1417
1418#ifdef _KERNEL
1419    /* Temporarily hide too verbose printfs. */
1420    if (!bootverbose)
1421        return;
1422#endif
1423
1424    AcpiOsPrintf ("ACPI: ");
1425
1426    va_start (args, Format);
1427    AcpiOsVprintf (Format, args);
1428    AcpiOsPrintf ("\n");
1429    va_end (args);
1430}
1431
1432ACPI_EXPORT_SYMBOL (AcpiError)
1433ACPI_EXPORT_SYMBOL (AcpiException)
1434ACPI_EXPORT_SYMBOL (AcpiWarning)
1435ACPI_EXPORT_SYMBOL (AcpiInfo)
1436
1437
1438/*******************************************************************************
1439 *
1440 * FUNCTION:    AcpiUtPredefinedWarning
1441 *
1442 * PARAMETERS:  ModuleName      - Caller's module name (for error output)
1443 *              LineNumber      - Caller's line number (for error output)
1444 *              Pathname        - Full pathname to the node
1445 *              NodeFlags       - From Namespace node for the method/object
1446 *              Format          - Printf format string + additional args
1447 *
1448 * RETURN:      None
1449 *
1450 * DESCRIPTION: Warnings for the predefined validation module. Messages are
1451 *              only emitted the first time a problem with a particular
1452 *              method/object is detected. This prevents a flood of error
1453 *              messages for methods that are repeatedly evaluated.
1454 *
1455 ******************************************************************************/
1456
1457void  ACPI_INTERNAL_VAR_XFACE
1458AcpiUtPredefinedWarning (
1459    const char              *ModuleName,
1460    UINT32                  LineNumber,
1461    char                    *Pathname,
1462    UINT8                   NodeFlags,
1463    const char              *Format,
1464    ...)
1465{
1466    va_list                 args;
1467
1468
1469    /*
1470     * Warning messages for this method/object will be disabled after the
1471     * first time a validation fails or an object is successfully repaired.
1472     */
1473    if (NodeFlags & ANOBJ_EVALUATED)
1474    {
1475        return;
1476    }
1477
1478    AcpiOsPrintf ("ACPI Warning for %s: ", Pathname);
1479
1480    va_start (args, Format);
1481    AcpiOsVprintf (Format, args);
1482    ACPI_COMMON_MSG_SUFFIX;
1483    va_end (args);
1484}
1485
1486/*******************************************************************************
1487 *
1488 * FUNCTION:    AcpiUtPredefinedInfo
1489 *
1490 * PARAMETERS:  ModuleName      - Caller's module name (for error output)
1491 *              LineNumber      - Caller's line number (for error output)
1492 *              Pathname        - Full pathname to the node
1493 *              NodeFlags       - From Namespace node for the method/object
1494 *              Format          - Printf format string + additional args
1495 *
1496 * RETURN:      None
1497 *
1498 * DESCRIPTION: Info messages for the predefined validation module. Messages
1499 *              are only emitted the first time a problem with a particular
1500 *              method/object is detected. This prevents a flood of
1501 *              messages for methods that are repeatedly evaluated.
1502 *
1503 ******************************************************************************/
1504
1505void  ACPI_INTERNAL_VAR_XFACE
1506AcpiUtPredefinedInfo (
1507    const char              *ModuleName,
1508    UINT32                  LineNumber,
1509    char                    *Pathname,
1510    UINT8                   NodeFlags,
1511    const char              *Format,
1512    ...)
1513{
1514    va_list                 args;
1515
1516
1517    /*
1518     * Warning messages for this method/object will be disabled after the
1519     * first time a validation fails or an object is successfully repaired.
1520     */
1521    if (NodeFlags & ANOBJ_EVALUATED)
1522    {
1523        return;
1524    }
1525
1526    AcpiOsPrintf ("ACPI Info for %s: ", Pathname);
1527
1528    va_start (args, Format);
1529    AcpiOsVprintf (Format, args);
1530    ACPI_COMMON_MSG_SUFFIX;
1531    va_end (args);
1532}
1533