blob: aa16aeaa89379e7678cd6846e3c379d2fa256e67 [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 Moore7735ca02017-02-08 11:00:08 +08008 * Copyright (C) 2000 - 2017, 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * FUNCTION: acpi_ns_get_external_pathname
55 *
Bob Mooreba494be2012-07-12 09:40:10 +080056 * PARAMETERS: node - Namespace node whose pathname is needed
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 *
58 * RETURN: Pointer to storage containing the fully qualified name of
59 * the node, In external format (name segments separated by path
60 * separators.)
61 *
Lv Zheng75c80442012-12-19 05:36:49 +000062 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
63 * for error and debug statements.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
65 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040066char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Len Brown4be44fc2005-08-05 00:44:28 -040068 char *name_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Bob Mooreb229cf92006-04-21 17:15:00 -040070 ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Lv Zhengd1e7ffe2015-07-23 12:52:39 +080072 name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
Len Brown4be44fc2005-08-05 00:44:28 -040073 return_PTR(name_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*******************************************************************************
77 *
78 * FUNCTION: acpi_ns_get_pathname_length
79 *
Bob Mooreba494be2012-07-12 09:40:10 +080080 * PARAMETERS: node - Namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * RETURN: Length of path, including prefix
83 *
84 * DESCRIPTION: Get the length of the pathname string for this node
85 *
86 ******************************************************************************/
87
Len Brown4be44fc2005-08-05 00:44:28 -040088acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Len Brown4be44fc2005-08-05 00:44:28 -040090 acpi_size size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Len Brown4be44fc2005-08-05 00:44:28 -040092 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Lv Zhengd1e7ffe2015-07-23 12:52:39 +080094 size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
Lv Zhengd1e7ffe2015-07-23 12:52:39 +080095 return (size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*******************************************************************************
99 *
Lv Zheng523db192016-11-30 15:20:52 +0800100 * FUNCTION: acpi_ns_handle_to_name
101 *
102 * PARAMETERS: target_handle - Handle of named object whose name is
103 * to be found
104 * buffer - Where the name is returned
105 *
106 * RETURN: Status, Buffer is filled with name if status is AE_OK
107 *
108 * DESCRIPTION: Build and return a full namespace name
109 *
110 ******************************************************************************/
111
112acpi_status
113acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
114{
115 acpi_status status;
116 struct acpi_namespace_node *node;
117 const char *node_name;
118
119 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
120
121 node = acpi_ns_validate_handle(target_handle);
122 if (!node) {
123 return_ACPI_STATUS(AE_BAD_PARAMETER);
124 }
125
126 /* Validate/Allocate/Clear caller buffer */
127
128 status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
129 if (ACPI_FAILURE(status)) {
130 return_ACPI_STATUS(status);
131 }
132
133 /* Just copy the ACPI name from the Node and zero terminate it */
134
135 node_name = acpi_ut_get_node_name(node);
136 ACPI_MOVE_NAME(buffer->pointer, node_name);
137 ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
138
139 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
140 return_ACPI_STATUS(AE_OK);
141}
142
143/*******************************************************************************
144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * FUNCTION: acpi_ns_handle_to_pathname
146 *
147 * PARAMETERS: target_handle - Handle of named object whose name is
148 * to be found
Bob Mooreba494be2012-07-12 09:40:10 +0800149 * buffer - Where the pathname is returned
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800150 * no_trailing - Remove trailing '_' for each name
151 * segment
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
153 * RETURN: Status, Buffer is filled with pathname if status is AE_OK
154 *
155 * DESCRIPTION: Build and return a full namespace pathname
156 *
157 ******************************************************************************/
158
159acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400160acpi_ns_handle_to_pathname(acpi_handle target_handle,
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800161 struct acpi_buffer *buffer, u8 no_trailing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Len Brown4be44fc2005-08-05 00:44:28 -0400163 acpi_status status;
164 struct acpi_namespace_node *node;
165 acpi_size required_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Bob Mooreb229cf92006-04-21 17:15:00 -0400167 ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Bob Mooref24b6642009-12-11 14:57:00 +0800169 node = acpi_ns_validate_handle(target_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400171 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 /* Determine size required for the caller buffer */
175
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800176 required_size =
177 acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200178 if (!required_size) {
Bob Moore3c7db222008-08-04 11:13:01 +0800179 return_ACPI_STATUS(AE_BAD_PARAMETER);
Ingo Molnarf88133d2008-07-21 15:57:45 +0200180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* Validate/Allocate/Clear caller buffer */
183
Len Brown4be44fc2005-08-05 00:44:28 -0400184 status = acpi_ut_initialize_buffer(buffer, required_size);
185 if (ACPI_FAILURE(status)) {
186 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 /* Build the path in the caller buffer */
190
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800191 (void)acpi_ns_build_normalized_path(node, buffer->pointer,
192 required_size, no_trailing);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Bob Moore50eca3e2005-09-30 19:03:00 -0400194 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400195 (char *)buffer->pointer, (u32) required_size));
196 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800198
199/*******************************************************************************
200 *
201 * FUNCTION: acpi_ns_build_normalized_path
202 *
203 * PARAMETERS: node - Namespace node
204 * full_path - Where the path name is returned
205 * path_size - Size of returned path name buffer
206 * no_trailing - Remove trailing '_' from each name segment
207 *
208 * RETURN: Return 1 if the AML path is empty, otherwise returning (length
209 * of pathname + 1) which means the 'FullPath' contains a trailing
210 * null.
211 *
212 * DESCRIPTION: Build and return a full namespace pathname.
213 * Note that if the size of 'FullPath' isn't large enough to
214 * contain the namespace node's path name, the actual required
215 * buffer length is returned, and it should be greater than
216 * 'PathSize'. So callers are able to check the returning value
217 * to determine the buffer size of 'FullPath'.
218 *
219 ******************************************************************************/
220
221u32
222acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
223 char *full_path, u32 path_size, u8 no_trailing)
224{
225 u32 length = 0, i;
226 char name[ACPI_NAME_SIZE];
227 u8 do_no_trailing;
228 char c, *left, *right;
229 struct acpi_namespace_node *next_node;
230
231 ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
232
233#define ACPI_PATH_PUT8(path, size, byte, length) \
234 do { \
235 if ((length) < (size)) \
236 { \
237 (path)[(length)] = (byte); \
238 } \
239 (length)++; \
240 } while (0)
241
242 /*
243 * Make sure the path_size is correct, so that we don't need to
244 * validate both full_path and path_size.
245 */
246 if (!full_path) {
247 path_size = 0;
248 }
249
250 if (!node) {
251 goto build_trailing_null;
252 }
253
254 next_node = node;
255 while (next_node && next_node != acpi_gbl_root_node) {
256 if (next_node != node) {
257 ACPI_PATH_PUT8(full_path, path_size,
258 AML_DUAL_NAME_PREFIX, length);
259 }
Bob Moore1fad8732015-12-29 13:54:36 +0800260
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800261 ACPI_MOVE_32_TO_32(name, &next_node->name);
262 do_no_trailing = no_trailing;
263 for (i = 0; i < 4; i++) {
264 c = name[4 - i - 1];
265 if (do_no_trailing && c != '_') {
266 do_no_trailing = FALSE;
267 }
268 if (!do_no_trailing) {
269 ACPI_PATH_PUT8(full_path, path_size, c, length);
270 }
271 }
Bob Moore1fad8732015-12-29 13:54:36 +0800272
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800273 next_node = next_node->parent;
274 }
Bob Moore1fad8732015-12-29 13:54:36 +0800275
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800276 ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
277
278 /* Reverse the path string */
279
280 if (length <= path_size) {
281 left = full_path;
282 right = full_path + length - 1;
Bob Moore1fad8732015-12-29 13:54:36 +0800283
Lv Zhengd1e7ffe2015-07-23 12:52:39 +0800284 while (left < right) {
285 c = *left;
286 *left++ = *right;
287 *right-- = c;
288 }
289 }
290
291 /* Append the trailing null */
292
293build_trailing_null:
294 ACPI_PATH_PUT8(full_path, path_size, '\0', length);
295
296#undef ACPI_PATH_PUT8
297
298 return_UINT32(length);
299}
300
301/*******************************************************************************
302 *
303 * FUNCTION: acpi_ns_get_normalized_pathname
304 *
305 * PARAMETERS: node - Namespace node whose pathname is needed
306 * no_trailing - Remove trailing '_' from each name segment
307 *
308 * RETURN: Pointer to storage containing the fully qualified name of
309 * the node, In external format (name segments separated by path
310 * separators.)
311 *
312 * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
313 * for error and debug statements. All trailing '_' will be
314 * removed from the full pathname if 'NoTrailing' is specified..
315 *
316 ******************************************************************************/
317
318char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
319 u8 no_trailing)
320{
321 char *name_buffer;
322 acpi_size size;
323
324 ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
325
326 /* Calculate required buffer size based on depth below root */
327
328 size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
329 if (!size) {
330 return_PTR(NULL);
331 }
332
333 /* Allocate a buffer to be returned to caller */
334
335 name_buffer = ACPI_ALLOCATE_ZEROED(size);
336 if (!name_buffer) {
337 ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
338 return_PTR(NULL);
339 }
340
341 /* Build the path in the allocated buffer */
342
343 (void)acpi_ns_build_normalized_path(node, name_buffer, size,
344 no_trailing);
345
346 return_PTR(name_buffer);
347}