From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110649+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110649+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031839; cv=none; d=zohomail.com; s=zohoarc; b=j3lFY2zIrgDiSWUT+QyQ7fHTRiYJZ3/zZM7fXOaP7zlTV0EdlVZ551X1+/knNo8FWVBSuY/XHbrg4tFYDlDDuKOprQb4jKnbZfA6eTZfT4rRbF6V0Xhm9/Hnd/uOSL43/QV4D+cdpE5pEJIfpAMIq3kW/FK3MkG8U99I5vKvDdI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031839; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=W3iJ/DAKgAPj6MtEu6UkM/ssgeu6qQ3Jbcg4WxsdMFs=; b=nJo/x3C80lW7WY60ygcTAbqhYtEHdLrPaGcn8nVg9CSAqXfdpZfZnFmyr9iKCZJMF+3tzQvuk5HF7C/w/XBkPLZSPK4C7mvw0ICxCxwTtTt+9mVVn+ExkzmMtF9SSAb7WgkHQqrz3gi+AB+XdGlFpkDKzSMEERtb07hYOW4ds4U= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110649+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 169903183989352.876133495963245; Fri, 3 Nov 2023 10:17:19 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=UXnffKRDLck8hRcKGucBgvW3+pKR91z4RmekpqB1kAc=; 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=1699031839; v=1; b=OOIJKLTBz2CUt0j5T9BmyqpKH5BsGujygYFXXBKOK3g3Bk0pOMEb+L/DWFspUTEN+WAd5Bu8 ETQRs4XlNbddH7qeXhLKXEnDz80K5luTDsrtLopvEzg9kHnho8R38p36bvgSa929MVpVx6SES3e KhoQo3f0RUTPFr8LZ1bmsBO4= X-Received: by 127.0.0.2 with SMTP id qO7zYY1788612xgi9nqI0ggg; Fri, 03 Nov 2023 10:17:19 -0700 X-Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by mx.groups.io with SMTP id smtpd.web10.59748.1699031839019533874 for ; Fri, 03 Nov 2023 10:17:19 -0700 X-Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1cc4f777ab9so19224615ad.0 for ; Fri, 03 Nov 2023 10:17:18 -0700 (PDT) X-Gm-Message-State: PpFuIxTnw6roqeXsIQzU8INmx1787277AA= X-Google-Smtp-Source: AGHT+IGKZpMQKseZetgIOepbeFSBA4beyA1MsmvrYApu6C/g4G21rO7UlPvb5hKwpl70Pfix7LddGg== X-Received: by 2002:a17:902:f305:b0:1c4:44a0:5c03 with SMTP id c5-20020a170902f30500b001c444a05c03mr16773687ple.9.1699031838117; Fri, 03 Nov 2023 10:17:18 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:17 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 01/14] MdeModulePkg: Add ImagePropertiesRecordLib Date: Fri, 3 Nov 2023 10:16:53 -0700 Message-ID: <20231103171706.148-2-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031841021100002 Content-Type: text/plain; charset="utf-8" Create a library for manipulating image properties records. The library is currently blank and will be filled in a future patch to help with reviewer readability. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c = | 9 +++++++ MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h = | 14 +++++++++++ MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf= | 25 ++++++++++++++++++++ MdeModulePkg/MdeModulePkg.dec = | 5 ++++ MdeModulePkg/MdeModulePkg.dsc = | 2 ++ 5 files changed, 55 insertions(+) diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c new file mode 100644 index 000000000000..df7c54ebb793 --- /dev/null +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -0,0 +1,9 @@ +/** @file + + Provides definitions and functionality for manipulating IMAGE_PROPERTIES= _RECORD. + + Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h new file mode 100644 index 000000000000..728008a2e5bb --- /dev/null +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -0,0 +1,14 @@ +/** @file + + Provides definitions and functionality for manipulating IMAGE_PROPERTIES= _RECORD. + + Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+ Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef IMAGE_PROPERTIES_RECORD_SUPPORT_LIB_H_ +#define IMAGE_PROPERTIES_RECORD_SUPPORT_LIB_H_ + +#endif diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.inf b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= sRecordLib.inf new file mode 100644 index 000000000000..b7e493056889 --- /dev/null +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf @@ -0,0 +1,25 @@ +## @file +# Provides definitions and functionality for manipulating +# IMAGE_PROPERTIES_RECORD. +# +# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D ImagePropertiesRecordLib + FILE_GUID =3D 5CCA36C1-C430-4A90-8BF7-23D2719D5928 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D ImagePropertiesRecordLib + +[Sources.common] + ImagePropertiesRecordLib.c + + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index d2fede4f87c0..1a162e97e6fb 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -164,6 +164,11 @@ [LibraryClasses] # VariableFlashInfoLib|Include/Library/VariableFlashInfoLib.h =20 + ## @libraryclass Memory Attribute Table support logic for tracking an= d reporting + # runtime images + # + ImagePropertiesRecordLib|Include/Library/ImagePropertiesRecordLib.h + [Guids] ## MdeModule package token space guid # Include/Guid/MdeModulePkgTokenSpace.h diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc index c7af921782ee..6b3052ff4614 100644 --- a/MdeModulePkg/MdeModulePkg.dsc +++ b/MdeModulePkg/MdeModulePkg.dsc @@ -96,6 +96,7 @@ [LibraryClasses] VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf NonDiscoverableDeviceRegistrationLib|MdeModulePkg/Library/NonDiscoverabl= eDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 FmpAuthenticationLib|MdeModulePkg/Library/FmpAuthenticationLibNull/FmpAu= thenticationLibNull.inf CapsuleLib|MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf @@ -237,6 +238,7 @@ [Components] MdeModulePkg/Library/BaseHobLibNull/BaseHobLibNull.inf MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocationLib= Null.inf MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf + MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.i= nf =20 MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf MdeModulePkg/Bus/Pci/PciSioSerialDxe/PciSioSerialDxe.inf --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110649): https://edk2.groups.io/g/devel/message/110649 Mute This Topic: https://groups.io/mt/102368840/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110650+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110650+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031841; cv=none; d=zohomail.com; s=zohoarc; b=WpnrYvyywp6xZb1cyz5ArAwIF7gs9aB4dnFGnqzfIOIVBCVqDxolwEsCS3LTuKmBeXzQld/MIU0+Meet8cVtWti8i24fjk0rwTOi+oRznJe6UZShmq59nVN/FpKwlrURCsZPhRun9kuVsxgOMMON52HwaihTgecVk/iT+TmTALE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031841; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=6laySC3IRCuAfcel4Xwr7PATR+DX0kQRPZu2OnpKxdk=; b=PsyOBPfY1Ygjs/R4/JELK1tLjw3eDxTcrOcnPowzvozB9QlsFPYAMUzXe44S4GUuvvCDAN/spf/coWRCiEG3JeAHJk9U1+cs8RrMKOwnlotc76WHu01Ax7hl1/lJxvqJXClGocpYdKDLDyRjYp8JCjF6wrDlLMLuk4GpeMtOP1g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110650+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031841231907.4227624382116; Fri, 3 Nov 2023 10:17:21 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=6y1XPkEZAgXWYf0RKFU7bjCORBYynvWtOHDKg+UImwI=; 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=1699031840; v=1; b=IU3qjfCD12Hux+v2w/Ft4hDATHB/+DRlLNC5SkPrS157kmiYeJMU+vWPeBk3n6lBhmW8Zj72 OySQfPOA6TMEM0N6hxeML9u5rBGH3it15N7yNDYLckYKAtl4aSNOnYyDHAMeboDNlLE3rxNKLka vDWXg8TjbUBxPjy9WdOidzWQ= X-Received: by 127.0.0.2 with SMTP id 8TS1YY1788612xJr5P7BYaYL; Fri, 03 Nov 2023 10:17:20 -0700 X-Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) by mx.groups.io with SMTP id smtpd.web10.59749.1699031839456101832 for ; Fri, 03 Nov 2023 10:17:19 -0700 X-Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-1c9b7c234a7so20902465ad.3 for ; Fri, 03 Nov 2023 10:17:19 -0700 (PDT) X-Gm-Message-State: WaNQ1D3P8WrYPoBQChzGEKpXx1787277AA= X-Google-Smtp-Source: AGHT+IHqb738t5B/JaqwHfCwyMpTl77FSGKh24G/C1tqR6ct2oNAgkuEupHHQWrKCKQoocrdxGCdWg== X-Received: by 2002:a17:902:f550:b0:1cc:686a:4120 with SMTP id h16-20020a170902f55000b001cc686a4120mr13077567plf.55.1699031838776; Fri, 03 Nov 2023 10:17:18 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:18 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Leif Lindholm , Sami Mujawar , Gerd Hoffmann , Michael D Kinney Subject: [edk2-devel] [PATCH v4 02/14] ArmVirtPkg: Add ImagePropertiesRecordLib Instance Date: Fri, 3 Nov 2023 10:16:54 -0700 Message-ID: <20231103171706.148-3-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031842905100013 Content-Type: text/plain; charset="utf-8" Add an instance of ImagePropertiesRecordLib which will be used by the DXE Core. Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Sami Mujawar Cc: Gerd Hoffmann Signed-off-by: Taylor Beebe Reviewed-by: Michael D Kinney --- ArmVirtPkg/ArmVirt.dsc.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/ArmVirtPkg/ArmVirt.dsc.inc b/ArmVirtPkg/ArmVirt.dsc.inc index fe6488ee9910..6bc72f1decbd 100644 --- a/ArmVirtPkg/ArmVirt.dsc.inc +++ b/ArmVirtPkg/ArmVirt.dsc.inc @@ -52,6 +52,7 @@ [LibraryClasses.common] IoLib|MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf UefiDecompressLib|MdePkg/Library/BaseUefiDecompressLib/BaseUefiDecompres= sLib.inf CpuLib|MdePkg/Library/BaseCpuLib/BaseCpuLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 UefiLib|MdePkg/Library/UefiLib/UefiLib.inf HobLib|ArmVirtPkg/Library/ArmVirtDxeHobLib/ArmVirtDxeHobLib.inf --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110650): https://edk2.groups.io/g/devel/message/110650 Mute This Topic: https://groups.io/mt/102368841/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110651+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110651+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031841; cv=none; d=zohomail.com; s=zohoarc; b=BSJSqQicOofIHmsKIBk2ty4v1guuH8V5p1I0ucCkXgTj66qb32vlIrITNW6PxkuhqrMYy34hHgZGCJhEHkOR+BJ25jVAZnzuuo9dE5U6azWM0y1I6BBOg31qm8jnOEI4i9xV/MllClhJMB7cjRpxl4wj9I4kImFUHfm+xl9WXfA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031841; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=nx3gpe6eBhpB5QgNpFqREibh7KFjLiWSFVRtX4APIjM=; b=ftmqe2+qBGr7ypWqlUwoND5I/f7UNBlhlgRNe1u9b5MdxvM6TVd6aWPDyhzNIz7vQxCfXlqsnBHsoDqMIioytO2atU+ab/8ZMybtxGpYRZKFqe/nkqHy6CCwWnWCdFrp/vmuyZAk4iIZo801wxXjObon7wB/3Xd0IGxUwySorFQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110651+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 169903184143538.73730478131563; Fri, 3 Nov 2023 10:17:21 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=Uz+AVCljzmiAxSkN+TbnCgPmzOgGQU74ltgB4Yx0768=; 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=1699031841; v=1; b=OlwFbRb6vSdBgvS7PjpBnIhAjezbfEalOyNrp5diWxxKbNYfa/JQVbImJwAm2cnX1RaYacaT BFJxBU67W+nuQNrngiNFuRfAqPq5afKAy86Xb5k1XyFsb8qUDbSbonoh4B2L9ZM4DiN0PXIAKgd JgsmTfxwpdzvJGO48sQZ3T7Q= X-Received: by 127.0.0.2 with SMTP id ZsCtYY1788612xaRgYS5HYmY; Fri, 03 Nov 2023 10:17:21 -0700 X-Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by mx.groups.io with SMTP id smtpd.web10.59750.1699031839882408779 for ; Fri, 03 Nov 2023 10:17:19 -0700 X-Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1cc5b7057d5so20874815ad.2 for ; Fri, 03 Nov 2023 10:17:19 -0700 (PDT) X-Gm-Message-State: Y5b1wwqpQ3pWxDdVl1WRH7EMx1787277AA= X-Google-Smtp-Source: AGHT+IF+Iz9BW+kUstIaSXUZ9jqGaPgF7d7ZMMg6MrjjrP36I8HNT2hgUEaeiLCVa2rlnfewz37ClA== X-Received: by 2002:a17:902:b593:b0:1cc:ab06:e6c8 with SMTP id a19-20020a170902b59300b001ccab06e6c8mr1499031pls.19.1699031839211; Fri, 03 Nov 2023 10:17:19 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:19 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Andrew Fish , Ray Ni , Michael D Kinney Subject: [edk2-devel] [PATCH v4 03/14] EmulatorPkg: Add ImagePropertiesRecordLib Instance Date: Fri, 3 Nov 2023 10:16:55 -0700 Message-ID: <20231103171706.148-4-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031842901100012 Content-Type: text/plain; charset="utf-8" Add an instance of ImagePropertiesRecordLib which will be used by the DXE Core. Cc: Andrew Fish Cc: Ray Ni Signed-off-by: Taylor Beebe Reviewed-by: Michael D Kinney --- EmulatorPkg/EmulatorPkg.dsc | 1 + 1 file changed, 1 insertion(+) diff --git a/EmulatorPkg/EmulatorPkg.dsc b/EmulatorPkg/EmulatorPkg.dsc index b14654739b54..85134b07816e 100644 --- a/EmulatorPkg/EmulatorPkg.dsc +++ b/EmulatorPkg/EmulatorPkg.dsc @@ -126,6 +126,7 @@ [LibraryClasses] SortLib|MdeModulePkg/Library/BaseSortLib/BaseSortLib.inf ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(SECURE_BOOT_ENABLE) =3D=3D TRUE RngLib|MdeModulePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110651): https://edk2.groups.io/g/devel/message/110651 Mute This Topic: https://groups.io/mt/102368842/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110652+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110652+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031842; cv=none; d=zohomail.com; s=zohoarc; b=H7svG+8OvOn1iVBsd79kKrXc68COW71Od3fJQGg+h+X99egObmNkfWgDD2FGpF6WQYxvFVh+pUVeA5gv29oz/4TnVx2O9i0PbZs+VuV6cUD6gl4YPZb5uvgAsbnQHt50VaknAMibQUhDde9+hGl8+dMEUOpLtlKkUQtpN/+67xI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031842; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=K73+6JTO4WuH+HWInhYW+opqoj7r1irvVNWlthWX6fs=; b=M+bQ/AhEB/1T+1FF4uwNNGtBLzCZFL/eUrOwuTbPMfOQ/S9UndWMxY3f+/1k4DTIlgAFW4WD4hcZdQTc7MQ2Tj6iT+AjWdVFkoidf34J8jnPNUotcjdL5i2b8z1caR1gPCRfhIGvbfxHKcO5wVCBDvHbebYQXxds4eFfrvtsLQE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110652+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031842212132.47306938376994; Fri, 3 Nov 2023 10:17:22 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=leoZDaXXr/8hpvu0bRWKCLxRNmt+1fju0YnZxH3cQto=; 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=1699031841; v=1; b=dYltdt0zJvpUjDyn2FzD2dW3pefoLb3xkXgwzJ9q0GF1aiFRliw2PeG/O10WIykm18qn/RR+ qA7AwI8nP8PJxT021jC3tPPV+wzd5qtyojU0s0SknnxmOaRapMrD85nXtDcwDrjFeX1HnYkGgCf 6tSmkfeNdbyDZXdbPH7hYiTU= X-Received: by 127.0.0.2 with SMTP id c1XtYY1788612xoALIB46hw6; Fri, 03 Nov 2023 10:17:21 -0700 X-Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by mx.groups.io with SMTP id smtpd.web10.59751.1699031840459190153 for ; Fri, 03 Nov 2023 10:17:20 -0700 X-Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1cc1e1e74beso20707925ad.1 for ; Fri, 03 Nov 2023 10:17:20 -0700 (PDT) X-Gm-Message-State: 2aVxgdAjfdQnhjtNwLHKGRaYx1787277AA= X-Google-Smtp-Source: AGHT+IHzAJTlMSJEJVSX6g0npw0ZtZmycsYLfri7mttUgNQkMLm2rECSYVrq6cHHPhisJERU8N7btg== X-Received: by 2002:a17:902:c1cb:b0:1cc:29ed:96ae with SMTP id c11-20020a170902c1cb00b001cc29ed96aemr20348253plc.41.1699031839785; Fri, 03 Nov 2023 10:17:19 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:19 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Michael D Kinney , Jiewen Yao Subject: [edk2-devel] [PATCH v4 04/14] OvmfPkg: Add ImagePropertiesRecordLib Instance Date: Fri, 3 Nov 2023 10:16:56 -0700 Message-ID: <20231103171706.148-5-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031842955100017 Content-Type: text/plain; charset="utf-8" Add an instance of ImagePropertiesRecordLib which will be used by the DXE Core. Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Signed-off-by: Taylor Beebe Reviewed-by: Michael D Kinney Acked-by: Jiewen Yao --- OvmfPkg/AmdSev/AmdSevX64.dsc | 1 + OvmfPkg/Bhyve/BhyveX64.dsc | 1 + OvmfPkg/CloudHv/CloudHvX64.dsc | 1 + OvmfPkg/IntelTdx/IntelTdxX64.dsc | 1 + OvmfPkg/Microvm/MicrovmX64.dsc | 1 + OvmfPkg/OvmfPkgIa32.dsc | 1 + OvmfPkg/OvmfPkgIa32X64.dsc | 1 + OvmfPkg/OvmfPkgX64.dsc | 1 + OvmfPkg/OvmfXen.dsc | 1 + OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc | 1 + 10 files changed, 10 insertions(+) diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index 302c90e7c2b4..3a1f788eb37f 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.dsc +++ b/OvmfPkg/AmdSev/AmdSevX64.dsc @@ -171,6 +171,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc index 6693342c5f6e..8f0cd93a2d6c 100644 --- a/OvmfPkg/Bhyve/BhyveX64.dsc +++ b/OvmfPkg/Bhyve/BhyveX64.dsc @@ -173,6 +173,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltL= ib.inf diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index c23c7eaf6cc2..821ad1b9fab2 100644 --- a/OvmfPkg/CloudHv/CloudHvX64.dsc +++ b/OvmfPkg/CloudHv/CloudHvX64.dsc @@ -182,6 +182,7 @@ [LibraryClasses] MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLi= b.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/IntelTdx/IntelTdxX64.dsc b/OvmfPkg/IntelTdx/IntelTdxX6= 4.dsc index 182ec3705dd3..0ed36daf7c44 100644 --- a/OvmfPkg/IntelTdx/IntelTdxX64.dsc +++ b/OvmfPkg/IntelTdx/IntelTdxX64.dsc @@ -171,6 +171,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/Customize= dDisplayLib.inf diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc index ea1fa3e2963f..8817006f90c4 100644 --- a/OvmfPkg/Microvm/MicrovmX64.dsc +++ b/OvmfPkg/Microvm/MicrovmX64.dsc @@ -185,6 +185,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index ed3a19feebe6..bce2aedcd781 100644 --- a/OvmfPkg/OvmfPkgIa32.dsc +++ b/OvmfPkg/OvmfPkgIa32.dsc @@ -187,6 +187,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 16ca139b2973..631e909a543d 100644 --- a/OvmfPkg/OvmfPkgIa32X64.dsc +++ b/OvmfPkg/OvmfPkgIa32X64.dsc @@ -192,6 +192,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ibNull.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index dc1a0942aa8b..4ea3008cc69a 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -204,6 +204,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(SMM_REQUIRE) =3D=3D FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 0063245b5659..ddaa441cabff 100644 --- a/OvmfPkg/OvmfXen.dsc +++ b/OvmfPkg/OvmfXen.dsc @@ -176,6 +176,7 @@ [LibraryClasses] MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxL= ib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf diff --git a/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc b/OvmfPkg/RiscVVirt/RiscVV= irtQemu.dsc index 34b2037824f1..f8b9479345d7 100644 --- a/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc +++ b/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc @@ -101,6 +101,7 @@ [LibraryClasses.common] PciHostBridgeUtilityLib|OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostB= ridgeUtilityLib.inf PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf PlatformHookLib|MdeModulePkg/Library/BasePlatformHookLibNull/BasePlatfor= mHookLibNull.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf =20 !if $(TPM2_ENABLE) =3D=3D TRUE Tpm2CommandLib|SecurityPkg/Library/Tpm2CommandLib/Tpm2CommandLib.inf --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110652): https://edk2.groups.io/g/devel/message/110652 Mute This Topic: https://groups.io/mt/102368843/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110653+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110653+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031842; cv=none; d=zohomail.com; s=zohoarc; b=ETF3cO0NbiiuvdeSdqhGeAQIi7AGpHdFOeD8GhlfxuS1mWwEaguPDPJ96/Yi2tY2qvGZIodXUiddGfQ68u+YRgecPN8goDyAAiJhcRyCA0v3lCCl34slTVlmhAg/H34tkHrXj9qYTcWtn7zhggcDwUCILfKdNHK9/2RT4GdT5xY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031842; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=4zrqFkq3gKkIcmWDDE9rS2FcZ3kxSVlv7nVgP0o8k3s=; b=Uoz43qfBKc5s1pBCGDxalFapo3V0NbErT9z1hRFrLDGgzpYuFKtfK7BL79CCvVy7YLXv0Z4mH+QNedSx4xge6vEX2p8UiiQly5YekUOyHOuUIeZPot1saFIagLTFTnaRzvYsvg6Z725IrndvZu7x5XCK/CrK5hktXbxvPhVQooQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110653+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031842413866.2813003111347; Fri, 3 Nov 2023 10:17:22 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=b5ux8smahd7aABQKyUkk4Nq4xGtodR2o3mR6NnbaAqU=; 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=1699031842; v=1; b=Im4WEgrRKyV/9LpQjHR+jvPBFkaUL9MCxAA4E5DX5Pj+1iXke4eY3TXxwR+RGLYeDg+bFnOE so0vwn2+1oG8ZeVw8Sa6IqrmOBHTSbNON90bWnlT9zD976Wh3xb6ix/zV6s/KXTXJHGbW4h+SX3 O6vbeE/0Pg3oNqgrhzrVY+LI= X-Received: by 127.0.0.2 with SMTP id cvShYY1788612xmJRkgpGEON; Fri, 03 Nov 2023 10:17:22 -0700 X-Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) by mx.groups.io with SMTP id smtpd.web11.59064.1699031841193196446 for ; Fri, 03 Nov 2023 10:17:21 -0700 X-Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-6c34e87b571so1266225b3a.3 for ; Fri, 03 Nov 2023 10:17:21 -0700 (PDT) X-Gm-Message-State: jNiJSCmcvQ2OqTlqGNy0ygWKx1787277AA= X-Google-Smtp-Source: AGHT+IFM6nEwC4Epbq+4P8KmM1olSSd02m6hL9oIwK1Ld5jGogYZnYHzgSFICI2M0xxcQ4I1NSzNIg== X-Received: by 2002:a05:6a21:1509:b0:174:464:9def with SMTP id nq9-20020a056a21150900b0017404649defmr19303143pzb.62.1699031840418; Fri, 03 Nov 2023 10:17:20 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:20 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Guo Dong , Sean Rhodes , James Lu , Gua Guo , Michael D Kinney Subject: [edk2-devel] [PATCH v4 05/14] UefiPayloadPkg: Add ImagePropertiesRecordLib Instance Date: Fri, 3 Nov 2023 10:16:57 -0700 Message-ID: <20231103171706.148-6-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031842897100011 Content-Type: text/plain; charset="utf-8" Add an instance of ImagePropertiesRecordLib which will be used by the DXE Core. Cc: Guo Dong Cc: Sean Rhodes Cc: James Lu Cc: Gua Guo Signed-off-by: Taylor Beebe Reviewed-by: Michael D Kinney Reviewed-by: Gua Guo --- UefiPayloadPkg/UefiPayloadPkg.dsc | 1 + 1 file changed, 1 insertion(+) diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index af9308ef8ed7..a65f9d5b831a 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -284,6 +284,7 @@ [LibraryClasses] # DebugPrintErrorLevelLib|UefiPayloadPkg/Library/DebugPrintErrorLevelLibHo= b/DebugPrintErrorLevelLibHob.inf PerformanceLib|MdePkg/Library/BasePerformanceLibNull/BasePerformanceLibN= ull.inf + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/I= magePropertiesRecordLib.inf !if $(SOURCE_DEBUG_ENABLE) =3D=3D TRUE PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDeb= ug/PeCoffExtraActionLibDebug.inf DebugCommunicationLib|SourceLevelDebugPkg/Library/DebugCommunicationLibS= erialPort/DebugCommunicationLibSerialPort.inf --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110653): https://edk2.groups.io/g/devel/message/110653 Mute This Topic: https://groups.io/mt/102368845/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110654+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110654+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031842; cv=none; d=zohomail.com; s=zohoarc; b=YORXB26J9paLGEcSULHqQ5yUkkqnOu6988D9bVT6fysdNe/2x+BqU51IwpGlUTP2Hxk/URvUsPZ5MTigv4m/N3if/RJDUpq6mJEa6qqZirZUZNRM8z1OYWxbCzxaiJ/gqRgd8Cf34MZjWasAAZZmZdAJFGVmgmwaQEGE/sJEHgQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031842; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=/b65t1iEdee/7YnLE8fxDnSs6XaLOCNUAv4E1B3Lmnc=; b=hQuVnA+Wv9wg+Ar5TM5xa+swwHWbOPL9xePTdU0GsmesiL+goI8LYXDendeJHVmHwbczTgw6hiOqsKdzeQ0oQevVtmrHxVk2NMYmP7rnNd8+3FXJdzyG1gjhx6DShSUnQUNf0/UlZjXaPStO5btS9QBnIkWD7K/fQmPO1SaXvP0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110654+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031842895675.4479450699303; Fri, 3 Nov 2023 10:17:22 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=q+nT++fj3oOwJFcxeKLShOgTgjhXiouOmfh8Nhp//S8=; 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=1699031842; v=1; b=FB5agH8ETujNB8ji6ji+OlRb4Z0pMY9ozwiqOBXEhEXbLU+BcYb7s+6hLFyQ4qkljEmDWX9g bQ6QXiQ+vgsZLlmRcCE8sc+BT7GQD2yLOAA+x7CJVhLSae5djHH5cGjwmu7nkDAfqktks0ztdTG qz20K96mHEAMgw2boon+lP+g= X-Received: by 127.0.0.2 with SMTP id Ae1lYY1788612xx3cQ24WRAL; Fri, 03 Nov 2023 10:17:22 -0700 X-Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) by mx.groups.io with SMTP id smtpd.web11.59066.1699031841752080475 for ; Fri, 03 Nov 2023 10:17:21 -0700 X-Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1cc3542e328so18126535ad.1 for ; Fri, 03 Nov 2023 10:17:21 -0700 (PDT) X-Gm-Message-State: I6xFFEswUcjmAz8cvIInNR0yx1787277AA= X-Google-Smtp-Source: AGHT+IF1r9joLJzdIlJvD85iMg1zoyoobpZ2v5JsJnANjSvgbMZ69V/Vcp/Zdg5g5YGUwWi32Cq7pQ== X-Received: by 2002:a17:903:104a:b0:1c9:d948:33ea with SMTP id f10-20020a170903104a00b001c9d94833eamr15475729plc.21.1699031841067; Fri, 03 Nov 2023 10:17:21 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:20 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 06/14] MdeModulePkg: Update MemoryAttributesTable.c to Reduce Global Variable Use Date: Fri, 3 Nov 2023 10:16:58 -0700 Message-ID: <20231103171706.148-7-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031844913100025 Content-Type: text/plain; charset="utf-8" This patch updates MemoryAttributesTable.c to reduce reliance on global variables and allow some logic to move to a library. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c | 102 +++++++++++------= --- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index fd127ee167e1..64b0aa1ff5e5 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -541,8 +541,9 @@ EnforceMemoryMapAttribute ( /** Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. =20 - @param Buffer Start Address - @param Length Address length + @param Buffer Start Address + @param Length Address length + @param ImageRecordList Image record list =20 @return first image record covered by [buffer, length] **/ @@ -550,14 +551,12 @@ STATIC IMAGE_PROPERTIES_RECORD * GetImageRecordByAddress ( IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length + IN UINT64 Length, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 for (ImageRecordLink =3D ImageRecordList->ForwardLink; ImageRecordLink !=3D ImageRecordList; @@ -692,7 +691,8 @@ SetNewRecord ( STATIC UINTN GetMaxSplitRecordCount ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; @@ -705,7 +705,7 @@ GetMaxSplitRecordCount ( PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize (OldRecor= d->NumberOfPages); =20 do { - ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart); + ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart, ImageRecordList); if (ImageRecord =3D=3D NULL) { break; } @@ -725,13 +725,16 @@ GetMaxSplitRecordCount ( Split the memory map to new entries, according to one old entry, based upon PE code section and data section. =20 - @param OldRecord A pointer to one old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param MaxSplitRecordCount The max number of splitted entries - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param OldRecord A pointer to one old memory map entr= y. + @param NewRecord A pointer to several new memory map = entries. + The caller gurantee the buffer size = be 1 + + (SplitRecordCount * DescriptorSize) = calculated + below. + @param MaxSplitRecordCount The max number of splitted entries + @param DescriptorSize Size, in bytes, of an individual EFI= _MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD en= tries used when searching + for an image record contained by the= memory range described in + the existing EFI memory map descript= or OldRecord =20 @retval 0 no entry is splitted. @return the real number of splitted record. @@ -742,7 +745,8 @@ SplitRecord ( IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, IN UINTN MaxSplitRecordCount, - IN UINTN DescriptorSize + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList ) { EFI_MEMORY_DESCRIPTOR TempRecord; @@ -770,7 +774,7 @@ SplitRecord ( =20 ImageRecord =3D NULL; do { - NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart); + NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); if (NewImageRecord =3D=3D NULL) { // // No more image covered by this range, stop @@ -867,23 +871,29 @@ SplitRecord ( | Record Y | +---------------+ =20 - @param MemoryMapSize A pointer to the size, in bytes, of the - MemoryMap buffer. On input, this is the s= ize of - old MemoryMap before split. The actual bu= ffer - size of MemoryMap is MemoryMapSize + - (AdditionalRecordCount * DescriptorSize) = calculated - below. On output, it is the size of new M= emoryMap - after split. - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param MemoryMapSize A pointer to the size, in bytes,= of the + MemoryMap buffer. On input, this= is the size of + old MemoryMap before split. The = actual buffer + size of MemoryMap is MemoryMapSi= ze + + (AdditionalRecordCount * Descrip= torSize) calculated + below. On output, it is the size= of new MemoryMap + after split. + @param MemoryMap A pointer to the buffer in which= firmware places + the current memory map. + @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching + for an image record contained by= the memory range described in + EFI memory map descriptors. + @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. **/ STATIC VOID SplitTable ( IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN DescriptorSize + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList, + IN UINTN NumberOfAdditionalDescriptors ) { INTN IndexOld; @@ -891,9 +901,6 @@ SplitTable ( UINTN MaxSplitRecordCount; UINTN RealSplitRecordCount; UINTN TotalSplitRecordCount; - UINTN AdditionalRecordCount; - - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; =20 TotalSplitRecordCount =3D 0; // @@ -903,9 +910,9 @@ SplitTable ( // // Let new record point to end of full MemoryMap buffer. // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordC= ount; + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; for ( ; IndexOld >=3D 0; IndexOld--) { - MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); + MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); // // Split this MemoryMap record // @@ -914,7 +921,8 @@ SplitTable ( (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), MaxSplitRecordCount, - DescriptorSize + DescriptorSize, + ImageRecordList ); // // Adjust IndexNew according to real split. @@ -934,7 +942,7 @@ SplitTable ( // CopyMem ( MemoryMap, - (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) *= DescriptorSize, + (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize ); =20 @@ -1035,7 +1043,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( // // Split PE code/data // - SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); + SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImageProper= tiesPrivateData.ImageRecordList, AdditionalRecordCount); } } =20 @@ -1233,11 +1241,13 @@ SwapImageRecord ( =20 /** Sort image record based upon the ImageBase from low to high. + + @param ImageRecordList Image record list to be sorted **/ STATIC VOID SortImageRecord ( - VOID + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; @@ -1245,9 +1255,6 @@ SortImageRecord ( LIST_ENTRY *ImageRecordLink; LIST_ENTRY *NextImageRecordLink; LIST_ENTRY *ImageRecordEndLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 ImageRecordLink =3D ImageRecordList->ForwardLink; NextImageRecordLink =3D ImageRecordLink->ForwardLink; @@ -1456,7 +1463,7 @@ InsertImageRecord ( mImagePropertiesPrivateData.CodeSegmentCountMax =3D ImageRecord->CodeS= egmentCount; } =20 - SortImageRecord (); + SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: return; @@ -1465,8 +1472,9 @@ Finish: /** Find image record according to image base and size. =20 - @param ImageBase Base of PE image - @param ImageSize Size of PE image + @param ImageBase Base of PE image + @param ImageSize Size of PE image + @param ImageRecordList Image record list to be searched =20 @return image record **/ @@ -1474,14 +1482,12 @@ STATIC IMAGE_PROPERTIES_RECORD * FindImageRecord ( IN EFI_PHYSICAL_ADDRESS ImageBase, - IN UINT64 ImageSize + IN UINT64 ImageSize, + IN LIST_ENTRY *ImageRecordList ) { IMAGE_PROPERTIES_RECORD *ImageRecord; LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; =20 for (ImageRecordLink =3D ImageRecordList->ForwardLink; ImageRecordLink !=3D ImageRecordList; @@ -1526,7 +1532,7 @@ RemoveImageRecord ( return; } =20 - ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize); + ImageRecord =3D FindImageRecord ((EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeIma= ge->ImageBase, RuntimeImage->ImageSize, &mImagePropertiesPrivateData.ImageR= ecordList); if (ImageRecord =3D=3D NULL) { DEBUG ((DEBUG_ERROR, "!!!!!!!! ImageRecord not found !!!!!!!!\n")); return; --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110654): https://edk2.groups.io/g/devel/message/110654 Mute This Topic: https://groups.io/mt/102368846/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110655+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110655+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031844; cv=none; d=zohomail.com; s=zohoarc; b=mijn4k/kCb2WWnrZYGfWxDeqAiCBqpFY+gPiU+sRfYzd066sc062A7lja6CpgI3dHl2pMl+OYyFl2VFk7fULsX30UE852cgEYYdWDDqC1VjkqbxkHAmk7sZD3HtvuQW+wnUeqMtXI5b0zsxLKaFW6zK2AT/gaAaIfkLwVg/rfXg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031844; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=GoYJQYWe6BBezx4sXtiRYtC/iYzvC5e7155cFlIq/KE=; b=Ch7FxcLTzDB5f0DsMQSsM4RKlMYf6NRC4Pt/lekODBmP92hMmrapcYAt8GE/7nbH41OrQgw3Ls4zUc8KQhJY5IrVBxF7v20fsk4jiw518LMoL2DuDSI3GUhFIU9N3hkd9DpUkymL0QjTJivnSLRkqGvSJ6xTnjzVi8poenXN9io= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110655+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031844915411.2726164553708; Fri, 3 Nov 2023 10:17:24 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=hoFPsiaXIZdii2wNQvzTBvNVVcnZCGyuS+88weIJxAA=; 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=1699031843; v=1; b=tG8LC7LduA6+aOREw5OrG1cHpEIY17ZchdWWkqAMuP57TeHvgPCKB7cmRNENd02bWupejuJt 0KLQ0EnYxz1c2WT12p027TQtI1HFfczh81/fHVJg1IVwXSS5fiYTUzOvLCU/eTtEoVfbBWwLnPJ MEuyJ8F7eFHAQcJcYgK1xx8k= X-Received: by 127.0.0.2 with SMTP id JPTpYY1788612xhp4g2Ye0A5; Fri, 03 Nov 2023 10:17:23 -0700 X-Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mx.groups.io with SMTP id smtpd.web10.59753.1699031842827983024 for ; Fri, 03 Nov 2023 10:17:22 -0700 X-Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1cc5b705769so20787635ad.0 for ; Fri, 03 Nov 2023 10:17:22 -0700 (PDT) X-Gm-Message-State: ZgD96XHA4ZuvXFWlPyAQ9tHUx1787277AA= X-Google-Smtp-Source: AGHT+IEpy+cnncXpxj4/q6vOMygd4zD5/GpZgUrUL0dAtNCdDlJkaG/9uNIKWh/LBNbVhYGiJdQ29Q== X-Received: by 2002:a17:902:dacc:b0:1cc:4677:2f0e with SMTP id q12-20020a170902dacc00b001cc46772f0emr20545979plx.45.1699031841844; Fri, 03 Nov 2023 10:17:21 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:21 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 07/14] MdeModulePkg: Move Some DXE MAT Logic to ImagePropertiesRecordLib Date: Fri, 3 Nov 2023 10:16:59 -0700 Message-ID: <20231103171706.148-8-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031847077100031 Content-Type: text/plain; charset="utf-8" Move some DXE MAT logic to ImagePropertiesRecordLib to consolidate code and enable unit testability. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c = | 774 +------------------- MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c = | 24 +- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c = | 770 +++++++++++++++++++ MdeModulePkg/Core/Dxe/DxeMain.h = | 20 - MdeModulePkg/Core/Dxe/DxeMain.inf = | 1 + MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h = | 159 ++++ MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf= | 4 + 7 files changed, 947 insertions(+), 805 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 64b0aa1ff5e5..51630f504ea1 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -14,6 +14,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include =20 #include =20 @@ -333,45 +334,6 @@ CoreInitializeMemoryAttributesTable ( // Below functions are for MemoryMap // =20 -/** - Converts a number of EFI_PAGEs to a size in bytes. - - NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. - - @param Pages The number of EFI_PAGES. - - @return The number of bytes associated with the number of EFI_PAGEs spe= cified - by Pages. -**/ -STATIC -UINT64 -EfiPagesToSize ( - IN UINT64 Pages - ) -{ - return LShiftU64 (Pages, EFI_PAGE_SHIFT); -} - -/** - Converts a size, in bytes, to a number of EFI_PAGESs. - - NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. - - @param Size A size in bytes. - - @return The number of EFI_PAGESs associated with the number of bytes sp= ecified - by Size. - -**/ -STATIC -UINT64 -EfiSizeToPages ( - IN UINT64 Size - ) -{ - return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0); -} - /** Acquire memory lock on mMemoryAttributesTableLock. **/ @@ -396,48 +358,6 @@ CoreReleasemMemoryAttributesTableLock ( CoreReleaseLock (&mMemoryAttributesTableLock); } =20 -/** - Sort memory map entries based upon PhysicalStart, from low to high. - - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -VOID -SortMemoryMap ( - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN MemoryMapSize, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; - EFI_MEMORY_DESCRIPTOR TempMemoryMap; - - MemoryMapEntry =3D MemoryMap; - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descripto= rSize); - MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + Me= moryMapSize); - while (MemoryMapEntry < MemoryMapEnd) { - while (NextMemoryMapEntry < MemoryMapEnd) { - if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStar= t) { - CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof (EFI_MEMORY_DESCRI= PTOR)); - CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof (EFI_MEMORY_DE= SCRIPTOR)); - CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof (EFI_MEMORY_DE= SCRIPTOR)); - } - - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, D= escriptorSize); - } - - MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); - } - - return; -} - /** Merge continous memory map entries whose have same attributes. =20 @@ -471,7 +391,7 @@ MergeMemoryMap ( =20 do { MergeGuardPages (NewMemoryMapEntry, NextMemoryMapEntry->PhysicalStar= t); - MemoryBlockLength =3D (UINT64)(EfiPagesToSize (NewMemoryMapEntry->Nu= mberOfPages)); + MemoryBlockLength =3D LShiftU64 (NewMemoryMapEntry->NumberOfPages, E= FI_PAGE_SHIFT); if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) && (NewMemoryMapEntry->Type =3D=3D NextMemoryMapEntry->Type) && (NewMemoryMapEntry->Attribute =3D=3D NextMemoryMapEntry->Attribu= te) && @@ -538,434 +458,6 @@ EnforceMemoryMapAttribute ( return; } =20 -/** - Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. - - @param Buffer Start Address - @param Length Address length - @param ImageRecordList Image record list - - @return first image record covered by [buffer, length] -**/ -STATIC -IMAGE_PROPERTIES_RECORD * -GetImageRecordByAddress ( - IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length, - IN LIST_ENTRY *ImageRecordList - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink) - { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - - if ((Buffer <=3D ImageRecord->ImageBase) && - (Buffer + Length >=3D ImageRecord->ImageBase + ImageRecord->ImageS= ize)) - { - return ImageRecord; - } - } - - return NULL; -} - -/** - Set the memory map to new entries, according to one old entry, - based upon PE code section and data section in image record - - @param ImageRecord An image record whose [ImageBase, ImageSi= ze] covered - by old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param OldRecord A pointer to one old memory map entry. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. -**/ -STATIC -UINTN -SetNewRecord ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - UINTN NewRecordCount; - UINT64 PhysicalEnd; - UINT64 ImageEnd; - - CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord= .NumberOfPages); - NewRecordCount =3D 0; - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - - if (TempRecord.PhysicalStart <=3D ImageRecordCodeSection->CodeSegmentB= ase) { - // - // DATA - // - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentBase - NewRecord->PhysicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount++; - } - - // - // CODE - // - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentSize); - NewRecord->Attribute =3D (TempRecord.Attribute & (~EFI_MEMORY_XP= )) | EFI_MEMORY_RO; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount++; - } - - TempRecord.PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase= + EfiPagesToSize (EfiSizeToPages (ImageRecordCodeSection->CodeSegmentSize)= ); - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - TempRecor= d.PhysicalStart); - if (TempRecord.NumberOfPages =3D=3D 0) { - break; - } - } - } - - ImageEnd =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - - // - // Final DATA - // - if (TempRecord.PhysicalStart < ImageEnd) { - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - NewRecordCount++; - } - - return NewRecordCount; -} - -/** - Return the max number of new splitted entries, according to one old entr= y, - based upon PE code section and data section. - - @param OldRecord A pointer to one old memory map entry. - - @retval 0 no entry need to be splitted. - @return the max number of new splitted entries -**/ -STATIC -UINTN -GetMaxSplitRecordCount ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN LIST_ENTRY *ImageRecordList - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - UINTN SplitRecordCount; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - - SplitRecordCount =3D 0; - PhysicalStart =3D OldRecord->PhysicalStart; - PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize (OldRecor= d->NumberOfPages); - - do { - ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart, ImageRecordList); - if (ImageRecord =3D=3D NULL) { - break; - } - - SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - if (SplitRecordCount !=3D 0) { - SplitRecordCount--; - } - - return SplitRecordCount; -} - -/** - Split the memory map to new entries, according to one old entry, - based upon PE code section and data section. - - @param OldRecord A pointer to one old memory map entr= y. - @param NewRecord A pointer to several new memory map = entries. - The caller gurantee the buffer size = be 1 + - (SplitRecordCount * DescriptorSize) = calculated - below. - @param MaxSplitRecordCount The max number of splitted entries - @param DescriptorSize Size, in bytes, of an individual EFI= _MEMORY_DESCRIPTOR. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD en= tries used when searching - for an image record contained by the= memory range described in - the existing EFI memory map descript= or OldRecord - - @retval 0 no entry is splitted. - @return the real number of splitted record. -**/ -STATIC -UINTN -SplitRecord ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN UINTN MaxSplitRecordCount, - IN UINTN DescriptorSize, - IN LIST_ENTRY *ImageRecordList - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NewImageRecord; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - UINTN NewRecordCount; - UINTN TotalNewRecordCount; - BOOLEAN IsLastRecordData; - - if (MaxSplitRecordCount =3D=3D 0) { - CopyMem (NewRecord, OldRecord, DescriptorSize); - return 0; - } - - TotalNewRecordCount =3D 0; - - // - // Override previous record - // - CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); - PhysicalStart =3D TempRecord.PhysicalStart; - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord.= NumberOfPages); - - ImageRecord =3D NULL; - do { - NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); - if (NewImageRecord =3D=3D NULL) { - // - // No more image covered by this range, stop - // - if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { - // - // If this is still address in this record, need record. - // - NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, Descri= ptorSize); - IsLastRecordData =3D FALSE; - if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { - IsLastRecordData =3D TRUE; - } - - if (IsLastRecordData) { - // - // Last record is DATA, just merge it. - // - NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - NewRe= cord->PhysicalStart); - } else { - // - // Last record is CODE, create a new DATA entry. - // - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, = DescriptorSize); - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; - TotalNewRecordCount++; - } - } - - break; - } - - ImageRecord =3D NewImageRecord; - - // - // Set new record - // - NewRecordCount =3D SetNewRecord (ImageRecord, NewRecord, &TempRe= cord, DescriptorSize); - TotalNewRecordCount +=3D NewRecordCount; - NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord = + NewRecordCount * DescriptorSize); - - // - // Update PhysicalStart, in order to exclude the image buffer already = splitted. - // - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->Ima= geSize; - TempRecord.PhysicalStart =3D PhysicalStart; - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalSta= rt); - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - // - // The logic in function SplitTable() ensures that TotalNewRecordCount w= ill not be zero if the - // code reaches here. - // - ASSERT (TotalNewRecordCount !=3D 0); - return TotalNewRecordCount - 1; -} - -/** - Split the original memory map, and add more entries to describe PE code = section and data section. - This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. - This function will merge entries with same attributes finally. - - NOTE: It assumes PE code/data section are page aligned. - NOTE: It assumes enough entry is prepared for new memory map. - - Split table: - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - =3D=3D> - +---------------+ - | Record X | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF1 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF2 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record Y | - +---------------+ - - @param MemoryMapSize A pointer to the size, in bytes,= of the - MemoryMap buffer. On input, this= is the size of - old MemoryMap before split. The = actual buffer - size of MemoryMap is MemoryMapSi= ze + - (AdditionalRecordCount * Descrip= torSize) calculated - below. On output, it is the size= of new MemoryMap - after split. - @param MemoryMap A pointer to the buffer in which= firmware places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching - for an image record contained by= the memory range described in - EFI memory map descriptors. - @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. -**/ -STATIC -VOID -SplitTable ( - IN OUT UINTN *MemoryMapSize, - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN DescriptorSize, - IN LIST_ENTRY *ImageRecordList, - IN UINTN NumberOfAdditionalDescriptors - ) -{ - INTN IndexOld; - INTN IndexNew; - UINTN MaxSplitRecordCount; - UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; - - TotalSplitRecordCount =3D 0; - // - // Let old record point to end of valid MemoryMap buffer. - // - IndexOld =3D ((*MemoryMapSize) / DescriptorSize) - 1; - // - // Let new record point to end of full MemoryMap buffer. - // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; - for ( ; IndexOld >=3D 0; IndexOld--) { - MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); - // - // Split this MemoryMap record - // - IndexNew -=3D MaxSplitRecordCount; - RealSplitRecordCount =3D SplitRecord ( - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), - MaxSplitRecordCount, - DescriptorSize, - ImageRecordList - ); - // - // Adjust IndexNew according to real split. - // - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - RealSplitRecordCount * DescriptorSize - ); - IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount; - TotalSplitRecordCount +=3D RealSplitRecordCount; - IndexNew--; - } - - // - // Move all records to the beginning. - // - CopyMem ( - MemoryMap, - (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize - ); - - *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; - - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Set RuntimeData to XP - // - EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Merge same type to save entry size - // - MergeMemoryMap (MemoryMap, MemoryMapSize, DescriptorSize); - - return; -} - /** This function for GetMemoryMap() with properties table capability. =20 @@ -1044,6 +536,16 @@ CoreGetMemoryMapWithSeparatedImageSection ( // Split PE code/data // SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImageProper= tiesPrivateData.ImageRecordList, AdditionalRecordCount); + + // + // Set RuntimeData to XP + // + EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, *DescriptorSiz= e); + + // + // Merge same type to save entry size + // + MergeMemoryMap (MemoryMap, MemoryMapSize, *DescriptorSize); } } =20 @@ -1074,217 +576,6 @@ SetMemoryAttributesTableSectionAlignment ( } } =20 -/** - Swap two code sections in image record. - - @param FirstImageRecordCodeSection first code section in image record - @param SecondImageRecordCodeSection second code section in image reco= rd -**/ -STATIC -VOID -SwapImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; - - TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; - TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; - - FirstImageRecordCodeSection->CodeSegmentBase =3D SecondImageRecordCodeSe= ction->CodeSegmentBase; - FirstImageRecordCodeSection->CodeSegmentSize =3D SecondImageRecordCodeSe= ction->CodeSegmentSize; - - SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; - SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; -} - -/** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. - - @param ImageRecord image record to be sorted -**/ -VOID -SortImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *NextImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLi= nk; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLi= nk; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - while (NextImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLi= nk) { - NextImageRecordCodeSection =3D CR ( - NextImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE - ); - if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { - SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); - } - - NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; - } - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; - } -} - -/** - Check if code section in image record is valid. - - @param ImageRecord image record to be checked - - @retval TRUE image record is valid - @retval FALSE image record is invalid -**/ -BOOLEAN -IsImageRecordCodeSectionValid ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - DEBUG ((DEBUG_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->C= odeSegmentCount)); - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - LastImageRecordCodeSection =3D NULL; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - if (ImageRecordCodeSection->CodeSegmentSize =3D=3D 0) { - return FALSE; - } - - if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) { - return FALSE; - } - - if (ImageRecordCodeSection->CodeSegmentBase >=3D MAX_ADDRESS - ImageRe= cordCodeSection->CodeSegmentSize) { - return FALSE; - } - - if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection-= >CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) { - return FALSE; - } - - if (LastImageRecordCodeSection !=3D NULL) { - if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCo= deSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) { - return FALSE; - } - } - - LastImageRecordCodeSection =3D ImageRecordCodeSection; - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - } - - return TRUE; -} - -/** - Swap two image records. - - @param FirstImageRecord first image record. - @param SecondImageRecord second image record. -**/ -STATIC -VOID -SwapImageRecord ( - IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, - IN IMAGE_PROPERTIES_RECORD *SecondImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD TempImageRecord; - - TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; - TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; - TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; - - FirstImageRecord->ImageBase =3D SecondImageRecord->ImageBase; - FirstImageRecord->ImageSize =3D SecondImageRecord->ImageSize; - FirstImageRecord->CodeSegmentCount =3D SecondImageRecord->CodeSegmentCou= nt; - - SecondImageRecord->ImageBase =3D TempImageRecord.ImageBase; - SecondImageRecord->ImageSize =3D TempImageRecord.ImageSize; - SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; - - SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); -} - -/** - Sort image record based upon the ImageBase from low to high. - - @param ImageRecordList Image record list to be sorted -**/ -STATIC -VOID -SortImageRecord ( - IN LIST_ENTRY *ImageRecordList - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NextImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *NextImageRecordLink; - LIST_ENTRY *ImageRecordEndLink; - - ImageRecordLink =3D ImageRecordList->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - ImageRecordEndLink =3D ImageRecordList; - while (ImageRecordLink !=3D ImageRecordEndLink) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - while (NextImageRecordLink !=3D ImageRecordEndLink) { - NextImageRecord =3D CR ( - NextImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { - SwapImageRecord (ImageRecord, NextImageRecord); - } - - NextImageRecordLink =3D NextImageRecordLink->ForwardLink; - } - - ImageRecordLink =3D ImageRecordLink->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - } -} - /** Insert image record. =20 @@ -1469,47 +760,6 @@ Finish: return; } =20 -/** - Find image record according to image base and size. - - @param ImageBase Base of PE image - @param ImageSize Size of PE image - @param ImageRecordList Image record list to be searched - - @return image record -**/ -STATIC -IMAGE_PROPERTIES_RECORD * -FindImageRecord ( - IN EFI_PHYSICAL_ADDRESS ImageBase, - IN UINT64 ImageSize, - IN LIST_ENTRY *ImageRecordList - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink) - { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - - if ((ImageBase =3D=3D ImageRecord->ImageBase) && - (ImageSize =3D=3D ImageRecord->ImageSize)) - { - return ImageRecord; - } - } - - return NULL; -} - /** Remove Image record. =20 diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/C= ore/Dxe/Misc/MemoryProtection.c index 7cc829b17402..977239d08afc 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -32,6 +32,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include =20 #include #include @@ -66,29 +67,6 @@ extern LIST_ENTRY mGcdMemorySpaceMap; =20 STATIC LIST_ENTRY mProtectedImageRecordList; =20 -/** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. - - @param ImageRecord image record to be sorted -**/ -VOID -SortImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ); - -/** - Check if code section in image record is valid. - - @param ImageRecord image record to be checked - - @retval TRUE image record is valid - @retval FALSE image record is invalid -**/ -BOOLEAN -IsImageRecordCodeSectionValid ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ); - /** Get the image type. =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index df7c54ebb793..9fb3b922038f 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -7,3 +7,773 @@ SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ + +#include + +#include +#include +#include +#include + +#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) + +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) + +/** + Converts a number of EFI_PAGEs to a size in bytes. + + NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. + + @param Pages The number of EFI_PAGES. + + @return The number of bytes associated with the number of EFI_PAGEs spe= cified + by Pages. +**/ +STATIC +UINT64 +EfiPagesToSize ( + IN UINT64 Pages + ) +{ + return LShiftU64 (Pages, EFI_PAGE_SHIFT); +} + +/** + Converts a size, in bytes, to a number of EFI_PAGESs. + + NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. + + @param Size A size in bytes. + + @return The number of EFI_PAGESs associated with the number of bytes sp= ecified + by Size. + +**/ +STATIC +UINT64 +EfiSizeToPages ( + IN UINT64 Size + ) +{ + return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0); +} + +/** + Sort memory map entries based upon PhysicalStart, from low to high. + + @param MemoryMap A pointer to the buffer in which firmware= places + the current memory map. + @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +VOID +SortMemoryMap ( + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN MemoryMapSize, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; + EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; + EFI_MEMORY_DESCRIPTOR TempMemoryMap; + + MemoryMapEntry =3D MemoryMap; + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descripto= rSize); + MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + Me= moryMapSize); + while (MemoryMapEntry < MemoryMapEnd) { + while (NextMemoryMapEntry < MemoryMapEnd) { + if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStar= t) { + CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof (EFI_MEMORY_DESCRI= PTOR)); + CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof (EFI_MEMORY_DE= SCRIPTOR)); + CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof (EFI_MEMORY_DE= SCRIPTOR)); + } + + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, D= escriptorSize); + } + + MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); + NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); + } + + return; +} + +/** + Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. + + @param Buffer Start Address + @param Length Address length + @param ImageRecordList Image record list + + @return first image record covered by [buffer, length] +**/ +STATIC +IMAGE_PROPERTIES_RECORD * +GetImageRecordByAddress ( + IN EFI_PHYSICAL_ADDRESS Buffer, + IN UINT64 Length, + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) + { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + if ((Buffer <=3D ImageRecord->ImageBase) && + (Buffer + Length >=3D ImageRecord->ImageBase + ImageRecord->ImageS= ize)) + { + return ImageRecord; + } + } + + return NULL; +} + +/** + Set the memory map to new entries, according to one old entry, + based upon PE code section and data section in image record + + @param ImageRecord An image record whose [ImageBase, ImageSi= ze] covered + by old memory map entry. + @param NewRecord A pointer to several new memory map entri= es. + The caller gurantee the buffer size be 1 + + (SplitRecordCount * DescriptorSize) calcu= lated + below. + @param OldRecord A pointer to one old memory map entry. + @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. +**/ +STATIC +UINTN +SetNewRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord, + IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN UINTN DescriptorSize + ) +{ + EFI_MEMORY_DESCRIPTOR TempRecord; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + UINTN NewRecordCount; + UINT64 PhysicalEnd; + UINT64 ImageEnd; + + CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); + PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord= .NumberOfPages); + NewRecordCount =3D 0; + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; + + if (TempRecord.PhysicalStart <=3D ImageRecordCodeSection->CodeSegmentB= ase) { + // + // DATA + // + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentBase - NewRecord->PhysicalStart); + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; + if (NewRecord->NumberOfPages !=3D 0) { + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); + NewRecordCount++; + } + + // + // CODE + // + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentSize); + NewRecord->Attribute =3D (TempRecord.Attribute & (~EFI_MEMORY_XP= )) | EFI_MEMORY_RO; + if (NewRecord->NumberOfPages !=3D 0) { + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); + NewRecordCount++; + } + + TempRecord.PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase= + EfiPagesToSize (EfiSizeToPages (ImageRecordCodeSection->CodeSegmentSize)= ); + TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - TempRecor= d.PhysicalStart); + if (TempRecord.NumberOfPages =3D=3D 0) { + break; + } + } + } + + ImageEnd =3D ImageRecord->ImageBase + ImageRecord->ImageSize; + + // + // Final DATA + // + if (TempRecord.PhysicalStart < ImageEnd) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; + NewRecordCount++; + } + + return NewRecordCount; +} + +/** + Return the max number of new splitted entries, according to one old entr= y, + based upon PE code section and data section. + + @param OldRecord A pointer to one old memory map entry. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries= used when searching + for an image record contained by the memo= ry range described in + the existing EFI memory map descriptor Ol= dRecord + + @retval 0 no entry need to be splitted. + @return the max number of new splitted entries +**/ +STATIC +UINTN +GetMaxSplitRecordCount ( + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + UINTN SplitRecordCount; + UINT64 PhysicalStart; + UINT64 PhysicalEnd; + + SplitRecordCount =3D 0; + PhysicalStart =3D OldRecord->PhysicalStart; + PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize (OldRecor= d->NumberOfPages); + + do { + ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart, ImageRecordList); + if (ImageRecord =3D=3D NULL) { + break; + } + + SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); + PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; + } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); + + if (SplitRecordCount !=3D 0) { + SplitRecordCount--; + } + + return SplitRecordCount; +} + +/** + Split the memory map to new entries, according to one old entry, + based upon PE code section and data section. + + @param OldRecord A pointer to one old memory map entr= y. + @param NewRecord A pointer to several new memory map = entries. + The caller gurantee the buffer size = be 1 + + (SplitRecordCount * DescriptorSize) = calculated + below. + @param MaxSplitRecordCount The max number of splitted entries + @param DescriptorSize Size, in bytes, of an individual EFI= _MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD en= tries used when searching + for an image record contained by the= memory range described in + the existing EFI memory map descript= or OldRecord + + @retval 0 no entry is splitted. + @return the real number of splitted record. +**/ +STATIC +UINTN +SplitRecord ( + IN EFI_MEMORY_DESCRIPTOR *OldRecord, + IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, + IN UINTN MaxSplitRecordCount, + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList + ) +{ + EFI_MEMORY_DESCRIPTOR TempRecord; + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD *NewImageRecord; + UINT64 PhysicalStart; + UINT64 PhysicalEnd; + UINTN NewRecordCount; + UINTN TotalNewRecordCount; + BOOLEAN IsLastRecordData; + + if (MaxSplitRecordCount =3D=3D 0) { + CopyMem (NewRecord, OldRecord, DescriptorSize); + return 0; + } + + TotalNewRecordCount =3D 0; + + // + // Override previous record + // + CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); + PhysicalStart =3D TempRecord.PhysicalStart; + PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord.= NumberOfPages); + + ImageRecord =3D NULL; + do { + NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); + if (NewImageRecord =3D=3D NULL) { + // + // No more image covered by this range, stop + // + if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { + // + // If this is still address in this record, need record. + // + NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, Descri= ptorSize); + IsLastRecordData =3D FALSE; + if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { + IsLastRecordData =3D TRUE; + } + + if (IsLastRecordData) { + // + // Last record is DATA, just merge it. + // + NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - NewRe= cord->PhysicalStart); + } else { + // + // Last record is CODE, create a new DATA entry. + // + NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, = DescriptorSize); + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; + NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; + TotalNewRecordCount++; + } + } + + break; + } + + ImageRecord =3D NewImageRecord; + + // + // Set new record + // + NewRecordCount =3D SetNewRecord (ImageRecord, NewRecord, &TempRe= cord, DescriptorSize); + TotalNewRecordCount +=3D NewRecordCount; + NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord = + NewRecordCount * DescriptorSize); + + // + // Update PhysicalStart, in order to exclude the image buffer already = splitted. + // + PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->Ima= geSize; + TempRecord.PhysicalStart =3D PhysicalStart; + TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalSta= rt); + } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); + + // + // The logic in function SplitTable() ensures that TotalNewRecordCount w= ill not be zero if the + // code reaches here. + // + ASSERT (TotalNewRecordCount !=3D 0); + return TotalNewRecordCount - 1; +} + +/** + Split the original memory map, and add more entries to describe PE code = section and data section. + This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. + This function will merge entries with same attributes finally. + + NOTE: It assumes PE code/data section are page aligned. + NOTE: It assumes enough entry is prepared for new memory map. + + Split table: + +---------------+ + | Record X | + +---------------+ + | Record RtCode | + +---------------+ + | Record Y | + +---------------+ + =3D=3D> + +---------------+ + | Record X | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF1 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF2 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record Y | + +---------------+ + + @param MemoryMapSize A pointer to the size, in bytes,= of the + MemoryMap buffer. On input, this= is the size of + old MemoryMap before split. The = actual buffer + size of MemoryMap is MemoryMapSi= ze + + (AdditionalRecordCount * Descrip= torSize) calculated + below. On output, it is the size= of new MemoryMap + after split. + @param MemoryMap A pointer to the buffer in which= firmware places + the current memory map. + @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching + for an image record contained by= the memory range described in + EFI memory map descriptors. + @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. +**/ +VOID +EFIAPI +SplitTable ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList, + IN UINTN NumberOfAdditionalDescriptors + ) +{ + INTN IndexOld; + INTN IndexNew; + UINTN MaxSplitRecordCount; + UINTN RealSplitRecordCount; + UINTN TotalSplitRecordCount; + + TotalSplitRecordCount =3D 0; + // + // Let old record point to end of valid MemoryMap buffer. + // + IndexOld =3D ((*MemoryMapSize) / DescriptorSize) - 1; + // + // Let new record point to end of full MemoryMap buffer. + // + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; + for ( ; IndexOld >=3D 0; IndexOld--) { + MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); + // + // Split this MemoryMap record + // + IndexNew -=3D MaxSplitRecordCount; + RealSplitRecordCount =3D SplitRecord ( + (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), + (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), + MaxSplitRecordCount, + DescriptorSize, + ImageRecordList + ); + // + // Adjust IndexNew according to real split. + // + CopyMem ( + ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), + ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), + RealSplitRecordCount * DescriptorSize + ); + IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount; + TotalSplitRecordCount +=3D RealSplitRecordCount; + IndexNew--; + } + + // + // Move all records to the beginning. + // + CopyMem ( + MemoryMap, + (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, + (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize + ); + + *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; + + // + // Sort from low to high (Just in case) + // + SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); + + return; +} + +/** + Swap two code sections in image record. + + @param FirstImageRecordCodeSection first code section in image record + @param SecondImageRecordCodeSection second code section in image reco= rd +**/ +VOID +EFIAPI +SwapImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; + + TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; + TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; + + FirstImageRecordCodeSection->CodeSegmentBase =3D SecondImageRecordCodeSe= ction->CodeSegmentBase; + FirstImageRecordCodeSection->CodeSegmentSize =3D SecondImageRecordCodeSe= ction->CodeSegmentSize; + + SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; + SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; +} + +/** + Sort code section in image record, based upon CodeSegmentBase from low t= o high. + + @param ImageRecord image record to be sorted +**/ +VOID +EFIAPI +SortImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *NextImageRecordCodeSectionLink; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLi= nk; + NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLi= nk; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + while (NextImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLi= nk) { + NextImageRecordCodeSection =3D CR ( + NextImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE + ); + if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { + SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); + } + + NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; + } + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; + NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; + } +} + +/** + Check if code section in image record is valid. + + @param ImageRecord image record to be checked + + @retval TRUE image record is valid + @retval FALSE image record is invalid +**/ +BOOLEAN +EFIAPI +IsImageRecordCodeSectionValid ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection; + LIST_ENTRY *ImageRecordCodeSectionLink; + LIST_ENTRY *ImageRecordCodeSectionEndLink; + LIST_ENTRY *ImageRecordCodeSectionList; + + DEBUG ((DEBUG_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->C= odeSegmentCount)); + + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; + + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; + ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; + LastImageRecordCodeSection =3D NULL; + while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + if (ImageRecordCodeSection->CodeSegmentSize =3D=3D 0) { + return FALSE; + } + + if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) { + return FALSE; + } + + if (ImageRecordCodeSection->CodeSegmentBase >=3D MAX_ADDRESS - ImageRe= cordCodeSection->CodeSegmentSize) { + return FALSE; + } + + if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection-= >CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) { + return FALSE; + } + + if (LastImageRecordCodeSection !=3D NULL) { + if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCo= deSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) { + return FALSE; + } + } + + LastImageRecordCodeSection =3D ImageRecordCodeSection; + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; + } + + return TRUE; +} + +/** + Swap two image records. + + @param FirstImageRecord first image record. + @param SecondImageRecord second image record. +**/ +VOID +EFIAPI +SwapImageRecord ( + IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, + IN IMAGE_PROPERTIES_RECORD *SecondImageRecord + ) +{ + IMAGE_PROPERTIES_RECORD TempImageRecord; + + TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; + TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; + TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; + + FirstImageRecord->ImageBase =3D SecondImageRecord->ImageBase; + FirstImageRecord->ImageSize =3D SecondImageRecord->ImageSize; + FirstImageRecord->CodeSegmentCount =3D SecondImageRecord->CodeSegmentCou= nt; + + SecondImageRecord->ImageBase =3D TempImageRecord.ImageBase; + SecondImageRecord->ImageSize =3D TempImageRecord.ImageSize; + SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; + + SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); +} + +/** + Sort image record based upon the ImageBase from low to high. + + @param ImageRecordList Image record list to be sorted +**/ +VOID +EFIAPI +SortImageRecord ( + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD *NextImageRecord; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *NextImageRecordLink; + LIST_ENTRY *ImageRecordEndLink; + + ImageRecordLink =3D ImageRecordList->ForwardLink; + NextImageRecordLink =3D ImageRecordLink->ForwardLink; + ImageRecordEndLink =3D ImageRecordList; + while (ImageRecordLink !=3D ImageRecordEndLink) { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + while (NextImageRecordLink !=3D ImageRecordEndLink) { + NextImageRecord =3D CR ( + NextImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + + ); + if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { + SwapImageRecord (ImageRecord, NextImageRecord); + } + + NextImageRecordLink =3D NextImageRecordLink->ForwardLink; + } + + ImageRecordLink =3D ImageRecordLink->ForwardLink; + NextImageRecordLink =3D ImageRecordLink->ForwardLink; + } +} + +/** + Find image record according to image base and size. + + @param ImageBase Base of PE image + @param ImageSize Size of PE image + @param ImageRecordList Image record list to be searched + + @return image record +**/ +IMAGE_PROPERTIES_RECORD * +EFIAPI +FindImageRecord ( + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize, + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) + { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + if ((ImageBase =3D=3D ImageRecord->ImageBase) && + (ImageSize =3D=3D ImageRecord->ImageSize)) + { + return ImageRecord; + } + } + + return NULL; +} diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMai= n.h index 43daa037be44..86a7be2f5188 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -228,26 +228,6 @@ typedef struct { #define LOADED_IMAGE_PRIVATE_DATA_FROM_THIS(a) \ CR(a, LOADED_IMAGE_PRIVATE_DATA, Info, LOADED_IMAGE_PRIVATE_DATA= _SIGNATURE) =20 -#define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I',= 'P','R','C') - -typedef struct { - UINT32 Signature; - LIST_ENTRY Link; - EFI_PHYSICAL_ADDRESS CodeSegmentBase; - UINT64 CodeSegmentSize; -} IMAGE_PROPERTIES_RECORD_CODE_SECTION; - -#define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D') - -typedef struct { - UINT32 Signature; - LIST_ENTRY Link; - EFI_PHYSICAL_ADDRESS ImageBase; - UINT64 ImageSize; - UINTN CodeSegmentCount; - LIST_ENTRY CodeSegmentList; -} IMAGE_PROPERTIES_RECORD; - // // DXE Core Global Variables // diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index 16871f2021e5..090970aec6df 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -94,6 +94,7 @@ [LibraryClasses] DebugAgentLib CpuExceptionHandlerLib PcdLib + ImagePropertiesRecordLib =20 [Guids] gEfiEventMemoryMapChangeGuid ## PRODUCES ##= Event diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index 728008a2e5bb..9139c7b6c05d 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -11,4 +11,163 @@ #ifndef IMAGE_PROPERTIES_RECORD_SUPPORT_LIB_H_ #define IMAGE_PROPERTIES_RECORD_SUPPORT_LIB_H_ =20 +#define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I',= 'P','R','C') + +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + EFI_PHYSICAL_ADDRESS CodeSegmentBase; + UINT64 CodeSegmentSize; +} IMAGE_PROPERTIES_RECORD_CODE_SECTION; + +#define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D') + +typedef struct { + UINT32 Signature; + LIST_ENTRY Link; + EFI_PHYSICAL_ADDRESS ImageBase; + UINT64 ImageSize; + UINTN CodeSegmentCount; + LIST_ENTRY CodeSegmentList; +} IMAGE_PROPERTIES_RECORD; + +/** + Split the original memory map, and add more entries to describe PE code = section and data section. + This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. + This function will merge entries with same attributes finally. + + NOTE: It assumes PE code/data section are page aligned. + NOTE: It assumes enough entry is prepared for new memory map. + + Split table: + +---------------+ + | Record X | + +---------------+ + | Record RtCode | + +---------------+ + | Record Y | + +---------------+ + =3D=3D> + +---------------+ + | Record X | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF1 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record RtData | | + +---------------+ | + | Record RtCode | |-> PE/COFF2 + +---------------+ | + | Record RtData | | + +---------------+ ---- + | Record Y | + +---------------+ + + @param MemoryMapSize A pointer to the size, in bytes,= of the + MemoryMap buffer. On input, this= is the size of + old MemoryMap before split. The = actual buffer + size of MemoryMap is MemoryMapSi= ze + + (AdditionalRecordCount * Descrip= torSize) calculated + below. On output, it is the size= of new MemoryMap + after split. + @param MemoryMap A pointer to the buffer in which= firmware places + the current memory map. + @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. + @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching + for an image record contained by= the memory range described in + EFI memory map descriptors. + @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. +**/ +VOID +EFIAPI +SplitTable ( + IN OUT UINTN *MemoryMapSize, + IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN UINTN DescriptorSize, + IN LIST_ENTRY *ImageRecordList, + IN UINTN NumberOfAdditionalDescriptors + ); + +/** + Sort code section in image record, based upon CodeSegmentBase from low t= o high. + + @param ImageRecord image record to be sorted +**/ +VOID +EFIAPI +SortImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ); + +/** + Check if code section in image record is valid. + + @param ImageRecord image record to be checked + + @retval TRUE image record is valid + @retval FALSE image record is invalid +**/ +BOOLEAN +EFIAPI +IsImageRecordCodeSectionValid ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ); + +/** + Sort image record based upon the ImageBase from low to high. + + @param ImageRecordList Image record list to be sorted +**/ +VOID +EFIAPI +SortImageRecord ( + IN LIST_ENTRY *ImageRecordList + ); + +/** + Swap two image records. + + @param[in] FirstImageRecord The first image record. + @param[in] SecondImageRecord The second image record. +**/ +VOID +EFIAPI +SwapImageRecord ( + IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, + IN IMAGE_PROPERTIES_RECORD *SecondImageRecord + ); + +/** + Swap two code sections in a single IMAGE_PROPERTIES_RECORD. + + @param[in] FirstImageRecordCodeSection The first code section + @param[in] SecondImageRecordCodeSection The second code section +**/ +VOID +EFIAPI +SwapImageRecordCodeSection ( + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, + IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection + ); + +/** + Find image record according to image base and size. + + @param ImageBase Base of PE image + @param ImageSize Size of PE image + @param ImageRecordList Image record list to be searched + + @return image record +**/ +IMAGE_PROPERTIES_RECORD * +EFIAPI +FindImageRecord ( + IN EFI_PHYSICAL_ADDRESS ImageBase, + IN UINT64 ImageSize, + IN LIST_ENTRY *ImageRecordList + ); + #endif diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.inf b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= sRecordLib.inf index b7e493056889..4c1466fc3336 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf @@ -19,6 +19,10 @@ [Defines] [Sources.common] ImagePropertiesRecordLib.c =20 +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib =20 [Packages] MdePkg/MdePkg.dec --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110655): https://edk2.groups.io/g/devel/message/110655 Mute This Topic: https://groups.io/mt/102368847/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110656+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110656+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031844; cv=none; d=zohomail.com; s=zohoarc; b=M+1VpuikBOSJBcvOKP2lM9xz66PRViuhEqPRXKdtM85ol/WXeNb1oQLFQhn7YxenkorUE1+jO+ifuyKx+jgIV906gX2u4Gu1iksRPIdPAdssNLNYGwHlu+FL8ORJCV9g+35k+bo7UmmlG/R5UTxAnsb9eQPsctXB9LNApOTPrtU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031844; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=UmoLBGQifQCULHjPqQQ+MV9laAotImiuj4gWcTo6ytg=; b=SWAhZrs1lsTDT+QSTwrqRgRP9SbUYO1z1iVlvEMMddvJlz5HrSyB3E0NkDM+k+tYu3ebFe7W1RY0jCQGFDGedEcyepvnsesL57yLSuQ69zQz/gDbtQbRi8q9TR3t8L0CA//owYi3Gd1oa+WNrm4zg4vqAyGUQDeH7mmekFm8SOg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110656+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 16990318446621020.9135215788376; Fri, 3 Nov 2023 10:17:24 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=o/gzatjtgU7XTGdMp6uKxwWJFnYwqVRdDxD8jeZaWNI=; 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=1699031844; v=1; b=he0nc4bLU87qpu2HOYqqMnv4o7K5Psi+ezLLezuJrEnti1wMrNYughD+OKZsFtxddu9MurET fDq7Vm6a1SoUu3GQ4JqvjDdsmDAnxxqQY6h/GZQ6t80pygl45XoTbmNzN0Y5VMqr4jzrVF+zLy5 LZwm9hyM16V+EENYWgiCZ4Ro= X-Received: by 127.0.0.2 with SMTP id C8b1YY1788612xTBBSLnldtf; Fri, 03 Nov 2023 10:17:24 -0700 X-Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) by mx.groups.io with SMTP id smtpd.web11.59067.1699031843315517319 for ; Fri, 03 Nov 2023 10:17:23 -0700 X-Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-1cc394f4cdfso18311125ad.0 for ; Fri, 03 Nov 2023 10:17:23 -0700 (PDT) X-Gm-Message-State: EHNpit3Zv2Zjw2QQMZyK1LxMx1787277AA= X-Google-Smtp-Source: AGHT+IGajkFJ6dPx/qZqPfsuq0xHp14aOTCKvshGPa3Fz0TkYUyCCFW5gESiYnqwTBy95vxz+4O0KA== X-Received: by 2002:a17:903:2808:b0:1cc:31c4:377b with SMTP id kp8-20020a170903280800b001cc31c4377bmr14430704plb.63.1699031842434; Fri, 03 Nov 2023 10:17:22 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:22 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 08/14] MdeModulePkg: Add ImagePropertiesRecordLib Host-Based Unit Test Date: Fri, 3 Nov 2023 10:17:00 -0700 Message-ID: <20231103171706.148-9-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031844921100026 Content-Type: text/plain; charset="utf-8" Create a host-based unit test for the ImagePropertiesRecordLib SplitTable() logic. This test has 4 cases which tests different potential image and memory map layouts. 3/4 of these tests fail with the logic in its current state to provide proof of the bugs in the current MAT logic. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertiesReco= rdLibUnitTestHost.c | 938 ++++++++++++++++++++ MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertiesReco= rdLibUnitTestHost.inf | 35 + MdeModulePkg/Test/MdeModulePkgHostTest.dsc = | 5 + 3 files changed, 978 insertions(+) diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePr= opertiesRecordLibUnitTestHost.c b/MdeModulePkg/Library/ImagePropertiesRecor= dLib/UnitTest/ImagePropertiesRecordLibUnitTestHost.c new file mode 100644 index 000000000000..8b0a55685ce3 --- /dev/null +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertie= sRecordLibUnitTestHost.c @@ -0,0 +1,938 @@ +/** @file + Unit tests the SplitTable() ImagePropertiesRecordLib Logic + + Copyright (C) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#define UNIT_TEST_APP_NAME "Image Properties Record Lib Unit Test" +#define UNIT_TEST_APP_VERSION "1.0" + +#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) + +// The starting memory map will contain 6 entries +#define NUMBER_OF_MEMORY_MAP_DESCRIPTORS 6 + +// Each memory map descriptor will be the sizeof(EFI_MEMORY_DESCRIPTOR) in= stead of a nonstandard size +// to catch pointer math issues +#define DESCRIPTOR_SIZE sizeof(EFI_MEMORY_DESCRIPTOR) + +// Each memory map descriptor will describe 12 pages +#define BASE_DESCRIPTOR_NUMBER_OF_PAGES 0x0C + +// The size, in bytes, of each memory map descriptor range +#define BASE_DESCRIPTOR_ENTRY_SIZE (EFI_PAGES_TO_SIZE(BASE_DESCRIPTOR_NUM= BER_OF_PAGES)) + +// MACRO to get the starting address of a descriptor's described range bas= ed on the index of that descriptor +#define BASE_DESCRIPTOR_START_ADDRESS(DescriptorNumber) (DescriptorNumber= * BASE_DESCRIPTOR_ENTRY_SIZE) + +// Virtual start must be zero +#define BASE_DESCRIPTOR_VIRTUAL_START 0x0 + +// Size of the default memory map +#define BASE_MEMORY_MAP_SIZE (NUMBER_OF_MEMORY_MAP_DESCRIPTORS * DESCRIPT= OR_SIZE) + +// Number of images in each test case +#define NUMBER_OF_IMAGES_TO_SPLIT 3 + +// Maximum number of descriptors required for each image (None->Data->Code= ->Data->Code->Data->None) +#define MAX_DESCRIPTORS_PER_IMAGE 7 + +// Number of unused additional descriptors in the starting memory map buff= er which is used by the +// SplitTable() logic +#define NUMBER_OF_ADDITIONAL_DESCRIPTORS (NUMBER_OF_IMAGES_TO_SPLIT * MAX= _DESCRIPTORS_PER_IMAGE) + +// Size of the memory map with enough space for the starting descriptors a= nd the split descriptors +#define SPLIT_MEMORY_MAP_SIZE (BASE_MEMORY_MAP_SIZE + (NUMBER_OF_ADDITION= AL_DESCRIPTORS * DESCRIPTOR_SIZE)) + +typedef enum { + SectionTypeCode, + SectionTypeData, + SectionTypeNotFound +} SECTION_TYPE; + +typedef struct { + EFI_MEMORY_DESCRIPTOR *MemoryMap; + LIST_ENTRY ImageList; +} IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT; + +EFI_MEMORY_DESCRIPTOR BaseMemoryMap[] =3D { + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (0), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + }, + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (1), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + }, + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (2), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + }, + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (3), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + }, + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (4), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + }, + { + EfiConventionalMemory, // Type + BASE_DESCRIPTOR_START_ADDRESS (5), // PhysicalStart + BASE_DESCRIPTOR_VIRTUAL_START, // VirtualStart + BASE_DESCRIPTOR_NUMBER_OF_PAGES, // Number of Pages + 0 // Attribute + } +}; + +/** + Returns a bitmap where one bit is set for each section in the image list= . For example, if + there are 3 images and each image 3 sections the returned bitmap will be= 111111111. + + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries + + @retval A bitmap such that the most significant bit is the number of sec= tions + in all images and every bit between 0 -> MSB is set + +**/ +STATIC +UINT64 +GetImageSectionBitmap ( + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *ImageRecordCodeSectionLink; + EFI_PHYSICAL_ADDRESS SectionBase; + UINT64 ReturnBitmap; + UINT64 Shift; + + if (ImageRecordList =3D=3D NULL) { + return 0; + } + + ReturnBitmap =3D 0; + Shift =3D 0; + + // Walk through each image record + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) + { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + SectionBase =3D ImageRecord->ImageBase; + + // Walk through each code entry + for (ImageRecordCodeSectionLink =3D ImageRecord->CodeSegmentList.Forwa= rdLink; + ImageRecordCodeSectionLink !=3D &ImageRecord->CodeSegmentList; + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forwar= dLink) + { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGN= ATURE + ); + + // Check for data region before the code section base + if (SectionBase < ImageRecordCodeSection->CodeSegmentBase) { + ReturnBitmap |=3D LShiftU64 (1, Shift++); + } + + // Code section + ReturnBitmap |=3D LShiftU64 (1, Shift++); + SectionBase =3D ImageRecordCodeSection->CodeSegmentBase + ImageRec= ordCodeSection->CodeSegmentSize; + } + + // Check for data region after the previous code section + if (SectionBase < (ImageRecord->ImageBase + ImageRecord->ImageSize)) { + ReturnBitmap |=3D LShiftU64 (1, Shift++); + } + } + + return ReturnBitmap; +} + +/** + Searches the input image list for a section which exactly matches the me= mory range Buffer -> Buffer + Length. + + @param[in] Buffer Start Address to check + @param[in] Length Length to check + @param[out] Type The type of the section which corresponds w= ith the memory + range Buffer -> Buffer + Length (Code or Dat= a) or SectionTypeNotFound + if no image section matches the memory range + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries to= check against + the memory range Buffer -> Buffer + Length + + @retval A bitmap with a single bit set (1 << Shift) where Shift correspo= nds with the number of sections inspected + in the image list before arriving at the section matching the me= mory range Buffer -> Buffer + Length +**/ +STATIC +UINT64 +MatchDescriptorToImageSection ( + IN EFI_PHYSICAL_ADDRESS Buffer, + IN UINT64 Length, + OUT SECTION_TYPE *Type, + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *ImageRecordCodeSectionLink; + EFI_PHYSICAL_ADDRESS SectionBase; + UINT8 Shift; + + Shift =3D 0; + + if (ImageRecordList =3D=3D NULL) { + return 1; + } + + // Walk through each image record + for (ImageRecordLink =3D ImageRecordList->ForwardLink; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink) + { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + SectionBase =3D ImageRecord->ImageBase; + + // Walk through each code entry + for (ImageRecordCodeSectionLink =3D ImageRecord->CodeSegmentList.Forwa= rdLink; + ImageRecordCodeSectionLink !=3D &ImageRecord->CodeSegmentList; + ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forwar= dLink) + { + ImageRecordCodeSection =3D CR ( + ImageRecordCodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGN= ATURE + ); + + if (SectionBase < ImageRecordCodeSection->CodeSegmentBase) { + // Check the data region before the code section base + if ((Buffer =3D=3D SectionBase) && + (Length =3D=3D ImageRecordCodeSection->CodeSegmentBase - Secti= onBase)) + { + *Type =3D SectionTypeData; + return LShiftU64 (1, Shift); + } + + Shift++; + } + + // Check the code region + if ((Buffer =3D=3D ImageRecordCodeSection->CodeSegmentBase) && + (Length =3D=3D ImageRecordCodeSection->CodeSegmentSize)) + { + *Type =3D SectionTypeCode; + return LShiftU64 (1, Shift); + } + + Shift++; + SectionBase =3D ImageRecordCodeSection->CodeSegmentBase + ImageRecor= dCodeSection->CodeSegmentSize; + } + + // Check the data region after the code section + if (SectionBase < (ImageRecord->ImageBase + ImageRecord->ImageSize)) { + if ((Buffer =3D=3D SectionBase) && + (Length =3D=3D (ImageRecord->ImageBase + ImageRecord->ImageSize)= - SectionBase)) + { + *Type =3D SectionTypeData; + return LShiftU64 (1, Shift); + } + + Shift++; + } + } + + // No image sections match + *Type =3D SectionTypeNotFound; + return 0; +} + +/** + Walks through the input memory map and checks that every memory descript= or with an attribute matches + an image in ImageRecordList. + + @param[in] MemoryMapSize The size, in bytes, of t= he memory map + @param[in] MemoryMap A pointer to the buffer = containing the memory map + @param[in] ImageRecordList A list of IMAGE_PROPERTI= ES_RECORD entries + + @retval TRUE if all memory descriptors with attributes match an image se= ction and have the correct attributes + +**/ +STATIC +BOOLEAN +IsMemoryMapValid ( + IN UINTN MemoryMapSize, + IN EFI_MEMORY_DESCRIPTOR *MemoryMap, + IN LIST_ENTRY *ImageRecordList + ) +{ + UINT64 ImageSectionsBitmap; + UINT64 ReturnSectionBitmask; + UINT64 NumberOfDescriptors; + UINT8 Index; + SECTION_TYPE Type; + + Index =3D 0; + NumberOfDescriptors =3D MemoryMapSize / DESCRIPTOR_SIZE; + + UT_ASSERT_EQUAL (MemoryMapSize % DESCRIPTOR_SIZE, 0); + UT_ASSERT_NOT_NULL (MemoryMap); + UT_ASSERT_NOT_NULL (ImageRecordList); + + // The returned bitmap will have one bit is set for each section in the = image list. + // If there are 3 images and 3 sections each image, the resulting bitmap= will + // be 0000000000000000000000000000000000000000000000000000000111111111. = Flipping that bitmap + // results in 1111111111111111111111111111111111111111111111111111111000= 000000. The return value + // of each iteration through MatchDescriptorToImageSection() is one set = bit corrosponding to the number + // of sections before finding the section which matched the descriptor m= emory range which we + // OR with ImageSectionsBitmap. If, at the end of the loop, every bit in= ImageSectionsBitmap is set, + // we must have matched every image in ImageRecordList with a descriptor= in the memory map which has + // nonzero attributes. + ImageSectionsBitmap =3D ~GetImageSectionBitmap (ImageRecordList); + + // For each descriptor in the memory map + for ( ; Index < NumberOfDescriptors; Index++) { + if (MemoryMap[Index].Attribute !=3D 0) { + ReturnSectionBitmask =3D MatchDescriptorToImageSection ( + MemoryMap[Index].PhysicalStart, + EFI_PAGES_TO_SIZE (MemoryMap[Index].NumberO= fPages), + &Type, + ImageRecordList + ); + + // Make sure the attributes of the descriptor match the returned sec= tion type. + // DATA sections should have execution protection and CODE sections = should have + // write protection. + if ((Type =3D=3D SectionTypeNotFound) || + ((Type =3D=3D SectionTypeData) && (MemoryMap[Index].Attribute = =3D=3D EFI_MEMORY_RP)) || + ((Type =3D=3D SectionTypeCode) && (MemoryMap[Index].Attribute = =3D=3D EFI_MEMORY_XP))) + { + return FALSE; + } + + // If the bit associated with image found has already been set, then= there must be a duplicate + // in the memory map meaning it is invalid. + UT_ASSERT_EQUAL (ImageSectionsBitmap & ReturnSectionBitmask, 0); + + ImageSectionsBitmap |=3D ReturnSectionBitmask; + } + } + + // If every bit in ImageSectionsBitmap is set, the return value will be = TRUE + return !(~ImageSectionsBitmap); +} + +/** + Separate the image sections in the memory map and run a check to ensure = the output is valid. + + @param[in] Context Context containing the memory map and image record= pointers + + @retval TRUE if the memory map is split correctly +**/ +STATIC +BOOLEAN +SeparateAndCheck ( + IN IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *Context + ) +{ + UINTN MemoryMapSize; + + MemoryMapSize =3D BASE_MEMORY_MAP_SIZE; + + // Separate the memory map so each image section has its own descriptor + SplitTable ( + &MemoryMapSize, + Context->MemoryMap, + DESCRIPTOR_SIZE, + &Context->ImageList, + NUMBER_OF_ADDITIONAL_DESCRIPTORS + ); + + // Ensure the updated memory map is valid + return IsMemoryMapValid (MemoryMapSize, Context->MemoryMap, &Context->Im= ageList); +} + +/** + Test the case where the image range contains multiple code sections and = does not perfectly align with + the existing memory descriptor. + + @param[in] Context Context containing the memory map and image record= pointers + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +MaxOutAdditionalDescriptors ( + IN UNIT_TEST_CONTEXT Context + ) +{ + IMAGE_PROPERTIES_RECORD *Image1; + IMAGE_PROPERTIES_RECORD *Image2; + IMAGE_PROPERTIES_RECORD *Image3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *AddCodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *AddCodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *AddCodeSectionInImage3; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext; + + TestContext =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)Context; + + Image1 =3D CR (TestContext->ImageList.ForwardLink, IMAGE_PROPERTIES_RECO= RD, Link, IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image2 =3D CR (Image1->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image3 =3D CR (Image2->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + + CodeSectionInImage1 =3D CR (Image1->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage2 =3D CR (Image2->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage3 =3D CR (Image3->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + + /////////////// + // Descriptor 1 + /////////////// + // | | | | | | | | + // | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE * 5 | + // | | | | | | | | + + Image1->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 1) + EFI_PAGE_SIZE; + Image1->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE - EFI_PAGE_SIZE; + Image1->CodeSegmentCount =3D 2; + CodeSectionInImage1->CodeSegmentBase =3D Image1->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage1->CodeSegmentSize =3D EFI_PAGE_SIZE; + TestContext->MemoryMap[1].Type =3D EfiBootServicesCode; + + AddCodeSectionInImage1 =3D AllocateZeroPool (sizeof (IM= AGE_PROPERTIES_RECORD_CODE_SECTION)); + AddCodeSectionInImage1->Signature =3D IMAGE_PROPERTIES_RECORD_CODE= _SECTION_SIGNATURE; + AddCodeSectionInImage1->CodeSegmentBase =3D CodeSectionInImage1->CodeSeg= mentBase + CodeSectionInImage1->CodeSegmentSize + EFI_PAGE_SIZE; + AddCodeSectionInImage1->CodeSegmentSize =3D EFI_PAGE_SIZE; + + InsertTailList (&Image1->CodeSegmentList, &AddCodeSectionInImage1->Link); + + /////////////// + // Descriptor 2 + /////////////// + // | | | | | | | | + // | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE * 5 | + // | | | | | | | | + + Image2->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 2) + EFI_PAGE_SIZE; + Image2->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE - EFI_PAGE_SIZE; + Image2->CodeSegmentCount =3D 2; + CodeSectionInImage2->CodeSegmentBase =3D Image2->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage2->CodeSegmentSize =3D EFI_PAGE_SIZE; + TestContext->MemoryMap[2].Type =3D EfiLoaderCode; + + AddCodeSectionInImage2 =3D AllocateZeroPool (sizeof (IM= AGE_PROPERTIES_RECORD_CODE_SECTION)); + AddCodeSectionInImage2->Signature =3D IMAGE_PROPERTIES_RECORD_CODE= _SECTION_SIGNATURE; + AddCodeSectionInImage2->CodeSegmentBase =3D CodeSectionInImage2->CodeSeg= mentBase + CodeSectionInImage2->CodeSegmentSize + EFI_PAGE_SIZE; + AddCodeSectionInImage2->CodeSegmentSize =3D EFI_PAGE_SIZE; + + InsertTailList (&Image2->CodeSegmentList, &AddCodeSectionInImage2->Link); + + /////////////// + // Descriptor 3 + /////////////// + // | | | | | | | | + // | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE * 5 | + // | | | | | | | | + + Image3->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 3) + EFI_PAGE_SIZE; + Image3->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE - EFI_PAGE_SIZE; + Image3->CodeSegmentCount =3D 2; + CodeSectionInImage3->CodeSegmentBase =3D Image3->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage3->CodeSegmentSize =3D EFI_PAGE_SIZE; + TestContext->MemoryMap[3].Type =3D EfiRuntimeServicesCode; + + AddCodeSectionInImage3 =3D AllocateZeroPool (sizeof (IM= AGE_PROPERTIES_RECORD_CODE_SECTION)); + AddCodeSectionInImage3->Signature =3D IMAGE_PROPERTIES_RECORD_CODE= _SECTION_SIGNATURE; + AddCodeSectionInImage3->CodeSegmentBase =3D CodeSectionInImage3->CodeSeg= mentBase + CodeSectionInImage3->CodeSegmentSize + EFI_PAGE_SIZE; + AddCodeSectionInImage3->CodeSegmentSize =3D EFI_PAGE_SIZE; + + InsertTailList (&Image3->CodeSegmentList, &AddCodeSectionInImage3->Link); + + UT_ASSERT_TRUE (SeparateAndCheck (TestContext)); + + return UNIT_TEST_PASSED; +} + +/** + Test the case where multiple image ranges lie within an existing memory = descriptor. + + @param[in] Context Context containing the memory map and image record= pointers + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +MultipleImagesInOneDescriptor ( + IN UNIT_TEST_CONTEXT Context + ) +{ + IMAGE_PROPERTIES_RECORD *Image1; + IMAGE_PROPERTIES_RECORD *Image2; + IMAGE_PROPERTIES_RECORD *Image3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage3; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext; + + TestContext =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)Context; + + Image1 =3D CR (TestContext->ImageList.ForwardLink, IMAGE_PROPERTIES_RECO= RD, Link, IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image2 =3D CR (Image1->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image3 =3D CR (Image2->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + + CodeSectionInImage1 =3D CR (Image1->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage2 =3D CR (Image2->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage3 =3D CR (Image3->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + + /////////////// + // Descriptor 1 + /////////////// + // | | | | | | | | | = | | | | + // | 4K PAGE | DATA | CODE | DATA | 4K PAGE | DATA | CODE | DATA | DATA = | CODE | DATA | 4K PAGE | + // | | | | | | | | | = | | | | + + Image1->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 1) + EFI_PAGE_SIZE; + Image1->ImageSize =3D EFI_PAGES_TO_SIZE (3); + Image1->CodeSegmentCount =3D 1; + CodeSectionInImage1->CodeSegmentBase =3D Image1->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage1->CodeSegmentSize =3D EFI_PAGE_SIZE; + TestContext->MemoryMap[1].Type =3D EfiBootServicesCode; + + Image2->ImageBase =3D Image1->ImageBase + Image1->Ima= geSize + EFI_PAGE_SIZE; + Image2->ImageSize =3D EFI_PAGES_TO_SIZE (3); + Image2->CodeSegmentCount =3D 1; + CodeSectionInImage2->CodeSegmentBase =3D Image2->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage2->CodeSegmentSize =3D EFI_PAGE_SIZE; + + Image3->ImageBase =3D Image2->ImageBase + Image2->Ima= geSize; + Image3->ImageSize =3D EFI_PAGES_TO_SIZE (3); + Image3->CodeSegmentCount =3D 1; + CodeSectionInImage3->CodeSegmentBase =3D Image3->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage3->CodeSegmentSize =3D EFI_PAGE_SIZE; + + UT_ASSERT_TRUE (SeparateAndCheck (TestContext)); + + return UNIT_TEST_PASSED; +} + +/** + Test the case where all image ranges do not fit perfectly within an exis= ting memory descriptor. + + @param[in] Context Context containing the memory map and image record= pointers + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +ImagesDontFitDescriptors ( + IN UNIT_TEST_CONTEXT Context + ) +{ + IMAGE_PROPERTIES_RECORD *Image1; + IMAGE_PROPERTIES_RECORD *Image2; + IMAGE_PROPERTIES_RECORD *Image3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage3; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext; + + TestContext =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)Context; + + Image1 =3D CR (TestContext->ImageList.ForwardLink, IMAGE_PROPERTIES_RECO= RD, Link, IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image2 =3D CR (Image1->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image3 =3D CR (Image2->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + + CodeSectionInImage1 =3D CR (Image1->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage2 =3D CR (Image2->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage3 =3D CR (Image3->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + + /////////////// + // Descriptor 1 + /////////////// + // | | | | | + // | 4K PAGE | DATA | CODE * 2 | DATA * 8 | + // | | | | | + + Image1->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 1) + EFI_PAGE_SIZE; + Image1->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE; + Image1->CodeSegmentCount =3D 1; + CodeSectionInImage1->CodeSegmentBase =3D Image1->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage1->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (2); + TestContext->MemoryMap[1].Type =3D EfiBootServicesCode; + + /////////////// + // Descriptor 3 + /////////////// + // | | | | | + // | DATA | CODE * 3 | DATA * 7 | 4K PAGE | + // | | | | | + + Image2->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 3); + Image2->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE; + Image2->CodeSegmentCount =3D 1; + CodeSectionInImage2->CodeSegmentBase =3D Image2->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage2->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (3); + TestContext->MemoryMap[3].Type =3D EfiLoaderCode; + + /////////////// + // Descriptor 4 + /////////////// + // | | | | | | + // | 4K PAGE | DATA | CODE * 2 | DATA * 7 | 4K PAGE | + // | | | | | | + + Image3->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 4) + EFI_PAGE_SIZE; + Image3->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE - EF= I_PAGE_SIZE - EFI_PAGE_SIZE; + Image3->CodeSegmentCount =3D 1; + CodeSectionInImage3->CodeSegmentBase =3D Image3->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage3->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (2); + TestContext->MemoryMap[4].Type =3D EfiRuntimeServicesCode; + + UT_ASSERT_TRUE (SeparateAndCheck (TestContext)); + + return UNIT_TEST_PASSED; +} + +/** + Test the case where all image ranges fit perfectly within an existing me= mory descriptor. + + @param[in] Context Context containing the memory map and image record= pointers + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +ImagesFitDescriptors ( + IN UNIT_TEST_CONTEXT Context + ) +{ + IMAGE_PROPERTIES_RECORD *Image1; + IMAGE_PROPERTIES_RECORD *Image2; + IMAGE_PROPERTIES_RECORD *Image3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage3; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext; + + TestContext =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)Context; + + Image1 =3D CR (TestContext->ImageList.ForwardLink, IMAGE_PROPERTIES_RECO= RD, Link, IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image2 =3D CR (Image1->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + Image3 =3D CR (Image2->Link.ForwardLink, IMAGE_PROPERTIES_RECORD, Link, = IMAGE_PROPERTIES_RECORD_SIGNATURE); + + CodeSectionInImage1 =3D CR (Image1->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage2 =3D CR (Image2->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + CodeSectionInImage3 =3D CR (Image3->CodeSegmentList.ForwardLink, IMAGE_P= ROPERTIES_RECORD_CODE_SECTION, Link, IMAGE_PROPERTIES_RECORD_CODE_SECTION_S= IGNATURE); + + /////////////// + // Descriptor 1 + /////////////// + // | | | | + // | DATA | CODE * 3 | DATA * 8 | + // | | | | + + Image1->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 1); + Image1->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE; + Image1->CodeSegmentCount =3D 1; + CodeSectionInImage1->CodeSegmentBase =3D Image1->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage1->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (3); + TestContext->MemoryMap[1].Type =3D EfiBootServicesCode; + + /////////////// + // Descriptor 2 + /////////////// + // | | | | + // | DATA | CODE * 4 | DATA * 7 | + // | | | | + + Image2->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 2); + Image2->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE; + Image2->CodeSegmentCount =3D 1; + CodeSectionInImage2->CodeSegmentBase =3D Image2->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage2->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (4); + TestContext->MemoryMap[2].Type =3D EfiLoaderCode; + + /////////////// + // Descriptor 3 + /////////////// + // | | | | + // | DATA | CODE * 3 | DATA * 8 | + // | | | | + + Image3->ImageBase =3D BASE_DESCRIPTOR_START_ADDRESS (= 3); + Image3->ImageSize =3D BASE_DESCRIPTOR_ENTRY_SIZE; + Image3->CodeSegmentCount =3D 1; + CodeSectionInImage3->CodeSegmentBase =3D Image3->ImageBase + EFI_PAGE_SI= ZE; + CodeSectionInImage3->CodeSegmentSize =3D EFI_PAGES_TO_SIZE (3); + TestContext->MemoryMap[3].Type =3D EfiRuntimeServicesCode; + + UT_ASSERT_TRUE (SeparateAndCheck (TestContext)); + + return UNIT_TEST_PASSED; +} + +/** + Free all allocated memory. + + @param[in] Context Context containing the memory map and image record= pointers +**/ +VOID +EFIAPI +TestCleanup ( + IN UNIT_TEST_CONTEXT Context + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + LIST_ENTRY *ImageRecordLink; + LIST_ENTRY *CodeSegmentListHead; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext; + + TestContext =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)Context; + ImageRecordLink =3D &TestContext->ImageList; + + while (!IsListEmpty (ImageRecordLink)) { + ImageRecord =3D CR ( + ImageRecordLink->ForwardLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; + while (!IsListEmpty (CodeSegmentListHead)) { + ImageRecordCodeSection =3D CR ( + CodeSegmentListHead->ForwardLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGN= ATURE + ); + RemoveEntryList (&ImageRecordCodeSection->Link); + FreePool (ImageRecordCodeSection); + } + + RemoveEntryList (&ImageRecord->Link); + FreePool (ImageRecord); + } + + if (TestContext->MemoryMap !=3D NULL) { + FreePool (TestContext->MemoryMap); + } +} + +/** + Create a generic image list with the proper signatures which will be cus= tomized for each test + and allocate the default memory map. + + @param[out] TestContext Context which will be passed to the test cases +**/ +STATIC +VOID +CreateBaseContextEntry ( + OUT IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *TestContext + ) +{ + IMAGE_PROPERTIES_RECORD *Image1; + IMAGE_PROPERTIES_RECORD *Image2; + IMAGE_PROPERTIES_RECORD *Image3; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage1; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage2; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CodeSectionInImage3; + + InitializeListHead (&TestContext->ImageList); + + Image1 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D)); + CodeSectionInImage1 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D_CODE_SECTION)); + + Image1->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + CodeSectionInImage1->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE; + InitializeListHead (&Image1->CodeSegmentList); + + InsertTailList (&TestContext->ImageList, &Image1->Link); + InsertTailList (&Image1->CodeSegmentList, &CodeSectionInImage1->Link); + + Image2 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D)); + CodeSectionInImage2 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D_CODE_SECTION)); + + Image2->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + CodeSectionInImage2->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE; + InitializeListHead (&Image2->CodeSegmentList); + + InsertTailList (&TestContext->ImageList, &Image2->Link); + InsertTailList (&Image2->CodeSegmentList, &CodeSectionInImage2->Link); + + Image3 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D)); + CodeSectionInImage3 =3D AllocateZeroPool (sizeof (IMAGE_PROPERTIES_RECOR= D_CODE_SECTION)); + + Image3->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + CodeSectionInImage3->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE; + InitializeListHead (&Image3->CodeSegmentList); + + InsertTailList (&TestContext->ImageList, &Image3->Link); + InsertTailList (&Image3->CodeSegmentList, &CodeSectionInImage3->Link); + + TestContext->MemoryMap =3D AllocateZeroPool (SPLIT_MEMORY_MAP_SIZE); + CopyMem (TestContext->MemoryMap, &BaseMemoryMap, BASE_MEMORY_MAP_SIZE); + + return; +} + +/** + Initialze the unit test framework, suite, and unit tests. + + @retval EFI_SUCCESS All test cases were dispatched. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available = to + initialize the unit tests. +**/ +STATIC +EFI_STATUS +EFIAPI +UnitTestingEntry ( + VOID + ) +{ + EFI_STATUS Status; + UNIT_TEST_FRAMEWORK_HANDLE Framework; + UNIT_TEST_SUITE_HANDLE ImagePropertiesRecordTests; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *Context1; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *Context2; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *Context3; + IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *Context4; + + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSIO= N)); + + Framework =3D NULL; + + Context1 =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)AllocateZeroPo= ol (sizeof (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT)); + Context2 =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)AllocateZeroPo= ol (sizeof (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT)); + Context3 =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)AllocateZeroPo= ol (sizeof (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT)); + Context4 =3D (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT *)AllocateZeroPo= ol (sizeof (IMAGE_PROPERTIES_RECORD_HOST_TEST_CONTEXT)); + + CreateBaseContextEntry (Context1); + CreateBaseContextEntry (Context2); + CreateBaseContextEntry (Context3); + CreateBaseContextEntry (Context4); + + // + // Start setting up the test framework for running the tests. + // + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCa= llerBaseName, UNIT_TEST_APP_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r\n= ", Status)); + goto EXIT; + } + + // + // Populate the Unit Test Suite. + // + Status =3D CreateUnitTestSuite (&ImagePropertiesRecordTests, Framework, = "Image Properties Record Tests", "ImagePropertiesRecordLib.SplitTable", NUL= L, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for the Image Prop= erties Record Tests\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // + // --------------Suite-----------Description--------------Name----------= Function--------Pre---Post-------------------Context----------- + // + AddTestCase (ImagePropertiesRecordTests, "All images fit perfectly into = existing descriptors", "ImagesFitDescriptors", ImagesFitDescriptors, NULL, = TestCleanup, Context1); + AddTestCase (ImagePropertiesRecordTests, "All images don't fit perfectly= into existing descriptors", "ImagesDontFitDescriptors", ImagesDontFitDescr= iptors, NULL, TestCleanup, Context2); + AddTestCase (ImagePropertiesRecordTests, "All Images are contined In sin= gle descriptor", "MultipleImagesInOneDescriptor", MultipleImagesInOneDescri= ptor, NULL, TestCleanup, Context3); + AddTestCase (ImagePropertiesRecordTests, "Multiple code sections each im= age", "MaxOutAdditionalDescriptors", MaxOutAdditionalDescriptors, NULL, Tes= tCleanup, Context4); + + // + // Execute the tests. + // + Status =3D RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/// +/// Avoid ECC error for function name that starts with lower case letter +/// +#define ImagePropertiesRecordLibUnitTestMain main + +/** + Standard POSIX C entry point for host based unit test execution. + + @param[in] Argc Number of arguments + @param[in] Argv Array of pointers to arguments + + @retval 0 Success + @retval other Error +**/ +INT32 +ImagePropertiesRecordLibUnitTestMain ( + IN INT32 Argc, + IN CHAR8 *Argv[] + ) +{ + return UnitTestingEntry (); +} diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePr= opertiesRecordLibUnitTestHost.inf b/MdeModulePkg/Library/ImagePropertiesRec= ordLib/UnitTest/ImagePropertiesRecordLibUnitTestHost.inf new file mode 100644 index 000000000000..cbc64a14bde7 --- /dev/null +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertie= sRecordLibUnitTestHost.inf @@ -0,0 +1,35 @@ +## @file +# Unit tests the SplitTable() ImagePropertiesRecordLib Logic +# +# Copyright (C) Microsoft Corporation. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION =3D 0x00010006 + BASE_NAME =3D ImagePropertiesRecordLibUnitTestHost + FILE_GUID =3D 45B39FAA-E25D-4724-A6F4-93C0C8588A80 + MODULE_TYPE =3D HOST_APPLICATION + VERSION_STRING =3D 1.0 + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 AARCH64 +# + +[Sources] + ImagePropertiesRecordLibUnitTestHost.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + MemoryAllocationLib + UnitTestLib + ImagePropertiesRecordLib diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test= /MdeModulePkgHostTest.dsc index 8fb982a2703d..2b8cbb867a73 100644 --- a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc +++ b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc @@ -54,6 +54,11 @@ [Components] DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf } =20 + MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertiesRe= cordLibUnitTestHost.inf { + + ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordL= ib/ImagePropertiesRecordLib.inf + } + # # Build HOST_APPLICATION Libraries # --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110656): https://edk2.groups.io/g/devel/message/110656 Mute This Topic: https://groups.io/mt/102368848/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110657+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110657+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031846; cv=none; d=zohomail.com; s=zohoarc; b=KF+YQIzQZBIf6BAvIlFJSBk28Xup5kvPhoBLJvyKzq8yeg+DeDg4eP3VJ8MdaKxZpIp7IwZ6AhDg6ttsjiKq/N1XAHtrtFS473Or0W6KL9MhCyUamvtkP0gAq3ERFQ03Hx8HVIeDNKPyEMKHWja49eP7a8EOskgPmM3uX9TGlG4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031846; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=xrlWGHFcS7JSZK75b2uAmd4PWkGqkLK1oxSp1YGGY/o=; b=CTgrJz/fZJba+nUcIvn+b9DYOP5GkjQflE0mKGezNEsypWz/4vOPk+W0KYvMOSzcHD9yIoA2gTLOC2ssg6WncfYTw3lhypHGUM8RDbUAvuOl9fbLMikn9cUgVie14PoNcT1PaVQumcns2QyNXJARrJ/ujvSVGSJjvMizXyEvBe0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110657+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031846602867.9213335462075; Fri, 3 Nov 2023 10:17:26 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=dggdGxWBJjjKCnNe9QxOMTnP8GwF0ClydLkccN05VbA=; 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=1699031846; v=1; b=B05ETV3Jr/GjIUTZWnTJwYIqdORIqlx5y+xWzV4luOR2bKzJyYPMAnxoCKwo30F4qq4rADVu XKAdbqVIF+91GR1czOJk1FLLZNJU+GeiEkuzSI+mNt7IGTnuB062E7B4DxEkF8S1wub55prJObz CipMEi0P4mgOqtYCZU/u6Xa4= X-Received: by 127.0.0.2 with SMTP id s1QvYY1788612xFh5JWbHisn; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by mx.groups.io with SMTP id smtpd.web10.59754.1699031843531937435 for ; Fri, 03 Nov 2023 10:17:23 -0700 X-Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-1cc1ee2d8dfso20656655ad.3 for ; Fri, 03 Nov 2023 10:17:23 -0700 (PDT) X-Gm-Message-State: sGo8IygqsDkkP3vgARjlcfRjx1787277AA= X-Google-Smtp-Source: AGHT+IFAKgq4MM+OIpGM4VAxKf9v4L6YdnVnvgoT6L4enryW+8eAIJCj6aK8Hfn/dHgZuof2pOkK0A== X-Received: by 2002:a17:902:e5c2:b0:1cc:3fc9:7d09 with SMTP id u2-20020a170902e5c200b001cc3fc97d09mr18941341plf.15.1699031842856; Fri, 03 Nov 2023 10:17:22 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:22 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 09/14] MdeModulePkg: Fix Bugs in MAT Logic Date: Fri, 3 Nov 2023 10:17:01 -0700 Message-ID: <20231103171706.148-10-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848944100039 Content-Type: text/plain; charset="utf-8" Fix the bugs in the MAT logic before switching the UEFI and SMM MAT logic to use the new library. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c |= 2 +- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c |= 98 ++++++++++---------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 51630f504ea1..af6c26244cc0 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -517,7 +517,7 @@ CoreGetMemoryMapWithSeparatedImageSection ( =20 CoreAcquiremMemoryAttributesTableLock (); =20 - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 1) * mImagePropertiesPrivateData.ImageRecordCount; + AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 3) * mImagePropertiesPrivateData.ImageRecordCount; =20 OldMemoryMapSize =3D *MemoryMapSize; Status =3D CoreGetMemoryMap (MemoryMapSize, MemoryMap, MapKey,= DescriptorSize, DescriptorVersion); diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 9fb3b922038f..379eb0c6cccd 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -277,7 +277,7 @@ GetMaxSplitRecordCount ( break; } =20 - SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 1); + SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 3); PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); =20 @@ -323,7 +323,6 @@ SplitRecord ( UINT64 PhysicalEnd; UINTN NewRecordCount; UINTN TotalNewRecordCount; - BOOLEAN IsLastRecordData; =20 if (MaxSplitRecordCount =3D=3D 0) { CopyMem (NewRecord, OldRecord, DescriptorSize); @@ -344,35 +343,16 @@ SplitRecord ( NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart, ImageRecordList); if (NewImageRecord =3D=3D NULL) { // - // No more image covered by this range, stop + // No more images cover this range, check if we've reached the end o= f the old descriptor. If not, + // add the remaining range to the new descriptor list. // - if ((PhysicalEnd > PhysicalStart) && (ImageRecord !=3D NULL)) { - // - // If this is still address in this record, need record. - // - NewRecord =3D PREVIOUS_MEMORY_DESCRIPTOR (NewRecord, Descri= ptorSize); - IsLastRecordData =3D FALSE; - if ((NewRecord->Attribute & EFI_MEMORY_XP) !=3D 0) { - IsLastRecordData =3D TRUE; - } - - if (IsLastRecordData) { - // - // Last record is DATA, just merge it. - // - NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - NewRe= cord->PhysicalStart); - } else { - // - // Last record is CODE, create a new DATA entry. - // - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, = DescriptorSize); - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_X= P; - TotalNewRecordCount++; - } + if (PhysicalEnd > PhysicalStart) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (PhysicalEnd - Physica= lStart); + NewRecord->Attribute =3D TempRecord.Attribute; + TotalNewRecordCount++; } =20 break; @@ -380,6 +360,24 @@ SplitRecord ( =20 ImageRecord =3D NewImageRecord; =20 + // + // Update PhysicalStart to exclude the portion before the image buffer + // + if (TempRecord.PhysicalStart < ImageRecord->ImageBase) { + NewRecord->Type =3D TempRecord.Type; + NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; + NewRecord->VirtualStart =3D 0; + NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecord->ImageBase = - TempRecord.PhysicalStart); + NewRecord->Attribute =3D TempRecord.Attribute; + TotalNewRecordCount++; + + PhysicalStart =3D ImageRecord->ImageBase; + TempRecord.PhysicalStart =3D PhysicalStart; + TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalS= tart); + + NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord + Descrip= torSize); + } + // // Set new record // @@ -465,11 +463,12 @@ SplitTable ( { INTN IndexOld; INTN IndexNew; + INTN IndexNewStarting; UINTN MaxSplitRecordCount; UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; + UINTN TotalSkippedRecords; =20 - TotalSplitRecordCount =3D 0; + TotalSkippedRecords =3D 0; // // Let old record point to end of valid MemoryMap buffer. // @@ -477,7 +476,8 @@ SplitTable ( // // Let new record point to end of full MemoryMap buffer. // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfAdditiona= lDescriptors; + IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + NumberOfA= dditionalDescriptors; + IndexNewStarting =3D IndexNew; for ( ; IndexOld >=3D 0; IndexOld--) { MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize), ImageRecordList); // @@ -491,16 +491,14 @@ SplitTable ( DescriptorSize, ImageRecordList ); - // - // Adjust IndexNew according to real split. - // - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitRec= ordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - RealSplitRecordCount * DescriptorSize - ); - IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount; - TotalSplitRecordCount +=3D RealSplitRecordCount; + + // If we didn't utilize all the extra allocated descriptor slots, set = the physical address of the unused slots + // to MAX_ADDRESS so they are moved to the bottom of the list when sor= ting. + for ( ; RealSplitRecordCount < MaxSplitRecordCount; RealSplitRecordCou= nt++) { + ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + ((IndexNew + RealSpl= itRecordCount + 1) * DescriptorSize)))->PhysicalStart =3D MAX_ADDRESS; + TotalSkippedRecords++; + } + IndexNew--; } =20 @@ -509,16 +507,16 @@ SplitTable ( // CopyMem ( MemoryMap, - (UINT8 *)MemoryMap + (NumberOfAdditionalDescriptors - TotalSplitRecord= Count) * DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize + (UINT8 *)MemoryMap + ((IndexNew + 1) * DescriptorSize), + (IndexNewStarting - IndexNew) * DescriptorSize ); =20 - *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; + // + // Sort from low to high to filter out the MAX_ADDRESS records. + // + SortMemoryMap (MemoryMap, (IndexNewStarting - IndexNew) * DescriptorSize= , DescriptorSize); =20 - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); + *MemoryMapSize =3D (IndexNewStarting - IndexNew - TotalSkippedRecords) *= DescriptorSize; =20 return; } --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110657): https://edk2.groups.io/g/devel/message/110657 Mute This Topic: https://groups.io/mt/102368849/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110658+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110658+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031846; cv=none; d=zohomail.com; s=zohoarc; b=cuNXlu9Zcp+sXCQINvvxBHuM+XoAIhqiY1Axz8JBqZLTnSinscrT+n0+4Z8fl7RYW4c23lf0gU2iUnoy6oZ1UlBGp2SUzrahTdWYUN8XGGoMH8k5YSXjK+HogWdlIyr10ksHMRdb6uajWNC2Em4WiW2PI9fL8RsxwcLQfpzMGKY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031846; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=Ht4FBttdXeyfu0CFy0HO8+gZYIyRFp54Nnno5BHcZoM=; b=P3XsAUoltnCXHLODolVZTWPPm0H4IS1+f0MJoBapuVIMgzAAGNRv9jUm70JzWvE5V+s3bGjuS0fTYS5yh43SNPzP7wZBEjBiby7MTZdoeixKjBvr8xse5v0zxqkrXXcrQ+zdYgQgWgEH+pDld88MQeDdDTH/U7gUq9x7y1WCS0k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110658+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031846503512.0858771360689; Fri, 3 Nov 2023 10:17:26 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=E+ZP0GC/xqJHoYp1PHtG3s+UjvgFl7muukLfzYN0nVY=; 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=1699031846; v=1; b=vqV7bImn4PfQ1qZsvVUQT0cx5aUMD/JccKAv0JMlK2fP2GSOfUqU+FINAB2V1SgRJb5dkmJc +EoaL8TCZT1AsZss5qZWshe6BcbpOHUhug4kF/4eYrlEOoEq50OuBQ1UnVUsxJVVFUQDN5cOXIW qyaay2zyMKJNKyCHGYX+HS7g= X-Received: by 127.0.0.2 with SMTP id RIwGYY1788612xejEbGj0DB2; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by mx.groups.io with SMTP id smtpd.web10.59755.1699031844474691411 for ; Fri, 03 Nov 2023 10:17:24 -0700 X-Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1cc394f4cdfso18311325ad.0 for ; Fri, 03 Nov 2023 10:17:24 -0700 (PDT) X-Gm-Message-State: jenm3isXLZK6Z6Jvu1aKURORx1787277AA= X-Google-Smtp-Source: AGHT+IGeVZT/7tcUTDZEs5R7MEvb/nJGaoo610NwF505lReUbwiF7c33sLNpwMQjCF5nk+m31yemUg== X-Received: by 2002:a17:902:d2d2:b0:1cc:5ac3:8544 with SMTP id n18-20020a170902d2d200b001cc5ac38544mr14420557plc.28.1699031843631; Fri, 03 Nov 2023 10:17:23 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:23 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi Subject: [edk2-devel] [PATCH v4 10/14] MdeModulePkg: Add NULL checks and Return Status to ImagePropertiesRecordLib Date: Fri, 3 Nov 2023 10:17:02 -0700 Message-ID: <20231103171706.148-11-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848964100040 Content-Type: text/plain; charset="utf-8" Update function headers to clarify the contract of each function and improve readability. Add NULL checks to all functions that take a pointer as an argument. Add return status to functions that may need to return early due to invalid input. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c |= 290 ++++++++++++-------- MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h |= 137 ++++----- 2 files changed, 246 insertions(+), 181 deletions(-) diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 379eb0c6cccd..c9378679e7bb 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -22,14 +22,13 @@ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) =20 /** - Converts a number of EFI_PAGEs to a size in bytes. + Converts a number of pages to a size in bytes. =20 NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. =20 - @param Pages The number of EFI_PAGES. + @param[in] Pages The number of EFI_PAGES. =20 - @return The number of bytes associated with the number of EFI_PAGEs spe= cified - by Pages. + @retval The number of bytes associated with the input number of pages. **/ STATIC UINT64 @@ -45,10 +44,9 @@ EfiPagesToSize ( =20 NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. =20 - @param Size A size in bytes. + @param[in] Size A size in bytes. =20 - @return The number of EFI_PAGESs associated with the number of bytes sp= ecified - by Size. + @retval The number of pages associated with the input number of bytes. =20 **/ STATIC @@ -61,12 +59,12 @@ EfiSizeToPages ( } =20 /** - Sort memory map entries based upon PhysicalStart, from low to high. + Sort memory map entries based upon PhysicalStart from low to high. =20 - @param MemoryMap A pointer to the buffer in which firmware= places - the current memory map. - @param MemoryMapSize Size, in bytes, of the MemoryMap buffer. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param[in, out] MemoryMap A pointer to the buffer in which firmwar= e places + the current memory map. + @param[in] MemoryMapSize Size, in bytes, of the MemoryMap buffer. + @param[in] DescriptorSize Size, in bytes, of an individual EFI_MEM= ORY_DESCRIPTOR. **/ STATIC VOID @@ -105,11 +103,12 @@ SortMemoryMap ( /** Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. =20 - @param Buffer Start Address - @param Length Address length - @param ImageRecordList Image record list + @param[in] Buffer Starting Address + @param[in] Length Length to check + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries to= check against + the memory range Buffer -> Buffer + Length =20 - @return first image record covered by [buffer, length] + @retval The first image record covered by [Buffer, Length] **/ STATIC IMAGE_PROPERTIES_RECORD * @@ -144,17 +143,19 @@ GetImageRecordByAddress ( } =20 /** - Set the memory map to new entries, according to one old entry, - based upon PE code section and data section in image record + Break up the input OldRecord into multiple new records based on the code + and data sections in the input ImageRecord. =20 - @param ImageRecord An image record whose [ImageBase, ImageSi= ze] covered - by old memory map entry. - @param NewRecord A pointer to several new memory map entri= es. - The caller gurantee the buffer size be 1 + - (SplitRecordCount * DescriptorSize) calcu= lated - below. - @param OldRecord A pointer to one old memory map entry. - @param DescriptorSize Size, in bytes, of an individual EFI_MEMO= RY_DESCRIPTOR. + @param[in] ImageRecord An IMAGE_PROPERTIES_RECORD whose Ima= geBase and + ImageSize is covered by by OldRecord. + @param[in, out] NewRecord A pointer to several new memory map = entries. + The caller gurantee the buffer size = be 1 + + (SplitRecordCount * DescriptorSize) = calculated + below. + @param[in] OldRecord A pointer to one old memory map entr= y. + @param[in] DescriptorSize The size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. + + @retval The number of new descriptors created. **/ STATIC UINTN @@ -244,16 +245,16 @@ SetNewRecord ( } =20 /** - Return the max number of new splitted entries, according to one old entr= y, - based upon PE code section and data section. + Return the maximum number of new entries required to describe the code a= nd data sections + of all images covered by the input OldRecord. =20 - @param OldRecord A pointer to one old memory map entry. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries= used when searching - for an image record contained by the memo= ry range described in - the existing EFI memory map descriptor Ol= dRecord + @param[in] OldRecord A pointer to one old memory map entry. + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries = used when searching + for an image record contained by the memor= y range described by + OldRecord =20 - @retval 0 no entry need to be splitted. - @return the max number of new splitted entries + @retval The maximum number of new descriptors required to describe the = code and data sections + of all images covered by OldRecord. **/ STATIC UINTN @@ -289,22 +290,20 @@ GetMaxSplitRecordCount ( } =20 /** - Split the memory map to new entries, according to one old entry, - based upon PE code section and data section. + Split the memory map into new entries based upon the PE code and data se= ctions + in ImageRecordList covered by the input OldRecord. =20 - @param OldRecord A pointer to one old memory map entr= y. - @param NewRecord A pointer to several new memory map = entries. - The caller gurantee the buffer size = be 1 + - (SplitRecordCount * DescriptorSize) = calculated - below. - @param MaxSplitRecordCount The max number of splitted entries - @param DescriptorSize Size, in bytes, of an individual EFI= _MEMORY_DESCRIPTOR. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECORD en= tries used when searching - for an image record contained by the= memory range described in - the existing EFI memory map descript= or OldRecord + @param[in] OldRecord A pointer to one old memory map = entry. + @param[in, out] NewRecord A pointer to several new memory = map entries. + The caller gurantee the buffer s= ize be + (SplitRecordCount * DescriptorSi= ze). + @param[in] MaxSplitRecordCount The maximum number of entries po= st-split. + @param[in] DescriptorSize The size, in bytes, of an indivi= dual EFI_MEMORY_DESCRIPTOR. + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching + for an image record contained by= the memory range described in + the existing EFI memory map desc= riptor OldRecord =20 - @retval 0 no entry is splitted. - @return the real number of splitted record. + @retval The number of split entries. **/ STATIC UINTN @@ -402,56 +401,47 @@ SplitRecord ( } =20 /** - Split the original memory map, and add more entries to describe PE code = section and data section. - This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. - This function will merge entries with same attributes finally. + Split the original memory map and add more entries to describe PE code + and data sections for each image in the input ImageRecordList. =20 - NOTE: It assumes PE code/data section are page aligned. - NOTE: It assumes enough entry is prepared for new memory map. + NOTE: This function assumes PE code/data section are page aligned. + NOTE: This function assumes there are enough entries for the new memory = map. =20 - Split table: - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - =3D=3D> - +---------------+ - | Record X | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF1 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF2 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record Y | - +---------------+ + | | | | | | | | + | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE | + | | | | | | | | + Assume the above memory region is the result of one split memory map des= criptor. It's unlikely + that a linker will orient an image this way, but the caller must assume = the worst case scenario. + This image layout example contains code sections oriented in a way that = maximizes the number of + descriptors which would be required to describe each section. To ensure = we have enough space + for every descriptor of the broken up memory map, the caller must assume= that every image will + have the maximum number of code sections oriented in a way which maximiz= es the number of data + sections with unrelated memory regions flanking each image within a sing= le descriptor. =20 - @param MemoryMapSize A pointer to the size, in bytes,= of the - MemoryMap buffer. On input, this= is the size of - old MemoryMap before split. The = actual buffer - size of MemoryMap is MemoryMapSi= ze + - (AdditionalRecordCount * Descrip= torSize) calculated - below. On output, it is the size= of new MemoryMap - after split. - @param MemoryMap A pointer to the buffer in which= firmware places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching - for an image record contained by= the memory range described in - EFI memory map descriptors. - @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. + Given an image record list, the caller should use the following formula = when allocating extra descriptors: + NumberOfAdditionalDescriptors =3D (MemoryMapSize / DescriptorSize) + + ((2 * + 3) * ) + + @param[in, out] MemoryMapSize IN: The size, in bytes= , of the old memory map before the split. + OUT: The size, in bytes= , of the used descriptors of the split + memory map + @param[in, out] MemoryMap IN: A pointer to the b= uffer containing the current memory map. + This buffer must h= ave enough space to accomodate the "worst case" + scenario where eve= ry image in ImageRecordList needs a new descriptor + to describe its co= de and data sections. + OUT: A pointer to the u= pdated memory map with separated image section + descriptors. + @param[in] DescriptorSize The size, in bytes, of a= n individual EFI_MEMORY_DESCRIPTOR. + @param[in] ImageRecordList A list of IMAGE_PROPERTI= ES_RECORD entries used when searching + for an image record cont= ained by the memory range described in + EFI memory map descripto= rs. + @param[in] NumberOfAdditionalDescriptors The number of unused des= criptors at the end of the input MemoryMap. + The formula in the descr= iption should be used to calculate this value. + + @retval EFI_SUCCESS The memory map was succe= ssfully split. + @retval EFI_INVALID_PARAMETER MemoryMapSize, MemoryMap= , or ImageRecordList was NULL. **/ -VOID +EFI_STATUS EFIAPI SplitTable ( IN OUT UINTN *MemoryMapSize, @@ -468,6 +458,10 @@ SplitTable ( UINTN RealSplitRecordCount; UINTN TotalSkippedRecords; =20 + if ((MemoryMapSize =3D=3D NULL) || (MemoryMap =3D=3D NULL) || (ImageReco= rdList =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + TotalSkippedRecords =3D 0; // // Let old record point to end of valid MemoryMap buffer. @@ -518,16 +512,19 @@ SplitTable ( =20 *MemoryMapSize =3D (IndexNewStarting - IndexNew - TotalSkippedRecords) *= DescriptorSize; =20 - return; + return EFI_SUCCESS; } =20 /** - Swap two code sections in image record. + Swap two code sections in a single IMAGE_PROPERTIES_RECORD. =20 - @param FirstImageRecordCodeSection first code section in image record - @param SecondImageRecordCodeSection second code section in image reco= rd + @param[in] FirstImageRecordCodeSection The first code section + @param[in] SecondImageRecordCodeSection The second code section + + @retval EFI_SUCCESS The code sections were swappe= d successfully + @retval EFI_INVALID_PARAMETER FirstImageRecordCodeSection o= r SecondImageRecordCodeSection is NULL **/ -VOID +EFI_STATUS EFIAPI SwapImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, @@ -536,6 +533,10 @@ SwapImageRecordCodeSection ( { IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; =20 + if ((FirstImageRecordCodeSection =3D=3D NULL) || (SecondImageRecordCodeS= ection =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; =20 @@ -544,19 +545,26 @@ SwapImageRecordCodeSection ( =20 SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; + + return EFI_SUCCESS; } =20 /** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. + Sort the code sections in the input ImageRecord based upon CodeSegmentBa= se from low to high. =20 - @param ImageRecord image record to be sorted + @param[in] ImageRecord IMAGE_PROPERTIES_RECORD to be sorted + + @retval EFI_SUCCESS The code sections in the input ImageReco= rd were sorted successfully + @retval EFI_ABORTED An error occurred while sorting the code= sections in the input ImageRecord + @retval EFI_INVALID_PARAMETER ImageRecord is NULL **/ -VOID +EFI_STATUS EFIAPI SortImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD *ImageRecord ) { + EFI_STATUS Status; IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; LIST_ENTRY *ImageRecordCodeSectionLink; @@ -564,6 +572,10 @@ SortImageRecordCodeSection ( LIST_ENTRY *ImageRecordCodeSectionEndLink; LIST_ENTRY *ImageRecordCodeSectionList; =20 + if (ImageRecord =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; =20 ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLi= nk; @@ -584,7 +596,11 @@ SortImageRecordCodeSection ( IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE ); if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { - SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); + Status =3D SwapImageRecordCodeSection (ImageRecordCodeSection, Nex= tImageRecordCodeSection); + if (EFI_ERROR (Status)) { + ASSERT_EFI_ERROR (Status); + return EFI_ABORTED; + } } =20 NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; @@ -593,15 +609,20 @@ SortImageRecordCodeSection ( ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; } + + return EFI_SUCCESS; } =20 /** - Check if code section in image record is valid. + Check if the code sections in the input ImageRecord are valid. + The code sections are valid if they don't overlap, are contained + within the the ImageRecord's ImageBase and ImageSize, and are + contained within the MAX_ADDRESS. =20 - @param ImageRecord image record to be checked + @param[in] ImageRecord IMAGE_PROPERTIES_RECORD to be checked =20 - @retval TRUE image record is valid - @retval FALSE image record is invalid + @retval TRUE The code sections in the input ImageRecord are valid + @retval FALSE The code sections in the input ImageRecord are invalid **/ BOOLEAN EFIAPI @@ -615,6 +636,10 @@ IsImageRecordCodeSectionValid ( LIST_ENTRY *ImageRecordCodeSectionEndLink; LIST_ENTRY *ImageRecordCodeSectionList; =20 + if (ImageRecord =3D=3D NULL) { + return FALSE; + } + DEBUG ((DEBUG_VERBOSE, "ImageCode SegmentCount - 0x%x\n", ImageRecord->C= odeSegmentCount)); =20 ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; @@ -661,10 +686,13 @@ IsImageRecordCodeSectionValid ( /** Swap two image records. =20 - @param FirstImageRecord first image record. - @param SecondImageRecord second image record. + @param[in] FirstImageRecord The first image record. + @param[in] SecondImageRecord The second image record. + + @retval EFI_SUCCESS The image records were swapped successful= ly + @retval EFI_INVALID_PARAMETER FirstImageRecord or SecondImageRecord is = NULL **/ -VOID +EFI_STATUS EFIAPI SwapImageRecord ( IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, @@ -673,6 +701,10 @@ SwapImageRecord ( { IMAGE_PROPERTIES_RECORD TempImageRecord; =20 + if ((FirstImageRecord =3D=3D NULL) || (SecondImageRecord =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; @@ -686,14 +718,19 @@ SwapImageRecord ( SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; =20 SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); + return EFI_SUCCESS; } =20 /** - Sort image record based upon the ImageBase from low to high. + Sort the input ImageRecordList based upon the ImageBase from low to high. =20 - @param ImageRecordList Image record list to be sorted + @param[in] ImageRecordList Image record list to be sorted + + @retval EFI_SUCCESS The image record list was sorted successfu= lly + @retval EFI_ABORTED An error occurred while sorting the image = record list + @retval EFI_INVALID_PARAMETER ImageRecordList is NULL **/ -VOID +EFI_STATUS EFIAPI SortImageRecord ( IN LIST_ENTRY *ImageRecordList @@ -704,6 +741,11 @@ SortImageRecord ( LIST_ENTRY *ImageRecordLink; LIST_ENTRY *NextImageRecordLink; LIST_ENTRY *ImageRecordEndLink; + EFI_STATUS Status; + + if (ImageRecordList =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } =20 ImageRecordLink =3D ImageRecordList->ForwardLink; NextImageRecordLink =3D ImageRecordLink->ForwardLink; @@ -724,7 +766,11 @@ SortImageRecord ( =20 ); if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { - SwapImageRecord (ImageRecord, NextImageRecord); + Status =3D SwapImageRecord (ImageRecord, NextImageRecord); + if (EFI_ERROR (Status)) { + ASSERT_EFI_ERROR (Status); + return EFI_ABORTED; + } } =20 NextImageRecordLink =3D NextImageRecordLink->ForwardLink; @@ -733,16 +779,20 @@ SortImageRecord ( ImageRecordLink =3D ImageRecordLink->ForwardLink; NextImageRecordLink =3D ImageRecordLink->ForwardLink; } + + return EFI_SUCCESS; } =20 /** Find image record according to image base and size. =20 - @param ImageBase Base of PE image - @param ImageSize Size of PE image - @param ImageRecordList Image record list to be searched + @param[in] ImageBase Base of PE image + @param[in] ImageSize Size of PE image + @param[in] ImageRecordList Image record list to be searched =20 - @return image record + @retval NULL No IMAGE_PROPERTIES_RECORD matches ImageBase + and ImageSize in the input ImageRecordList + @retval Other The found IMAGE_PROPERTIES_RECORD **/ IMAGE_PROPERTIES_RECORD * EFIAPI @@ -755,6 +805,10 @@ FindImageRecord ( IMAGE_PROPERTIES_RECORD *ImageRecord; LIST_ENTRY *ImageRecordLink; =20 + if (ImageRecordList =3D=3D NULL) { + return NULL; + } + for (ImageRecordLink =3D ImageRecordList->ForwardLink; ImageRecordLink !=3D ImageRecordList; ImageRecordLink =3D ImageRecordLink->ForwardLink) diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index 9139c7b6c05d..b6365662646a 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -32,56 +32,47 @@ typedef struct { } IMAGE_PROPERTIES_RECORD; =20 /** - Split the original memory map, and add more entries to describe PE code = section and data section. - This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. - This function will merge entries with same attributes finally. + Split the original memory map and add more entries to describe PE code + and data sections for each image in the input ImageRecordList. =20 - NOTE: It assumes PE code/data section are page aligned. - NOTE: It assumes enough entry is prepared for new memory map. + NOTE: This function assumes PE code/data section are page aligned. + NOTE: This function assumes there are enough entries for the new memory = map. =20 - Split table: - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - =3D=3D> - +---------------+ - | Record X | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF1 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF2 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record Y | - +---------------+ + | | | | | | | | + | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE | + | | | | | | | | + Assume the above memory region is the result of one split memory map des= criptor. It's unlikely + that a linker will orient an image this way, but the caller must assume = the worst case scenario. + This image layout example contains code sections oriented in a way that = maximizes the number of + descriptors which would be required to describe each section. To ensure = we have enough space + for every descriptor of the broken up memory map, the caller must assume= that every image will + have the maximum number of code sections oriented in a way which maximiz= es the number of data + sections with unrelated memory regions flanking each image within a sing= le descriptor. =20 - @param MemoryMapSize A pointer to the size, in bytes,= of the - MemoryMap buffer. On input, this= is the size of - old MemoryMap before split. The = actual buffer - size of MemoryMap is MemoryMapSi= ze + - (AdditionalRecordCount * Descrip= torSize) calculated - below. On output, it is the size= of new MemoryMap - after split. - @param MemoryMap A pointer to the buffer in which= firmware places - the current memory map. - @param DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. - @param ImageRecordList A list of IMAGE_PROPERTIES_RECOR= D entries used when searching - for an image record contained by= the memory range described in - EFI memory map descriptors. - @param NumberOfAdditionalDescriptors The number of unused descriptors= at the end of the input MemoryMap. + Given an image record list, the caller should use the following formula = when allocating extra descriptors: + NumberOfAdditionalDescriptors =3D (MemoryMapSize / DescriptorSize) + + ((2 * + 3) * ) + + @param[in, out] MemoryMapSize IN: The size, in bytes= , of the old memory map before the split. + OUT: The size, in bytes= , of the used descriptors of the split + memory map + @param[in, out] MemoryMap IN: A pointer to the b= uffer containing the current memory map. + This buffer must h= ave enough space to accomodate the "worst case" + scenario where eve= ry image in ImageRecordList needs a new descriptor + to describe its co= de and data sections. + OUT: A pointer to the u= pdated memory map with separated image section + descriptors. + @param[in] DescriptorSize The size, in bytes, of a= n individual EFI_MEMORY_DESCRIPTOR. + @param[in] ImageRecordList A list of IMAGE_PROPERTI= ES_RECORD entries used when searching + for an image record cont= ained by the memory range described in + EFI memory map descripto= rs. + @param[in] NumberOfAdditionalDescriptors The number of unused des= criptors at the end of the input MemoryMap. + The formula in the descr= iption should be used to calculate this value. + + @retval EFI_SUCCESS The memory map was succe= ssfully split. + @retval EFI_INVALID_PARAMETER MemoryMapSize, MemoryMap= , or ImageRecordList was NULL. **/ -VOID +EFI_STATUS EFIAPI SplitTable ( IN OUT UINTN *MemoryMapSize, @@ -92,23 +83,30 @@ SplitTable ( ); =20 /** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. + Sort the code sections in the input ImageRecord based upon CodeSegmentBa= se from low to high. =20 - @param ImageRecord image record to be sorted + @param[in] ImageRecord IMAGE_PROPERTIES_RECORD to be sorted + + @retval EFI_SUCCESS The code sections in the input ImageReco= rd were sorted successfully + @retval EFI_ABORTED An error occurred while sorting the code= sections in the input ImageRecord + @retval EFI_INVALID_PARAMETER ImageRecord is NULL **/ -VOID +EFI_STATUS EFIAPI SortImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD *ImageRecord ); =20 /** - Check if code section in image record is valid. + Check if the code sections in the input ImageRecord are valid. + The code sections are valid if they don't overlap, are contained + within the the ImageRecord's ImageBase and ImageSize, and are + contained within the MAX_ADDRESS. =20 - @param ImageRecord image record to be checked + @param[in] ImageRecord IMAGE_PROPERTIES_RECORD to be checked =20 - @retval TRUE image record is valid - @retval FALSE image record is invalid + @retval TRUE The code sections in the input ImageRecord are valid + @retval FALSE The code sections in the input ImageRecord are invalid **/ BOOLEAN EFIAPI @@ -117,11 +115,15 @@ IsImageRecordCodeSectionValid ( ); =20 /** - Sort image record based upon the ImageBase from low to high. + Sort the input ImageRecordList based upon the ImageBase from low to high. =20 - @param ImageRecordList Image record list to be sorted + @param[in] ImageRecordList Image record list to be sorted + + @retval EFI_SUCCESS The image record list was sorted successfu= lly + @retval EFI_ABORTED An error occurred while sorting the image = record list + @retval EFI_INVALID_PARAMETER ImageRecordList is NULL **/ -VOID +EFI_STATUS EFIAPI SortImageRecord ( IN LIST_ENTRY *ImageRecordList @@ -132,8 +134,11 @@ SortImageRecord ( =20 @param[in] FirstImageRecord The first image record. @param[in] SecondImageRecord The second image record. + + @retval EFI_SUCCESS The image records were swapped successful= ly + @retval EFI_INVALID_PARAMETER FirstImageRecord or SecondImageRecord is = NULL **/ -VOID +EFI_STATUS EFIAPI SwapImageRecord ( IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, @@ -145,8 +150,11 @@ SwapImageRecord ( =20 @param[in] FirstImageRecordCodeSection The first code section @param[in] SecondImageRecordCodeSection The second code section + + @retval EFI_SUCCESS The code sections were swappe= d successfully + @retval EFI_INVALID_PARAMETER FirstImageRecordCodeSection o= r SecondImageRecordCodeSection is NULL **/ -VOID +EFI_STATUS EFIAPI SwapImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, @@ -154,13 +162,16 @@ SwapImageRecordCodeSection ( ); =20 /** - Find image record according to image base and size. + Find image properties record according to image base and size in the + input ImageRecordList. =20 - @param ImageBase Base of PE image - @param ImageSize Size of PE image - @param ImageRecordList Image record list to be searched + @param[in] ImageBase Base of PE image + @param[in] ImageSize Size of PE image + @param[in] ImageRecordList Image record list to be searched =20 - @return image record + @retval NULL No IMAGE_PROPERTIES_RECORD matches ImageBase + and ImageSize in the input ImageRecordList + @retval Other The found IMAGE_PROPERTIES_RECORD **/ IMAGE_PROPERTIES_RECORD * EFIAPI --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110658): https://edk2.groups.io/g/devel/message/110658 Mute This Topic: https://groups.io/mt/102368850/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110659+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110659+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031847; cv=none; d=zohomail.com; s=zohoarc; b=O+W/aNCG5UYwvY6xPc2UWVmH6o/5xvX4NEMmd5jRmFoGi14HTy+M7/294ExgkJXaAfA8TvKS4l2bLXgClC9Wi36pnVNwAaokBTZB92toFdH/MacAKgPKh5T8tK36yJIxy7FpDH1BTdZx2DLQdVga+JLsNtbmZTyUXlNUBv0o3Fc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031847; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=o4QfmAEkzypQt0ZT/W+JUJjvaqsMOIMQ/zolZhu2I+8=; b=cxwUAYCaDJ+vvjPMeqDKFtpqRodOd1EYCC3nHS999NN1Vqc33JEJwDtvBqlI2q1cEMvd95iCsNgXDZvtcLRyXzxkGLA4y05YjjLxkntcvJSOkq+Ob3WPHdMqPzM+nAy7a0Gt/GdnhWvf0S05xwbTXXMmCMDJsZ3nOaYwu8WhpC8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110659+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031847345678.3453865442185; Fri, 3 Nov 2023 10:17:27 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=8/tOeaMqyx31QmZxKJPTfeGmyT4KtIwVhKl0M6YOQP4=; 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=1699031846; v=1; b=WLQTCbaRD6lLji2mLeADBTkEqlxooFLlm+6A85/jZUlzLy56EAsqeQMtGcqyJD7+ia6mBmiY JmO3YQO1T/+E1s6eS75ecoXeS+mBAO/64sqO1CwFRw8vf3Mw6GHifgd1J6qsbfyEZht1GMv2Hmy oaw05ih6guGt9zp+JalaW4Rc= X-Received: by 127.0.0.2 with SMTP id AnVLYY1788612xAc4sRnvCo6; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by mx.groups.io with SMTP id smtpd.web10.59756.1699031844803351782 for ; Fri, 03 Nov 2023 10:17:24 -0700 X-Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1cc5b705769so20787975ad.0 for ; Fri, 03 Nov 2023 10:17:24 -0700 (PDT) X-Gm-Message-State: ClFgx7hVVjNfX8i6IcSVG85Hx1787277AA= X-Google-Smtp-Source: AGHT+IEw4dwD1KKTl6D2q9tbfpMJASPrZRYXdeDzElTA+cObGeL4TboLsWAvvOLKbLprMU85aavAEg== X-Received: by 2002:a17:902:f549:b0:1cc:49e7:ee16 with SMTP id h9-20020a170902f54900b001cc49e7ee16mr18325352plf.12.1699031844122; Fri, 03 Nov 2023 10:17:24 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:23 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [PATCH v4 11/14] UefiCpuPkg: Use Attribute From SMM MemoryAttributesTable if Nonzero Date: Fri, 3 Nov 2023 10:17:03 -0700 Message-ID: <20231103171706.148-12-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848908100038 Content-Type: text/plain; charset="utf-8" The function EnforceMemoryMapAttribute() in the SMM MAT logic will ensure that the CODE and DATA memory types have the desired attributes. The consumer of the SMM MAT should only override the Attributes field in the MAT if it is nonzero. This also allows the UEFI and SMM MAT logic to use ImagePropertiesRecordLib instead of carrying two copies of the image properties record manipulation logic. Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann Signed-off-by: Taylor Beebe --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 6f498666157e..d302a9b0cbcf 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -1062,14 +1062,17 @@ SetMemMapAttributes ( MemoryMap =3D MemoryMapStart; for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", M= emoryMap->PhysicalStart, MemoryMap->NumberOfPages)); - if (MemoryMap->Type =3D=3D EfiRuntimeServicesCode) { - MemoryAttribute =3D EFI_MEMORY_RO; - } else { - ASSERT ((MemoryMap->Type =3D=3D EfiRuntimeServicesData) || (MemoryMa= p->Type =3D=3D EfiConventionalMemory)); - // - // Set other type memory as NX. - // - MemoryAttribute =3D EFI_MEMORY_XP; + MemoryAttribute =3D MemoryMap->Attribute & EFI_MEMORY_ACCESS_MASK; + if (MemoryAttribute =3D=3D 0) { + if (MemoryMap->Type =3D=3D EfiRuntimeServicesCode) { + MemoryAttribute =3D EFI_MEMORY_RO; + } else { + ASSERT ((MemoryMap->Type =3D=3D EfiRuntimeServicesData) || (Memory= Map->Type =3D=3D EfiConventionalMemory)); + // + // Set other type memory as NX. + // + MemoryAttribute =3D EFI_MEMORY_XP; + } } =20 // --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110659): https://edk2.groups.io/g/devel/message/110659 Mute This Topic: https://groups.io/mt/102368851/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110660+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110660+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031848; cv=none; d=zohomail.com; s=zohoarc; b=PlmW2PgNEoGwLQQ/zlbISJsMelvp3+eVGiKT7mPJknFHwf16yfEst3zMKf2KiVISfzANQVEaS7tThOmyemxvfCIMFhS/McXStHCuyMxzEeT6h5xv96swBSoc9PtU61ju6JCUhAze8dnJnMrbVYMXB7yfz2qz8LQoQsEtefRWY8k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031848; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=TY4sqhyYo5ke6n9nqf78f11B446c+t/NysYsSBsBmik=; b=F6GKsIMWd2EIcemPPvNLktH4iysDFBOM29wq63cCqbhoTor5exyxni3NOaNXiGisacVAajzPPYNWEqnjtgTeJS+7rDY46cC6RshShbHX/3dZ0UT8c1KkcKJ928wryazXNk8hx+HUkElVoKkVoI+liA2Wnglt4Tl/1P45C+wI8Yg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110660+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 169903184806954.66550575714632; Fri, 3 Nov 2023 10:17:28 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=G6O84h/q+u6rnxo+cqe9WQ8oBJv/+mTWHiwzK/u6zhM=; 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=1699031847; v=1; b=iGptSrtbfYuo3cbGh5gjNFtGwZkpr65hyzE/EDC34QP6eq6T7fGjQSOX5TmM9yspQNoXKcly tIrRS8snxYg9G75ZJkfhmhNrh+/ieQse3rRAWLa9Y+Eo4ghTsw7HnPtT0qqkyvXVSm/o05VcaXC Z/j1LB10rbEltxrCZ7B/XtgM= X-Received: by 127.0.0.2 with SMTP id rGVoYY1788612xjieq1gBpZZ; Fri, 03 Nov 2023 10:17:27 -0700 X-Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by mx.groups.io with SMTP id smtpd.web10.59757.1699031845819355469 for ; Fri, 03 Nov 2023 10:17:25 -0700 X-Received: by mail-pg1-f171.google.com with SMTP id 41be03b00d2f7-5a9bc2ec556so2408824a12.0 for ; Fri, 03 Nov 2023 10:17:25 -0700 (PDT) X-Gm-Message-State: I7ymVKcT5ujKAzzwZKwJPKDSx1787277AA= X-Google-Smtp-Source: AGHT+IHAEc6G+s9NNBBBrZUcASVwdc4G5ZhjcAQ9WN3It4J+1WcsvN8r/40JXPNPqjpGlvXSpmm3bg== X-Received: by 2002:a05:6a20:7289:b0:136:ea0e:d23 with SMTP id o9-20020a056a20728900b00136ea0e0d23mr21265824pzk.11.1699031844932; Fri, 03 Nov 2023 10:17:24 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:24 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi , Jiaxin Wu , Ray Ni Subject: [edk2-devel] [PATCH v4 12/14] MdeModulePkg: Transition SMM MAT Logic to Use ImagePropertiesRecordLib Date: Fri, 3 Nov 2023 10:17:04 -0700 Message-ID: <20231103171706.148-13-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848982100043 Content-Type: text/plain; charset="utf-8" Now that the bugs are fixed in the MAT logic, we can remove the duplicate logic from PiSmmCore/MemoryAttributesTable.c and use ImagePropertiesRecordLib instead. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Jiaxin Wu Cc: Ray Ni Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c |= 785 +------------------- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c |= 29 + MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf |= 1 + MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h |= 11 + 4 files changed, 58 insertions(+), 768 deletions(-) diff --git a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c b/MdeModul= ePkg/Core/PiSmmCore/MemoryAttributesTable.c index 394fdae50741..2e4aaddef4e5 100644 --- a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c @@ -14,6 +14,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include =20 #include #include @@ -25,26 +26,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) =20 -#define IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE SIGNATURE_32 ('I',= 'P','R','C') - -typedef struct { - UINT32 Signature; - LIST_ENTRY Link; - EFI_PHYSICAL_ADDRESS CodeSegmentBase; - UINT64 CodeSegmentSize; -} IMAGE_PROPERTIES_RECORD_CODE_SECTION; - -#define IMAGE_PROPERTIES_RECORD_SIGNATURE SIGNATURE_32 ('I','P','R','D') - -typedef struct { - UINT32 Signature; - LIST_ENTRY Link; - EFI_PHYSICAL_ADDRESS ImageBase; - UINT64 ImageSize; - UINTN CodeSegmentCount; - LIST_ENTRY CodeSegmentList; -} IMAGE_PROPERTIES_RECORD; - #define IMAGE_PROPERTIES_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('I','P','P'= ,'D') =20 typedef struct { @@ -69,87 +50,6 @@ UINT64 mMemoryProtectionAttribute =3D EFI_MEMORY_ATTRIB= UTES_RUNTIME_MEMORY_PROTEC // Below functions are for MemoryMap // =20 -/** - Converts a number of EFI_PAGEs to a size in bytes. - - NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only. - - @param[in] Pages The number of EFI_PAGES. - - @return The number of bytes associated with the number of EFI_PAGEs spe= cified - by Pages. -**/ -STATIC -UINT64 -EfiPagesToSize ( - IN UINT64 Pages - ) -{ - return LShiftU64 (Pages, EFI_PAGE_SHIFT); -} - -/** - Converts a size, in bytes, to a number of EFI_PAGESs. - - NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only. - - @param[in] Size A size in bytes. - - @return The number of EFI_PAGESs associated with the number of bytes sp= ecified - by Size. - -**/ -STATIC -UINT64 -EfiSizeToPages ( - IN UINT64 Size - ) -{ - return RShiftU64 (Size, EFI_PAGE_SHIFT) + ((((UINTN)Size) & EFI_PAGE_MAS= K) ? 1 : 0); -} - -/** - Sort memory map entries based upon PhysicalStart, from low to high. - - @param[in,out] MemoryMap A pointer to the buffer in which firmw= are places - the current memory map. - @param[in] MemoryMapSize Size, in bytes, of the MemoryMap buffe= r. - @param[in] DescriptorSize Size, in bytes, of an individual EFI_M= EMORY_DESCRIPTOR. -**/ -STATIC -VOID -SortMemoryMap ( - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN MemoryMapSize, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR *MemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *NextMemoryMapEntry; - EFI_MEMORY_DESCRIPTOR *MemoryMapEnd; - EFI_MEMORY_DESCRIPTOR TempMemoryMap; - - MemoryMapEntry =3D MemoryMap; - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descripto= rSize); - MemoryMapEnd =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap + Me= moryMapSize); - while (MemoryMapEntry < MemoryMapEnd) { - while (NextMemoryMapEntry < MemoryMapEnd) { - if (MemoryMapEntry->PhysicalStart > NextMemoryMapEntry->PhysicalStar= t) { - CopyMem (&TempMemoryMap, MemoryMapEntry, sizeof (EFI_MEMORY_DESCRI= PTOR)); - CopyMem (MemoryMapEntry, NextMemoryMapEntry, sizeof (EFI_MEMORY_DE= SCRIPTOR)); - CopyMem (NextMemoryMapEntry, &TempMemoryMap, sizeof (EFI_MEMORY_DE= SCRIPTOR)); - } - - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (NextMemoryMapEntry, D= escriptorSize); - } - - MemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); - NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); - } - - return; -} - /** Merge continuous memory map entries whose have same attributes. =20 @@ -183,7 +83,7 @@ MergeMemoryMap ( NextMemoryMapEntry =3D NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, Descrip= torSize); =20 do { - MemoryBlockLength =3D (UINT64)(EfiPagesToSize (MemoryMapEntry->Numbe= rOfPages)); + MemoryBlockLength =3D LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_= PAGE_SHIFT); if (((UINTN)NextMemoryMapEntry < (UINTN)MemoryMapEnd) && (MemoryMapEntry->Type =3D=3D NextMemoryMapEntry->Type) && (MemoryMapEntry->Attribute =3D=3D NextMemoryMapEntry->Attribute)= && @@ -254,423 +154,6 @@ EnforceMemoryMapAttribute ( return; } =20 -/** - Return the first image record, whose [ImageBase, ImageSize] covered by [= Buffer, Length]. - - @param[in] Buffer Start Address - @param[in] Length Address length - - @return first image record covered by [buffer, length] -**/ -STATIC -IMAGE_PROPERTIES_RECORD * -GetImageRecordByAddress ( - IN EFI_PHYSICAL_ADDRESS Buffer, - IN UINT64 Length - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink) - { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - - if ((Buffer <=3D ImageRecord->ImageBase) && - (Buffer + Length >=3D ImageRecord->ImageBase + ImageRecord->ImageS= ize)) - { - return ImageRecord; - } - } - - return NULL; -} - -/** - Set the memory map to new entries, according to one old entry, - based upon PE code section and data section in image record - - @param[in] ImageRecord An image record whose [ImageBase= , ImageSize] covered - by old memory map entry. - @param[in, out] NewRecord A pointer to several new memory = map entries. - The caller guarantee the buffer = size be 1 + - (SplitRecordCount * DescriptorSi= ze) calculated - below. - @param[in] OldRecord A pointer to one old memory map = entry. - @param[in] DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. -**/ -STATIC -UINTN -SetNewRecord ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - UINTN NewRecordCount; - UINT64 PhysicalEnd; - UINT64 ImageEnd; - - CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord= .NumberOfPages); - NewRecordCount =3D 0; - - // - // Always create a new entry for non-PE image record - // - if (ImageRecord->ImageBase > TempRecord.PhysicalStart) { - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecord->ImageBase - = TempRecord.PhysicalStart); - NewRecord->Attribute =3D TempRecord.Attribute; - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, Descri= ptorSize); - NewRecordCount++; - TempRecord.PhysicalStart =3D ImageRecord->ImageBase; - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - TempRecord.= PhysicalStart); - } - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - - if (TempRecord.PhysicalStart <=3D ImageRecordCodeSection->CodeSegmentB= ase) { - // - // DATA - // - NewRecord->Type =3D EfiRuntimeServicesData; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentBase - NewRecord->PhysicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount++; - } - - // - // CODE - // - NewRecord->Type =3D EfiRuntimeServicesCode; - NewRecord->PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageRecordCodeSection-= >CodeSegmentSize); - NewRecord->Attribute =3D (TempRecord.Attribute & (~EFI_MEMORY_XP= )) | EFI_MEMORY_RO; - if (NewRecord->NumberOfPages !=3D 0) { - NewRecord =3D NEXT_MEMORY_DESCRIPTOR (NewRecord, DescriptorSize); - NewRecordCount++; - } - - TempRecord.PhysicalStart =3D ImageRecordCodeSection->CodeSegmentBase= + EfiPagesToSize (EfiSizeToPages (ImageRecordCodeSection->CodeSegmentSize)= ); - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - TempRecor= d.PhysicalStart); - if (TempRecord.NumberOfPages =3D=3D 0) { - break; - } - } - } - - ImageEnd =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - - // - // Final DATA - // - if (TempRecord.PhysicalStart < ImageEnd) { - NewRecord->Type =3D EfiRuntimeServicesData; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D EfiSizeToPages (ImageEnd - TempRecord.Phy= sicalStart); - NewRecord->Attribute =3D TempRecord.Attribute | EFI_MEMORY_XP; - NewRecordCount++; - } - - return NewRecordCount; -} - -/** - Return the max number of new splitted entries, according to one old entr= y, - based upon PE code section and data section. - - @param[in] OldRecord A pointer to one old memory map entry. - - @retval 0 no entry need to be splitted. - @return the max number of new splitted entries -**/ -STATIC -UINTN -GetMaxSplitRecordCount ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - UINTN SplitRecordCount; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - - SplitRecordCount =3D 0; - PhysicalStart =3D OldRecord->PhysicalStart; - PhysicalEnd =3D OldRecord->PhysicalStart + EfiPagesToSize (OldRecor= d->NumberOfPages); - - do { - ImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd - = PhysicalStart); - if (ImageRecord =3D=3D NULL) { - break; - } - - SplitRecordCount +=3D (2 * ImageRecord->CodeSegmentCount + 2); - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->ImageSize; - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - return SplitRecordCount; -} - -/** - Split the memory map to new entries, according to one old entry, - based upon PE code section and data section. - - @param[in] OldRecord A pointer to one old memory map = entry. - @param[in, out] NewRecord A pointer to several new memory = map entries. - The caller guarantee the buffer = size be 1 + - (SplitRecordCount * DescriptorSi= ze) calculated - below. - @param[in] MaxSplitRecordCount The max number of splitted entri= es - @param[in] DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. - - @retval 0 no entry is splitted. - @return the real number of splitted record. -**/ -STATIC -UINTN -SplitRecord ( - IN EFI_MEMORY_DESCRIPTOR *OldRecord, - IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, - IN UINTN MaxSplitRecordCount, - IN UINTN DescriptorSize - ) -{ - EFI_MEMORY_DESCRIPTOR TempRecord; - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NewImageRecord; - UINT64 PhysicalStart; - UINT64 PhysicalEnd; - UINTN NewRecordCount; - UINTN TotalNewRecordCount; - - if (MaxSplitRecordCount =3D=3D 0) { - CopyMem (NewRecord, OldRecord, DescriptorSize); - return 0; - } - - TotalNewRecordCount =3D 0; - - // - // Override previous record - // - CopyMem (&TempRecord, OldRecord, sizeof (EFI_MEMORY_DESCRIPTOR)); - PhysicalStart =3D TempRecord.PhysicalStart; - PhysicalEnd =3D TempRecord.PhysicalStart + EfiPagesToSize (TempRecord.= NumberOfPages); - - ImageRecord =3D NULL; - do { - NewImageRecord =3D GetImageRecordByAddress (PhysicalStart, PhysicalEnd= - PhysicalStart); - if (NewImageRecord =3D=3D NULL) { - // - // No more image covered by this range, stop - // - if (PhysicalEnd > PhysicalStart) { - // - // Always create a new entry for non-PE image record - // - NewRecord->Type =3D TempRecord.Type; - NewRecord->PhysicalStart =3D TempRecord.PhysicalStart; - NewRecord->VirtualStart =3D 0; - NewRecord->NumberOfPages =3D TempRecord.NumberOfPages; - NewRecord->Attribute =3D TempRecord.Attribute; - TotalNewRecordCount++; - } - - break; - } - - ImageRecord =3D NewImageRecord; - - // - // Set new record - // - NewRecordCount =3D SetNewRecord (ImageRecord, NewRecord, &TempRe= cord, DescriptorSize); - TotalNewRecordCount +=3D NewRecordCount; - NewRecord =3D (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)NewRecord = + NewRecordCount * DescriptorSize); - - // - // Update PhysicalStart, in order to exclude the image buffer already = splitted. - // - PhysicalStart =3D ImageRecord->ImageBase + ImageRecord->Ima= geSize; - TempRecord.PhysicalStart =3D PhysicalStart; - TempRecord.NumberOfPages =3D EfiSizeToPages (PhysicalEnd - PhysicalSta= rt); - } while ((ImageRecord !=3D NULL) && (PhysicalStart < PhysicalEnd)); - - return TotalNewRecordCount - 1; -} - -/** - Split the original memory map, and add more entries to describe PE code = section and data section. - This function will set EfiRuntimeServicesData to be EFI_MEMORY_XP. - This function will merge entries with same attributes finally. - - NOTE: It assumes PE code/data section are page aligned. - NOTE: It assumes enough entry is prepared for new memory map. - - Split table: - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - =3D=3D> - +---------------+ - | Record X | - +---------------+ - | Record RtCode | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF1 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtCode | - +---------------+ ---- - | Record RtData | | - +---------------+ | - | Record RtCode | |-> PE/COFF2 - +---------------+ | - | Record RtData | | - +---------------+ ---- - | Record RtCode | - +---------------+ - | Record Y | - +---------------+ - - @param[in, out] MemoryMapSize A pointer to the size, in bytes,= of the - MemoryMap buffer. On input, this= is the size of - old MemoryMap before split. The = actual buffer - size of MemoryMap is MemoryMapSi= ze + - (AdditionalRecordCount * Descrip= torSize) calculated - below. On output, it is the size= of new MemoryMap - after split. - @param[in, out] MemoryMap A pointer to the buffer in which= firmware places - the current memory map. - @param[in] DescriptorSize Size, in bytes, of an individual= EFI_MEMORY_DESCRIPTOR. -**/ -STATIC -VOID -SplitTable ( - IN OUT UINTN *MemoryMapSize, - IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, - IN UINTN DescriptorSize - ) -{ - INTN IndexOld; - INTN IndexNew; - UINTN MaxSplitRecordCount; - UINTN RealSplitRecordCount; - UINTN TotalSplitRecordCount; - UINTN AdditionalRecordCount; - - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 2) * mImagePropertiesPrivateData.ImageRecordCount; - - TotalSplitRecordCount =3D 0; - // - // Let old record point to end of valid MemoryMap buffer. - // - IndexOld =3D ((*MemoryMapSize) / DescriptorSize) - 1; - // - // Let new record point to end of full MemoryMap buffer. - // - IndexNew =3D ((*MemoryMapSize) / DescriptorSize) - 1 + AdditionalRecordC= ount; - for ( ; IndexOld >=3D 0; IndexOld--) { - MaxSplitRecordCount =3D GetMaxSplitRecordCount ((EFI_MEMORY_DESCRIPTOR= *)((UINT8 *)MemoryMap + IndexOld * DescriptorSize)); - // - // Split this MemoryMap record - // - IndexNew -=3D MaxSplitRecordCount; - RealSplitRecordCount =3D SplitRecord ( - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexOld * DescriptorSize), - (EFI_MEMORY_DESCRIPTOR *)((UINT8 *)MemoryMap = + IndexNew * DescriptorSize), - MaxSplitRecordCount, - DescriptorSize - ); - // - // Adjust IndexNew according to real split. - // - if (MaxSplitRecordCount !=3D RealSplitRecordCount) { - CopyMem ( - ((UINT8 *)MemoryMap + (IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount) * DescriptorSize), - ((UINT8 *)MemoryMap + IndexNew * DescriptorSize), - (RealSplitRecordCount + 1) * DescriptorSize - ); - } - - IndexNew =3D IndexNew + MaxSplitRecordCount - RealSplitR= ecordCount; - TotalSplitRecordCount +=3D RealSplitRecordCount; - IndexNew--; - } - - // - // Move all records to the beginning. - // - CopyMem ( - MemoryMap, - (UINT8 *)MemoryMap + (AdditionalRecordCount - TotalSplitRecordCount) *= DescriptorSize, - (*MemoryMapSize) + TotalSplitRecordCount * DescriptorSize - ); - - *MemoryMapSize =3D (*MemoryMapSize) + DescriptorSize * TotalSplitRecordC= ount; - - // - // Sort from low to high (Just in case) - // - SortMemoryMap (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Set RuntimeData to XP - // - EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, DescriptorSize); - - // - // Merge same type to save entry size - // - MergeMemoryMap (MemoryMap, MemoryMapSize, DescriptorSize); - - return; -} - /** This function for GetMemoryMap() with memory attributes table. =20 @@ -729,7 +212,7 @@ SmmCoreGetMemoryMapMemoryAttributesTable ( return EFI_INVALID_PARAMETER; } =20 - AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 2) * mImagePropertiesPrivateData.ImageRecordCount; + AdditionalRecordCount =3D (2 * mImagePropertiesPrivateData.CodeSegmentCo= untMax + 3) * mImagePropertiesPrivateData.ImageRecordCount; =20 OldMemoryMapSize =3D *MemoryMapSize; Status =3D SmmCoreGetMemoryMap (MemoryMapSize, MemoryMap, MapK= ey, DescriptorSize, DescriptorVersion); @@ -747,7 +230,17 @@ SmmCoreGetMemoryMapMemoryAttributesTable ( // Split PE code/data // ASSERT (MemoryMap !=3D NULL); - SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize); + SplitTable (MemoryMapSize, MemoryMap, *DescriptorSize, &mImageProper= tiesPrivateData.ImageRecordList, AdditionalRecordCount); + + // + // Set RuntimeData to XP + // + EnforceMemoryMapAttribute (MemoryMap, *MemoryMapSize, *DescriptorSiz= e); + + // + // Merge same type to save entry size + // + MergeMemoryMap (MemoryMap, MemoryMapSize, *DescriptorSize); } } =20 @@ -777,250 +270,6 @@ SetMemoryAttributesTableSectionAlignment ( } } =20 -/** - Swap two code sections in image record. - - @param[in] FirstImageRecordCodeSection first code section in image r= ecord - @param[in] SecondImageRecordCodeSection second code section in image = record -**/ -STATIC -VOID -SwapImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, - IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION TempImageRecordCodeSection; - - TempImageRecordCodeSection.CodeSegmentBase =3D FirstImageRecordCodeSecti= on->CodeSegmentBase; - TempImageRecordCodeSection.CodeSegmentSize =3D FirstImageRecordCodeSecti= on->CodeSegmentSize; - - FirstImageRecordCodeSection->CodeSegmentBase =3D SecondImageRecordCodeSe= ction->CodeSegmentBase; - FirstImageRecordCodeSection->CodeSegmentSize =3D SecondImageRecordCodeSe= ction->CodeSegmentSize; - - SecondImageRecordCodeSection->CodeSegmentBase =3D TempImageRecordCodeSec= tion.CodeSegmentBase; - SecondImageRecordCodeSection->CodeSegmentSize =3D TempImageRecordCodeSec= tion.CodeSegmentSize; -} - -/** - Sort code section in image record, based upon CodeSegmentBase from low t= o high. - - @param[in] ImageRecord image record to be sorted -**/ -STATIC -VOID -SortImageRecordCodeSection ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *NextImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *NextImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLi= nk; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLi= nk; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - while (NextImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLi= nk) { - NextImageRecordCodeSection =3D CR ( - NextImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_= SIGNATURE - ); - if (ImageRecordCodeSection->CodeSegmentBase > NextImageRecordCodeSec= tion->CodeSegmentBase) { - SwapImageRecordCodeSection (ImageRecordCodeSection, NextImageRecor= dCodeSection); - } - - NextImageRecordCodeSectionLink =3D NextImageRecordCodeSectionLink->F= orwardLink; - } - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; - NextImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->Forward= Link; - } -} - -/** - Check if code section in image record is valid. - - @param[in] ImageRecord image record to be checked - - @retval TRUE image record is valid - @retval FALSE image record is invalid -**/ -STATIC -BOOLEAN -IsImageRecordCodeSectionValid ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *LastImageRecordCodeSection; - LIST_ENTRY *ImageRecordCodeSectionLink; - LIST_ENTRY *ImageRecordCodeSectionEndLink; - LIST_ENTRY *ImageRecordCodeSectionList; - - DEBUG ((DEBUG_VERBOSE, "SMM ImageCode SegmentCount - 0x%x\n", ImageRecor= d->CodeSegmentCount)); - - ImageRecordCodeSectionList =3D &ImageRecord->CodeSegmentList; - - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionList->ForwardLin= k; - ImageRecordCodeSectionEndLink =3D ImageRecordCodeSectionList; - LastImageRecordCodeSection =3D NULL; - while (ImageRecordCodeSectionLink !=3D ImageRecordCodeSectionEndLink) { - ImageRecordCodeSection =3D CR ( - ImageRecordCodeSectionLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - if (ImageRecordCodeSection->CodeSegmentSize =3D=3D 0) { - return FALSE; - } - - if (ImageRecordCodeSection->CodeSegmentBase < ImageRecord->ImageBase) { - return FALSE; - } - - if (ImageRecordCodeSection->CodeSegmentBase >=3D MAX_ADDRESS - ImageRe= cordCodeSection->CodeSegmentSize) { - return FALSE; - } - - if ((ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection-= >CodeSegmentSize) > (ImageRecord->ImageBase + ImageRecord->ImageSize)) { - return FALSE; - } - - if (LastImageRecordCodeSection !=3D NULL) { - if ((LastImageRecordCodeSection->CodeSegmentBase + LastImageRecordCo= deSection->CodeSegmentSize) > ImageRecordCodeSection->CodeSegmentBase) { - return FALSE; - } - } - - LastImageRecordCodeSection =3D ImageRecordCodeSection; - ImageRecordCodeSectionLink =3D ImageRecordCodeSectionLink->ForwardLink; - } - - return TRUE; -} - -/** - Swap two image records. - - @param[in] FirstImageRecord first image record. - @param[in] SecondImageRecord second image record. -**/ -STATIC -VOID -SwapImageRecord ( - IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, - IN IMAGE_PROPERTIES_RECORD *SecondImageRecord - ) -{ - IMAGE_PROPERTIES_RECORD TempImageRecord; - - TempImageRecord.ImageBase =3D FirstImageRecord->ImageBase; - TempImageRecord.ImageSize =3D FirstImageRecord->ImageSize; - TempImageRecord.CodeSegmentCount =3D FirstImageRecord->CodeSegmentCount; - - FirstImageRecord->ImageBase =3D SecondImageRecord->ImageBase; - FirstImageRecord->ImageSize =3D SecondImageRecord->ImageSize; - FirstImageRecord->CodeSegmentCount =3D SecondImageRecord->CodeSegmentCou= nt; - - SecondImageRecord->ImageBase =3D TempImageRecord.ImageBase; - SecondImageRecord->ImageSize =3D TempImageRecord.ImageSize; - SecondImageRecord->CodeSegmentCount =3D TempImageRecord.CodeSegmentCount; - - SwapListEntries (&FirstImageRecord->CodeSegmentList, &SecondImageRecord-= >CodeSegmentList); -} - -/** - Sort image record based upon the ImageBase from low to high. -**/ -STATIC -VOID -SortImageRecord ( - VOID - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - IMAGE_PROPERTIES_RECORD *NextImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *NextImageRecordLink; - LIST_ENTRY *ImageRecordEndLink; - LIST_ENTRY *ImageRecordList; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - ImageRecordLink =3D ImageRecordList->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - ImageRecordEndLink =3D ImageRecordList; - while (ImageRecordLink !=3D ImageRecordEndLink) { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - while (NextImageRecordLink !=3D ImageRecordEndLink) { - NextImageRecord =3D CR ( - NextImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - if (ImageRecord->ImageBase > NextImageRecord->ImageBase) { - SwapImageRecord (ImageRecord, NextImageRecord); - } - - NextImageRecordLink =3D NextImageRecordLink->ForwardLink; - } - - ImageRecordLink =3D ImageRecordLink->ForwardLink; - NextImageRecordLink =3D ImageRecordLink->ForwardLink; - } -} - -/** - Dump image record. -**/ -STATIC -VOID -DumpImageRecord ( - VOID - ) -{ - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - LIST_ENTRY *ImageRecordList; - UINTN Index; - - ImageRecordList =3D &mImagePropertiesPrivateData.ImageRecordList; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink, Index =3D 0; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink, Index++) - { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - DEBUG ((DEBUG_VERBOSE, "SMM Image[%d]: 0x%016lx - 0x%016lx\n", Index,= ImageRecord->ImageBase, ImageRecord->ImageSize)); - } -} - /** Insert image record. =20 @@ -1059,7 +308,7 @@ SmmInsertImageRecord ( // Step 1: record whole region // ImageRecord->ImageBase =3D DriverEntry->ImageBuffer; - ImageRecord->ImageSize =3D EfiPagesToSize (DriverEntry->NumberOfPage); + ImageRecord->ImageSize =3D LShiftU64 (DriverEntry->NumberOfPage, EFI_PAG= E_SHIFT); =20 ImageAddress =3D (VOID *)(UINTN)DriverEntry->ImageBuffer; =20 @@ -1193,7 +442,7 @@ SmmInsertImageRecord ( mImagePropertiesPrivateData.CodeSegmentCountMax =3D ImageRecord->CodeS= egmentCount; } =20 - SortImageRecord (); + SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: return; @@ -1361,7 +610,7 @@ SmmInstallMemoryAttributesTable ( =20 DEBUG ((DEBUG_VERBOSE, "SMM Total Image Count - 0x%x\n", mImagePropertie= sPrivateData.ImageRecordCount)); DEBUG ((DEBUG_VERBOSE, "SMM Dump ImageRecord:\n")); - DumpImageRecord (); + DumpImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 PublishMemoryAttributesTable (); =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index c9378679e7bb..9b296aa45762 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -783,6 +783,35 @@ SortImageRecord ( return EFI_SUCCESS; } =20 +/** + Dump image record. + + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries +**/ +VOID +EFIAPI +DumpImageRecord ( + IN LIST_ENTRY *ImageRecordList + ) +{ + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + UINTN Index; + + for (ImageRecordLink =3D ImageRecordList->ForwardLink, Index =3D 0; + ImageRecordLink !=3D ImageRecordList; + ImageRecordLink =3D ImageRecordLink->ForwardLink, Index++) + { + ImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + DEBUG ((DEBUG_VERBOSE, "Image[%d]: 0x%016lx - 0x%016lx\n", Index, Imag= eRecord->ImageBase, ImageRecord->ImageSize)); + } +} + /** Find image record according to image base and size. =20 diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf b/MdeModulePkg/Core/= PiSmmCore/PiSmmCore.inf index 3df44b38f13c..75a5934f0c1d 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmCore.inf @@ -61,6 +61,7 @@ [LibraryClasses] HobLib SmmMemLib SafeIntLib + ImagePropertiesRecordLib =20 [Protocols] gEfiDxeSmmReadyToLockProtocolGuid ## UNDEFINED # SmiHandlerR= egister diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index b6365662646a..e3f569ab03d1 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -181,4 +181,15 @@ FindImageRecord ( IN LIST_ENTRY *ImageRecordList ); =20 +/** + Dump image record. + + @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries +**/ +VOID +EFIAPI +DumpImageRecord ( + IN LIST_ENTRY *ImageRecordList + ); + #endif --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110660): https://edk2.groups.io/g/devel/message/110660 Mute This Topic: https://groups.io/mt/102368853/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110661+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110661+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031848; cv=none; d=zohomail.com; s=zohoarc; b=XBWDvKvsOkg4Fo2/XAr1TcSof+eSOwvOOUW7DKl+81GpLXTYDS1Rybh6GyjzvuW0LL5Bl0bCn+hiaVRkNzKqkEzmzEYRieHuroN9SeGbWmZonSaAkHzNt8sAntvnsnjTUCoaoKvtB6K9pez6nJxPzk4s8osVwj9mgmW/KMg8Ykg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031848; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=NuNVJkuycq4Us51NdnMJ9c4svL6Rwv1neBKjcOYEesQ=; b=oBfRwwU5I+YpycXrLtEJ8hnqTgI7i/sjQ5K0lw8ilhd8sTeNO8A6zniCGhaGlLRO4g6z5wrb0FSTjGiw7DrGFX9xVb/7lktNw8Xu5fKILGz8+gqEifGg84274p8O/K5aCd8CKNSNScHPYCkD2aQ1D9ik2zYwWIK8kqE77QNP2S8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110661+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031848543561.2777530671618; Fri, 3 Nov 2023 10:17:28 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=tzIURsTHnbYyE97BiA0kO187gX/Y23N/S+76lgPW0N4=; 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=1699031848; v=1; b=dmUq/V4McASRky2DYxcxwryF8ta9tqcGuwjStiFNq4/InbKDne9F35Z+e8ssGIptxLxmCeEg IJ39rDMjNgfsoEct3Wdj7b5m7JQwWmzKAl0+3VrUF5nQslPqS9ltxMvWSqARF7F211SLKGrRDSC LGVjQllMM5ZOiUPXlIZx+Et4= X-Received: by 127.0.0.2 with SMTP id Kec4YY1788612xqZAphZy00F; Fri, 03 Nov 2023 10:17:28 -0700 X-Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) by mx.groups.io with SMTP id smtpd.web10.59758.1699031846705387868 for ; Fri, 03 Nov 2023 10:17:26 -0700 X-Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1cc5b7057d5so20875815ad.2 for ; Fri, 03 Nov 2023 10:17:26 -0700 (PDT) X-Gm-Message-State: ZtM2LqxlBbn1CBfKVbK0ZiMyx1787277AA= X-Google-Smtp-Source: AGHT+IGdj5heZqjqkQv0/SctsXAceVxlIDQeYyGh89cnOJg1JwxryRKHhcCBvByPgXO3y9auP0lcbQ== X-Received: by 2002:a17:903:1ca:b0:1cc:3bd3:73d8 with SMTP id e10-20020a17090301ca00b001cc3bd373d8mr20675434plh.59.1699031845937; Fri, 03 Nov 2023 10:17:25 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:25 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi , Jiaxin Wu , Ray Ni Subject: [edk2-devel] [PATCH v4 13/14] MdeModulePkg: Add Logic to Create/Delete Image Properties Records Date: Fri, 3 Nov 2023 10:17:05 -0700 Message-ID: <20231103171706.148-14-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031848974100041 Content-Type: text/plain; charset="utf-8" Add logic to create and delete image properties records. Where applicable, redirect existing code to use the new library. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Jiaxin Wu Cc: Ray Ni Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c = | 184 +++---------------- MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c = | 166 +++-------------- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c = | 186 ++++++++++++++++++++ MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h = | 39 ++++ MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf= | 1 + 5 files changed, 281 insertions(+), 295 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index af6c26244cc0..993db281062a 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -557,25 +557,6 @@ CoreGetMemoryMapWithSeparatedImageSection ( // Below functions are for ImageRecord // =20 -/** - Set MemoryAttributesTable according to PE/COFF image section alignment. - - @param SectionAlignment PE/COFF section alignment -**/ -STATIC -VOID -SetMemoryAttributesTableSectionAlignment ( - IN UINT32 SectionAlignment - ) -{ - if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - mMemoryAttributesTableEnable) - { - DEBUG ((DEBUG_VERBOSE, "SetMemoryAttributesTableSectionAlignment - Cle= ar\n")); - mMemoryAttributesTableEnable =3D FALSE; - } -} - /** Insert image record. =20 @@ -586,20 +567,12 @@ InsertImageRecord ( IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage ) { - VOID *ImageAddress; - EFI_IMAGE_DOS_HEADER *DosHdr; - UINT32 PeCoffHeaderOffset; - UINT32 SectionAlignment; - EFI_IMAGE_SECTION_HEADER *Section; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; - UINT8 *Name; - UINTN Index; - IMAGE_PROPERTIES_RECORD *ImageRecord; - CHAR8 *PdbPointer; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + EFI_STATUS Status; + IMAGE_PROPERTIES_RECORD *ImageRecord; + CHAR8 *PdbPointer; + UINT32 RequiredAlignment; =20 DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%x\n", RuntimeImage)); - DEBUG ((DEBUG_VERBOSE, "InsertImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); =20 if (mMemoryAttributesTableEndOfDxe) { DEBUG ((DEBUG_INFO, "Do not insert runtime image record after EndOfDxe= \n")); @@ -611,139 +584,48 @@ InsertImageRecord ( return; } =20 - ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); =20 - DEBUG ((DEBUG_VERBOSE, "ImageRecordCount - 0x%x\n", mImagePropertiesPriv= ateData.ImageRecordCount)); - - // - // Step 1: record whole region - // - ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->Im= ageBase; - ImageRecord->ImageSize =3D RuntimeImage->ImageSize; - - ImageAddress =3D RuntimeImage->ImageBase; - - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)RuntimeImage->I= mageBase); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_VERBOSE, " Image - %a\n", PdbPointer)); } =20 - // - // Check PE/COFF image - // - DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageAddress; - PeCoffHeaderOffset =3D 0; - if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { - PeCoffHeaderOffset =3D DosHdr->e_lfanew; - } - - Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageAddress + P= eCoffHeaderOffset); - if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { - DEBUG ((DEBUG_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); - // It might be image in SMM. - goto Finish; - } - - // - // Get SectionAlignment - // - if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { - SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; - } else { - SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; - } - - SetMemoryAttributesTableSectionAlignment (SectionAlignment); - if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { - DEBUG (( - DEBUG_WARN, - "!!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %dK != !!!!!!!\n", - SectionAlignment, - RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10 - )); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); - if (PdbPointer !=3D NULL) { - DEBUG ((DEBUG_WARN, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)); + RequiredAlignment =3D RUNTIME_PAGE_ALLOCATION_GRANULARITY; + Status =3D CreateImagePropertiesRecord ( + RuntimeImage->ImageBase, + RuntimeImage->ImageSize, + &RequiredAlignment, + ImageRecord + ); + + if (EFI_ERROR (Status)) { + if (Status =3D=3D EFI_ABORTED) { + mMemoryAttributesTableEnable =3D FALSE; } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - Section =3D (EFI_IMAGE_SECTION_HEADER *)( - (UINT8 *)(UINTN)ImageAddress + - PeCoffHeaderOffset + - sizeof (UINT32) + - sizeof (EFI_IMAGE_FILE_HEADER) + - Hdr.Pe32->FileHeader.SizeOfOption= alHeader - ); - ImageRecord->CodeSegmentCount =3D 0; - InitializeListHead (&ImageRecord->CodeSegmentList); - for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { - Name =3D Section[Index].Name; - DEBUG (( - DEBUG_VERBOSE, - " Section - '%c%c%c%c%c%c%c%c'\n", - Name[0], - Name[1], - Name[2], - Name[3], - Name[4], - Name[5], - Name[6], - Name[7] - )); - - if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { - DEBUG ((DEBUG_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); - DEBUG ((DEBUG_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); - DEBUG ((DEBUG_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); - DEBUG ((DEBUG_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); - DEBUG ((DEBUG_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); - DEBUG ((DEBUG_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); - DEBUG ((DEBUG_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); - DEBUG ((DEBUG_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); - DEBUG ((DEBUG_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); - - // - // Step 2: record code section - // - ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); - if (ImageRecordCodeSection =3D=3D NULL) { - return; - } - - ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; - - ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; - - DEBUG ((DEBUG_VERBOSE, "ImageCode: 0x%016lx - 0x%016lx\n", ImageReco= rdCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize)); - - InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); - ImageRecord->CodeSegmentCount++; - } - } - if (ImageRecord->CodeSegmentCount =3D=3D 0) { - SetMemoryAttributesTableSectionAlignment (1); + mMemoryAttributesTableEnable =3D FALSE; DEBUG ((DEBUG_ERROR, "!!!!!!!! InsertImageRecord - CodeSegmentCount i= s 0 !!!!!!!!\n")); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_ERROR, "!!!!!!!! Image - %a !!!!!!!!\n", PdbPointer)= ); } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - // - // Final - // - SortImageRecordCodeSection (ImageRecord); // // Check overlap all section in ImageBase/Size // if (!IsImageRecordCodeSectionValid (ImageRecord)) { DEBUG ((DEBUG_ERROR, "IsImageRecordCodeSectionValid - FAIL\n")); + Status =3D EFI_ABORTED; goto Finish; } =20 @@ -757,6 +639,10 @@ InsertImageRecord ( SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: + if (EFI_ERROR (Status) && (ImageRecord !=3D NULL)) { + DeleteImagePropertiesRecord (ImageRecord); + } + return; } =20 @@ -770,9 +656,7 @@ RemoveImageRecord ( IN EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage ) { - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *CodeSegmentListHead; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + IMAGE_PROPERTIES_RECORD *ImageRecord; =20 DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%x\n", RuntimeImage)); DEBUG ((DEBUG_VERBOSE, "RemoveImageRecord - 0x%016lx - 0x%016lx\n", (EFI= _PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase, RuntimeImage->ImageSize)); @@ -788,19 +672,7 @@ RemoveImageRecord ( return; } =20 - CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; - while (!IsListEmpty (CodeSegmentListHead)) { - ImageRecordCodeSection =3D CR ( - CodeSegmentListHead->ForwardLink, - IMAGE_PROPERTIES_RECORD_CODE_SECTION, - Link, - IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE - ); - RemoveEntryList (&ImageRecordCodeSection->Link); - FreePool (ImageRecordCodeSection); - } + DeleteImagePropertiesRecord (ImageRecord); =20 - RemoveEntryList (&ImageRecord->Link); - FreePool (ImageRecord); mImagePropertiesPrivateData.ImageRecordCount--; } diff --git a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c b/MdeModul= ePkg/Core/PiSmmCore/MemoryAttributesTable.c index 2e4aaddef4e5..03de9b2c5fff 100644 --- a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c @@ -251,25 +251,6 @@ SmmCoreGetMemoryMapMemoryAttributesTable ( // Below functions are for ImageRecord // =20 -/** - Set MemoryProtectionAttribute according to PE/COFF image section alignme= nt. - - @param[in] SectionAlignment PE/COFF section alignment -**/ -STATIC -VOID -SetMemoryAttributesTableSectionAlignment ( - IN UINT32 SectionAlignment - ) -{ - if (((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D= 0) && - ((mMemoryProtectionAttribute & EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_= PROTECTION_NON_EXECUTABLE_PE_DATA) !=3D 0)) - { - DEBUG ((DEBUG_VERBOSE, "SMM SetMemoryAttributesTableSectionAlignment -= Clear\n")); - mMemoryProtectionAttribute &=3D ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIM= E_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA); - } -} - /** Insert image record. =20 @@ -280,158 +261,61 @@ SmmInsertImageRecord ( IN EFI_SMM_DRIVER_ENTRY *DriverEntry ) { - VOID *ImageAddress; - EFI_IMAGE_DOS_HEADER *DosHdr; - UINT32 PeCoffHeaderOffset; - UINT32 SectionAlignment; - EFI_IMAGE_SECTION_HEADER *Section; - EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; - UINT8 *Name; - UINTN Index; - IMAGE_PROPERTIES_RECORD *ImageRecord; - CHAR8 *PdbPointer; - IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + EFI_STATUS Status; + IMAGE_PROPERTIES_RECORD *ImageRecord; + CHAR8 *PdbPointer; + UINT32 RequiredAlignment; =20 DEBUG ((DEBUG_VERBOSE, "SMM InsertImageRecord - 0x%x\n", DriverEntry)); - DEBUG ((DEBUG_VERBOSE, "SMM InsertImageRecord - 0x%016lx - 0x%08x\n", Dr= iverEntry->ImageBuffer, DriverEntry->NumberOfPage)); =20 ImageRecord =3D AllocatePool (sizeof (*ImageRecord)); if (ImageRecord =3D=3D NULL) { return; } =20 - ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); =20 - DEBUG ((DEBUG_VERBOSE, "SMM ImageRecordCount - 0x%x\n", mImageProperties= PrivateData.ImageRecordCount)); - - // - // Step 1: record whole region - // - ImageRecord->ImageBase =3D DriverEntry->ImageBuffer; - ImageRecord->ImageSize =3D LShiftU64 (DriverEntry->NumberOfPage, EFI_PAG= E_SHIFT); - - ImageAddress =3D (VOID *)(UINTN)DriverEntry->ImageBuffer; - - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)DriverEntry->Im= ageBuffer); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_VERBOSE, "SMM Image - %a\n", PdbPointer)); } =20 - // - // Check PE/COFF image - // - DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageAddress; - PeCoffHeaderOffset =3D 0; - if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { - PeCoffHeaderOffset =3D DosHdr->e_lfanew; - } - - Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageAddress + P= eCoffHeaderOffset); - if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { - DEBUG ((DEBUG_VERBOSE, "SMM Hdr.Pe32->Signature invalid - 0x%x\n", Hdr= .Pe32->Signature)); - goto Finish; - } - - // - // Get SectionAlignment - // - if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { - SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; - } else { - SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; - } - - SetMemoryAttributesTableSectionAlignment (SectionAlignment); - if ((SectionAlignment & (RUNTIME_PAGE_ALLOCATION_GRANULARITY - 1)) !=3D = 0) { - DEBUG (( - DEBUG_WARN, - "SMM !!!!!!!! InsertImageRecord - Section Alignment(0x%x) is not %d= K !!!!!!!!\n", - SectionAlignment, - RUNTIME_PAGE_ALLOCATION_GRANULARITY >> 10 - )); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); - if (PdbPointer !=3D NULL) { - DEBUG ((DEBUG_WARN, "SMM !!!!!!!! Image - %a !!!!!!!!\n", PdbPoint= er)); + RequiredAlignment =3D RUNTIME_PAGE_ALLOCATION_GRANULARITY; + Status =3D CreateImagePropertiesRecord ( + (VOID *)(UINTN)DriverEntry->ImageBuffer, + LShiftU64 (DriverEntry->NumberOfPage, EFI_PAGE_SHI= FT), + &RequiredAlignment, + ImageRecord + ); + + if (EFI_ERROR (Status)) { + if (Status =3D=3D EFI_ABORTED) { + mMemoryProtectionAttribute &=3D + ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_PROTECTION_NON_EXEC= UTABLE_PE_DATA); } =20 goto Finish; } =20 - Section =3D (EFI_IMAGE_SECTION_HEADER *)( - (UINT8 *)(UINTN)ImageAddress + - PeCoffHeaderOffset + - sizeof (UINT32) + - sizeof (EFI_IMAGE_FILE_HEADER) + - Hdr.Pe32->FileHeader.SizeOfOption= alHeader - ); - ImageRecord->CodeSegmentCount =3D 0; - InitializeListHead (&ImageRecord->CodeSegmentList); - for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { - Name =3D Section[Index].Name; - DEBUG (( - DEBUG_VERBOSE, - "SMM Section - '%c%c%c%c%c%c%c%c'\n", - Name[0], - Name[1], - Name[2], - Name[3], - Name[4], - Name[5], - Name[6], - Name[7] - )); - - if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { - DEBUG ((DEBUG_VERBOSE, "SMM VirtualSize - 0x%08x\n", Sect= ion[Index].Misc.VirtualSize)); - DEBUG ((DEBUG_VERBOSE, "SMM VirtualAddress - 0x%08x\n", Sect= ion[Index].VirtualAddress)); - DEBUG ((DEBUG_VERBOSE, "SMM SizeOfRawData - 0x%08x\n", Sect= ion[Index].SizeOfRawData)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToRawData - 0x%08x\n", Sect= ion[Index].PointerToRawData)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToRelocations - 0x%08x\n", Sect= ion[Index].PointerToRelocations)); - DEBUG ((DEBUG_VERBOSE, "SMM PointerToLinenumbers - 0x%08x\n", Sect= ion[Index].PointerToLinenumbers)); - DEBUG ((DEBUG_VERBOSE, "SMM NumberOfRelocations - 0x%08x\n", Sect= ion[Index].NumberOfRelocations)); - DEBUG ((DEBUG_VERBOSE, "SMM NumberOfLinenumbers - 0x%08x\n", Sect= ion[Index].NumberOfLinenumbers)); - DEBUG ((DEBUG_VERBOSE, "SMM Characteristics - 0x%08x\n", Sect= ion[Index].Characteristics)); - - // - // Step 2: record code section - // - ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); - if (ImageRecordCodeSection =3D=3D NULL) { - return; - } - - ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; - - ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageAddress + Se= ction[Index].VirtualAddress; - ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; - - DEBUG ((DEBUG_VERBOSE, "SMM ImageCode: 0x%016lx - 0x%016lx\n", Image= RecordCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize= )); - - InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); - ImageRecord->CodeSegmentCount++; - } - } - if (ImageRecord->CodeSegmentCount =3D=3D 0) { - SetMemoryAttributesTableSectionAlignment (1); + mMemoryProtectionAttribute &=3D + ~((UINT64)EFI_MEMORY_ATTRIBUTES_RUNTIME_MEMORY_PROTECTION_NON_EXECUT= ABLE_PE_DATA); DEBUG ((DEBUG_ERROR, "SMM !!!!!!!! InsertImageRecord - CodeSegmentCou= nt is 0 !!!!!!!!\n")); - PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)ImageAddress); if (PdbPointer !=3D NULL) { DEBUG ((DEBUG_ERROR, "SMM !!!!!!!! Image - %a !!!!!!!!\n", PdbPoin= ter)); } =20 + Status =3D EFI_ABORTED; goto Finish; } =20 - // - // Final - // - SortImageRecordCodeSection (ImageRecord); // // Check overlap all section in ImageBase/Size // if (!IsImageRecordCodeSectionValid (ImageRecord)) { DEBUG ((DEBUG_ERROR, "SMM IsImageRecordCodeSectionValid - FAIL\n")); + Status =3D EFI_ABORTED; goto Finish; } =20 @@ -445,6 +329,10 @@ SmmInsertImageRecord ( SortImageRecord (&mImagePropertiesPrivateData.ImageRecordList); =20 Finish: + if (EFI_ERROR (Status) && (ImageRecord !=3D NULL)) { + DeleteImagePropertiesRecord (ImageRecord); + } + return; } =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 9b296aa45762..6c5eb1dc3185 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -13,6 +13,7 @@ #include #include #include +#include #include =20 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ @@ -858,3 +859,188 @@ FindImageRecord ( =20 return NULL; } + +/** + Creates an IMAGE_PROPERTIES_RECORD from a loaded PE image. The PE/COFF h= eader will be found + and parsed to determine the number of code segments and their base addre= sses and sizes. + + @param[in] ImageBase Base of the PE image + @param[in] ImageSize Size of the PE image + @param[in] RequiredAlignment If non-NULL, the alignment speci= fied in the PE/COFF header + will be compared against this va= lue. + @param[out] ImageRecord On out, a populated image proper= ties record + + @retval EFI_INVALID_PARAMETER This function ImageBase or ImageReco= rd was NULL, or the + image located at ImageBase was not a= valid PE/COFF image + @retval EFI_OUT_OF_RESOURCES Failure to Allocate() + @retval EFI_ABORTED The input Alignment was non-NULL and= did not match the + alignment specified in the PE/COFF h= eader + @retval EFI_SUCCESS The image properties record was succ= essfully created +**/ +EFI_STATUS +EFIAPI +CreateImagePropertiesRecord ( + IN CONST VOID *ImageBase, + IN CONST UINT64 ImageSize, + IN CONST UINT32 *RequiredAlignment OPTIONAL, + OUT IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + EFI_STATUS Status; + EFI_IMAGE_DOS_HEADER *DosHdr; + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; + EFI_IMAGE_SECTION_HEADER *Section; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + UINTN Index; + UINT8 *Name; + UINT32 SectionAlignment; + UINT32 PeCoffHeaderOffset; + + if ((ImageRecord =3D=3D NULL) || (ImageBase =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + DEBUG (( + DEBUG_VERBOSE, + "Creating Image Properties Record: 0x%016lx - 0x%016lx\n", + (EFI_PHYSICAL_ADDRESS)(UINTN)ImageBase, + ImageSize + )); + + // + // Step 1: record whole region + // + Status =3D EFI_SUCCESS; + ImageRecord->Signature =3D IMAGE_PROPERTIES_RECORD_SIGNATURE; + ImageRecord->ImageBase =3D (EFI_PHYSICAL_ADDRESS)(UINTN)ImageBase; + ImageRecord->ImageSize =3D ImageSize; + ImageRecord->CodeSegmentCount =3D 0; + InitializeListHead (&ImageRecord->Link); + InitializeListHead (&ImageRecord->CodeSegmentList); + + // Check PE/COFF image + DosHdr =3D (EFI_IMAGE_DOS_HEADER *)(UINTN)ImageBase; + PeCoffHeaderOffset =3D 0; + if (DosHdr->e_magic =3D=3D EFI_IMAGE_DOS_SIGNATURE) { + PeCoffHeaderOffset =3D DosHdr->e_lfanew; + } + + Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINT8 *)(UINTN)ImageBase + PeCo= ffHeaderOffset); + if (Hdr.Pe32->Signature !=3D EFI_IMAGE_NT_SIGNATURE) { + DEBUG ((DEBUG_VERBOSE, "Hdr.Pe32->Signature invalid - 0x%x\n", Hdr.Pe3= 2->Signature)); + return EFI_INVALID_PARAMETER; + } + + // Get SectionAlignment + if (Hdr.Pe32->OptionalHeader.Magic =3D=3D EFI_IMAGE_NT_OPTIONAL_HDR32_MA= GIC) { + SectionAlignment =3D Hdr.Pe32->OptionalHeader.SectionAlignment; + } else { + SectionAlignment =3D Hdr.Pe32Plus->OptionalHeader.SectionAlignment; + } + + // Check RequiredAlignment + if ((RequiredAlignment !=3D NULL) && ((SectionAlignment & (*RequiredAlig= nment - 1)) !=3D 0)) { + DEBUG (( + DEBUG_WARN, + "!!!!!!!! Image Section Alignment(0x%x) does not match Required Ali= gnment (0x%x) !!!!!!!!\n", + SectionAlignment, + *RequiredAlignment + )); + + return EFI_ABORTED; + } + + Section =3D (EFI_IMAGE_SECTION_HEADER *)( + (UINT8 *)(UINTN)ImageBase + + PeCoffHeaderOffset + + sizeof (UINT32) + + sizeof (EFI_IMAGE_FILE_HEADER) + + Hdr.Pe32->FileHeader.SizeOfOption= alHeader + ); + for (Index =3D 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++= ) { + Name =3D Section[Index].Name; + DEBUG (( + DEBUG_VERBOSE, + " Section - '%c%c%c%c%c%c%c%c'\n", + Name[0], + Name[1], + Name[2], + Name[3], + Name[4], + Name[5], + Name[6], + Name[7] + )); + + if ((Section[Index].Characteristics & EFI_IMAGE_SCN_CNT_CODE) !=3D 0) { + DEBUG ((DEBUG_VERBOSE, " VirtualSize - 0x%08x\n", Section[= Index].Misc.VirtualSize)); + DEBUG ((DEBUG_VERBOSE, " VirtualAddress - 0x%08x\n", Section[= Index].VirtualAddress)); + DEBUG ((DEBUG_VERBOSE, " SizeOfRawData - 0x%08x\n", Section[= Index].SizeOfRawData)); + DEBUG ((DEBUG_VERBOSE, " PointerToRawData - 0x%08x\n", Section[= Index].PointerToRawData)); + DEBUG ((DEBUG_VERBOSE, " PointerToRelocations - 0x%08x\n", Section[= Index].PointerToRelocations)); + DEBUG ((DEBUG_VERBOSE, " PointerToLinenumbers - 0x%08x\n", Section[= Index].PointerToLinenumbers)); + DEBUG ((DEBUG_VERBOSE, " NumberOfRelocations - 0x%08x\n", Section[= Index].NumberOfRelocations)); + DEBUG ((DEBUG_VERBOSE, " NumberOfLinenumbers - 0x%08x\n", Section[= Index].NumberOfLinenumbers)); + DEBUG ((DEBUG_VERBOSE, " Characteristics - 0x%08x\n", Section[= Index].Characteristics)); + + // Record code section(s) + ImageRecordCodeSection =3D AllocatePool (sizeof (*ImageRecordCodeSec= tion)); + if (ImageRecordCodeSection =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + ImageRecordCodeSection->Signature =3D IMAGE_PROPERTIES_RECORD_CODE_S= ECTION_SIGNATURE; + + ImageRecordCodeSection->CodeSegmentBase =3D (UINTN)ImageBase + Secti= on[Index].VirtualAddress; + ImageRecordCodeSection->CodeSegmentSize =3D Section[Index].SizeOfRaw= Data; + + InsertTailList (&ImageRecord->CodeSegmentList, &ImageRecordCodeSecti= on->Link); + ImageRecord->CodeSegmentCount++; + } + } + + if (ImageRecord->CodeSegmentCount > 0) { + SortImageRecordCodeSection (ImageRecord); + } + + return Status; +} + +/** + Deleted an image properties record. The function will also call + RemoveEntryList() on each code segment and the input ImageRecord before + freeing each pool. + + @param[in] ImageRecord The IMAGE_PROPERTIES_RECORD to d= elete +**/ +VOID +EFIAPI +DeleteImagePropertiesRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ) +{ + LIST_ENTRY *CodeSegmentListHead; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; + + if (ImageRecord =3D=3D NULL) { + return; + } + + CodeSegmentListHead =3D &ImageRecord->CodeSegmentList; + while (!IsListEmpty (CodeSegmentListHead)) { + ImageRecordCodeSection =3D CR ( + CodeSegmentListHead->ForwardLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNAT= URE + ); + RemoveEntryList (&ImageRecordCodeSection->Link); + FreePool (ImageRecordCodeSection); + } + + if (!IsListEmpty (&ImageRecord->Link)) { + RemoveEntryList (&ImageRecord->Link); + } + + FreePool (ImageRecord); +} diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index e3f569ab03d1..5090a521536b 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -192,4 +192,43 @@ DumpImageRecord ( IN LIST_ENTRY *ImageRecordList ); =20 +/** + Creates an IMAGE_PROPERTIES_RECORD from a loaded PE image. The PE/COFF h= eader will be found + and parsed to determine the number of code segments and their base addre= sses and sizes. + + @param[in] ImageBase Base of the PE image + @param[in] ImageSize Size of the PE image + @param[in] RequiredAlignment If non-NULL, the alignment speci= fied in the PE/COFF header + will be compared against this va= lue. + @param[out] ImageRecord On out, a populated image proper= ties record + + @retval EFI_INVALID_PARAMETER This function ImageBase or ImageReco= rd was NULL, or the + image located at ImageBase was not a= valid PE/COFF image + @retval EFI_OUT_OF_RESOURCES Failure to Allocate() + @retval EFI_ABORTED The input Alignment was non-NULL and= did not match the + alignment specified in the PE/COFF h= eader + @retval EFI_SUCCESS The image properties record was succ= essfully created +**/ +EFI_STATUS +EFIAPI +CreateImagePropertiesRecord ( + IN CONST VOID *ImageBase, + IN CONST UINT64 ImageSize, + IN CONST UINT32 *Alignment OPTIONAL, + OUT IMAGE_PROPERTIES_RECORD *ImageRecord + ); + +/** + Deleted an image properties record. The function will also call + RemoveEntryList() on each code segment and the input ImageRecord before + freeing each pool. + + @param[in] ImageRecord The IMAGE_PROPERTIES_RECORD to d= elete +**/ +VOID +EFIAPI +DeleteImagePropertiesRecord ( + IN IMAGE_PROPERTIES_RECORD *ImageRecord + ); + #endif diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.inf b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= sRecordLib.inf index 4c1466fc3336..cfe0c04b3b05 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf @@ -23,6 +23,7 @@ [LibraryClasses] BaseLib BaseMemoryLib DebugLib + MemoryAllocationLib =20 [Packages] MdePkg/MdePkg.dec --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110661): https://edk2.groups.io/g/devel/message/110661 Mute This Topic: https://groups.io/mt/102368854/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Nov 14 07:09:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+110662+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110662+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1699031848; cv=none; d=zohomail.com; s=zohoarc; b=GAoJUY9kSEFrr7WggmqlGeh7K5x0DKWwZOIQXT0RGLJiLMEy1whPj0QqPw5Y6up4AlNvpZtGK5ngH6uJ59zH192XspMunM4SD8bcZpxj7hiPDDVoh6NaSueKz8D1mCD1y3xQOiPoQ6nSR9qSib6xk2f70pMJcZdFzAuWdFIvfXk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1699031848; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=QnaQsgTtxV37CBcadqrsXl86eSjfYfC17Z3m7DlK/7k=; b=bnxxL//KzdUsx5vaWNh6UTLYhlCCP94xwa/PmEby1pKMwNxQVFfuA1J1daWLXecMlViEpo+pHuI/JITERMoY1gb+tbDFcjjU9qPQ6JiwoyUy5c7E6P24D/VwKlOyuqj7rXvPaTxngbbEhe6XpJ9w3+EoouYOfLt5XxVEnQ9dDAY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+110662+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1699031848716539.3546595974186; Fri, 3 Nov 2023 10:17:28 -0700 (PDT) Return-Path: DKIM-Signature: a=rsa-sha256; bh=IKnsoao+0D7VWfFBvNa+55aLSyqdl9bdZuxELIREBaE=; 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=1699031848; v=1; b=fngYqhM8BjyVfiKocMv3fb/f2DB/qjvBFGk+865dKpel4yVo/EIEK7nUm8dN/NmuiZmM/azJ wRsRoz+21Srbk0Ehw1PAflBNYCvYmppontItvssrcCCxrQJ4zpPFFKJhY40KCPUQMSWk2GUVvzw wjfDW/+AksRgWWBXuC0RRAdc= X-Received: by 127.0.0.2 with SMTP id bmYjYY1788612x2cRp3jPzrv; Fri, 03 Nov 2023 10:17:28 -0700 X-Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mx.groups.io with SMTP id smtpd.web11.59071.1699031847048630009 for ; Fri, 03 Nov 2023 10:17:27 -0700 X-Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1cc131e52f1so27367305ad.0 for ; Fri, 03 Nov 2023 10:17:27 -0700 (PDT) X-Gm-Message-State: NuoJsqftXk1qh5Qbf2edzlYox1787277AA= X-Google-Smtp-Source: AGHT+IF3nrPgcrN1+d5NjCIEEahP4aqdJrG9dPVKVd9IbXdnO5Lg9/HD3R/+3GLt+KEgEXxwWITvYA== X-Received: by 2002:a17:902:ea02:b0:1cc:476c:8974 with SMTP id s2-20020a170902ea0200b001cc476c8974mr4369256plg.0.1699031846375; Fri, 03 Nov 2023 10:17:26 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id b8-20020a170903228800b001bf5e24b2a8sm1625152plh.174.2023.11.03.10.17.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 10:17:26 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Dandan Bi , Jiaxin Wu , Ray Ni Subject: [edk2-devel] [PATCH v4 14/14] MdeModulePkg: Update DumpImageRecord() in ImagePropertiesRecordLib Date: Fri, 3 Nov 2023 10:17:06 -0700 Message-ID: <20231103171706.148-15-taylor.d.beebe@gmail.com> In-Reply-To: <20231103171706.148-1-taylor.d.beebe@gmail.com> References: <20231103171706.148-1-taylor.d.beebe@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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1699031850981100055 Content-Type: text/plain; charset="utf-8" Update DumpImageRecord() to be DumpImageRecords(), and improve the debug output. The function will output at DEBUG_INFO instead, and the function will be run in DXE and SMM MAT logic when the MAT is installed at EndOfDxe on DEBUG builds. Cc: Jian J Wang Cc: Liming Gao Cc: Dandan Bi Cc: Jiaxin Wu Cc: Ray Ni Signed-off-by: Taylor Beebe Reviewed-by: Liming Gao --- MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c = | 9 ++ MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c = | 11 +- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.c = | 134 +++++++++++++++++--- MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h = | 6 +- MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf= | 1 + MdeModulePkg/Test/MdeModulePkgHostTest.dsc = | 1 + 6 files changed, 138 insertions(+), 24 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c b/MdeModule= Pkg/Core/Dxe/Misc/MemoryAttributesTable.c index 993db281062a..e9343a2c4ef1 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryAttributesTable.c @@ -284,6 +284,15 @@ InstallMemoryAttributesTableOnEndOfDxe ( { mMemoryAttributesTableEndOfDxe =3D TRUE; InstallMemoryAttributesTable (); + + DEBUG_CODE_BEGIN (); + if ( mImagePropertiesPrivateData.ImageRecordCount > 0) { + DEBUG ((DEBUG_INFO, "DXE - Total Runtime Image Count: 0x%x\n", mImageP= ropertiesPrivateData.ImageRecordCount)); + DEBUG ((DEBUG_INFO, "DXE - Dump Runtime Image Records:\n")); + DumpImageRecords (&mImagePropertiesPrivateData.ImageRecordList); + } + + DEBUG_CODE_END (); } =20 /** diff --git a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c b/MdeModul= ePkg/Core/PiSmmCore/MemoryAttributesTable.c index 03de9b2c5fff..28fe74ecc421 100644 --- a/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c +++ b/MdeModulePkg/Core/PiSmmCore/MemoryAttributesTable.c @@ -496,9 +496,14 @@ SmmInstallMemoryAttributesTable ( return EFI_SUCCESS; } =20 - DEBUG ((DEBUG_VERBOSE, "SMM Total Image Count - 0x%x\n", mImagePropertie= sPrivateData.ImageRecordCount)); - DEBUG ((DEBUG_VERBOSE, "SMM Dump ImageRecord:\n")); - DumpImageRecord (&mImagePropertiesPrivateData.ImageRecordList); + DEBUG_CODE_BEGIN (); + if ( mImagePropertiesPrivateData.ImageRecordCount > 0) { + DEBUG ((DEBUG_INFO, "SMM - Total Runtime Image Count - 0x%x\n", mImage= PropertiesPrivateData.ImageRecordCount)); + DEBUG ((DEBUG_INFO, "SMM - Dump Runtime Image Records:\n")); + DumpImageRecords (&mImagePropertiesPrivateData.ImageRecordList); + } + + DEBUG_CODE_END (); =20 PublishMemoryAttributesTable (); =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.c index 6c5eb1dc3185..e53ce086c54c 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.c @@ -14,6 +14,7 @@ #include #include #include +#include #include =20 #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ @@ -785,31 +786,128 @@ SortImageRecord ( } =20 /** - Dump image record. + Extract the .efi filename out of the input PDB. =20 - @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries + @param[in] PdbPointer Pointer to the PDB file path. + @param[out] EfiFileName Pointer to the .efi filename. + @param[in] EfiFileNameSize Size of the .efi filename buffer. +**/ +STATIC +VOID +GetFilename ( + IN CHAR8 *PdbPointer, + OUT CHAR8 *EfiFileName, + IN UINTN EfiFileNameSize + ) +{ + UINTN Index; + UINTN StartIndex; + + if ((PdbPointer =3D=3D NULL) || (EfiFileNameSize < 5)) { + return; + } + + // Print Module Name by Pdb file path. + StartIndex =3D 0; + for (Index =3D 0; PdbPointer[Index] !=3D 0; Index++) { + if ((PdbPointer[Index] =3D=3D '\\') || (PdbPointer[Index] =3D=3D '/'))= { + StartIndex =3D Index + 1; + } + } + + // Copy the PDB file name to EfiFileName and replace .pdb with .efi + for (Index =3D 0; Index < EfiFileNameSize - 4; Index++) { + EfiFileName[Index] =3D PdbPointer[Index + StartIndex]; + if (EfiFileName[Index] =3D=3D 0) { + EfiFileName[Index] =3D '.'; + } + + if (EfiFileName[Index] =3D=3D '.') { + EfiFileName[Index + 1] =3D 'e'; + EfiFileName[Index + 2] =3D 'f'; + EfiFileName[Index + 3] =3D 'i'; + EfiFileName[Index + 4] =3D 0; + break; + } + } + + if (Index =3D=3D sizeof (EfiFileName) - 4) { + EfiFileName[Index] =3D 0; + } +} + +/** + Debug dumps the input list of IMAGE_PROPERTIES_RECORD structs. + + @param[in] ImageRecordList Head of the IMAGE_PROPERTIES_RECORD list **/ VOID EFIAPI -DumpImageRecord ( +DumpImageRecords ( IN LIST_ENTRY *ImageRecordList ) { - IMAGE_PROPERTIES_RECORD *ImageRecord; - LIST_ENTRY *ImageRecordLink; - UINTN Index; - - for (ImageRecordLink =3D ImageRecordList->ForwardLink, Index =3D 0; - ImageRecordLink !=3D ImageRecordList; - ImageRecordLink =3D ImageRecordLink->ForwardLink, Index++) - { - ImageRecord =3D CR ( - ImageRecordLink, - IMAGE_PROPERTIES_RECORD, - Link, - IMAGE_PROPERTIES_RECORD_SIGNATURE - ); - DEBUG ((DEBUG_VERBOSE, "Image[%d]: 0x%016lx - 0x%016lx\n", Index, Imag= eRecord->ImageBase, ImageRecord->ImageSize)); + LIST_ENTRY *ImageRecordLink; + IMAGE_PROPERTIES_RECORD *CurrentImageRecord; + LIST_ENTRY *CodeSectionLink; + IMAGE_PROPERTIES_RECORD_CODE_SECTION *CurrentCodeSection; + CHAR8 *PdbPointer; + CHAR8 EfiFileName[256]; + + if (ImageRecordList =3D=3D NULL) { + return; + } + + ImageRecordLink =3D ImageRecordList->ForwardLink; + + while (ImageRecordLink !=3D ImageRecordList) { + CurrentImageRecord =3D CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + PdbPointer =3D PeCoffLoaderGetPdbPointer ((VOID *)(UINTN)CurrentImageR= ecord->ImageBase); + if (PdbPointer !=3D NULL) { + GetFilename (PdbPointer, EfiFileName, sizeof (EfiFileName)); + DEBUG (( + DEBUG_INFO, + "%a: 0x%llx - 0x%llx\n", + EfiFileName, + CurrentImageRecord->ImageBase, + CurrentImageRecord->ImageBase + CurrentImageRecord->ImageSize + )); + } else { + DEBUG (( + DEBUG_INFO, + "Unknown Image: 0x%llx - 0x%llx\n", + CurrentImageRecord->ImageBase, + CurrentImageRecord->ImageBase + CurrentImageRecord->ImageSize + )); + } + + CodeSectionLink =3D CurrentImageRecord->CodeSegmentList.ForwardLink; + + while (CodeSectionLink !=3D &CurrentImageRecord->CodeSegmentList) { + CurrentCodeSection =3D CR ( + CodeSectionLink, + IMAGE_PROPERTIES_RECORD_CODE_SECTION, + Link, + IMAGE_PROPERTIES_RECORD_CODE_SECTION_SIGNATURE + ); + + DEBUG (( + DEBUG_INFO, + " Code Section: 0x%llx - 0x%llx\n", + CurrentCodeSection->CodeSegmentBase, + CurrentCodeSection->CodeSegmentBase + CurrentCodeSection->CodeSegm= entSize + )); + + CodeSectionLink =3D CodeSectionLink->ForwardLink; + } + + ImageRecordLink =3D ImageRecordLink->ForwardLink; } } =20 diff --git a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h b/MdeM= odulePkg/Include/Library/ImagePropertiesRecordLib.h index 5090a521536b..87c94c723752 100644 --- a/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h +++ b/MdeModulePkg/Include/Library/ImagePropertiesRecordLib.h @@ -182,13 +182,13 @@ FindImageRecord ( ); =20 /** - Dump image record. + Debug dumps the input list of IMAGE_PROPERTIES_RECORD structs. =20 - @param[in] ImageRecordList A list of IMAGE_PROPERTIES_RECORD entries + @param[in] ImageRecordList Head of the IMAGE_PROPERTIES_RECORD list **/ VOID EFIAPI -DumpImageRecord ( +DumpImageRecords ( IN LIST_ENTRY *ImageRecordList ); =20 diff --git a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesR= ecordLib.inf b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertie= sRecordLib.inf index cfe0c04b3b05..1f82cc3e469c 100644 --- a/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf +++ b/MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLi= b.inf @@ -24,6 +24,7 @@ [LibraryClasses] BaseMemoryLib DebugLib MemoryAllocationLib + PeCoffGetEntryPointLib =20 [Packages] MdePkg/MdePkg.dec diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test= /MdeModulePkgHostTest.dsc index 2b8cbb867a73..198cdd814fb9 100644 --- a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc +++ b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc @@ -57,6 +57,7 @@ [Components] MdeModulePkg/Library/ImagePropertiesRecordLib/UnitTest/ImagePropertiesRe= cordLibUnitTestHost.inf { ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordL= ib/ImagePropertiesRecordLib.inf + PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/Bas= ePeCoffGetEntryPointLib.inf } =20 # --=20 2.42.0.windows.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110662): https://edk2.groups.io/g/devel/message/110662 Mute This Topic: https://groups.io/mt/102368856/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-