2 /******************************************************************************
4 * Module Name: exnames - interpreter/scanner name load/execute
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, R. Byron Moore
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.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME ("exnames")
53 /* Local prototypes */
56 acpi_ex_allocate_name_string (
61 acpi_ex_name_segment (
66 /*******************************************************************************
68 * FUNCTION: acpi_ex_allocate_name_string
70 * PARAMETERS: prefix_count - Count of parent levels. Special cases:
72 * num_name_segs - count of 4-character name segments
74 * RETURN: A pointer to the allocated string segment. This segment must
75 * be deleted by the caller.
77 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
78 * string is long enough, and set up prefix if any.
80 ******************************************************************************/
83 acpi_ex_allocate_name_string (
91 ACPI_FUNCTION_TRACE ("ex_allocate_name_string");
95 * Allow room for all \ and ^ prefixes, all segments and a multi_name_prefix.
96 * Also, one byte for the null terminator.
97 * This may actually be somewhat longer than needed.
99 if (prefix_count == ACPI_UINT32_MAX) {
100 /* Special case for root */
102 size_needed = 1 + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
105 size_needed = prefix_count + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
109 * Allocate a buffer for the name.
110 * This buffer must be deleted by the caller!
112 name_string = ACPI_MEM_ALLOCATE (size_needed);
115 "ex_allocate_name_string: Could not allocate size %d\n", size_needed));
119 temp_ptr = name_string;
121 /* Set up Root or Parent prefixes if needed */
123 if (prefix_count == ACPI_UINT32_MAX) {
124 *temp_ptr++ = AML_ROOT_PREFIX;
127 while (prefix_count--) {
128 *temp_ptr++ = AML_PARENT_PREFIX;
133 /* Set up Dual or Multi prefixes if needed */
135 if (num_name_segs > 2) {
136 /* Set up multi prefixes */
138 *temp_ptr++ = AML_MULTI_NAME_PREFIX_OP;
139 *temp_ptr++ = (char) num_name_segs;
141 else if (2 == num_name_segs) {
142 /* Set up dual prefixes */
144 *temp_ptr++ = AML_DUAL_NAME_PREFIX;
148 * Terminate string following prefixes. acpi_ex_name_segment() will
149 * append the segment(s)
153 return_PTR (name_string);
156 /*******************************************************************************
158 * FUNCTION: acpi_ex_name_segment
160 * PARAMETERS: in_aml_address - Pointer to the name in the AML code
161 * name_string - Where to return the name. The name is appended
162 * to any existing string to form a namepath
166 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
168 ******************************************************************************/
171 acpi_ex_name_segment (
175 char *aml_address = (void *) *in_aml_address;
176 acpi_status status = AE_OK;
181 ACPI_FUNCTION_TRACE ("ex_name_segment");
185 * If first character is a digit, then we know that we aren't looking at a
188 char_buf[0] = *aml_address;
190 if ('0' <= char_buf[0] && char_buf[0] <= '9') {
191 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "leading digit: %c\n", char_buf[0]));
192 return_ACPI_STATUS (AE_CTRL_PENDING);
195 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Bytes from stream:\n"));
198 (index < ACPI_NAME_SIZE) && (acpi_ut_valid_acpi_character (*aml_address));
200 char_buf[index] = *aml_address++;
201 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "%c\n", char_buf[index]));
205 /* Valid name segment */
208 /* Found 4 valid characters */
213 ACPI_STRCAT (name_string, char_buf);
214 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
215 "Appended to - %s \n", name_string));
218 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
219 "No Name string - %s \n", char_buf));
222 else if (index == 0) {
224 * First character was not a valid name character,
225 * so we are looking at something other than a name.
227 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
228 "Leading character is not alpha: %02Xh (not a name)\n",
230 status = AE_CTRL_PENDING;
234 * Segment started with one or more valid characters, but fewer than
237 status = AE_AML_BAD_NAME;
238 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
239 "Bad character %02x in name, at %p\n",
240 *aml_address, aml_address));
243 *in_aml_address = (u8 *) aml_address;
244 return_ACPI_STATUS (status);
248 /*******************************************************************************
250 * FUNCTION: acpi_ex_get_name_string
252 * PARAMETERS: data_type - Object type to be associated with this
254 * in_aml_address - Pointer to the namestring in the AML code
255 * out_name_string - Where the namestring is returned
256 * out_name_length - Length of the returned string
258 * RETURN: Status, namestring and length
260 * DESCRIPTION: Extract a full namepath from the AML byte stream,
261 * including any prefixes.
263 ******************************************************************************/
266 acpi_ex_get_name_string (
267 acpi_object_type data_type,
269 char **out_name_string,
270 u32 *out_name_length)
272 acpi_status status = AE_OK;
273 u8 *aml_address = in_aml_address;
274 char *name_string = NULL;
276 u32 prefix_count = 0;
277 u8 has_prefix = FALSE;
280 ACPI_FUNCTION_TRACE_PTR ("ex_get_name_string", aml_address);
283 if (ACPI_TYPE_LOCAL_REGION_FIELD == data_type ||
284 ACPI_TYPE_LOCAL_BANK_FIELD == data_type ||
285 ACPI_TYPE_LOCAL_INDEX_FIELD == data_type) {
286 /* Disallow prefixes for types associated with field_unit names */
288 name_string = acpi_ex_allocate_name_string (0, 1);
290 status = AE_NO_MEMORY;
293 status = acpi_ex_name_segment (&aml_address, name_string);
298 * data_type is not a field name.
299 * Examine first character of name for root or parent prefix operators
301 switch (*aml_address) {
302 case AML_ROOT_PREFIX:
304 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "root_prefix(\\) at %p\n",
308 * Remember that we have a root_prefix --
309 * see comment in acpi_ex_allocate_name_string()
312 prefix_count = ACPI_UINT32_MAX;
317 case AML_PARENT_PREFIX:
319 /* Increment past possibly multiple parent prefixes */
322 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "parent_prefix (^) at %p\n",
328 } while (*aml_address == AML_PARENT_PREFIX);
336 /* Not a prefix character */
341 /* Examine first character of name for name segment prefix operator */
343 switch (*aml_address) {
344 case AML_DUAL_NAME_PREFIX:
346 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "dual_name_prefix at %p\n",
350 name_string = acpi_ex_allocate_name_string (prefix_count, 2);
352 status = AE_NO_MEMORY;
356 /* Indicate that we processed a prefix */
360 status = acpi_ex_name_segment (&aml_address, name_string);
361 if (ACPI_SUCCESS (status)) {
362 status = acpi_ex_name_segment (&aml_address, name_string);
367 case AML_MULTI_NAME_PREFIX_OP:
369 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "multi_name_prefix at %p\n",
372 /* Fetch count of segments remaining in name path */
375 num_segments = *aml_address;
377 name_string = acpi_ex_allocate_name_string (prefix_count, num_segments);
379 status = AE_NO_MEMORY;
383 /* Indicate that we processed a prefix */
388 while (num_segments &&
389 (status = acpi_ex_name_segment (&aml_address, name_string)) ==
399 /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
401 if (prefix_count == ACPI_UINT32_MAX) {
402 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
403 "name_seg is \"\\\" followed by NULL\n"));
406 /* Consume the NULL byte */
409 name_string = acpi_ex_allocate_name_string (prefix_count, 0);
411 status = AE_NO_MEMORY;
420 /* Name segment string */
422 name_string = acpi_ex_allocate_name_string (prefix_count, 1);
424 status = AE_NO_MEMORY;
428 status = acpi_ex_name_segment (&aml_address, name_string);
433 if (AE_CTRL_PENDING == status && has_prefix) {
434 /* Ran out of segments after processing a prefix */
437 ("ex_do_name: Malformed Name at %p\n", name_string));
438 status = AE_AML_BAD_NAME;
441 *out_name_string = name_string;
442 *out_name_length = (u32) (aml_address - in_aml_address);
444 return_ACPI_STATUS (status);