Merge from vendor branch LESS:
[dragonfly.git] / sys / contrib / dev / acpica / exnames.c
1
2 /******************************************************************************
3  *
4  * Module Name: exnames - interpreter/scanner name load/execute
5  *              $Revision: 94 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/exnames.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
118
119 #define __EXNAMES_C__
120
121 #include "acpi.h"
122 #include "acinterp.h"
123 #include "amlcode.h"
124
125 #define _COMPONENT          ACPI_EXECUTER
126         ACPI_MODULE_NAME    ("exnames")
127
128
129 /* AML Package Length encodings */
130
131 #define ACPI_AML_PACKAGE_TYPE1   0x40
132 #define ACPI_AML_PACKAGE_TYPE2   0x4000
133 #define ACPI_AML_PACKAGE_TYPE3   0x400000
134 #define ACPI_AML_PACKAGE_TYPE4   0x40000000
135
136
137 /*******************************************************************************
138  *
139  * FUNCTION:    AcpiExAllocateNameString
140  *
141  * PARAMETERS:  PrefixCount         - Count of parent levels. Special cases:
142  *                                    (-1) = root,  0 = none
143  *              NumNameSegs         - count of 4-character name segments
144  *
145  * RETURN:      A pointer to the allocated string segment.  This segment must
146  *              be deleted by the caller.
147  *
148  * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
149  *              string is long enough, and set up prefix if any.
150  *
151  ******************************************************************************/
152
153 char *
154 AcpiExAllocateNameString (
155     UINT32                  PrefixCount,
156     UINT32                  NumNameSegs)
157 {
158     char                    *TempPtr;
159     char                    *NameString;
160     UINT32                   SizeNeeded;
161
162     ACPI_FUNCTION_TRACE ("ExAllocateNameString");
163
164
165     /*
166      * Allow room for all \ and ^ prefixes, all segments, and a MultiNamePrefix.
167      * Also, one byte for the null terminator.
168      * This may actually be somewhat longer than needed.
169      */
170     if (PrefixCount == ACPI_UINT32_MAX)
171     {
172         /* Special case for root */
173
174         SizeNeeded = 1 + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
175     }
176     else
177     {
178         SizeNeeded = PrefixCount + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
179     }
180
181     /*
182      * Allocate a buffer for the name.
183      * This buffer must be deleted by the caller!
184      */
185     NameString = ACPI_MEM_ALLOCATE (SizeNeeded);
186     if (!NameString)
187     {
188         ACPI_REPORT_ERROR (("ExAllocateNameString: Could not allocate size %d\n", SizeNeeded));
189         return_PTR (NULL);
190     }
191
192     TempPtr = NameString;
193
194     /* Set up Root or Parent prefixes if needed */
195
196     if (PrefixCount == ACPI_UINT32_MAX)
197     {
198         *TempPtr++ = AML_ROOT_PREFIX;
199     }
200     else
201     {
202         while (PrefixCount--)
203         {
204             *TempPtr++ = AML_PARENT_PREFIX;
205         }
206     }
207
208
209     /* Set up Dual or Multi prefixes if needed */
210
211     if (NumNameSegs > 2)
212     {
213         /* Set up multi prefixes   */
214
215         *TempPtr++ = AML_MULTI_NAME_PREFIX_OP;
216         *TempPtr++ = (char) NumNameSegs;
217     }
218     else if (2 == NumNameSegs)
219     {
220         /* Set up dual prefixes */
221
222         *TempPtr++ = AML_DUAL_NAME_PREFIX;
223     }
224
225     /*
226      * Terminate string following prefixes. AcpiExNameSegment() will
227      * append the segment(s)
228      */
229     *TempPtr = 0;
230
231     return_PTR (NameString);
232 }
233
234 /*******************************************************************************
235  *
236  * FUNCTION:    AcpiExNameSegment
237  *
238  * PARAMETERS:  InterpreterMode     - Current running mode (load1/Load2/Exec)
239  *
240  * RETURN:      Status
241  *
242  * DESCRIPTION: Execute a name segment (4 bytes)
243  *
244  ******************************************************************************/
245
246 ACPI_STATUS
247 AcpiExNameSegment (
248     UINT8                   **InAmlAddress,
249     char                    *NameString)
250 {
251     char                    *AmlAddress = (void *) *InAmlAddress;
252     ACPI_STATUS             Status = AE_OK;
253     UINT32                  Index;
254     char                    CharBuf[5];
255
256
257     ACPI_FUNCTION_TRACE ("ExNameSegment");
258
259
260     /*
261      * If first character is a digit, then we know that we aren't looking at a
262      * valid name segment
263      */
264     CharBuf[0] = *AmlAddress;
265
266     if ('0' <= CharBuf[0] && CharBuf[0] <= '9')
267     {
268         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "leading digit: %c\n", CharBuf[0]));
269         return_ACPI_STATUS (AE_CTRL_PENDING);
270     }
271
272     ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
273
274     for (Index = 0;
275         (Index < ACPI_NAME_SIZE) && (AcpiUtValidAcpiCharacter (*AmlAddress));
276         Index++)
277     {
278         CharBuf[Index] = *AmlAddress++;
279         ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", CharBuf[Index]));
280     }
281
282
283     /* Valid name segment  */
284
285     if (Index == 4)
286     {
287         /* Found 4 valid characters */
288
289         CharBuf[4] = '\0';
290
291         if (NameString)
292         {
293             ACPI_STRCAT (NameString, CharBuf);
294             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
295                 "Appended to - %s \n", NameString));
296         }
297         else
298         {
299             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
300                 "No Name string - %s \n", CharBuf));
301         }
302     }
303     else if (Index == 0)
304     {
305         /*
306          * First character was not a valid name character,
307          * so we are looking at something other than a name.
308          */
309         ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
310             "Leading character is not alpha: %02Xh (not a name)\n",
311             CharBuf[0]));
312         Status = AE_CTRL_PENDING;
313     }
314     else
315     {
316         /* Segment started with one or more valid characters, but fewer than 4 */
317
318         Status = AE_AML_BAD_NAME;
319         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Bad character %02x in name, at %p\n",
320             *AmlAddress, AmlAddress));
321     }
322
323     *InAmlAddress = (UINT8 *) AmlAddress;
324     return_ACPI_STATUS (Status);
325 }
326
327
328 /*******************************************************************************
329  *
330  * FUNCTION:    AcpiExGetNameString
331  *
332  * PARAMETERS:  DataType            - Data type to be associated with this name
333  *
334  * RETURN:      Status
335  *
336  * DESCRIPTION: Get a name, including any prefixes.
337  *
338  ******************************************************************************/
339
340 ACPI_STATUS
341 AcpiExGetNameString (
342     ACPI_OBJECT_TYPE        DataType,
343     UINT8                   *InAmlAddress,
344     char                    **OutNameString,
345     UINT32                  *OutNameLength)
346 {
347     ACPI_STATUS             Status = AE_OK;
348     UINT8                   *AmlAddress = InAmlAddress;
349     char                    *NameString = NULL;
350     UINT32                  NumSegments;
351     UINT32                  PrefixCount = 0;
352     BOOLEAN                 HasPrefix = FALSE;
353
354
355     ACPI_FUNCTION_TRACE_PTR ("ExGetNameString", AmlAddress);
356
357
358     if (ACPI_TYPE_LOCAL_REGION_FIELD == DataType   ||
359         ACPI_TYPE_LOCAL_BANK_FIELD == DataType     ||
360         ACPI_TYPE_LOCAL_INDEX_FIELD == DataType)
361     {
362         /* Disallow prefixes for types associated with FieldUnit names */
363
364         NameString = AcpiExAllocateNameString (0, 1);
365         if (!NameString)
366         {
367             Status = AE_NO_MEMORY;
368         }
369         else
370         {
371             Status = AcpiExNameSegment (&AmlAddress, NameString);
372         }
373     }
374     else
375     {
376         /*
377          * DataType is not a field name.
378          * Examine first character of name for root or parent prefix operators
379          */
380         switch (*AmlAddress)
381         {
382         case AML_ROOT_PREFIX:
383
384             ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "RootPrefix(\\) at %p\n", AmlAddress));
385
386             /*
387              * Remember that we have a RootPrefix --
388              * see comment in AcpiExAllocateNameString()
389              */
390             AmlAddress++;
391             PrefixCount = ACPI_UINT32_MAX;
392             HasPrefix = TRUE;
393             break;
394
395
396         case AML_PARENT_PREFIX:
397
398             /* Increment past possibly multiple parent prefixes */
399
400             do
401             {
402                 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "ParentPrefix (^) at %p\n", AmlAddress));
403
404                 AmlAddress++;
405                 PrefixCount++;
406
407             } while (*AmlAddress == AML_PARENT_PREFIX);
408
409             HasPrefix = TRUE;
410             break;
411
412
413         default:
414
415             /* Not a prefix character */
416
417             break;
418         }
419
420
421         /* Examine first character of name for name segment prefix operator */
422
423         switch (*AmlAddress)
424         {
425         case AML_DUAL_NAME_PREFIX:
426
427             ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "DualNamePrefix at %p\n", AmlAddress));
428
429             AmlAddress++;
430             NameString = AcpiExAllocateNameString (PrefixCount, 2);
431             if (!NameString)
432             {
433                 Status = AE_NO_MEMORY;
434                 break;
435             }
436
437             /* Indicate that we processed a prefix */
438
439             HasPrefix = TRUE;
440
441             Status = AcpiExNameSegment (&AmlAddress, NameString);
442             if (ACPI_SUCCESS (Status))
443             {
444                 Status = AcpiExNameSegment (&AmlAddress, NameString);
445             }
446             break;
447
448
449         case AML_MULTI_NAME_PREFIX_OP:
450
451             ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "MultiNamePrefix at %p\n", AmlAddress));
452
453             /* Fetch count of segments remaining in name path */
454
455             AmlAddress++;
456             NumSegments = *AmlAddress;
457
458             NameString = AcpiExAllocateNameString (PrefixCount, NumSegments);
459             if (!NameString)
460             {
461                 Status = AE_NO_MEMORY;
462                 break;
463             }
464
465             /* Indicate that we processed a prefix */
466
467             AmlAddress++;
468             HasPrefix = TRUE;
469
470             while (NumSegments &&
471                     (Status = AcpiExNameSegment (&AmlAddress, NameString)) == AE_OK)
472             {
473                 NumSegments--;
474             }
475
476             break;
477
478
479         case 0:
480
481             /* NullName valid as of 8-12-98 ASL/AML Grammar Update */
482
483             if (PrefixCount == ACPI_UINT32_MAX)
484             {
485                 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "NameSeg is \"\\\" followed by NULL\n"));
486             }
487
488             /* Consume the NULL byte */
489
490             AmlAddress++;
491             NameString = AcpiExAllocateNameString (PrefixCount, 0);
492             if (!NameString)
493             {
494                 Status = AE_NO_MEMORY;
495                 break;
496             }
497
498             break;
499
500
501         default:
502
503             /* Name segment string */
504
505             NameString = AcpiExAllocateNameString (PrefixCount, 1);
506             if (!NameString)
507             {
508                 Status = AE_NO_MEMORY;
509                 break;
510             }
511
512             Status = AcpiExNameSegment (&AmlAddress, NameString);
513             break;
514         }
515     }
516
517     if (AE_CTRL_PENDING == Status && HasPrefix)
518     {
519         /* Ran out of segments after processing a prefix */
520
521         ACPI_REPORT_ERROR (
522             ("ExDoName: Malformed Name at %p\n", NameString));
523         Status = AE_AML_BAD_NAME;
524     }
525
526     *OutNameString = NameString;
527     *OutNameLength = (UINT32) (AmlAddress - InAmlAddress);
528
529     return_ACPI_STATUS (Status);
530 }
531
532