From nobody Mon Feb 9 19:30:56 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+106203+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+106203+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1687255377; cv=none; d=zohomail.com; s=zohoarc; b=j0nQUQTBPLlXWV0vRPgznIO1J5VmWGA+nt6aWSrBorUlK7ygkel6AZ6gwm35jMmwGV5SPv0WdXsVhii2ge/rcwbuXgatDZ90L/8A/37d8zZpZAGHIZL6etJ8PqLusuW0lrUwt9Bu8RY5mT75P0t7WKx/0eBFqXtaezjQ66doTYk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687255377; 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=Wab4bmpXCStlRID4p32QDLDyuNI/Z/2XQadnwVJlFnQ=; b=hyVU1zsVsjO//QzAk6l3mg7quNebkuRXO2MDpSXpHqvQl7cGG+566SQYGSK64XK7H8H25WAhSWb3AaY/yJg1SQkpSG+sdflvr3wPqgsQ+WbyS6nvXgs2PSV0o9e1XtTP39Q0zXB3eUHLOIB5+DF3UgKIo+YF5Yf1m5xHHzjn0lI= 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+106203+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1687255377807500.53043354226475; Tue, 20 Jun 2023 03:02:57 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id ZOk3YY1788612x3uLl1TwCd5; Tue, 20 Jun 2023 03:02:57 -0700 X-Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) by mx.groups.io with SMTP id smtpd.web10.6811.1687255376119670322 for ; Tue, 20 Jun 2023 03:02:56 -0700 X-Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6b58439a892so1371844a34.0 for ; Tue, 20 Jun 2023 03:02:56 -0700 (PDT) X-Gm-Message-State: oOOvkrb2bufgvjOhUh16yguJx1787277AA= X-Google-Smtp-Source: ACHHUZ6QL7Uirp6cA7zabq1JV7Sj7AxxSdJf7Lhf8yW4k3Q8bObAc62qYsKDBVWb5DqGBkAhkKeE7w== X-Received: by 2002:a05:6358:6156:b0:12b:c37e:d46b with SMTP id 22-20020a056358615600b0012bc37ed46bmr7129644rwt.21.1687255375116; Tue, 20 Jun 2023 03:02:55 -0700 (PDT) X-Received: from kerodi.Dlink ([106.51.184.72]) by smtp.gmail.com with ESMTPSA id v14-20020aa7808e000000b00666b3b04758sm1033433pff.37.2023.06.20.03.02.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 03:02:54 -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 , Heinrich Schuchardt Subject: [edk2-devel] [PATCH v3 2/4] OvmfPkg/RiscVVirt: Add VirtNorFlashDeviceTreeLib library Date: Tue, 20 Jun 2023 15:32:42 +0530 Message-Id: <20230620100244.1404606-3-sunilvl@ventanamicro.com> In-Reply-To: <20230620100244.1404606-1-sunilvl@ventanamicro.com> References: <20230620100244.1404606-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=1687255377; bh=yyYAoxPANlghjAdbRtY0HO3vbSw0eZJKFcfhBCkzsnw=; h=Cc:Date:From:Reply-To:Subject:To; b=wWbLTAvaDPT63oMwux+ypdlohIbgSVj/6HBh3Au+jU71uZzdYcE8pgdA/HWyWuc2R+g 9F8jtMc4G++85KFuWfv2hEIMlv81GGnl13J1fElrRrXz0bZZnlojSZSJhSU1lfGs9IVVK UZ4ZIIbbj+TyaNGiy10VuaE2vf7gUbNgPBo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1687255379263100014 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 Cc: Heinrich Schuchardt --- .../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 (#106203): https://edk2.groups.io/g/devel/message/106203 Mute This Topic: https://groups.io/mt/99641169/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-