nspredef.c revision dbdc8f02fe8339686623c84745ba15b0f4f889a1
1/******************************************************************************
2 *
3 * Module Name: nspredef - Validation of ACPI predefined methods and objects
4 *              $Revision: 1.1 $
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2008, Intel Corp.
10 * 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
45#include <acpi/acpi.h>
46#include "accommon.h"
47#include "acnamesp.h"
48#include "acpredef.h"
49
50#define _COMPONENT          ACPI_NAMESPACE
51ACPI_MODULE_NAME("nspredef")
52
53/*******************************************************************************
54 *
55 * This module validates predefined ACPI objects that appear in the namespace,
56 * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
57 * validation is to detect problems with BIOS-exposed predefined ACPI objects
58 * before the results are returned to the ACPI-related drivers.
59 *
60 * There are several areas that are validated:
61 *
62 *  1) The number of input arguments as defined by the method/object in the
63 *      ASL is validated against the ACPI specification.
64 *  2) The type of the return object (if any) is validated against the ACPI
65 *      specification.
66 *  3) For returned package objects, the count of package elements is
67 *      validated, as well as the type of each package element. Nested
68 *      packages are supported.
69 *
70 * For any problems found, a warning message is issued.
71 *
72 ******************************************************************************/
73/* Local prototypes */
74static acpi_status
75acpi_ns_check_package(char *pathname,
76		      union acpi_operand_object **return_object_ptr,
77		      const union acpi_predefined_info *predefined);
78
79static acpi_status
80acpi_ns_check_package_elements(char *pathname,
81			       union acpi_operand_object **elements,
82			       u8 type1,
83			       u32 count1,
84			       u8 type2, u32 count2, u32 start_index);
85
86static acpi_status
87acpi_ns_check_object_type(char *pathname,
88			  union acpi_operand_object **return_object_ptr,
89			  u32 expected_btypes, u32 package_index);
90
91static acpi_status
92acpi_ns_check_reference(char *pathname,
93			union acpi_operand_object *return_object);
94
95static acpi_status
96acpi_ns_repair_object(u32 expected_btypes,
97		      u32 package_index,
98		      union acpi_operand_object **return_object_ptr);
99
100/*
101 * Names for the types that can be returned by the predefined objects.
102 * Used for warning messages. Must be in the same order as the ACPI_RTYPEs
103 */
104static const char *acpi_rtype_names[] = {
105	"/Integer",
106	"/String",
107	"/Buffer",
108	"/Package",
109	"/Reference",
110};
111
112#define ACPI_NOT_PACKAGE    ACPI_UINT32_MAX
113
114/*******************************************************************************
115 *
116 * FUNCTION:    acpi_ns_check_predefined_names
117 *
118 * PARAMETERS:  Node            - Namespace node for the method/object
119 *              return_object_ptr - Pointer to the object returned from the
120 *                                evaluation of a method or object
121 *
122 * RETURN:      Status
123 *
124 * DESCRIPTION: Check an ACPI name for a match in the predefined name list.
125 *
126 ******************************************************************************/
127
128acpi_status
129acpi_ns_check_predefined_names(struct acpi_namespace_node *node,
130			       u32 user_param_count,
131			       acpi_status return_status,
132			       union acpi_operand_object **return_object_ptr)
133{
134	union acpi_operand_object *return_object = *return_object_ptr;
135	acpi_status status = AE_OK;
136	const union acpi_predefined_info *predefined;
137	char *pathname;
138
139	/* Match the name for this method/object against the predefined list */
140
141	predefined = acpi_ns_check_for_predefined_name(node);
142
143	/* Get the full pathname to the object, for use in error messages */
144
145	pathname = acpi_ns_get_external_pathname(node);
146	if (!pathname) {
147		return AE_OK;	/* Could not get pathname, ignore */
148	}
149
150	/*
151	 * Check that the parameter count for this method matches the ASL
152	 * definition. For predefined names, ensure that both the caller and
153	 * the method itself are in accordance with the ACPI specification.
154	 */
155	acpi_ns_check_parameter_count(pathname, node, user_param_count,
156				      predefined);
157
158	/* If not a predefined name, we cannot validate the return object */
159
160	if (!predefined) {
161		goto exit;
162	}
163
164	/* If the method failed, we cannot validate the return object */
165
166	if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
167		goto exit;
168	}
169
170	/*
171	 * Only validate the return value on the first successful evaluation of
172	 * the method. This ensures that any warnings will only be emitted during
173	 * the very first evaluation of the method/object.
174	 */
175	if (node->flags & ANOBJ_EVALUATED) {
176		goto exit;
177	}
178
179	/* Mark the node as having been successfully evaluated */
180
181	node->flags |= ANOBJ_EVALUATED;
182
183	/*
184	 * If there is no return value, check if we require a return value for
185	 * this predefined name. Either one return value is expected, or none,
186	 * for both methods and other objects.
187	 *
188	 * Exit now if there is no return object. Warning if one was expected.
189	 */
190	if (!return_object) {
191		if ((predefined->info.expected_btypes) &&
192		    (!(predefined->info.expected_btypes & ACPI_RTYPE_NONE))) {
193			ACPI_ERROR((AE_INFO,
194				    "%s: Missing expected return value",
195				    pathname));
196
197			status = AE_AML_NO_RETURN_VALUE;
198		}
199		goto exit;
200	}
201
202	/*
203	 * We have a return value, but if one wasn't expected, just exit, this is
204	 * not a problem
205	 *
206	 * For example, if the "Implicit Return" feature is enabled, methods will
207	 * always return a value
208	 */
209	if (!predefined->info.expected_btypes) {
210		goto exit;
211	}
212
213	/*
214	 * Check that the type of the return object is what is expected for
215	 * this predefined name
216	 */
217	status = acpi_ns_check_object_type(pathname, return_object_ptr,
218					   predefined->info.expected_btypes,
219					   ACPI_NOT_PACKAGE);
220	if (ACPI_FAILURE(status)) {
221		goto exit;
222	}
223
224	/* For returned Package objects, check the type of all sub-objects */
225
226	if (return_object->common.type == ACPI_TYPE_PACKAGE) {
227		status =
228		    acpi_ns_check_package(pathname, return_object_ptr,
229					  predefined);
230	}
231
232      exit:
233	ACPI_FREE(pathname);
234	return (status);
235}
236
237/*******************************************************************************
238 *
239 * FUNCTION:    acpi_ns_check_parameter_count
240 *
241 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
242 *              Node            - Namespace node for the method/object
243 *              user_param_count - Number of args passed in by the caller
244 *              Predefined      - Pointer to entry in predefined name table
245 *
246 * RETURN:      None
247 *
248 * DESCRIPTION: Check that the declared (in ASL/AML) parameter count for a
249 *              predefined name is what is expected (i.e., what is defined in
250 *              the ACPI specification for this predefined name.)
251 *
252 ******************************************************************************/
253
254void
255acpi_ns_check_parameter_count(char *pathname,
256			      struct acpi_namespace_node *node,
257			      u32 user_param_count,
258			      const union acpi_predefined_info *predefined)
259{
260	u32 param_count;
261	u32 required_params_current;
262	u32 required_params_old;
263
264	/* Methods have 0-7 parameters. All other types have zero. */
265
266	param_count = 0;
267	if (node->type == ACPI_TYPE_METHOD) {
268		param_count = node->object->method.param_count;
269	}
270
271	/* Argument count check for non-predefined methods/objects */
272
273	if (!predefined) {
274		/*
275		 * Warning if too few or too many arguments have been passed by the
276		 * caller. An incorrect number of arguments may not cause the method
277		 * to fail. However, the method will fail if there are too few
278		 * arguments and the method attempts to use one of the missing ones.
279		 */
280		if (user_param_count < param_count) {
281			ACPI_WARNING((AE_INFO,
282				      "%s: Insufficient arguments - needs %d, found %d",
283				      pathname, param_count, user_param_count));
284		} else if (user_param_count > param_count) {
285			ACPI_WARNING((AE_INFO,
286				      "%s: Excess arguments - needs %d, found %d",
287				      pathname, param_count, user_param_count));
288		}
289		return;
290	}
291
292	/* Allow two different legal argument counts (_SCP, etc.) */
293
294	required_params_current = predefined->info.param_count & 0x0F;
295	required_params_old = predefined->info.param_count >> 4;
296
297	if (user_param_count != ACPI_UINT32_MAX) {
298
299		/* Validate the user-supplied parameter count */
300
301		if ((user_param_count != required_params_current) &&
302		    (user_param_count != required_params_old)) {
303			ACPI_WARNING((AE_INFO,
304				      "%s: Parameter count mismatch - "
305				      "caller passed %d, ACPI requires %d",
306				      pathname, user_param_count,
307				      required_params_current));
308		}
309	}
310
311	/*
312	 * Only validate the argument count on the first successful evaluation of
313	 * the method. This ensures that any warnings will only be emitted during
314	 * the very first evaluation of the method/object.
315	 */
316	if (node->flags & ANOBJ_EVALUATED) {
317		return;
318	}
319
320	/*
321	 * Check that the ASL-defined parameter count is what is expected for
322	 * this predefined name.
323	 */
324	if ((param_count != required_params_current) &&
325	    (param_count != required_params_old)) {
326		ACPI_WARNING((AE_INFO,
327			      "%s: Parameter count mismatch - ASL declared %d, ACPI requires %d",
328			      pathname, param_count, required_params_current));
329	}
330}
331
332/*******************************************************************************
333 *
334 * FUNCTION:    acpi_ns_check_for_predefined_name
335 *
336 * PARAMETERS:  Node            - Namespace node for the method/object
337 *
338 * RETURN:      Pointer to entry in predefined table. NULL indicates not found.
339 *
340 * DESCRIPTION: Check an object name against the predefined object list.
341 *
342 ******************************************************************************/
343
344const union acpi_predefined_info *acpi_ns_check_for_predefined_name(struct
345								    acpi_namespace_node
346								    *node)
347{
348	const union acpi_predefined_info *this_name;
349
350	/* Quick check for a predefined name, first character must be underscore */
351
352	if (node->name.ascii[0] != '_') {
353		return (NULL);
354	}
355
356	/* Search info table for a predefined method/object name */
357
358	this_name = predefined_names;
359	while (this_name->info.name[0]) {
360		if (ACPI_COMPARE_NAME(node->name.ascii, this_name->info.name)) {
361
362			/* Return pointer to this table entry */
363
364			return (this_name);
365		}
366
367		/*
368		 * Skip next entry in the table if this name returns a Package
369		 * (next entry contains the package info)
370		 */
371		if (this_name->info.expected_btypes & ACPI_RTYPE_PACKAGE) {
372			this_name++;
373		}
374
375		this_name++;
376	}
377
378	return (NULL);
379}
380
381/*******************************************************************************
382 *
383 * FUNCTION:    acpi_ns_check_package
384 *
385 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
386 *              return_object_ptr - Pointer to the object returned from the
387 *                                evaluation of a method or object
388 *              Predefined      - Pointer to entry in predefined name table
389 *
390 * RETURN:      Status
391 *
392 * DESCRIPTION: Check a returned package object for the correct count and
393 *              correct type of all sub-objects.
394 *
395 ******************************************************************************/
396
397static acpi_status
398acpi_ns_check_package(char *pathname,
399		      union acpi_operand_object **return_object_ptr,
400		      const union acpi_predefined_info *predefined)
401{
402	union acpi_operand_object *return_object = *return_object_ptr;
403	const union acpi_predefined_info *package;
404	union acpi_operand_object *sub_package;
405	union acpi_operand_object **elements;
406	union acpi_operand_object **sub_elements;
407	acpi_status status;
408	u32 expected_count;
409	u32 count;
410	u32 i;
411	u32 j;
412
413	ACPI_FUNCTION_NAME(ns_check_package);
414
415	/* The package info for this name is in the next table entry */
416
417	package = predefined + 1;
418
419	ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
420			  "%s Validating return Package of Type %X, Count %X\n",
421			  pathname, package->ret_info.type,
422			  return_object->package.count));
423
424	/* Extract package count and elements array */
425
426	elements = return_object->package.elements;
427	count = return_object->package.count;
428
429	/* The package must have at least one element, else invalid */
430
431	if (!count) {
432		ACPI_WARNING((AE_INFO,
433			      "%s: Return Package has no elements (empty)",
434			      pathname));
435
436		return (AE_AML_OPERAND_VALUE);
437	}
438
439	/*
440	 * Decode the type of the expected package contents
441	 *
442	 * PTYPE1 packages contain no subpackages
443	 * PTYPE2 packages contain sub-packages
444	 */
445	switch (package->ret_info.type) {
446	case ACPI_PTYPE1_FIXED:
447
448		/*
449		 * The package count is fixed and there are no sub-packages
450		 *
451		 * If package is too small, exit.
452		 * If package is larger than expected, issue warning but continue
453		 */
454		expected_count =
455		    package->ret_info.count1 + package->ret_info.count2;
456		if (count < expected_count) {
457			goto package_too_small;
458		} else if (count > expected_count) {
459			ACPI_WARNING((AE_INFO,
460				      "%s: Return Package is larger than needed - "
461				      "found %u, expected %u", pathname, count,
462				      expected_count));
463		}
464
465		/* Validate all elements of the returned package */
466
467		status = acpi_ns_check_package_elements(pathname, elements,
468							package->ret_info.
469							object_type1,
470							package->ret_info.
471							count1,
472							package->ret_info.
473							object_type2,
474							package->ret_info.
475							count2, 0);
476		if (ACPI_FAILURE(status)) {
477			return (status);
478		}
479		break;
480
481	case ACPI_PTYPE1_VAR:
482
483		/*
484		 * The package count is variable, there are no sub-packages, and all
485		 * elements must be of the same type
486		 */
487		for (i = 0; i < count; i++) {
488			status = acpi_ns_check_object_type(pathname, elements,
489							   package->ret_info.
490							   object_type1, i);
491			if (ACPI_FAILURE(status)) {
492				return (status);
493			}
494			elements++;
495		}
496		break;
497
498	case ACPI_PTYPE1_OPTION:
499
500		/*
501		 * The package count is variable, there are no sub-packages. There are
502		 * a fixed number of required elements, and a variable number of
503		 * optional elements.
504		 *
505		 * Check if package is at least as large as the minimum required
506		 */
507		expected_count = package->ret_info3.count;
508		if (count < expected_count) {
509			goto package_too_small;
510		}
511
512		/* Variable number of sub-objects */
513
514		for (i = 0; i < count; i++) {
515			if (i < package->ret_info3.count) {
516
517				/* These are the required package elements (0, 1, or 2) */
518
519				status =
520				    acpi_ns_check_object_type(pathname,
521							      elements,
522							      package->
523							      ret_info3.
524							      object_type[i],
525							      i);
526				if (ACPI_FAILURE(status)) {
527					return (status);
528				}
529			} else {
530				/* These are the optional package elements */
531
532				status =
533				    acpi_ns_check_object_type(pathname,
534							      elements,
535							      package->
536							      ret_info3.
537							      tail_object_type,
538							      i);
539				if (ACPI_FAILURE(status)) {
540					return (status);
541				}
542			}
543			elements++;
544		}
545		break;
546
547	case ACPI_PTYPE2_PKG_COUNT:
548
549		/* First element is the (Integer) count of sub-packages to follow */
550
551		status = acpi_ns_check_object_type(pathname, elements,
552						   ACPI_RTYPE_INTEGER, 0);
553		if (ACPI_FAILURE(status)) {
554			return (status);
555		}
556
557		/*
558		 * Count cannot be larger than the parent package length, but allow it
559		 * to be smaller. The >= accounts for the Integer above.
560		 */
561		expected_count = (u32) (*elements)->integer.value;
562		if (expected_count >= count) {
563			goto package_too_small;
564		}
565
566		count = expected_count;
567		elements++;
568
569		/* Now we can walk the sub-packages */
570
571		/*lint -fallthrough */
572
573	case ACPI_PTYPE2:
574	case ACPI_PTYPE2_FIXED:
575	case ACPI_PTYPE2_MIN:
576	case ACPI_PTYPE2_COUNT:
577
578		/*
579		 * These types all return a single package that consists of a variable
580		 * number of sub-packages
581		 */
582		for (i = 0; i < count; i++) {
583			sub_package = *elements;
584			sub_elements = sub_package->package.elements;
585
586			/* Each sub-object must be of type Package */
587
588			status =
589			    acpi_ns_check_object_type(pathname, &sub_package,
590						      ACPI_RTYPE_PACKAGE, i);
591			if (ACPI_FAILURE(status)) {
592				return (status);
593			}
594
595			/* Examine the different types of sub-packages */
596
597			switch (package->ret_info.type) {
598			case ACPI_PTYPE2:
599			case ACPI_PTYPE2_PKG_COUNT:
600
601				/* Each subpackage has a fixed number of elements */
602
603				expected_count =
604				    package->ret_info.count1 +
605				    package->ret_info.count2;
606				if (sub_package->package.count !=
607				    expected_count) {
608					count = sub_package->package.count;
609					goto package_too_small;
610				}
611
612				status =
613				    acpi_ns_check_package_elements(pathname,
614								   sub_elements,
615								   package->
616								   ret_info.
617								   object_type1,
618								   package->
619								   ret_info.
620								   count1,
621								   package->
622								   ret_info.
623								   object_type2,
624								   package->
625								   ret_info.
626								   count2, 0);
627				if (ACPI_FAILURE(status)) {
628					return (status);
629				}
630				break;
631
632			case ACPI_PTYPE2_FIXED:
633
634				/* Each sub-package has a fixed length */
635
636				expected_count = package->ret_info2.count;
637				if (sub_package->package.count < expected_count) {
638					count = sub_package->package.count;
639					goto package_too_small;
640				}
641
642				/* Check the type of each sub-package element */
643
644				for (j = 0; j < expected_count; j++) {
645					status =
646					    acpi_ns_check_object_type(pathname,
647						&sub_elements[j],
648						package->ret_info2.object_type[j], j);
649					if (ACPI_FAILURE(status)) {
650						return (status);
651					}
652				}
653				break;
654
655			case ACPI_PTYPE2_MIN:
656
657				/* Each sub-package has a variable but minimum length */
658
659				expected_count = package->ret_info.count1;
660				if (sub_package->package.count < expected_count) {
661					count = sub_package->package.count;
662					goto package_too_small;
663				}
664
665				/* Check the type of each sub-package element */
666
667				status =
668				    acpi_ns_check_package_elements(pathname,
669								   sub_elements,
670								   package->
671								   ret_info.
672								   object_type1,
673								   sub_package->
674								   package.
675								   count, 0, 0,
676								   0);
677				if (ACPI_FAILURE(status)) {
678					return (status);
679				}
680				break;
681
682			case ACPI_PTYPE2_COUNT:
683
684				/* First element is the (Integer) count of elements to follow */
685
686				status =
687				    acpi_ns_check_object_type(pathname,
688							      sub_elements,
689							      ACPI_RTYPE_INTEGER,
690							      0);
691				if (ACPI_FAILURE(status)) {
692					return (status);
693				}
694
695				/* Make sure package is large enough for the Count */
696
697				expected_count =
698				    (u32) (*sub_elements)->integer.value;
699				if (sub_package->package.count < expected_count) {
700					count = sub_package->package.count;
701					goto package_too_small;
702				}
703
704				/* Check the type of each sub-package element */
705
706				status =
707				    acpi_ns_check_package_elements(pathname,
708								   (sub_elements
709								    + 1),
710								   package->
711								   ret_info.
712								   object_type1,
713								   (expected_count
714								    - 1), 0, 0,
715								   1);
716				if (ACPI_FAILURE(status)) {
717					return (status);
718				}
719				break;
720
721			default:
722				break;
723			}
724
725			elements++;
726		}
727		break;
728
729	default:
730
731		/* Should not get here if predefined info table is correct */
732
733		ACPI_WARNING((AE_INFO,
734			      "%s: Invalid internal return type in table entry: %X",
735			      pathname, package->ret_info.type));
736
737		return (AE_AML_INTERNAL);
738	}
739
740	return (AE_OK);
741
742      package_too_small:
743
744	/* Error exit for the case with an incorrect package count */
745
746	ACPI_WARNING((AE_INFO, "%s: Return Package is too small - "
747		      "found %u, expected %u", pathname, count,
748		      expected_count));
749
750	return (AE_AML_OPERAND_VALUE);
751}
752
753/*******************************************************************************
754 *
755 * FUNCTION:    acpi_ns_check_package_elements
756 *
757 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
758 *              Elements        - Pointer to the package elements array
759 *              Type1           - Object type for first group
760 *              Count1          - Count for first group
761 *              Type2           - Object type for second group
762 *              Count2          - Count for second group
763 *              start_index     - Start of the first group of elements
764 *
765 * RETURN:      Status
766 *
767 * DESCRIPTION: Check that all elements of a package are of the correct object
768 *              type. Supports up to two groups of different object types.
769 *
770 ******************************************************************************/
771
772static acpi_status
773acpi_ns_check_package_elements(char *pathname,
774			       union acpi_operand_object **elements,
775			       u8 type1,
776			       u32 count1,
777			       u8 type2, u32 count2, u32 start_index)
778{
779	union acpi_operand_object **this_element = elements;
780	acpi_status status;
781	u32 i;
782
783	/*
784	 * Up to two groups of package elements are supported by the data
785	 * structure. All elements in each group must be of the same type.
786	 * The second group can have a count of zero.
787	 */
788	for (i = 0; i < count1; i++) {
789		status = acpi_ns_check_object_type(pathname, this_element,
790						   type1, i + start_index);
791		if (ACPI_FAILURE(status)) {
792			return (status);
793		}
794		this_element++;
795	}
796
797	for (i = 0; i < count2; i++) {
798		status = acpi_ns_check_object_type(pathname, this_element,
799						   type2,
800						   (i + count1 + start_index));
801		if (ACPI_FAILURE(status)) {
802			return (status);
803		}
804		this_element++;
805	}
806
807	return (AE_OK);
808}
809
810/*******************************************************************************
811 *
812 * FUNCTION:    acpi_ns_check_object_type
813 *
814 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
815 *              return_object_ptr - Pointer to the object returned from the
816 *                                evaluation of a method or object
817 *              expected_btypes - Bitmap of expected return type(s)
818 *              package_index   - Index of object within parent package (if
819 *                                applicable - ACPI_NOT_PACKAGE otherwise)
820 *
821 * RETURN:      Status
822 *
823 * DESCRIPTION: Check the type of the return object against the expected object
824 *              type(s). Use of Btype allows multiple expected object types.
825 *
826 ******************************************************************************/
827
828static acpi_status
829acpi_ns_check_object_type(char *pathname,
830			  union acpi_operand_object **return_object_ptr,
831			  u32 expected_btypes, u32 package_index)
832{
833	union acpi_operand_object *return_object = *return_object_ptr;
834	acpi_status status = AE_OK;
835	u32 return_btype;
836	char type_buffer[48];	/* Room for 5 types */
837	u32 this_rtype;
838	u32 i;
839	u32 j;
840
841	/*
842	 * If we get a NULL return_object here, it is a NULL package element,
843	 * and this is always an error.
844	 */
845	if (!return_object) {
846		goto type_error_exit;
847	}
848
849	/* A Namespace node should not get here, but make sure */
850
851	if (ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
852		ACPI_WARNING((AE_INFO,
853			      "%s: Invalid return type - Found a Namespace node [%4.4s] type %s",
854			      pathname, return_object->node.name.ascii,
855			      acpi_ut_get_type_name(return_object->node.type)));
856		return (AE_AML_OPERAND_TYPE);
857	}
858
859	/*
860	 * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
861	 * The bitmapped type allows multiple possible return types.
862	 *
863	 * Note, the cases below must handle all of the possible types returned
864	 * from all of the predefined names (including elements of returned
865	 * packages)
866	 */
867	switch (return_object->common.type) {
868	case ACPI_TYPE_INTEGER:
869		return_btype = ACPI_RTYPE_INTEGER;
870		break;
871
872	case ACPI_TYPE_BUFFER:
873		return_btype = ACPI_RTYPE_BUFFER;
874		break;
875
876	case ACPI_TYPE_STRING:
877		return_btype = ACPI_RTYPE_STRING;
878		break;
879
880	case ACPI_TYPE_PACKAGE:
881		return_btype = ACPI_RTYPE_PACKAGE;
882		break;
883
884	case ACPI_TYPE_LOCAL_REFERENCE:
885		return_btype = ACPI_RTYPE_REFERENCE;
886		break;
887
888	default:
889		/* Not one of the supported objects, must be incorrect */
890
891		goto type_error_exit;
892	}
893
894	/* Is the object one of the expected types? */
895
896	if (!(return_btype & expected_btypes)) {
897
898		/* Type mismatch -- attempt repair of the returned object */
899
900		status = acpi_ns_repair_object(expected_btypes, package_index,
901					       return_object_ptr);
902		if (ACPI_SUCCESS(status)) {
903			return (status);
904		}
905		goto type_error_exit;
906	}
907
908	/* For reference objects, check that the reference type is correct */
909
910	if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
911		status = acpi_ns_check_reference(pathname, return_object);
912	}
913
914	return (status);
915
916      type_error_exit:
917
918	/* Create a string with all expected types for this predefined object */
919
920	j = 1;
921	type_buffer[0] = 0;
922	this_rtype = ACPI_RTYPE_INTEGER;
923
924	for (i = 0; i < ACPI_NUM_RTYPES; i++) {
925
926		/* If one of the expected types, concatenate the name of this type */
927
928		if (expected_btypes & this_rtype) {
929			ACPI_STRCAT(type_buffer, &acpi_rtype_names[i][j]);
930			j = 0;	/* Use name separator from now on */
931		}
932		this_rtype <<= 1;	/* Next Rtype */
933	}
934
935	if (package_index == ACPI_NOT_PACKAGE) {
936		ACPI_WARNING((AE_INFO,
937			      "%s: Return type mismatch - found %s, expected %s",
938			      pathname,
939			      acpi_ut_get_object_type_name(return_object),
940			      type_buffer));
941	} else {
942		ACPI_WARNING((AE_INFO,
943			      "%s: Return Package type mismatch at index %u - "
944			      "found %s, expected %s", pathname, package_index,
945			      acpi_ut_get_object_type_name(return_object),
946			      type_buffer));
947	}
948
949	return (AE_AML_OPERAND_TYPE);
950}
951
952/*******************************************************************************
953 *
954 * FUNCTION:    acpi_ns_check_reference
955 *
956 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
957 *              return_object   - Object returned from the evaluation of a
958 *                                method or object
959 *
960 * RETURN:      Status
961 *
962 * DESCRIPTION: Check a returned reference object for the correct reference
963 *              type. The only reference type that can be returned from a
964 *              predefined method is a named reference. All others are invalid.
965 *
966 ******************************************************************************/
967
968static acpi_status
969acpi_ns_check_reference(char *pathname,
970			union acpi_operand_object *return_object)
971{
972
973	/*
974	 * Check the reference object for the correct reference type (opcode).
975	 * The only type of reference that can be converted to an union acpi_object is
976	 * a reference to a named object (reference class: NAME)
977	 */
978	if (return_object->reference.class == ACPI_REFCLASS_NAME) {
979		return (AE_OK);
980	}
981
982	ACPI_WARNING((AE_INFO,
983		      "%s: Return type mismatch - "
984		      "unexpected reference object type [%s] %2.2X",
985		      pathname, acpi_ut_get_reference_name(return_object),
986		      return_object->reference.class));
987
988	return (AE_AML_OPERAND_TYPE);
989}
990
991/*******************************************************************************
992 *
993 * FUNCTION:    acpi_ns_repair_object
994 *
995 * PARAMETERS:  Pathname        - Full pathname to the node (for error msgs)
996 *              package_index   - Used to determine if target is in a package
997 *              return_object_ptr - Pointer to the object returned from the
998 *                                evaluation of a method or object
999 *
1000 * RETURN:      Status. AE_OK if repair was successful.
1001 *
1002 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
1003 *              not expected.
1004 *
1005 ******************************************************************************/
1006
1007static acpi_status
1008acpi_ns_repair_object(u32 expected_btypes,
1009		      u32 package_index,
1010		      union acpi_operand_object **return_object_ptr)
1011{
1012	union acpi_operand_object *return_object = *return_object_ptr;
1013	union acpi_operand_object *new_object;
1014	acpi_size length;
1015
1016	switch (return_object->common.type) {
1017	case ACPI_TYPE_BUFFER:
1018
1019		if (!(expected_btypes & ACPI_RTYPE_STRING)) {
1020			return (AE_AML_OPERAND_TYPE);
1021		}
1022
1023		/*
1024		 * Have a Buffer, expected a String, convert. Use a to_string
1025		 * conversion, no transform performed on the buffer data. The best
1026		 * example of this is the _BIF method, where the string data from
1027		 * the battery is often (incorrectly) returned as buffer object(s).
1028		 */
1029		length = 0;
1030		while ((length < return_object->buffer.length) &&
1031		       (return_object->buffer.pointer[length])) {
1032			length++;
1033		}
1034
1035		/* Allocate a new string object */
1036
1037		new_object = acpi_ut_create_string_object(length);
1038		if (!new_object) {
1039			return (AE_NO_MEMORY);
1040		}
1041
1042		/*
1043		 * Copy the raw buffer data with no transform. String is already NULL
1044		 * terminated at Length+1.
1045		 */
1046		ACPI_MEMCPY(new_object->string.pointer,
1047			    return_object->buffer.pointer, length);
1048
1049		/*
1050		 * If the original object is a package element, we need to:
1051		 * 1. Set the reference count of the new object to match the
1052		 *    reference count of the old object.
1053		 * 2. Decrement the reference count of the original object.
1054		 */
1055		if (package_index != ACPI_NOT_PACKAGE) {
1056			new_object->common.reference_count =
1057			    return_object->common.reference_count;
1058
1059			if (return_object->common.reference_count > 1) {
1060				return_object->common.reference_count--;
1061			}
1062		}
1063
1064		/* Delete old object, install the new return object */
1065
1066		acpi_ut_remove_reference(return_object);
1067		*return_object_ptr = new_object;
1068		return (AE_OK);
1069
1070	default:
1071		break;
1072	}
1073
1074	return (AE_AML_OPERAND_TYPE);
1075}
1076