edb6c78
From d0c879747147c24c47bae363359127d62cd0cae1 Mon Sep 17 00:00:00 2001
edb6c78
From: Al Stone <ahs3@redhat.com>
edb6c78
Date: Fri, 18 Sep 2020 15:14:30 -0600
edb6c78
Subject: [PATCH 02/40] Modify utility functions to be endian-agnostic
edb6c78
edb6c78
All of the modifications here use the big-endian code previously added
edb6c78
(see utendian.c) to make themselves endian-agnostic; i.e., that the code
edb6c78
does not need to change further to work on both big- and little-endian
edb6c78
machines.
edb6c78
edb6c78
These particular files were changed to handle the reading and writing
edb6c78
of files (the length is often embedded in the binary stream), and to
edb6c78
handle the reading and writing of integer values.  The common cases are
edb6c78
to "read" a 32-bit unsigned int in little-endian format, but convert it
edb6c78
to host-native, and to write a byte, word, double word or quad word value
edb6c78
as little-endian, regardless of host-native format.
edb6c78
edb6c78
Signed-off-by: Al Stone <ahs3@redhat.com>
edb6c78
---
edb6c78
 source/common/acfileio.c           | 16 ++++++++++------
edb6c78
 source/common/dmtable.c            |  8 ++++----
edb6c78
 source/compiler/dtfield.c          |  2 +-
edb6c78
 source/compiler/dtsubtable.c       |  4 ++--
edb6c78
 source/components/tables/tbprint.c | 13 +++++++++----
edb6c78
 5 files changed, 26 insertions(+), 17 deletions(-)
edb6c78
edb6c78
Index: acpica-unix2-20200925/source/common/acfileio.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/common/acfileio.c
edb6c78
+++ acpica-unix2-20200925/source/common/acfileio.c
edb6c78
@@ -280,6 +280,7 @@ AcGetOneTableFromFile (
edb6c78
     ACPI_TABLE_HEADER       *Table;
edb6c78
     INT32                   Count;
edb6c78
     long                    TableOffset;
edb6c78
+    UINT32		    Length;
edb6c78
 
edb6c78
 
edb6c78
     *ReturnTable = NULL;
edb6c78
@@ -319,7 +320,8 @@ AcGetOneTableFromFile (
edb6c78
 
edb6c78
     /* Allocate a buffer for the entire table */
edb6c78
 
edb6c78
-    Table = AcpiOsAllocate ((ACPI_SIZE) TableHeader.Length);
edb6c78
+    Length = AcpiUtReadUint32(&TableHeader.Length);
edb6c78
+    Table = AcpiOsAllocate ((ACPI_SIZE) Length);
edb6c78
     if (!Table)
edb6c78
     {
edb6c78
         return (AE_NO_MEMORY);
edb6c78
@@ -329,13 +331,13 @@ AcGetOneTableFromFile (
edb6c78
 
edb6c78
     fseek (File, TableOffset, SEEK_SET);
edb6c78
 
edb6c78
-    Count = fread (Table, 1, TableHeader.Length, File);
edb6c78
+    Count = fread (Table, 1, Length, File);
edb6c78
 
edb6c78
     /*
edb6c78
      * Checks for data table headers happen later in the execution. Only verify
edb6c78
      * for Aml tables at this point in the code.
edb6c78
      */
edb6c78
-    if (GetOnlyAmlTables && Count != (INT32) TableHeader.Length)
edb6c78
+    if (GetOnlyAmlTables && Count != (INT32) Length)
edb6c78
     {
edb6c78
         Status = AE_ERROR;
edb6c78
         goto ErrorExit;
edb6c78
@@ -343,7 +345,7 @@ AcGetOneTableFromFile (
edb6c78
 
edb6c78
     /* Validate the checksum (just issue a warning) */
edb6c78
 
edb6c78
-    Status = AcpiTbVerifyChecksum (Table, TableHeader.Length);
edb6c78
+    Status = AcpiTbVerifyChecksum (Table, Length);
edb6c78
     if (ACPI_FAILURE (Status))
edb6c78
     {
edb6c78
         Status = AcCheckTextModeCorruption (Table);
edb6c78
@@ -436,6 +438,7 @@ AcValidateTableHeader (
edb6c78
     long                    OriginalOffset;
edb6c78
     UINT32                  FileSize;
edb6c78
     UINT32                  i;
edb6c78
+    UINT32		    Length;
edb6c78
 
edb6c78
 
edb6c78
     ACPI_FUNCTION_TRACE (AcValidateTableHeader);
edb6c78
@@ -464,11 +467,12 @@ AcValidateTableHeader (
edb6c78
     /* Validate table length against bytes remaining in the file */
edb6c78
 
edb6c78
     FileSize = CmGetFileSize (File);
edb6c78
-    if (TableHeader.Length > (UINT32) (FileSize - TableOffset))
edb6c78
+    Length = AcpiUtReadUint32(&TableHeader.Length);
edb6c78
+    if (Length > (UINT32) (FileSize - TableOffset))
edb6c78
     {
edb6c78
         fprintf (stderr, "Table [%4.4s] is too long for file - "
edb6c78
             "needs: 0x%.2X, remaining in file: 0x%.2X\n",
edb6c78
-            TableHeader.Signature, TableHeader.Length,
edb6c78
+            TableHeader.Signature, Length,
edb6c78
             (UINT32) (FileSize - TableOffset));
edb6c78
         return (AE_BAD_HEADER);
edb6c78
     }
edb6c78
Index: acpica-unix2-20200925/source/common/dmtable.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/common/dmtable.c
edb6c78
+++ acpica-unix2-20200925/source/common/dmtable.c
edb6c78
@@ -534,7 +534,7 @@ AcpiDmDumpDataTable (
edb6c78
         {
edb6c78
             /* Dump the raw table data */
edb6c78
 
edb6c78
-            Length = Table->Length;
edb6c78
+            Length = AcpiUtReadUint32(&Table->Length);
edb6c78
 
edb6c78
             AcpiOsPrintf ("\n/*\n%s: Length %d (0x%X)\n\n",
edb6c78
                 ACPI_RAW_TABLE_DATA_HEADER, Length, Length);
edb6c78
@@ -551,7 +551,7 @@ AcpiDmDumpDataTable (
edb6c78
      */
edb6c78
     if (ACPI_COMPARE_NAMESEG (Table->Signature, ACPI_SIG_FACS))
edb6c78
     {
edb6c78
-        Length = Table->Length;
edb6c78
+        Length = AcpiUtReadUint32(&Table->Length);
edb6c78
         Status = AcpiDmDumpTable (Length, 0, Table, 0, AcpiDmTableInfoFacs);
edb6c78
         if (ACPI_FAILURE (Status))
edb6c78
         {
edb6c78
@@ -571,7 +571,7 @@ AcpiDmDumpDataTable (
edb6c78
         /*
edb6c78
          * All other tables must use the common ACPI table header, dump it now
edb6c78
          */
edb6c78
-        Length = Table->Length;
edb6c78
+        Length = AcpiUtReadUint32(&Table->Length);
edb6c78
         Status = AcpiDmDumpTable (Length, 0, Table, 0, AcpiDmTableInfoHeader);
edb6c78
         if (ACPI_FAILURE (Status))
edb6c78
         {
edb6c78
@@ -1179,7 +1179,7 @@ AcpiDmDumpTable (
edb6c78
 
edb6c78
             AcpiOsPrintf ("%2.2X", *Target);
edb6c78
             Temp8 = AcpiDmGenerateChecksum (Table,
edb6c78
-                ACPI_CAST_PTR (ACPI_TABLE_HEADER, Table)->Length,
edb6c78
+                AcpiUtReadUint32(&(ACPI_CAST_PTR (ACPI_TABLE_HEADER, Table)->Length)),
edb6c78
                 ACPI_CAST_PTR (ACPI_TABLE_HEADER, Table)->Checksum);
edb6c78
 
edb6c78
             if (Temp8 != ACPI_CAST_PTR (ACPI_TABLE_HEADER, Table)->Checksum)
edb6c78
Index: acpica-unix2-20200925/source/compiler/dtfield.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/compiler/dtfield.c
edb6c78
+++ acpica-unix2-20200925/source/compiler/dtfield.c
edb6c78
@@ -361,7 +361,7 @@ DtCompileInteger (
edb6c78
         DtError (ASL_ERROR, ASL_MSG_INTEGER_SIZE, Field, AslGbl_MsgBuffer);
edb6c78
     }
edb6c78
 
edb6c78
-    memcpy (Buffer, &Value, ByteLength);
edb6c78
+    AcpiUtWriteUint(Buffer, ByteLength, &Value, sizeof(UINT64));
edb6c78
     return;
edb6c78
 }
edb6c78
 
edb6c78
Index: acpica-unix2-20200925/source/compiler/dtsubtable.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/compiler/dtsubtable.c
edb6c78
+++ acpica-unix2-20200925/source/compiler/dtsubtable.c
edb6c78
@@ -378,6 +378,6 @@ DtSetSubtableLength (
edb6c78
         return;
edb6c78
     }
edb6c78
 
edb6c78
-    memcpy (Subtable->LengthField, &Subtable->TotalLength,
edb6c78
-        Subtable->SizeOfLengthField);
edb6c78
+    AcpiUtWriteUint(Subtable->LengthField, Subtable->SizeOfLengthField,
edb6c78
+		    &Subtable->TotalLength, sizeof(Subtable->TotalLength));
edb6c78
 }
edb6c78
Index: acpica-unix2-20200925/source/components/tables/tbprint.c
edb6c78
===================================================================
edb6c78
--- acpica-unix2-20200925.orig/source/components/tables/tbprint.c
edb6c78
+++ acpica-unix2-20200925/source/components/tables/tbprint.c
edb6c78
@@ -44,6 +44,8 @@
edb6c78
 #include "acpi.h"
edb6c78
 #include "accommon.h"
edb6c78
 #include "actables.h"
edb6c78
+#include "platform/acenv.h"
edb6c78
+#include "acutils.h"
edb6c78
 
edb6c78
 #define _COMPONENT          ACPI_TABLES
edb6c78
         ACPI_MODULE_NAME    ("tbprint")
edb6c78
@@ -151,7 +153,7 @@ AcpiTbPrintTableHeader (
edb6c78
 
edb6c78
         ACPI_INFO (("%-4.4s 0x%8.8X%8.8X %06X",
edb6c78
             Header->Signature, ACPI_FORMAT_UINT64 (Address),
edb6c78
-            Header->Length));
edb6c78
+            AcpiUtReadUint32(&Header->Length)));
edb6c78
     }
edb6c78
     else if (ACPI_VALIDATE_RSDP_SIG (Header->Signature))
edb6c78
     {
edb6c78
@@ -178,9 +180,12 @@ AcpiTbPrintTableHeader (
edb6c78
             "%-4.4s 0x%8.8X%8.8X"
edb6c78
             " %06X (v%.2d %-6.6s %-8.8s %08X %-4.4s %08X)",
edb6c78
             LocalHeader.Signature, ACPI_FORMAT_UINT64 (Address),
edb6c78
-            LocalHeader.Length, LocalHeader.Revision, LocalHeader.OemId,
edb6c78
-            LocalHeader.OemTableId, LocalHeader.OemRevision,
edb6c78
-            LocalHeader.AslCompilerId, LocalHeader.AslCompilerRevision));
edb6c78
+            AcpiUtReadUint32(&LocalHeader.Length),
edb6c78
+            LocalHeader.Revision, LocalHeader.OemId,
edb6c78
+            LocalHeader.OemTableId,
edb6c78
+            AcpiUtReadUint32(&LocalHeader.OemRevision),
edb6c78
+            LocalHeader.AslCompilerId,
edb6c78
+            AcpiUtReadUint32(&LocalHeader.AslCompilerRevision)));
edb6c78
     }
edb6c78
 }
edb6c78