Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oOgtt-0004nr-DV for mharc-grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36550) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oOgtr-0004kE-Gi for grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:35 -0400 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]:51138) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oOgto-0001Cw-FR for grub-devel@gnu.org; Thu, 18 Aug 2022 10:53:34 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B92D1B8218F; Thu, 18 Aug 2022 14:53:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5853DC433C1; Thu, 18 Aug 2022 14:53:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660834407; bh=k2UO8HelSkz+8GRaRPGWyK8im8ylx1c2njdpoI2nOpU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jErMdp9znw9GOWiCRbeMNwW1ChQWO0RcPa5iABtMVW9te6Be00lHoN5gVMIEH2q/7 a65ka/+5VNEXlxaVnjhHKaVY9Qm8jfnEN2+RnHK0fC42xr58aM9+JQPxSrJUmUyY0W L0HGdshKDQFni+vG02VswEh03OU+Wy1k0Ok77pTgTVcjC3zp8sqWUwzZu3AwXd49HY ghasshHLYqUPFZfI5i4sVXARIs22pcPRN6mTI82/HOc6z75udYnhqeJ+lTXfbxKA5X Lc42HvHsCa2tbPmJvmG2teicNOwXkZ9PE6bclLWPqOIZxT22YhQuPDchIX5e/vMYV0 2PBoN46UM/Lvg== From: Ard Biesheuvel To: grub-devel@gnu.org Cc: Ard Biesheuvel , Daniel Kiper , Leif Lindholm , Nikita Ermakov , Atish Patra , Huacai Chen , Heinrich Schuchardt , dann frazier , Julian Andres Klode Subject: [PATCH v3 2/8] efi: move MS-DOS stub out of generic PE header definition Date: Thu, 18 Aug 2022 16:51:29 +0200 Message-Id: <20220818145135.2208251-3-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220818145135.2208251-1-ardb@kernel.org> References: <20220818145135.2208251-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2567; i=ardb@kernel.org; h=from:subject; bh=k2UO8HelSkz+8GRaRPGWyK8im8ylx1c2njdpoI2nOpU=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/lHnQdqCnOdynwO+RnWMsZoYnJV20mPQ/Lae/HQB bKHn2jiJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv5R5wAKCRDDTyI5ktmPJGwQC/ 9eJ9ZE9TDSG8qE/FbADcZIsszcrbqkqxqgC+dhXmZIH6R6KG0d6bEgwrM44nUenZBd5X3NsV+pNsff nbxQQSeXqoeK8ztzd6kgu+8Lj6uxp2BaXvqy3d7il50Aideu1sM7xw+nsFPm0SH9JdZkBBPVcAo3YJ 2VC/SZOBFFEZq/xpuTKfDFLTl2p9meCwftqt+s37Pmv7yNf29t2QrGRfik6J3Kdv4Qbt3UpbjJEEqN lSX3+hBvh1Sc0brHfXG7j8QXmJX/1mGs1mTNG1BLBhBqr6H2I9zlvwBkRQCK2m7LQ772scEERB7O/L /34//fN6+waIP/3bmg3kDt4tf+o9h2xGQtQAD5rZs5TJ3YoFAwCQr18bhW/0hK6dYpqdyQk0yeRSAi IKXeocfvIBlOrN8wjaiQLoqFiMKDabwjZWFCD7OOCYMMH1z5j6rHyTq3W+oW0G7sitp09OdKJ9jhku AI4JAz5dYyCVn+324yg1UBZYRvh8hPQxPN5nHmkWgK6Mk= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2604:1380:4601:e00::1; envelope-from=ardb@kernel.org; helo=ams.source.kernel.org X-Spam_score_int: -71 X-Spam_score: -7.2 X-Spam_bar: ------- X-Spam_report: (-7.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Aug 2022 14:53:35 -0000 The PE/COFF spec permits the COFF signature and file header to appear anywhere in the file, and the actual offset is recorded in 4 byte little endian field at offset 0x3c of the image. When GRUB is emitted as a PE/COFF binary, we reuse the 128 byte MS-DOS stub (even for non-x86 architectures), putting the COFF signature and file header at offset 0x80. However, other PE/COFF images may use different values, and non-x86 Linux kernels use an offset of 0x40 instead. So let's get rid of the grub_pe32_header struct from pe32.h, given that it does not represent anything defined by the PE/COFF spec. Instead, use the GRUB_PE32_MSDOS_STUB_SIZE macro explicitly to reference the COFF header in the only place in the code where we rely on this. The remaining fields are moved into a struct grub_coff_image_header, which we will use later to access COFF header fields of arbitrary images (and which may therefore appear at different offsets) Signed-off-by: Ard Biesheuvel --- grub-core/kern/efi/efi.c | 5 +++-- include/grub/efi/pe32.h | 5 +---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index e8a976a22f15..8bef81663853 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -302,7 +302,7 @@ grub_addr_t grub_efi_modules_addr (void) { grub_efi_loaded_image_t *image; - struct grub_pe32_header *header; + struct grub_coff_image_header *header; struct grub_pe32_coff_header *coff_header; struct grub_pe32_section_table *sections; struct grub_pe32_section_table *section; @@ -313,7 +313,8 @@ grub_efi_modules_addr (void) if (! image) return 0; - header = image->image_base; + header = (struct grub_coff_image_header *) ((char *) image->image_base + + GRUB_PE32_MSDOS_STUB_SIZE); coff_header = &(header->coff_header); sections = (struct grub_pe32_section_table *) ((char *) coff_header diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h index 0ed8781f0376..a2da4b318c85 100644 --- a/include/grub/efi/pe32.h +++ b/include/grub/efi/pe32.h @@ -254,11 +254,8 @@ struct grub_pe32_section_table #define GRUB_PE32_SIGNATURE_SIZE 4 -struct grub_pe32_header +struct grub_coff_image_header { - /* This should be filled in with GRUB_PE32_MSDOS_STUB. */ - grub_uint8_t msdos_stub[GRUB_PE32_MSDOS_STUB_SIZE]; - /* This is always PE\0\0. */ char signature[GRUB_PE32_SIGNATURE_SIZE]; -- 2.35.1