]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmPkg/BdsLinuxFdt.c: Check that FDT blob is correctly loaded.
authoroliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 12 Mar 2013 01:01:55 +0000 (01:01 +0000)
committeroliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 12 Mar 2013 01:01:55 +0000 (01:01 +0000)
Add some checks in the code loading an FDT blob from a memory-mapped device
so that UEFI will detect and print an error message if the address
range doesn't cover the whole file.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Olivier Martin <olivier.martin@arm.com>
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@14192 6f19259b-4bc3-4df7-8a09-765794883524

ArmPkg/Library/BdsLib/BdsLinuxFdt.c

index 5fccf8cac791152370e44fb1daa8f40771347080..1baa23716246cee65dbf73befb0e83aa4711d685 100644 (file)
@@ -1,14 +1,14 @@
 /** @file\r
 *\r
-*  Copyright (c) 2011-2012, ARM Limited. All rights reserved.\r
-*  \r
-*  This program and the accompanying materials                          \r
-*  are licensed and made available under the terms and conditions of the BSD License         \r
-*  which accompanies this distribution.  The full text of the license may be found at        \r
-*  http://opensource.org/licenses/bsd-license.php                                            \r
+*  Copyright (c) 2011-2013, ARM Limited. All rights reserved.\r
 *\r
-*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+*  This program and the accompanying materials\r
+*  are licensed and made available under the terms and conditions of the BSD License\r
+*  which accompanies this distribution.  The full text of the license may be found at\r
+*  http://opensource.org/licenses/bsd-license.php\r
+*\r
+*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 *\r
 **/\r
 \r
@@ -243,6 +243,7 @@ PrepareFdt (
   UINTN                 Pages;\r
   BOOLEAN               PsciSmcSupported;\r
   UINTN                 Rx;\r
+  UINTN                 OriginalFdtSize;\r
 \r
   //\r
   // Ensure the Power State Coordination Interface (PSCI) SMCs are there if supported\r
@@ -271,16 +272,28 @@ PrepareFdt (
     }\r
   }\r
 \r
+  //\r
+  // Sanity checks on the original FDT blob.\r
+  //\r
   err = fdt_check_header ((VOID*)(UINTN)(*FdtBlobBase));\r
   if (err != 0) {\r
     Print (L"ERROR: Device Tree header not valid (err:%d)\n", err);\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  // The original FDT blob might have been loaded partially.\r
+  // Check that it is not the case.\r
+  OriginalFdtSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r
+  if (OriginalFdtSize > *FdtBlobSize) {\r
+    Print (L"ERROR: Incomplete FDT. Only %d/%d bytes have been loaded.\n",\r
+           *FdtBlobSize, OriginalFdtSize);\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   // Allocate memory for the new FDT\r
   //\r
-  NewFdtBlobSize = fdt_totalsize((VOID*)(UINTN)(*FdtBlobBase)) + FDT_ADDITIONAL_ENTRIES_SIZE;\r
+  NewFdtBlobSize = OriginalFdtSize + FDT_ADDITIONAL_ENTRIES_SIZE;\r
 \r
   // Try below a watermark address\r
   Status = EFI_NOT_FOUND;\r
@@ -370,10 +383,10 @@ PrepareFdt (
     if (node >= 0) {\r
       fdt_setprop_string(fdt, node, "name", "memory");\r
       fdt_setprop_string(fdt, node, "device_type", "memory");\r
-      \r
+\r
       GetSystemMemoryResources (&ResourceList);\r
       Resource = (BDS_SYSTEM_MEMORY_RESOURCE*)ResourceList.ForwardLink;\r
-      \r
+\r
       if (sizeof(UINTN) == sizeof(UINT32)) {\r
         Region.Base = cpu_to_fdt32((UINTN)Resource->PhysicalStart);\r
         Region.Size = cpu_to_fdt32((UINTN)Resource->ResourceLength);\r
@@ -507,7 +520,7 @@ PrepareFdt (
   return EFI_SUCCESS;\r
 \r
 FAIL_NEW_FDT:\r
-  *FdtBlobSize = (UINTN)fdt_totalsize ((VOID*)(UINTN)(*FdtBlobBase));\r
+  *FdtBlobSize = OriginalFdtSize;\r
   // Return success even if we failed to update the FDT blob. The original one is still valid.\r
   return EFI_SUCCESS;\r
 }\r