blob: 8d8104b8bd28affdf70a35c38295116c25ec423c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
5 *
6 *****************************************************************************/
7
8/*
David E. Box82a80942015-02-05 15:20:45 +08009 * Copyright (C) 2000 - 2015, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
26 *
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.
30 *
31 * NO WARRANTY
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.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
48#include "amlcode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("nsutils")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Robert Moore44f6c012005-04-18 22:49:35 -040053/* Local prototypes */
Robert Moore44f6c012005-04-18 22:49:35 -040054#ifdef ACPI_OBSOLETE_FUNCTIONS
Len Brown4be44fc2005-08-05 00:44:28 -040055acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node *node_to_search);
Robert Moore44f6c012005-04-18 22:49:35 -040056#endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*******************************************************************************
59 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * FUNCTION: acpi_ns_print_node_pathname
61 *
Bob Mooreba494be2012-07-12 09:40:10 +080062 * PARAMETERS: node - Object
63 * message - Prefix message
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
65 * DESCRIPTION: Print an object's full namespace pathname
66 * Manages allocation/freeing of a pathname buffer
67 *
68 ******************************************************************************/
69
70void
Bob Moore4b8ed632008-06-10 13:55:53 +080071acpi_ns_print_node_pathname(struct acpi_namespace_node *node,
72 const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Len Brown4be44fc2005-08-05 00:44:28 -040074 struct acpi_buffer buffer;
75 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -040078 acpi_os_printf("[NULL NAME]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return;
80 }
81
82 /* Convert handle to full pathname and print it (with supplied message) */
83
84 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
85
Len Brown4be44fc2005-08-05 00:44:28 -040086 status = acpi_ns_handle_to_pathname(node, &buffer);
87 if (ACPI_SUCCESS(status)) {
Robert Moore44f6c012005-04-18 22:49:35 -040088 if (message) {
Len Brown4be44fc2005-08-05 00:44:28 -040089 acpi_os_printf("%s ", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Len Brown4be44fc2005-08-05 00:44:28 -040092 acpi_os_printf("[%s] (Node %p)", (char *)buffer.pointer, node);
Bob Moore83135242006-10-03 00:00:00 -040093 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*******************************************************************************
98 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * FUNCTION: acpi_ns_get_type
100 *
Bob Mooreba494be2012-07-12 09:40:10 +0800101 * PARAMETERS: node - Parent Node to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 *
103 * RETURN: Type field from Node whose handle is passed
104 *
Robert Moore44f6c012005-04-18 22:49:35 -0400105 * DESCRIPTION: Return the type of a Namespace node
106 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ******************************************************************************/
108
Len Brown4be44fc2005-08-05 00:44:28 -0400109acpi_object_type acpi_ns_get_type(struct acpi_namespace_node * node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Bob Mooreb229cf92006-04-21 17:15:00 -0400111 ACPI_FUNCTION_TRACE(ns_get_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (!node) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500114 ACPI_WARNING((AE_INFO, "Null Node parameter"));
Bob Moorefd1af712013-03-08 09:22:23 +0000115 return_UINT8(ACPI_TYPE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
Bob Moorefd1af712013-03-08 09:22:23 +0000118 return_UINT8(node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*******************************************************************************
122 *
123 * FUNCTION: acpi_ns_local
124 *
Bob Mooreba494be2012-07-12 09:40:10 +0800125 * PARAMETERS: type - A namespace object type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 *
127 * RETURN: LOCAL if names must be found locally in objects of the
128 * passed type, 0 if enclosing scopes should be searched
129 *
Robert Moore44f6c012005-04-18 22:49:35 -0400130 * DESCRIPTION: Returns scope rule for the given object type.
131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 ******************************************************************************/
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134u32 acpi_ns_local(acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Bob Mooreb229cf92006-04-21 17:15:00 -0400136 ACPI_FUNCTION_TRACE(ns_local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Len Brown4be44fc2005-08-05 00:44:28 -0400138 if (!acpi_ut_valid_object_type(type)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Type code out of range */
141
Bob Mooref6a22b02010-03-05 17:56:40 +0800142 ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
Bob Moorefd1af712013-03-08 09:22:23 +0000143 return_UINT32(ACPI_NS_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Bob Moorefd1af712013-03-08 09:22:23 +0000146 return_UINT32(acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*******************************************************************************
150 *
151 * FUNCTION: acpi_ns_get_internal_name_length
152 *
Bob Mooreba494be2012-07-12 09:40:10 +0800153 * PARAMETERS: info - Info struct initialized with the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * external name pointer.
155 *
Robert Moore44f6c012005-04-18 22:49:35 -0400156 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
158 * DESCRIPTION: Calculate the length of the internal (AML) namestring
159 * corresponding to the external (ASL) namestring.
160 *
161 ******************************************************************************/
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Bob Moore4b8ed632008-06-10 13:55:53 +0800165 const char *next_external_char;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Len Brown4be44fc2005-08-05 00:44:28 -0400168 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 next_external_char = info->external_name;
171 info->num_carats = 0;
172 info->num_segments = 0;
173 info->fully_qualified = FALSE;
174
175 /*
176 * For the internal name, the required length is 4 bytes per segment, plus
177 * 1 each for root_prefix, multi_name_prefix_op, segment count, trailing null
178 * (which is not really needed, but no there's harm in putting it there)
179 *
180 * strlen() + 1 covers the first name_seg, which has no path separator
181 */
Bob Moore04a81dc2012-12-31 00:05:17 +0000182 if (ACPI_IS_ROOT_PREFIX(*next_external_char)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 info->fully_qualified = TRUE;
184 next_external_char++;
Lin Mingd037c5f2008-11-13 10:54:39 +0800185
186 /* Skip redundant root_prefix, like \\_SB.PCI0.SBRG.EC0 */
187
Bob Moore04a81dc2012-12-31 00:05:17 +0000188 while (ACPI_IS_ROOT_PREFIX(*next_external_char)) {
Lin Mingd037c5f2008-11-13 10:54:39 +0800189 next_external_char++;
190 }
Len Brown4be44fc2005-08-05 00:44:28 -0400191 } else {
Bob Moored4913dc2009-03-06 10:05:18 +0800192 /* Handle Carat prefixes */
193
Bob Moore04a81dc2012-12-31 00:05:17 +0000194 while (ACPI_IS_PARENT_PREFIX(*next_external_char)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 info->num_carats++;
196 next_external_char++;
197 }
198 }
199
200 /*
201 * Determine the number of ACPI name "segments" by counting the number of
202 * path separators within the string. Start with one segment since the
203 * segment count is [(# separators) + 1], and zero separators is ok.
204 */
205 if (*next_external_char) {
206 info->num_segments = 1;
207 for (i = 0; next_external_char[i]; i++) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000208 if (ACPI_IS_PATH_SEPARATOR(next_external_char[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 info->num_segments++;
210 }
211 }
212 }
213
214 info->length = (ACPI_NAME_SIZE * info->num_segments) +
Len Brown4be44fc2005-08-05 00:44:28 -0400215 4 + info->num_carats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 info->next_external_char = next_external_char;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*******************************************************************************
221 *
222 * FUNCTION: acpi_ns_build_internal_name
223 *
Bob Mooreba494be2012-07-12 09:40:10 +0800224 * PARAMETERS: info - Info struct fully initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
226 * RETURN: Status
227 *
228 * DESCRIPTION: Construct the internal (AML) namestring
229 * corresponding to the external (ASL) namestring.
230 *
231 ******************************************************************************/
232
Len Brown4be44fc2005-08-05 00:44:28 -0400233acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Len Brown4be44fc2005-08-05 00:44:28 -0400235 u32 num_segments = info->num_segments;
236 char *internal_name = info->internal_name;
Bob Moore4b8ed632008-06-10 13:55:53 +0800237 const char *external_name = info->next_external_char;
Len Brown4be44fc2005-08-05 00:44:28 -0400238 char *result = NULL;
Bob Moore67a119f2008-06-10 13:42:13 +0800239 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Bob Mooreb229cf92006-04-21 17:15:00 -0400241 ACPI_FUNCTION_TRACE(ns_build_internal_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* Setup the correct prefixes, counts, and pointers */
244
245 if (info->fully_qualified) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000246 internal_name[0] = AML_ROOT_PREFIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (num_segments <= 1) {
249 result = &internal_name[1];
Len Brown4be44fc2005-08-05 00:44:28 -0400250 } else if (num_segments == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 internal_name[1] = AML_DUAL_NAME_PREFIX;
252 result = &internal_name[2];
Len Brown4be44fc2005-08-05 00:44:28 -0400253 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
Len Brown4be44fc2005-08-05 00:44:28 -0400255 internal_name[2] = (char)num_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 result = &internal_name[3];
257 }
Len Brown4be44fc2005-08-05 00:44:28 -0400258 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * Not fully qualified.
261 * Handle Carats first, then append the name segments
262 */
263 i = 0;
264 if (info->num_carats) {
265 for (i = 0; i < info->num_carats; i++) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000266 internal_name[i] = AML_PARENT_PREFIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 }
269
270 if (num_segments <= 1) {
271 result = &internal_name[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400272 } else if (num_segments == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 internal_name[i] = AML_DUAL_NAME_PREFIX;
Bob Moore67a119f2008-06-10 13:42:13 +0800274 result = &internal_name[(acpi_size) i + 1];
Len Brown4be44fc2005-08-05 00:44:28 -0400275 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
Bob Moore67a119f2008-06-10 13:42:13 +0800277 internal_name[(acpi_size) i + 1] = (char)num_segments;
278 result = &internal_name[(acpi_size) i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280 }
281
282 /* Build the name (minus path separators) */
283
284 for (; num_segments; num_segments--) {
285 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000286 if (ACPI_IS_PATH_SEPARATOR(*external_name) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400287 (*external_name == 0)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Pad the segment with underscore(s) if segment is short */
290
291 result[i] = '_';
Len Brown4be44fc2005-08-05 00:44:28 -0400292 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Convert the character to uppercase and save it */
294
Bob Moore4fa46162015-07-01 14:45:11 +0800295 result[i] = (char)toupper((int)*external_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 external_name++;
297 }
298 }
299
300 /* Now we must have a path separator, or the pathname is bad */
301
Bob Moore04a81dc2012-12-31 00:05:17 +0000302 if (!ACPI_IS_PATH_SEPARATOR(*external_name) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400303 (*external_name != 0)) {
Bob Moorea1acd222012-03-21 09:44:00 +0800304 return_ACPI_STATUS(AE_BAD_PATHNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307 /* Move on the next segment */
308
309 external_name++;
310 result += ACPI_NAME_SIZE;
311 }
312
313 /* Terminate the string */
314
315 *result = 0;
316
317 if (info->fully_qualified) {
Len Brown4be44fc2005-08-05 00:44:28 -0400318 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
319 "Returning [%p] (abs) \"\\%s\"\n",
320 internal_name, internal_name));
321 } else {
322 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
323 internal_name, internal_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*******************************************************************************
330 *
331 * FUNCTION: acpi_ns_internalize_name
332 *
333 * PARAMETERS: *external_name - External representation of name
Bob Mooreba494be2012-07-12 09:40:10 +0800334 * **Converted name - Where to return the resulting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * internal represention of the name
336 *
337 * RETURN: Status
338 *
339 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
340 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
341 *
342 *******************************************************************************/
343
Bob Moore4b8ed632008-06-10 13:55:53 +0800344acpi_status
345acpi_ns_internalize_name(const char *external_name, char **converted_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Len Brown4be44fc2005-08-05 00:44:28 -0400347 char *internal_name;
348 struct acpi_namestring_info info;
349 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Bob Mooreb229cf92006-04-21 17:15:00 -0400351 ACPI_FUNCTION_TRACE(ns_internalize_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 if ((!external_name) || (*external_name == 0) || (!converted_name)) {
354 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
357 /* Get the length of the new internal name */
358
359 info.external_name = external_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400360 acpi_ns_get_internal_name_length(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* We need a segment to store the internal name */
363
Bob Moore83135242006-10-03 00:00:00 -0400364 internal_name = ACPI_ALLOCATE_ZEROED(info.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!internal_name) {
Len Brown4be44fc2005-08-05 00:44:28 -0400366 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 /* Build the name */
370
371 info.internal_name = internal_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400372 status = acpi_ns_build_internal_name(&info);
373 if (ACPI_FAILURE(status)) {
Bob Moore83135242006-10-03 00:00:00 -0400374 ACPI_FREE(internal_name);
Len Brown4be44fc2005-08-05 00:44:28 -0400375 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 *converted_name = internal_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400379 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*******************************************************************************
383 *
384 * FUNCTION: acpi_ns_externalize_name
385 *
Robert Moore44f6c012005-04-18 22:49:35 -0400386 * PARAMETERS: internal_name_length - Lenth of the internal name below
387 * internal_name - Internal representation of name
388 * converted_name_length - Where the length is returned
389 * converted_name - Where the resulting external name
390 * is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
392 * RETURN: Status
393 *
394 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
Robert Moore44f6c012005-04-18 22:49:35 -0400395 * to its external (printable) form (e.g. "\_PR_.CPU0")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
397 ******************************************************************************/
398
399acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400400acpi_ns_externalize_name(u32 internal_name_length,
Bob Moore4b8ed632008-06-10 13:55:53 +0800401 const char *internal_name,
Len Brown4be44fc2005-08-05 00:44:28 -0400402 u32 * converted_name_length, char **converted_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Bob Moore67a119f2008-06-10 13:42:13 +0800404 u32 names_index = 0;
405 u32 num_segments = 0;
406 u32 required_length;
407 u32 prefix_length = 0;
408 u32 i = 0;
409 u32 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Bob Mooreb229cf92006-04-21 17:15:00 -0400411 ACPI_FUNCTION_TRACE(ns_externalize_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Len Brown4be44fc2005-08-05 00:44:28 -0400413 if (!internal_name_length || !internal_name || !converted_name) {
414 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
Bob Moored4913dc2009-03-06 10:05:18 +0800417 /* Check for a prefix (one '\' | one or more '^') */
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 switch (internal_name[0]) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000420 case AML_ROOT_PREFIX:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 prefix_length = 1;
423 break;
424
Bob Moore04a81dc2012-12-31 00:05:17 +0000425 case AML_PARENT_PREFIX:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 for (i = 0; i < internal_name_length; i++) {
Bob Moore04a81dc2012-12-31 00:05:17 +0000428 if (ACPI_IS_PARENT_PREFIX(internal_name[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 prefix_length = i + 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400430 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 break;
432 }
433 }
434
435 if (i == internal_name_length) {
436 prefix_length = i;
437 }
438
439 break;
440
441 default:
Chao Guan1d1ea1b2013-06-08 00:58:14 +0000442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 }
445
446 /*
Bob Moored4913dc2009-03-06 10:05:18 +0800447 * Check for object names. Note that there could be 0-255 of these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * 4-byte elements.
449 */
450 if (prefix_length < internal_name_length) {
451 switch (internal_name[prefix_length]) {
452 case AML_MULTI_NAME_PREFIX_OP:
453
454 /* <count> 4-byte names */
455
456 names_index = prefix_length + 2;
Bob Moore67a119f2008-06-10 13:42:13 +0800457 num_segments = (u8)
458 internal_name[(acpi_size) prefix_length + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460
461 case AML_DUAL_NAME_PREFIX:
462
463 /* Two 4-byte names */
464
465 names_index = prefix_length + 1;
466 num_segments = 2;
467 break;
468
469 case 0:
470
471 /* null_name */
472
473 names_index = 0;
474 num_segments = 0;
475 break;
476
477 default:
478
479 /* one 4-byte name */
480
481 names_index = prefix_length;
482 num_segments = 1;
483 break;
484 }
485 }
486
487 /*
488 * Calculate the length of converted_name, which equals the length
489 * of the prefix, length of all object names, length of any required
490 * punctuation ('.') between object names, plus the NULL terminator.
491 */
492 required_length = prefix_length + (4 * num_segments) +
Len Brown4be44fc2005-08-05 00:44:28 -0400493 ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /*
Bob Moore73a30902012-10-31 02:26:55 +0000496 * Check to see if we're still in bounds. If not, there's a problem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * with internal_name (invalid format).
498 */
499 if (required_length > internal_name_length) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500500 ACPI_ERROR((AE_INFO, "Invalid internal name"));
Len Brown4be44fc2005-08-05 00:44:28 -0400501 return_ACPI_STATUS(AE_BAD_PATHNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Bob Moored4913dc2009-03-06 10:05:18 +0800504 /* Build the converted_name */
505
Bob Moore83135242006-10-03 00:00:00 -0400506 *converted_name = ACPI_ALLOCATE_ZEROED(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!(*converted_name)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400508 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 j = 0;
512
513 for (i = 0; i < prefix_length; i++) {
514 (*converted_name)[j++] = internal_name[i];
515 }
516
517 if (num_segments > 0) {
518 for (i = 0; i < num_segments; i++) {
519 if (i > 0) {
520 (*converted_name)[j++] = '.';
521 }
522
Bob Moore47abd132012-10-31 02:28:19 +0000523 /* Copy and validate the 4-char name segment */
524
525 ACPI_MOVE_NAME(&(*converted_name)[j],
526 &internal_name[names_index]);
527 acpi_ut_repair_name(&(*converted_name)[j]);
Bob Moore00eb3252012-10-31 02:27:48 +0000528
529 j += ACPI_NAME_SIZE;
530 names_index += ACPI_NAME_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
534 if (converted_name_length) {
535 *converted_name_length = (u32) required_length;
536 }
537
Len Brown4be44fc2005-08-05 00:44:28 -0400538 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541/*******************************************************************************
542 *
Bob Mooref24b6642009-12-11 14:57:00 +0800543 * FUNCTION: acpi_ns_validate_handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *
Bob Mooreba494be2012-07-12 09:40:10 +0800545 * PARAMETERS: handle - Handle to be validated and typecast to a
Bob Mooref24b6642009-12-11 14:57:00 +0800546 * namespace node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
Bob Mooref24b6642009-12-11 14:57:00 +0800548 * RETURN: A pointer to a namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
Bob Mooref24b6642009-12-11 14:57:00 +0800550 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
551 * cases for the root node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
Bob Mooref24b6642009-12-11 14:57:00 +0800553 * NOTE: Real integer handles would allow for more verification
Robert Moore44f6c012005-04-18 22:49:35 -0400554 * and keep all pointers within this subsystem - however this introduces
Bob Mooref24b6642009-12-11 14:57:00 +0800555 * more overhead and has not been necessary to this point. Drivers
556 * holding handles are typically notified before a node becomes invalid
557 * due to a table unload.
Bob Moored4913dc2009-03-06 10:05:18 +0800558 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ******************************************************************************/
560
Bob Mooref24b6642009-12-11 14:57:00 +0800561struct acpi_namespace_node *acpi_ns_validate_handle(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563
Len Brown4be44fc2005-08-05 00:44:28 -0400564 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Bob Moored4913dc2009-03-06 10:05:18 +0800566 /* Parameter validation */
567
Bob Moore41195322006-05-26 16:36:00 -0400568 if ((!handle) || (handle == ACPI_ROOT_OBJECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return (acpi_gbl_root_node);
570 }
571
572 /* We can at least attempt to verify the handle */
573
Len Brown4be44fc2005-08-05 00:44:28 -0400574 if (ACPI_GET_DESCRIPTOR_TYPE(handle) != ACPI_DESC_TYPE_NAMED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return (NULL);
576 }
577
Bob Moore41195322006-05-26 16:36:00 -0400578 return (ACPI_CAST_PTR(struct acpi_namespace_node, handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*******************************************************************************
582 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 * FUNCTION: acpi_ns_terminate
584 *
585 * PARAMETERS: none
586 *
587 * RETURN: none
588 *
Robert Moore44f6c012005-04-18 22:49:35 -0400589 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 *
591 ******************************************************************************/
592
Len Brown4be44fc2005-08-05 00:44:28 -0400593void acpi_ns_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Bob Moore3f69fe12013-11-21 12:20:06 +0800595 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Bob Mooreb229cf92006-04-21 17:15:00 -0400597 ACPI_FUNCTION_TRACE(ns_terminate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /*
Bob Moore3f69fe12013-11-21 12:20:06 +0800600 * Free the entire namespace -- all nodes and all objects
601 * attached to the nodes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Len Brown4be44fc2005-08-05 00:44:28 -0400603 acpi_ns_delete_namespace_subtree(acpi_gbl_root_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Bob Moore3f69fe12013-11-21 12:20:06 +0800605 /* Delete any objects attached to the root node */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Bob Moore3f69fe12013-11-21 12:20:06 +0800607 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
608 if (ACPI_FAILURE(status)) {
609 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Bob Moore3f69fe12013-11-21 12:20:06 +0800612 acpi_ns_delete_node(acpi_gbl_root_node);
613 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
614
Len Brown4be44fc2005-08-05 00:44:28 -0400615 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Namespace freed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return_VOID;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*******************************************************************************
620 *
621 * FUNCTION: acpi_ns_opens_scope
622 *
Bob Mooreba494be2012-07-12 09:40:10 +0800623 * PARAMETERS: type - A valid namespace type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *
625 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
626 * to the ACPI specification, else 0
627 *
628 ******************************************************************************/
629
Len Brown4be44fc2005-08-05 00:44:28 -0400630u32 acpi_ns_opens_scope(acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Bob Moore0e770b32012-12-19 05:37:59 +0000632 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Bob Moore0e770b32012-12-19 05:37:59 +0000634 if (type > ACPI_TYPE_LOCAL_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* type code out of range */
637
Bob Mooref6a22b02010-03-05 17:56:40 +0800638 ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
Bob Moore0e770b32012-12-19 05:37:59 +0000639 return (ACPI_NS_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Bob Moore0e770b32012-12-19 05:37:59 +0000642 return (((u32)acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/*******************************************************************************
646 *
Bob Moore41195322006-05-26 16:36:00 -0400647 * FUNCTION: acpi_ns_get_node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 *
Bob Mooreba494be2012-07-12 09:40:10 +0800649 * PARAMETERS: *pathname - Name to be found, in external (ASL) format. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * \ (backslash) and ^ (carat) prefixes, and the
651 * . (period) to separate segments are supported.
Bob Moore41195322006-05-26 16:36:00 -0400652 * prefix_node - Root of subtree to be searched, or NS_ALL for the
Bob Moore73a30902012-10-31 02:26:55 +0000653 * root of the name space. If Name is fully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * qualified (first s8 is '\'), the passed value
655 * of Scope will not be accessed.
Bob Mooreba494be2012-07-12 09:40:10 +0800656 * flags - Used to indicate whether to perform upsearch or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * not.
658 * return_node - Where the Node is returned
659 *
660 * DESCRIPTION: Look up a name relative to a given scope and return the
Bob Moore73a30902012-10-31 02:26:55 +0000661 * corresponding Node. NOTE: Scope can be null.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * MUTEX: Locks namespace
664 *
665 ******************************************************************************/
666
667acpi_status
Bob Moore41195322006-05-26 16:36:00 -0400668acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
Bob Moore4b8ed632008-06-10 13:55:53 +0800669 const char *pathname,
Bob Moore41195322006-05-26 16:36:00 -0400670 u32 flags, struct acpi_namespace_node **return_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Len Brown4be44fc2005-08-05 00:44:28 -0400672 union acpi_generic_state scope_info;
673 acpi_status status;
Bob Moore41195322006-05-26 16:36:00 -0400674 char *internal_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Bob Moore5853a9f2009-06-02 13:20:00 +0800676 ACPI_FUNCTION_TRACE_PTR(ns_get_node, ACPI_CAST_PTR(char, pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Bob Mooref8c9bfe2012-12-31 00:05:33 +0000678 /* Simplest case is a null pathname */
679
Bob Moore41195322006-05-26 16:36:00 -0400680 if (!pathname) {
681 *return_node = prefix_node;
682 if (!prefix_node) {
683 *return_node = acpi_gbl_root_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Bob Moore41195322006-05-26 16:36:00 -0400685 return_ACPI_STATUS(AE_OK);
686 }
687
Bob Mooref8c9bfe2012-12-31 00:05:33 +0000688 /* Quick check for a reference to the root */
689
690 if (ACPI_IS_ROOT_PREFIX(pathname[0]) && (!pathname[1])) {
691 *return_node = acpi_gbl_root_node;
692 return_ACPI_STATUS(AE_OK);
693 }
694
Bob Moore41195322006-05-26 16:36:00 -0400695 /* Convert path to internal representation */
696
697 status = acpi_ns_internalize_name(pathname, &internal_path);
698 if (ACPI_FAILURE(status)) {
699 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701
702 /* Must lock namespace during lookup */
703
Len Brown4be44fc2005-08-05 00:44:28 -0400704 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
705 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto cleanup;
707 }
708
709 /* Setup lookup scope (search starting point) */
710
Bob Moore41195322006-05-26 16:36:00 -0400711 scope_info.scope.node = prefix_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* Lookup the name in the namespace */
714
Bob Moore41195322006-05-26 16:36:00 -0400715 status = acpi_ns_lookup(&scope_info, internal_path, ACPI_TYPE_ANY,
716 ACPI_IMODE_EXECUTE,
717 (flags | ACPI_NS_DONT_OPEN_SCOPE), NULL,
718 return_node);
Len Brown4be44fc2005-08-05 00:44:28 -0400719 if (ACPI_FAILURE(status)) {
Bob Moore7bcc06e2009-02-18 14:58:08 +0800720 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s, %s\n",
Bob Moore41195322006-05-26 16:36:00 -0400721 pathname, acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
Len Brown4be44fc2005-08-05 00:44:28 -0400724 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Lv Zheng10622bf2013-10-29 09:30:02 +0800726cleanup:
Bob Moore41195322006-05-26 16:36:00 -0400727 ACPI_FREE(internal_path);
Len Brown4be44fc2005-08-05 00:44:28 -0400728 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}