From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1oWHcJ-0007wk-Ab for mharc-grub-devel@gnu.org; Thu, 08 Sep 2022 09:30:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56048) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWHcG-0007tG-Gz for grub-devel@gnu.org; Thu, 08 Sep 2022 09:30:48 -0400 Received: from dfw.source.kernel.org ([139.178.84.217]:59826) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oWHcE-00036q-Hv for grub-devel@gnu.org; Thu, 08 Sep 2022 09:30:48 -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 dfw.source.kernel.org (Postfix) with ESMTPS id C208F61CE6; Thu, 8 Sep 2022 13:30:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CABFC4347C; Thu, 8 Sep 2022 13:30:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662643845; bh=PUH8ubi1inN+sPHNwcOFPPEDn/oy1ETIJeGxkRhlphk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KiBGT4EIAXhprNYgy/uLPoyQaVcSrbMPqsrlmrx85a2RMNWP9SN0yLvQiS6DxAcsW WovV6r9ZvdJxasQkq6VtFp0FgWwN9JeMmal9n+qMa4YAXX/Sft+0oVjgmrM16TQzna kW8A2SBLiMMsXQ0xO2XxzTkaA1w3V8vTUcC5o/z30kERl7OrTSr1YfIxjqSeScZ2Ly 6awiBmUHAB9n4YmH3oZjTSEX/EywkhPx8ARWJxdftDco/48mHAVHVn2BrgCCiLnba1 s5VwqH+bU9eXv4IujVDCloOiC8rUXqVgHGZFELavPcvT8kdwgjHka2i8mAU0lPGE2D gwIkQE5fvjWTQ== 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 , Ilias Apalodimas Subject: [PATCH v4 1/6] efi: move MS-DOS stub out of generic PE header definition Date: Thu, 8 Sep 2022 15:30:12 +0200 Message-Id: <20220908133017.1464494-2-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220908133017.1464494-1-ardb@kernel.org> References: <20220908133017.1464494-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3347; i=ardb@kernel.org; h=from:subject; bh=PUH8ubi1inN+sPHNwcOFPPEDn/oy1ETIJeGxkRhlphk=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjGe5fPD5tXwbnUl9v/H2Wn94iMp9gbfbyl/gDTAwW 1AF0u5iJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYxnuXwAKCRDDTyI5ktmPJNSyDA CUA1RfyGDiRveQqPFNjoP8NqcXq9hy+h3r8EMwREIS9h7jDLFAzwrAG42AqQFJRYfqGESRCodp7x74 PNFKEKrFfU4WNOsXRXsnrAt3fGHAuWiN6UNgCBLgH/imX7jX5zFX+polCBgxDi7hT/3pTnIrORK5WA fTapBNJj7+N7Heni8mcDVh8DFTreb08d4+hELhO3QuxZP0aSq8GPiXlwjDKtQBrVyegnGP/hQVM+ei o+4eQ3BvQ9yeSRJl11K4jXkfHlFvyBjuGwG+2qKSUoip+BAyukzr9XxnHpDmWF09HXy7wsaCx2TyQ7 zJopvdltltOPgO0FzWeQx/NYBLul3t3bUJ9kJydybwo/S+832rS1Z0439bif3z//hTFWiUvcY9CoT8 tUuJtSQAKfQNlg5BMm1c092XIUrgucirjJELMRTnZ/qsJCIsh5RqFBefMaoLHmiJ3KrS8BEk7mCdtc G+XiTEW/ii6btRS84d94ct389aFz7rcTzqt3PCK/vHTPY= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=139.178.84.217; envelope-from=ardb@kernel.org; helo=dfw.source.kernel.org X-Spam_score_int: -70 X-Spam_score: -7.1 X-Spam_bar: ------- X-Spam_report: (-7.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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, 08 Sep 2022 13:30:48 -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, introduce a minimal struct grub_msdos_image_header type based on the PE/COFF spec's description of the image header, and use the offset recorded at file position 0x3c to discover the actual location of the PE signature and the COFF image header. 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 | 8 ++++++-- include/grub/efi/pe32.h | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c index e8a976a22f15..f85587d66635 100644 --- a/grub-core/kern/efi/efi.c +++ b/grub-core/kern/efi/efi.c @@ -302,7 +302,8 @@ grub_addr_t grub_efi_modules_addr (void) { grub_efi_loaded_image_t *image; - struct grub_pe32_header *header; + struct grub_msdos_image_header *dos_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 +314,10 @@ grub_efi_modules_addr (void) if (! image) return 0; - header = image->image_base; + dos_header = (struct grub_msdos_image_header *)image->image_base; + + header = (struct grub_coff_image_header *) ((char *) dos_header + + dos_header->pe_signature_offset); 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..6688d96c0046 100644 --- a/include/grub/efi/pe32.h +++ b/include/grub/efi/pe32.h @@ -48,6 +48,17 @@ #define GRUB_PE32_MAGIC 0x5a4d +struct grub_msdos_image_header +{ + /* This is always 'MZ'. (GRUB_PE32_MAGIC) */ + grub_uint16_t msdos_magic; + + grub_uint16_t reserved[29]; + + /* The file offset of the PE signature and COFF image header. */ + grub_uint32_t pe_signature_offset; +}; + /* According to the spec, the minimal alignment is 512 bytes... But some examples (such as EFI drivers in the Intel Sample Implementation) use 32 bytes (0x20) instead, and it seems @@ -254,11 +265,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