From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by mx.groups.io with SMTP id smtpd.web11.18278.1686762122529910620 for ; Wed, 14 Jun 2023 10:02:02 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=PkDfa0MW; spf=pass (domain: ventanamicro.com, ip: 209.85.210.179, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-666683eb028so527019b3a.0 for ; Wed, 14 Jun 2023 10:02:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1686762122; x=1689354122; 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=V4CYaL+2UZsb+63atsiZx5rN9ChhtXOA459q41FxE08=; b=PkDfa0MW5Jg/co1x6D1LA//8NuT8HM6SZbYFH3Bwef95QqwbzWNIuP72lCJBJsBhd5 Eld2Ye+QotkNJFVMprAfZpZZd2IeFNypsLjuoIYJagADLP22FFyAzIedsioKzB4iePSi YVacBdBmUbSCtribE9MfR0VBItfKEO+fX8yUIEVqmlXSAlJ62vWtamXNf2825DMKvXGu DlFGMVbmTONrOL89/hLNBaN2PPPObcV8VM2cmzIfjqNAgVY2GwkUIdZ9t2mp2XXgaeJd 4EdRE1aKa2h6+IRc/kNJM41Z/Ox+PWAAh8wRu8VdKMKKp74P+pPeH4tY9YPzfWXBw3Py YYVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686762122; x=1689354122; 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=V4CYaL+2UZsb+63atsiZx5rN9ChhtXOA459q41FxE08=; b=BqearZB1A8pZDF6KX+g1tWqStXd4ShZL2BASD5ENzavCWbvOaDHizDyueKAnxNTo/Z WvnGMKPpIw8woz91aKIx/UE3hGSxNv4tJJpC039PIPtWYBsOjFH51nM+FWnxzpybLqFt 3F+pkuzmI7jmpWb0zTc6aSc90tOoHTzbuIIyde3oTkV5AIk/TVvL9nfVCRorw97SDbMv untU/56EVWt/ZQrrsVxjJD/D8272hPPU1cDfEqIG6mXNYTd1tRduESR/dA/V3UJ6GaWp oCUo4PE7gN7uSF3pkf/kZMGI4N5NekxxjUboqzjM/CldVSxjleEbf+X6ziupYIQlYm50 COow== X-Gm-Message-State: AC+VfDwpdvw8tcPZcYeOP1lccVYIj8JOcEM/OivUmlC3Lx021TCMWopG UoO5MFGzBhKFf6YZQRh2wEvkrNIryfGmFwIVuc8= X-Google-Smtp-Source: ACHHUZ5R9e39oCiM2yeQltWYVhF4Md5W/hdtxQKQCtMp13/JPvGSEZeyFqOnJytYuBaTLTMN1XJ3OQ== X-Received: by 2002:a05:6a20:431f:b0:10b:2f5a:83af with SMTP id h31-20020a056a20431f00b0010b2f5a83afmr2034911pzk.16.1686762121759; Wed, 14 Jun 2023 10:02:01 -0700 (PDT) Return-Path: Received: from kerodi.Dlink ([106.51.184.72]) by smtp.gmail.com with ESMTPSA id s7-20020a170902988700b001b3ef11d8fdsm1850424plp.201.2023.06.14.10.01.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jun 2023 10:02: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: [PATCH 2/4] OvmfPkg/RiscVVirt: Add VirtNorFlashDeviceTreeLib library Date: Wed, 14 Jun 2023 22:31:49 +0530 Message-Id: <20230614170151.1204982-3-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230614170151.1204982-1-sunilvl@ventanamicro.com> References: <20230614170151.1204982-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 --- .../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