Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / namespace / nsprepkg.c
blob6d74d57452993d2c4cd10f7a6bb297c3f1006975
1 /******************************************************************************
3 * Module Name: nsprepkg - Validation of package objects for predefined names
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, 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.h"
45 #include "accommon.h"
46 #include "acnamesp.h"
47 #include "acpredef.h"
50 #define _COMPONENT ACPI_NAMESPACE
51 ACPI_MODULE_NAME ("nsprepkg")
54 /* Local prototypes */
56 static ACPI_STATUS
57 AcpiNsCheckPackageList (
58 ACPI_EVALUATE_INFO *Info,
59 const ACPI_PREDEFINED_INFO *Package,
60 ACPI_OPERAND_OBJECT **Elements,
61 UINT32 Count);
63 static ACPI_STATUS
64 AcpiNsCheckPackageElements (
65 ACPI_EVALUATE_INFO *Info,
66 ACPI_OPERAND_OBJECT **Elements,
67 UINT8 Type1,
68 UINT32 Count1,
69 UINT8 Type2,
70 UINT32 Count2,
71 UINT32 StartIndex);
74 /*******************************************************************************
76 * FUNCTION: AcpiNsCheckPackage
78 * PARAMETERS: Info - Method execution information block
79 * ReturnObjectPtr - Pointer to the object returned from the
80 * evaluation of a method or object
82 * RETURN: Status
84 * DESCRIPTION: Check a returned package object for the correct count and
85 * correct type of all sub-objects.
87 ******************************************************************************/
89 ACPI_STATUS
90 AcpiNsCheckPackage (
91 ACPI_EVALUATE_INFO *Info,
92 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
94 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
95 const ACPI_PREDEFINED_INFO *Package;
96 ACPI_OPERAND_OBJECT **Elements;
97 ACPI_STATUS Status = AE_OK;
98 UINT32 ExpectedCount;
99 UINT32 Count;
100 UINT32 i;
103 ACPI_FUNCTION_NAME (NsCheckPackage);
106 /* The package info for this name is in the next table entry */
108 Package = Info->Predefined + 1;
110 ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
111 "%s Validating return Package of Type %X, Count %X\n",
112 Info->FullPathname, Package->RetInfo.Type,
113 ReturnObject->Package.Count));
116 * For variable-length Packages, we can safely remove all embedded
117 * and trailing NULL package elements
119 AcpiNsRemoveNullElements (Info, Package->RetInfo.Type, ReturnObject);
121 /* Extract package count and elements array */
123 Elements = ReturnObject->Package.Elements;
124 Count = ReturnObject->Package.Count;
127 * Most packages must have at least one element. The only exception
128 * is the variable-length package (ACPI_PTYPE1_VAR).
130 if (!Count)
132 if (Package->RetInfo.Type == ACPI_PTYPE1_VAR)
134 return (AE_OK);
137 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
138 "Return Package has no elements (empty)"));
140 return (AE_AML_OPERAND_VALUE);
144 * Decode the type of the expected package contents
146 * PTYPE1 packages contain no subpackages
147 * PTYPE2 packages contain sub-packages
149 switch (Package->RetInfo.Type)
151 case ACPI_PTYPE1_FIXED:
153 * The package count is fixed and there are no sub-packages
155 * If package is too small, exit.
156 * If package is larger than expected, issue warning but continue
158 ExpectedCount = Package->RetInfo.Count1 + Package->RetInfo.Count2;
159 if (Count < ExpectedCount)
161 goto PackageTooSmall;
163 else if (Count > ExpectedCount)
165 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
166 "%s: Return Package is larger than needed - "
167 "found %u, expected %u\n",
168 Info->FullPathname, Count, ExpectedCount));
171 /* Validate all elements of the returned package */
173 Status = AcpiNsCheckPackageElements (Info, Elements,
174 Package->RetInfo.ObjectType1, Package->RetInfo.Count1,
175 Package->RetInfo.ObjectType2, Package->RetInfo.Count2, 0);
176 break;
178 case ACPI_PTYPE1_VAR:
180 * The package count is variable, there are no sub-packages, and all
181 * elements must be of the same type
183 for (i = 0; i < Count; i++)
185 Status = AcpiNsCheckObjectType (Info, Elements,
186 Package->RetInfo.ObjectType1, i);
187 if (ACPI_FAILURE (Status))
189 return (Status);
191 Elements++;
193 break;
195 case ACPI_PTYPE1_OPTION:
197 * The package count is variable, there are no sub-packages. There are
198 * a fixed number of required elements, and a variable number of
199 * optional elements.
201 * Check if package is at least as large as the minimum required
203 ExpectedCount = Package->RetInfo3.Count;
204 if (Count < ExpectedCount)
206 goto PackageTooSmall;
209 /* Variable number of sub-objects */
211 for (i = 0; i < Count; i++)
213 if (i < Package->RetInfo3.Count)
215 /* These are the required package elements (0, 1, or 2) */
217 Status = AcpiNsCheckObjectType (Info, Elements,
218 Package->RetInfo3.ObjectType[i], i);
219 if (ACPI_FAILURE (Status))
221 return (Status);
224 else
226 /* These are the optional package elements */
228 Status = AcpiNsCheckObjectType (Info, Elements,
229 Package->RetInfo3.TailObjectType, i);
230 if (ACPI_FAILURE (Status))
232 return (Status);
235 Elements++;
237 break;
239 case ACPI_PTYPE2_REV_FIXED:
241 /* First element is the (Integer) revision */
243 Status = AcpiNsCheckObjectType (Info, Elements,
244 ACPI_RTYPE_INTEGER, 0);
245 if (ACPI_FAILURE (Status))
247 return (Status);
250 Elements++;
251 Count--;
253 /* Examine the sub-packages */
255 Status = AcpiNsCheckPackageList (Info, Package, Elements, Count);
256 break;
258 case ACPI_PTYPE2_PKG_COUNT:
260 /* First element is the (Integer) count of sub-packages to follow */
262 Status = AcpiNsCheckObjectType (Info, Elements,
263 ACPI_RTYPE_INTEGER, 0);
264 if (ACPI_FAILURE (Status))
266 return (Status);
270 * Count cannot be larger than the parent package length, but allow it
271 * to be smaller. The >= accounts for the Integer above.
273 ExpectedCount = (UINT32) (*Elements)->Integer.Value;
274 if (ExpectedCount >= Count)
276 goto PackageTooSmall;
279 Count = ExpectedCount;
280 Elements++;
282 /* Examine the sub-packages */
284 Status = AcpiNsCheckPackageList (Info, Package, Elements, Count);
285 break;
287 case ACPI_PTYPE2:
288 case ACPI_PTYPE2_FIXED:
289 case ACPI_PTYPE2_MIN:
290 case ACPI_PTYPE2_COUNT:
291 case ACPI_PTYPE2_FIX_VAR:
293 * These types all return a single Package that consists of a
294 * variable number of sub-Packages.
296 * First, ensure that the first element is a sub-Package. If not,
297 * the BIOS may have incorrectly returned the object as a single
298 * package instead of a Package of Packages (a common error if
299 * there is only one entry). We may be able to repair this by
300 * wrapping the returned Package with a new outer Package.
302 if (*Elements && ((*Elements)->Common.Type != ACPI_TYPE_PACKAGE))
304 /* Create the new outer package and populate it */
306 Status = AcpiNsWrapWithPackage (Info, ReturnObject, ReturnObjectPtr);
307 if (ACPI_FAILURE (Status))
309 return (Status);
312 /* Update locals to point to the new package (of 1 element) */
314 ReturnObject = *ReturnObjectPtr;
315 Elements = ReturnObject->Package.Elements;
316 Count = 1;
319 /* Examine the sub-packages */
321 Status = AcpiNsCheckPackageList (Info, Package, Elements, Count);
322 break;
324 default:
326 /* Should not get here if predefined info table is correct */
328 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
329 "Invalid internal return type in table entry: %X",
330 Package->RetInfo.Type));
332 return (AE_AML_INTERNAL);
335 return (Status);
338 PackageTooSmall:
340 /* Error exit for the case with an incorrect package count */
342 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
343 "Return Package is too small - found %u elements, expected %u",
344 Count, ExpectedCount));
346 return (AE_AML_OPERAND_VALUE);
350 /*******************************************************************************
352 * FUNCTION: AcpiNsCheckPackageList
354 * PARAMETERS: Info - Method execution information block
355 * Package - Pointer to package-specific info for method
356 * Elements - Element list of parent package. All elements
357 * of this list should be of type Package.
358 * Count - Count of subpackages
360 * RETURN: Status
362 * DESCRIPTION: Examine a list of subpackages
364 ******************************************************************************/
366 static ACPI_STATUS
367 AcpiNsCheckPackageList (
368 ACPI_EVALUATE_INFO *Info,
369 const ACPI_PREDEFINED_INFO *Package,
370 ACPI_OPERAND_OBJECT **Elements,
371 UINT32 Count)
373 ACPI_OPERAND_OBJECT *SubPackage;
374 ACPI_OPERAND_OBJECT **SubElements;
375 ACPI_STATUS Status;
376 UINT32 ExpectedCount;
377 UINT32 i;
378 UINT32 j;
382 * Validate each sub-Package in the parent Package
384 * NOTE: assumes list of sub-packages contains no NULL elements.
385 * Any NULL elements should have been removed by earlier call
386 * to AcpiNsRemoveNullElements.
388 for (i = 0; i < Count; i++)
390 SubPackage = *Elements;
391 SubElements = SubPackage->Package.Elements;
392 Info->ParentPackage = SubPackage;
394 /* Each sub-object must be of type Package */
396 Status = AcpiNsCheckObjectType (Info, &SubPackage,
397 ACPI_RTYPE_PACKAGE, i);
398 if (ACPI_FAILURE (Status))
400 return (Status);
403 /* Examine the different types of expected sub-packages */
405 Info->ParentPackage = SubPackage;
406 switch (Package->RetInfo.Type)
408 case ACPI_PTYPE2:
409 case ACPI_PTYPE2_PKG_COUNT:
410 case ACPI_PTYPE2_REV_FIXED:
412 /* Each subpackage has a fixed number of elements */
414 ExpectedCount = Package->RetInfo.Count1 + Package->RetInfo.Count2;
415 if (SubPackage->Package.Count < ExpectedCount)
417 goto PackageTooSmall;
420 Status = AcpiNsCheckPackageElements (Info, SubElements,
421 Package->RetInfo.ObjectType1,
422 Package->RetInfo.Count1,
423 Package->RetInfo.ObjectType2,
424 Package->RetInfo.Count2, 0);
425 if (ACPI_FAILURE (Status))
427 return (Status);
429 break;
431 case ACPI_PTYPE2_FIX_VAR:
433 * Each subpackage has a fixed number of elements and an
434 * optional element
436 ExpectedCount = Package->RetInfo.Count1 + Package->RetInfo.Count2;
437 if (SubPackage->Package.Count < ExpectedCount)
439 goto PackageTooSmall;
442 Status = AcpiNsCheckPackageElements (Info, SubElements,
443 Package->RetInfo.ObjectType1,
444 Package->RetInfo.Count1,
445 Package->RetInfo.ObjectType2,
446 SubPackage->Package.Count - Package->RetInfo.Count1, 0);
447 if (ACPI_FAILURE (Status))
449 return (Status);
451 break;
453 case ACPI_PTYPE2_FIXED:
455 /* Each sub-package has a fixed length */
457 ExpectedCount = Package->RetInfo2.Count;
458 if (SubPackage->Package.Count < ExpectedCount)
460 goto PackageTooSmall;
463 /* Check the type of each sub-package element */
465 for (j = 0; j < ExpectedCount; j++)
467 Status = AcpiNsCheckObjectType (Info, &SubElements[j],
468 Package->RetInfo2.ObjectType[j], j);
469 if (ACPI_FAILURE (Status))
471 return (Status);
474 break;
476 case ACPI_PTYPE2_MIN:
478 /* Each sub-package has a variable but minimum length */
480 ExpectedCount = Package->RetInfo.Count1;
481 if (SubPackage->Package.Count < ExpectedCount)
483 goto PackageTooSmall;
486 /* Check the type of each sub-package element */
488 Status = AcpiNsCheckPackageElements (Info, SubElements,
489 Package->RetInfo.ObjectType1,
490 SubPackage->Package.Count, 0, 0, 0);
491 if (ACPI_FAILURE (Status))
493 return (Status);
495 break;
497 case ACPI_PTYPE2_COUNT:
499 * First element is the (Integer) count of elements, including
500 * the count field (the ACPI name is NumElements)
502 Status = AcpiNsCheckObjectType (Info, SubElements,
503 ACPI_RTYPE_INTEGER, 0);
504 if (ACPI_FAILURE (Status))
506 return (Status);
510 * Make sure package is large enough for the Count and is
511 * is as large as the minimum size
513 ExpectedCount = (UINT32) (*SubElements)->Integer.Value;
514 if (SubPackage->Package.Count < ExpectedCount)
516 goto PackageTooSmall;
518 if (SubPackage->Package.Count < Package->RetInfo.Count1)
520 ExpectedCount = Package->RetInfo.Count1;
521 goto PackageTooSmall;
523 if (ExpectedCount == 0)
526 * Either the NumEntries element was originally zero or it was
527 * a NULL element and repaired to an Integer of value zero.
528 * In either case, repair it by setting NumEntries to be the
529 * actual size of the subpackage.
531 ExpectedCount = SubPackage->Package.Count;
532 (*SubElements)->Integer.Value = ExpectedCount;
535 /* Check the type of each sub-package element */
537 Status = AcpiNsCheckPackageElements (Info, (SubElements + 1),
538 Package->RetInfo.ObjectType1,
539 (ExpectedCount - 1), 0, 0, 1);
540 if (ACPI_FAILURE (Status))
542 return (Status);
544 break;
546 default: /* Should not get here, type was validated by caller */
548 return (AE_AML_INTERNAL);
551 Elements++;
554 return (AE_OK);
557 PackageTooSmall:
559 /* The sub-package count was smaller than required */
561 ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, Info->NodeFlags,
562 "Return Sub-Package[%u] is too small - found %u elements, expected %u",
563 i, SubPackage->Package.Count, ExpectedCount));
565 return (AE_AML_OPERAND_VALUE);
569 /*******************************************************************************
571 * FUNCTION: AcpiNsCheckPackageElements
573 * PARAMETERS: Info - Method execution information block
574 * Elements - Pointer to the package elements array
575 * Type1 - Object type for first group
576 * Count1 - Count for first group
577 * Type2 - Object type for second group
578 * Count2 - Count for second group
579 * StartIndex - Start of the first group of elements
581 * RETURN: Status
583 * DESCRIPTION: Check that all elements of a package are of the correct object
584 * type. Supports up to two groups of different object types.
586 ******************************************************************************/
588 static ACPI_STATUS
589 AcpiNsCheckPackageElements (
590 ACPI_EVALUATE_INFO *Info,
591 ACPI_OPERAND_OBJECT **Elements,
592 UINT8 Type1,
593 UINT32 Count1,
594 UINT8 Type2,
595 UINT32 Count2,
596 UINT32 StartIndex)
598 ACPI_OPERAND_OBJECT **ThisElement = Elements;
599 ACPI_STATUS Status;
600 UINT32 i;
604 * Up to two groups of package elements are supported by the data
605 * structure. All elements in each group must be of the same type.
606 * The second group can have a count of zero.
608 for (i = 0; i < Count1; i++)
610 Status = AcpiNsCheckObjectType (Info, ThisElement,
611 Type1, i + StartIndex);
612 if (ACPI_FAILURE (Status))
614 return (Status);
616 ThisElement++;
619 for (i = 0; i < Count2; i++)
621 Status = AcpiNsCheckObjectType (Info, ThisElement,
622 Type2, (i + Count1 + StartIndex));
623 if (ACPI_FAILURE (Status))
625 return (Status);
627 ThisElement++;
630 return (AE_OK);