blob: 479aad0dbf417f273a707b222fee33c3b1afd547 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: nsutils - Utilities for accessing ACPI namespace, accessing
4 * parents and siblings and Scope manipulation
5 *
6 *****************************************************************************/
7
8/*
Bob Moore77848132012-01-12 13:27:23 +08009 * Copyright (C) 2000 - 2012, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
48#include "amlcode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("nsutils")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Robert Moore44f6c012005-04-18 22:49:35 -040053/* Local prototypes */
Len Brown4be44fc2005-08-05 00:44:28 -040054static u8 acpi_ns_valid_path_separator(char sep);
Robert Moore44f6c012005-04-18 22:49:35 -040055
56#ifdef ACPI_OBSOLETE_FUNCTIONS
Len Brown4be44fc2005-08-05 00:44:28 -040057acpi_name acpi_ns_find_parent_name(struct acpi_namespace_node *node_to_search);
Robert Moore44f6c012005-04-18 22:49:35 -040058#endif
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*******************************************************************************
61 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * FUNCTION: acpi_ns_print_node_pathname
63 *
Bob Mooreba494be2012-07-12 09:40:10 +080064 * PARAMETERS: node - Object
65 * message - Prefix message
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 * DESCRIPTION: Print an object's full namespace pathname
68 * Manages allocation/freeing of a pathname buffer
69 *
70 ******************************************************************************/
71
72void
Bob Moore4b8ed632008-06-10 13:55:53 +080073acpi_ns_print_node_pathname(struct acpi_namespace_node *node,
74 const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Len Brown4be44fc2005-08-05 00:44:28 -040076 struct acpi_buffer buffer;
77 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -040080 acpi_os_printf("[NULL NAME]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
82 }
83
84 /* Convert handle to full pathname and print it (with supplied message) */
85
86 buffer.length = ACPI_ALLOCATE_LOCAL_BUFFER;
87
Len Brown4be44fc2005-08-05 00:44:28 -040088 status = acpi_ns_handle_to_pathname(node, &buffer);
89 if (ACPI_SUCCESS(status)) {
Robert Moore44f6c012005-04-18 22:49:35 -040090 if (message) {
Len Brown4be44fc2005-08-05 00:44:28 -040091 acpi_os_printf("%s ", message);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Len Brown4be44fc2005-08-05 00:44:28 -040094 acpi_os_printf("[%s] (Node %p)", (char *)buffer.pointer, node);
Bob Moore83135242006-10-03 00:00:00 -040095 ACPI_FREE(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*******************************************************************************
100 *
101 * FUNCTION: acpi_ns_valid_root_prefix
102 *
Bob Mooreba494be2012-07-12 09:40:10 +0800103 * PARAMETERS: prefix - Character to be checked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *
105 * RETURN: TRUE if a valid prefix
106 *
107 * DESCRIPTION: Check if a character is a valid ACPI Root prefix
108 *
109 ******************************************************************************/
110
Len Brown4be44fc2005-08-05 00:44:28 -0400111u8 acpi_ns_valid_root_prefix(char prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113
114 return ((u8) (prefix == '\\'));
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/*******************************************************************************
118 *
119 * FUNCTION: acpi_ns_valid_path_separator
120 *
Bob Mooreba494be2012-07-12 09:40:10 +0800121 * PARAMETERS: sep - Character to be checked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
123 * RETURN: TRUE if a valid path separator
124 *
125 * DESCRIPTION: Check if a character is a valid ACPI path separator
126 *
127 ******************************************************************************/
128
Len Brown4be44fc2005-08-05 00:44:28 -0400129static u8 acpi_ns_valid_path_separator(char sep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131
132 return ((u8) (sep == '.'));
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*******************************************************************************
136 *
137 * FUNCTION: acpi_ns_get_type
138 *
Bob Mooreba494be2012-07-12 09:40:10 +0800139 * PARAMETERS: node - Parent Node to be examined
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * RETURN: Type field from Node whose handle is passed
142 *
Robert Moore44f6c012005-04-18 22:49:35 -0400143 * DESCRIPTION: Return the type of a Namespace node
144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ******************************************************************************/
146
Len Brown4be44fc2005-08-05 00:44:28 -0400147acpi_object_type acpi_ns_get_type(struct acpi_namespace_node * node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Bob Mooreb229cf92006-04-21 17:15:00 -0400149 ACPI_FUNCTION_TRACE(ns_get_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (!node) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500152 ACPI_WARNING((AE_INFO, "Null Node parameter"));
Bob Moore50eca3e2005-09-30 19:03:00 -0400153 return_UINT32(ACPI_TYPE_ANY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Bob Moore50eca3e2005-09-30 19:03:00 -0400156 return_UINT32((acpi_object_type) node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*******************************************************************************
160 *
161 * FUNCTION: acpi_ns_local
162 *
Bob Mooreba494be2012-07-12 09:40:10 +0800163 * PARAMETERS: type - A namespace object type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * RETURN: LOCAL if names must be found locally in objects of the
166 * passed type, 0 if enclosing scopes should be searched
167 *
Robert Moore44f6c012005-04-18 22:49:35 -0400168 * DESCRIPTION: Returns scope rule for the given object type.
169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ******************************************************************************/
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172u32 acpi_ns_local(acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Bob Mooreb229cf92006-04-21 17:15:00 -0400174 ACPI_FUNCTION_TRACE(ns_local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Len Brown4be44fc2005-08-05 00:44:28 -0400176 if (!acpi_ut_valid_object_type(type)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Type code out of range */
179
Bob Mooref6a22b02010-03-05 17:56:40 +0800180 ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
Bob Moore50eca3e2005-09-30 19:03:00 -0400181 return_UINT32(ACPI_NS_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
Bob Moore50eca3e2005-09-30 19:03:00 -0400184 return_UINT32((u32) acpi_gbl_ns_properties[type] & ACPI_NS_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/*******************************************************************************
188 *
189 * FUNCTION: acpi_ns_get_internal_name_length
190 *
Bob Mooreba494be2012-07-12 09:40:10 +0800191 * PARAMETERS: info - Info struct initialized with the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * external name pointer.
193 *
Robert Moore44f6c012005-04-18 22:49:35 -0400194 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * DESCRIPTION: Calculate the length of the internal (AML) namestring
197 * corresponding to the external (ASL) namestring.
198 *
199 ******************************************************************************/
200
Len Brown4be44fc2005-08-05 00:44:28 -0400201void acpi_ns_get_internal_name_length(struct acpi_namestring_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Bob Moore4b8ed632008-06-10 13:55:53 +0800203 const char *next_external_char;
Len Brown4be44fc2005-08-05 00:44:28 -0400204 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Len Brown4be44fc2005-08-05 00:44:28 -0400206 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 next_external_char = info->external_name;
209 info->num_carats = 0;
210 info->num_segments = 0;
211 info->fully_qualified = FALSE;
212
213 /*
214 * For the internal name, the required length is 4 bytes per segment, plus
215 * 1 each for root_prefix, multi_name_prefix_op, segment count, trailing null
216 * (which is not really needed, but no there's harm in putting it there)
217 *
218 * strlen() + 1 covers the first name_seg, which has no path separator
219 */
Lin Mingd037c5f2008-11-13 10:54:39 +0800220 if (acpi_ns_valid_root_prefix(*next_external_char)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 info->fully_qualified = TRUE;
222 next_external_char++;
Lin Mingd037c5f2008-11-13 10:54:39 +0800223
224 /* Skip redundant root_prefix, like \\_SB.PCI0.SBRG.EC0 */
225
226 while (acpi_ns_valid_root_prefix(*next_external_char)) {
227 next_external_char++;
228 }
Len Brown4be44fc2005-08-05 00:44:28 -0400229 } else {
Bob Moored4913dc2009-03-06 10:05:18 +0800230 /* Handle Carat prefixes */
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 while (*next_external_char == '^') {
233 info->num_carats++;
234 next_external_char++;
235 }
236 }
237
238 /*
239 * Determine the number of ACPI name "segments" by counting the number of
240 * path separators within the string. Start with one segment since the
241 * segment count is [(# separators) + 1], and zero separators is ok.
242 */
243 if (*next_external_char) {
244 info->num_segments = 1;
245 for (i = 0; next_external_char[i]; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400246 if (acpi_ns_valid_path_separator(next_external_char[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 info->num_segments++;
248 }
249 }
250 }
251
252 info->length = (ACPI_NAME_SIZE * info->num_segments) +
Len Brown4be44fc2005-08-05 00:44:28 -0400253 4 + info->num_carats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 info->next_external_char = next_external_char;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*******************************************************************************
259 *
260 * FUNCTION: acpi_ns_build_internal_name
261 *
Bob Mooreba494be2012-07-12 09:40:10 +0800262 * PARAMETERS: info - Info struct fully initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
264 * RETURN: Status
265 *
266 * DESCRIPTION: Construct the internal (AML) namestring
267 * corresponding to the external (ASL) namestring.
268 *
269 ******************************************************************************/
270
Len Brown4be44fc2005-08-05 00:44:28 -0400271acpi_status acpi_ns_build_internal_name(struct acpi_namestring_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Len Brown4be44fc2005-08-05 00:44:28 -0400273 u32 num_segments = info->num_segments;
274 char *internal_name = info->internal_name;
Bob Moore4b8ed632008-06-10 13:55:53 +0800275 const char *external_name = info->next_external_char;
Len Brown4be44fc2005-08-05 00:44:28 -0400276 char *result = NULL;
Bob Moore67a119f2008-06-10 13:42:13 +0800277 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Bob Mooreb229cf92006-04-21 17:15:00 -0400279 ACPI_FUNCTION_TRACE(ns_build_internal_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Setup the correct prefixes, counts, and pointers */
282
283 if (info->fully_qualified) {
284 internal_name[0] = '\\';
285
286 if (num_segments <= 1) {
287 result = &internal_name[1];
Len Brown4be44fc2005-08-05 00:44:28 -0400288 } else if (num_segments == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 internal_name[1] = AML_DUAL_NAME_PREFIX;
290 result = &internal_name[2];
Len Brown4be44fc2005-08-05 00:44:28 -0400291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 internal_name[1] = AML_MULTI_NAME_PREFIX_OP;
Len Brown4be44fc2005-08-05 00:44:28 -0400293 internal_name[2] = (char)num_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 result = &internal_name[3];
295 }
Len Brown4be44fc2005-08-05 00:44:28 -0400296 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /*
298 * Not fully qualified.
299 * Handle Carats first, then append the name segments
300 */
301 i = 0;
302 if (info->num_carats) {
303 for (i = 0; i < info->num_carats; i++) {
304 internal_name[i] = '^';
305 }
306 }
307
308 if (num_segments <= 1) {
309 result = &internal_name[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400310 } else if (num_segments == 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 internal_name[i] = AML_DUAL_NAME_PREFIX;
Bob Moore67a119f2008-06-10 13:42:13 +0800312 result = &internal_name[(acpi_size) i + 1];
Len Brown4be44fc2005-08-05 00:44:28 -0400313 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 internal_name[i] = AML_MULTI_NAME_PREFIX_OP;
Bob Moore67a119f2008-06-10 13:42:13 +0800315 internal_name[(acpi_size) i + 1] = (char)num_segments;
316 result = &internal_name[(acpi_size) i + 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318 }
319
320 /* Build the name (minus path separators) */
321
322 for (; num_segments; num_segments--) {
323 for (i = 0; i < ACPI_NAME_SIZE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400324 if (acpi_ns_valid_path_separator(*external_name) ||
325 (*external_name == 0)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Pad the segment with underscore(s) if segment is short */
328
329 result[i] = '_';
Len Brown4be44fc2005-08-05 00:44:28 -0400330 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 /* Convert the character to uppercase and save it */
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333 result[i] =
334 (char)ACPI_TOUPPER((int)*external_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 external_name++;
336 }
337 }
338
339 /* Now we must have a path separator, or the pathname is bad */
340
Len Brown4be44fc2005-08-05 00:44:28 -0400341 if (!acpi_ns_valid_path_separator(*external_name) &&
342 (*external_name != 0)) {
Bob Moorea1acd222012-03-21 09:44:00 +0800343 return_ACPI_STATUS(AE_BAD_PATHNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 /* Move on the next segment */
347
348 external_name++;
349 result += ACPI_NAME_SIZE;
350 }
351
352 /* Terminate the string */
353
354 *result = 0;
355
356 if (info->fully_qualified) {
Len Brown4be44fc2005-08-05 00:44:28 -0400357 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
358 "Returning [%p] (abs) \"\\%s\"\n",
359 internal_name, internal_name));
360 } else {
361 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Returning [%p] (rel) \"%s\"\n",
362 internal_name, internal_name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Len Brown4be44fc2005-08-05 00:44:28 -0400365 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*******************************************************************************
369 *
370 * FUNCTION: acpi_ns_internalize_name
371 *
372 * PARAMETERS: *external_name - External representation of name
Bob Mooreba494be2012-07-12 09:40:10 +0800373 * **Converted name - Where to return the resulting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * internal represention of the name
375 *
376 * RETURN: Status
377 *
378 * DESCRIPTION: Convert an external representation (e.g. "\_PR_.CPU0")
379 * to internal form (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
380 *
381 *******************************************************************************/
382
Bob Moore4b8ed632008-06-10 13:55:53 +0800383acpi_status
384acpi_ns_internalize_name(const char *external_name, char **converted_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Len Brown4be44fc2005-08-05 00:44:28 -0400386 char *internal_name;
387 struct acpi_namestring_info info;
388 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bob Mooreb229cf92006-04-21 17:15:00 -0400390 ACPI_FUNCTION_TRACE(ns_internalize_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 if ((!external_name) || (*external_name == 0) || (!converted_name)) {
393 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 /* Get the length of the new internal name */
397
398 info.external_name = external_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400399 acpi_ns_get_internal_name_length(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* We need a segment to store the internal name */
402
Bob Moore83135242006-10-03 00:00:00 -0400403 internal_name = ACPI_ALLOCATE_ZEROED(info.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!internal_name) {
Len Brown4be44fc2005-08-05 00:44:28 -0400405 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 /* Build the name */
409
410 info.internal_name = internal_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400411 status = acpi_ns_build_internal_name(&info);
412 if (ACPI_FAILURE(status)) {
Bob Moore83135242006-10-03 00:00:00 -0400413 ACPI_FREE(internal_name);
Len Brown4be44fc2005-08-05 00:44:28 -0400414 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 *converted_name = internal_name;
Len Brown4be44fc2005-08-05 00:44:28 -0400418 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*******************************************************************************
422 *
423 * FUNCTION: acpi_ns_externalize_name
424 *
Robert Moore44f6c012005-04-18 22:49:35 -0400425 * PARAMETERS: internal_name_length - Lenth of the internal name below
426 * internal_name - Internal representation of name
427 * converted_name_length - Where the length is returned
428 * converted_name - Where the resulting external name
429 * is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
431 * RETURN: Status
432 *
433 * DESCRIPTION: Convert internal name (e.g. 5c 2f 02 5f 50 52 5f 43 50 55 30)
Robert Moore44f6c012005-04-18 22:49:35 -0400434 * to its external (printable) form (e.g. "\_PR_.CPU0")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *
436 ******************************************************************************/
437
438acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400439acpi_ns_externalize_name(u32 internal_name_length,
Bob Moore4b8ed632008-06-10 13:55:53 +0800440 const char *internal_name,
Len Brown4be44fc2005-08-05 00:44:28 -0400441 u32 * converted_name_length, char **converted_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Bob Moore67a119f2008-06-10 13:42:13 +0800443 u32 names_index = 0;
444 u32 num_segments = 0;
445 u32 required_length;
446 u32 prefix_length = 0;
447 u32 i = 0;
448 u32 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Bob Mooreb229cf92006-04-21 17:15:00 -0400450 ACPI_FUNCTION_TRACE(ns_externalize_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Len Brown4be44fc2005-08-05 00:44:28 -0400452 if (!internal_name_length || !internal_name || !converted_name) {
453 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Bob Moored4913dc2009-03-06 10:05:18 +0800456 /* Check for a prefix (one '\' | one or more '^') */
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 switch (internal_name[0]) {
459 case '\\':
460 prefix_length = 1;
461 break;
462
463 case '^':
464 for (i = 0; i < internal_name_length; i++) {
465 if (internal_name[i] == '^') {
466 prefix_length = i + 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400467 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 }
470 }
471
472 if (i == internal_name_length) {
473 prefix_length = i;
474 }
475
476 break;
477
478 default:
479 break;
480 }
481
482 /*
Bob Moored4913dc2009-03-06 10:05:18 +0800483 * Check for object names. Note that there could be 0-255 of these
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * 4-byte elements.
485 */
486 if (prefix_length < internal_name_length) {
487 switch (internal_name[prefix_length]) {
488 case AML_MULTI_NAME_PREFIX_OP:
489
490 /* <count> 4-byte names */
491
492 names_index = prefix_length + 2;
Bob Moore67a119f2008-06-10 13:42:13 +0800493 num_segments = (u8)
494 internal_name[(acpi_size) prefix_length + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
496
497 case AML_DUAL_NAME_PREFIX:
498
499 /* Two 4-byte names */
500
501 names_index = prefix_length + 1;
502 num_segments = 2;
503 break;
504
505 case 0:
506
507 /* null_name */
508
509 names_index = 0;
510 num_segments = 0;
511 break;
512
513 default:
514
515 /* one 4-byte name */
516
517 names_index = prefix_length;
518 num_segments = 1;
519 break;
520 }
521 }
522
523 /*
524 * Calculate the length of converted_name, which equals the length
525 * of the prefix, length of all object names, length of any required
526 * punctuation ('.') between object names, plus the NULL terminator.
527 */
528 required_length = prefix_length + (4 * num_segments) +
Len Brown4be44fc2005-08-05 00:44:28 -0400529 ((num_segments > 0) ? (num_segments - 1) : 0) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /*
Bob Moore73a30902012-10-31 02:26:55 +0000532 * Check to see if we're still in bounds. If not, there's a problem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * with internal_name (invalid format).
534 */
535 if (required_length > internal_name_length) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500536 ACPI_ERROR((AE_INFO, "Invalid internal name"));
Len Brown4be44fc2005-08-05 00:44:28 -0400537 return_ACPI_STATUS(AE_BAD_PATHNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
Bob Moored4913dc2009-03-06 10:05:18 +0800540 /* Build the converted_name */
541
Bob Moore83135242006-10-03 00:00:00 -0400542 *converted_name = ACPI_ALLOCATE_ZEROED(required_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (!(*converted_name)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400544 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 j = 0;
548
549 for (i = 0; i < prefix_length; i++) {
550 (*converted_name)[j++] = internal_name[i];
551 }
552
553 if (num_segments > 0) {
554 for (i = 0; i < num_segments; i++) {
555 if (i > 0) {
556 (*converted_name)[j++] = '.';
557 }
558
Bob Moore47abd132012-10-31 02:28:19 +0000559 /* Copy and validate the 4-char name segment */
560
561 ACPI_MOVE_NAME(&(*converted_name)[j],
562 &internal_name[names_index]);
563 acpi_ut_repair_name(&(*converted_name)[j]);
Bob Moore00eb3252012-10-31 02:27:48 +0000564
565 j += ACPI_NAME_SIZE;
566 names_index += ACPI_NAME_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
569
570 if (converted_name_length) {
571 *converted_name_length = (u32) required_length;
572 }
573
Len Brown4be44fc2005-08-05 00:44:28 -0400574 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*******************************************************************************
578 *
Bob Mooref24b6642009-12-11 14:57:00 +0800579 * FUNCTION: acpi_ns_validate_handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Bob Mooreba494be2012-07-12 09:40:10 +0800581 * PARAMETERS: handle - Handle to be validated and typecast to a
Bob Mooref24b6642009-12-11 14:57:00 +0800582 * namespace node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
Bob Mooref24b6642009-12-11 14:57:00 +0800584 * RETURN: A pointer to a namespace node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
Bob Mooref24b6642009-12-11 14:57:00 +0800586 * DESCRIPTION: Convert a namespace handle to a namespace node. Handles special
587 * cases for the root node.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
Bob Mooref24b6642009-12-11 14:57:00 +0800589 * NOTE: Real integer handles would allow for more verification
Robert Moore44f6c012005-04-18 22:49:35 -0400590 * and keep all pointers within this subsystem - however this introduces
Bob Mooref24b6642009-12-11 14:57:00 +0800591 * more overhead and has not been necessary to this point. Drivers
592 * holding handles are typically notified before a node becomes invalid
593 * due to a table unload.
Bob Moored4913dc2009-03-06 10:05:18 +0800594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ******************************************************************************/
596
Bob Mooref24b6642009-12-11 14:57:00 +0800597struct acpi_namespace_node *acpi_ns_validate_handle(acpi_handle handle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599
Len Brown4be44fc2005-08-05 00:44:28 -0400600 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Bob Moored4913dc2009-03-06 10:05:18 +0800602 /* Parameter validation */
603
Bob Moore41195322006-05-26 16:36:00 -0400604 if ((!handle) || (handle == ACPI_ROOT_OBJECT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return (acpi_gbl_root_node);
606 }
607
608 /* We can at least attempt to verify the handle */
609
Len Brown4be44fc2005-08-05 00:44:28 -0400610 if (ACPI_GET_DESCRIPTOR_TYPE(handle) != ACPI_DESC_TYPE_NAMED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return (NULL);
612 }
613
Bob Moore41195322006-05-26 16:36:00 -0400614 return (ACPI_CAST_PTR(struct acpi_namespace_node, handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/*******************************************************************************
618 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * FUNCTION: acpi_ns_terminate
620 *
621 * PARAMETERS: none
622 *
623 * RETURN: none
624 *
Robert Moore44f6c012005-04-18 22:49:35 -0400625 * DESCRIPTION: free memory allocated for namespace and ACPI table storage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 *
627 ******************************************************************************/
628
Len Brown4be44fc2005-08-05 00:44:28 -0400629void acpi_ns_terminate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Len Brown4be44fc2005-08-05 00:44:28 -0400631 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Bob Mooreb229cf92006-04-21 17:15:00 -0400633 ACPI_FUNCTION_TRACE(ns_terminate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /*
636 * 1) Free the entire namespace -- all nodes and objects
637 *
638 * Delete all object descriptors attached to namepsace nodes
639 */
Len Brown4be44fc2005-08-05 00:44:28 -0400640 acpi_ns_delete_namespace_subtree(acpi_gbl_root_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 /* Detach any objects attached to the root */
643
Len Brown4be44fc2005-08-05 00:44:28 -0400644 obj_desc = acpi_ns_get_attached_object(acpi_gbl_root_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400646 acpi_ns_detach_object(acpi_gbl_root_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Len Brown4be44fc2005-08-05 00:44:28 -0400649 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Namespace freed\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return_VOID;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*******************************************************************************
654 *
655 * FUNCTION: acpi_ns_opens_scope
656 *
Bob Mooreba494be2012-07-12 09:40:10 +0800657 * PARAMETERS: type - A valid namespace type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
659 * RETURN: NEWSCOPE if the passed type "opens a name scope" according
660 * to the ACPI specification, else 0
661 *
662 ******************************************************************************/
663
Len Brown4be44fc2005-08-05 00:44:28 -0400664u32 acpi_ns_opens_scope(acpi_object_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Bob Mooreb229cf92006-04-21 17:15:00 -0400666 ACPI_FUNCTION_TRACE_STR(ns_opens_scope, acpi_ut_get_type_name(type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Len Brown4be44fc2005-08-05 00:44:28 -0400668 if (!acpi_ut_valid_object_type(type)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /* type code out of range */
671
Bob Mooref6a22b02010-03-05 17:56:40 +0800672 ACPI_WARNING((AE_INFO, "Invalid Object Type 0x%X", type));
Bob Moore50eca3e2005-09-30 19:03:00 -0400673 return_UINT32(ACPI_NS_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675
Bob Moore50eca3e2005-09-30 19:03:00 -0400676 return_UINT32(((u32) acpi_gbl_ns_properties[type]) & ACPI_NS_NEWSCOPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/*******************************************************************************
680 *
Bob Moore41195322006-05-26 16:36:00 -0400681 * FUNCTION: acpi_ns_get_node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
Bob Mooreba494be2012-07-12 09:40:10 +0800683 * PARAMETERS: *pathname - Name to be found, in external (ASL) format. The
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * \ (backslash) and ^ (carat) prefixes, and the
685 * . (period) to separate segments are supported.
Bob Moore41195322006-05-26 16:36:00 -0400686 * prefix_node - Root of subtree to be searched, or NS_ALL for the
Bob Moore73a30902012-10-31 02:26:55 +0000687 * root of the name space. If Name is fully
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * qualified (first s8 is '\'), the passed value
689 * of Scope will not be accessed.
Bob Mooreba494be2012-07-12 09:40:10 +0800690 * flags - Used to indicate whether to perform upsearch or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 * not.
692 * return_node - Where the Node is returned
693 *
694 * DESCRIPTION: Look up a name relative to a given scope and return the
Bob Moore73a30902012-10-31 02:26:55 +0000695 * corresponding Node. NOTE: Scope can be null.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *
697 * MUTEX: Locks namespace
698 *
699 ******************************************************************************/
700
701acpi_status
Bob Moore41195322006-05-26 16:36:00 -0400702acpi_ns_get_node(struct acpi_namespace_node *prefix_node,
Bob Moore4b8ed632008-06-10 13:55:53 +0800703 const char *pathname,
Bob Moore41195322006-05-26 16:36:00 -0400704 u32 flags, struct acpi_namespace_node **return_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Len Brown4be44fc2005-08-05 00:44:28 -0400706 union acpi_generic_state scope_info;
707 acpi_status status;
Bob Moore41195322006-05-26 16:36:00 -0400708 char *internal_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Bob Moore5853a9f2009-06-02 13:20:00 +0800710 ACPI_FUNCTION_TRACE_PTR(ns_get_node, ACPI_CAST_PTR(char, pathname));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Bob Moore41195322006-05-26 16:36:00 -0400712 if (!pathname) {
713 *return_node = prefix_node;
714 if (!prefix_node) {
715 *return_node = acpi_gbl_root_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Bob Moore41195322006-05-26 16:36:00 -0400717 return_ACPI_STATUS(AE_OK);
718 }
719
720 /* Convert path to internal representation */
721
722 status = acpi_ns_internalize_name(pathname, &internal_path);
723 if (ACPI_FAILURE(status)) {
724 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 /* Must lock namespace during lookup */
728
Len Brown4be44fc2005-08-05 00:44:28 -0400729 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
730 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto cleanup;
732 }
733
734 /* Setup lookup scope (search starting point) */
735
Bob Moore41195322006-05-26 16:36:00 -0400736 scope_info.scope.node = prefix_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* Lookup the name in the namespace */
739
Bob Moore41195322006-05-26 16:36:00 -0400740 status = acpi_ns_lookup(&scope_info, internal_path, ACPI_TYPE_ANY,
741 ACPI_IMODE_EXECUTE,
742 (flags | ACPI_NS_DONT_OPEN_SCOPE), NULL,
743 return_node);
Len Brown4be44fc2005-08-05 00:44:28 -0400744 if (ACPI_FAILURE(status)) {
Bob Moore7bcc06e2009-02-18 14:58:08 +0800745 ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "%s, %s\n",
Bob Moore41195322006-05-26 16:36:00 -0400746 pathname, acpi_format_exception(status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Len Brown4be44fc2005-08-05 00:44:28 -0400749 (void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Len Brown4be44fc2005-08-05 00:44:28 -0400751 cleanup:
Bob Moore41195322006-05-26 16:36:00 -0400752 ACPI_FREE(internal_path);
Len Brown4be44fc2005-08-05 00:44:28 -0400753 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}