From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 8E6EF7803CF for ; Fri, 17 May 2024 07:18:14 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=D3+w7GhJ5w7PUnurcPYjzU7skxw2690GeIEFyJqhSjc=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1715930292; v=1; b=W+nE1WkCdhfC0A+NV4zqxc2S0GL+zFdmDKcHfB7EJmSihMxT5Dl7q2Ixe9xkWwg/sZtLoX83 KgK8H3nhNAMYRY+FahcUT0c8jUD3QjWAsWnxoeJTbVNhyi3iimCD27w3rtXos7PevUtg2k9noiW MBzvaRW2ru8SxW+NxBzjwE+VtJuXyX9FPaqfX2kXPXbREPTC9a0/6Ot2PbArN8l8JTWUgQjYdRX 63K2qUTvfH0su0NKpkuoFviU2cAjnk+scXOtaO5UZK1PYWEo0VOQRvTeaLFvmXA0AVLz8DdtTaY m6BrODB1eRbj6augxhZRQvijFjP1RgPaxsY1vvs+JqZDQ== X-Received: by 127.0.0.2 with SMTP id KQT9YY7687511xsKFjXhMzCl; Fri, 17 May 2024 00:18:12 -0700 X-Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web11.34971.1715930287005982485 for ; Fri, 17 May 2024 00:18:12 -0700 X-Received: from loongson.cn (unknown [10.2.9.245]) by gateway (Coremail) with SMTP id _____8BxSfGlBEdmkd0NAA--.32957S3; Fri, 17 May 2024 15:17:57 +0800 (CST) X-Received: from code-server.gen (unknown [10.2.9.245]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxElajBEdmbxskAA--.4575S2; Fri, 17 May 2024 15:17:55 +0800 (CST) From: "Chao Li" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Leif Lindholm , Sami Mujawar , Gerd Hoffmann , Jiewen Yao , Xianglai Li Subject: [edk2-devel] [PATCH v1 1/2] OvmfPkg: Add no hardcode version of FtdNorFlashQemuLib Date: Fri, 17 May 2024 15:17:54 +0800 Message-Id: <20240517071754.188521-1-lichao@loongson.cn> In-Reply-To: <20240517071729.188409-1-lichao@loongson.cn> References: <20240517071729.188409-1-lichao@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8BxElajBEdmbxskAA--.4575S2 X-CM-SenderInfo: xolfxt3r6o00pqjv00gofq/1tbiAQAACGZGxZUDpQAMsT X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 17 May 2024 00:18:12 -0700 Resent-From: lichao@loongson.cn Reply-To: devel@edk2.groups.io,lichao@loongson.cn List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: fsDyNKYx5jWRRX6N9iCk3R2ox7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=W+nE1WkC; dmarc=none; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io This library is copied from ArmVirtPkg, in the Arm version, the value of PcdFlashNvStorageVariableBase, PcdFlashNvStorageFtwWorkingBase and PcdFlashNvStorageFtwSpareBase are hardcoded in INC file. This version will calculate them from FDT resource and using the set PCD to store when the NorFlashInitialise is called. By default, the first available flash(not used for storage UEFI code) as NV variable storage medium. In this way, UEFI can better handle the change of flash base address, which is suitable for different cpu architecture board implementation. BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4770 Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Sami Mujawar Cc: Gerd Hoffmann Cc: Jiewen Yao Signed-off-by: Chao Li Signed-off-by: Xianglai Li --- .../FdtNorFlashQemuLib/FdtNorFlashQemuLib.c | 165 ++++++++++++++++++ .../FdtNorFlashQemuLib/FdtNorFlashQemuLib.inf | 46 +++++ 2 files changed, 211 insertions(+) create mode 100644 OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.c create mode 100644 OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.inf diff --git a/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.c b/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.c new file mode 100644 index 0000000000..e5c7d4cdfa --- /dev/null +++ b/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.c @@ -0,0 +1,165 @@ +/** @file + + Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
+ Copyright (c) 2024 Loongson Technology Corporation Limited. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include + +#include +#include + +#define QEMU_NOR_BLOCK_SIZE SIZE_256KB +#define MAX_FLASH_BANKS 4 + +STATIC VIRT_NOR_FLASH_DESCRIPTION mNorFlashDevices[MAX_FLASH_BANKS]; + +EFI_STATUS +VirtNorFlashPlatformInitialization ( + VOID + ) +{ + return EFI_SUCCESS; +} + +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; + BOOLEAN Found; + + Status = gBS->LocateProtocol ( + &gFdtClientProtocolGuid, + NULL, + (VOID **)&FdtClient + ); + ASSERT_EFI_ERROR (Status); + + Num = 0; + Found = FALSE; + 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++; + if (!Found) { + // + // By default, the second available flash is stored as a non-volatile variable. + // + Status = PcdSet32S (PcdFlashNvStorageVariableBase, Base); + ASSERT_EFI_ERROR (Status); + + // + // The Base is the value of PcdFlashNvStorageVariableBase, + // PcdFlashNvStorageFtwWorkingBase can be got by + // PcdFlashNvStorageVariableBase + PcdFlashNvStorageVariableSize + // + Base += PcdGet32 (PcdFlashNvStorageVariableSize); + Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, Base); + ASSERT_EFI_ERROR (Status); + + // + // Now, the Base is the value of PcdFlashNvStorageFtwWorkingBase, + // PcdFlashNvStorageFtwSpareBase can be got by + // PcdFlashNvStorageFtwWorkingBase + PcdFlashNvStorageFtwWorkingSize. + // + Base += PcdGet32 (PcdFlashNvStorageFtwWorkingSize); + Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, Base); + ASSERT_EFI_ERROR (Status); + Found = TRUE; + } + } + + // + // 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; +} diff --git a/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.inf b/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.inf new file mode 100644 index 0000000000..14ddb4c11e --- /dev/null +++ b/OvmfPkg/Library/FdtNorFlashQemuLib/FdtNorFlashQemuLib.inf @@ -0,0 +1,46 @@ +## @file +# +# Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.
+# Copyright (c) 2024 Loongson Technology Corporation Limited. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 1.29 + BASE_NAME = NorFlashQemuLib + FILE_GUID = E225C90F-6CB9-8AF3-095B-2668FC633A57 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = NorFlashQemuLib|DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION + +[Sources] + FdtNorFlashQemuLib.c + +[Packages] + EmbeddedPkg/EmbeddedPkg.dec + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + UefiBootServicesTableLib + +[Protocols] + gFdtClientProtocolGuid ## CONSUMES + +[Depex] + gFdtClientProtocolGuid + +[Pcd] +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFdBaseAddress +gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFirmwareFdSize +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase -- 2.27.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118988): https://edk2.groups.io/g/devel/message/118988 Mute This Topic: https://groups.io/mt/106149595/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-