From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oObKc-0006UW-7D for mharc-grub-devel@gnu.org; Thu, 18 Aug 2022 04:56:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50204) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oObJw-0006FG-LN for grub-devel@gnu.org; Thu, 18 Aug 2022 04:56:11 -0400 Received: from ams.source.kernel.org ([145.40.68.75]:35440) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oObJq-00018x-24 for grub-devel@gnu.org; Thu, 18 Aug 2022 04:56:07 -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 D63BFB8212F; Thu, 18 Aug 2022 08:55:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DC11C433D6; Thu, 18 Aug 2022 08:55:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812952; bh=k2UO8HelSkz+8GRaRPGWyK8im8ylx1c2njdpoI2nOpU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DcqUIsPlBOaS50uRmOOBdwsEcurT0MyOrflb/x0elsocQ3PtoQxkCbpsPGiCL9nPn iGQTxTauT5f65idC60m6mA1J4j4DEw/KeBPjLD5uxh6vghbg74Q81sIvOwh/xXKGhb Wl+3k9hNPolKcQa9PpCa+JijzzDIXkiLCVusQ7KWURwgDeZW0kG000gKWOH53l2csA MWhMrQMt/Oom/DxPEEs4IyRwH1ViU4tQKyqInok9GrL/InH7y2/ycpXhMKrv7oanxV JAZzcfFnh5YhSnBiwn+E7Fb+GeNEGng7KNpd77bZJ3NboETMayJmiOF6wABCPStymj GgxuxP59fMStg== From: Ard Biesheuvel To: grub-devel@nongnu.org Cc: linux-efi@vger.kernel.org, Ard Biesheuvel , grub-devel@gnu.org, Daniel Kiper , Nikita Ermakov , Atish Patra , Huacai Chen , Heinrich Schuchardt , dann frazier , Julian Andres Klode Subject: [PATCH resend 2/9] efi: move MS-DOS stub out of generic PE header definition Date: Thu, 18 Aug 2022 10:55:32 +0200 Message-Id: <20220818085540.2075028-3-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220818085540.2075028-1-ardb@kernel.org> References: <20220818085540.2075028-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/f56QdqCnOdynwO+RnWMsZoYnJV20mPQ/Lae/HQB bKHn2jiJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+egAKCRDDTyI5ktmPJKs8C/ 0YWR2+S0Ee4+hBFdguiDzMuGbTNIiU/O0N3hrHqBWzRgYcK4W6tDwFKDeDCwjL3L8AJW0qugdmw6v8 3cAQkccamZOdtR6OqjUNqvmk9wGlGx8c/afLzac1C4ZSZO6a88ZRahMmjfWJE/syz0UXPyjP+wk0OG WQnGYrywimRpm8n3t2s/hdTdwGWdKcbkaMjP7dQKcjZ53rfz2zJ81zwCmmihDXzxBn3P52xMqnAK6l Xk0mPOkBjF/m972LDE4el3lu4n2okGxjHurnCLeLlhL9NYwOjPfHdGIix7lAx1leo0SKXHHjOiPdG4 nxIB04AWGLdK+9jPNjzXcnvQFfjUGgliT1duByVTvwTPFogj320BFIHXrFvf5QSug5v77383EhpuWQ jlCMwuwBNNCzlvtfGm7Lq6h+YIFgpZvs+1kXaGT7Tcjyc7U+tMAcK5AapQ/iLqqRyZeVJrepBPghW5 MD6zgtLgzkTRBRXu1aRq9JDmF9LFY+OLtklOfazbl5muc= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=145.40.68.75; 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 08:56:13 -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