blob: 90a0380fb8a04b51fd508817247531303825d18a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
3 * Module Name: nsnames - Name manipulation and search
4 *
5 ******************************************************************************/
6
7/*
Bob Moore25f044e2013-01-25 05:38:56 +00008 * Copyright (C) 2000 - 2013, 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 "amlcode.h"
47#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("nsnames")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*******************************************************************************
53 *
54 * FUNCTION: acpi_ns_build_external_path
55 *
Bob Mooreba494be2012-07-12 09:40:10 +080056 * PARAMETERS: node - NS node whose pathname is needed
57 * size - Size of the pathname
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * *name_buffer - Where to return the pathname
59 *
Bob Moore3c7db222008-08-04 11:13:01 +080060 * RETURN: Status
61 * Places the pathname into the name_buffer, in external format
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * (name segments separated by path separators)
63 *
64 * DESCRIPTION: Generate a full pathaname
65 *
66 ******************************************************************************/
Bob Moore3c7db222008-08-04 11:13:01 +080067acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040068acpi_ns_build_external_path(struct acpi_namespace_node *node,
69 acpi_size size, char *name_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Len Brown4be44fc2005-08-05 00:44:28 -040071 acpi_size index;
72 struct acpi_namespace_node *parent_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Bob Moore4a90c7e2006-01-13 16:22:00 -050074 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* Special case for root */
77
78 index = size - 1;
79 if (index < ACPI_NAME_SIZE) {
80 name_buffer[0] = AML_ROOT_PREFIX;
81 name_buffer[1] = 0;
Bob Moore3c7db222008-08-04 11:13:01 +080082 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
85 /* Store terminator byte, then build name backwards */
86
87 parent_node = node;
88 name_buffer[index] = 0;
89
90 while ((index > ACPI_NAME_SIZE) && (parent_node != acpi_gbl_root_node)) {
91 index -= ACPI_NAME_SIZE;
92
93 /* Put the name into the buffer */
94
Len Brown4be44fc2005-08-05 00:44:28 -040095 ACPI_MOVE_32_TO_32((name_buffer + index), &parent_node->name);
Alexey Starikovskiyc45b5c02010-05-26 11:53:07 +080096 parent_node = parent_node->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 /* Prefix name with the path separator */
99
100 index--;
101 name_buffer[index] = ACPI_PATH_SEPARATOR;
102 }
103
104 /* Overwrite final separator with the root prefix character */
105
106 name_buffer[index] = AML_ROOT_PREFIX;
107
108 if (index != 0) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500109 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800110 "Could not construct external pathname; index=%u, size=%u, Path=%s",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500111 (u32) index, (u32) size, &name_buffer[size]));
Bob Moore3c7db222008-08-04 11:13:01 +0800112
113 return (AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Bob Moore3c7db222008-08-04 11:13:01 +0800116 return (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*******************************************************************************
120 *
121 * FUNCTION: acpi_ns_get_external_pathname
122 *
Bob Mooreba494be2012-07-12 09:40:10 +0800123 * 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 *
Lv Zheng75c80442012-12-19 05:36:49 +0000129 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
130 * for error and debug statements.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *
132 ******************************************************************************/
133
Len Brown4be44fc2005-08-05 00:44:28 -0400134char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Bob Moore3c7db222008-08-04 11:13:01 +0800136 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400137 char *name_buffer;
138 acpi_size size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Bob Mooreb229cf92006-04-21 17:15:00 -0400140 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* Calculate required buffer size based on depth below root */
143
Len Brown4be44fc2005-08-05 00:44:28 -0400144 size = acpi_ns_get_pathname_length(node);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200145 if (!size) {
Bob Moore393a75d2008-09-27 11:12:36 +0800146 return_PTR(NULL);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* Allocate a buffer to be returned to caller */
150
Bob Moore83135242006-10-03 00:00:00 -0400151 name_buffer = ACPI_ALLOCATE_ZEROED(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!name_buffer) {
Bob Mooreb21245a2009-04-22 12:52:51 +0800153 ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
Len Brown4be44fc2005-08-05 00:44:28 -0400154 return_PTR(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156
157 /* Build the path in the allocated buffer */
158
Bob Moore3c7db222008-08-04 11:13:01 +0800159 status = acpi_ns_build_external_path(node, size, name_buffer);
160 if (ACPI_FAILURE(status)) {
Bob Moore393a75d2008-09-27 11:12:36 +0800161 ACPI_FREE(name_buffer);
162 return_PTR(NULL);
Bob Moore3c7db222008-08-04 11:13:01 +0800163 }
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165 return_PTR(name_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*******************************************************************************
169 *
170 * FUNCTION: acpi_ns_get_pathname_length
171 *
Bob Mooreba494be2012-07-12 09:40:10 +0800172 * PARAMETERS: node - Namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
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));
Bob Moore68aafc32012-10-31 02:26:01 +0000199 return (0);
Bob Moored8841642008-04-10 19:06:39 +0400200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 size += ACPI_PATH_SEGMENT_LENGTH;
Alexey Starikovskiyc45b5c02010-05-26 11:53:07 +0800202 next_node = next_node->parent;
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
Bob Mooreba494be2012-07-12 09:40:10 +0800218 * buffer - Where the pathname is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *
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
Bob Mooref24b6642009-12-11 14:57:00 +0800236 node = acpi_ns_validate_handle(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}