[edk2,v2,3/4] ArmVirtPkg/NorFlashQemuLib: disregard our primary FV

Message ID 20181130112829.12173-4-ard.biesheuvel@linaro.org
State New
Headers show
Series
  • ArmVirtQemu: unmap page #0 to catch NULL pointer dereferences
Related show

Commit Message

Ard Biesheuvel Nov. 30, 2018, 11:28 a.m.
The primary FV contains the firmware boot image, which is not
runtime updatable in our case. So exposing it to the NOR flash
driver is undesirable, since it may attempt to modify the NOR
flash contents. It is also rather pointless, since we don't
keep anything there that we care to expose. (the SEC and PEI
phase modules are not executable from DXE context, and the
contents of the embedded DXE phase FV are exposed by the DXE
core directly via the FVB2 protocol)

So let's disregard the NOR flash block that covers the primary
FV.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf |  5 +++++
 ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c   | 13 +++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

-- 
2.19.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Comments

Laszlo Ersek Dec. 3, 2018, 1:32 p.m. | #1
On 11/30/18 12:28, Ard Biesheuvel wrote:
> The primary FV contains the firmware boot image, which is not

> runtime updatable in our case. So exposing it to the NOR flash

> driver is undesirable, since it may attempt to modify the NOR

> flash contents. It is also rather pointless, since we don't

> keep anything there that we care to expose. (the SEC and PEI

> phase modules are not executable from DXE context, and the

> contents of the embedded DXE phase FV are exposed by the DXE

> core directly via the FVB2 protocol)

> 

> So let's disregard the NOR flash block that covers the primary

> FV.

> 

> Contributed-under: TianoCore Contribution Agreement 1.1

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

> ---

>  ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf |  5 +++++

>  ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c   | 13 +++++++++++--

>  2 files changed, 16 insertions(+), 2 deletions(-)

> 

> diff --git a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf

> index d86ff36dbd58..c5752a243e6b 100644

> --- a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf

> +++ b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf

> @@ -28,6 +28,7 @@ [Sources.common]

>  [Packages]

>    MdePkg/MdePkg.dec

>    ArmPlatformPkg/ArmPlatformPkg.dec

> +  ArmPkg/ArmPkg.dec

>    ArmVirtPkg/ArmVirtPkg.dec

>  

>  [LibraryClasses]

> @@ -40,3 +41,7 @@ [Protocols]

>  

>  [Depex]

>    gFdtClientProtocolGuid

> +

> +[Pcd]

> +  gArmTokenSpaceGuid.PcdFvBaseAddress

> +  gArmTokenSpaceGuid.PcdFvSize

> diff --git a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c

> index 2678f57eaaad..d238e39a59f1 100644

> --- a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c

> +++ b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c

> @@ -75,13 +75,22 @@ NorFlashPlatformGetDevices (

>        Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));

>        Reg += 4;

>  

> +      PropSize -= 4 * sizeof (UINT32);

> +

> +      //

> +      // Disregard any flash devices that overlap with the primary FV.

> +      // The firmware is not updatable from inside the guest anyway.

> +      //

> +      if ((PcdGet64 (PcdFvBaseAddress) + PcdGet32 (PcdFvSize) > Base) &&

> +          (Base + Size) > PcdGet64 (PcdFvBaseAddress)) {

> +        continue;

> +      }

> +

>        mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;

>        mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;

>        mNorFlashDevices[Num].Size              = (UINTN)Size;

>        mNorFlashDevices[Num].BlockSize         = QEMU_NOR_BLOCK_SIZE;

>        Num++;

> -

> -      PropSize -= 4 * sizeof (UINT32);

>      }

>    }

>  

> 


Reviewed-by: Laszlo Ersek <lersek@redhat.com>


_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Patch

diff --git a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
index d86ff36dbd58..c5752a243e6b 100644
--- a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
+++ b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
@@ -28,6 +28,7 @@  [Sources.common]
 [Packages]
   MdePkg/MdePkg.dec
   ArmPlatformPkg/ArmPlatformPkg.dec
+  ArmPkg/ArmPkg.dec
   ArmVirtPkg/ArmVirtPkg.dec
 
 [LibraryClasses]
@@ -40,3 +41,7 @@  [Protocols]
 
 [Depex]
   gFdtClientProtocolGuid
+
+[Pcd]
+  gArmTokenSpaceGuid.PcdFvBaseAddress
+  gArmTokenSpaceGuid.PcdFvSize
diff --git a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
index 2678f57eaaad..d238e39a59f1 100644
--- a/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
+++ b/ArmVirtPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
@@ -75,13 +75,22 @@  NorFlashPlatformGetDevices (
       Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
       Reg += 4;
 
+      PropSize -= 4 * sizeof (UINT32);
+
+      //
+      // Disregard any flash devices that overlap with the primary FV.
+      // The firmware is not updatable from inside the guest anyway.
+      //
+      if ((PcdGet64 (PcdFvBaseAddress) + PcdGet32 (PcdFvSize) > Base) &&
+          (Base + Size) > PcdGet64 (PcdFvBaseAddress)) {
+        continue;
+      }
+
       mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;
       mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;
       mNorFlashDevices[Num].Size              = (UINTN)Size;
       mNorFlashDevices[Num].BlockSize         = QEMU_NOR_BLOCK_SIZE;
       Num++;
-
-      PropSize -= 4 * sizeof (UINT32);
     }
   }