2 /******************************************************************************
4 * Module Name: exnames - interpreter/scanner name load/execute
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exnames")
55 /* Local prototypes */
58 AcpiExAllocateNameString (
68 /*******************************************************************************
70 * FUNCTION: AcpiExAllocateNameString
72 * PARAMETERS: PrefixCount - Count of parent levels. Special cases:
74 * NumNameSegs - count of 4-character name segments
76 * RETURN: A pointer to the allocated string segment. This segment must
77 * be deleted by the caller.
79 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
80 * string is long enough, and set up prefix if any.
82 ******************************************************************************/
85 AcpiExAllocateNameString (
93 ACPI_FUNCTION_TRACE (ExAllocateNameString);
97 * Allow room for all \ and ^ prefixes, all segments and a MultiNamePrefix.
98 * Also, one byte for the null terminator.
99 * This may actually be somewhat longer than needed.
101 if (PrefixCount == ACPI_UINT32_MAX)
103 /* Special case for root */
105 SizeNeeded = 1 + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
109 SizeNeeded = PrefixCount + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
113 * Allocate a buffer for the name.
114 * This buffer must be deleted by the caller!
116 NameString = ACPI_ALLOCATE (SizeNeeded);
119 ACPI_ERROR ((AE_INFO,
120 "Could not allocate size %u", SizeNeeded));
124 TempPtr = NameString;
126 /* Set up Root or Parent prefixes if needed */
128 if (PrefixCount == ACPI_UINT32_MAX)
130 *TempPtr++ = AML_ROOT_PREFIX;
134 while (PrefixCount--)
136 *TempPtr++ = AML_PARENT_PREFIX;
141 /* Set up Dual or Multi prefixes if needed */
145 /* Set up multi prefixes */
147 *TempPtr++ = AML_MULTI_NAME_PREFIX_OP;
148 *TempPtr++ = (char) NumNameSegs;
150 else if (2 == NumNameSegs)
152 /* Set up dual prefixes */
154 *TempPtr++ = AML_DUAL_NAME_PREFIX;
158 * Terminate string following prefixes. AcpiExNameSegment() will
159 * append the segment(s)
163 return_PTR (NameString);
166 /*******************************************************************************
168 * FUNCTION: AcpiExNameSegment
170 * PARAMETERS: InAmlAddress - Pointer to the name in the AML code
171 * NameString - Where to return the name. The name is appended
172 * to any existing string to form a namepath
176 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
178 ******************************************************************************/
182 UINT8 **InAmlAddress,
185 char *AmlAddress = (void *) *InAmlAddress;
186 ACPI_STATUS Status = AE_OK;
191 ACPI_FUNCTION_TRACE (ExNameSegment);
195 * If first character is a digit, then we know that we aren't looking at a
198 CharBuf[0] = *AmlAddress;
200 if ('0' <= CharBuf[0] && CharBuf[0] <= '9')
202 ACPI_ERROR ((AE_INFO, "Invalid leading digit: %c", CharBuf[0]));
203 return_ACPI_STATUS (AE_CTRL_PENDING);
206 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
209 (Index < ACPI_NAME_SIZE) && (AcpiUtValidAcpiChar (*AmlAddress, 0));
212 CharBuf[Index] = *AmlAddress++;
213 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", CharBuf[Index]));
217 /* Valid name segment */
221 /* Found 4 valid characters */
227 ACPI_STRCAT (NameString, CharBuf);
228 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
229 "Appended to - %s\n", NameString));
233 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
234 "No Name string - %s\n", CharBuf));
240 * First character was not a valid name character,
241 * so we are looking at something other than a name.
243 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
244 "Leading character is not alpha: %02Xh (not a name)\n",
246 Status = AE_CTRL_PENDING;
251 * Segment started with one or more valid characters, but fewer than
254 Status = AE_AML_BAD_NAME;
255 ACPI_ERROR ((AE_INFO,
256 "Bad character 0x%02x in name, at %p",
257 *AmlAddress, AmlAddress));
260 *InAmlAddress = ACPI_CAST_PTR (UINT8, AmlAddress);
261 return_ACPI_STATUS (Status);
265 /*******************************************************************************
267 * FUNCTION: AcpiExGetNameString
269 * PARAMETERS: DataType - Object type to be associated with this
271 * InAmlAddress - Pointer to the namestring in the AML code
272 * OutNameString - Where the namestring is returned
273 * OutNameLength - Length of the returned string
275 * RETURN: Status, namestring and length
277 * DESCRIPTION: Extract a full namepath from the AML byte stream,
278 * including any prefixes.
280 ******************************************************************************/
283 AcpiExGetNameString (
284 ACPI_OBJECT_TYPE DataType,
286 char **OutNameString,
287 UINT32 *OutNameLength)
289 ACPI_STATUS Status = AE_OK;
290 UINT8 *AmlAddress = InAmlAddress;
291 char *NameString = NULL;
293 UINT32 PrefixCount = 0;
294 BOOLEAN HasPrefix = FALSE;
297 ACPI_FUNCTION_TRACE_PTR (ExGetNameString, AmlAddress);
300 if (ACPI_TYPE_LOCAL_REGION_FIELD == DataType ||
301 ACPI_TYPE_LOCAL_BANK_FIELD == DataType ||
302 ACPI_TYPE_LOCAL_INDEX_FIELD == DataType)
304 /* Disallow prefixes for types associated with FieldUnit names */
306 NameString = AcpiExAllocateNameString (0, 1);
309 Status = AE_NO_MEMORY;
313 Status = AcpiExNameSegment (&AmlAddress, NameString);
319 * DataType is not a field name.
320 * Examine first character of name for root or parent prefix operators
324 case AML_ROOT_PREFIX:
326 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "RootPrefix(\\) at %p\n",
330 * Remember that we have a RootPrefix --
331 * see comment in AcpiExAllocateNameString()
334 PrefixCount = ACPI_UINT32_MAX;
339 case AML_PARENT_PREFIX:
341 /* Increment past possibly multiple parent prefixes */
345 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "ParentPrefix (^) at %p\n",
351 } while (*AmlAddress == AML_PARENT_PREFIX);
359 /* Not a prefix character */
364 /* Examine first character of name for name segment prefix operator */
368 case AML_DUAL_NAME_PREFIX:
370 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "DualNamePrefix at %p\n",
374 NameString = AcpiExAllocateNameString (PrefixCount, 2);
377 Status = AE_NO_MEMORY;
381 /* Indicate that we processed a prefix */
385 Status = AcpiExNameSegment (&AmlAddress, NameString);
386 if (ACPI_SUCCESS (Status))
388 Status = AcpiExNameSegment (&AmlAddress, NameString);
393 case AML_MULTI_NAME_PREFIX_OP:
395 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "MultiNamePrefix at %p\n",
398 /* Fetch count of segments remaining in name path */
401 NumSegments = *AmlAddress;
403 NameString = AcpiExAllocateNameString (PrefixCount, NumSegments);
406 Status = AE_NO_MEMORY;
410 /* Indicate that we processed a prefix */
415 while (NumSegments &&
416 (Status = AcpiExNameSegment (&AmlAddress, NameString)) ==
427 /* NullName valid as of 8-12-98 ASL/AML Grammar Update */
429 if (PrefixCount == ACPI_UINT32_MAX)
431 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
432 "NameSeg is \"\\\" followed by NULL\n"));
435 /* Consume the NULL byte */
438 NameString = AcpiExAllocateNameString (PrefixCount, 0);
441 Status = AE_NO_MEMORY;
450 /* Name segment string */
452 NameString = AcpiExAllocateNameString (PrefixCount, 1);
455 Status = AE_NO_MEMORY;
459 Status = AcpiExNameSegment (&AmlAddress, NameString);
464 if (AE_CTRL_PENDING == Status && HasPrefix)
466 /* Ran out of segments after processing a prefix */
468 ACPI_ERROR ((AE_INFO,
469 "Malformed Name at %p", NameString));
470 Status = AE_AML_BAD_NAME;
473 if (ACPI_FAILURE (Status))
477 ACPI_FREE (NameString);
479 return_ACPI_STATUS (Status);
482 *OutNameString = NameString;
483 *OutNameLength = (UINT32) (AmlAddress - InAmlAddress);
485 return_ACPI_STATUS (Status);