2014-09-18 19:53:36 +02:00
|
|
|
/** @file
|
|
|
|
|
2018-11-21 12:58:27 +01:00
|
|
|
Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.<BR>
|
2014-09-18 19:53:36 +02:00
|
|
|
|
2019-04-04 01:03:23 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2014-09-18 19:53:36 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
2018-11-21 12:58:27 +01:00
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
#include <Library/DebugLib.h>
|
2014-09-18 19:53:36 +02:00
|
|
|
#include <Library/NorFlashPlatformLib.h>
|
2018-11-21 12:58:27 +01:00
|
|
|
#include <Library/UefiBootServicesTableLib.h>
|
|
|
|
|
|
|
|
#include <Protocol/FdtClient.h>
|
2014-09-18 19:53:36 +02:00
|
|
|
|
2017-11-15 14:59:08 +01:00
|
|
|
#define QEMU_NOR_BLOCK_SIZE SIZE_256KB
|
2018-11-21 12:58:27 +01:00
|
|
|
|
|
|
|
#define MAX_FLASH_BANKS 4
|
2017-11-15 14:59:08 +01:00
|
|
|
|
2014-09-18 19:53:36 +02:00
|
|
|
EFI_STATUS
|
|
|
|
NorFlashPlatformInitialization (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:58:27 +01:00
|
|
|
NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_BANKS];
|
2014-09-18 19:53:36 +02:00
|
|
|
|
|
|
|
EFI_STATUS
|
|
|
|
NorFlashPlatformGetDevices (
|
|
|
|
OUT NOR_FLASH_DESCRIPTION **NorFlashDescriptions,
|
|
|
|
OUT UINT32 *Count
|
|
|
|
)
|
|
|
|
{
|
2018-11-21 12:58:27 +01:00
|
|
|
FDT_CLIENT_PROTOCOL *FdtClient;
|
|
|
|
INT32 Node;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_STATUS FindNodeStatus;
|
|
|
|
CONST UINT32 *Reg;
|
|
|
|
UINT32 PropSize;
|
|
|
|
UINT32 Num;
|
|
|
|
UINT64 Base;
|
|
|
|
UINT64 Size;
|
|
|
|
|
|
|
|
Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,
|
|
|
|
(VOID **)&FdtClient);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
|
|
|
|
Num = 0;
|
|
|
|
for (FindNodeStatus = FdtClient->FindCompatibleNode (FdtClient,
|
|
|
|
"cfi-flash", &Node);
|
|
|
|
!EFI_ERROR (FindNodeStatus) && Num < MAX_FLASH_BANKS;
|
|
|
|
FindNodeStatus = FdtClient->FindNextCompatibleNode (FdtClient,
|
|
|
|
"cfi-flash", Node, &Node)) {
|
|
|
|
|
|
|
|
Status = FdtClient->GetNodeProperty (FdtClient, Node, "reg",
|
|
|
|
(CONST VOID **)&Reg, &PropSize);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((DEBUG_ERROR, "%a: GetNodeProperty () failed (Status == %r)\n",
|
|
|
|
__FUNCTION__, Status));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
|
|
|
|
|
|
|
|
while (PropSize >= (4 * sizeof (UINT32)) && Num < MAX_FLASH_BANKS) {
|
|
|
|
Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
|
|
|
|
Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
|
|
|
|
Reg += 4;
|
|
|
|
|
2018-11-30 12:28:28 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:58:27 +01:00
|
|
|
mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;
|
|
|
|
mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;
|
|
|
|
mNorFlashDevices[Num].Size = (UINTN)Size;
|
|
|
|
mNorFlashDevices[Num].BlockSize = QEMU_NOR_BLOCK_SIZE;
|
|
|
|
Num++;
|
|
|
|
}
|
ArmVirtPkg/NorFlashQemuLib: disable NOR flash DT nodes upon discovery
Our UEFI guest firmware takes ownership of the emulated NOR flash in
order to support the variable runtime services, and it does not expect
the OS to interfere with the underlying storage directly. So disable
the NOR flash DT nodes as we discover them, in a way similar to how we
disable the PL031 RTC in the device tree when we attach our RTC runtime
driver to it.
Note that this also hides the NOR flash bank that carries the UEFI
executable code, but this is not intended to be updatable from inside
the guest anyway, and if it was, we should use capsule update to do so.
Also, the first -pflash argument that defines the backing for this flash
bank is often issued with the 'readonly' modifier, in order to prevent
any changes whatsoever to be made to the executable firmware image by
the guest.
This issue has become relevant due to the following Linux changes,
which enable the flash driver stack for default build configurations
targetting arm64 and 32-bit ARM.
ce693fc2a877
("arm64: defconfig: Enable flash device drivers for QorIQ boards", 2020-03-16).
5f068190cc10
("ARM: multi_v7_defconfig: Enable support for CFI NOR FLASH", 2019-04-03)
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Philippe Mathieu-Daude <philmd@redhat.com>
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
2020-06-23 19:54:46 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// UEFI takes ownership of the NOR flash, and exposes its functionality
|
|
|
|
// through the UEFI Runtime Services GetVariable, SetVariable, etc. This
|
|
|
|
// means we need to disable it in the device tree to prevent the OS from
|
|
|
|
// attaching its device driver as well.
|
|
|
|
// Note that this also hides other flash banks, but the only other flash
|
|
|
|
// bank we expect to encounter is the one that carries the UEFI executable
|
|
|
|
// code, which is not intended to be guest updatable, and is usually backed
|
|
|
|
// in a readonly manner by QEMU anyway.
|
|
|
|
//
|
|
|
|
Status = FdtClient->SetNodeProperty (FdtClient, Node, "status",
|
|
|
|
"disabled", sizeof ("disabled"));
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 'disabled'\n"));
|
|
|
|
}
|
2018-11-21 12:58:27 +01:00
|
|
|
}
|
|
|
|
|
2014-09-18 19:53:36 +02:00
|
|
|
*NorFlashDescriptions = mNorFlashDevices;
|
2018-11-21 12:58:27 +01:00
|
|
|
*Count = Num;
|
|
|
|
|
2014-09-18 19:53:36 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|