9822 want iasl
[unleashed.git] / usr / src / common / acpica / executer / exnames.c
blob817a011b2c987aba2b8a1e78ffa3010ea62d0dd7
1 /******************************************************************************
3 * Module Name: exnames - interpreter/scanner name load/execute
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acinterp.h"
47 #include "amlcode.h"
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME ("exnames")
52 /* Local prototypes */
54 static char *
55 AcpiExAllocateNameString (
56 UINT32 PrefixCount,
57 UINT32 NumNameSegs);
59 static ACPI_STATUS
60 AcpiExNameSegment (
61 UINT8 **InAmlAddress,
62 char *NameString);
65 /*******************************************************************************
67 * FUNCTION: AcpiExAllocateNameString
69 * PARAMETERS: PrefixCount - Count of parent levels. Special cases:
70 * (-1)==root, 0==none
71 * NumNameSegs - count of 4-character name segments
73 * RETURN: A pointer to the allocated string segment. This segment must
74 * be deleted by the caller.
76 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
77 * string is long enough, and set up prefix if any.
79 ******************************************************************************/
81 static char *
82 AcpiExAllocateNameString (
83 UINT32 PrefixCount,
84 UINT32 NumNameSegs)
86 char *TempPtr;
87 char *NameString;
88 UINT32 SizeNeeded;
90 ACPI_FUNCTION_TRACE (ExAllocateNameString);
94 * Allow room for all \ and ^ prefixes, all segments and a MultiNamePrefix.
95 * Also, one byte for the null terminator.
96 * This may actually be somewhat longer than needed.
98 if (PrefixCount == ACPI_UINT32_MAX)
100 /* Special case for root */
102 SizeNeeded = 1 + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
104 else
106 SizeNeeded = PrefixCount + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
110 * Allocate a buffer for the name.
111 * This buffer must be deleted by the caller!
113 NameString = ACPI_ALLOCATE (SizeNeeded);
114 if (!NameString)
116 ACPI_ERROR ((AE_INFO,
117 "Could not allocate size %u", SizeNeeded));
118 return_PTR (NULL);
121 TempPtr = NameString;
123 /* Set up Root or Parent prefixes if needed */
125 if (PrefixCount == ACPI_UINT32_MAX)
127 *TempPtr++ = AML_ROOT_PREFIX;
129 else
131 while (PrefixCount--)
133 *TempPtr++ = AML_PARENT_PREFIX;
138 /* Set up Dual or Multi prefixes if needed */
140 if (NumNameSegs > 2)
142 /* Set up multi prefixes */
144 *TempPtr++ = AML_MULTI_NAME_PREFIX_OP;
145 *TempPtr++ = (char) NumNameSegs;
147 else if (2 == NumNameSegs)
149 /* Set up dual prefixes */
151 *TempPtr++ = AML_DUAL_NAME_PREFIX;
155 * Terminate string following prefixes. AcpiExNameSegment() will
156 * append the segment(s)
158 *TempPtr = 0;
160 return_PTR (NameString);
164 /*******************************************************************************
166 * FUNCTION: AcpiExNameSegment
168 * PARAMETERS: InAmlAddress - Pointer to the name in the AML code
169 * NameString - Where to return the name. The name is appended
170 * to any existing string to form a namepath
172 * RETURN: Status
174 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
176 ******************************************************************************/
178 static ACPI_STATUS
179 AcpiExNameSegment (
180 UINT8 **InAmlAddress,
181 char *NameString)
183 char *AmlAddress = (void *) *InAmlAddress;
184 ACPI_STATUS Status = AE_OK;
185 UINT32 Index;
186 char CharBuf[5];
189 ACPI_FUNCTION_TRACE (ExNameSegment);
193 * If first character is a digit, then we know that we aren't looking
194 * at a valid name segment
196 CharBuf[0] = *AmlAddress;
198 if ('0' <= CharBuf[0] && CharBuf[0] <= '9')
200 ACPI_ERROR ((AE_INFO, "Invalid leading digit: %c", CharBuf[0]));
201 return_ACPI_STATUS (AE_CTRL_PENDING);
204 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
206 for (Index = 0;
207 (Index < ACPI_NAME_SIZE) && (AcpiUtValidNameChar (*AmlAddress, 0));
208 Index++)
210 CharBuf[Index] = *AmlAddress++;
211 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", CharBuf[Index]));
215 /* Valid name segment */
217 if (Index == 4)
219 /* Found 4 valid characters */
221 CharBuf[4] = '\0';
223 if (NameString)
225 strcat (NameString, CharBuf);
226 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
227 "Appended to - %s\n", NameString));
229 else
231 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
232 "No Name string - %s\n", CharBuf));
235 else if (Index == 0)
238 * First character was not a valid name character,
239 * so we are looking at something other than a name.
241 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
242 "Leading character is not alpha: %02Xh (not a name)\n",
243 CharBuf[0]));
244 Status = AE_CTRL_PENDING;
246 else
249 * Segment started with one or more valid characters, but fewer than
250 * the required 4
252 Status = AE_AML_BAD_NAME;
253 ACPI_ERROR ((AE_INFO,
254 "Bad character 0x%02x in name, at %p",
255 *AmlAddress, AmlAddress));
258 *InAmlAddress = ACPI_CAST_PTR (UINT8, AmlAddress);
259 return_ACPI_STATUS (Status);
263 /*******************************************************************************
265 * FUNCTION: AcpiExGetNameString
267 * PARAMETERS: DataType - Object type to be associated with this
268 * name
269 * InAmlAddress - Pointer to the namestring in the AML code
270 * OutNameString - Where the namestring is returned
271 * OutNameLength - Length of the returned string
273 * RETURN: Status, namestring and length
275 * DESCRIPTION: Extract a full namepath from the AML byte stream,
276 * including any prefixes.
278 ******************************************************************************/
280 ACPI_STATUS
281 AcpiExGetNameString (
282 ACPI_OBJECT_TYPE DataType,
283 UINT8 *InAmlAddress,
284 char **OutNameString,
285 UINT32 *OutNameLength)
287 ACPI_STATUS Status = AE_OK;
288 UINT8 *AmlAddress = InAmlAddress;
289 char *NameString = NULL;
290 UINT32 NumSegments;
291 UINT32 PrefixCount = 0;
292 BOOLEAN HasPrefix = FALSE;
295 ACPI_FUNCTION_TRACE_PTR (ExGetNameString, AmlAddress);
298 if (ACPI_TYPE_LOCAL_REGION_FIELD == DataType ||
299 ACPI_TYPE_LOCAL_BANK_FIELD == DataType ||
300 ACPI_TYPE_LOCAL_INDEX_FIELD == DataType)
302 /* Disallow prefixes for types associated with FieldUnit names */
304 NameString = AcpiExAllocateNameString (0, 1);
305 if (!NameString)
307 Status = AE_NO_MEMORY;
309 else
311 Status = AcpiExNameSegment (&AmlAddress, NameString);
314 else
317 * DataType is not a field name.
318 * Examine first character of name for root or parent prefix operators
320 switch (*AmlAddress)
322 case AML_ROOT_PREFIX:
324 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "RootPrefix(\\) at %p\n",
325 AmlAddress));
328 * Remember that we have a RootPrefix --
329 * see comment in AcpiExAllocateNameString()
331 AmlAddress++;
332 PrefixCount = ACPI_UINT32_MAX;
333 HasPrefix = TRUE;
334 break;
336 case AML_PARENT_PREFIX:
338 /* Increment past possibly multiple parent prefixes */
342 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "ParentPrefix (^) at %p\n",
343 AmlAddress));
345 AmlAddress++;
346 PrefixCount++;
348 } while (*AmlAddress == AML_PARENT_PREFIX);
350 HasPrefix = TRUE;
351 break;
353 default:
355 /* Not a prefix character */
357 break;
360 /* Examine first character of name for name segment prefix operator */
362 switch (*AmlAddress)
364 case AML_DUAL_NAME_PREFIX:
366 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "DualNamePrefix at %p\n",
367 AmlAddress));
369 AmlAddress++;
370 NameString = AcpiExAllocateNameString (PrefixCount, 2);
371 if (!NameString)
373 Status = AE_NO_MEMORY;
374 break;
377 /* Indicate that we processed a prefix */
379 HasPrefix = TRUE;
381 Status = AcpiExNameSegment (&AmlAddress, NameString);
382 if (ACPI_SUCCESS (Status))
384 Status = AcpiExNameSegment (&AmlAddress, NameString);
386 break;
388 case AML_MULTI_NAME_PREFIX_OP:
390 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "MultiNamePrefix at %p\n",
391 AmlAddress));
393 /* Fetch count of segments remaining in name path */
395 AmlAddress++;
396 NumSegments = *AmlAddress;
398 NameString = AcpiExAllocateNameString (
399 PrefixCount, NumSegments);
400 if (!NameString)
402 Status = AE_NO_MEMORY;
403 break;
406 /* Indicate that we processed a prefix */
408 AmlAddress++;
409 HasPrefix = TRUE;
411 while (NumSegments &&
412 (Status = AcpiExNameSegment (&AmlAddress, NameString)) ==
413 AE_OK)
415 NumSegments--;
418 break;
420 case 0:
422 /* NullName valid as of 8-12-98 ASL/AML Grammar Update */
424 if (PrefixCount == ACPI_UINT32_MAX)
426 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
427 "NameSeg is \"\\\" followed by NULL\n"));
430 /* Consume the NULL byte */
432 AmlAddress++;
433 NameString = AcpiExAllocateNameString (PrefixCount, 0);
434 if (!NameString)
436 Status = AE_NO_MEMORY;
437 break;
440 break;
442 default:
444 /* Name segment string */
446 NameString = AcpiExAllocateNameString (PrefixCount, 1);
447 if (!NameString)
449 Status = AE_NO_MEMORY;
450 break;
453 Status = AcpiExNameSegment (&AmlAddress, NameString);
454 break;
458 if (AE_CTRL_PENDING == Status && HasPrefix)
460 /* Ran out of segments after processing a prefix */
462 ACPI_ERROR ((AE_INFO,
463 "Malformed Name at %p", NameString));
464 Status = AE_AML_BAD_NAME;
467 if (ACPI_FAILURE (Status))
469 if (NameString)
471 ACPI_FREE (NameString);
473 return_ACPI_STATUS (Status);
476 *OutNameString = NameString;
477 *OutNameLength = (UINT32) (AmlAddress - InAmlAddress);
479 return_ACPI_STATUS (Status);