1 /******************************************************************************
3 * Module Name: nsprepkg - Validation of package objects for predefined names
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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);
65 /*******************************************************************************
67 * FUNCTION: acpi_ns_check_package
69 * PARAMETERS: info - Method execution information block
70 * return_object_ptr - Pointer to the object returned from the
71 * evaluation of a method or object
75 * DESCRIPTION: Check a returned package object for the correct count and
76 * correct type of all sub-objects.
78 ******************************************************************************/
81 acpi_ns_check_package(struct acpi_evaluate_info *info,
82 union acpi_operand_object **return_object_ptr)
84 union acpi_operand_object *return_object = *return_object_ptr;
85 const union acpi_predefined_info *package;
86 union acpi_operand_object **elements;
87 acpi_status status = AE_OK;
92 ACPI_FUNCTION_NAME(ns_check_package);
94 /* The package info for this name is in the next table entry */
96 package = info->predefined + 1;
98 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
99 "%s Validating return Package of Type %X, Count %X\n",
100 info->full_pathname, package->ret_info.type,
101 return_object->package.count));
104 * For variable-length Packages, we can safely remove all embedded
105 * and trailing NULL package elements
107 acpi_ns_remove_null_elements(info, package->ret_info.type,
110 /* Extract package count and elements array */
112 elements = return_object->package.elements;
113 count = return_object->package.count;
116 * Most packages must have at least one element. The only exception
117 * is the variable-length package (ACPI_PTYPE1_VAR).
120 if (package->ret_info.type == ACPI_PTYPE1_VAR) {
124 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
126 "Return Package has no elements (empty)"));
128 return (AE_AML_OPERAND_VALUE);
132 * Decode the type of the expected package contents
134 * PTYPE1 packages contain no subpackages
135 * PTYPE2 packages contain subpackages
137 switch (package->ret_info.type) {
138 case ACPI_PTYPE1_FIXED:
140 * The package count is fixed and there are no subpackages
142 * If package is too small, exit.
143 * If package is larger than expected, issue warning but continue
146 package->ret_info.count1 + package->ret_info.count2;
147 if (count < expected_count) {
148 goto package_too_small;
149 } else if (count > expected_count) {
150 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
151 "%s: Return Package is larger than needed - "
152 "found %u, expected %u\n",
153 info->full_pathname, count,
157 /* Validate all elements of the returned package */
159 status = acpi_ns_check_package_elements(info, elements,
170 case ACPI_PTYPE1_VAR:
172 * The package count is variable, there are no subpackages, and all
173 * elements must be of the same type
175 for (i = 0; i < count; i++) {
176 status = acpi_ns_check_object_type(info, elements,
179 if (ACPI_FAILURE(status)) {
187 case ACPI_PTYPE1_OPTION:
189 * The package count is variable, there are no subpackages. There are
190 * a fixed number of required elements, and a variable number of
193 * Check if package is at least as large as the minimum required
195 expected_count = package->ret_info3.count;
196 if (count < expected_count) {
197 goto package_too_small;
200 /* Variable number of sub-objects */
202 for (i = 0; i < count; i++) {
203 if (i < package->ret_info3.count) {
205 /* These are the required package elements (0, 1, or 2) */
208 acpi_ns_check_object_type(info, elements,
213 if (ACPI_FAILURE(status)) {
217 /* These are the optional package elements */
220 acpi_ns_check_object_type(info, elements,
225 if (ACPI_FAILURE(status)) {
234 case ACPI_PTYPE2_REV_FIXED:
236 /* First element is the (Integer) revision */
239 acpi_ns_check_object_type(info, elements,
240 ACPI_RTYPE_INTEGER, 0);
241 if (ACPI_FAILURE(status)) {
248 /* Examine the subpackages */
251 acpi_ns_check_package_list(info, package, elements, count);
254 case ACPI_PTYPE2_PKG_COUNT:
256 /* First element is the (Integer) count of subpackages to follow */
259 acpi_ns_check_object_type(info, elements,
260 ACPI_RTYPE_INTEGER, 0);
261 if (ACPI_FAILURE(status)) {
266 * Count cannot be larger than the parent package length, but allow it
267 * to be smaller. The >= accounts for the Integer above.
269 expected_count = (u32)(*elements)->integer.value;
270 if (expected_count >= count) {
271 goto package_too_small;
274 count = expected_count;
277 /* Examine the subpackages */
280 acpi_ns_check_package_list(info, package, elements, count);
284 case ACPI_PTYPE2_FIXED:
285 case ACPI_PTYPE2_MIN:
286 case ACPI_PTYPE2_COUNT:
287 case ACPI_PTYPE2_FIX_VAR:
289 * These types all return a single Package that consists of a
290 * variable number of subpackages.
292 * First, ensure that the first element is a subpackage. If not,
293 * the BIOS may have incorrectly returned the object as a single
294 * package instead of a Package of Packages (a common error if
295 * there is only one entry). We may be able to repair this by
296 * wrapping the returned Package with a new outer Package.
299 && ((*elements)->common.type != ACPI_TYPE_PACKAGE)) {
301 /* Create the new outer package and populate it */
304 acpi_ns_wrap_with_package(info, return_object,
306 if (ACPI_FAILURE(status)) {
310 /* Update locals to point to the new package (of 1 element) */
312 return_object = *return_object_ptr;
313 elements = return_object->package.elements;
317 /* Examine the subpackages */
320 acpi_ns_check_package_list(info, package, elements, count);
323 case ACPI_PTYPE2_VAR_VAR:
325 * Returns a variable list of packages, each with a variable list
330 case ACPI_PTYPE2_UUID_PAIR:
332 /* The package must contain pairs of (UUID + type) */
335 expected_count = count + 1;
336 goto package_too_small;
340 status = acpi_ns_check_object_type(info, elements,
343 if (ACPI_FAILURE(status)) {
347 /* Validate length of the UUID buffer */
349 if ((*elements)->buffer.length != 16) {
350 ACPI_WARN_PREDEFINED((AE_INFO,
353 "Invalid length for UUID Buffer"));
354 return (AE_AML_OPERAND_VALUE);
357 status = acpi_ns_check_object_type(info, elements + 1,
360 if (ACPI_FAILURE(status)) {
371 /* Should not get here if predefined info table is correct */
373 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
375 "Invalid internal return type in table entry: %X",
376 package->ret_info.type));
378 return (AE_AML_INTERNAL);
385 /* Error exit for the case with an incorrect package count */
387 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
388 "Return Package is too small - found %u elements, expected %u",
389 count, expected_count));
391 return (AE_AML_OPERAND_VALUE);
394 /*******************************************************************************
396 * FUNCTION: acpi_ns_check_package_list
398 * PARAMETERS: info - Method execution information block
399 * package - Pointer to package-specific info for method
400 * elements - Element list of parent package. All elements
401 * of this list should be of type Package.
402 * count - Count of subpackages
406 * DESCRIPTION: Examine a list of subpackages
408 ******************************************************************************/
411 acpi_ns_check_package_list(struct acpi_evaluate_info *info,
412 const union acpi_predefined_info *package,
413 union acpi_operand_object **elements, u32 count)
415 union acpi_operand_object *sub_package;
416 union acpi_operand_object **sub_elements;
423 * Validate each subpackage in the parent Package
425 * NOTE: assumes list of subpackages contains no NULL elements.
426 * Any NULL elements should have been removed by earlier call
427 * to acpi_ns_remove_null_elements.
429 for (i = 0; i < count; i++) {
430 sub_package = *elements;
431 sub_elements = sub_package->package.elements;
432 info->parent_package = sub_package;
434 /* Each sub-object must be of type Package */
436 status = acpi_ns_check_object_type(info, &sub_package,
437 ACPI_RTYPE_PACKAGE, i);
438 if (ACPI_FAILURE(status)) {
442 /* Examine the different types of expected subpackages */
444 info->parent_package = sub_package;
445 switch (package->ret_info.type) {
447 case ACPI_PTYPE2_PKG_COUNT:
448 case ACPI_PTYPE2_REV_FIXED:
450 /* Each subpackage has a fixed number of elements */
453 package->ret_info.count1 + package->ret_info.count2;
454 if (sub_package->package.count < expected_count) {
455 goto package_too_small;
459 acpi_ns_check_package_elements(info, sub_elements,
468 if (ACPI_FAILURE(status)) {
473 case ACPI_PTYPE2_FIX_VAR:
475 * Each subpackage has a fixed number of elements and an
479 package->ret_info.count1 + package->ret_info.count2;
480 if (sub_package->package.count < expected_count) {
481 goto package_too_small;
485 acpi_ns_check_package_elements(info, sub_elements,
492 sub_package->package.
496 if (ACPI_FAILURE(status)) {
501 case ACPI_PTYPE2_VAR_VAR:
503 * Each subpackage has a fixed or variable number of elements
507 case ACPI_PTYPE2_FIXED:
509 /* Each subpackage has a fixed length */
511 expected_count = package->ret_info2.count;
512 if (sub_package->package.count < expected_count) {
513 goto package_too_small;
516 /* Check the type of each subpackage element */
518 for (j = 0; j < expected_count; j++) {
520 acpi_ns_check_object_type(info,
526 if (ACPI_FAILURE(status)) {
532 case ACPI_PTYPE2_MIN:
534 /* Each subpackage has a variable but minimum length */
536 expected_count = package->ret_info.count1;
537 if (sub_package->package.count < expected_count) {
538 goto package_too_small;
541 /* Check the type of each subpackage element */
544 acpi_ns_check_package_elements(info, sub_elements,
547 sub_package->package.
549 if (ACPI_FAILURE(status)) {
554 case ACPI_PTYPE2_COUNT:
556 * First element is the (Integer) count of elements, including
557 * the count field (the ACPI name is num_elements)
559 status = acpi_ns_check_object_type(info, sub_elements,
562 if (ACPI_FAILURE(status)) {
567 * Make sure package is large enough for the Count and is
568 * is as large as the minimum size
570 expected_count = (u32)(*sub_elements)->integer.value;
571 if (sub_package->package.count < expected_count) {
572 goto package_too_small;
575 if (sub_package->package.count <
576 package->ret_info.count1) {
577 expected_count = package->ret_info.count1;
578 goto package_too_small;
581 if (expected_count == 0) {
583 * Either the num_entries element was originally zero or it was
584 * a NULL element and repaired to an Integer of value zero.
585 * In either case, repair it by setting num_entries to be the
586 * actual size of the subpackage.
588 expected_count = sub_package->package.count;
589 (*sub_elements)->integer.value = expected_count;
592 /* Check the type of each subpackage element */
595 acpi_ns_check_package_elements(info,
599 (expected_count - 1),
601 if (ACPI_FAILURE(status)) {
606 default: /* Should not get here, type was validated by caller */
608 return (AE_AML_INTERNAL);
618 /* The subpackage count was smaller than required */
620 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
621 "Return SubPackage[%u] is too small - found %u elements, expected %u",
622 i, sub_package->package.count, expected_count));
624 return (AE_AML_OPERAND_VALUE);
627 /*******************************************************************************
629 * FUNCTION: acpi_ns_check_package_elements
631 * PARAMETERS: info - Method execution information block
632 * elements - Pointer to the package elements array
633 * type1 - Object type for first group
634 * count1 - Count for first group
635 * type2 - Object type for second group
636 * count2 - Count for second group
637 * start_index - Start of the first group of elements
641 * DESCRIPTION: Check that all elements of a package are of the correct object
642 * type. Supports up to two groups of different object types.
644 ******************************************************************************/
647 acpi_ns_check_package_elements(struct acpi_evaluate_info *info,
648 union acpi_operand_object **elements,
651 u8 type2, u32 count2, u32 start_index)
653 union acpi_operand_object **this_element = elements;
658 * Up to two groups of package elements are supported by the data
659 * structure. All elements in each group must be of the same type.
660 * The second group can have a count of zero.
662 for (i = 0; i < count1; i++) {
663 status = acpi_ns_check_object_type(info, this_element,
664 type1, i + start_index);
665 if (ACPI_FAILURE(status)) {
672 for (i = 0; i < count2; i++) {
673 status = acpi_ns_check_object_type(info, this_element,
675 (i + count1 + start_index));
676 if (ACPI_FAILURE(status)) {