blob: 6dc2682cbbea8aece96b20dc4814d2c89c496dca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: exnames - interpreter/scanner name load/execute
4 *
5 *****************************************************************************/
6
7/*
Bob Mooreda6f8322018-01-04 10:06:38 -08008 * Copyright (C) 2000 - 2018, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
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.
25 *
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.
29 *
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.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acinterp.h"
47#include "amlcode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("exnames")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040053static char *acpi_ex_allocate_name_string(u32 prefix_count, u32 num_name_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Lv Zheng1f86e8c2012-10-31 02:25:45 +000055static acpi_status acpi_ex_name_segment(u8 **in_aml_address, char *name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*******************************************************************************
58 *
59 * FUNCTION: acpi_ex_allocate_name_string
60 *
61 * PARAMETERS: prefix_count - Count of parent levels. Special cases:
Robert Moore44f6c012005-04-18 22:49:35 -040062 * (-1)==root, 0==none
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * num_name_segs - count of 4-character name segments
64 *
Bob Moore73a30902012-10-31 02:26:55 +000065 * RETURN: A pointer to the allocated string segment. This segment must
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * be deleted by the caller.
67 *
68 * DESCRIPTION: Allocate a buffer for a name string. Ensure allocated name
69 * string is long enough, and set up prefix if any.
70 *
71 ******************************************************************************/
72
Len Brown4be44fc2005-08-05 00:44:28 -040073static char *acpi_ex_allocate_name_string(u32 prefix_count, u32 num_name_segs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Len Brown4be44fc2005-08-05 00:44:28 -040075 char *temp_ptr;
76 char *name_string;
77 u32 size_needed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Bob Mooreb229cf92006-04-21 17:15:00 -040079 ACPI_FUNCTION_TRACE(ex_allocate_name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /*
Robert Moore44f6c012005-04-18 22:49:35 -040082 * Allow room for all \ and ^ prefixes, all segments and a multi_name_prefix.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * Also, one byte for the null terminator.
84 * This may actually be somewhat longer than needed.
85 */
86 if (prefix_count == ACPI_UINT32_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -040087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* Special case for root */
89
90 size_needed = 1 + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
Len Brown4be44fc2005-08-05 00:44:28 -040091 } else {
92 size_needed =
93 prefix_count + (ACPI_NAME_SIZE * num_name_segs) + 2 + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 /*
97 * Allocate a buffer for the name.
98 * This buffer must be deleted by the caller!
99 */
Bob Moore83135242006-10-03 00:00:00 -0400100 name_string = ACPI_ALLOCATE(size_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if (!name_string) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500102 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800103 "Could not allocate size %u", size_needed));
Len Brown4be44fc2005-08-05 00:44:28 -0400104 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 temp_ptr = name_string;
108
109 /* Set up Root or Parent prefixes if needed */
110
111 if (prefix_count == ACPI_UINT32_MAX) {
112 *temp_ptr++ = AML_ROOT_PREFIX;
Len Brown4be44fc2005-08-05 00:44:28 -0400113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 while (prefix_count--) {
115 *temp_ptr++ = AML_PARENT_PREFIX;
116 }
117 }
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 /* Set up Dual or Multi prefixes if needed */
120
121 if (num_name_segs > 2) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 /* Set up multi prefixes */
124
Bob Moore9ff5a21a2017-04-26 16:18:40 +0800125 *temp_ptr++ = AML_MULTI_NAME_PREFIX;
Len Brown4be44fc2005-08-05 00:44:28 -0400126 *temp_ptr++ = (char)num_name_segs;
127 } else if (2 == num_name_segs) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* Set up dual prefixes */
130
131 *temp_ptr++ = AML_DUAL_NAME_PREFIX;
132 }
133
134 /*
135 * Terminate string following prefixes. acpi_ex_name_segment() will
136 * append the segment(s)
137 */
138 *temp_ptr = 0;
139
Len Brown4be44fc2005-08-05 00:44:28 -0400140 return_PTR(name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/*******************************************************************************
144 *
145 * FUNCTION: acpi_ex_name_segment
146 *
Robert Moore44f6c012005-04-18 22:49:35 -0400147 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 * RETURN: Status
152 *
Robert Moore44f6c012005-04-18 22:49:35 -0400153 * DESCRIPTION: Extract an ACPI name (4 bytes) from the AML byte stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 *
155 ******************************************************************************/
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157static acpi_status acpi_ex_name_segment(u8 ** in_aml_address, char *name_string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Len Brown4be44fc2005-08-05 00:44:28 -0400159 char *aml_address = (void *)*in_aml_address;
160 acpi_status status = AE_OK;
161 u32 index;
162 char char_buf[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Bob Mooreb229cf92006-04-21 17:15:00 -0400164 ACPI_FUNCTION_TRACE(ex_name_segment);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /*
Bob Moore1fad8732015-12-29 13:54:36 +0800167 * If first character is a digit, then we know that we aren't looking
168 * at a valid name segment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 */
170 char_buf[0] = *aml_address;
171
172 if ('0' <= char_buf[0] && char_buf[0] <= '9') {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500173 ACPI_ERROR((AE_INFO, "Invalid leading digit: %c", char_buf[0]));
Len Brown4be44fc2005-08-05 00:44:28 -0400174 return_ACPI_STATUS(AE_CTRL_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000177 for (index = 0;
178 (index < ACPI_NAME_SIZE)
Bob Moore6a0df322016-05-05 13:00:36 +0800179 && (acpi_ut_valid_name_char(*aml_address, 0)); index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 char_buf[index] = *aml_address++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Valid name segment */
184
185 if (index == 4) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* Found 4 valid characters */
188
189 char_buf[4] = '\0';
190
191 if (name_string) {
Len Brown4be44fc2005-08-05 00:44:28 -0400192 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
Bob Moore1ef63232018-02-15 13:09:28 -0800193 "Appending NameSeg %s\n", char_buf));
194 strcat(name_string, char_buf);
Len Brown4be44fc2005-08-05 00:44:28 -0400195 } else {
196 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
Bob Moore50eca3e2005-09-30 19:03:00 -0400197 "No Name string - %s\n", char_buf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Len Brown4be44fc2005-08-05 00:44:28 -0400199 } else if (index == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * First character was not a valid name character,
202 * so we are looking at something other than a name.
203 */
Len Brown4be44fc2005-08-05 00:44:28 -0400204 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
205 "Leading character is not alpha: %02Xh (not a name)\n",
206 char_buf[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 status = AE_CTRL_PENDING;
Len Brown4be44fc2005-08-05 00:44:28 -0400208 } else {
Robert Moore44f6c012005-04-18 22:49:35 -0400209 /*
210 * Segment started with one or more valid characters, but fewer than
211 * the required 4
212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 status = AE_AML_BAD_NAME;
Bob Mooreb8e4d892006-01-27 16:43:00 -0500214 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800215 "Bad character 0x%02x in name, at %p",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500216 *aml_address, aml_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Bob Moorec51a4de2005-11-17 13:07:00 -0500219 *in_aml_address = ACPI_CAST_PTR(u8, aml_address);
Len Brown4be44fc2005-08-05 00:44:28 -0400220 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*******************************************************************************
224 *
225 * FUNCTION: acpi_ex_get_name_string
226 *
Robert Moore44f6c012005-04-18 22:49:35 -0400227 * PARAMETERS: data_type - Object type to be associated with this
228 * name
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
Robert Moore44f6c012005-04-18 22:49:35 -0400233 * RETURN: Status, namestring and length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Robert Moore44f6c012005-04-18 22:49:35 -0400235 * DESCRIPTION: Extract a full namepath from the AML byte stream,
236 * including any prefixes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 ******************************************************************************/
239
240acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400241acpi_ex_get_name_string(acpi_object_type data_type,
242 u8 * in_aml_address,
243 char **out_name_string, u32 * out_name_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Len Brown4be44fc2005-08-05 00:44:28 -0400245 acpi_status status = AE_OK;
246 u8 *aml_address = in_aml_address;
247 char *name_string = NULL;
248 u32 num_segments;
249 u32 prefix_count = 0;
250 u8 has_prefix = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Bob Mooreb229cf92006-04-21 17:15:00 -0400252 ACPI_FUNCTION_TRACE_PTR(ex_get_name_string, aml_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Len Brown4be44fc2005-08-05 00:44:28 -0400254 if (ACPI_TYPE_LOCAL_REGION_FIELD == data_type ||
255 ACPI_TYPE_LOCAL_BANK_FIELD == data_type ||
256 ACPI_TYPE_LOCAL_INDEX_FIELD == data_type) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* Disallow prefixes for types associated with field_unit names */
259
Len Brown4be44fc2005-08-05 00:44:28 -0400260 name_string = acpi_ex_allocate_name_string(0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!name_string) {
262 status = AE_NO_MEMORY;
Len Brown4be44fc2005-08-05 00:44:28 -0400263 } else {
264 status =
265 acpi_ex_name_segment(&aml_address, name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Len Brown4be44fc2005-08-05 00:44:28 -0400267 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * data_type is not a field name.
270 * Examine first character of name for root or parent prefix operators
271 */
272 switch (*aml_address) {
273 case AML_ROOT_PREFIX:
274
Len Brown4be44fc2005-08-05 00:44:28 -0400275 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
Bob Mooreb229cf92006-04-21 17:15:00 -0400276 "RootPrefix(\\) at %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400277 aml_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /*
280 * Remember that we have a root_prefix --
281 * see comment in acpi_ex_allocate_name_string()
282 */
283 aml_address++;
284 prefix_count = ACPI_UINT32_MAX;
285 has_prefix = TRUE;
286 break;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case AML_PARENT_PREFIX:
289
290 /* Increment past possibly multiple parent prefixes */
291
292 do {
Len Brown4be44fc2005-08-05 00:44:28 -0400293 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
Bob Mooreb229cf92006-04-21 17:15:00 -0400294 "ParentPrefix (^) at %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400295 aml_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 aml_address++;
298 prefix_count++;
299
300 } while (*aml_address == AML_PARENT_PREFIX);
301
302 has_prefix = TRUE;
303 break;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 default:
306
307 /* Not a prefix character */
308
309 break;
310 }
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 /* Examine first character of name for name segment prefix operator */
313
314 switch (*aml_address) {
315 case AML_DUAL_NAME_PREFIX:
316
Len Brown4be44fc2005-08-05 00:44:28 -0400317 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
Bob Mooreb229cf92006-04-21 17:15:00 -0400318 "DualNamePrefix at %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400319 aml_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 aml_address++;
Len Brown4be44fc2005-08-05 00:44:28 -0400322 name_string =
323 acpi_ex_allocate_name_string(prefix_count, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!name_string) {
325 status = AE_NO_MEMORY;
326 break;
327 }
328
329 /* Indicate that we processed a prefix */
330
331 has_prefix = TRUE;
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333 status =
334 acpi_ex_name_segment(&aml_address, name_string);
335 if (ACPI_SUCCESS(status)) {
336 status =
337 acpi_ex_name_segment(&aml_address,
338 name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 break;
341
Bob Moore9ff5a21a2017-04-26 16:18:40 +0800342 case AML_MULTI_NAME_PREFIX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 ACPI_DEBUG_PRINT((ACPI_DB_LOAD,
Bob Mooreb229cf92006-04-21 17:15:00 -0400345 "MultiNamePrefix at %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400346 aml_address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Fetch count of segments remaining in name path */
349
350 aml_address++;
351 num_segments = *aml_address;
352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 name_string =
354 acpi_ex_allocate_name_string(prefix_count,
355 num_segments);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!name_string) {
357 status = AE_NO_MEMORY;
358 break;
359 }
360
361 /* Indicate that we processed a prefix */
362
363 aml_address++;
364 has_prefix = TRUE;
365
366 while (num_segments &&
Len Brown4be44fc2005-08-05 00:44:28 -0400367 (status =
368 acpi_ex_name_segment(&aml_address,
369 name_string)) == AE_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 num_segments--;
371 }
372
373 break;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case 0:
376
377 /* null_name valid as of 8-12-98 ASL/AML Grammar Update */
378
379 if (prefix_count == ACPI_UINT32_MAX) {
Len Brown4be44fc2005-08-05 00:44:28 -0400380 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Bob Mooreb229cf92006-04-21 17:15:00 -0400381 "NameSeg is \"\\\" followed by NULL\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
384 /* Consume the NULL byte */
385
386 aml_address++;
Len Brown4be44fc2005-08-05 00:44:28 -0400387 name_string =
388 acpi_ex_allocate_name_string(prefix_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!name_string) {
390 status = AE_NO_MEMORY;
391 break;
392 }
393
394 break;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 default:
397
398 /* Name segment string */
399
Len Brown4be44fc2005-08-05 00:44:28 -0400400 name_string =
401 acpi_ex_allocate_name_string(prefix_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!name_string) {
403 status = AE_NO_MEMORY;
404 break;
405 }
406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 status =
408 acpi_ex_name_segment(&aml_address, name_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410 }
411 }
412
413 if (AE_CTRL_PENDING == status && has_prefix) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Ran out of segments after processing a prefix */
416
Bob Mooreb8e4d892006-01-27 16:43:00 -0500417 ACPI_ERROR((AE_INFO, "Malformed Name at %p", name_string));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 status = AE_AML_BAD_NAME;
419 }
420
Len Brown4be44fc2005-08-05 00:44:28 -0400421 if (ACPI_FAILURE(status)) {
Robert Moore88ac00f2005-05-26 00:00:00 -0400422 if (name_string) {
Bob Moore83135242006-10-03 00:00:00 -0400423 ACPI_FREE(name_string);
Robert Moore88ac00f2005-05-26 00:00:00 -0400424 }
Len Brown4be44fc2005-08-05 00:44:28 -0400425 return_ACPI_STATUS(status);
Robert Moore88ac00f2005-05-26 00:00:00 -0400426 }
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *out_name_string = name_string;
429 *out_name_length = (u32) (aml_address - in_aml_address);
430
Len Brown4be44fc2005-08-05 00:44:28 -0400431 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}