Skip to contentSkip to conversation

Repair predefined Package -> Buffer

PATCH New Jung-uk Kim 1 participant 1 message Sep 4, 2009 ACPI component architecture (acpica)

lore
Share & tools
lore

Jung-uk Kim

author
#0 lore raw

PATCH 1/1 Repair predefined Package -> Buffer

One of the most common mistake of _FDE is returning Package instead of Buffer of five 32-bit integers. The attached patch repairs the case. Do you think it is a reasonable fix? Basically, I want to get rid of a quirk handling in FreeBSD. :-)

Thanks!

Jung-uk Kim

nsrepair.c renamed from nsrepair.c.orig +81 -26
@@ -152,7 +152,9 @@
 {
     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
     ACPI_OPERAND_OBJECT     *NewObject;
+    UINT8                   *Buffer;
     ACPI_SIZE               Length;
+    UINT32                  Count, Value;
 
 
     switch (ReturnObject->Common.Type)
@@ -194,44 +196,97 @@
         ACPI_MEMCPY (NewObject->String.Pointer,
             ReturnObject->Buffer.Pointer, Length);
 
-        /*
-         * If the original object is a package element, we need to:
-         * 1. Set the reference count of the new object to match the
-         *    reference count of the old object.
-         * 2. Decrement the reference count of the original object.
-         */
-        if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
+        break;
+
+    case ACPI_TYPE_PACKAGE:
+
+        /* Does the method/object legally return a buffer? */
+
+        if (!(ExpectedBtypes & ACPI_RTYPE_BUFFER))
         {
-            NewObject->Common.ReferenceCount =
-                ReturnObject->Common.ReferenceCount;
+            return (AE_AML_OPERAND_TYPE);
+        }
 
-            if (ReturnObject->Common.ReferenceCount > 1)
-            {
-                ReturnObject->Common.ReferenceCount--;
-            }
+        /* Does the object actually contain integers? */
 
-            ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
-                "Converted Buffer to expected String at index %u",
-                PackageIndex));
+        if (ReturnObject->Package.Count == 0 ||
+            ReturnObject->Package.Elements[0]->Common.Type != ACPI_TYPE_INTEGER)
+        {
+            return (AE_AML_OPERAND_TYPE);
         }
-        else
+
+        /*
+         * Have a Package, expected a Buffer, convert. The best example of
+         * this is the _FDE method, where the buffer of 32-bit integers
+         * is often (incorrectly) returned as package object of integers.
+         */
+        Length = ReturnObject->Package.Count * sizeof (UINT32);
+
+        /* Allocate a new buffer object */
+
+        NewObject = AcpiUtCreateBufferObject (Length);
+        if (!NewObject)
         {
-            ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
-                "Converted Buffer to expected String"));
+            return (AE_NO_MEMORY);
         }
 
-        /* Delete old object, install the new return object */
+        /*
+         * Copy each raw element to buffer of 32-bit integers.
+         */
+        Count = 0;
+        Buffer = NewObject->Buffer.Pointer;
+        while (Count < ReturnObject->Package.Count)
+        {
+            Value = ReturnObject->Package.Elements[Count]->Integer.Value;
+            ACPI_MEMCPY (Buffer, ACPI_CAST_PTR (UINT8, &Value),
+                sizeof (Value));
+            Buffer += sizeof (Value);
+            Count++;
+        }
 
-        AcpiUtRemoveReference (ReturnObject);
-        *ReturnObjectPtr = NewObject;
-        Data->Flags |= ACPI_OBJECT_REPAIRED;
-        return (AE_OK);
+        break;
 
     default:
-        break;
+
+        return (AE_AML_OPERAND_TYPE);
     }
 
-    return (AE_AML_OPERAND_TYPE);
+    /*
+     * If the original object is a package element, we need to:
+     * 1. Set the reference count of the new object to match the
+     *    reference count of the old object.
+     * 2. Decrement the reference count of the original object.
+     */
+    if (PackageIndex != ACPI_NOT_PACKAGE_ELEMENT)
+    {
+        NewObject->Common.ReferenceCount =
+            ReturnObject->Common.ReferenceCount;
+
+        if (ReturnObject->Common.ReferenceCount > 1)
+        {
+            ReturnObject->Common.ReferenceCount--;
+        }
+
+        ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
+            "Converted %s to expected %s at index %u",
+            AcpiUtGetTypeName (ReturnObject->Common.Type),
+            AcpiUtGetTypeName (NewObject->Common.Type), PackageIndex));
+    }
+    else
+    {
+        ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
+            "Converted %s to expected %s",
+            AcpiUtGetTypeName (ReturnObject->Common.Type),
+            AcpiUtGetTypeName (NewObject->Common.Type)));
+    }
+
+    /* Delete old object, install the new return object */
+
+    AcpiUtRemoveReference (ReturnObject);
+    *ReturnObjectPtr = NewObject;
+    Data->Flags |= ACPI_OBJECT_REPAIRED;
+
+    return (AE_OK);
 }