ArmVirtPkg/FdtParser: avoid unaligned accesses with the MMU off
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 13 Sep 2016 14:13:31 +0000 (15:13 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 13 Sep 2016 14:35:36 +0000 (15:35 +0100)
When parsing the device tree to find the memory node, we are still running
with the MMU off, which means unaligned memory accesses are not allowed.
Since the FDT only mandates 32-bit alignment, 64-bit quantities are not
guaranteed to appear naturally aligned, and so should be accessed using
32-bit accesses instead.

Reported-by: Julien Grall <julien.grall@arm.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
ArmVirtPkg/Library/ArmQemuRelocatablePlatformLib/FdtParser.c
ArmVirtPkg/Library/ArmXenRelocatablePlatformLib/FdtParser.c

index 46a5fe6409f613346337e972bca7d425fda44976..afdc81a8839df66e87c24b61f4e218bf342bb7df 100644 (file)
@@ -65,17 +65,15 @@ FindMemnode (
     return FALSE;\r
   }\r
 \r
-  if (AddressCells == 1) {\r
-    *SystemMemoryBase = fdt32_to_cpu (*Prop);\r
-  } else {\r
-    *SystemMemoryBase = fdt64_to_cpu (*(UINT64 *)Prop);\r
+  *SystemMemoryBase = fdt32_to_cpu (Prop[0]);\r
+  if (AddressCells > 1) {\r
+    *SystemMemoryBase = (*SystemMemoryBase << 32) | fdt32_to_cpu (Prop[1]);\r
   }\r
   Prop += AddressCells;\r
 \r
-  if (SizeCells == 1) {\r
-    *SystemMemorySize = fdt32_to_cpu (*Prop);\r
-  } else {\r
-    *SystemMemorySize = fdt64_to_cpu (*(UINT64 *)Prop);\r
+  *SystemMemorySize = fdt32_to_cpu (Prop[0]);\r
+  if (SizeCells > 1) {\r
+    *SystemMemorySize = (*SystemMemorySize << 32) | fdt32_to_cpu (Prop[1]);\r
   }\r
 \r
   return TRUE;\r
index 992932ee9754eaee441d0df74a7710de4c566588..38fd5d3ed00c6e5088356faaacafdb154aa468f2 100644 (file)
@@ -65,17 +65,15 @@ FindMemnode (
     return FALSE;\r
   }\r
 \r
-  if (AddressCells == 1) {\r
-    *SystemMemoryBase = fdt32_to_cpu (*Prop);\r
-  } else {\r
-    *SystemMemoryBase = fdt64_to_cpu (*(UINT64 *)Prop);\r
+  *SystemMemoryBase = fdt32_to_cpu (Prop[0]);\r
+  if (AddressCells > 1) {\r
+    *SystemMemoryBase = (*SystemMemoryBase << 32) | fdt32_to_cpu (Prop[1]);\r
   }\r
   Prop += AddressCells;\r
 \r
-  if (SizeCells == 1) {\r
-    *SystemMemorySize = fdt32_to_cpu (*Prop);\r
-  } else {\r
-    *SystemMemorySize = fdt64_to_cpu (*(UINT64 *)Prop);\r
+  *SystemMemorySize = fdt32_to_cpu (Prop[0]);\r
+  if (SizeCells > 1) {\r
+    *SystemMemorySize = (*SystemMemorySize << 32) | fdt32_to_cpu (Prop[1]);\r
   }\r
 \r
   return TRUE;\r