Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * |
| 3 | * Module Name: nsnames - Name manipulation and search |
| 4 | * |
| 5 | ******************************************************************************/ |
| 6 | |
| 7 | /* |
Bob Moore | c8100dc | 2016-01-15 08:17:03 +0800 | [diff] [blame] | 8 | * Copyright (C) 2000 - 2016, Intel Corp. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <acpi/acpi.h> |
Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 45 | #include "accommon.h" |
| 46 | #include "amlcode.h" |
| 47 | #include "acnamesp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #define _COMPONENT ACPI_NAMESPACE |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 50 | ACPI_MODULE_NAME("nsnames") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /******************************************************************************* |
| 53 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * FUNCTION: acpi_ns_get_external_pathname |
| 55 | * |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 56 | * PARAMETERS: node - Namespace node whose pathname is needed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * |
| 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 Zheng | 75c8044 | 2012-12-19 05:36:49 +0000 | [diff] [blame] | 62 | * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually |
| 63 | * for error and debug statements. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * |
| 65 | ******************************************************************************/ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 66 | char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 68 | char *name_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 70 | ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 72 | name_buffer = acpi_ns_get_normalized_pathname(node, FALSE); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 73 | return_PTR(name_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /******************************************************************************* |
| 77 | * |
| 78 | * FUNCTION: acpi_ns_get_pathname_length |
| 79 | * |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 80 | * PARAMETERS: node - Namespace node |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | * |
| 82 | * RETURN: Length of path, including prefix |
| 83 | * |
| 84 | * DESCRIPTION: Get the length of the pathname string for this node |
| 85 | * |
| 86 | ******************************************************************************/ |
| 87 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 88 | acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 90 | acpi_size size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 92 | ACPI_FUNCTION_ENTRY(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 94 | size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE); |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 95 | return (size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /******************************************************************************* |
| 99 | * |
Lv Zheng | 523db19 | 2016-11-30 15:20:52 +0800 | [diff] [blame] | 100 | * 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 | |
| 112 | acpi_status |
| 113 | acpi_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * FUNCTION: acpi_ns_handle_to_pathname |
| 146 | * |
| 147 | * PARAMETERS: target_handle - Handle of named object whose name is |
| 148 | * to be found |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 149 | * buffer - Where the pathname is returned |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 150 | * no_trailing - Remove trailing '_' for each name |
| 151 | * segment |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | * |
| 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 | |
| 159 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 160 | acpi_ns_handle_to_pathname(acpi_handle target_handle, |
Lv Zheng | f5c1e1c | 2016-05-05 12:57:53 +0800 | [diff] [blame] | 161 | struct acpi_buffer *buffer, u8 no_trailing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 163 | acpi_status status; |
| 164 | struct acpi_namespace_node *node; |
| 165 | acpi_size required_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 167 | ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Bob Moore | f24b664 | 2009-12-11 14:57:00 +0800 | [diff] [blame] | 169 | node = acpi_ns_validate_handle(target_handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (!node) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 171 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* Determine size required for the caller buffer */ |
| 175 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 176 | required_size = |
| 177 | acpi_ns_build_normalized_path(node, NULL, 0, no_trailing); |
Ingo Molnar | f88133d | 2008-07-21 15:57:45 +0200 | [diff] [blame] | 178 | if (!required_size) { |
Bob Moore | 3c7db22 | 2008-08-04 11:13:01 +0800 | [diff] [blame] | 179 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Ingo Molnar | f88133d | 2008-07-21 15:57:45 +0200 | [diff] [blame] | 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | /* Validate/Allocate/Clear caller buffer */ |
| 183 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 184 | status = acpi_ut_initialize_buffer(buffer, required_size); |
| 185 | if (ACPI_FAILURE(status)) { |
| 186 | return_ACPI_STATUS(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* Build the path in the caller buffer */ |
| 190 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 191 | (void)acpi_ns_build_normalized_path(node, buffer->pointer, |
| 192 | required_size, no_trailing); |
Bob Moore | 3c7db22 | 2008-08-04 11:13:01 +0800 | [diff] [blame] | 193 | if (ACPI_FAILURE(status)) { |
| 194 | return_ACPI_STATUS(status); |
| 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Bob Moore | 50eca3e | 2005-09-30 19:03:00 -0400 | [diff] [blame] | 197 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 198 | (char *)buffer->pointer, (u32) required_size)); |
| 199 | return_ACPI_STATUS(AE_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 201 | |
| 202 | /******************************************************************************* |
| 203 | * |
| 204 | * FUNCTION: acpi_ns_build_normalized_path |
| 205 | * |
| 206 | * PARAMETERS: node - Namespace node |
| 207 | * full_path - Where the path name is returned |
| 208 | * path_size - Size of returned path name buffer |
| 209 | * no_trailing - Remove trailing '_' from each name segment |
| 210 | * |
| 211 | * RETURN: Return 1 if the AML path is empty, otherwise returning (length |
| 212 | * of pathname + 1) which means the 'FullPath' contains a trailing |
| 213 | * null. |
| 214 | * |
| 215 | * DESCRIPTION: Build and return a full namespace pathname. |
| 216 | * Note that if the size of 'FullPath' isn't large enough to |
| 217 | * contain the namespace node's path name, the actual required |
| 218 | * buffer length is returned, and it should be greater than |
| 219 | * 'PathSize'. So callers are able to check the returning value |
| 220 | * to determine the buffer size of 'FullPath'. |
| 221 | * |
| 222 | ******************************************************************************/ |
| 223 | |
| 224 | u32 |
| 225 | acpi_ns_build_normalized_path(struct acpi_namespace_node *node, |
| 226 | char *full_path, u32 path_size, u8 no_trailing) |
| 227 | { |
| 228 | u32 length = 0, i; |
| 229 | char name[ACPI_NAME_SIZE]; |
| 230 | u8 do_no_trailing; |
| 231 | char c, *left, *right; |
| 232 | struct acpi_namespace_node *next_node; |
| 233 | |
| 234 | ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node); |
| 235 | |
| 236 | #define ACPI_PATH_PUT8(path, size, byte, length) \ |
| 237 | do { \ |
| 238 | if ((length) < (size)) \ |
| 239 | { \ |
| 240 | (path)[(length)] = (byte); \ |
| 241 | } \ |
| 242 | (length)++; \ |
| 243 | } while (0) |
| 244 | |
| 245 | /* |
| 246 | * Make sure the path_size is correct, so that we don't need to |
| 247 | * validate both full_path and path_size. |
| 248 | */ |
| 249 | if (!full_path) { |
| 250 | path_size = 0; |
| 251 | } |
| 252 | |
| 253 | if (!node) { |
| 254 | goto build_trailing_null; |
| 255 | } |
| 256 | |
| 257 | next_node = node; |
| 258 | while (next_node && next_node != acpi_gbl_root_node) { |
| 259 | if (next_node != node) { |
| 260 | ACPI_PATH_PUT8(full_path, path_size, |
| 261 | AML_DUAL_NAME_PREFIX, length); |
| 262 | } |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 263 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 264 | ACPI_MOVE_32_TO_32(name, &next_node->name); |
| 265 | do_no_trailing = no_trailing; |
| 266 | for (i = 0; i < 4; i++) { |
| 267 | c = name[4 - i - 1]; |
| 268 | if (do_no_trailing && c != '_') { |
| 269 | do_no_trailing = FALSE; |
| 270 | } |
| 271 | if (!do_no_trailing) { |
| 272 | ACPI_PATH_PUT8(full_path, path_size, c, length); |
| 273 | } |
| 274 | } |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 275 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 276 | next_node = next_node->parent; |
| 277 | } |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 278 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 279 | ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length); |
| 280 | |
| 281 | /* Reverse the path string */ |
| 282 | |
| 283 | if (length <= path_size) { |
| 284 | left = full_path; |
| 285 | right = full_path + length - 1; |
Bob Moore | 1fad873 | 2015-12-29 13:54:36 +0800 | [diff] [blame] | 286 | |
Lv Zheng | d1e7ffe | 2015-07-23 12:52:39 +0800 | [diff] [blame] | 287 | while (left < right) { |
| 288 | c = *left; |
| 289 | *left++ = *right; |
| 290 | *right-- = c; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /* Append the trailing null */ |
| 295 | |
| 296 | build_trailing_null: |
| 297 | ACPI_PATH_PUT8(full_path, path_size, '\0', length); |
| 298 | |
| 299 | #undef ACPI_PATH_PUT8 |
| 300 | |
| 301 | return_UINT32(length); |
| 302 | } |
| 303 | |
| 304 | /******************************************************************************* |
| 305 | * |
| 306 | * FUNCTION: acpi_ns_get_normalized_pathname |
| 307 | * |
| 308 | * PARAMETERS: node - Namespace node whose pathname is needed |
| 309 | * no_trailing - Remove trailing '_' from each name segment |
| 310 | * |
| 311 | * RETURN: Pointer to storage containing the fully qualified name of |
| 312 | * the node, In external format (name segments separated by path |
| 313 | * separators.) |
| 314 | * |
| 315 | * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually |
| 316 | * for error and debug statements. All trailing '_' will be |
| 317 | * removed from the full pathname if 'NoTrailing' is specified.. |
| 318 | * |
| 319 | ******************************************************************************/ |
| 320 | |
| 321 | char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node, |
| 322 | u8 no_trailing) |
| 323 | { |
| 324 | char *name_buffer; |
| 325 | acpi_size size; |
| 326 | |
| 327 | ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node); |
| 328 | |
| 329 | /* Calculate required buffer size based on depth below root */ |
| 330 | |
| 331 | size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing); |
| 332 | if (!size) { |
| 333 | return_PTR(NULL); |
| 334 | } |
| 335 | |
| 336 | /* Allocate a buffer to be returned to caller */ |
| 337 | |
| 338 | name_buffer = ACPI_ALLOCATE_ZEROED(size); |
| 339 | if (!name_buffer) { |
| 340 | ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size)); |
| 341 | return_PTR(NULL); |
| 342 | } |
| 343 | |
| 344 | /* Build the path in the allocated buffer */ |
| 345 | |
| 346 | (void)acpi_ns_build_normalized_path(node, name_buffer, size, |
| 347 | no_trailing); |
| 348 | |
| 349 | return_PTR(name_buffer); |
| 350 | } |