Merge branch 'md-raid6-accel' into ioat3.2
[linux-2.6-block.git] / drivers / acpi / acpica / nspredef.c
CommitLineData
e8707b34
BM
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>
e2f7a777
LB
46#include "accommon.h"
47#include "acnamesp.h"
48#include "acpredef.h"
e8707b34
BM
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,
a647b5c3 76 union acpi_operand_object **return_object_ptr,
e8707b34
BM
77 const union acpi_predefined_info *predefined);
78
79static acpi_status
80acpi_ns_check_package_elements(char *pathname,
81 union acpi_operand_object **elements,
03ef132b
BM
82 u8 type1,
83 u32 count1,
84 u8 type2, u32 count2, u32 start_index);
e8707b34
BM
85
86static acpi_status
87acpi_ns_check_object_type(char *pathname,
a647b5c3 88 union acpi_operand_object **return_object_ptr,
e8707b34
BM
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
a647b5c3
BM
95static acpi_status
96acpi_ns_repair_object(u32 expected_btypes,
97 u32 package_index,
98 union acpi_operand_object **return_object_ptr);
99
e8707b34
BM
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
a647b5c3
BM
119 * return_object_ptr - Pointer to the object returned from the
120 * evaluation of a method or object
e8707b34
BM
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,
eeb4437e
BM
130 u32 user_param_count,
131 acpi_status return_status,
a647b5c3 132 union acpi_operand_object **return_object_ptr)
e8707b34 133{
a647b5c3 134 union acpi_operand_object *return_object = *return_object_ptr;
e8707b34
BM
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);
e8707b34
BM
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 /*
eeb4437e
BM
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.
e8707b34 174 */
eeb4437e
BM
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;
e8707b34
BM
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 *
eeb4437e 206 * For example, if the "Implicit Return" feature is enabled, methods will
e8707b34
BM
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 */
a647b5c3 217 status = acpi_ns_check_object_type(pathname, return_object_ptr,
e8707b34
BM
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
3371c19c 226 if (return_object->common.type == ACPI_TYPE_PACKAGE) {
e8707b34 227 status =
a647b5c3
BM
228 acpi_ns_check_package(pathname, return_object_ptr,
229 predefined);
e8707b34
BM
230 }
231
232 exit:
eeb4437e 233 if (pathname != predefined->info.name) {
e8707b34
BM
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
eeb4437e 246 * user_param_count - Number of args passed in by the caller
e8707b34
BM
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
257void
258acpi_ns_check_parameter_count(char *pathname,
259 struct acpi_namespace_node *node,
eeb4437e 260 u32 user_param_count,
e8707b34
BM
261 const union acpi_predefined_info *predefined)
262{
263 u32 param_count;
264 u32 required_params_current;
265 u32 required_params_old;
266
eeb4437e
BM
267 /* Methods have 0-7 parameters. All other types have zero. */
268
e8707b34
BM
269 param_count = 0;
270 if (node->type == ACPI_TYPE_METHOD) {
271 param_count = node->object->method.param_count;
272 }
273
eeb4437e
BM
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.) */
e8707b34
BM
296
297 required_params_current = predefined->info.param_count & 0x0F;
298 required_params_old = predefined->info.param_count >> 4;
299
eeb4437e
BM
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,
d4913dc6
BM
307 "%s: Parameter count mismatch - "
308 "caller passed %d, ACPI requires %d",
eeb4437e
BM
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 */
e8707b34
BM
327 if ((param_count != required_params_current) &&
328 (param_count != required_params_old)) {
329 ACPI_WARNING((AE_INFO,
eeb4437e 330 "%s: Parameter count mismatch - ASL declared %d, ACPI requires %d",
e8707b34
BM
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
347const 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)
a647b5c3
BM
389 * return_object_ptr - Pointer to the object returned from the
390 * evaluation of a method or object
e8707b34
BM
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
400static acpi_status
401acpi_ns_check_package(char *pathname,
a647b5c3 402 union acpi_operand_object **return_object_ptr,
e8707b34
BM
403 const union acpi_predefined_info *predefined)
404{
a647b5c3 405 union acpi_operand_object *return_object = *return_object_ptr;
e8707b34
BM
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.
03ef132b 478 count2, 0);
e8707b34
BM
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++) {
a647b5c3 491 status = acpi_ns_check_object_type(pathname, elements,
e8707b34
BM
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,
a647b5c3 524 elements,
e8707b34
BM
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,
a647b5c3 537 elements,
e8707b34
BM
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
a647b5c3 554 status = acpi_ns_check_object_type(pathname, elements,
e8707b34
BM
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 =
a647b5c3 592 acpi_ns_check_object_type(pathname, &sub_package,
e8707b34
BM
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.
03ef132b 629 count2, 0);
e8707b34
BM
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,
a647b5c3
BM
650 &sub_elements[j],
651 package->ret_info2.object_type[j], j);
e8707b34
BM
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.
03ef132b
BM
678 count, 0, 0,
679 0);
e8707b34
BM
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,
a647b5c3 691 sub_elements,
e8707b34
BM
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
03ef132b
BM
717 - 1), 0, 0,
718 1);
e8707b34
BM
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
03ef132b 766 * start_index - Start of the first group of elements
e8707b34
BM
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
775static acpi_status
776acpi_ns_check_package_elements(char *pathname,
777 union acpi_operand_object **elements,
03ef132b
BM
778 u8 type1,
779 u32 count1,
780 u8 type2, u32 count2, u32 start_index)
e8707b34
BM
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++) {
a647b5c3 792 status = acpi_ns_check_object_type(pathname, this_element,
03ef132b 793 type1, i + start_index);
e8707b34
BM
794 if (ACPI_FAILURE(status)) {
795 return (status);
796 }
797 this_element++;
798 }
799
800 for (i = 0; i < count2; i++) {
a647b5c3 801 status = acpi_ns_check_object_type(pathname, this_element,
03ef132b
BM
802 type2,
803 (i + count1 + start_index));
e8707b34
BM
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)
a647b5c3
BM
818 * return_object_ptr - Pointer to the object returned from the
819 * evaluation of a method or object
e8707b34
BM
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
831static acpi_status
832acpi_ns_check_object_type(char *pathname,
a647b5c3 833 union acpi_operand_object **return_object_ptr,
e8707b34
BM
834 u32 expected_btypes, u32 package_index)
835{
a647b5c3 836 union acpi_operand_object *return_object = *return_object_ptr;
e8707b34
BM
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 */
3371c19c 870 switch (return_object->common.type) {
e8707b34
BM
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)) {
a647b5c3
BM
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 }
e8707b34
BM
908 goto type_error_exit;
909 }
910
911 /* For reference objects, check that the reference type is correct */
912
3371c19c 913 if (return_object->common.type == ACPI_TYPE_LOCAL_REFERENCE) {
e8707b34
BM
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
971static acpi_status
972acpi_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,
d4913dc6
BM
986 "%s: Return type mismatch - "
987 "unexpected reference object type [%s] %2.2X",
e8707b34
BM
988 pathname, acpi_ut_get_reference_name(return_object),
989 return_object->reference.class));
990
991 return (AE_AML_OPERAND_TYPE);
992}
a647b5c3
BM
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
1010static acpi_status
1011acpi_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
3371c19c 1019 switch (return_object->common.type) {
a647b5c3
BM
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}