From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1okrvD-0007aA-Ed for mharc-grub-devel@gnu.org; Tue, 18 Oct 2022 15:06:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47140) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okruW-0007NW-Nz for grub-devel@gnu.org; Tue, 18 Oct 2022 15:05:59 -0400 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]:47440) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1okruB-0000bW-0g for grub-devel@gnu.org; Tue, 18 Oct 2022 15:05:40 -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 A6602B820F5; Tue, 18 Oct 2022 19:05:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B02AC433C1; Tue, 18 Oct 2022 19:05:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666119925; bh=pVZF77q3LXk2Lj3zRE2CmEChu4ebQraIeh1lqjxGUWY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KqUDh4HMV/9pRBJE0Oknybn/ZmYle372ncc6+sqBVGoGuYXWKd1rBS8JjkJiLO+v3 sA4l+yYzsXhkVimXRFFfYCPnvftiVizWZEs8s+xn6/jUzubbKLCYjTfBOxwV5Dq9ot +8QaAUKS+wyH5l8wsnS6qtY0yJa+FE8TQiTI9mWboyYCM3eAZGbQ9WyG0xDn6Tw6Kk UcXPYqhAxRLpDwNAvzRW0/i/vDlksMFceZf4WNKKFDHrQA19fA4LFZMtAAxFznDySp OoLRfkma3xM+8UyMl1usl6c6y+JzGMqyjrRV/aukGZ3rW/dV/TdAuGJ5YUQ8zXhQH9 I+EQNgG1sRaxw== 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 v5 2/6] linux/arm: unify ARM/arm64 vs Xen PE/COFF header handling Date: Tue, 18 Oct 2022 21:05:03 +0200 Message-Id: <20221018190508.177568-3-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221018190508.177568-1-ardb@kernel.org> References: <20221018190508.177568-1-ardb@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6397; i=ardb@kernel.org; h=from:subject; bh=pVZF77q3LXk2Lj3zRE2CmEChu4ebQraIeh1lqjxGUWY=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBjTvjc4n9Gsg6tUzqfVJmjSbYrnW95jVtiQ1dettVy fKrqh1iJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCY0743AAKCRDDTyI5ktmPJIayDA CmT9IiZpOW+U1OU3JnHn0wXxeFi2tWTcCkhMbYLGJhuxdWYx+lNzSWJWCUI8TdZ06qORtrHG/XfJ0d hVOIfzEVss6aTf/bNLozI9miqIeFUGac1NJtPv+bqE5D47MP1Q/cagy5UkNtgC63vKIGz5C4sUZ+oe FVgGWSAi9kM12c12QqzvbYeAo9fTLi4DueG3AKxx+SyUGvw9wFF6H/7Q2Uy+uv7tGwka4lWZEedYoy lT+IpMYPbmHFW/HLnmp/jmAMqevNUZYPsaUEznOC3IAbzjO5Fx4Orwv6zHxbhlICPztA9E3As3tl25 VIn35m09ytxmhKfqgIjvckc1LAjbcuSbFyj1CTPH59uWT1feKHvrsJ6OLboO5YzkRIAL3SVEJ5N6KC Ni1Rc+ZiuuACmiEQA8pYhxnwqtOj2QX3LD8/HJuCqf6hrSevQK6iPko+JBXj6m/MUpC0OMvUirGvFN rFPfIUsMZA1fcduVuugQ17rGXuc2soQu3Ifj1wx/KAZXA= 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: -73 X-Spam_score: -7.4 X-Spam_bar: ------- X-Spam_report: (-7.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.256, 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 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: Tue, 18 Oct 2022 19:06:00 -0000 Xen has its own version of the image header, to account for the additional PE/COFF header fields. Since we are adding references to those in the shared EFI loader code, update the common definitions and drop the Xen specific one which no longer has a purpose. Since in both cases, the call to grub_arch_efi_linux_check_image() is preceded by a load of the image header, let's move the load into that function, and rename it to grub_arch_efi_linux_load_image_header()/ Signed-off-by: Ard Biesheuvel --- grub-core/loader/arm64/linux.c | 12 +++++----- grub-core/loader/arm64/xen_boot.c | 23 ++++---------------- include/grub/arm/linux.h | 5 +++++ include/grub/arm64/linux.h | 2 ++ include/grub/efi/efi.h | 4 +++- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index b5b559c236e0..3733a69d6e36 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -49,8 +49,13 @@ static grub_addr_t initrd_start; static grub_addr_t initrd_end; grub_err_t -grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh) +grub_arch_efi_linux_load_image_header (grub_file_t file, + struct linux_arch_kernel_header * lh) { + grub_file_seek (file, 0); + if (grub_file_read (file, lh, sizeof (*lh)) < (grub_ssize_t) sizeof (*lh)) + return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read Linux image header"); + if ((lh->code0 & 0xffff) != GRUB_PE32_MAGIC) return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET, N_("plain image kernel not supported - rebuild with CONFIG_(U)EFI_STUB enabled")); @@ -301,10 +306,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), kernel_size = grub_file_size (file); - if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) - return grub_errno; - - if (grub_arch_efi_linux_check_image (&lh) != GRUB_ERR_NONE) + if (grub_arch_efi_linux_load_image_header (file, &lh) != GRUB_ERR_NONE) goto fail; grub_loader_unset(); diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c index 22cc25eccd9d..763d87dcde5b 100644 --- a/grub-core/loader/arm64/xen_boot.c +++ b/grub-core/loader/arm64/xen_boot.c @@ -31,7 +31,6 @@ #include #include #include -#include /* required by struct xen_hypervisor_header */ #include #include @@ -65,18 +64,6 @@ enum module_type }; typedef enum module_type module_type_t; -struct xen_hypervisor_header -{ - struct linux_arm64_kernel_header efi_head; - - /* This is always PE\0\0. */ - grub_uint8_t signature[GRUB_PE32_SIGNATURE_SIZE]; - /* The COFF file header. */ - struct grub_pe32_coff_header coff_header; - /* The Optional header. */ - struct grub_pe64_optional_header optional_header; -}; - struct xen_boot_binary { struct xen_boot_binary *next; @@ -452,7 +439,7 @@ static grub_err_t grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { - struct xen_hypervisor_header sh; + struct linux_arm64_kernel_header lh; grub_file_t file = NULL; grub_dl_ref (my_mod); @@ -467,10 +454,7 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)), if (!file) goto fail; - if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh)) - goto fail; - if (grub_arch_efi_linux_check_image - ((struct linux_arch_kernel_header *) &sh) != GRUB_ERR_NONE) + if (grub_arch_efi_linux_load_image_header (file, &lh) != GRUB_ERR_NONE) goto fail; grub_file_seek (file, 0); @@ -484,7 +468,8 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)), return grub_errno; xen_hypervisor->is_hypervisor = 1; - xen_hypervisor->align = (grub_size_t) sh.optional_header.section_alignment; + xen_hypervisor->align + = (grub_size_t) lh.pe_image_header.optional_header.section_alignment; xen_boot_binary_load (xen_hypervisor, file, argc, argv); if (grub_errno == GRUB_ERR_NONE) diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h index bcd5a7eb186e..f38e695b1408 100644 --- a/include/grub/arm/linux.h +++ b/include/grub/arm/linux.h @@ -22,6 +22,8 @@ #include "system.h" +#include + #define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818 struct linux_arm_kernel_header { @@ -32,6 +34,9 @@ struct linux_arm_kernel_header { grub_uint32_t end; /* _edata */ grub_uint32_t reserved2[3]; grub_uint32_t hdr_offset; +#if defined GRUB_MACHINE_EFI + struct grub_pe_image_header pe_image_header; +#endif }; #if defined(__arm__) diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h index 7e22b4ab6990..3da71a51256f 100644 --- a/include/grub/arm64/linux.h +++ b/include/grub/arm64/linux.h @@ -20,6 +20,7 @@ #define GRUB_ARM64_LINUX_HEADER 1 #include +#include #define GRUB_LINUX_ARM64_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */ @@ -36,6 +37,7 @@ struct linux_arm64_kernel_header grub_uint64_t res4; /* reserved */ grub_uint32_t magic; /* Magic number, little endian, "ARM\x64" */ grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ + struct grub_pe_image_header pe_image_header; }; #if defined(__aarch64__) diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index eb2dfdfce9f8..e61272de5330 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -102,7 +102,9 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd, void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *); #include -grub_err_t grub_arch_efi_linux_check_image(struct linux_arch_kernel_header *lh); +#include +grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file, + struct linux_arch_kernel_header *lh); grub_err_t grub_arch_efi_linux_boot_image(grub_addr_t addr, grub_size_t size, char *args); #endif -- 2.35.1