nsnames.c 11 KB

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