1 /******************************************************************************
3 * Module Name: nsprepkg - Validation of package objects for predefined names
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsprepkg")
52 /* Local prototypes */
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);
59 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
60 union acpi_operand_object **elements,
63 u8 type2, u32 count2, u32 start_index);
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
79 * DESCRIPTION: Check a returned package object for the correct count and
80 * correct type of all sub-objects.
82 ******************************************************************************/
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;
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,
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).
124 if (package->ret_info.type == ACPI_PTYPE1_VAR) {
128 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
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);
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
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,
166 /* Validate all elements of the returned package */
168 status = acpi_ns_check_package_elements(info, elements,
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,
188 if (ACPI_FAILURE(status)) {
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
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) */
217 acpi_ns_check_object_type(info, elements,
222 if (ACPI_FAILURE(status)) {
226 /* These are the optional package elements */
229 acpi_ns_check_object_type(info, elements,
234 if (ACPI_FAILURE(status)) {
243 case ACPI_PTYPE2_REV_FIXED:
245 /* First element is the (Integer) revision */
248 acpi_ns_check_object_type(info, elements,
249 ACPI_RTYPE_INTEGER, 0);
250 if (ACPI_FAILURE(status)) {
257 /* Examine the subpackages */
260 acpi_ns_check_package_list(info, package, elements, count);
263 case ACPI_PTYPE2_PKG_COUNT:
265 /* First element is the (Integer) count of subpackages to follow */
268 acpi_ns_check_object_type(info, elements,
269 ACPI_RTYPE_INTEGER, 0);
270 if (ACPI_FAILURE(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;
286 /* Examine the subpackages */
289 acpi_ns_check_package_list(info, package, elements, count);
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.
308 && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
310 /* Create the new outer package and populate it */
313 acpi_ns_wrap_with_package(info, return_object,
315 if (ACPI_FAILURE(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;
326 /* Examine the subpackages */
329 acpi_ns_check_package_list(info, package, elements, count);
332 case ACPI_PTYPE2_VAR_VAR:
334 * Returns a variable list of packages, each with a variable list
339 case ACPI_PTYPE2_UUID_PAIR:
341 /* The package must contain pairs of (UUID + type) */
344 expected_count = count + 1;
345 goto package_too_small;
349 status = acpi_ns_check_object_type(info, elements,
352 if (ACPI_FAILURE(status)) {
356 /* Validate length of the UUID buffer */
358 if ((*elements)->buffer.length != 16) {
359 ACPI_WARN_PREDEFINED((AE_INFO,
362 "Invalid length for UUID Buffer"));
363 return (AE_AML_OPERAND_VALUE);
366 status = acpi_ns_check_object_type(info, elements + 1,
369 if (ACPI_FAILURE(status)) {
380 /* Should not get here if predefined info table is correct */
382 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
384 "Invalid internal return type in table entry: %X",
385 package->ret_info.type));
387 return (AE_AML_INTERNAL);
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
415 * DESCRIPTION: Examine a list of subpackages
417 ******************************************************************************/
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;
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)) {
451 /* Examine the different types of expected subpackages */
453 info->parent_package = sub_package;
454 switch (package->ret_info.type) {
456 case ACPI_PTYPE2_PKG_COUNT:
457 case ACPI_PTYPE2_REV_FIXED:
459 /* Each subpackage has a fixed number of elements */
462 package->ret_info.count1 + package->ret_info.count2;
463 if (sub_package->package.count < expected_count) {
464 goto package_too_small;
468 acpi_ns_check_package_elements(info, sub_elements,
477 if (ACPI_FAILURE(status)) {
482 case ACPI_PTYPE2_FIX_VAR:
484 * Each subpackage has a fixed number of elements and an
488 package->ret_info.count1 + package->ret_info.count2;
489 if (sub_package->package.count < expected_count) {
490 goto package_too_small;
494 acpi_ns_check_package_elements(info, sub_elements,
501 sub_package->package.
505 if (ACPI_FAILURE(status)) {
510 case ACPI_PTYPE2_VAR_VAR:
512 * Each subpackage has a fixed or variable number of elements
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++) {
529 acpi_ns_check_object_type(info,
535 if (ACPI_FAILURE(status)) {
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 */
553 acpi_ns_check_package_elements(info, sub_elements,
556 sub_package->package.
558 if (ACPI_FAILURE(status)) {
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,
571 if (ACPI_FAILURE(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 */
604 acpi_ns_check_package_elements(info,
608 (expected_count - 1),
610 if (ACPI_FAILURE(status)) {
615 default: /* Should not get here, type was validated by caller */
617 ACPI_ERROR((AE_INFO, "Invalid Package type: %X",
618 package->ret_info.type));
619 return (AE_AML_INTERNAL);
629 /* The subpackage count was smaller than required */
631 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
632 "Return SubPackage[%u] is too small - found %u elements, expected %u",
633 i, sub_package->package.count, expected_count));
635 return (AE_AML_OPERAND_VALUE);
638 /*******************************************************************************
640 * FUNCTION: acpi_ns_custom_package
642 * PARAMETERS: info - Method execution information block
643 * elements - Pointer to the package elements array
644 * count - Element count for the package
648 * DESCRIPTION: Check a returned package object for the correct count and
649 * correct type of all sub-objects.
651 * NOTE: Currently used for the _BIX method only. When needed for two or more
652 * methods, probably a detect/dispatch mechanism will be required.
654 ******************************************************************************/
657 acpi_ns_custom_package(struct acpi_evaluate_info *info,
658 union acpi_operand_object **elements, u32 count)
662 acpi_status status = AE_OK;
664 ACPI_FUNCTION_NAME(ns_custom_package);
666 /* Get version number, must be Integer */
668 if ((*elements)->common.type != ACPI_TYPE_INTEGER) {
669 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
671 "Return Package has invalid object type for version number"));
672 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
675 version = (u32)(*elements)->integer.value;
676 expected_count = 21; /* Version 1 */
679 expected_count = 20; /* Version 0 */
682 if (count < expected_count) {
683 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
685 "Return Package is too small - found %u elements, expected %u",
686 count, expected_count));
687 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
688 } else if (count > expected_count) {
689 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
690 "%s: Return Package is larger than needed - "
691 "found %u, expected %u\n",
692 info->full_pathname, count, expected_count));
695 /* Validate all elements of the returned package */
697 status = acpi_ns_check_package_elements(info, elements,
698 ACPI_RTYPE_INTEGER, 16,
699 ACPI_RTYPE_STRING, 4, 0);
700 if (ACPI_FAILURE(status)) {
701 return_ACPI_STATUS(status);
704 /* Version 1 has a single trailing integer */
707 status = acpi_ns_check_package_elements(info, elements + 20,
708 ACPI_RTYPE_INTEGER, 1,
712 return_ACPI_STATUS(status);
715 /*******************************************************************************
717 * FUNCTION: acpi_ns_check_package_elements
719 * PARAMETERS: info - Method execution information block
720 * elements - Pointer to the package elements array
721 * type1 - Object type for first group
722 * count1 - Count for first group
723 * type2 - Object type for second group
724 * count2 - Count for second group
725 * start_index - Start of the first group of elements
729 * DESCRIPTION: Check that all elements of a package are of the correct object
730 * type. Supports up to two groups of different object types.
732 ******************************************************************************/
735 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
736 union acpi_operand_object **elements,
739 u8 type2, u32 count2, u32 start_index)
741 union acpi_operand_object **this_element = elements;
746 * Up to two groups of package elements are supported by the data
747 * structure. All elements in each group must be of the same type.
748 * The second group can have a count of zero.
750 for (i = 0; i < count1; i++) {
751 status = acpi_ns_check_object_type(info, this_element,
752 type1, i + start_index);
753 if (ACPI_FAILURE(status)) {
760 for (i = 0; i < count2; i++) {
761 status = acpi_ns_check_object_type(info, this_element,
763 (i + count1 + start_index));
764 if (ACPI_FAILURE(status)) {