nsnames.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsnames - Name manipulation and search
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "amlcode.h"
  45. #include "acnamesp.h"
  46. #define _COMPONENT ACPI_NAMESPACE
  47. ACPI_MODULE_NAME("nsnames")
  48. /*******************************************************************************
  49. *
  50. * FUNCTION: acpi_ns_get_external_pathname
  51. *
  52. * PARAMETERS: node - Namespace node whose pathname is needed
  53. *
  54. * RETURN: Pointer to storage containing the fully qualified name of
  55. * the node, In external format (name segments separated by path
  56. * separators.)
  57. *
  58. * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
  59. * for error and debug statements.
  60. *
  61. ******************************************************************************/
  62. char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
  63. {
  64. char *name_buffer;
  65. ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
  66. name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
  67. return_PTR(name_buffer);
  68. }
  69. /*******************************************************************************
  70. *
  71. * FUNCTION: acpi_ns_get_pathname_length
  72. *
  73. * PARAMETERS: node - Namespace node
  74. *
  75. * RETURN: Length of path, including prefix
  76. *
  77. * DESCRIPTION: Get the length of the pathname string for this node
  78. *
  79. ******************************************************************************/
  80. acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
  81. {
  82. acpi_size size;
  83. ACPI_FUNCTION_ENTRY();
  84. size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
  85. return (size);
  86. }
  87. /*******************************************************************************
  88. *
  89. * FUNCTION: acpi_ns_handle_to_pathname
  90. *
  91. * PARAMETERS: target_handle - Handle of named object whose name is
  92. * to be found
  93. * buffer - Where the pathname is returned
  94. * no_trailing - Remove trailing '_' for each name
  95. * segment
  96. *
  97. * RETURN: Status, Buffer is filled with pathname if status is AE_OK
  98. *
  99. * DESCRIPTION: Build and return a full namespace pathname
  100. *
  101. ******************************************************************************/
  102. acpi_status
  103. acpi_ns_handle_to_pathname(acpi_handle target_handle,
  104. struct acpi_buffer * buffer, u8 no_trailing)
  105. {
  106. acpi_status status;
  107. struct acpi_namespace_node *node;
  108. acpi_size required_size;
  109. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
  110. node = acpi_ns_validate_handle(target_handle);
  111. if (!node) {
  112. return_ACPI_STATUS(AE_BAD_PARAMETER);
  113. }
  114. /* Determine size required for the caller buffer */
  115. required_size =
  116. acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  117. if (!required_size) {
  118. return_ACPI_STATUS(AE_BAD_PARAMETER);
  119. }
  120. /* Validate/Allocate/Clear caller buffer */
  121. status = acpi_ut_initialize_buffer(buffer, required_size);
  122. if (ACPI_FAILURE(status)) {
  123. return_ACPI_STATUS(status);
  124. }
  125. /* Build the path in the caller buffer */
  126. (void)acpi_ns_build_normalized_path(node, buffer->pointer,
  127. required_size, no_trailing);
  128. if (ACPI_FAILURE(status)) {
  129. return_ACPI_STATUS(status);
  130. }
  131. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
  132. (char *)buffer->pointer, (u32) required_size));
  133. return_ACPI_STATUS(AE_OK);
  134. }
  135. /*******************************************************************************
  136. *
  137. * FUNCTION: acpi_ns_build_normalized_path
  138. *
  139. * PARAMETERS: node - Namespace node
  140. * full_path - Where the path name is returned
  141. * path_size - Size of returned path name buffer
  142. * no_trailing - Remove trailing '_' from each name segment
  143. *
  144. * RETURN: Return 1 if the AML path is empty, otherwise returning (length
  145. * of pathname + 1) which means the 'FullPath' contains a trailing
  146. * null.
  147. *
  148. * DESCRIPTION: Build and return a full namespace pathname.
  149. * Note that if the size of 'FullPath' isn't large enough to
  150. * contain the namespace node's path name, the actual required
  151. * buffer length is returned, and it should be greater than
  152. * 'PathSize'. So callers are able to check the returning value
  153. * to determine the buffer size of 'FullPath'.
  154. *
  155. ******************************************************************************/
  156. u32
  157. acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
  158. char *full_path, u32 path_size, u8 no_trailing)
  159. {
  160. u32 length = 0, i;
  161. char name[ACPI_NAME_SIZE];
  162. u8 do_no_trailing;
  163. char c, *left, *right;
  164. struct acpi_namespace_node *next_node;
  165. ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
  166. #define ACPI_PATH_PUT8(path, size, byte, length) \
  167. do { \
  168. if ((length) < (size)) \
  169. { \
  170. (path)[(length)] = (byte); \
  171. } \
  172. (length)++; \
  173. } while (0)
  174. /*
  175. * Make sure the path_size is correct, so that we don't need to
  176. * validate both full_path and path_size.
  177. */
  178. if (!full_path) {
  179. path_size = 0;
  180. }
  181. if (!node) {
  182. goto build_trailing_null;
  183. }
  184. next_node = node;
  185. while (next_node && next_node != acpi_gbl_root_node) {
  186. if (next_node != node) {
  187. ACPI_PATH_PUT8(full_path, path_size,
  188. AML_DUAL_NAME_PREFIX, length);
  189. }
  190. ACPI_MOVE_32_TO_32(name, &next_node->name);
  191. do_no_trailing = no_trailing;
  192. for (i = 0; i < 4; i++) {
  193. c = name[4 - i - 1];
  194. if (do_no_trailing && c != '_') {
  195. do_no_trailing = FALSE;
  196. }
  197. if (!do_no_trailing) {
  198. ACPI_PATH_PUT8(full_path, path_size, c, length);
  199. }
  200. }
  201. next_node = next_node->parent;
  202. }
  203. ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
  204. /* Reverse the path string */
  205. if (length <= path_size) {
  206. left = full_path;
  207. right = full_path + length - 1;
  208. while (left < right) {
  209. c = *left;
  210. *left++ = *right;
  211. *right-- = c;
  212. }
  213. }
  214. /* Append the trailing null */
  215. build_trailing_null:
  216. ACPI_PATH_PUT8(full_path, path_size, '\0', length);
  217. #undef ACPI_PATH_PUT8
  218. return_UINT32(length);
  219. }
  220. /*******************************************************************************
  221. *
  222. * FUNCTION: acpi_ns_get_normalized_pathname
  223. *
  224. * PARAMETERS: node - Namespace node whose pathname is needed
  225. * no_trailing - Remove trailing '_' from each name segment
  226. *
  227. * RETURN: Pointer to storage containing the fully qualified name of
  228. * the node, In external format (name segments separated by path
  229. * separators.)
  230. *
  231. * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
  232. * for error and debug statements. All trailing '_' will be
  233. * removed from the full pathname if 'NoTrailing' is specified..
  234. *
  235. ******************************************************************************/
  236. char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
  237. u8 no_trailing)
  238. {
  239. char *name_buffer;
  240. acpi_size size;
  241. ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
  242. /* Calculate required buffer size based on depth below root */
  243. size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  244. if (!size) {
  245. return_PTR(NULL);
  246. }
  247. /* Allocate a buffer to be returned to caller */
  248. name_buffer = ACPI_ALLOCATE_ZEROED(size);
  249. if (!name_buffer) {
  250. ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
  251. return_PTR(NULL);
  252. }
  253. /* Build the path in the allocated buffer */
  254. (void)acpi_ns_build_normalized_path(node, name_buffer, size,
  255. no_trailing);
  256. return_PTR(name_buffer);
  257. }