blob: 42a39a7c96e913a519e6b1bf7b5a79d152dd4a4d [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/*******************************************************************************
119 *
120 * FUNCTION: acpi_ns_get_external_pathname
121 *
Robert Moore44f6c012005-04-18 22:49:35 -0400122 * PARAMETERS: Node - Namespace node whose pathname is needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 *
124 * RETURN: Pointer to storage containing the fully qualified name of
125 * the node, In external format (name segments separated by path
126 * separators.)
127 *
128 * DESCRIPTION: Used for debug printing in acpi_ns_search_table().
129 *
130 ******************************************************************************/
131
Len Brown4be44fc2005-08-05 00:44:28 -0400132char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Bob Moore3c7db222008-08-04 11:13:01 +0800134 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400135 char *name_buffer;
136 acpi_size size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Bob Mooreb229cf92006-04-21 17:15:00 -0400138 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Calculate required buffer size based on depth below root */
141
Len Brown4be44fc2005-08-05 00:44:28 -0400142 size = acpi_ns_get_pathname_length(node);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200143 if (!size) {
Bob Moore393a75d2008-09-27 11:12:36 +0800144 return_PTR(NULL);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* Allocate a buffer to be returned to caller */
148
Bob Moore83135242006-10-03 00:00:00 -0400149 name_buffer = ACPI_ALLOCATE_ZEROED(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!name_buffer) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500151 ACPI_ERROR((AE_INFO, "Allocation failure"));
Len Brown4be44fc2005-08-05 00:44:28 -0400152 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 /* Build the path in the allocated buffer */
156
Bob Moore3c7db222008-08-04 11:13:01 +0800157 status = acpi_ns_build_external_path(node, size, name_buffer);
158 if (ACPI_FAILURE(status)) {
Bob Moore393a75d2008-09-27 11:12:36 +0800159 ACPI_FREE(name_buffer);
160 return_PTR(NULL);
Bob Moore3c7db222008-08-04 11:13:01 +0800161 }
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163 return_PTR(name_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*******************************************************************************
167 *
168 * FUNCTION: acpi_ns_get_pathname_length
169 *
170 * PARAMETERS: Node - Namespace node
171 *
172 * RETURN: Length of path, including prefix
173 *
174 * DESCRIPTION: Get the length of the pathname string for this node
175 *
176 ******************************************************************************/
177
Len Brown4be44fc2005-08-05 00:44:28 -0400178acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Len Brown4be44fc2005-08-05 00:44:28 -0400180 acpi_size size;
181 struct acpi_namespace_node *next_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Len Brown4be44fc2005-08-05 00:44:28 -0400183 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /*
186 * Compute length of pathname as 5 * number of name segments.
187 * Go back up the parent tree to the root
188 */
189 size = 0;
190 next_node = node;
191
192 while (next_node && (next_node != acpi_gbl_root_node)) {
Bob Moored8841642008-04-10 19:06:39 +0400193 if (ACPI_GET_DESCRIPTOR_TYPE(next_node) != ACPI_DESC_TYPE_NAMED) {
194 ACPI_ERROR((AE_INFO,
Bob Moore3c7db222008-08-04 11:13:01 +0800195 "Invalid Namespace Node (%p) while traversing namespace",
Bob Moored8841642008-04-10 19:06:39 +0400196 next_node));
197 return 0;
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 size += ACPI_PATH_SEGMENT_LENGTH;
Len Brown4be44fc2005-08-05 00:44:28 -0400200 next_node = acpi_ns_get_parent_node(next_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
203 if (!size) {
Len Brown4be44fc2005-08-05 00:44:28 -0400204 size = 1; /* Root node case */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Len Brown4be44fc2005-08-05 00:44:28 -0400207 return (size + 1); /* +1 for null string terminator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*******************************************************************************
211 *
212 * FUNCTION: acpi_ns_handle_to_pathname
213 *
214 * PARAMETERS: target_handle - Handle of named object whose name is
215 * to be found
216 * Buffer - Where the pathname is returned
217 *
218 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
219 *
220 * DESCRIPTION: Build and return a full namespace pathname
221 *
222 ******************************************************************************/
223
224acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400225acpi_ns_handle_to_pathname(acpi_handle target_handle,
226 struct acpi_buffer * buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Len Brown4be44fc2005-08-05 00:44:28 -0400228 acpi_status status;
229 struct acpi_namespace_node *node;
230 acpi_size required_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Bob Mooreb229cf92006-04-21 17:15:00 -0400232 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Len Brown4be44fc2005-08-05 00:44:28 -0400234 node = acpi_ns_map_handle_to_node(target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400236 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
239 /* Determine size required for the caller buffer */
240
Len Brown4be44fc2005-08-05 00:44:28 -0400241 required_size = acpi_ns_get_pathname_length(node);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200242 if (!required_size) {
Bob Moore3c7db222008-08-04 11:13:01 +0800243 return_ACPI_STATUS(AE_BAD_PARAMETER);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Validate/Allocate/Clear caller buffer */
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 status = acpi_ut_initialize_buffer(buffer, required_size);
249 if (ACPI_FAILURE(status)) {
250 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 /* Build the path in the caller buffer */
254
Bob Moore3c7db222008-08-04 11:13:01 +0800255 status =
256 acpi_ns_build_external_path(node, required_size, buffer->pointer);
257 if (ACPI_FAILURE(status)) {
258 return_ACPI_STATUS(status);
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Bob Moore50eca3e2005-09-30 19:03:00 -0400261 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400262 (char *)buffer->pointer, (u32) required_size));
263 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}