From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=fail reason="signature has expired" header.i=@ventanamicro.com header.s=google header.b=l6F2S+gB; spf=pass (domain: ventanamicro.com, ip: 209.85.214.182, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1b52864b701so4270885ad.3 for ; Fri, 16 Jun 2023 03:17:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1686910622; x=1689502622; 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=l6F2S+gB6xYSwubM0S9JJjVHn057Cap54nKmrCNjv6KO8T1RqfEF83K/Qx4dUhGzI2 rke5PNcoD6tGSPCLdRCjzCMl3bjCVcJvroHhL7qInaepDxy4TpTWb8FlNGhzBEyHi6sl s1+L3pF01CXIJVjfq19o4F7ewBuQGbQoXMkBgQA9v4Q0bhI0aylZPaAfGiCfv2nMDwW8 PERtSiTnKABZaG4owEKQU2ri5I+IZCX/V9JzFn6hxfKEVjLp/hj3ZiT8scuw+VLjeaq1 riSpKzxzelHXOifzmhr7/HZlC5+ms1+yjtSVCf2KkuJ2GnYcxH/n5mMb13jEomQpq5Si ViWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686910622; x=1689502622; 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=dUIsRiRhsw6MGjabAuZ6nOysGRxu6tT9kodht+ENbwJm2u6PguEhdVWUaPHvbBNek1 dC2DKoaRKSHXWB95XnuruWkSZGLZDu3b0hUYUxnw8N21YMpPhcqMvNBrNR9CV5TtpuoL K4vJGAk7J+ZEM1sMfzQzFZsP3Xv1qr7aNm6oHUEc0OQSCiTdDJrO5iR8gULX4ZwFg+Ip Kg7Yz9McHoqlkrz9Kz1F8t6vRwYqVCkwhjymjdPzCmXiNVTkjV9CiAikiexy6FEcuZpM SokZexZekNVRoYjkYLGI48xFwC8Z14lwNKugraAFYkzocOQ8Mse8jtyxpqEhygdnG03z Gd3Q== X-Gm-Message-State: AC+VfDywg/+uTLCZnC5FEfJ4ppMqsSN1M8yZuMXXlfiCzHzWCxXlyNx1 isPb2pckRE39YPMGmdM9lFWpmMVb63PlqBkuKmg= 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) Return-Path: 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: [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> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230616101651.1319722-1-sunilvl@ventanamicro.com> References: <20230616101651.1319722-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