From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 0DED274003A for ; Mon, 11 Dec 2023 22:39:30 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Uz6AVVAPleU6KyTCrnuGr8QIZjO5P1aqEAskpfAe+MI=; 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:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1702334369; v=1; b=ealENq5HsVsvS+RiU6pdEKwnpU7ncTxUADty0MO5O4/SJcIqn5jYJWgqCccoGMruJqUuhRGG CY+YwM3n+RIYCC/pf/A4O6dul1AOOzHN/1RiKRou836YGODG2Dn4vVpdc6KTrujZIEfjMY6cQd3 VQbootc/qNORQ7rKlR+b7Fbk= X-Received: by 127.0.0.2 with SMTP id JCdOYY7687511xXntVFlKWar; Mon, 11 Dec 2023 14:39:29 -0800 X-Received: from mail-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) by mx.groups.io with SMTP id smtpd.web11.5168.1702334369146750161 for ; Mon, 11 Dec 2023 14:39:29 -0800 X-Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-dbc666461daso2525703276.0 for ; Mon, 11 Dec 2023 14:39:29 -0800 (PST) X-Gm-Message-State: pnvSZR5MIYcdV0eAAi5q6abzx7686176AA= X-Google-Smtp-Source: AGHT+IGKh95/Xu38GqtvdTSJaEph15yS7nkLF7QSbCuQ2VD0YUyTeJyGmAtlh4v4/7/7vs1vEPL2ug== X-Received: by 2002:a25:6902:0:b0:db5:4c2c:f941 with SMTP id e2-20020a256902000000b00db54c2cf941mr3900349ybc.10.1702334367930; Mon, 11 Dec 2023 14:39:27 -0800 (PST) X-Received: from aturtleortwo-benjamindomain.. ([2607:f2c0:e9b1:f59a:bf3c:db4:6459:ea26]) by smtp.gmail.com with ESMTPSA id l7-20020a0cc207000000b0067aa8e67fc5sm3619070qvh.84.2023.12.11.14.39.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 14:39:27 -0800 (PST) From: "Benjamin Doron" To: devel@edk2.groups.io Cc: Guo Dong , Ray Ni , Sean Rhodes , James Lu , Gua Guo Subject: [edk2-devel] [PATCH v2 2/4] UefiPayloadPkg: Introduce coreboot FMAP parser library Date: Mon, 11 Dec 2023 17:39:10 -0500 Message-ID: <20231211223919.1225565-2-benjamin.doron00@gmail.com> In-Reply-To: <20231211223919.1225565-1-benjamin.doron00@gmail.com> References: <20231211223919.1225565-1-benjamin.doron00@gmail.com> MIME-Version: 1.0 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 Reply-To: devel@edk2.groups.io,benjamin.doron00@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=ealENq5H; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Benjamin Doron Parse coreboot FMAP structures to find the memory region in SPI flash by the FMAP area name. Cc: Guo Dong Cc: Ray Ni Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo Signed-off-by: Benjamin Doron --- UefiPayloadPkg/Include/Coreboot.h | 21 ++++ UefiPayloadPkg/Include/Library/FmapParserLib.h | 35 +++++++ UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.c | 104 +++++++++= +++++++++++ UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.inf | 28 ++++++ UefiPayloadPkg/UefiPayloadPkg.dsc | 1 + 5 files changed, 189 insertions(+) diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Cor= eboot.h index 2d454f7c893c..6c33dda9ef85 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -249,6 +249,27 @@ struct cb_cbmem_tab { UINT64 cbmem_tab;=0D };=0D =0D +#define CB_TAG_FMAP 0x0037=0D +#define FMAP_STRLEN 32 /* includes null-terminator */=0D +struct fmap_area {=0D + UINT32 offset; /* offset relative to base */=0D + UINT32 size; /* size in bytes */=0D + UINT8 name[FMAP_STRLEN]; /* descriptive name */=0D + UINT16 flags; /* flags for this area */=0D +} __attribute__ ((packed));=0D +=0D +struct fmap {=0D + UINT8 signature[8]; /* "__FMAP__" (0x5F5F464D41505F5F) */=0D + UINT8 ver_major; /* major version */=0D + UINT8 ver_minor; /* minor version */=0D + UINT64 base; /* address of the firmware binary */=0D + UINT32 size; /* size of firmware binary in bytes */=0D + UINT8 name[FMAP_STRLEN]; /* name of this firmware binary */=0D + UINT16 nareas; /* number of areas described by=0D + fmap_areas[] below */=0D + struct fmap_area areas[];=0D +} __attribute__ ((packed));=0D +=0D /* Helpful macros */=0D =0D #define MEM_RANGE_COUNT(_rec) \=0D diff --git a/UefiPayloadPkg/Include/Library/FmapParserLib.h b/UefiPayloadPk= g/Include/Library/FmapParserLib.h new file mode 100644 index 000000000000..f03b9b22a85b --- /dev/null +++ b/UefiPayloadPkg/Include/Library/FmapParserLib.h @@ -0,0 +1,35 @@ +/** @file=0D + Contains helper functions for parsing FMAP.=0D +=0D + Copyright (c) 2023, 9elements GmbH.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +=0D +#define FMAP_SIGNATURE "__FMAP__"=0D +#define FMAP_VER_MAJOR 1=0D +#define FMAP_VER_MINOR 1=0D +=0D +/**=0D + Find a requested FMAP area's address and size.=0D +=0D + @param[in] FmapAreaName Name string of FMAP area to find=0D + @param[out] FmapAreaAddress Pointer to return of FMAP area memory addr= ess=0D + @param[out] FmapAreaSize Pointer to return of FMAP area size=0D +=0D + @retval EFI_SUCCESS Successfully found the FMAP area informat= ion.=0D + @retval EFI_INVALID_PARAMETER Input arguments are invalid.=0D + @retval EFI_NOT_FOUND Failed to find the FMAP area information.= =0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +FmapLocateArea (=0D + IN CHAR8 *FmapAreaName,=0D + IN OUT EFI_PHYSICAL_ADDRESS *FmapAreaAddress,=0D + IN OUT UINT32 *FmapAreaSize=0D + );=0D diff --git a/UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.c b/Uef= iPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.c new file mode 100644 index 000000000000..1a181b7266f0 --- /dev/null +++ b/UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.c @@ -0,0 +1,104 @@ +/** @file=0D + Contains helper functions for parsing FMAP.=0D +=0D + Copyright (c) 2023, 9elements GmbH.
=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + Find coreboot record with given Tag.=0D + NOTE: This coreboot-specific function definition is absent=0D + from the common BlParseLib header.=0D +=0D + @param Tag The tag id to be found=0D +=0D + @retval NULL The Tag is not found.=0D + @retval Others The pointer to the record found.=0D +=0D +**/=0D +VOID *=0D +FindCbTag (=0D + IN UINT32 Tag=0D + );=0D +=0D +/**=0D + Find a requested FMAP area's address and size.=0D +=0D + @param[in] FmapAreaName Name string of FMAP area to find=0D + @param[out] FmapAreaAddress Pointer to return of FMAP area memory addr= ess=0D + @param[out] FmapAreaSize Pointer to return of FMAP area size=0D +=0D + @retval EFI_SUCCESS Successfully found the FMAP area informat= ion.=0D + @retval EFI_INVALID_PARAMETER Input arguments are invalid.=0D + @retval EFI_NOT_FOUND Failed to find the FMAP area information.= =0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +FmapLocateArea (=0D + IN CHAR8 *FmapAreaName,=0D + IN OUT EFI_PHYSICAL_ADDRESS *FmapAreaAddress,=0D + IN OUT UINT32 *FmapAreaSize=0D + )=0D +{=0D + struct cb_cbmem_ref *CbMemRef;=0D + struct fmap *Fmap;=0D + UINTN Index;=0D +=0D + //=0D + // Perform basic validation=0D + //=0D + if ((FmapAreaName =3D=3D NULL) || (FmapAreaAddress =3D=3D NULL) || (Fmap= AreaSize =3D=3D NULL)) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // FMAP_STRLEN includes sizeof NULL-terminator, so this also too long=0D + if (AsciiStrLen (FmapAreaName) >=3D FMAP_STRLEN) {=0D + return EFI_INVALID_PARAMETER;=0D + }=0D +=0D + // The coreboot table contains large structures as references=0D + CbMemRef =3D FindCbTag (CB_TAG_FMAP);=0D + if (CbMemRef =3D=3D NULL) {=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + Fmap =3D (VOID *)(UINTN)CbMemRef->cbmem_addr; // Support PEI and DXE=0D + if (Fmap =3D=3D NULL) {=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + //=0D + // Perform FMAP validation=0D + //=0D + if ((AsciiStrnCmp ((CHAR8 *)Fmap->signature, FMAP_SIGNATURE, 8) !=3D 0)= =0D + || (Fmap->ver_major !=3D FMAP_VER_MAJOR) || (Fmap->ver_minor !=3D FM= AP_VER_MINOR)) {=0D + return EFI_NOT_FOUND;=0D + }=0D +=0D + for (Index =3D 0; Index < Fmap->nareas; Index++) {=0D + if (AsciiStrCmp ((CHAR8 *)Fmap->areas[Index].name, FmapAreaName) =3D= =3D 0) {=0D + DEBUG ((=0D + DEBUG_INFO,=0D + "FMAP: Found area \"%a\" at offset 0x%x (size 0x%0x)\n",=0D + FmapAreaName,=0D + Fmap->areas[Index].offset,=0D + Fmap->areas[Index].size=0D + ));=0D +=0D + *FmapAreaAddress =3D Fmap->base + Fmap->areas[Index].offset;=0D + *FmapAreaSize =3D Fmap->areas[Index].size;=0D + return EFI_SUCCESS;=0D + }=0D + }=0D +=0D + return EFI_NOT_FOUND;=0D +}=0D diff --git a/UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.inf b/U= efiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.inf new file mode 100644 index 000000000000..8e64a67614d9 --- /dev/null +++ b/UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.inf @@ -0,0 +1,28 @@ +## @file=0D +# Contains helper functions for parsing FMAP.=0D +#=0D +# Copyright (c) 2023, 9elements GmbH.
=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x00010005=0D + BASE_NAME =3D CbFmapParserLib=0D + FILE_GUID =3D 6B4EE7FA-413A-445A-91B2-E63F3106FDE3= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D FmapParserLib=0D +=0D +[Sources]=0D + CbFmapParserLib.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + MdeModulePkg/MdeModulePkg.dec=0D + UefiPayloadPkg/UefiPayloadPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + BlParseLib=0D + DebugLib=0D diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 2dbd875f377a..35e2f85a2ba8 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -280,6 +280,7 @@ !if $(UNIVERSAL_PAYLOAD) =3D=3D FALSE=0D !if $(BOOTLOADER) =3D=3D "COREBOOT"=0D BlParseLib|UefiPayloadPkg/Library/CbParseLib/CbParseLib.inf=0D + FmapParserLib|UefiPayloadPkg/Library/CbFmapParserLib/CbFmapParserLib.i= nf=0D !else=0D BlParseLib|UefiPayloadPkg/Library/SblParseLib/SblParseLib.inf=0D !endif=0D --=20 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#112337): https://edk2.groups.io/g/devel/message/112337 Mute This Topic: https://groups.io/mt/103119570/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-