nsnames.c 7.5 KB

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