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