]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmVirtPkg/NorFlashQemuLib: discover NOR flash banks dynamically
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 21 Nov 2018 11:58:27 +0000 (12:58 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Mon, 26 Nov 2018 16:57:41 +0000 (17:57 +0100)
NorFlashQemuLib is one of the last remaining drivers in ArmVirtPkg
that are not based on the device tree received from QEMU.

For ArmVirtQemu, this does not really matter, given that the NOR
flash banks are always the same: the PEI code is linked to execute
in place from flash bank #0, and the fixed varstore PCDs refer to
flash bank #1 directly.

However, ArmVirtQemuKernel can execute at any offset, permitting it
to be used as an intermediary loader when running QEMU with secure
world emulation enabled, in which case NOR flash bank #0 is secure
only and contains the secure world firmware. In this case,
NorFlashQemuLib should not expose the first flash bank at all.

To prevent introducing too much internal knowledge about which flash
bank is accessible under which circumstances, let's switch to using
the DTB to decide which flash banks to expose to the NOR flash driver.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf

index e3bbae5b06c5222de41df82e9587e0c9b7a32aaa..2678f57eaaad0915cee88268a10e19b430d2d429 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
 /** @file\r
 \r
- Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>\r
+ Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.<BR>\r
 \r
  This program and the accompanying materials\r
  are licensed and made available under the terms and conditions of the BSD License\r
 \r
  This program and the accompanying materials\r
  are licensed and made available under the terms and conditions of the BSD License\r
 \r
  **/\r
 \r
 \r
  **/\r
 \r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
 #include <Library/NorFlashPlatformLib.h>\r
 #include <Library/NorFlashPlatformLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+\r
+#include <Protocol/FdtClient.h>\r
 \r
 #define QEMU_NOR_BLOCK_SIZE    SIZE_256KB\r
 \r
 #define QEMU_NOR_BLOCK_SIZE    SIZE_256KB\r
-#define QEMU_NOR0_BASE         0x0\r
-#define QEMU_NOR0_SIZE         SIZE_64MB\r
-#define QEMU_NOR1_BASE         0x04000000\r
-#define QEMU_NOR1_SIZE         SIZE_64MB\r
+\r
+#define MAX_FLASH_BANKS        4\r
 \r
 EFI_STATUS\r
 NorFlashPlatformInitialization (\r
 \r
 EFI_STATUS\r
 NorFlashPlatformInitialization (\r
@@ -28,21 +31,7 @@ NorFlashPlatformInitialization (
   return EFI_SUCCESS;\r
 }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-NOR_FLASH_DESCRIPTION mNorFlashDevices[] = {\r
-  {\r
-    QEMU_NOR0_BASE,\r
-    QEMU_NOR0_BASE,\r
-    QEMU_NOR0_SIZE,\r
-    QEMU_NOR_BLOCK_SIZE,\r
-    {0xF9B94AE2, 0x8BA6, 0x409B, {0x9D, 0x56, 0xB9, 0xB4, 0x17, 0xF5, 0x3C, 0xB3}}\r
-  }, {\r
-    QEMU_NOR1_BASE,\r
-    QEMU_NOR1_BASE,\r
-    QEMU_NOR1_SIZE,\r
-    QEMU_NOR_BLOCK_SIZE,\r
-    {0x8047DB4B, 0x7E9C, 0x4C0C, {0x8E, 0xBC, 0xDF, 0xBB, 0xAA, 0xCA, 0xCE, 0x8F}}\r
-  }\r
-};\r
+NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_BANKS];\r
 \r
 EFI_STATUS\r
 NorFlashPlatformGetDevices (\r
 \r
 EFI_STATUS\r
 NorFlashPlatformGetDevices (\r
@@ -50,7 +39,54 @@ NorFlashPlatformGetDevices (
   OUT UINT32                  *Count\r
   )\r
 {\r
   OUT UINT32                  *Count\r
   )\r
 {\r
+  FDT_CLIENT_PROTOCOL         *FdtClient;\r
+  INT32                       Node;\r
+  EFI_STATUS                  Status;\r
+  EFI_STATUS                  FindNodeStatus;\r
+  CONST UINT32                *Reg;\r
+  UINT32                      PropSize;\r
+  UINT32                      Num;\r
+  UINT64                      Base;\r
+  UINT64                      Size;\r
+\r
+  Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,\r
+                  (VOID **)&FdtClient);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Num = 0;\r
+  for (FindNodeStatus = FdtClient->FindCompatibleNode (FdtClient,\r
+                                     "cfi-flash", &Node);\r
+       !EFI_ERROR (FindNodeStatus) && Num < MAX_FLASH_BANKS;\r
+       FindNodeStatus = FdtClient->FindNextCompatibleNode (FdtClient,\r
+                                     "cfi-flash", Node, &Node)) {\r
+\r
+    Status = FdtClient->GetNodeProperty (FdtClient, Node, "reg",\r
+                          (CONST VOID **)&Reg, &PropSize);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "%a: GetNodeProperty () failed (Status == %r)\n",\r
+        __FUNCTION__, Status));\r
+      continue;\r
+    }\r
+\r
+    ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);\r
+\r
+    while (PropSize >= (4 * sizeof (UINT32)) && Num < MAX_FLASH_BANKS) {\r
+      Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));\r
+      Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));\r
+      Reg += 4;\r
+\r
+      mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;\r
+      mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;\r
+      mNorFlashDevices[Num].Size              = (UINTN)Size;\r
+      mNorFlashDevices[Num].BlockSize         = QEMU_NOR_BLOCK_SIZE;\r
+      Num++;\r
+\r
+      PropSize -= 4 * sizeof (UINT32);\r
+    }\r
+  }\r
+\r
   *NorFlashDescriptions = mNorFlashDevices;\r
   *NorFlashDescriptions = mNorFlashDevices;\r
-  *Count = ARRAY_SIZE (mNorFlashDevices);\r
+  *Count = Num;\r
+\r
   return EFI_SUCCESS;\r
 }\r
   return EFI_SUCCESS;\r
 }\r
index 126d1671f544c806fd9fef62ea830334474901e2..d86ff36dbd58a40c89a9d7a82694cb057b0bd570 100644 (file)
 [Packages]\r
   MdePkg/MdePkg.dec\r
   ArmPlatformPkg/ArmPlatformPkg.dec\r
 [Packages]\r
   MdePkg/MdePkg.dec\r
   ArmPlatformPkg/ArmPlatformPkg.dec\r
+  ArmVirtPkg/ArmVirtPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  UefiBootServicesTableLib\r
+\r
+[Protocols]\r
+  gFdtClientProtocolGuid          ## CONSUMES\r
+\r
+[Depex]\r
+  gFdtClientProtocolGuid\r