nsnames.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsnames - Name manipulation and search
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, 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_name
  90. *
  91. * PARAMETERS: target_handle - Handle of named object whose name is
  92. * to be found
  93. * buffer - Where the name is returned
  94. *
  95. * RETURN: Status, Buffer is filled with name if status is AE_OK
  96. *
  97. * DESCRIPTION: Build and return a full namespace name
  98. *
  99. ******************************************************************************/
  100. acpi_status
  101. acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
  102. {
  103. acpi_status status;
  104. struct acpi_namespace_node *node;
  105. const char *node_name;
  106. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
  107. node = acpi_ns_validate_handle(target_handle);
  108. if (!node) {
  109. return_ACPI_STATUS(AE_BAD_PARAMETER);
  110. }
  111. /* Validate/Allocate/Clear caller buffer */
  112. status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
  113. if (ACPI_FAILURE(status)) {
  114. return_ACPI_STATUS(status);
  115. }
  116. /* Just copy the ACPI name from the Node and zero terminate it */
  117. node_name = acpi_ut_get_node_name(node);
  118. ACPI_MOVE_NAME(buffer->pointer, node_name);
  119. ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
  120. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
  121. return_ACPI_STATUS(AE_OK);
  122. }
  123. /*******************************************************************************
  124. *
  125. * FUNCTION: acpi_ns_handle_to_pathname
  126. *
  127. * PARAMETERS: target_handle - Handle of named object whose name is
  128. * to be found
  129. * buffer - Where the pathname is returned
  130. * no_trailing - Remove trailing '_' for each name
  131. * segment
  132. *
  133. * RETURN: Status, Buffer is filled with pathname if status is AE_OK
  134. *
  135. * DESCRIPTION: Build and return a full namespace pathname
  136. *
  137. ******************************************************************************/
  138. acpi_status
  139. acpi_ns_handle_to_pathname(acpi_handle target_handle,
  140. struct acpi_buffer *buffer, u8 no_trailing)
  141. {
  142. acpi_status status;
  143. struct acpi_namespace_node *node;
  144. acpi_size required_size;
  145. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
  146. node = acpi_ns_validate_handle(target_handle);
  147. if (!node) {
  148. return_ACPI_STATUS(AE_BAD_PARAMETER);
  149. }
  150. /* Determine size required for the caller buffer */
  151. required_size =
  152. acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  153. if (!required_size) {
  154. return_ACPI_STATUS(AE_BAD_PARAMETER);
  155. }
  156. /* Validate/Allocate/Clear caller buffer */
  157. status = acpi_ut_initialize_buffer(buffer, required_size);
  158. if (ACPI_FAILURE(status)) {
  159. return_ACPI_STATUS(status);
  160. }
  161. /* Build the path in the caller buffer */
  162. (void)acpi_ns_build_normalized_path(node, buffer->pointer,
  163. required_size, no_trailing);
  164. if (ACPI_FAILURE(status)) {
  165. return_ACPI_STATUS(status);
  166. }
  167. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
  168. (char *)buffer->pointer, (u32) required_size));
  169. return_ACPI_STATUS(AE_OK);
  170. }
  171. /*******************************************************************************
  172. *
  173. * FUNCTION: acpi_ns_build_normalized_path
  174. *
  175. * PARAMETERS: node - Namespace node
  176. * full_path - Where the path name is returned
  177. * path_size - Size of returned path name buffer
  178. * no_trailing - Remove trailing '_' from each name segment
  179. *
  180. * RETURN: Return 1 if the AML path is empty, otherwise returning (length
  181. * of pathname + 1) which means the 'FullPath' contains a trailing
  182. * null.
  183. *
  184. * DESCRIPTION: Build and return a full namespace pathname.
  185. * Note that if the size of 'FullPath' isn't large enough to
  186. * contain the namespace node's path name, the actual required
  187. * buffer length is returned, and it should be greater than
  188. * 'PathSize'. So callers are able to check the returning value
  189. * to determine the buffer size of 'FullPath'.
  190. *
  191. ******************************************************************************/
  192. u32
  193. acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
  194. char *full_path, u32 path_size, u8 no_trailing)
  195. {
  196. u32 length = 0, i;
  197. char name[ACPI_NAME_SIZE];
  198. u8 do_no_trailing;
  199. char c, *left, *right;
  200. struct acpi_namespace_node *next_node;
  201. ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
  202. #define ACPI_PATH_PUT8(path, size, byte, length) \
  203. do { \
  204. if ((length) < (size)) \
  205. { \
  206. (path)[(length)] = (byte); \
  207. } \
  208. (length)++; \
  209. } while (0)
  210. /*
  211. * Make sure the path_size is correct, so that we don't need to
  212. * validate both full_path and path_size.
  213. */
  214. if (!full_path) {
  215. path_size = 0;
  216. }
  217. if (!node) {
  218. goto build_trailing_null;
  219. }
  220. next_node = node;
  221. while (next_node && next_node != acpi_gbl_root_node) {
  222. if (next_node != node) {
  223. ACPI_PATH_PUT8(full_path, path_size,
  224. AML_DUAL_NAME_PREFIX, length);
  225. }
  226. ACPI_MOVE_32_TO_32(name, &next_node->name);
  227. do_no_trailing = no_trailing;
  228. for (i = 0; i < 4; i++) {
  229. c = name[4 - i - 1];
  230. if (do_no_trailing && c != '_') {
  231. do_no_trailing = FALSE;
  232. }
  233. if (!do_no_trailing) {
  234. ACPI_PATH_PUT8(full_path, path_size, c, length);
  235. }
  236. }
  237. next_node = next_node->parent;
  238. }
  239. ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
  240. /* Reverse the path string */
  241. if (length <= path_size) {
  242. left = full_path;
  243. right = full_path + length - 1;
  244. while (left < right) {
  245. c = *left;
  246. *left++ = *right;
  247. *right-- = c;
  248. }
  249. }
  250. /* Append the trailing null */
  251. build_trailing_null:
  252. ACPI_PATH_PUT8(full_path, path_size, '\0', length);
  253. #undef ACPI_PATH_PUT8
  254. return_UINT32(length);
  255. }
  256. /*******************************************************************************
  257. *
  258. * FUNCTION: acpi_ns_get_normalized_pathname
  259. *
  260. * PARAMETERS: node - Namespace node whose pathname is needed
  261. * no_trailing - Remove trailing '_' from each name segment
  262. *
  263. * RETURN: Pointer to storage containing the fully qualified name of
  264. * the node, In external format (name segments separated by path
  265. * separators.)
  266. *
  267. * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
  268. * for error and debug statements. All trailing '_' will be
  269. * removed from the full pathname if 'NoTrailing' is specified..
  270. *
  271. ******************************************************************************/
  272. char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
  273. u8 no_trailing)
  274. {
  275. char *name_buffer;
  276. acpi_size size;
  277. ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
  278. /* Calculate required buffer size based on depth below root */
  279. size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  280. if (!size) {
  281. return_PTR(NULL);
  282. }
  283. /* Allocate a buffer to be returned to caller */
  284. name_buffer = ACPI_ALLOCATE_ZEROED(size);
  285. if (!name_buffer) {
  286. ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
  287. return_PTR(NULL);
  288. }
  289. /* Build the path in the allocated buffer */
  290. (void)acpi_ns_build_normalized_path(node, name_buffer, size,
  291. no_trailing);
  292. return_PTR(name_buffer);
  293. }