1/*******************************************************************************
2 *
3 * Module Name: nsaccess - Top-level functions for accessing ACPI namespace
4 *
5 ******************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2014, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions, and the following disclaimer,
16 *    without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 *    substantially similar to the "NO WARRANTY" disclaimer below
19 *    ("Disclaimer") and any redistribution must be conditioned upon
20 *    including a substantially similar Disclaimer requirement for further
21 *    binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 *    of any contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44#include <acpi/acpi.h>
45#include "accommon.h"
46#include "amlcode.h"
47#include "acnamesp.h"
48#include "acdispat.h"
49
50#define _COMPONENT          ACPI_NAMESPACE
51ACPI_MODULE_NAME("nsaccess")
52
53/*******************************************************************************
54 *
55 * FUNCTION:    acpi_ns_root_initialize
56 *
57 * PARAMETERS:  None
58 *
59 * RETURN:      Status
60 *
61 * DESCRIPTION: Allocate and initialize the default root named objects
62 *
63 * MUTEX:       Locks namespace for entire execution
64 *
65 ******************************************************************************/
66acpi_status acpi_ns_root_initialize(void)
67{
68	acpi_status status;
69	const struct acpi_predefined_names *init_val = NULL;
70	struct acpi_namespace_node *new_node;
71	union acpi_operand_object *obj_desc;
72	acpi_string val = NULL;
73
74	ACPI_FUNCTION_TRACE(ns_root_initialize);
75
76	status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
77	if (ACPI_FAILURE(status)) {
78		return_ACPI_STATUS(status);
79	}
80
81	/*
82	 * The global root ptr is initially NULL, so a non-NULL value indicates
83	 * that acpi_ns_root_initialize() has already been called; just return.
84	 */
85	if (acpi_gbl_root_node) {
86		status = AE_OK;
87		goto unlock_and_exit;
88	}
89
90	/*
91	 * Tell the rest of the subsystem that the root is initialized
92	 * (This is OK because the namespace is locked)
93	 */
94	acpi_gbl_root_node = &acpi_gbl_root_node_struct;
95
96	/* Enter the pre-defined names in the name table */
97
98	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
99			  "Entering predefined entries into namespace\n"));
100
101	for (init_val = acpi_gbl_pre_defined_names; init_val->name; init_val++) {
102
103		/* _OSI is optional for now, will be permanent later */
104
105		if (!ACPI_STRCMP(init_val->name, "_OSI")
106		    && !acpi_gbl_create_osi_method) {
107			continue;
108		}
109
110		status = acpi_ns_lookup(NULL, init_val->name, init_val->type,
111					ACPI_IMODE_LOAD_PASS2,
112					ACPI_NS_NO_UPSEARCH, NULL, &new_node);
113		if (ACPI_FAILURE(status)) {
114			ACPI_EXCEPTION((AE_INFO, status,
115					"Could not create predefined name %s",
116					init_val->name));
117			continue;
118		}
119
120		/*
121		 * Name entered successfully. If entry in pre_defined_names[] specifies
122		 * an initial value, create the initial value.
123		 */
124		if (init_val->val) {
125			status = acpi_os_predefined_override(init_val, &val);
126			if (ACPI_FAILURE(status)) {
127				ACPI_ERROR((AE_INFO,
128					    "Could not override predefined %s",
129					    init_val->name));
130			}
131
132			if (!val) {
133				val = init_val->val;
134			}
135
136			/*
137			 * Entry requests an initial value, allocate a
138			 * descriptor for it.
139			 */
140			obj_desc =
141			    acpi_ut_create_internal_object(init_val->type);
142			if (!obj_desc) {
143				status = AE_NO_MEMORY;
144				goto unlock_and_exit;
145			}
146
147			/*
148			 * Convert value string from table entry to
149			 * internal representation. Only types actually
150			 * used for initial values are implemented here.
151			 */
152			switch (init_val->type) {
153			case ACPI_TYPE_METHOD:
154
155				obj_desc->method.param_count =
156				    (u8) ACPI_TO_INTEGER(val);
157				obj_desc->common.flags |= AOPOBJ_DATA_VALID;
158
159#if defined (ACPI_ASL_COMPILER)
160
161				/* Save the parameter count for the iASL compiler */
162
163				new_node->value = obj_desc->method.param_count;
164#else
165				/* Mark this as a very SPECIAL method */
166
167				obj_desc->method.info_flags =
168				    ACPI_METHOD_INTERNAL_ONLY;
169				obj_desc->method.dispatch.implementation =
170				    acpi_ut_osi_implementation;
171#endif
172				break;
173
174			case ACPI_TYPE_INTEGER:
175
176				obj_desc->integer.value = ACPI_TO_INTEGER(val);
177				break;
178
179			case ACPI_TYPE_STRING:
180
181				/* Build an object around the static string */
182
183				obj_desc->string.length = (u32)ACPI_STRLEN(val);
184				obj_desc->string.pointer = val;
185				obj_desc->common.flags |= AOPOBJ_STATIC_POINTER;
186				break;
187
188			case ACPI_TYPE_MUTEX:
189
190				obj_desc->mutex.node = new_node;
191				obj_desc->mutex.sync_level =
192				    (u8) (ACPI_TO_INTEGER(val) - 1);
193
194				/* Create a mutex */
195
196				status =
197				    acpi_os_create_mutex(&obj_desc->mutex.
198							 os_mutex);
199				if (ACPI_FAILURE(status)) {
200					acpi_ut_remove_reference(obj_desc);
201					goto unlock_and_exit;
202				}
203
204				/* Special case for ACPI Global Lock */
205
206				if (ACPI_STRCMP(init_val->name, "_GL_") == 0) {
207					acpi_gbl_global_lock_mutex = obj_desc;
208
209					/* Create additional counting semaphore for global lock */
210
211					status =
212					    acpi_os_create_semaphore(1, 0,
213								     &acpi_gbl_global_lock_semaphore);
214					if (ACPI_FAILURE(status)) {
215						acpi_ut_remove_reference
216						    (obj_desc);
217						goto unlock_and_exit;
218					}
219				}
220				break;
221
222			default:
223
224				ACPI_ERROR((AE_INFO,
225					    "Unsupported initial type value 0x%X",
226					    init_val->type));
227				acpi_ut_remove_reference(obj_desc);
228				obj_desc = NULL;
229				continue;
230			}
231
232			/* Store pointer to value descriptor in the Node */
233
234			status = acpi_ns_attach_object(new_node, obj_desc,
235						       obj_desc->common.type);
236
237			/* Remove local reference to the object */
238
239			acpi_ut_remove_reference(obj_desc);
240		}
241	}
242
243unlock_and_exit:
244	(void)acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
245
246	/* Save a handle to "_GPE", it is always present */
247
248	if (ACPI_SUCCESS(status)) {
249		status = acpi_ns_get_node(NULL, "\\_GPE", ACPI_NS_NO_UPSEARCH,
250					  &acpi_gbl_fadt_gpe_device);
251	}
252
253	return_ACPI_STATUS(status);
254}
255
256/*******************************************************************************
257 *
258 * FUNCTION:    acpi_ns_lookup
259 *
260 * PARAMETERS:  scope_info      - Current scope info block
261 *              pathname        - Search pathname, in internal format
262 *                                (as represented in the AML stream)
263 *              type            - Type associated with name
264 *              interpreter_mode - IMODE_LOAD_PASS2 => add name if not found
265 *              flags           - Flags describing the search restrictions
266 *              walk_state      - Current state of the walk
267 *              return_node     - Where the Node is placed (if found
268 *                                or created successfully)
269 *
270 * RETURN:      Status
271 *
272 * DESCRIPTION: Find or enter the passed name in the name space.
273 *              Log an error if name not found in Exec mode.
274 *
275 * MUTEX:       Assumes namespace is locked.
276 *
277 ******************************************************************************/
278
279acpi_status
280acpi_ns_lookup(union acpi_generic_state *scope_info,
281	       char *pathname,
282	       acpi_object_type type,
283	       acpi_interpreter_mode interpreter_mode,
284	       u32 flags,
285	       struct acpi_walk_state *walk_state,
286	       struct acpi_namespace_node **return_node)
287{
288	acpi_status status;
289	char *path = pathname;
290	struct acpi_namespace_node *prefix_node;
291	struct acpi_namespace_node *current_node = NULL;
292	struct acpi_namespace_node *this_node = NULL;
293	u32 num_segments;
294	u32 num_carats;
295	acpi_name simple_name;
296	acpi_object_type type_to_check_for;
297	acpi_object_type this_search_type;
298	u32 search_parent_flag = ACPI_NS_SEARCH_PARENT;
299	u32 local_flags;
300
301	ACPI_FUNCTION_TRACE(ns_lookup);
302
303	if (!return_node) {
304		return_ACPI_STATUS(AE_BAD_PARAMETER);
305	}
306
307	local_flags = flags & ~(ACPI_NS_ERROR_IF_FOUND | ACPI_NS_SEARCH_PARENT);
308	*return_node = ACPI_ENTRY_NOT_FOUND;
309	acpi_gbl_ns_lookup_count++;
310
311	if (!acpi_gbl_root_node) {
312		return_ACPI_STATUS(AE_NO_NAMESPACE);
313	}
314
315	/* Get the prefix scope. A null scope means use the root scope */
316
317	if ((!scope_info) || (!scope_info->scope.node)) {
318		ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
319				  "Null scope prefix, using root node (%p)\n",
320				  acpi_gbl_root_node));
321
322		prefix_node = acpi_gbl_root_node;
323	} else {
324		prefix_node = scope_info->scope.node;
325		if (ACPI_GET_DESCRIPTOR_TYPE(prefix_node) !=
326		    ACPI_DESC_TYPE_NAMED) {
327			ACPI_ERROR((AE_INFO, "%p is not a namespace node [%s]",
328				    prefix_node,
329				    acpi_ut_get_descriptor_name(prefix_node)));
330			return_ACPI_STATUS(AE_AML_INTERNAL);
331		}
332
333		if (!(flags & ACPI_NS_PREFIX_IS_SCOPE)) {
334			/*
335			 * This node might not be a actual "scope" node (such as a
336			 * Device/Method, etc.)  It could be a Package or other object
337			 * node. Backup up the tree to find the containing scope node.
338			 */
339			while (!acpi_ns_opens_scope(prefix_node->type) &&
340			       prefix_node->type != ACPI_TYPE_ANY) {
341				prefix_node = prefix_node->parent;
342			}
343		}
344	}
345
346	/* Save type. TBD: may be no longer necessary */
347
348	type_to_check_for = type;
349
350	/*
351	 * Begin examination of the actual pathname
352	 */
353	if (!pathname) {
354
355		/* A Null name_path is allowed and refers to the root */
356
357		num_segments = 0;
358		this_node = acpi_gbl_root_node;
359		path = "";
360
361		ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
362				  "Null Pathname (Zero segments), Flags=%X\n",
363				  flags));
364	} else {
365		/*
366		 * Name pointer is valid (and must be in internal name format)
367		 *
368		 * Check for scope prefixes:
369		 *
370		 * As represented in the AML stream, a namepath consists of an
371		 * optional scope prefix followed by a name segment part.
372		 *
373		 * If present, the scope prefix is either a Root Prefix (in
374		 * which case the name is fully qualified), or one or more
375		 * Parent Prefixes (in which case the name's scope is relative
376		 * to the current scope).
377		 */
378		if (*path == (u8) AML_ROOT_PREFIX) {
379
380			/* Pathname is fully qualified, start from the root */
381
382			this_node = acpi_gbl_root_node;
383			search_parent_flag = ACPI_NS_NO_UPSEARCH;
384
385			/* Point to name segment part */
386
387			path++;
388
389			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
390					  "Path is absolute from root [%p]\n",
391					  this_node));
392		} else {
393			/* Pathname is relative to current scope, start there */
394
395			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
396					  "Searching relative to prefix scope [%4.4s] (%p)\n",
397					  acpi_ut_get_node_name(prefix_node),
398					  prefix_node));
399
400			/*
401			 * Handle multiple Parent Prefixes (carat) by just getting
402			 * the parent node for each prefix instance.
403			 */
404			this_node = prefix_node;
405			num_carats = 0;
406			while (*path == (u8) AML_PARENT_PREFIX) {
407
408				/* Name is fully qualified, no search rules apply */
409
410				search_parent_flag = ACPI_NS_NO_UPSEARCH;
411
412				/*
413				 * Point past this prefix to the name segment
414				 * part or the next Parent Prefix
415				 */
416				path++;
417
418				/* Backup to the parent node */
419
420				num_carats++;
421				this_node = this_node->parent;
422				if (!this_node) {
423
424					/* Current scope has no parent scope */
425
426					ACPI_ERROR((AE_INFO,
427						    "%s: Path has too many parent prefixes (^) "
428						    "- reached beyond root node",
429						    pathname));
430					return_ACPI_STATUS(AE_NOT_FOUND);
431				}
432			}
433
434			if (search_parent_flag == ACPI_NS_NO_UPSEARCH) {
435				ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
436						  "Search scope is [%4.4s], path has %u carat(s)\n",
437						  acpi_ut_get_node_name
438						  (this_node), num_carats));
439			}
440		}
441
442		/*
443		 * Determine the number of ACPI name segments in this pathname.
444		 *
445		 * The segment part consists of either:
446		 *  - A Null name segment (0)
447		 *  - A dual_name_prefix followed by two 4-byte name segments
448		 *  - A multi_name_prefix followed by a byte indicating the
449		 *      number of segments and the segments themselves.
450		 *  - A single 4-byte name segment
451		 *
452		 * Examine the name prefix opcode, if any, to determine the number of
453		 * segments.
454		 */
455		switch (*path) {
456		case 0:
457			/*
458			 * Null name after a root or parent prefixes. We already
459			 * have the correct target node and there are no name segments.
460			 */
461			num_segments = 0;
462			type = this_node->type;
463
464			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
465					  "Prefix-only Pathname (Zero name segments), Flags=%X\n",
466					  flags));
467			break;
468
469		case AML_DUAL_NAME_PREFIX:
470
471			/* More than one name_seg, search rules do not apply */
472
473			search_parent_flag = ACPI_NS_NO_UPSEARCH;
474
475			/* Two segments, point to first name segment */
476
477			num_segments = 2;
478			path++;
479
480			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
481					  "Dual Pathname (2 segments, Flags=%X)\n",
482					  flags));
483			break;
484
485		case AML_MULTI_NAME_PREFIX_OP:
486
487			/* More than one name_seg, search rules do not apply */
488
489			search_parent_flag = ACPI_NS_NO_UPSEARCH;
490
491			/* Extract segment count, point to first name segment */
492
493			path++;
494			num_segments = (u32) (u8) * path;
495			path++;
496
497			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
498					  "Multi Pathname (%u Segments, Flags=%X)\n",
499					  num_segments, flags));
500			break;
501
502		default:
503			/*
504			 * Not a Null name, no Dual or Multi prefix, hence there is
505			 * only one name segment and Pathname is already pointing to it.
506			 */
507			num_segments = 1;
508
509			ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
510					  "Simple Pathname (1 segment, Flags=%X)\n",
511					  flags));
512			break;
513		}
514
515		ACPI_DEBUG_EXEC(acpi_ns_print_pathname(num_segments, path));
516	}
517
518	/*
519	 * Search namespace for each segment of the name. Loop through and
520	 * verify (or add to the namespace) each name segment.
521	 *
522	 * The object type is significant only at the last name
523	 * segment. (We don't care about the types along the path, only
524	 * the type of the final target object.)
525	 */
526	this_search_type = ACPI_TYPE_ANY;
527	current_node = this_node;
528	while (num_segments && current_node) {
529		num_segments--;
530		if (!num_segments) {
531
532			/* This is the last segment, enable typechecking */
533
534			this_search_type = type;
535
536			/*
537			 * Only allow automatic parent search (search rules) if the caller
538			 * requested it AND we have a single, non-fully-qualified name_seg
539			 */
540			if ((search_parent_flag != ACPI_NS_NO_UPSEARCH) &&
541			    (flags & ACPI_NS_SEARCH_PARENT)) {
542				local_flags |= ACPI_NS_SEARCH_PARENT;
543			}
544
545			/* Set error flag according to caller */
546
547			if (flags & ACPI_NS_ERROR_IF_FOUND) {
548				local_flags |= ACPI_NS_ERROR_IF_FOUND;
549			}
550		}
551
552		/* Extract one ACPI name from the front of the pathname */
553
554		ACPI_MOVE_32_TO_32(&simple_name, path);
555
556		/* Try to find the single (4 character) ACPI name */
557
558		status =
559		    acpi_ns_search_and_enter(simple_name, walk_state,
560					     current_node, interpreter_mode,
561					     this_search_type, local_flags,
562					     &this_node);
563		if (ACPI_FAILURE(status)) {
564			if (status == AE_NOT_FOUND) {
565
566				/* Name not found in ACPI namespace */
567
568				ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
569						  "Name [%4.4s] not found in scope [%4.4s] %p\n",
570						  (char *)&simple_name,
571						  (char *)&current_node->name,
572						  current_node));
573			}
574
575			*return_node = this_node;
576			return_ACPI_STATUS(status);
577		}
578
579		/* More segments to follow? */
580
581		if (num_segments > 0) {
582			/*
583			 * If we have an alias to an object that opens a scope (such as a
584			 * device or processor), we need to dereference the alias here so
585			 * that we can access any children of the original node (via the
586			 * remaining segments).
587			 */
588			if (this_node->type == ACPI_TYPE_LOCAL_ALIAS) {
589				if (!this_node->object) {
590					return_ACPI_STATUS(AE_NOT_EXIST);
591				}
592
593				if (acpi_ns_opens_scope
594				    (((struct acpi_namespace_node *)
595				      this_node->object)->type)) {
596					this_node =
597					    (struct acpi_namespace_node *)
598					    this_node->object;
599				}
600			}
601		}
602
603		/* Special handling for the last segment (num_segments == 0) */
604
605		else {
606			/*
607			 * Sanity typecheck of the target object:
608			 *
609			 * If 1) This is the last segment (num_segments == 0)
610			 *    2) And we are looking for a specific type
611			 *       (Not checking for TYPE_ANY)
612			 *    3) Which is not an alias
613			 *    4) Which is not a local type (TYPE_SCOPE)
614			 *    5) And the type of target object is known (not TYPE_ANY)
615			 *    6) And target object does not match what we are looking for
616			 *
617			 * Then we have a type mismatch. Just warn and ignore it.
618			 */
619			if ((type_to_check_for != ACPI_TYPE_ANY) &&
620			    (type_to_check_for != ACPI_TYPE_LOCAL_ALIAS) &&
621			    (type_to_check_for != ACPI_TYPE_LOCAL_METHOD_ALIAS)
622			    && (type_to_check_for != ACPI_TYPE_LOCAL_SCOPE)
623			    && (this_node->type != ACPI_TYPE_ANY)
624			    && (this_node->type != type_to_check_for)) {
625
626				/* Complain about a type mismatch */
627
628				ACPI_WARNING((AE_INFO,
629					      "NsLookup: Type mismatch on %4.4s (%s), searching for (%s)",
630					      ACPI_CAST_PTR(char, &simple_name),
631					      acpi_ut_get_type_name(this_node->
632								    type),
633					      acpi_ut_get_type_name
634					      (type_to_check_for)));
635			}
636
637			/*
638			 * If this is the last name segment and we are not looking for a
639			 * specific type, but the type of found object is known, use that
640			 * type to (later) see if it opens a scope.
641			 */
642			if (type == ACPI_TYPE_ANY) {
643				type = this_node->type;
644			}
645		}
646
647		/* Point to next name segment and make this node current */
648
649		path += ACPI_NAME_SIZE;
650		current_node = this_node;
651	}
652
653	/* Always check if we need to open a new scope */
654
655	if (!(flags & ACPI_NS_DONT_OPEN_SCOPE) && (walk_state)) {
656		/*
657		 * If entry is a type which opens a scope, push the new scope on the
658		 * scope stack.
659		 */
660		if (acpi_ns_opens_scope(type)) {
661			status =
662			    acpi_ds_scope_stack_push(this_node, type,
663						     walk_state);
664			if (ACPI_FAILURE(status)) {
665				return_ACPI_STATUS(status);
666			}
667		}
668	}
669
670	*return_node = this_node;
671	return_ACPI_STATUS(AE_OK);
672}
673