blob: 9c587bdfe3f3e23e6e0a476f978f8675b20962d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: nsnames - Name manipulation and search
4 *
5 ******************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, 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>
45#include <acpi/amlcode.h>
46#include <acpi/acnamesp.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("nsnames")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*******************************************************************************
52 *
53 * FUNCTION: acpi_ns_build_external_path
54 *
55 * PARAMETERS: Node - NS node whose pathname is needed
56 * Size - Size of the pathname
57 * *name_buffer - Where to return the pathname
58 *
Bob Moore3c7db222008-08-04 11:13:01 +080059 * RETURN: Status
60 * Places the pathname into the name_buffer, in external format
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * (name segments separated by path separators)
62 *
63 * DESCRIPTION: Generate a full pathaname
64 *
65 ******************************************************************************/
Bob Moore3c7db222008-08-04 11:13:01 +080066acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040067acpi_ns_build_external_path(struct acpi_namespace_node *node,
68 acpi_size size, char *name_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Len Brown4be44fc2005-08-05 00:44:28 -040070 acpi_size index;
71 struct acpi_namespace_node *parent_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Bob Moore4a90c7e2006-01-13 16:22:00 -050073 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /* Special case for root */
76
77 index = size - 1;
78 if (index < ACPI_NAME_SIZE) {
79 name_buffer[0] = AML_ROOT_PREFIX;
80 name_buffer[1] = 0;
Bob Moore3c7db222008-08-04 11:13:01 +080081 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83
84 /* Store terminator byte, then build name backwards */
85
86 parent_node = node;
87 name_buffer[index] = 0;
88
89 while ((index > ACPI_NAME_SIZE) && (parent_node != acpi_gbl_root_node)) {
90 index -= ACPI_NAME_SIZE;
91
92 /* Put the name into the buffer */
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 ACPI_MOVE_32_TO_32((name_buffer + index), &parent_node->name);
95 parent_node = acpi_ns_get_parent_node(parent_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /* Prefix name with the path separator */
98
99 index--;
100 name_buffer[index] = ACPI_PATH_SEPARATOR;
101 }
102
103 /* Overwrite final separator with the root prefix character */
104
105 name_buffer[index] = AML_ROOT_PREFIX;
106
107 if (index != 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500108 ACPI_ERROR((AE_INFO,
Bob Moore3c7db222008-08-04 11:13:01 +0800109 "Could not construct external pathname; index=%X, size=%X, Path=%s",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500110 (u32) index, (u32) size, &name_buffer[size]));
Bob Moore3c7db222008-08-04 11:13:01 +0800111
112 return (AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
Bob Moore3c7db222008-08-04 11:13:01 +0800115 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#ifdef ACPI_DEBUG_OUTPUT
119/*******************************************************************************
120 *
121 * FUNCTION: acpi_ns_get_external_pathname
122 *
Robert Moore44f6c012005-04-18 22:49:35 -0400123 * PARAMETERS: Node - Namespace node whose pathname is needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *
125 * RETURN: Pointer to storage containing the fully qualified name of
126 * the node, In external format (name segments separated by path
127 * separators.)
128 *
129 * DESCRIPTION: Used for debug printing in acpi_ns_search_table().
130 *
131 ******************************************************************************/
132
Len Brown4be44fc2005-08-05 00:44:28 -0400133char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Bob Moore3c7db222008-08-04 11:13:01 +0800135 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400136 char *name_buffer;
137 acpi_size size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bob Mooreb229cf92006-04-21 17:15:00 -0400139 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Calculate required buffer size based on depth below root */
142
Len Brown4be44fc2005-08-05 00:44:28 -0400143 size = acpi_ns_get_pathname_length(node);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200144 if (!size) {
Bob Moore393a75d2008-09-27 11:12:36 +0800145 return_PTR(NULL);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* Allocate a buffer to be returned to caller */
149
Bob Moore83135242006-10-03 00:00:00 -0400150 name_buffer = ACPI_ALLOCATE_ZEROED(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!name_buffer) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500152 ACPI_ERROR((AE_INFO, "Allocation failure"));
Len Brown4be44fc2005-08-05 00:44:28 -0400153 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
156 /* Build the path in the allocated buffer */
157
Bob Moore3c7db222008-08-04 11:13:01 +0800158 status = acpi_ns_build_external_path(node, size, name_buffer);
159 if (ACPI_FAILURE(status)) {
Bob Moore393a75d2008-09-27 11:12:36 +0800160 ACPI_FREE(name_buffer);
161 return_PTR(NULL);
Bob Moore3c7db222008-08-04 11:13:01 +0800162 }
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164 return_PTR(name_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166#endif
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*******************************************************************************
169 *
170 * FUNCTION: acpi_ns_get_pathname_length
171 *
172 * PARAMETERS: Node - Namespace node
173 *
174 * RETURN: Length of path, including prefix
175 *
176 * DESCRIPTION: Get the length of the pathname string for this node
177 *
178 ******************************************************************************/
179
Len Brown4be44fc2005-08-05 00:44:28 -0400180acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_size size;
183 struct acpi_namespace_node *next_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Len Brown4be44fc2005-08-05 00:44:28 -0400185 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /*
188 * Compute length of pathname as 5 * number of name segments.
189 * Go back up the parent tree to the root
190 */
191 size = 0;
192 next_node = node;
193
194 while (next_node && (next_node != acpi_gbl_root_node)) {
Bob Moored8841642008-04-10 19:06:39 +0400195 if (ACPI_GET_DESCRIPTOR_TYPE(next_node) != ACPI_DESC_TYPE_NAMED) {
196 ACPI_ERROR((AE_INFO,
Bob Moore3c7db222008-08-04 11:13:01 +0800197 "Invalid Namespace Node (%p) while traversing namespace",
Bob Moored8841642008-04-10 19:06:39 +0400198 next_node));
199 return 0;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 size += ACPI_PATH_SEGMENT_LENGTH;
Len Brown4be44fc2005-08-05 00:44:28 -0400202 next_node = acpi_ns_get_parent_node(next_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 if (!size) {
Len Brown4be44fc2005-08-05 00:44:28 -0400206 size = 1; /* Root node case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209 return (size + 1); /* +1 for null string terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*******************************************************************************
213 *
214 * FUNCTION: acpi_ns_handle_to_pathname
215 *
216 * PARAMETERS: target_handle - Handle of named object whose name is
217 * to be found
218 * Buffer - Where the pathname is returned
219 *
220 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
221 *
222 * DESCRIPTION: Build and return a full namespace pathname
223 *
224 ******************************************************************************/
225
226acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400227acpi_ns_handle_to_pathname(acpi_handle target_handle,
228 struct acpi_buffer * buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Len Brown4be44fc2005-08-05 00:44:28 -0400230 acpi_status status;
231 struct acpi_namespace_node *node;
232 acpi_size required_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Bob Mooreb229cf92006-04-21 17:15:00 -0400234 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Len Brown4be44fc2005-08-05 00:44:28 -0400236 node = acpi_ns_map_handle_to_node(target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400238 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 /* Determine size required for the caller buffer */
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 required_size = acpi_ns_get_pathname_length(node);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200244 if (!required_size) {
Bob Moore3c7db222008-08-04 11:13:01 +0800245 return_ACPI_STATUS(AE_BAD_PARAMETER);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Validate/Allocate/Clear caller buffer */
249
Len Brown4be44fc2005-08-05 00:44:28 -0400250 status = acpi_ut_initialize_buffer(buffer, required_size);
251 if (ACPI_FAILURE(status)) {
252 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
255 /* Build the path in the caller buffer */
256
Bob Moore3c7db222008-08-04 11:13:01 +0800257 status =
258 acpi_ns_build_external_path(node, required_size, buffer->pointer);
259 if (ACPI_FAILURE(status)) {
260 return_ACPI_STATUS(status);
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Bob Moore50eca3e2005-09-30 19:03:00 -0400263 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400264 (char *)buffer->pointer, (u32) required_size));
265 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}