From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@ventanamicro.com header.s=google header.b=fplmk/Ym; spf=pass (domain: ventanamicro.com, ip: 209.85.210.51, mailfrom: sunilvl@ventanamicro.com) Received: by mail-ot1-f51.google.com with SMTP id 46e09a7af769-6b58439a892so1371844a34.0 for ; Tue, 20 Jun 2023 03:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1687255375; x=1689847375; 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=NBKhfEUf74Tv/fDaU/vSw3uHXVzoHQBV8/H3r+mIOe4=; b=fplmk/YmTQHMGwjbYaGSvibxuo6d6NQJxv1d5v9QG0txF529f96Gc0Y2lrXU5uSxdN WuW1PeJtNseJtC2pS2KXa+Kpx8fiSAa57290hdoEOnInuX6J5ZFNljFu+Cg1NKH2NHX9 eGWF/S80SZmAOQeGEM9lxXJHDUKIWCiD8p+oKw1ABvWrNYKGitHn8N2cxB7IG36KzrD7 oXMa2wHBEg3P7FeUsXuawSSo8ygZb3m4R9l1RcQvYukpNltzXwX9Tod66R0aQ9FwNU+t EnlmZh7POblVneLU2J/tDWB0W39uM++/hQ9rNZRw/GNN3joXDiLYJ0OvZLjPQuIpEJTq h8Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687255375; x=1689847375; 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=NBKhfEUf74Tv/fDaU/vSw3uHXVzoHQBV8/H3r+mIOe4=; b=bnqm3T9SR/sVGw/cGH18oazqBz67oLvBlVF2mKG3SR/v43il5OIdLk3so9AcDxLM34 J9e2BR+09JYM6Tf2BOLtTCRaOD1WGaQ10pQzU7c1G77ucDYistXZiY97V4q86Lzy0lXh l8NT1Bbw4or6cY7UV0xiIKtY85jAHFea/s9ylpDzNLwWHJHIC4hL5wl/Av9CBkiApufH X2BOyEqjhj9kzZywVqilTT7L4oSXjvkG/2ERW036tLJesnxBwGmasf+sQ1AqkYgMNmGu +np7m63ovSw3RNXEsxG6nXJH/ajdJK8mC2bNBqEEIjPBXyE9Kx0GQdfK6D6PjxGRsrZH nWSQ== X-Gm-Message-State: AC+VfDyo+gvQDAloXxVAHPOp2yZtZa4JBJq1LLVITdXqTS866wZeSuQQ ZyhTHrdqVz42a4vZlgNo6zSD9c4KhoyW/1uMRQc= 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) Return-Path: 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: [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> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230620100244.1404606-1-sunilvl@ventanamicro.com> References: <20230620100244.1404606-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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/VirtNorFlashDeviceTreeLib.inf create mode 100644 OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c diff --git a/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf new file mode 100644 index 000000000000..90df756e79a9 --- /dev/null +++ b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.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 = 0x0001001B + BASE_NAME = VirtNorFlashDeviceTreeLib + FILE_GUID = 1D74E65F-A468-4FA6-ACB6-E4E941EB79D6 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = 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/VirtNorFlashDeviceTreeLib.c b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c new file mode 100644 index 000000000000..73534a866430 --- /dev/null +++ b/OvmfPkg/RiscVVirt/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.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 = 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", + __func__, + 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 ((PcdGet32 (PcdOvmfFdBaseAddress) + PcdGet32 (PcdOvmfFirmwareFdSize) > Base) && + ((Base + Size) > PcdGet32 (PcdOvmfFdBaseAddress))) + { + 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; +} -- 2.34.1