2 /******************************************************************************
4 * Module Name: exnames - interpreter/scanner name load/execute
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2006, 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.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/amlcode.h>
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME("exnames")
52 /* Local prototypes */
53 static char *acpi_ex_allocate_name_string(u32 prefix_count
, u32 num_name_segs
);
56 acpi_ex_name_segment(u8
** in_aml_address
, char *name_string
);
58 /*******************************************************************************
60 * FUNCTION: acpi_ex_allocate_name_string
62 * PARAMETERS: prefix_count - Count of parent levels. Special cases:
64 * num_name_segs - count of 4-character name segments
66 * RETURN: A pointer to the allocated string segment. This segment must
67 * be deleted by the caller.
69 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
70 * string is long enough, and set up prefix if any.
72 ******************************************************************************/
74 static char *acpi_ex_allocate_name_string(u32 prefix_count
, u32 num_name_segs
)
80 ACPI_FUNCTION_TRACE("ex_allocate_name_string");
83 * Allow room for all \ and ^ prefixes, all segments and a multi_name_prefix.
84 * Also, one byte for the null terminator.
85 * This may actually be somewhat longer than needed.
87 if (prefix_count
== ACPI_UINT32_MAX
) {
88 /* Special case for root */
90 size_needed
= 1 + (ACPI_NAME_SIZE
* num_name_segs
) + 2 + 1;
93 prefix_count
+ (ACPI_NAME_SIZE
* num_name_segs
) + 2 + 1;
97 * Allocate a buffer for the name.
98 * This buffer must be deleted by the caller!
100 name_string
= ACPI_MEM_ALLOCATE(size_needed
);
103 "Could not allocate size %d", size_needed
));
107 temp_ptr
= name_string
;
109 /* Set up Root or Parent prefixes if needed */
111 if (prefix_count
== ACPI_UINT32_MAX
) {
112 *temp_ptr
++ = AML_ROOT_PREFIX
;
114 while (prefix_count
--) {
115 *temp_ptr
++ = AML_PARENT_PREFIX
;
119 /* Set up Dual or Multi prefixes if needed */
121 if (num_name_segs
> 2) {
122 /* Set up multi prefixes */
124 *temp_ptr
++ = AML_MULTI_NAME_PREFIX_OP
;
125 *temp_ptr
++ = (char)num_name_segs
;
126 } else if (2 == num_name_segs
) {
127 /* Set up dual prefixes */
129 *temp_ptr
++ = AML_DUAL_NAME_PREFIX
;
133 * Terminate string following prefixes. acpi_ex_name_segment() will
134 * append the segment(s)
138 return_PTR(name_string
);
141 /*******************************************************************************
143 * FUNCTION: acpi_ex_name_segment
145 * PARAMETERS: in_aml_address - Pointer to the name in the AML code
146 * name_string - Where to return the name. The name is appended
147 * to any existing string to form a namepath
151 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
153 ******************************************************************************/
155 static acpi_status
acpi_ex_name_segment(u8
** in_aml_address
, char *name_string
)
157 char *aml_address
= (void *)*in_aml_address
;
158 acpi_status status
= AE_OK
;
162 ACPI_FUNCTION_TRACE("ex_name_segment");
165 * If first character is a digit, then we know that we aren't looking at a
168 char_buf
[0] = *aml_address
;
170 if ('0' <= char_buf
[0] && char_buf
[0] <= '9') {
171 ACPI_ERROR((AE_INFO
, "Invalid leading digit: %c", char_buf
[0]));
172 return_ACPI_STATUS(AE_CTRL_PENDING
);
175 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
, "Bytes from stream:\n"));
178 (index
< ACPI_NAME_SIZE
)
179 && (acpi_ut_valid_acpi_character(*aml_address
)); index
++) {
180 char_buf
[index
] = *aml_address
++;
181 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
, "%c\n", char_buf
[index
]));
184 /* Valid name segment */
187 /* Found 4 valid characters */
192 ACPI_STRCAT(name_string
, char_buf
);
193 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
194 "Appended to - %s\n", name_string
));
196 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
197 "No Name string - %s\n", char_buf
));
199 } else if (index
== 0) {
201 * First character was not a valid name character,
202 * so we are looking at something other than a name.
204 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
205 "Leading character is not alpha: %02Xh (not a name)\n",
207 status
= AE_CTRL_PENDING
;
210 * Segment started with one or more valid characters, but fewer than
213 status
= AE_AML_BAD_NAME
;
215 "Bad character %02x in name, at %p",
216 *aml_address
, aml_address
));
219 *in_aml_address
= ACPI_CAST_PTR(u8
, aml_address
);
220 return_ACPI_STATUS(status
);
223 /*******************************************************************************
225 * FUNCTION: acpi_ex_get_name_string
227 * PARAMETERS: data_type - Object type to be associated with this
229 * in_aml_address - Pointer to the namestring in the AML code
230 * out_name_string - Where the namestring is returned
231 * out_name_length - Length of the returned string
233 * RETURN: Status, namestring and length
235 * DESCRIPTION: Extract a full namepath from the AML byte stream,
236 * including any prefixes.
238 ******************************************************************************/
241 acpi_ex_get_name_string(acpi_object_type data_type
,
243 char **out_name_string
, u32
* out_name_length
)
245 acpi_status status
= AE_OK
;
246 u8
*aml_address
= in_aml_address
;
247 char *name_string
= NULL
;
249 u32 prefix_count
= 0;
250 u8 has_prefix
= FALSE
;
252 ACPI_FUNCTION_TRACE_PTR("ex_get_name_string", aml_address
);
254 if (ACPI_TYPE_LOCAL_REGION_FIELD
== data_type
||
255 ACPI_TYPE_LOCAL_BANK_FIELD
== data_type
||
256 ACPI_TYPE_LOCAL_INDEX_FIELD
== data_type
) {
257 /* Disallow prefixes for types associated with field_unit names */
259 name_string
= acpi_ex_allocate_name_string(0, 1);
261 status
= AE_NO_MEMORY
;
264 acpi_ex_name_segment(&aml_address
, name_string
);
268 * data_type is not a field name.
269 * Examine first character of name for root or parent prefix operators
271 switch (*aml_address
) {
272 case AML_ROOT_PREFIX
:
274 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
275 "root_prefix(\\) at %p\n",
279 * Remember that we have a root_prefix --
280 * see comment in acpi_ex_allocate_name_string()
283 prefix_count
= ACPI_UINT32_MAX
;
287 case AML_PARENT_PREFIX
:
289 /* Increment past possibly multiple parent prefixes */
292 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
293 "parent_prefix (^) at %p\n",
299 } while (*aml_address
== AML_PARENT_PREFIX
);
306 /* Not a prefix character */
311 /* Examine first character of name for name segment prefix operator */
313 switch (*aml_address
) {
314 case AML_DUAL_NAME_PREFIX
:
316 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
317 "dual_name_prefix at %p\n",
322 acpi_ex_allocate_name_string(prefix_count
, 2);
324 status
= AE_NO_MEMORY
;
328 /* Indicate that we processed a prefix */
333 acpi_ex_name_segment(&aml_address
, name_string
);
334 if (ACPI_SUCCESS(status
)) {
336 acpi_ex_name_segment(&aml_address
,
341 case AML_MULTI_NAME_PREFIX_OP
:
343 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
344 "multi_name_prefix at %p\n",
347 /* Fetch count of segments remaining in name path */
350 num_segments
= *aml_address
;
353 acpi_ex_allocate_name_string(prefix_count
,
356 status
= AE_NO_MEMORY
;
360 /* Indicate that we processed a prefix */
365 while (num_segments
&&
367 acpi_ex_name_segment(&aml_address
,
368 name_string
)) == AE_OK
) {
376 /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
378 if (prefix_count
== ACPI_UINT32_MAX
) {
379 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
380 "name_seg is \"\\\" followed by NULL\n"));
383 /* Consume the NULL byte */
387 acpi_ex_allocate_name_string(prefix_count
, 0);
389 status
= AE_NO_MEMORY
;
397 /* Name segment string */
400 acpi_ex_allocate_name_string(prefix_count
, 1);
402 status
= AE_NO_MEMORY
;
407 acpi_ex_name_segment(&aml_address
, name_string
);
412 if (AE_CTRL_PENDING
== status
&& has_prefix
) {
413 /* Ran out of segments after processing a prefix */
415 ACPI_ERROR((AE_INFO
, "Malformed Name at %p", name_string
));
416 status
= AE_AML_BAD_NAME
;
419 if (ACPI_FAILURE(status
)) {
421 ACPI_MEM_FREE(name_string
);
423 return_ACPI_STATUS(status
);
426 *out_name_string
= name_string
;
427 *out_name_length
= (u32
) (aml_address
- in_aml_address
);
429 return_ACPI_STATUS(status
);