Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / acpi / acpica / nsprepkg.c
blobfbedc6e8ab3653e1fce44ba8c1c88f1232fed69f
1 /******************************************************************************
3 * Module Name: nsprepkg - Validation of package objects for predefined names
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acpredef.h"
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsprepkg")
52 /* Local prototypes */
53 static acpi_status
54 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
55 const union acpi_predefined_info *package,
56 union acpi_operand_object **elements, u32 count);
58 static acpi_status
59 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
60 union acpi_operand_object **elements,
61 u8 type1,
62 u32 count1,
63 u8 type2, u32 count2, u32 start_index);
65 static acpi_status
66 acpi_ns_custom_package(struct acpi_evaluate_info *info,
67 union acpi_operand_object **elements, u32 count);
69 /*******************************************************************************
71 * FUNCTION: acpi_ns_check_package
73 * PARAMETERS: info - Method execution information block
74 * return_object_ptr - Pointer to the object returned from the
75 * evaluation of a method or object
77 * RETURN: Status
79 * DESCRIPTION: Check a returned package object for the correct count and
80 * correct type of all sub-objects.
82 ******************************************************************************/
84 acpi_status
85 acpi_ns_check_package(struct acpi_evaluate_info *info,
86 union acpi_operand_object **return_object_ptr)
88 union acpi_operand_object *return_object = *return_object_ptr;
89 const union acpi_predefined_info *package;
90 union acpi_operand_object **elements;
91 acpi_status status = AE_OK;
92 u32 expected_count;
93 u32 count;
94 u32 i;
96 ACPI_FUNCTION_NAME(ns_check_package);
98 /* The package info for this name is in the next table entry */
100 package = info->predefined + 1;
102 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
103 "%s Validating return Package of Type %X, Count %X\n",
104 info->full_pathname, package->ret_info.type,
105 return_object->package.count));
108 * For variable-length Packages, we can safely remove all embedded
109 * and trailing NULL package elements
111 acpi_ns_remove_null_elements(info, package->ret_info.type,
112 return_object);
114 /* Extract package count and elements array */
116 elements = return_object->package.elements;
117 count = return_object->package.count;
120 * Most packages must have at least one element. The only exception
121 * is the variable-length package (ACPI_PTYPE1_VAR).
123 if (!count) {
124 if (package->ret_info.type == ACPI_PTYPE1_VAR) {
125 return (AE_OK);
128 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
129 info->node_flags,
130 "Return Package has no elements (empty)"));
132 return (AE_AML_OPERAND_VALUE);
136 * Decode the type of the expected package contents
138 * PTYPE1 packages contain no subpackages
139 * PTYPE2 packages contain subpackages
141 switch (package->ret_info.type) {
142 case ACPI_PTYPE_CUSTOM:
144 status = acpi_ns_custom_package(info, elements, count);
145 break;
147 case ACPI_PTYPE1_FIXED:
149 * The package count is fixed and there are no subpackages
151 * If package is too small, exit.
152 * If package is larger than expected, issue warning but continue
154 expected_count =
155 package->ret_info.count1 + package->ret_info.count2;
156 if (count < expected_count) {
157 goto package_too_small;
158 } else if (count > expected_count) {
159 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
160 "%s: Return Package is larger than needed - "
161 "found %u, expected %u\n",
162 info->full_pathname, count,
163 expected_count));
166 /* Validate all elements of the returned package */
168 status = acpi_ns_check_package_elements(info, elements,
169 package->ret_info.
170 object_type1,
171 package->ret_info.
172 count1,
173 package->ret_info.
174 object_type2,
175 package->ret_info.
176 count2, 0);
177 break;
179 case ACPI_PTYPE1_VAR:
181 * The package count is variable, there are no subpackages, and all
182 * elements must be of the same type
184 for (i = 0; i < count; i++) {
185 status = acpi_ns_check_object_type(info, elements,
186 package->ret_info.
187 object_type1, i);
188 if (ACPI_FAILURE(status)) {
189 return (status);
192 elements++;
194 break;
196 case ACPI_PTYPE1_OPTION:
198 * The package count is variable, there are no subpackages. There are
199 * a fixed number of required elements, and a variable number of
200 * optional elements.
202 * Check if package is at least as large as the minimum required
204 expected_count = package->ret_info3.count;
205 if (count < expected_count) {
206 goto package_too_small;
209 /* Variable number of sub-objects */
211 for (i = 0; i < count; i++) {
212 if (i < package->ret_info3.count) {
214 /* These are the required package elements (0, 1, or 2) */
216 status =
217 acpi_ns_check_object_type(info, elements,
218 package->
219 ret_info3.
220 object_type[i],
222 if (ACPI_FAILURE(status)) {
223 return (status);
225 } else {
226 /* These are the optional package elements */
228 status =
229 acpi_ns_check_object_type(info, elements,
230 package->
231 ret_info3.
232 tail_object_type,
234 if (ACPI_FAILURE(status)) {
235 return (status);
239 elements++;
241 break;
243 case ACPI_PTYPE2_REV_FIXED:
245 /* First element is the (Integer) revision */
247 status =
248 acpi_ns_check_object_type(info, elements,
249 ACPI_RTYPE_INTEGER, 0);
250 if (ACPI_FAILURE(status)) {
251 return (status);
254 elements++;
255 count--;
257 /* Examine the subpackages */
259 status =
260 acpi_ns_check_package_list(info, package, elements, count);
261 break;
263 case ACPI_PTYPE2_PKG_COUNT:
265 /* First element is the (Integer) count of subpackages to follow */
267 status =
268 acpi_ns_check_object_type(info, elements,
269 ACPI_RTYPE_INTEGER, 0);
270 if (ACPI_FAILURE(status)) {
271 return (status);
275 * Count cannot be larger than the parent package length, but allow it
276 * to be smaller. The >= accounts for the Integer above.
278 expected_count = (u32)(*elements)->integer.value;
279 if (expected_count >= count) {
280 goto package_too_small;
283 count = expected_count;
284 elements++;
286 /* Examine the subpackages */
288 status =
289 acpi_ns_check_package_list(info, package, elements, count);
290 break;
292 case ACPI_PTYPE2:
293 case ACPI_PTYPE2_FIXED:
294 case ACPI_PTYPE2_MIN:
295 case ACPI_PTYPE2_COUNT:
296 case ACPI_PTYPE2_FIX_VAR:
298 * These types all return a single Package that consists of a
299 * variable number of subpackages.
301 * First, ensure that the first element is a subpackage. If not,
302 * the BIOS may have incorrectly returned the object as a single
303 * package instead of a Package of Packages (a common error if
304 * there is only one entry). We may be able to repair this by
305 * wrapping the returned Package with a new outer Package.
307 if (*elements
308 && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
310 /* Create the new outer package and populate it */
312 status =
313 acpi_ns_wrap_with_package(info, return_object,
314 return_object_ptr);
315 if (ACPI_FAILURE(status)) {
316 return (status);
319 /* Update locals to point to the new package (of 1 element) */
321 return_object = *return_object_ptr;
322 elements = return_object->package.elements;
323 count = 1;
326 /* Examine the subpackages */
328 status =
329 acpi_ns_check_package_list(info, package, elements, count);
330 break;
332 case ACPI_PTYPE2_VAR_VAR:
334 * Returns a variable list of packages, each with a variable list
335 * of objects.
337 break;
339 case ACPI_PTYPE2_UUID_PAIR:
341 /* The package must contain pairs of (UUID + type) */
343 if (count & 1) {
344 expected_count = count + 1;
345 goto package_too_small;
348 while (count > 0) {
349 status = acpi_ns_check_object_type(info, elements,
350 package->ret_info.
351 object_type1, 0);
352 if (ACPI_FAILURE(status)) {
353 return (status);
356 /* Validate length of the UUID buffer */
358 if ((*elements)->buffer.length != 16) {
359 ACPI_WARN_PREDEFINED((AE_INFO,
360 info->full_pathname,
361 info->node_flags,
362 "Invalid length for UUID Buffer"));
363 return (AE_AML_OPERAND_VALUE);
366 status = acpi_ns_check_object_type(info, elements + 1,
367 package->ret_info.
368 object_type2, 0);
369 if (ACPI_FAILURE(status)) {
370 return (status);
373 elements += 2;
374 count -= 2;
376 break;
378 default:
380 /* Should not get here if predefined info table is correct */
382 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
383 info->node_flags,
384 "Invalid internal return type in table entry: %X",
385 package->ret_info.type));
387 return (AE_AML_INTERNAL);
390 return (status);
392 package_too_small:
394 /* Error exit for the case with an incorrect package count */
396 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
397 "Return Package is too small - found %u elements, expected %u",
398 count, expected_count));
400 return (AE_AML_OPERAND_VALUE);
403 /*******************************************************************************
405 * FUNCTION: acpi_ns_check_package_list
407 * PARAMETERS: info - Method execution information block
408 * package - Pointer to package-specific info for method
409 * elements - Element list of parent package. All elements
410 * of this list should be of type Package.
411 * count - Count of subpackages
413 * RETURN: Status
415 * DESCRIPTION: Examine a list of subpackages
417 ******************************************************************************/
419 static acpi_status
420 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
421 const union acpi_predefined_info *package,
422 union acpi_operand_object **elements, u32 count)
424 union acpi_operand_object *sub_package;
425 union acpi_operand_object **sub_elements;
426 acpi_status status;
427 u32 expected_count;
428 u32 i;
429 u32 j;
432 * Validate each subpackage in the parent Package
434 * NOTE: assumes list of subpackages contains no NULL elements.
435 * Any NULL elements should have been removed by earlier call
436 * to acpi_ns_remove_null_elements.
438 for (i = 0; i < count; i++) {
439 sub_package = *elements;
440 sub_elements = sub_package->package.elements;
441 info->parent_package = sub_package;
443 /* Each sub-object must be of type Package */
445 status = acpi_ns_check_object_type(info, &sub_package,
446 ACPI_RTYPE_PACKAGE, i);
447 if (ACPI_FAILURE(status)) {
448 return (status);
451 /* Examine the different types of expected subpackages */
453 info->parent_package = sub_package;
454 switch (package->ret_info.type) {
455 case ACPI_PTYPE2:
456 case ACPI_PTYPE2_PKG_COUNT:
457 case ACPI_PTYPE2_REV_FIXED:
459 /* Each subpackage has a fixed number of elements */
461 expected_count =
462 package->ret_info.count1 + package->ret_info.count2;
463 if (sub_package->package.count < expected_count) {
464 goto package_too_small;
467 status =
468 acpi_ns_check_package_elements(info, sub_elements,
469 package->ret_info.
470 object_type1,
471 package->ret_info.
472 count1,
473 package->ret_info.
474 object_type2,
475 package->ret_info.
476 count2, 0);
477 if (ACPI_FAILURE(status)) {
478 return (status);
480 break;
482 case ACPI_PTYPE2_FIX_VAR:
484 * Each subpackage has a fixed number of elements and an
485 * optional element
487 expected_count =
488 package->ret_info.count1 + package->ret_info.count2;
489 if (sub_package->package.count < expected_count) {
490 goto package_too_small;
493 status =
494 acpi_ns_check_package_elements(info, sub_elements,
495 package->ret_info.
496 object_type1,
497 package->ret_info.
498 count1,
499 package->ret_info.
500 object_type2,
501 sub_package->package.
502 count -
503 package->ret_info.
504 count1, 0);
505 if (ACPI_FAILURE(status)) {
506 return (status);
508 break;
510 case ACPI_PTYPE2_VAR_VAR:
512 * Each subpackage has a fixed or variable number of elements
514 break;
516 case ACPI_PTYPE2_FIXED:
518 /* Each subpackage has a fixed length */
520 expected_count = package->ret_info2.count;
521 if (sub_package->package.count < expected_count) {
522 goto package_too_small;
525 /* Check the type of each subpackage element */
527 for (j = 0; j < expected_count; j++) {
528 status =
529 acpi_ns_check_object_type(info,
530 &sub_elements[j],
531 package->
532 ret_info2.
533 object_type[j],
535 if (ACPI_FAILURE(status)) {
536 return (status);
539 break;
541 case ACPI_PTYPE2_MIN:
543 /* Each subpackage has a variable but minimum length */
545 expected_count = package->ret_info.count1;
546 if (sub_package->package.count < expected_count) {
547 goto package_too_small;
550 /* Check the type of each subpackage element */
552 status =
553 acpi_ns_check_package_elements(info, sub_elements,
554 package->ret_info.
555 object_type1,
556 sub_package->package.
557 count, 0, 0, 0);
558 if (ACPI_FAILURE(status)) {
559 return (status);
561 break;
563 case ACPI_PTYPE2_COUNT:
565 * First element is the (Integer) count of elements, including
566 * the count field (the ACPI name is num_elements)
568 status = acpi_ns_check_object_type(info, sub_elements,
569 ACPI_RTYPE_INTEGER,
571 if (ACPI_FAILURE(status)) {
572 return (status);
576 * Make sure package is large enough for the Count and is
577 * is as large as the minimum size
579 expected_count = (u32)(*sub_elements)->integer.value;
580 if (sub_package->package.count < expected_count) {
581 goto package_too_small;
584 if (sub_package->package.count <
585 package->ret_info.count1) {
586 expected_count = package->ret_info.count1;
587 goto package_too_small;
590 if (expected_count == 0) {
592 * Either the num_entries element was originally zero or it was
593 * a NULL element and repaired to an Integer of value zero.
594 * In either case, repair it by setting num_entries to be the
595 * actual size of the subpackage.
597 expected_count = sub_package->package.count;
598 (*sub_elements)->integer.value = expected_count;
601 /* Check the type of each subpackage element */
603 status =
604 acpi_ns_check_package_elements(info,
605 (sub_elements + 1),
606 package->ret_info.
607 object_type1,
608 (expected_count - 1),
609 0, 0, 1);
610 if (ACPI_FAILURE(status)) {
611 return (status);
613 break;
615 default: /* Should not get here, type was validated by caller */
617 return (AE_AML_INTERNAL);
620 elements++;
623 return (AE_OK);
625 package_too_small:
627 /* The subpackage count was smaller than required */
629 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
630 "Return SubPackage[%u] is too small - found %u elements, expected %u",
631 i, sub_package->package.count, expected_count));
633 return (AE_AML_OPERAND_VALUE);
636 /*******************************************************************************
638 * FUNCTION: acpi_ns_custom_package
640 * PARAMETERS: info - Method execution information block
641 * elements - Pointer to the package elements array
642 * count - Element count for the package
644 * RETURN: Status
646 * DESCRIPTION: Check a returned package object for the correct count and
647 * correct type of all sub-objects.
649 * NOTE: Currently used for the _BIX method only. When needed for two or more
650 * methods, probably a detect/dispatch mechanism will be required.
652 ******************************************************************************/
654 static acpi_status
655 acpi_ns_custom_package(struct acpi_evaluate_info *info,
656 union acpi_operand_object **elements, u32 count)
658 u32 expected_count;
659 u32 version;
660 acpi_status status = AE_OK;
662 ACPI_FUNCTION_NAME(ns_custom_package);
664 /* Get version number, must be Integer */
666 if ((*elements)->common.type != ACPI_TYPE_INTEGER) {
667 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
668 info->node_flags,
669 "Return Package has invalid object type for version number"));
670 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
673 version = (u32)(*elements)->integer.value;
674 expected_count = 21; /* Version 1 */
676 if (version == 0) {
677 expected_count = 20; /* Version 0 */
680 if (count < expected_count) {
681 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
682 info->node_flags,
683 "Return Package is too small - found %u elements, expected %u",
684 count, expected_count));
685 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
686 } else if (count > expected_count) {
687 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
688 "%s: Return Package is larger than needed - "
689 "found %u, expected %u\n",
690 info->full_pathname, count, expected_count));
693 /* Validate all elements of the returned package */
695 status = acpi_ns_check_package_elements(info, elements,
696 ACPI_RTYPE_INTEGER, 16,
697 ACPI_RTYPE_STRING, 4, 0);
698 if (ACPI_FAILURE(status)) {
699 return_ACPI_STATUS(status);
702 /* Version 1 has a single trailing integer */
704 if (version > 0) {
705 status = acpi_ns_check_package_elements(info, elements + 20,
706 ACPI_RTYPE_INTEGER, 1,
707 0, 0, 20);
710 return_ACPI_STATUS(status);
713 /*******************************************************************************
715 * FUNCTION: acpi_ns_check_package_elements
717 * PARAMETERS: info - Method execution information block
718 * elements - Pointer to the package elements array
719 * type1 - Object type for first group
720 * count1 - Count for first group
721 * type2 - Object type for second group
722 * count2 - Count for second group
723 * start_index - Start of the first group of elements
725 * RETURN: Status
727 * DESCRIPTION: Check that all elements of a package are of the correct object
728 * type. Supports up to two groups of different object types.
730 ******************************************************************************/
732 static acpi_status
733 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
734 union acpi_operand_object **elements,
735 u8 type1,
736 u32 count1,
737 u8 type2, u32 count2, u32 start_index)
739 union acpi_operand_object **this_element = elements;
740 acpi_status status;
741 u32 i;
744 * Up to two groups of package elements are supported by the data
745 * structure. All elements in each group must be of the same type.
746 * The second group can have a count of zero.
748 for (i = 0; i < count1; i++) {
749 status = acpi_ns_check_object_type(info, this_element,
750 type1, i + start_index);
751 if (ACPI_FAILURE(status)) {
752 return (status);
755 this_element++;
758 for (i = 0; i < count2; i++) {
759 status = acpi_ns_check_object_type(info, this_element,
760 type2,
761 (i + count1 + start_index));
762 if (ACPI_FAILURE(status)) {
763 return (status);
766 this_element++;
769 return (AE_OK);