nsnames.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: nsnames - Name manipulation and search
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "amlcode.h"
  10. #include "acnamesp.h"
  11. #define _COMPONENT ACPI_NAMESPACE
  12. ACPI_MODULE_NAME("nsnames")
  13. /* Local Prototypes */
  14. static void acpi_ns_normalize_pathname(char *original_path);
  15. /*******************************************************************************
  16. *
  17. * FUNCTION: acpi_ns_get_external_pathname
  18. *
  19. * PARAMETERS: node - Namespace node whose pathname is needed
  20. *
  21. * RETURN: Pointer to storage containing the fully qualified name of
  22. * the node, In external format (name segments separated by path
  23. * separators.)
  24. *
  25. * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
  26. * for error and debug statements.
  27. *
  28. ******************************************************************************/
  29. char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
  30. {
  31. char *name_buffer;
  32. ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
  33. name_buffer = acpi_ns_get_normalized_pathname(node, FALSE);
  34. return_PTR(name_buffer);
  35. }
  36. /*******************************************************************************
  37. *
  38. * FUNCTION: acpi_ns_get_pathname_length
  39. *
  40. * PARAMETERS: node - Namespace node
  41. *
  42. * RETURN: Length of path, including prefix
  43. *
  44. * DESCRIPTION: Get the length of the pathname string for this node
  45. *
  46. ******************************************************************************/
  47. acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
  48. {
  49. acpi_size size;
  50. /* Validate the Node */
  51. if (ACPI_GET_DESCRIPTOR_TYPE(node) != ACPI_DESC_TYPE_NAMED) {
  52. ACPI_ERROR((AE_INFO,
  53. "Invalid/cached reference target node: %p, descriptor type %d",
  54. node, ACPI_GET_DESCRIPTOR_TYPE(node)));
  55. return (0);
  56. }
  57. size = acpi_ns_build_normalized_path(node, NULL, 0, FALSE);
  58. return (size);
  59. }
  60. /*******************************************************************************
  61. *
  62. * FUNCTION: acpi_ns_handle_to_name
  63. *
  64. * PARAMETERS: target_handle - Handle of named object whose name is
  65. * to be found
  66. * buffer - Where the name is returned
  67. *
  68. * RETURN: Status, Buffer is filled with name if status is AE_OK
  69. *
  70. * DESCRIPTION: Build and return a full namespace name
  71. *
  72. ******************************************************************************/
  73. acpi_status
  74. acpi_ns_handle_to_name(acpi_handle target_handle, struct acpi_buffer *buffer)
  75. {
  76. acpi_status status;
  77. struct acpi_namespace_node *node;
  78. const char *node_name;
  79. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_name, target_handle);
  80. node = acpi_ns_validate_handle(target_handle);
  81. if (!node) {
  82. return_ACPI_STATUS(AE_BAD_PARAMETER);
  83. }
  84. /* Validate/Allocate/Clear caller buffer */
  85. status = acpi_ut_initialize_buffer(buffer, ACPI_PATH_SEGMENT_LENGTH);
  86. if (ACPI_FAILURE(status)) {
  87. return_ACPI_STATUS(status);
  88. }
  89. /* Just copy the ACPI name from the Node and zero terminate it */
  90. node_name = acpi_ut_get_node_name(node);
  91. ACPI_MOVE_NAME(buffer->pointer, node_name);
  92. ((char *)buffer->pointer)[ACPI_NAME_SIZE] = 0;
  93. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%4.4s\n", (char *)buffer->pointer));
  94. return_ACPI_STATUS(AE_OK);
  95. }
  96. /*******************************************************************************
  97. *
  98. * FUNCTION: acpi_ns_handle_to_pathname
  99. *
  100. * PARAMETERS: target_handle - Handle of named object whose name is
  101. * to be found
  102. * buffer - Where the pathname is returned
  103. * no_trailing - Remove trailing '_' for each name
  104. * segment
  105. *
  106. * RETURN: Status, Buffer is filled with pathname if status is AE_OK
  107. *
  108. * DESCRIPTION: Build and return a full namespace pathname
  109. *
  110. ******************************************************************************/
  111. acpi_status
  112. acpi_ns_handle_to_pathname(acpi_handle target_handle,
  113. struct acpi_buffer *buffer, u8 no_trailing)
  114. {
  115. acpi_status status;
  116. struct acpi_namespace_node *node;
  117. acpi_size required_size;
  118. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
  119. node = acpi_ns_validate_handle(target_handle);
  120. if (!node) {
  121. return_ACPI_STATUS(AE_BAD_PARAMETER);
  122. }
  123. /* Determine size required for the caller buffer */
  124. required_size =
  125. acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  126. if (!required_size) {
  127. return_ACPI_STATUS(AE_BAD_PARAMETER);
  128. }
  129. /* Validate/Allocate/Clear caller buffer */
  130. status = acpi_ut_initialize_buffer(buffer, required_size);
  131. if (ACPI_FAILURE(status)) {
  132. return_ACPI_STATUS(status);
  133. }
  134. /* Build the path in the caller buffer */
  135. (void)acpi_ns_build_normalized_path(node, buffer->pointer,
  136. required_size, no_trailing);
  137. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
  138. (char *)buffer->pointer, (u32) required_size));
  139. return_ACPI_STATUS(AE_OK);
  140. }
  141. /*******************************************************************************
  142. *
  143. * FUNCTION: acpi_ns_build_normalized_path
  144. *
  145. * PARAMETERS: node - Namespace node
  146. * full_path - Where the path name is returned
  147. * path_size - Size of returned path name buffer
  148. * no_trailing - Remove trailing '_' from each name segment
  149. *
  150. * RETURN: Return 1 if the AML path is empty, otherwise returning (length
  151. * of pathname + 1) which means the 'FullPath' contains a trailing
  152. * null.
  153. *
  154. * DESCRIPTION: Build and return a full namespace pathname.
  155. * Note that if the size of 'FullPath' isn't large enough to
  156. * contain the namespace node's path name, the actual required
  157. * buffer length is returned, and it should be greater than
  158. * 'PathSize'. So callers are able to check the returning value
  159. * to determine the buffer size of 'FullPath'.
  160. *
  161. ******************************************************************************/
  162. u32
  163. acpi_ns_build_normalized_path(struct acpi_namespace_node *node,
  164. char *full_path, u32 path_size, u8 no_trailing)
  165. {
  166. u32 length = 0, i;
  167. char name[ACPI_NAME_SIZE];
  168. u8 do_no_trailing;
  169. char c, *left, *right;
  170. struct acpi_namespace_node *next_node;
  171. ACPI_FUNCTION_TRACE_PTR(ns_build_normalized_path, node);
  172. #define ACPI_PATH_PUT8(path, size, byte, length) \
  173. do { \
  174. if ((length) < (size)) \
  175. { \
  176. (path)[(length)] = (byte); \
  177. } \
  178. (length)++; \
  179. } while (0)
  180. /*
  181. * Make sure the path_size is correct, so that we don't need to
  182. * validate both full_path and path_size.
  183. */
  184. if (!full_path) {
  185. path_size = 0;
  186. }
  187. if (!node) {
  188. goto build_trailing_null;
  189. }
  190. next_node = node;
  191. while (next_node && next_node != acpi_gbl_root_node) {
  192. if (next_node != node) {
  193. ACPI_PATH_PUT8(full_path, path_size,
  194. AML_DUAL_NAME_PREFIX, length);
  195. }
  196. ACPI_MOVE_32_TO_32(name, &next_node->name);
  197. do_no_trailing = no_trailing;
  198. for (i = 0; i < 4; i++) {
  199. c = name[4 - i - 1];
  200. if (do_no_trailing && c != '_') {
  201. do_no_trailing = FALSE;
  202. }
  203. if (!do_no_trailing) {
  204. ACPI_PATH_PUT8(full_path, path_size, c, length);
  205. }
  206. }
  207. next_node = next_node->parent;
  208. }
  209. ACPI_PATH_PUT8(full_path, path_size, AML_ROOT_PREFIX, length);
  210. /* Reverse the path string */
  211. if (length <= path_size) {
  212. left = full_path;
  213. right = full_path + length - 1;
  214. while (left < right) {
  215. c = *left;
  216. *left++ = *right;
  217. *right-- = c;
  218. }
  219. }
  220. /* Append the trailing null */
  221. build_trailing_null:
  222. ACPI_PATH_PUT8(full_path, path_size, '\0', length);
  223. #undef ACPI_PATH_PUT8
  224. return_UINT32(length);
  225. }
  226. /*******************************************************************************
  227. *
  228. * FUNCTION: acpi_ns_get_normalized_pathname
  229. *
  230. * PARAMETERS: node - Namespace node whose pathname is needed
  231. * no_trailing - Remove trailing '_' from each name segment
  232. *
  233. * RETURN: Pointer to storage containing the fully qualified name of
  234. * the node, In external format (name segments separated by path
  235. * separators.)
  236. *
  237. * DESCRIPTION: Used to obtain the full pathname to a namespace node, usually
  238. * for error and debug statements. All trailing '_' will be
  239. * removed from the full pathname if 'NoTrailing' is specified..
  240. *
  241. ******************************************************************************/
  242. char *acpi_ns_get_normalized_pathname(struct acpi_namespace_node *node,
  243. u8 no_trailing)
  244. {
  245. char *name_buffer;
  246. acpi_size size;
  247. ACPI_FUNCTION_TRACE_PTR(ns_get_normalized_pathname, node);
  248. /* Calculate required buffer size based on depth below root */
  249. size = acpi_ns_build_normalized_path(node, NULL, 0, no_trailing);
  250. if (!size) {
  251. return_PTR(NULL);
  252. }
  253. /* Allocate a buffer to be returned to caller */
  254. name_buffer = ACPI_ALLOCATE_ZEROED(size);
  255. if (!name_buffer) {
  256. ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
  257. return_PTR(NULL);
  258. }
  259. /* Build the path in the allocated buffer */
  260. (void)acpi_ns_build_normalized_path(node, name_buffer, size,
  261. no_trailing);
  262. ACPI_DEBUG_PRINT_RAW((ACPI_DB_NAMES, "%s: Path \"%s\"\n",
  263. ACPI_GET_FUNCTION_NAME, name_buffer));
  264. return_PTR(name_buffer);
  265. }
  266. /*******************************************************************************
  267. *
  268. * FUNCTION: acpi_ns_build_prefixed_pathname
  269. *
  270. * PARAMETERS: prefix_scope - Scope/Path that prefixes the internal path
  271. * internal_path - Name or path of the namespace node
  272. *
  273. * RETURN: None
  274. *
  275. * DESCRIPTION: Construct a fully qualified pathname from a concatenation of:
  276. * 1) Path associated with the prefix_scope namespace node
  277. * 2) External path representation of the Internal path
  278. *
  279. ******************************************************************************/
  280. char *acpi_ns_build_prefixed_pathname(union acpi_generic_state *prefix_scope,
  281. const char *internal_path)
  282. {
  283. acpi_status status;
  284. char *full_path = NULL;
  285. char *external_path = NULL;
  286. char *prefix_path = NULL;
  287. u32 prefix_path_length = 0;
  288. /* If there is a prefix, get the pathname to it */
  289. if (prefix_scope && prefix_scope->scope.node) {
  290. prefix_path =
  291. acpi_ns_get_normalized_pathname(prefix_scope->scope.node,
  292. TRUE);
  293. if (prefix_path) {
  294. prefix_path_length = strlen(prefix_path);
  295. }
  296. }
  297. status = acpi_ns_externalize_name(ACPI_UINT32_MAX, internal_path,
  298. NULL, &external_path);
  299. if (ACPI_FAILURE(status)) {
  300. goto cleanup;
  301. }
  302. /* Merge the prefix path and the path. 2 is for one dot and trailing null */
  303. full_path =
  304. ACPI_ALLOCATE_ZEROED(prefix_path_length + strlen(external_path) +
  305. 2);
  306. if (!full_path) {
  307. goto cleanup;
  308. }
  309. /* Don't merge if the External path is already fully qualified */
  310. if (prefix_path && (*external_path != '\\') && (*external_path != '^')) {
  311. strcat(full_path, prefix_path);
  312. if (prefix_path[1]) {
  313. strcat(full_path, ".");
  314. }
  315. }
  316. acpi_ns_normalize_pathname(external_path);
  317. strcat(full_path, external_path);
  318. cleanup:
  319. if (prefix_path) {
  320. ACPI_FREE(prefix_path);
  321. }
  322. if (external_path) {
  323. ACPI_FREE(external_path);
  324. }
  325. return (full_path);
  326. }
  327. /*******************************************************************************
  328. *
  329. * FUNCTION: acpi_ns_normalize_pathname
  330. *
  331. * PARAMETERS: original_path - Path to be normalized, in External format
  332. *
  333. * RETURN: The original path is processed in-place
  334. *
  335. * DESCRIPTION: Remove trailing underscores from each element of a path.
  336. *
  337. * For example: \A___.B___.C___ becomes \A.B.C
  338. *
  339. ******************************************************************************/
  340. static void acpi_ns_normalize_pathname(char *original_path)
  341. {
  342. char *input_path = original_path;
  343. char *new_path_buffer;
  344. char *new_path;
  345. u32 i;
  346. /* Allocate a temp buffer in which to construct the new path */
  347. new_path_buffer = ACPI_ALLOCATE_ZEROED(strlen(input_path) + 1);
  348. new_path = new_path_buffer;
  349. if (!new_path_buffer) {
  350. return;
  351. }
  352. /* Special characters may appear at the beginning of the path */
  353. if (*input_path == '\\') {
  354. *new_path = *input_path;
  355. new_path++;
  356. input_path++;
  357. }
  358. while (*input_path == '^') {
  359. *new_path = *input_path;
  360. new_path++;
  361. input_path++;
  362. }
  363. /* Remainder of the path */
  364. while (*input_path) {
  365. /* Do one nameseg at a time */
  366. for (i = 0; (i < ACPI_NAME_SIZE) && *input_path; i++) {
  367. if ((i == 0) || (*input_path != '_')) { /* First char is allowed to be underscore */
  368. *new_path = *input_path;
  369. new_path++;
  370. }
  371. input_path++;
  372. }
  373. /* Dot means that there are more namesegs to come */
  374. if (*input_path == '.') {
  375. *new_path = *input_path;
  376. new_path++;
  377. input_path++;
  378. }
  379. }
  380. *new_path = 0;
  381. strcpy(original_path, new_path_buffer);
  382. ACPI_FREE(new_path_buffer);
  383. }