1 /******************************************************************************
3 * Module Name: exnames - interpreter/scanner name load/execute
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acinterp.h>
47 #include <contrib/dev/acpica/include/amlcode.h>
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME ("exnames")
52 /* Local prototypes */
55 AcpiExAllocateNameString (
65 /*******************************************************************************
67 * FUNCTION: AcpiExAllocateNameString
69 * PARAMETERS: PrefixCount - Count of parent levels. Special cases:
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 ******************************************************************************/
82 AcpiExAllocateNameString (
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;
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);
116 ACPI_ERROR ((AE_INFO,
117 "Could not allocate size %u", SizeNeeded));
121 TempPtr = NameString;
123 /* Set up Root or Parent prefixes if needed */
125 if (PrefixCount == ACPI_UINT32_MAX)
127 *TempPtr++ = AML_ROOT_PREFIX;
131 while (PrefixCount--)
133 *TempPtr++ = AML_PARENT_PREFIX;
138 /* Set up Dual or Multi prefixes if needed */
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)
160 return_PTR (NameString);
163 /*******************************************************************************
165 * FUNCTION: AcpiExNameSegment
167 * PARAMETERS: InAmlAddress - Pointer to the name in the AML code
168 * NameString - Where to return the name. The name is appended
169 * to any existing string to form a namepath
173 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
175 ******************************************************************************/
179 UINT8 **InAmlAddress,
182 char *AmlAddress = (void *) *InAmlAddress;
183 ACPI_STATUS Status = AE_OK;
188 ACPI_FUNCTION_TRACE (ExNameSegment);
192 * If first character is a digit, then we know that we aren't looking at a
195 CharBuf[0] = *AmlAddress;
197 if ('0' <= CharBuf[0] && CharBuf[0] <= '9')
199 ACPI_ERROR ((AE_INFO, "Invalid leading digit: %c", CharBuf[0]));
200 return_ACPI_STATUS (AE_CTRL_PENDING);
203 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
206 (Index < ACPI_NAME_SIZE) && (AcpiUtValidAcpiChar (*AmlAddress, 0));
209 CharBuf[Index] = *AmlAddress++;
210 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", CharBuf[Index]));
214 /* Valid name segment */
218 /* Found 4 valid characters */
224 ACPI_STRCAT (NameString, CharBuf);
225 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
226 "Appended to - %s\n", NameString));
230 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
231 "No Name string - %s\n", CharBuf));
237 * First character was not a valid name character,
238 * so we are looking at something other than a name.
240 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
241 "Leading character is not alpha: %02Xh (not a name)\n",
243 Status = AE_CTRL_PENDING;
248 * Segment started with one or more valid characters, but fewer than
251 Status = AE_AML_BAD_NAME;
252 ACPI_ERROR ((AE_INFO,
253 "Bad character 0x%02x in name, at %p",
254 *AmlAddress, AmlAddress));
257 *InAmlAddress = ACPI_CAST_PTR (UINT8, AmlAddress);
258 return_ACPI_STATUS (Status);
262 /*******************************************************************************
264 * FUNCTION: AcpiExGetNameString
266 * PARAMETERS: DataType - Object type to be associated with this
268 * InAmlAddress - Pointer to the namestring in the AML code
269 * OutNameString - Where the namestring is returned
270 * OutNameLength - Length of the returned string
272 * RETURN: Status, namestring and length
274 * DESCRIPTION: Extract a full namepath from the AML byte stream,
275 * including any prefixes.
277 ******************************************************************************/
280 AcpiExGetNameString (
281 ACPI_OBJECT_TYPE DataType,
283 char **OutNameString,
284 UINT32 *OutNameLength)
286 ACPI_STATUS Status = AE_OK;
287 UINT8 *AmlAddress = InAmlAddress;
288 char *NameString = NULL;
290 UINT32 PrefixCount = 0;
291 BOOLEAN HasPrefix = FALSE;
294 ACPI_FUNCTION_TRACE_PTR (ExGetNameString, AmlAddress);
297 if (ACPI_TYPE_LOCAL_REGION_FIELD == DataType ||
298 ACPI_TYPE_LOCAL_BANK_FIELD == DataType ||
299 ACPI_TYPE_LOCAL_INDEX_FIELD == DataType)
301 /* Disallow prefixes for types associated with FieldUnit names */
303 NameString = AcpiExAllocateNameString (0, 1);
306 Status = AE_NO_MEMORY;
310 Status = AcpiExNameSegment (&AmlAddress, NameString);
316 * DataType is not a field name.
317 * Examine first character of name for root or parent prefix operators
321 case AML_ROOT_PREFIX:
323 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "RootPrefix(\\) at %p\n",
327 * Remember that we have a RootPrefix --
328 * see comment in AcpiExAllocateNameString()
331 PrefixCount = ACPI_UINT32_MAX;
335 case AML_PARENT_PREFIX:
337 /* Increment past possibly multiple parent prefixes */
341 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "ParentPrefix (^) at %p\n",
347 } while (*AmlAddress == AML_PARENT_PREFIX);
354 /* Not a prefix character */
359 /* Examine first character of name for name segment prefix operator */
363 case AML_DUAL_NAME_PREFIX:
365 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "DualNamePrefix at %p\n",
369 NameString = AcpiExAllocateNameString (PrefixCount, 2);
372 Status = AE_NO_MEMORY;
376 /* Indicate that we processed a prefix */
380 Status = AcpiExNameSegment (&AmlAddress, NameString);
381 if (ACPI_SUCCESS (Status))
383 Status = AcpiExNameSegment (&AmlAddress, NameString);
387 case AML_MULTI_NAME_PREFIX_OP:
389 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "MultiNamePrefix at %p\n",
392 /* Fetch count of segments remaining in name path */
395 NumSegments = *AmlAddress;
397 NameString = AcpiExAllocateNameString (PrefixCount, NumSegments);
400 Status = AE_NO_MEMORY;
404 /* Indicate that we processed a prefix */
409 while (NumSegments &&
410 (Status = AcpiExNameSegment (&AmlAddress, NameString)) ==
420 /* NullName valid as of 8-12-98 ASL/AML Grammar Update */
422 if (PrefixCount == ACPI_UINT32_MAX)
424 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
425 "NameSeg is \"\\\" followed by NULL\n"));
428 /* Consume the NULL byte */
431 NameString = AcpiExAllocateNameString (PrefixCount, 0);
434 Status = AE_NO_MEMORY;
442 /* Name segment string */
444 NameString = AcpiExAllocateNameString (PrefixCount, 1);
447 Status = AE_NO_MEMORY;
451 Status = AcpiExNameSegment (&AmlAddress, NameString);
456 if (AE_CTRL_PENDING == Status && HasPrefix)
458 /* Ran out of segments after processing a prefix */
460 ACPI_ERROR ((AE_INFO,
461 "Malformed Name at %p", NameString));
462 Status = AE_AML_BAD_NAME;
465 if (ACPI_FAILURE (Status))
469 ACPI_FREE (NameString);
471 return_ACPI_STATUS (Status);
474 *OutNameString = NameString;
475 *OutNameLength = (UINT32) (AmlAddress - InAmlAddress);
477 return_ACPI_STATUS (Status);