nsnames.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*******************************************************************************
  2. *
  3. * Module Name: nsnames - Name manipulation and search
  4. *
  5. ******************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2011, 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_build_external_path
  51. *
  52. * PARAMETERS: Node - NS node whose pathname is needed
  53. * Size - Size of the pathname
  54. * *name_buffer - Where to return the pathname
  55. *
  56. * RETURN: Status
  57. * Places the pathname into the name_buffer, in external format
  58. * (name segments separated by path separators)
  59. *
  60. * DESCRIPTION: Generate a full pathaname
  61. *
  62. ******************************************************************************/
  63. acpi_status
  64. acpi_ns_build_external_path(struct acpi_namespace_node *node,
  65. acpi_size size, char *name_buffer)
  66. {
  67. acpi_size index;
  68. struct acpi_namespace_node *parent_node;
  69. ACPI_FUNCTION_ENTRY();
  70. /* Special case for root */
  71. index = size - 1;
  72. if (index < ACPI_NAME_SIZE) {
  73. name_buffer[0] = AML_ROOT_PREFIX;
  74. name_buffer[1] = 0;
  75. return (AE_OK);
  76. }
  77. /* Store terminator byte, then build name backwards */
  78. parent_node = node;
  79. name_buffer[index] = 0;
  80. while ((index > ACPI_NAME_SIZE) && (parent_node != acpi_gbl_root_node)) {
  81. index -= ACPI_NAME_SIZE;
  82. /* Put the name into the buffer */
  83. ACPI_MOVE_32_TO_32((name_buffer + index), &parent_node->name);
  84. parent_node = parent_node->parent;
  85. /* Prefix name with the path separator */
  86. index--;
  87. name_buffer[index] = ACPI_PATH_SEPARATOR;
  88. }
  89. /* Overwrite final separator with the root prefix character */
  90. name_buffer[index] = AML_ROOT_PREFIX;
  91. if (index != 0) {
  92. ACPI_ERROR((AE_INFO,
  93. "Could not construct external pathname; index=%u, size=%u, Path=%s",
  94. (u32) index, (u32) size, &name_buffer[size]));
  95. return (AE_BAD_PARAMETER);
  96. }
  97. return (AE_OK);
  98. }
  99. /*******************************************************************************
  100. *
  101. * FUNCTION: acpi_ns_get_external_pathname
  102. *
  103. * PARAMETERS: Node - Namespace node whose pathname is needed
  104. *
  105. * RETURN: Pointer to storage containing the fully qualified name of
  106. * the node, In external format (name segments separated by path
  107. * separators.)
  108. *
  109. * DESCRIPTION: Used for debug printing in acpi_ns_search_table().
  110. *
  111. ******************************************************************************/
  112. char *acpi_ns_get_external_pathname(struct acpi_namespace_node *node)
  113. {
  114. acpi_status status;
  115. char *name_buffer;
  116. acpi_size size;
  117. ACPI_FUNCTION_TRACE_PTR(ns_get_external_pathname, node);
  118. /* Calculate required buffer size based on depth below root */
  119. size = acpi_ns_get_pathname_length(node);
  120. if (!size) {
  121. return_PTR(NULL);
  122. }
  123. /* Allocate a buffer to be returned to caller */
  124. name_buffer = ACPI_ALLOCATE_ZEROED(size);
  125. if (!name_buffer) {
  126. ACPI_ERROR((AE_INFO, "Could not allocate %u bytes", (u32)size));
  127. return_PTR(NULL);
  128. }
  129. /* Build the path in the allocated buffer */
  130. status = acpi_ns_build_external_path(node, size, name_buffer);
  131. if (ACPI_FAILURE(status)) {
  132. ACPI_FREE(name_buffer);
  133. return_PTR(NULL);
  134. }
  135. return_PTR(name_buffer);
  136. }
  137. /*******************************************************************************
  138. *
  139. * FUNCTION: acpi_ns_get_pathname_length
  140. *
  141. * PARAMETERS: Node - Namespace node
  142. *
  143. * RETURN: Length of path, including prefix
  144. *
  145. * DESCRIPTION: Get the length of the pathname string for this node
  146. *
  147. ******************************************************************************/
  148. acpi_size acpi_ns_get_pathname_length(struct acpi_namespace_node *node)
  149. {
  150. acpi_size size;
  151. struct acpi_namespace_node *next_node;
  152. ACPI_FUNCTION_ENTRY();
  153. /*
  154. * Compute length of pathname as 5 * number of name segments.
  155. * Go back up the parent tree to the root
  156. */
  157. size = 0;
  158. next_node = node;
  159. while (next_node && (next_node != acpi_gbl_root_node)) {
  160. if (ACPI_GET_DESCRIPTOR_TYPE(next_node) != ACPI_DESC_TYPE_NAMED) {
  161. ACPI_ERROR((AE_INFO,
  162. "Invalid Namespace Node (%p) while traversing namespace",
  163. next_node));
  164. return 0;
  165. }
  166. size += ACPI_PATH_SEGMENT_LENGTH;
  167. next_node = next_node->parent;
  168. }
  169. if (!size) {
  170. size = 1; /* Root node case */
  171. }
  172. return (size + 1); /* +1 for null string terminator */
  173. }
  174. /*******************************************************************************
  175. *
  176. * FUNCTION: acpi_ns_handle_to_pathname
  177. *
  178. * PARAMETERS: target_handle - Handle of named object whose name is
  179. * to be found
  180. * Buffer - Where the pathname is returned
  181. *
  182. * RETURN: Status, Buffer is filled with pathname if status is AE_OK
  183. *
  184. * DESCRIPTION: Build and return a full namespace pathname
  185. *
  186. ******************************************************************************/
  187. acpi_status
  188. acpi_ns_handle_to_pathname(acpi_handle target_handle,
  189. struct acpi_buffer * buffer)
  190. {
  191. acpi_status status;
  192. struct acpi_namespace_node *node;
  193. acpi_size required_size;
  194. ACPI_FUNCTION_TRACE_PTR(ns_handle_to_pathname, target_handle);
  195. node = acpi_ns_validate_handle(target_handle);
  196. if (!node) {
  197. return_ACPI_STATUS(AE_BAD_PARAMETER);
  198. }
  199. /* Determine size required for the caller buffer */
  200. required_size = acpi_ns_get_pathname_length(node);
  201. if (!required_size) {
  202. return_ACPI_STATUS(AE_BAD_PARAMETER);
  203. }
  204. /* Validate/Allocate/Clear caller buffer */
  205. status = acpi_ut_initialize_buffer(buffer, required_size);
  206. if (ACPI_FAILURE(status)) {
  207. return_ACPI_STATUS(status);
  208. }
  209. /* Build the path in the caller buffer */
  210. status =
  211. acpi_ns_build_external_path(node, required_size, buffer->pointer);
  212. if (ACPI_FAILURE(status)) {
  213. return_ACPI_STATUS(status);
  214. }
  215. ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s [%X]\n",
  216. (char *)buffer->pointer, (u32) required_size));
  217. return_ACPI_STATUS(AE_OK);
  218. }