1 /******************************************************************************
3 * Module Name: nsrepair2 - Repair for objects returned by specific
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2017, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acnamesp.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME ("nsrepair2")
54 * Information structure and handler for ACPI predefined names that can
55 * be repaired on a per-name basis.
58 ACPI_STATUS (*ACPI_REPAIR_FUNCTION) (
59 ACPI_EVALUATE_INFO *Info,
60 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
62 typedef struct acpi_repair_info
64 char Name[ACPI_NAME_SIZE];
65 ACPI_REPAIR_FUNCTION RepairFunction;
70 /* Local prototypes */
72 static const ACPI_REPAIR_INFO *
73 AcpiNsMatchComplexRepair (
74 ACPI_NAMESPACE_NODE *Node);
78 ACPI_EVALUATE_INFO *Info,
79 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
83 ACPI_EVALUATE_INFO *Info,
84 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
88 ACPI_EVALUATE_INFO *Info,
89 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
93 ACPI_EVALUATE_INFO *Info,
94 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
98 ACPI_EVALUATE_INFO *Info,
99 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
103 ACPI_EVALUATE_INFO *Info,
104 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
108 ACPI_EVALUATE_INFO *Info,
109 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
113 ACPI_EVALUATE_INFO *Info,
114 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
117 AcpiNsCheckSortedList (
118 ACPI_EVALUATE_INFO *Info,
119 ACPI_OPERAND_OBJECT *ReturnObject,
121 UINT32 ExpectedCount,
126 /* Values for SortDirection above */
128 #define ACPI_SORT_ASCENDING 0
129 #define ACPI_SORT_DESCENDING 1
132 AcpiNsRemoveElement (
133 ACPI_OPERAND_OBJECT *ObjDesc,
138 ACPI_OPERAND_OBJECT **Elements,
141 UINT8 SortDirection);
145 * This table contains the names of the predefined methods for which we can
146 * perform more complex repairs.
150 * _ALR: Sort the list ascending by AmbientIlluminance
151 * _CID: Strings: uppercase all, remove any leading asterisk
152 * _CST: Sort the list ascending by C state type
153 * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
154 * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
155 * _HID: Strings: uppercase all, remove any leading asterisk
156 * _PRT: Fix reversed SourceName and SourceIndex
157 * _PSS: Sort the list descending by Power
158 * _TSS: Sort the list descending by Power
160 * Names that must be packages, but cannot be sorted:
162 * _BCL: Values are tied to the Package index where they appear, and cannot
163 * be moved or sorted. These index values are used for _BQC and _BCM.
164 * However, we can fix the case where a buffer is returned, by converting
165 * it to a Package of integers.
167 static const ACPI_REPAIR_INFO AcpiNsRepairableNames[] =
169 {"_ALR", AcpiNsRepair_ALR},
170 {"_CID", AcpiNsRepair_CID},
171 {"_CST", AcpiNsRepair_CST},
172 {"_FDE", AcpiNsRepair_FDE},
173 {"_GTM", AcpiNsRepair_FDE}, /* _GTM has same repair as _FDE */
174 {"_HID", AcpiNsRepair_HID},
175 {"_PRT", AcpiNsRepair_PRT},
176 {"_PSS", AcpiNsRepair_PSS},
177 {"_TSS", AcpiNsRepair_TSS},
178 {{0,0,0,0}, NULL} /* Table terminator */
182 #define ACPI_FDE_FIELD_COUNT 5
183 #define ACPI_FDE_BYTE_BUFFER_SIZE 5
184 #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (UINT32))
187 /******************************************************************************
189 * FUNCTION: AcpiNsComplexRepairs
191 * PARAMETERS: Info - Method execution information block
192 * Node - Namespace node for the method/object
193 * ValidateStatus - Original status of earlier validation
194 * ReturnObjectPtr - Pointer to the object returned from the
195 * evaluation of a method or object
197 * RETURN: Status. AE_OK if repair was successful. If name is not
198 * matched, ValidateStatus is returned.
200 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
203 *****************************************************************************/
206 AcpiNsComplexRepairs (
207 ACPI_EVALUATE_INFO *Info,
208 ACPI_NAMESPACE_NODE *Node,
209 ACPI_STATUS ValidateStatus,
210 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
212 const ACPI_REPAIR_INFO *Predefined;
216 /* Check if this name is in the list of repairable names */
218 Predefined = AcpiNsMatchComplexRepair (Node);
221 return (ValidateStatus);
224 Status = Predefined->RepairFunction (Info, ReturnObjectPtr);
229 /******************************************************************************
231 * FUNCTION: AcpiNsMatchComplexRepair
233 * PARAMETERS: Node - Namespace node for the method/object
235 * RETURN: Pointer to entry in repair table. NULL indicates not found.
237 * DESCRIPTION: Check an object name against the repairable object list.
239 *****************************************************************************/
241 static const ACPI_REPAIR_INFO *
242 AcpiNsMatchComplexRepair (
243 ACPI_NAMESPACE_NODE *Node)
245 const ACPI_REPAIR_INFO *ThisName;
248 /* Search info table for a repairable predefined method/object name */
250 ThisName = AcpiNsRepairableNames;
251 while (ThisName->RepairFunction)
253 if (ACPI_COMPARE_NAME (Node->Name.Ascii, ThisName->Name))
261 return (NULL); /* Not found */
265 /******************************************************************************
267 * FUNCTION: AcpiNsRepair_ALR
269 * PARAMETERS: Info - Method execution information block
270 * ReturnObjectPtr - Pointer to the object returned from the
271 * evaluation of a method or object
273 * RETURN: Status. AE_OK if object is OK or was repaired successfully
275 * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
276 * ascending by the ambient illuminance values.
278 *****************************************************************************/
282 ACPI_EVALUATE_INFO *Info,
283 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
285 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
289 Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 2, 1,
290 ACPI_SORT_ASCENDING, "AmbientIlluminance");
296 /******************************************************************************
298 * FUNCTION: AcpiNsRepair_FDE
300 * PARAMETERS: Info - Method execution information block
301 * ReturnObjectPtr - Pointer to the object returned from the
302 * evaluation of a method or object
304 * RETURN: Status. AE_OK if object is OK or was repaired successfully
306 * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
307 * value is a Buffer of 5 DWORDs. This function repairs a common
308 * problem where the return value is a Buffer of BYTEs, not
311 *****************************************************************************/
315 ACPI_EVALUATE_INFO *Info,
316 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
318 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
319 ACPI_OPERAND_OBJECT *BufferObject;
325 ACPI_FUNCTION_NAME (NsRepair_FDE);
328 switch (ReturnObject->Common.Type)
330 case ACPI_TYPE_BUFFER:
332 /* This is the expected type. Length should be (at least) 5 DWORDs */
334 if (ReturnObject->Buffer.Length >= ACPI_FDE_DWORD_BUFFER_SIZE)
339 /* We can only repair if we have exactly 5 BYTEs */
341 if (ReturnObject->Buffer.Length != ACPI_FDE_BYTE_BUFFER_SIZE)
343 ACPI_WARN_PREDEFINED ((AE_INFO,
344 Info->FullPathname, Info->NodeFlags,
345 "Incorrect return buffer length %u, expected %u",
346 ReturnObject->Buffer.Length, ACPI_FDE_DWORD_BUFFER_SIZE));
348 return (AE_AML_OPERAND_TYPE);
351 /* Create the new (larger) buffer object */
353 BufferObject = AcpiUtCreateBufferObject (
354 ACPI_FDE_DWORD_BUFFER_SIZE);
357 return (AE_NO_MEMORY);
360 /* Expand each byte to a DWORD */
362 ByteBuffer = ReturnObject->Buffer.Pointer;
363 DwordBuffer = ACPI_CAST_PTR (UINT32,
364 BufferObject->Buffer.Pointer);
366 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++)
368 *DwordBuffer = (UINT32) *ByteBuffer;
373 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
374 "%s Expanded Byte Buffer to expected DWord Buffer\n",
375 Info->FullPathname));
380 return (AE_AML_OPERAND_TYPE);
383 /* Delete the original return object, return the new buffer object */
385 AcpiUtRemoveReference (ReturnObject);
386 *ReturnObjectPtr = BufferObject;
388 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
393 /******************************************************************************
395 * FUNCTION: AcpiNsRepair_CID
397 * PARAMETERS: Info - Method execution information block
398 * ReturnObjectPtr - Pointer to the object returned from the
399 * evaluation of a method or object
401 * RETURN: Status. AE_OK if object is OK or was repaired successfully
403 * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
404 * letters are uppercase and that there is no leading asterisk.
405 * If a Package, ensure same for all string elements.
407 *****************************************************************************/
411 ACPI_EVALUATE_INFO *Info,
412 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
415 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
416 ACPI_OPERAND_OBJECT **ElementPtr;
417 ACPI_OPERAND_OBJECT *OriginalElement;
418 UINT16 OriginalRefCount;
422 /* Check for _CID as a simple string */
424 if (ReturnObject->Common.Type == ACPI_TYPE_STRING)
426 Status = AcpiNsRepair_HID (Info, ReturnObjectPtr);
430 /* Exit if not a Package */
432 if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
437 /* Examine each element of the _CID package */
439 ElementPtr = ReturnObject->Package.Elements;
440 for (i = 0; i < ReturnObject->Package.Count; i++)
442 OriginalElement = *ElementPtr;
443 OriginalRefCount = OriginalElement->Common.ReferenceCount;
445 Status = AcpiNsRepair_HID (Info, ElementPtr);
446 if (ACPI_FAILURE (Status))
451 /* Take care with reference counts */
453 if (OriginalElement != *ElementPtr)
455 /* Element was replaced */
457 (*ElementPtr)->Common.ReferenceCount =
460 AcpiUtRemoveReference (OriginalElement);
470 /******************************************************************************
472 * FUNCTION: AcpiNsRepair_CST
474 * PARAMETERS: Info - Method execution information block
475 * ReturnObjectPtr - Pointer to the object returned from the
476 * evaluation of a method or object
478 * RETURN: Status. AE_OK if object is OK or was repaired successfully
480 * DESCRIPTION: Repair for the _CST object:
481 * 1. Sort the list ascending by C state type
482 * 2. Ensure type cannot be zero
483 * 3. A subpackage count of zero means _CST is meaningless
484 * 4. Count must match the number of C state subpackages
486 *****************************************************************************/
490 ACPI_EVALUATE_INFO *Info,
491 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
493 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
494 ACPI_OPERAND_OBJECT **OuterElements;
495 UINT32 OuterElementCount;
496 ACPI_OPERAND_OBJECT *ObjDesc;
502 ACPI_FUNCTION_NAME (NsRepair_CST);
506 * Check if the C-state type values are proportional.
508 OuterElementCount = ReturnObject->Package.Count - 1;
510 while (i < OuterElementCount)
512 OuterElements = &ReturnObject->Package.Elements[i + 1];
515 if ((*OuterElements)->Package.Count == 0)
517 ACPI_WARN_PREDEFINED ((AE_INFO,
518 Info->FullPathname, Info->NodeFlags,
519 "SubPackage[%u] - removing entry due to zero count", i));
524 ObjDesc = (*OuterElements)->Package.Elements[1]; /* Index1 = Type */
525 if ((UINT32) ObjDesc->Integer.Value == 0)
527 ACPI_WARN_PREDEFINED ((AE_INFO,
528 Info->FullPathname, Info->NodeFlags,
529 "SubPackage[%u] - removing entry due to invalid Type(0)", i));
536 AcpiNsRemoveElement (ReturnObject, i + 1);
545 /* Update top-level package count, Type "Integer" checked elsewhere */
547 ObjDesc = ReturnObject->Package.Elements[0];
548 ObjDesc->Integer.Value = OuterElementCount;
551 * Entries (subpackages) in the _CST Package must be sorted by the
552 * C-state type, in ascending order.
554 Status = AcpiNsCheckSortedList (Info, ReturnObject, 1, 4, 1,
555 ACPI_SORT_ASCENDING, "C-State Type");
556 if (ACPI_FAILURE (Status))
565 /******************************************************************************
567 * FUNCTION: AcpiNsRepair_HID
569 * PARAMETERS: Info - Method execution information block
570 * ReturnObjectPtr - Pointer to the object returned from the
571 * evaluation of a method or object
573 * RETURN: Status. AE_OK if object is OK or was repaired successfully
575 * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
576 * letters are uppercase and that there is no leading asterisk.
578 *****************************************************************************/
582 ACPI_EVALUATE_INFO *Info,
583 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
585 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
586 ACPI_OPERAND_OBJECT *NewString;
591 ACPI_FUNCTION_NAME (NsRepair_HID);
594 /* We only care about string _HID objects (not integers) */
596 if (ReturnObject->Common.Type != ACPI_TYPE_STRING)
601 if (ReturnObject->String.Length == 0)
603 ACPI_WARN_PREDEFINED ((AE_INFO,
604 Info->FullPathname, Info->NodeFlags,
605 "Invalid zero-length _HID or _CID string"));
607 /* Return AE_OK anyway, let driver handle it */
609 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
613 /* It is simplest to always create a new string object */
615 NewString = AcpiUtCreateStringObject (ReturnObject->String.Length);
618 return (AE_NO_MEMORY);
622 * Remove a leading asterisk if present. For some unknown reason, there
623 * are many machines in the field that contains IDs like this.
625 * Examples: "*PNP0C03", "*ACPI0003"
627 Source = ReturnObject->String.Pointer;
631 NewString->String.Length--;
633 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
634 "%s: Removed invalid leading asterisk\n", Info->FullPathname));
638 * Copy and uppercase the string. From the ACPI 5.0 specification:
640 * A valid PNP ID must be of the form "AAA####" where A is an uppercase
641 * letter and # is a hex digit. A valid ACPI ID must be of the form
642 * "NNNN####" where N is an uppercase letter or decimal digit, and
645 for (Dest = NewString->String.Pointer; *Source; Dest++, Source++)
647 *Dest = (char) toupper ((int) *Source);
650 AcpiUtRemoveReference (ReturnObject);
651 *ReturnObjectPtr = NewString;
656 /******************************************************************************
658 * FUNCTION: AcpiNsRepair_PRT
660 * PARAMETERS: Info - Method execution information block
661 * ReturnObjectPtr - Pointer to the object returned from the
662 * evaluation of a method or object
664 * RETURN: Status. AE_OK if object is OK or was repaired successfully
666 * DESCRIPTION: Repair for the _PRT object. If necessary, fix reversed
667 * SourceName and SourceIndex field, a common BIOS bug.
669 *****************************************************************************/
673 ACPI_EVALUATE_INFO *Info,
674 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
676 ACPI_OPERAND_OBJECT *PackageObject = *ReturnObjectPtr;
677 ACPI_OPERAND_OBJECT **TopObjectList;
678 ACPI_OPERAND_OBJECT **SubObjectList;
679 ACPI_OPERAND_OBJECT *ObjDesc;
680 ACPI_OPERAND_OBJECT *SubPackage;
685 /* Each element in the _PRT package is a subpackage */
687 TopObjectList = PackageObject->Package.Elements;
688 ElementCount = PackageObject->Package.Count;
690 /* Examine each subpackage */
692 for (Index = 0; Index < ElementCount; Index++, TopObjectList++)
694 SubPackage = *TopObjectList;
695 SubObjectList = SubPackage->Package.Elements;
697 /* Check for minimum required element count */
699 if (SubPackage->Package.Count < 4)
705 * If the BIOS has erroneously reversed the _PRT SourceName (index 2)
706 * and the SourceIndex (index 3), fix it. _PRT is important enough to
707 * workaround this BIOS error. This also provides compatibility with
708 * other ACPI implementations.
710 ObjDesc = SubObjectList[3];
711 if (!ObjDesc || (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
713 SubObjectList[3] = SubObjectList[2];
714 SubObjectList[2] = ObjDesc;
715 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
717 ACPI_WARN_PREDEFINED ((AE_INFO,
718 Info->FullPathname, Info->NodeFlags,
719 "PRT[%X]: Fixed reversed SourceName and SourceIndex",
728 /******************************************************************************
730 * FUNCTION: AcpiNsRepair_PSS
732 * PARAMETERS: Info - Method execution information block
733 * ReturnObjectPtr - Pointer to the object returned from the
734 * evaluation of a method or object
736 * RETURN: Status. AE_OK if object is OK or was repaired successfully
738 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
739 * by the CPU frequencies. Check that the power dissipation values
740 * are all proportional to CPU frequency (i.e., sorting by
741 * frequency should be the same as sorting by power.)
743 *****************************************************************************/
747 ACPI_EVALUATE_INFO *Info,
748 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
750 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
751 ACPI_OPERAND_OBJECT **OuterElements;
752 UINT32 OuterElementCount;
753 ACPI_OPERAND_OBJECT **Elements;
754 ACPI_OPERAND_OBJECT *ObjDesc;
755 UINT32 PreviousValue;
761 * Entries (subpackages) in the _PSS Package must be sorted by power
762 * dissipation, in descending order. If it appears that the list is
763 * incorrectly sorted, sort it. We sort by CpuFrequency, since this
764 * should be proportional to the power.
766 Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 6, 0,
767 ACPI_SORT_DESCENDING, "CpuFrequency");
768 if (ACPI_FAILURE (Status))
774 * We now know the list is correctly sorted by CPU frequency. Check if
775 * the power dissipation values are proportional.
777 PreviousValue = ACPI_UINT32_MAX;
778 OuterElements = ReturnObject->Package.Elements;
779 OuterElementCount = ReturnObject->Package.Count;
781 for (i = 0; i < OuterElementCount; i++)
783 Elements = (*OuterElements)->Package.Elements;
784 ObjDesc = Elements[1]; /* Index1 = PowerDissipation */
786 if ((UINT32) ObjDesc->Integer.Value > PreviousValue)
788 ACPI_WARN_PREDEFINED ((AE_INFO,
789 Info->FullPathname, Info->NodeFlags,
790 "SubPackage[%u,%u] - suspicious power dissipation values",
794 PreviousValue = (UINT32) ObjDesc->Integer.Value;
802 /******************************************************************************
804 * FUNCTION: AcpiNsRepair_TSS
806 * PARAMETERS: Info - Method execution information block
807 * ReturnObjectPtr - Pointer to the object returned from the
808 * evaluation of a method or object
810 * RETURN: Status. AE_OK if object is OK or was repaired successfully
812 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
813 * descending by the power dissipation values.
815 *****************************************************************************/
819 ACPI_EVALUATE_INFO *Info,
820 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
822 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
824 ACPI_NAMESPACE_NODE *Node;
828 * We can only sort the _TSS return package if there is no _PSS in the
829 * same scope. This is because if _PSS is present, the ACPI specification
830 * dictates that the _TSS Power Dissipation field is to be ignored, and
831 * therefore some BIOSs leave garbage values in the _TSS Power field(s).
832 * In this case, it is best to just return the _TSS package as-is.
835 Status = AcpiNsGetNode (Info->Node, "^_PSS",
836 ACPI_NS_NO_UPSEARCH, &Node);
837 if (ACPI_SUCCESS (Status))
842 Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 5, 1,
843 ACPI_SORT_DESCENDING, "PowerDissipation");
849 /******************************************************************************
851 * FUNCTION: AcpiNsCheckSortedList
853 * PARAMETERS: Info - Method execution information block
854 * ReturnObject - Pointer to the top-level returned object
855 * StartIndex - Index of the first subpackage
856 * ExpectedCount - Minimum length of each subpackage
857 * SortIndex - Subpackage entry to sort on
858 * SortDirection - Ascending or descending
859 * SortKeyName - Name of the SortIndex field
861 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
862 * has been repaired by sorting the list.
864 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
865 * SortIndex. If not, then sort the list.
867 *****************************************************************************/
870 AcpiNsCheckSortedList (
871 ACPI_EVALUATE_INFO *Info,
872 ACPI_OPERAND_OBJECT *ReturnObject,
874 UINT32 ExpectedCount,
879 UINT32 OuterElementCount;
880 ACPI_OPERAND_OBJECT **OuterElements;
881 ACPI_OPERAND_OBJECT **Elements;
882 ACPI_OPERAND_OBJECT *ObjDesc;
884 UINT32 PreviousValue;
887 ACPI_FUNCTION_NAME (NsCheckSortedList);
890 /* The top-level object must be a package */
892 if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
894 return (AE_AML_OPERAND_TYPE);
898 * NOTE: assumes list of subpackages contains no NULL elements.
899 * Any NULL elements should have been removed by earlier call
900 * to AcpiNsRemoveNullElements.
902 OuterElementCount = ReturnObject->Package.Count;
903 if (!OuterElementCount || StartIndex >= OuterElementCount)
905 return (AE_AML_PACKAGE_LIMIT);
908 OuterElements = &ReturnObject->Package.Elements[StartIndex];
909 OuterElementCount -= StartIndex;
912 if (SortDirection == ACPI_SORT_DESCENDING)
914 PreviousValue = ACPI_UINT32_MAX;
917 /* Examine each subpackage */
919 for (i = 0; i < OuterElementCount; i++)
921 /* Each element of the top-level package must also be a package */
923 if ((*OuterElements)->Common.Type != ACPI_TYPE_PACKAGE)
925 return (AE_AML_OPERAND_TYPE);
928 /* Each subpackage must have the minimum length */
930 if ((*OuterElements)->Package.Count < ExpectedCount)
932 return (AE_AML_PACKAGE_LIMIT);
935 Elements = (*OuterElements)->Package.Elements;
936 ObjDesc = Elements[SortIndex];
938 if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
940 return (AE_AML_OPERAND_TYPE);
944 * The list must be sorted in the specified order. If we detect a
945 * discrepancy, sort the entire list.
947 if (((SortDirection == ACPI_SORT_ASCENDING) &&
948 (ObjDesc->Integer.Value < PreviousValue)) ||
949 ((SortDirection == ACPI_SORT_DESCENDING) &&
950 (ObjDesc->Integer.Value > PreviousValue)))
952 AcpiNsSortList (&ReturnObject->Package.Elements[StartIndex],
953 OuterElementCount, SortIndex, SortDirection);
955 Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
957 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
958 "%s: Repaired unsorted list - now sorted by %s\n",
959 Info->FullPathname, SortKeyName));
963 PreviousValue = (UINT32) ObjDesc->Integer.Value;
971 /******************************************************************************
973 * FUNCTION: AcpiNsSortList
975 * PARAMETERS: Elements - Package object element list
976 * Count - Element count for above
977 * Index - Sort by which package element
978 * SortDirection - Ascending or Descending sort
982 * DESCRIPTION: Sort the objects that are in a package element list.
984 * NOTE: Assumes that all NULL elements have been removed from the package,
985 * and that all elements have been verified to be of type Integer.
987 *****************************************************************************/
991 ACPI_OPERAND_OBJECT **Elements,
996 ACPI_OPERAND_OBJECT *ObjDesc1;
997 ACPI_OPERAND_OBJECT *ObjDesc2;
998 ACPI_OPERAND_OBJECT *TempObj;
1003 /* Simple bubble sort */
1005 for (i = 1; i < Count; i++)
1007 for (j = (Count - 1); j >= i; j--)
1009 ObjDesc1 = Elements[j-1]->Package.Elements[Index];
1010 ObjDesc2 = Elements[j]->Package.Elements[Index];
1012 if (((SortDirection == ACPI_SORT_ASCENDING) &&
1013 (ObjDesc1->Integer.Value > ObjDesc2->Integer.Value)) ||
1015 ((SortDirection == ACPI_SORT_DESCENDING) &&
1016 (ObjDesc1->Integer.Value < ObjDesc2->Integer.Value)))
1018 TempObj = Elements[j-1];
1019 Elements[j-1] = Elements[j];
1020 Elements[j] = TempObj;
1027 /******************************************************************************
1029 * FUNCTION: AcpiNsRemoveElement
1031 * PARAMETERS: ObjDesc - Package object element list
1032 * Index - Index of element to remove
1036 * DESCRIPTION: Remove the requested element of a package and delete it.
1038 *****************************************************************************/
1041 AcpiNsRemoveElement (
1042 ACPI_OPERAND_OBJECT *ObjDesc,
1045 ACPI_OPERAND_OBJECT **Source;
1046 ACPI_OPERAND_OBJECT **Dest;
1052 ACPI_FUNCTION_NAME (NsRemoveElement);
1055 Count = ObjDesc->Package.Count;
1056 NewCount = Count - 1;
1058 Source = ObjDesc->Package.Elements;
1061 /* Examine all elements of the package object, remove matched index */
1063 for (i = 0; i < Count; i++)
1067 AcpiUtRemoveReference (*Source); /* Remove one ref for being in pkg */
1068 AcpiUtRemoveReference (*Source);
1079 /* NULL terminate list and update the package count */
1082 ObjDesc->Package.Count = NewCount;