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 <acpi/acpi.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
);
55 static acpi_status
acpi_ex_name_segment(u8
**in_aml_address
, char *name_string
);
57 /*******************************************************************************
59 * FUNCTION: acpi_ex_allocate_name_string
61 * PARAMETERS: prefix_count - Count of parent levels. Special cases:
63 * num_name_segs - count of 4-character name segments
65 * RETURN: A pointer to the allocated string segment. This segment must
66 * be deleted by the caller.
68 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
69 * string is long enough, and set up prefix if any.
71 ******************************************************************************/
73 static char *acpi_ex_allocate_name_string(u32 prefix_count
, u32 num_name_segs
)
79 ACPI_FUNCTION_TRACE(ex_allocate_name_string
);
82 * Allow room for all \ and ^ prefixes, all segments and a multi_name_prefix.
83 * Also, one byte for the null terminator.
84 * This may actually be somewhat longer than needed.
86 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_ALLOCATE(size_needed
);
103 "Could not allocate size %u", 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) {
123 /* Set up multi prefixes */
125 *temp_ptr
++ = AML_MULTI_NAME_PREFIX_OP
;
126 *temp_ptr
++ = (char)num_name_segs
;
127 } else if (2 == num_name_segs
) {
129 /* Set up dual prefixes */
131 *temp_ptr
++ = AML_DUAL_NAME_PREFIX
;
135 * Terminate string following prefixes. acpi_ex_name_segment() will
136 * append the segment(s)
140 return_PTR(name_string
);
143 /*******************************************************************************
145 * FUNCTION: acpi_ex_name_segment
147 * PARAMETERS: in_aml_address - Pointer to the name in the AML code
148 * name_string - Where to return the name. The name is appended
149 * to any existing string to form a namepath
153 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
155 ******************************************************************************/
157 static acpi_status
acpi_ex_name_segment(u8
** in_aml_address
, char *name_string
)
159 char *aml_address
= (void *)*in_aml_address
;
160 acpi_status status
= AE_OK
;
164 ACPI_FUNCTION_TRACE(ex_name_segment
);
167 * If first character is a digit, then we know that we aren't looking at a
170 char_buf
[0] = *aml_address
;
172 if ('0' <= char_buf
[0] && char_buf
[0] <= '9') {
173 ACPI_ERROR((AE_INFO
, "Invalid leading digit: %c", char_buf
[0]));
174 return_ACPI_STATUS(AE_CTRL_PENDING
);
177 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
, "Bytes from stream:\n"));
180 (index
< ACPI_NAME_SIZE
)
181 && (acpi_ut_valid_acpi_char(*aml_address
, 0)); index
++) {
182 char_buf
[index
] = *aml_address
++;
183 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
, "%c\n", char_buf
[index
]));
186 /* Valid name segment */
190 /* Found 4 valid characters */
195 strcat(name_string
, char_buf
);
196 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
197 "Appended to - %s\n", name_string
));
199 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
200 "No Name string - %s\n", char_buf
));
202 } else if (index
== 0) {
204 * First character was not a valid name character,
205 * so we are looking at something other than a name.
207 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
208 "Leading character is not alpha: %02Xh (not a name)\n",
210 status
= AE_CTRL_PENDING
;
213 * Segment started with one or more valid characters, but fewer than
216 status
= AE_AML_BAD_NAME
;
218 "Bad character 0x%02x in name, at %p",
219 *aml_address
, aml_address
));
222 *in_aml_address
= ACPI_CAST_PTR(u8
, aml_address
);
223 return_ACPI_STATUS(status
);
226 /*******************************************************************************
228 * FUNCTION: acpi_ex_get_name_string
230 * PARAMETERS: data_type - Object type to be associated with this
232 * in_aml_address - Pointer to the namestring in the AML code
233 * out_name_string - Where the namestring is returned
234 * out_name_length - Length of the returned string
236 * RETURN: Status, namestring and length
238 * DESCRIPTION: Extract a full namepath from the AML byte stream,
239 * including any prefixes.
241 ******************************************************************************/
244 acpi_ex_get_name_string(acpi_object_type data_type
,
246 char **out_name_string
, u32
* out_name_length
)
248 acpi_status status
= AE_OK
;
249 u8
*aml_address
= in_aml_address
;
250 char *name_string
= NULL
;
252 u32 prefix_count
= 0;
253 u8 has_prefix
= FALSE
;
255 ACPI_FUNCTION_TRACE_PTR(ex_get_name_string
, aml_address
);
257 if (ACPI_TYPE_LOCAL_REGION_FIELD
== data_type
||
258 ACPI_TYPE_LOCAL_BANK_FIELD
== data_type
||
259 ACPI_TYPE_LOCAL_INDEX_FIELD
== data_type
) {
261 /* Disallow prefixes for types associated with field_unit names */
263 name_string
= acpi_ex_allocate_name_string(0, 1);
265 status
= AE_NO_MEMORY
;
268 acpi_ex_name_segment(&aml_address
, name_string
);
272 * data_type is not a field name.
273 * Examine first character of name for root or parent prefix operators
275 switch (*aml_address
) {
276 case AML_ROOT_PREFIX
:
278 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
279 "RootPrefix(\\) at %p\n",
283 * Remember that we have a root_prefix --
284 * see comment in acpi_ex_allocate_name_string()
287 prefix_count
= ACPI_UINT32_MAX
;
291 case AML_PARENT_PREFIX
:
293 /* Increment past possibly multiple parent prefixes */
296 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
297 "ParentPrefix (^) at %p\n",
303 } while (*aml_address
== AML_PARENT_PREFIX
);
310 /* Not a prefix character */
315 /* Examine first character of name for name segment prefix operator */
317 switch (*aml_address
) {
318 case AML_DUAL_NAME_PREFIX
:
320 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
321 "DualNamePrefix at %p\n",
326 acpi_ex_allocate_name_string(prefix_count
, 2);
328 status
= AE_NO_MEMORY
;
332 /* Indicate that we processed a prefix */
337 acpi_ex_name_segment(&aml_address
, name_string
);
338 if (ACPI_SUCCESS(status
)) {
340 acpi_ex_name_segment(&aml_address
,
345 case AML_MULTI_NAME_PREFIX_OP
:
347 ACPI_DEBUG_PRINT((ACPI_DB_LOAD
,
348 "MultiNamePrefix at %p\n",
351 /* Fetch count of segments remaining in name path */
354 num_segments
= *aml_address
;
357 acpi_ex_allocate_name_string(prefix_count
,
360 status
= AE_NO_MEMORY
;
364 /* Indicate that we processed a prefix */
369 while (num_segments
&&
371 acpi_ex_name_segment(&aml_address
,
372 name_string
)) == AE_OK
) {
380 /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
382 if (prefix_count
== ACPI_UINT32_MAX
) {
383 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
384 "NameSeg is \"\\\" followed by NULL\n"));
387 /* Consume the NULL byte */
391 acpi_ex_allocate_name_string(prefix_count
, 0);
393 status
= AE_NO_MEMORY
;
401 /* Name segment string */
404 acpi_ex_allocate_name_string(prefix_count
, 1);
406 status
= AE_NO_MEMORY
;
411 acpi_ex_name_segment(&aml_address
, name_string
);
416 if (AE_CTRL_PENDING
== status
&& has_prefix
) {
418 /* Ran out of segments after processing a prefix */
420 ACPI_ERROR((AE_INFO
, "Malformed Name at %p", name_string
));
421 status
= AE_AML_BAD_NAME
;
424 if (ACPI_FAILURE(status
)) {
426 ACPI_FREE(name_string
);
428 return_ACPI_STATUS(status
);
431 *out_name_string
= name_string
;
432 *out_name_length
= (u32
) (aml_address
- in_aml_address
);
434 return_ACPI_STATUS(status
);