From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by mx.groups.io with SMTP id smtpd.web11.8335.1667136601943906464 for ; Sun, 30 Oct 2022 06:30:02 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=cRNSA4kZ; spf=pass (domain: ventanamicro.com, ip: 209.85.216.41, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pj1-f41.google.com with SMTP id q1-20020a17090a750100b002139ec1e999so3863783pjk.1 for ; Sun, 30 Oct 2022 06:30:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pzkQL4xVj/pthDn9UhhUbM41zlSBPoNB5nPmSWQzmW0=; b=cRNSA4kZR1VU2NoWcFkb6umGfzUzDG4Z17vWSWBva505dlOH5ja2yVXwGCbhaq8WgS AB0cR1Y5o8yDc1ZfUbz/Ex6UGmT5gAhX1Tg6q1Etns5kYTL+2sbSHc1ikG2nMfPeGoqC xmxOfyiDKAf3QOCyYNvU1mp74IGTR6OR4sNReHXE+hCKulIWxpIdveEyOPhWpRUX29J2 /S5VP2+oN1791AMEevBc6T2WBvqTd9L3vvRmXK+j6wbOWYwoHSuVWlwvNEDkZByRwwsS EHCyMg0YGlOI8u3ukPhMd3z34u2K4K5nE1sxgc1zXnfFD+g9Xl2iy0/P9ZDYJrDUmvLh gW5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pzkQL4xVj/pthDn9UhhUbM41zlSBPoNB5nPmSWQzmW0=; b=FKHJ+hOothGHG2AGl9UFuJGxVm9a/XqVh5/1bGCt5J7Sy74FmwpEoNtUfJ/J8xP70y JwU57DfXvvaVq4jrwG5kGQ/4P+l1O+rcQ/KpYWLfopZ3gq7q7/7YBP3qqxWvPdoYBgvl WT08S6fSzqFapyA8kkJlJJU9rDy+8Y4cXA+h5bz0qs7JDDLg40eWrt4utUCTK0+RE6bD nNBDyMB8jqjApFBqB4OyA2pQmP0gzfWqFBYiXiKNkQShR0m6q8sRARdSKg58wCBdItLC dyviCFMiOIC/02BrEHsAFLJLrFf/UUXlwfaI8QHp2+gQKVFsQy+EtZ/p3eXTYGN8vdOF tXvQ== X-Gm-Message-State: ACrzQf3ziFKYGjSV8D3ymMMfniqikAYZ3QX+4vJQaBFQIgAMhy1XmMC7 ZD7ZLoltLz1Rss7X4QwImx0aDpAttPJL5A== X-Google-Smtp-Source: AMsMyM4VFvZh2kFOtO5CRiN33kIgorKiPs7oKTguCb8Vx1r5VFy74v2FCrXFhXK/bLs/cn/2AO8SNw== X-Received: by 2002:a17:90a:5781:b0:20a:9962:bb4a with SMTP id g1-20020a17090a578100b0020a9962bb4amr26119422pji.185.1667136601190; Sun, 30 Oct 2022 06:30:01 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([49.206.12.236]) by smtp.gmail.com with ESMTPSA id r10-20020aa79eca000000b0056b6a22d6c9sm2612330pfq.212.2022.10.30.06.29.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Oct 2022 06:30:00 -0700 (PDT) From: "Sunil V L" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Daniel Schaefer Subject: [edk2-staging/RiscV64QemuVirt PATCH V5 27/30] OvmfPkg: Add VirtNorFlashPlatformLib library Date: Sun, 30 Oct 2022 18:58:39 +0530 Message-Id: <20221030132842.54077-28-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221030132842.54077-1-sunilvl@ventanamicro.com> References: <20221030132842.54077-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4076 Add the VirtNorFlashPlatformLib library for qemu virt machines. Add two instances of the library. One which uses DT information and the other which is static. They are copied from ArmVirtPkg and SbsaQemu. Two PCD variables used by the library are added in the OvmfPkg. Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Daniel Schaefer Signed-off-by: Sunil V L --- OvmfPkg/OvmfPkg.dec | 4 + OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf | 40 ++++++ OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf | 30 +++++ OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c | 136 ++++++++++++++++++++ OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c | 40 ++++++ 5 files changed, 250 insertions(+) diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index ba5194114c5c..142c34193d60 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -409,6 +409,10 @@ [PcdsFixedAtBuild] # check to decide whether to abort dispatch of the driver it is linked into. gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName|""|VOID*|0x68 + ## The base address and size of the FVMAIN + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvBaseAddress|0|UINT64|0x71 + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvSize|0|UINT32|0x72 + [PcdsDynamic, PcdsDynamicEx] gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x10 diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf new file mode 100644 index 000000000000..5b7a45d15782 --- /dev/null +++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf @@ -0,0 +1,40 @@ +#/** @file +# +# Component description file for VirtNorFlashDeviceTreeLib module +# +# Copyright (c) 2014, Linaro Ltd. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VirtNorFlashDeviceTreeLib + FILE_GUID = 42C30D8E-BFAD-4E77-9041-E7DAAE88DF7A + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = VirtNorFlashPlatformLib + +[Sources.common] + VirtNorFlashDeviceTreeLib.c + +[Packages] + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + +[Protocols] + gFdtClientProtocolGuid ## CONSUMES + +[Depex] + gFdtClientProtocolGuid + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvBaseAddress + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvSize diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf new file mode 100644 index 000000000000..4e87bd437380 --- /dev/null +++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf @@ -0,0 +1,30 @@ +#/** @file +# +# Component description file for VirtNorFlashStaticLib module +# +# Copyright (c) 2014, Linaro Ltd. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VirtNorFlashStaticLib + FILE_GUID = 064742F1-E531-4D7D-A154-22315889CC23 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = VirtNorFlashPlatformLib + +[Sources.common] + VirtNorFlashStaticLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + OvmfPkg/OvmfPkg.dec + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFdBaseAddress + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFirmwareFdSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c new file mode 100644 index 000000000000..08750e66095d --- /dev/null +++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c @@ -0,0 +1,136 @@ +/** @file + + Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + **/ + +#include +#include +#include +#include + +#include + +#define QEMU_NOR_BLOCK_SIZE SIZE_256KB + +#define MAX_FLASH_BANKS 4 + +EFI_STATUS +VirtNorFlashPlatformInitialization ( + VOID + ) +{ + return EFI_SUCCESS; +} + +VIRT_NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_BANKS]; + +EFI_STATUS +VirtNorFlashPlatformGetDevices ( + OUT VIRT_NOR_FLASH_DESCRIPTION **NorFlashDescriptions, + OUT UINT32 *Count + ) +{ + 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; + + 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 (PcdOvmfFvBaseAddress) + PcdGet32 (PcdOvmfFvSize) > Base) && + ((Base + Size) > PcdGet64 (PcdOvmfFvBaseAddress))) + { + continue; + } + + mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base; + mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base; + mNorFlashDevices[Num].Size = (UINTN)Size; + mNorFlashDevices[Num].BlockSize = QEMU_NOR_BLOCK_SIZE; + Num++; + } + + // + // 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")); + } + } + + *NorFlashDescriptions = mNorFlashDevices; + *Count = Num; + + return EFI_SUCCESS; +} diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c new file mode 100644 index 000000000000..fdc2ccb6294e --- /dev/null +++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c @@ -0,0 +1,40 @@ +/** @file + + Copyright (c) 2019, Linaro Ltd. All rights reserved + + SPDX-License-Identifier: BSD-2-Clause-Patent + + **/ + +#include +#include +#include + +#define QEMU_NOR_BLOCK_SIZE SIZE_256KB + +EFI_STATUS +VirtNorFlashPlatformInitialization ( + VOID + ) +{ + return EFI_SUCCESS; +} + +VIRT_NOR_FLASH_DESCRIPTION mNorFlashDevice = +{ + FixedPcdGet32 (PcdOvmfFdBaseAddress), + FixedPcdGet64 (PcdFlashNvStorageVariableBase), + FixedPcdGet32 (PcdOvmfFirmwareFdSize), + QEMU_NOR_BLOCK_SIZE +}; + +EFI_STATUS +VirtNorFlashPlatformGetDevices ( + OUT VIRT_NOR_FLASH_DESCRIPTION **NorFlashDescriptions, + OUT UINT32 *Count + ) +{ + *NorFlashDescriptions = &mNorFlashDevice; + *Count = 1; + return EFI_SUCCESS; +} -- 2.38.0