1 /******************************************************************************
3 * Module Name: nsprepkg - Validation of package objects for predefined names
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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_predefined_data
*data
,
55 const union acpi_predefined_info
*package
,
56 union acpi_operand_object
**elements
, u32 count
);
59 acpi_ns_check_package_elements(struct acpi_predefined_data
*data
,
60 union acpi_operand_object
**elements
,
63 u8 type2
, u32 count2
, u32 start_index
);
65 /*******************************************************************************
67 * FUNCTION: acpi_ns_check_package
69 * PARAMETERS: data - Pointer to validation data structure
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_predefined_data
*data
,
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
= data
->predefined
+ 1;
98 ACPI_DEBUG_PRINT((ACPI_DB_NAMES
,
99 "%s Validating return Package of Type %X, Count %X\n",
100 data
->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(data
, 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
, data
->pathname
, data
->node_flags
,
125 "Return Package has no elements (empty)"));
127 return (AE_AML_OPERAND_VALUE
);
131 * Decode the type of the expected package contents
133 * PTYPE1 packages contain no subpackages
134 * PTYPE2 packages contain sub-packages
136 switch (package
->ret_info
.type
) {
137 case ACPI_PTYPE1_FIXED
:
140 * The package count is fixed and there are no sub-packages
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 data
->pathname
, count
,
157 /* Validate all elements of the returned package */
159 status
= acpi_ns_check_package_elements(data
, elements
,
170 case ACPI_PTYPE1_VAR
:
173 * The package count is variable, there are no sub-packages, and all
174 * elements must be of the same type
176 for (i
= 0; i
< count
; i
++) {
177 status
= acpi_ns_check_object_type(data
, elements
,
180 if (ACPI_FAILURE(status
)) {
187 case ACPI_PTYPE1_OPTION
:
190 * The package count is variable, there are no sub-packages. There are
191 * a fixed number of required elements, and a variable number of
194 * Check if package is at least as large as the minimum required
196 expected_count
= package
->ret_info3
.count
;
197 if (count
< expected_count
) {
198 goto package_too_small
;
201 /* Variable number of sub-objects */
203 for (i
= 0; i
< count
; i
++) {
204 if (i
< package
->ret_info3
.count
) {
206 /* These are the required package elements (0, 1, or 2) */
209 acpi_ns_check_object_type(data
, elements
,
214 if (ACPI_FAILURE(status
)) {
218 /* These are the optional package elements */
221 acpi_ns_check_object_type(data
, elements
,
226 if (ACPI_FAILURE(status
)) {
234 case ACPI_PTYPE2_REV_FIXED
:
236 /* First element is the (Integer) revision */
238 status
= acpi_ns_check_object_type(data
, elements
,
239 ACPI_RTYPE_INTEGER
, 0);
240 if (ACPI_FAILURE(status
)) {
247 /* Examine the sub-packages */
250 acpi_ns_check_package_list(data
, package
, elements
, count
);
253 case ACPI_PTYPE2_PKG_COUNT
:
255 /* First element is the (Integer) count of sub-packages to follow */
257 status
= acpi_ns_check_object_type(data
, elements
,
258 ACPI_RTYPE_INTEGER
, 0);
259 if (ACPI_FAILURE(status
)) {
264 * Count cannot be larger than the parent package length, but allow it
265 * to be smaller. The >= accounts for the Integer above.
267 expected_count
= (u32
)(*elements
)->integer
.value
;
268 if (expected_count
>= count
) {
269 goto package_too_small
;
272 count
= expected_count
;
275 /* Examine the sub-packages */
278 acpi_ns_check_package_list(data
, package
, elements
, count
);
282 case ACPI_PTYPE2_FIXED
:
283 case ACPI_PTYPE2_MIN
:
284 case ACPI_PTYPE2_COUNT
:
285 case ACPI_PTYPE2_FIX_VAR
:
288 * These types all return a single Package that consists of a
289 * variable number of sub-Packages.
291 * First, ensure that the first element is a sub-Package. If not,
292 * the BIOS may have incorrectly returned the object as a single
293 * package instead of a Package of Packages (a common error if
294 * there is only one entry). We may be able to repair this by
295 * wrapping the returned Package with a new outer Package.
298 && ((*elements
)->common
.type
!= ACPI_TYPE_PACKAGE
)) {
300 /* Create the new outer package and populate it */
303 acpi_ns_wrap_with_package(data
, return_object
,
305 if (ACPI_FAILURE(status
)) {
309 /* Update locals to point to the new package (of 1 element) */
311 return_object
= *return_object_ptr
;
312 elements
= return_object
->package
.elements
;
316 /* Examine the sub-packages */
319 acpi_ns_check_package_list(data
, package
, elements
, count
);
324 /* Should not get here if predefined info table is correct */
326 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
, data
->node_flags
,
327 "Invalid internal return type in table entry: %X",
328 package
->ret_info
.type
));
330 return (AE_AML_INTERNAL
);
337 /* Error exit for the case with an incorrect package count */
339 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
, data
->node_flags
,
340 "Return Package is too small - found %u elements, expected %u",
341 count
, expected_count
));
343 return (AE_AML_OPERAND_VALUE
);
346 /*******************************************************************************
348 * FUNCTION: acpi_ns_check_package_list
350 * PARAMETERS: data - Pointer to validation data structure
351 * package - Pointer to package-specific info for method
352 * elements - Element list of parent package. All elements
353 * of this list should be of type Package.
354 * count - Count of subpackages
358 * DESCRIPTION: Examine a list of subpackages
360 ******************************************************************************/
363 acpi_ns_check_package_list(struct acpi_predefined_data
*data
,
364 const union acpi_predefined_info
*package
,
365 union acpi_operand_object
**elements
, u32 count
)
367 union acpi_operand_object
*sub_package
;
368 union acpi_operand_object
**sub_elements
;
375 * Validate each sub-Package in the parent Package
377 * NOTE: assumes list of sub-packages contains no NULL elements.
378 * Any NULL elements should have been removed by earlier call
379 * to acpi_ns_remove_null_elements.
381 for (i
= 0; i
< count
; i
++) {
382 sub_package
= *elements
;
383 sub_elements
= sub_package
->package
.elements
;
384 data
->parent_package
= sub_package
;
386 /* Each sub-object must be of type Package */
388 status
= acpi_ns_check_object_type(data
, &sub_package
,
389 ACPI_RTYPE_PACKAGE
, i
);
390 if (ACPI_FAILURE(status
)) {
394 /* Examine the different types of expected sub-packages */
396 data
->parent_package
= sub_package
;
397 switch (package
->ret_info
.type
) {
399 case ACPI_PTYPE2_PKG_COUNT
:
400 case ACPI_PTYPE2_REV_FIXED
:
402 /* Each subpackage has a fixed number of elements */
405 package
->ret_info
.count1
+ package
->ret_info
.count2
;
406 if (sub_package
->package
.count
< expected_count
) {
407 goto package_too_small
;
411 acpi_ns_check_package_elements(data
, sub_elements
,
420 if (ACPI_FAILURE(status
)) {
425 case ACPI_PTYPE2_FIX_VAR
:
427 * Each subpackage has a fixed number of elements and an
431 package
->ret_info
.count1
+ package
->ret_info
.count2
;
432 if (sub_package
->package
.count
< expected_count
) {
433 goto package_too_small
;
437 acpi_ns_check_package_elements(data
, sub_elements
,
444 sub_package
->package
.
448 if (ACPI_FAILURE(status
)) {
453 case ACPI_PTYPE2_FIXED
:
455 /* Each sub-package has a fixed length */
457 expected_count
= package
->ret_info2
.count
;
458 if (sub_package
->package
.count
< expected_count
) {
459 goto package_too_small
;
462 /* Check the type of each sub-package element */
464 for (j
= 0; j
< expected_count
; j
++) {
466 acpi_ns_check_object_type(data
,
472 if (ACPI_FAILURE(status
)) {
478 case ACPI_PTYPE2_MIN
:
480 /* Each sub-package has a variable but minimum length */
482 expected_count
= package
->ret_info
.count1
;
483 if (sub_package
->package
.count
< expected_count
) {
484 goto package_too_small
;
487 /* Check the type of each sub-package element */
490 acpi_ns_check_package_elements(data
, sub_elements
,
493 sub_package
->package
.
495 if (ACPI_FAILURE(status
)) {
500 case ACPI_PTYPE2_COUNT
:
503 * First element is the (Integer) count of elements, including
504 * the count field (the ACPI name is num_elements)
506 status
= acpi_ns_check_object_type(data
, sub_elements
,
509 if (ACPI_FAILURE(status
)) {
514 * Make sure package is large enough for the Count and is
515 * is as large as the minimum size
517 expected_count
= (u32
)(*sub_elements
)->integer
.value
;
518 if (sub_package
->package
.count
< expected_count
) {
519 goto package_too_small
;
521 if (sub_package
->package
.count
<
522 package
->ret_info
.count1
) {
523 expected_count
= package
->ret_info
.count1
;
524 goto package_too_small
;
526 if (expected_count
== 0) {
528 * Either the num_entries element was originally zero or it was
529 * a NULL element and repaired to an Integer of value zero.
530 * In either case, repair it by setting num_entries to be the
531 * actual size of the subpackage.
533 expected_count
= sub_package
->package
.count
;
534 (*sub_elements
)->integer
.value
= expected_count
;
537 /* Check the type of each sub-package element */
540 acpi_ns_check_package_elements(data
,
544 (expected_count
- 1),
546 if (ACPI_FAILURE(status
)) {
551 default: /* Should not get here, type was validated by caller */
553 return (AE_AML_INTERNAL
);
563 /* The sub-package count was smaller than required */
565 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
, data
->node_flags
,
566 "Return Sub-Package[%u] is too small - found %u elements, expected %u",
567 i
, sub_package
->package
.count
, expected_count
));
569 return (AE_AML_OPERAND_VALUE
);
572 /*******************************************************************************
574 * FUNCTION: acpi_ns_check_package_elements
576 * PARAMETERS: data - Pointer to validation data structure
577 * elements - Pointer to the package elements array
578 * type1 - Object type for first group
579 * count1 - Count for first group
580 * type2 - Object type for second group
581 * count2 - Count for second group
582 * start_index - Start of the first group of elements
586 * DESCRIPTION: Check that all elements of a package are of the correct object
587 * type. Supports up to two groups of different object types.
589 ******************************************************************************/
592 acpi_ns_check_package_elements(struct acpi_predefined_data
*data
,
593 union acpi_operand_object
**elements
,
596 u8 type2
, u32 count2
, u32 start_index
)
598 union acpi_operand_object
**this_element
= elements
;
603 * Up to two groups of package elements are supported by the data
604 * structure. All elements in each group must be of the same type.
605 * The second group can have a count of zero.
607 for (i
= 0; i
< count1
; i
++) {
608 status
= acpi_ns_check_object_type(data
, this_element
,
609 type1
, i
+ start_index
);
610 if (ACPI_FAILURE(status
)) {
616 for (i
= 0; i
< count2
; i
++) {
617 status
= acpi_ns_check_object_type(data
, this_element
,
619 (i
+ count1
+ start_index
));
620 if (ACPI_FAILURE(status
)) {