From nobody Tue Feb 10 20:06:01 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+106145+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+106145+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1686910624; cv=none; d=zohomail.com; s=zohoarc; b=gauarGdUh+AxnRozymr0OheufjJ+MGCitp1D7B0d/sEfs6A1u9V00Ky+mkLl7utYlCMXyDF3xOhUZvXxWfVUNrqGk1n3VMiLOHLjhBAqL1YKjF/57Qtbbv1U7BX4c0Ygw4CtQUumEMgr7WgXTtQR7L0rBZhH/OWps8ISDntrMAk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1686910624; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=VDK4aYPG9WvAatgaib/TtVNvzOcBWVRudU6/U8OU9wo=; b=hJ3DC+Kx9nO/tunH4MHl7jalvnU6c8QvmC8naq+w7jnDvpyoFVYXkPaFBVTLAtztDruNQAznNhfU7iWRIlME3WgYhhWa5wF3NkFlgbNLyJpKiy3kSex7qtX6znemuiubsXREBi2X0IWn0J43MB66NcS6syRwU+tCVoH+HiAoBmQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+106145+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1686910624556573.948035346218; Fri, 16 Jun 2023 03:17:04 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id dJsQYY1788612xemAaYVcz5k; Fri, 16 Jun 2023 03:17:03 -0700 X-Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by mx.groups.io with SMTP id smtpd.web11.5097.1686910623021961851 for ; Fri, 16 Jun 2023 03:17:03 -0700 X-Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1b52864b701so4270885ad.3 for ; Fri, 16 Jun 2023 03:17:02 -0700 (PDT) X-Gm-Message-State: QFx7r6a5rro4QGXMfO9UeFd4x1787277AA= X-Google-Smtp-Source: ACHHUZ6UQXiSN7NyfIFilCUEauEZzp5J7LhlmRRO2pMc5AAXt3DgDOeM3MOqbStaJJACGH2JM4dKCA== X-Received: by 2002:a17:902:9f97:b0:1b0:637e:e25a with SMTP id g23-20020a1709029f9700b001b0637ee25amr1304364plq.67.1686910622161; Fri, 16 Jun 2023 03:17:02 -0700 (PDT) X-Received: from kerodi.Dlink ([106.51.184.72]) by smtp.gmail.com with ESMTPSA id h12-20020a170902f54c00b001a1a82fc6d3sm15435565plf.268.2023.06.16.03.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jun 2023 03:17:01 -0700 (PDT) From: "Sunil V L" To: devel@edk2.groups.io Cc: Sunil V L , Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Andrei Warkentin Subject: [edk2-devel] [PATCH v2 2/4] OvmfPkg/RiscVVirt: Add VirtNorFlashDeviceTreeLib library Date: Fri, 16 Jun 2023 15:46:49 +0530 Message-Id: <20230616101651.1319722-3-sunilvl@ventanamicro.com> In-Reply-To: <20230616101651.1319722-1-sunilvl@ventanamicro.com> References: <20230616101651.1319722-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,sunilvl@ventanamicro.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1686910623; bh=jQi3a5RIJ5Yys7o6bhqIA9HYdMdn37Rx3JVtTHqtIwo=; h=Cc:Date:From:Reply-To:Subject:To; b=CqPa/OtS7yREmkVuOPLS7NEpSCk58L0lwKopfi842jWcMwHLsl1Wo1Zv9IWsepMIK+1 NRyKwnHvSuYYJD1L47950lny4lDTxsznaNxCJhTBaFIgkfW1eAQoeyeU5Lh5PNl3jNWWw 32mNDTjx/CpdTVULcHi12N+eQ/dDClQOQj4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1686910625295100005 Content-Type: text/plain; charset="utf-8" This library is required to support separate code and variable store images. Signed-off-by: Sunil V L Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Andrei Warkentin --- .../VirtNorFlashDeviceTreeLib.inf | 40 +++++ .../VirtNorFlashDeviceTreeLib.c | 137 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtN= orFlashDeviceTreeLib.inf create mode 100644 OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtN= orFlashDeviceTreeLib.c diff --git a/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlash= DeviceTreeLib.inf b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtN= orFlashDeviceTreeLib.inf new file mode 100644 index 000000000000..90df756e79a9 --- /dev/null +++ b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceT= reeLib.inf @@ -0,0 +1,40 @@ +#/** @file +# +# Component description file for VirtNorFlashDeviceTreeLib module +# +# Copyright (c) 2023, Ventana Micro Systems Inc. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D VirtNorFlashDeviceTreeLib + FILE_GUID =3D 1D74E65F-A468-4FA6-ACB6-E4E941EB79D6 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D VirtNorFlashPlatformLib + +[Sources.common] + VirtNorFlashDeviceTreeLib.c + +[Packages] + MdePkg/MdePkg.dec + EmbeddedPkg/EmbeddedPkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + +[Protocols] + gFdtClientProtocolGuid ## CONSUMES + +[Depex] + gFdtClientProtocolGuid + +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFdBaseAddress + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFirmwareFdSize diff --git a/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlash= DeviceTreeLib.c b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNor= FlashDeviceTreeLib.c new file mode 100644 index 000000000000..73534a866430 --- /dev/null +++ b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceT= reeLib.c @@ -0,0 +1,137 @@ +/** @file + + Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
+ Copyright (c) 2023, Ventana Micro Systems Inc. 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; +} + +STATIC 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 =3D gBS->LocateProtocol ( + &gFdtClientProtocolGuid, + NULL, + (VOID **)&FdtClient + ); + ASSERT_EFI_ERROR (Status); + + Num =3D 0; + for (FindNodeStatus =3D FdtClient->FindCompatibleNode ( + FdtClient, + "cfi-flash", + &Node + ); + !EFI_ERROR (FindNodeStatus) && Num < MAX_FLASH_BANKS; + FindNodeStatus =3D FdtClient->FindNextCompatibleNode ( + FdtClient, + "cfi-flash", + Node, + &Node + )) + { + Status =3D FdtClient->GetNodeProperty ( + FdtClient, + Node, + "reg", + (CONST VOID **)&Reg, + &PropSize + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: GetNodeProperty () failed (Status =3D=3D %r)\n", + __func__, + Status + )); + continue; + } + + ASSERT ((PropSize % (4 * sizeof (UINT32))) =3D=3D 0); + + while (PropSize >=3D (4 * sizeof (UINT32)) && Num < MAX_FLASH_BANKS) { + Base =3D SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0])); + Size =3D SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2])); + Reg +=3D 4; + + PropSize -=3D 4 * sizeof (UINT32); + + // + // Disregard any flash devices that overlap with the primary FV. + // The firmware is not updatable from inside the guest anyway. + // + if ((PcdGet32 (PcdOvmfFdBaseAddress) + PcdGet32 (PcdOvmfFirmwareFdSi= ze) > Base) && + ((Base + Size) > PcdGet32 (PcdOvmfFdBaseAddress))) + { + continue; + } + + mNorFlashDevices[Num].DeviceBaseAddress =3D (UINTN)Base; + mNorFlashDevices[Num].RegionBaseAddress =3D (UINTN)Base; + mNorFlashDevices[Num].Size =3D (UINTN)Size; + mNorFlashDevices[Num].BlockSize =3D QEMU_NOR_BLOCK_SIZE; + Num++; + } + + // + // UEFI takes ownership of the NOR flash, and exposes its functionality + // through the UEFI Runtime Services GetVariable, SetVariable, etc. Th= is + // means we need to disable it in the device tree to prevent the OS fr= om + // attaching its device driver as well. + // Note that this also hides other flash banks, but the only other fla= sh + // bank we expect to encounter is the one that carries the UEFI execut= able + // code, which is not intended to be guest updatable, and is usually b= acked + // in a readonly manner by QEMU anyway. + // + Status =3D 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 =3D mNorFlashDevices; + *Count =3D Num; + + return EFI_SUCCESS; +} --=20 2.34.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#106145): https://edk2.groups.io/g/devel/message/106145 Mute This Topic: https://groups.io/mt/99567398/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-