Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / executer / exnames.c
blobac12e1ac56e602d599828b97a7d2aed896abddb0
1 /******************************************************************************
3 * Module Name: exnames - interpreter/scanner name load/execute
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, 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 #define __EXNAMES_C__
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "acinterp.h"
49 #include "amlcode.h"
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exnames")
54 /* Local prototypes */
56 static char *
57 AcpiExAllocateNameString (
58 UINT32 PrefixCount,
59 UINT32 NumNameSegs);
61 static ACPI_STATUS
62 AcpiExNameSegment (
63 UINT8 **InAmlAddress,
64 char *NameString);
67 /*******************************************************************************
69 * FUNCTION: AcpiExAllocateNameString
71 * PARAMETERS: PrefixCount - Count of parent levels. Special cases:
72 * (-1)==root, 0==none
73 * NumNameSegs - count of 4-character name segments
75 * RETURN: A pointer to the allocated string segment. This segment must
76 * be deleted by the caller.
78 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
79 * string is long enough, and set up prefix if any.
81 ******************************************************************************/
83 static char *
84 AcpiExAllocateNameString (
85 UINT32 PrefixCount,
86 UINT32 NumNameSegs)
88 char *TempPtr;
89 char *NameString;
90 UINT32 SizeNeeded;
92 ACPI_FUNCTION_TRACE (ExAllocateNameString);
96 * Allow room for all \ and ^ prefixes, all segments and a MultiNamePrefix.
97 * Also, one byte for the null terminator.
98 * This may actually be somewhat longer than needed.
100 if (PrefixCount == ACPI_UINT32_MAX)
102 /* Special case for root */
104 SizeNeeded = 1 + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
106 else
108 SizeNeeded = PrefixCount + (ACPI_NAME_SIZE * NumNameSegs) + 2 + 1;
112 * Allocate a buffer for the name.
113 * This buffer must be deleted by the caller!
115 NameString = ACPI_ALLOCATE (SizeNeeded);
116 if (!NameString)
118 ACPI_ERROR ((AE_INFO,
119 "Could not allocate size %u", SizeNeeded));
120 return_PTR (NULL);
123 TempPtr = NameString;
125 /* Set up Root or Parent prefixes if needed */
127 if (PrefixCount == ACPI_UINT32_MAX)
129 *TempPtr++ = AML_ROOT_PREFIX;
131 else
133 while (PrefixCount--)
135 *TempPtr++ = AML_PARENT_PREFIX;
140 /* Set up Dual or Multi prefixes if needed */
142 if (NumNameSegs > 2)
144 /* Set up multi prefixes */
146 *TempPtr++ = AML_MULTI_NAME_PREFIX_OP;
147 *TempPtr++ = (char) NumNameSegs;
149 else if (2 == NumNameSegs)
151 /* Set up dual prefixes */
153 *TempPtr++ = AML_DUAL_NAME_PREFIX;
157 * Terminate string following prefixes. AcpiExNameSegment() will
158 * append the segment(s)
160 *TempPtr = 0;
162 return_PTR (NameString);
165 /*******************************************************************************
167 * FUNCTION: AcpiExNameSegment
169 * PARAMETERS: InAmlAddress - Pointer to the name in the AML code
170 * NameString - Where to return the name. The name is appended
171 * to any existing string to form a namepath
173 * RETURN: Status
175 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
177 ******************************************************************************/
179 static ACPI_STATUS
180 AcpiExNameSegment (
181 UINT8 **InAmlAddress,
182 char *NameString)
184 char *AmlAddress = (void *) *InAmlAddress;
185 ACPI_STATUS Status = AE_OK;
186 UINT32 Index;
187 char CharBuf[5];
190 ACPI_FUNCTION_TRACE (ExNameSegment);
194 * If first character is a digit, then we know that we aren't looking at a
195 * valid name segment
197 CharBuf[0] = *AmlAddress;
199 if ('0' <= CharBuf[0] && CharBuf[0] <= '9')
201 ACPI_ERROR ((AE_INFO, "Invalid leading digit: %c", CharBuf[0]));
202 return_ACPI_STATUS (AE_CTRL_PENDING);
205 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
207 for (Index = 0;
208 (Index < ACPI_NAME_SIZE) && (AcpiUtValidAcpiChar (*AmlAddress, 0));
209 Index++)
211 CharBuf[Index] = *AmlAddress++;
212 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", CharBuf[Index]));
216 /* Valid name segment */
218 if (Index == 4)
220 /* Found 4 valid characters */
222 CharBuf[4] = '\0';
224 if (NameString)
226 ACPI_STRCAT (NameString, CharBuf);
227 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
228 "Appended to - %s\n", NameString));
230 else
232 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
233 "No Name string - %s\n", CharBuf));
236 else if (Index == 0)
239 * First character was not a valid name character,
240 * so we are looking at something other than a name.
242 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
243 "Leading character is not alpha: %02Xh (not a name)\n",
244 CharBuf[0]));
245 Status = AE_CTRL_PENDING;
247 else
250 * Segment started with one or more valid characters, but fewer than
251 * the required 4
253 Status = AE_AML_BAD_NAME;
254 ACPI_ERROR ((AE_INFO,
255 "Bad character 0x%02x in name, at %p",
256 *AmlAddress, AmlAddress));
259 *InAmlAddress = ACPI_CAST_PTR (UINT8, AmlAddress);
260 return_ACPI_STATUS (Status);
264 /*******************************************************************************
266 * FUNCTION: AcpiExGetNameString
268 * PARAMETERS: DataType - Object type to be associated with this
269 * name
270 * InAmlAddress - Pointer to the namestring in the AML code
271 * OutNameString - Where the namestring is returned
272 * OutNameLength - Length of the returned string
274 * RETURN: Status, namestring and length
276 * DESCRIPTION: Extract a full namepath from the AML byte stream,
277 * including any prefixes.
279 ******************************************************************************/
281 ACPI_STATUS
282 AcpiExGetNameString (
283 ACPI_OBJECT_TYPE DataType,
284 UINT8 *InAmlAddress,
285 char **OutNameString,
286 UINT32 *OutNameLength)
288 ACPI_STATUS Status = AE_OK;
289 UINT8 *AmlAddress = InAmlAddress;
290 char *NameString = NULL;
291 UINT32 NumSegments;
292 UINT32 PrefixCount = 0;
293 BOOLEAN HasPrefix = FALSE;
296 ACPI_FUNCTION_TRACE_PTR (ExGetNameString, AmlAddress);
299 if (ACPI_TYPE_LOCAL_REGION_FIELD == DataType ||
300 ACPI_TYPE_LOCAL_BANK_FIELD == DataType ||
301 ACPI_TYPE_LOCAL_INDEX_FIELD == DataType)
303 /* Disallow prefixes for types associated with FieldUnit names */
305 NameString = AcpiExAllocateNameString (0, 1);
306 if (!NameString)
308 Status = AE_NO_MEMORY;
310 else
312 Status = AcpiExNameSegment (&AmlAddress, NameString);
315 else
318 * DataType is not a field name.
319 * Examine first character of name for root or parent prefix operators
321 switch (*AmlAddress)
323 case AML_ROOT_PREFIX:
325 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "RootPrefix(\\) at %p\n",
326 AmlAddress));
329 * Remember that we have a RootPrefix --
330 * see comment in AcpiExAllocateNameString()
332 AmlAddress++;
333 PrefixCount = ACPI_UINT32_MAX;
334 HasPrefix = TRUE;
335 break;
337 case AML_PARENT_PREFIX:
339 /* Increment past possibly multiple parent prefixes */
343 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "ParentPrefix (^) at %p\n",
344 AmlAddress));
346 AmlAddress++;
347 PrefixCount++;
349 } while (*AmlAddress == AML_PARENT_PREFIX);
351 HasPrefix = TRUE;
352 break;
354 default:
356 /* Not a prefix character */
358 break;
361 /* Examine first character of name for name segment prefix operator */
363 switch (*AmlAddress)
365 case AML_DUAL_NAME_PREFIX:
367 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "DualNamePrefix at %p\n",
368 AmlAddress));
370 AmlAddress++;
371 NameString = AcpiExAllocateNameString (PrefixCount, 2);
372 if (!NameString)
374 Status = AE_NO_MEMORY;
375 break;
378 /* Indicate that we processed a prefix */
380 HasPrefix = TRUE;
382 Status = AcpiExNameSegment (&AmlAddress, NameString);
383 if (ACPI_SUCCESS (Status))
385 Status = AcpiExNameSegment (&AmlAddress, NameString);
387 break;
389 case AML_MULTI_NAME_PREFIX_OP:
391 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "MultiNamePrefix at %p\n",
392 AmlAddress));
394 /* Fetch count of segments remaining in name path */
396 AmlAddress++;
397 NumSegments = *AmlAddress;
399 NameString = AcpiExAllocateNameString (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);