From patchwork Thu Aug 18 08:55:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A160C32772 for ; Thu, 18 Aug 2022 08:55:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243348AbiHRIzx (ORCPT ); Thu, 18 Aug 2022 04:55:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243575AbiHRIzv (ORCPT ); Thu, 18 Aug 2022 04:55:51 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04CF6B07EF for ; Thu, 18 Aug 2022 01:55:51 -0700 (PDT) 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 85C566124A for ; Thu, 18 Aug 2022 08:55:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7574C433B5; Thu, 18 Aug 2022 08:55:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812949; bh=niZBhpkIdscPcN208ArKi+A6d3GsNSweCzX3nRlNVU0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s/jGf+iXDlaucr52n07TwBPBEYTwYtd6xTpfzVWAV4k/Ywc4lVOoMadj3bWKwWf31 XQAfOC8+RU6kybvtdRp9dgUwFBmtE5KyMCEmtn8x1BhEYnkJYMysgbcrwxGQdFImNI cLbfG4Ty/f+eLs2HrIJY/YH/s9iV0Psj64kHRYcg/AhI5XkPVgHgcEtOIOSI8zJlVj NlG6FzSbboyr1X4JaQsYz0cOc9OH7rcW7Rnghx1RUaeefPa/7DmyjOKeAqe0Lkqqn/ bTP3x9/KYV1DppbmOGBEjLnz4l4rvl0Awmxlr6Kz3XSwtszcmtVISaVPJZDDN5zahW N6N6GtfU4rzMQ== 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 1/9] loader: drop argv[] argument in grub_initrd_load() Date: Thu, 18 Aug 2022 10:55:31 +0200 Message-Id: <20220818085540.2075028-2-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=6961; i=ardb@kernel.org; h=from:subject; bh=wQFzLxMZkWavlzJH8Og0nbf4Zqw+CNHB0gdJJ0VYcRY=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f54vyc/5gAIH8tbljhw9fftrlbKH9aFI02A4l14 WVG0zQOJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+eAAKCRDDTyI5ktmPJN75C/ 9rBG8POkI63bZ1k///q43La/3UJVVoKG7MvhrA1rs3A7ZqDYRPSPR3XwbCD5GnYd85+4hq/So6kjzN 77Mi/MR5tCTabPV8tBSCdXEgkg6MLpKCxcfaAJEFxLHM878BOZFCBxlHEXD+ZaKe4aItOv0g1Bq297 O768a2ypEHSVuiF371N63sNAPQ3RrLnvEYBS+zKD9zXT+VxrSw+BVLb2PGux8IrUIejLINvun7Xqcb Ocih5yKgS+P2X3FtM3vEd0CTbwn3a6PB0gMLikCyUuDcnEuMlHk4B9UBPNfROXnnH2NmTulrJMtjAQ XozufayXHoeG5OWJ5jmIsUDC88v4JWImNS1k5yhiRNWC019f8FnRmVooV3L6Vs7R+N/99ZbJLgPpYs Ug8k18+A2WzLt1BkdGlcqvO/RYAcIlCQM5/zmI3eFp+IDogeT7uHKUeFwpuPrJRr4rGtafpxhL2LeY /h/Xbnp+qBTZyfPjYjewzUMxzDsmtGoWUI5T2X6uhJew8= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org From: Nikita Ermakov In the case of an error grub_initrd_load() uses argv[] to print the filename that caused the error. It is also possible to obtain the filename from the file handles and there is no need to duplicate that information in argv[], so let's drop it. Signed-off-by: Nikita Ermakov Signed-off-by: Ard Biesheuvel --- grub-core/loader/arm/linux.c | 2 +- grub-core/loader/arm64/linux.c | 2 +- grub-core/loader/i386/linux.c | 2 +- grub-core/loader/i386/pc/linux.c | 2 +- grub-core/loader/i386/xen.c | 3 +-- grub-core/loader/ia64/efi/linux.c | 2 +- grub-core/loader/linux.c | 4 ++-- grub-core/loader/mips/linux.c | 2 +- grub-core/loader/powerpc/ieee1275/linux.c | 2 +- grub-core/loader/sparc64/ieee1275/linux.c | 2 +- include/grub/linux.h | 2 +- 11 files changed, 12 insertions(+), 13 deletions(-) diff --git a/grub-core/loader/arm/linux.c b/grub-core/loader/arm/linux.c index 30b366601c39..f00b538ebad0 100644 --- a/grub-core/loader/arm/linux.c +++ b/grub-core/loader/arm/linux.c @@ -422,7 +422,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("loader", "Loading initrd to 0x%08x\n", (grub_addr_t) initrd_start); - if (grub_initrd_load (&initrd_ctx, argv, (void *) initrd_start)) + if (grub_initrd_load (&initrd_ctx, (void *) initrd_start)) goto fail; initrd_end = initrd_start + size; diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index ef3e9f9444ca..aed7a200b848 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -266,7 +266,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), goto fail; } - if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) + if (grub_initrd_load (&initrd_ctx, initrd_mem)) goto fail; initrd_start = (grub_addr_t) initrd_mem; diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c index c5984d4b27e9..edd6c2bb1d23 100644 --- a/grub-core/loader/i386/linux.c +++ b/grub-core/loader/i386/linux.c @@ -1107,7 +1107,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), initrd_mem_target = get_physical_target_address (ch); } - if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) + if (grub_initrd_load (&initrd_ctx, initrd_mem)) goto fail; grub_dprintf ("linux", "Initrd, addr=0x%x, size=0x%x\n", diff --git a/grub-core/loader/i386/pc/linux.c b/grub-core/loader/i386/pc/linux.c index 7b89d431051e..4adeee9ae001 100644 --- a/grub-core/loader/i386/pc/linux.c +++ b/grub-core/loader/i386/pc/linux.c @@ -462,7 +462,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), initrd_addr = get_physical_target_address (ch); } - if (grub_initrd_load (&initrd_ctx, argv, initrd_chunk)) + if (grub_initrd_load (&initrd_ctx, initrd_chunk)) goto fail; lh->ramdisk_image = initrd_addr; diff --git a/grub-core/loader/i386/xen.c b/grub-core/loader/i386/xen.c index cd24874ca324..3b856e842709 100644 --- a/grub-core/loader/i386/xen.c +++ b/grub-core/loader/i386/xen.c @@ -809,8 +809,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), if (err) goto fail; - if (grub_initrd_load (&initrd_ctx, argv, - get_virtual_current_address (ch))) + if (grub_initrd_load (&initrd_ctx, get_virtual_current_address (ch))) goto fail; } diff --git a/grub-core/loader/ia64/efi/linux.c b/grub-core/loader/ia64/efi/linux.c index 41266109e03c..fb9b961f71a2 100644 --- a/grub-core/loader/ia64/efi/linux.c +++ b/grub-core/loader/ia64/efi/linux.c @@ -563,7 +563,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("linux", "[addr=0x%lx, size=0x%lx]\n", (grub_uint64_t) initrd_mem, initrd_size); - if (grub_initrd_load (&initrd_ctx, argv, initrd_mem)) + if (grub_initrd_load (&initrd_ctx, initrd_mem)) goto fail; fail: grub_initrd_close (&initrd_ctx); diff --git a/grub-core/loader/linux.c b/grub-core/loader/linux.c index ade9b2fef470..830360172c3d 100644 --- a/grub-core/loader/linux.c +++ b/grub-core/loader/linux.c @@ -271,7 +271,7 @@ grub_initrd_close (struct grub_linux_initrd_context *initrd_ctx) grub_err_t grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx, - char *argv[], void *target) + void *target) { grub_uint8_t *ptr = target; int i; @@ -317,7 +317,7 @@ grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx, { if (!grub_errno) grub_error (GRUB_ERR_FILE_READ_ERROR, N_("premature end of file %s"), - argv[i]); + initrd_ctx->components[i].file->name); grub_initrd_close (initrd_ctx); return grub_errno; } diff --git a/grub-core/loader/mips/linux.c b/grub-core/loader/mips/linux.c index 2f912c61ccda..7264ba2b663b 100644 --- a/grub-core/loader/mips/linux.c +++ b/grub-core/loader/mips/linux.c @@ -452,7 +452,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), initrd_dest = get_physical_target_address (ch) | 0x80000000; } - if (grub_initrd_load (&initrd_ctx, argv, initrd_src)) + if (grub_initrd_load (&initrd_ctx, initrd_src)) goto fail; #ifdef GRUB_MACHINE_MIPS_QEMU_MIPS diff --git a/grub-core/loader/powerpc/ieee1275/linux.c b/grub-core/loader/powerpc/ieee1275/linux.c index 6fdd86313083..e6d071508d8d 100644 --- a/grub-core/loader/powerpc/ieee1275/linux.c +++ b/grub-core/loader/powerpc/ieee1275/linux.c @@ -349,7 +349,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("loader", "Loading initrd at 0x%x, size 0x%x\n", addr, size); - if (grub_initrd_load (&initrd_ctx, argv, (void *) addr)) + if (grub_initrd_load (&initrd_ctx, (void *) addr)) goto fail; initrd_addr = addr; diff --git a/grub-core/loader/sparc64/ieee1275/linux.c b/grub-core/loader/sparc64/ieee1275/linux.c index bb47ee0cc640..ac2206f3c051 100644 --- a/grub-core/loader/sparc64/ieee1275/linux.c +++ b/grub-core/loader/sparc64/ieee1275/linux.c @@ -413,7 +413,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), grub_dprintf ("loader", "Loading initrd at vaddr 0x%lx, paddr 0x%lx, size 0x%lx\n", addr, paddr, size); - if (grub_initrd_load (&initrd_ctx, argv, (void *) addr)) + if (grub_initrd_load (&initrd_ctx, (void *) addr)) goto fail; initrd_addr = addr; diff --git a/include/grub/linux.h b/include/grub/linux.h index 594a3f3079b0..a96ac20483d3 100644 --- a/include/grub/linux.h +++ b/include/grub/linux.h @@ -21,4 +21,4 @@ grub_initrd_close (struct grub_linux_initrd_context *initrd_ctx); grub_err_t grub_initrd_load (struct grub_linux_initrd_context *initrd_ctx, - char *argv[], void *target); + void *target); From patchwork Thu Aug 18 08:55:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598622 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD59DC00140 for ; Thu, 18 Aug 2022 08:55:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241348AbiHRIzz (ORCPT ); Thu, 18 Aug 2022 04:55:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235263AbiHRIzy (ORCPT ); Thu, 18 Aug 2022 04:55:54 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85DC4B07E9 for ; Thu, 18 Aug 2022 01:55:53 -0700 (PDT) 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 2B9D061303 for ; 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 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org 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]; From patchwork Thu Aug 18 08:55:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598284 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8EADC00140 for ; Thu, 18 Aug 2022 08:55:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243489AbiHRIz7 (ORCPT ); Thu, 18 Aug 2022 04:55:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235263AbiHRIz6 (ORCPT ); Thu, 18 Aug 2022 04:55:58 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6C80985AD for ; Thu, 18 Aug 2022 01:55:57 -0700 (PDT) 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 8954CB8213D for ; Thu, 18 Aug 2022 08:55:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03D2DC433C1; Thu, 18 Aug 2022 08:55:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812955; bh=UUvKXR2cO27YDKET1AyG/i9gzYw04CdLto0thocEz2Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eZ3+lreY6dvpCx5DNQhI9brjoeeNla8/nRGqDU9UG+xKbVMdmoq7lgGU9JI1nJP3+ YURN7Wgs+A4qn58Pi+NbJdAglfTwWEMa+0db8JEct+53GwqhuC8NeWUt6RjT+mcb0D jLiKePwk35+w6JPse7DYfwo3ttn24NKJ++n4XhHRWR1r/kHRP/XrRQ63uvaMpAW4uD P1F+/ctW4dNzV+jlv8sHlHOWEd7JKKNzP/GIgw9KGheNIXBQMAPJoWrLSaYpN+Cpk1 SDsM/1Lg4x2asqITaqvrTQZ+6KlAZIzruuL+RaN46FHqugrb0mJCsQdia/GhPfV5GG Lm5u45yo1dBDg== 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 3/9] arm64/linux: Remove magic number header field check Date: Thu, 18 Aug 2022 10:55:33 +0200 Message-Id: <20220818085540.2075028-4-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=2374; i=ardb@kernel.org; h=from:subject; bh=UUvKXR2cO27YDKET1AyG/i9gzYw04CdLto0thocEz2Y=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f59S2E/1h05DNgErqGpeX5fGPw91TcV1H2M0xeP qWzAX1OJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+fQAKCRDDTyI5ktmPJGuIC/ 48Idyt81w/P+gZvj7mywXsJSt2MW0nB7uxRn0sr6qZoegCyZWFJ0zejv4QIQjevXqM/tTI5byTqZUF wNPAdrsqcvDj9Ibd7tOrXuFTZZStPMdyd1ZuMSIuEEb5VtJe1Sxd9O+H9BiAhch9PR6SN99LtgdMu8 fCBFpoh5K44L0XBDDTtO0wnA1V4Lq9n0Kzb1o4r9KEL9IS1nB9RtEd02i8BLQyt3Ntbz0sqMIdmdYW kMGfNuYXjT5uC0ssvt4G331s5lB7/GRoSqjuCHEr5mtCe+DEJ1GGC66Ed06w0WTVIXizNfwDKQq9Ni 7bp0HLGpGXHGabpjfP79oJqhrqyZabjGlFHSUa3S3a/DwFxaNJkmoUL0g3/3SmjJcKP59/9IVfIxjb XH+Vun9iOXCTUhL2ELVpMWydSEdtOJEcpMhb6mnkdy/iZxe4ORB3MwFyoFIbF9mrUrWtG98rxHruma hc0n09nc5Hs0SSuDJYqO8p86KEydddWp7Fg7IwtU28HTY= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org The 'ARM\x64' magic number in the file header identifies an image as one that implements the bare metal boot protocol, allowing the loader to simply move the file to a suitably aligned address in memory, with sufficient headroom for the trailing .bss segment (the required memory size is described in the header as well). Note of this matters for GRUB, as it only supports EFI boot. EFI does not care about this magic number, and nor should GRUB: this prevents us from booting other PE linux images, such as the generic EFI zboot decompressor, which is a pure PE/COFF image, and does not implement the bare metal boot protocol. So drop the magic number check. Signed-off-by: Ard Biesheuvel --- grub-core/loader/arm64/linux.c | 3 --- include/grub/arm/linux.h | 1 - include/grub/arm64/linux.h | 1 - 3 files changed, 5 deletions(-) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index aed7a200b848..b5b559c236e0 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -51,9 +51,6 @@ static grub_addr_t initrd_end; grub_err_t grub_arch_efi_linux_check_image (struct linux_arch_kernel_header * lh) { - if (lh->magic != GRUB_LINUX_ARMXX_MAGIC_SIGNATURE) - return grub_error(GRUB_ERR_BAD_OS, "invalid magic number"); - 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")); diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h index bcd5a7eb186e..bfab334dd87f 100644 --- a/include/grub/arm/linux.h +++ b/include/grub/arm/linux.h @@ -35,7 +35,6 @@ struct linux_arm_kernel_header { }; #if defined(__arm__) -# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE # define linux_arch_kernel_header linux_arm_kernel_header #endif diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h index 7e22b4ab6990..96f1494e05a2 100644 --- a/include/grub/arm64/linux.h +++ b/include/grub/arm64/linux.h @@ -39,7 +39,6 @@ struct linux_arm64_kernel_header }; #if defined(__aarch64__) -# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE # define linux_arch_kernel_header linux_arm64_kernel_header #endif From patchwork Thu Aug 18 08:55:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12F92C32772 for ; Thu, 18 Aug 2022 08:56:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243513AbiHRI4B (ORCPT ); Thu, 18 Aug 2022 04:56:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235263AbiHRIz7 (ORCPT ); Thu, 18 Aug 2022 04:55:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD90EB07DA for ; Thu, 18 Aug 2022 01:55:58 -0700 (PDT) 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 6AECE6122E for ; Thu, 18 Aug 2022 08:55:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1538C433B5; Thu, 18 Aug 2022 08:55:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812957; bh=J6A6dWdEir0BbOuEt8vCg3aRhfPyn7L5Yj1djqNZlsA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KjCuPZf4J5/nnlemCbYp8mgMUz8uD+EV5HYL5aK+43M1wsQ0lipqnA0YIDPZjuxxS pgJt/KaarhH2NzR5jzo1LAiSa1FNS3QvQzP2n2vqICAd4TK10hEepw7zT1T7oceZJL CVdZutq2UZwz2pukIr6ikgtHKigX6eo9mbvoSlPlOxEbUTERK6A95wVv0nlrgwYoHV rtdb6kjxwuFJc0RKrSpKE38pwhAoeR3ViTZ7phbSwSOmv1TxIM4ZFEUQ/srQ30yqPT I94btamKNWEvbu+bCcGTdOgmMHcliWI0nZIUIgzZzb5a83w40dkYIhAXTlpolkC4jR f7It0EvozG3Og== 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 4/9] linux/arm: unify ARM/arm64 vs Xen PE/COFF header handling Date: Thu, 18 Aug 2022 10:55:34 +0200 Message-Id: <20220818085540.2075028-5-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=6213; i=ardb@kernel.org; h=from:subject; bh=J6A6dWdEir0BbOuEt8vCg3aRhfPyn7L5Yj1djqNZlsA=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f5/oTrcSD5ck8E6gpLDjyV5xGj4YdLIU0Fu32o3 QlY1JCiJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+fwAKCRDDTyI5ktmPJM4tC/ oDdU9LA9WkkPVjZPNFNF1I+luM7QhEq+k6nSZoDzfD32pXV7L9aZefPT1hh8xldREmF7vv9H/cg2N5 aWF1y2B1AAWev0IXGFdWsee0svEEkBXGfjOwEpbPhO/tsNOl0ES0qnpIc09HycQCdr5kaXEemedgl5 tfn8Ey/FsaF3yEH06s+O/FagOC7o97d1+NDLJSUluiNCgnUmbCmbTnTKCpOSju6S3maAsIjD6c05XS zuZHKYNG13BUtcoaHs4Xa2wp4s7xYz7/q1NcjQ44coEs+CCEckBjl/UrRcy1o1O0QPV5z1NEOqQoH3 O5/KnXOpPyqh4jXm6a8adxtY2vbk0DZD2Qu4RsK9M8KrnSowS7mlOQGeH+6+PbvM30WWDE03OUx69c NMdon4AXJ2O+pkboY6czOjwjFQvUv5L+K5Yqmr9WdqSQRyfqR1zDp76WHoM25JoFGbl3ovoYTI8OkN au0lqvNg+IIbe533IhcFYmiIy2KBmdMMoNbqzPEl4HPKk= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org 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. 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 | 6 +++++ include/grub/arm64/linux.h | 4 ++++ include/grub/efi/efi.h | 4 +++- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index b5b559c236e0..7c0f17cf933d 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)) < (long) 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..e5895ee78218 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.coff_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 bfab334dd87f..f2a2c0379795 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,10 @@ 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_coff_image_header coff_image_header; +#endif }; #if defined(__arm__) diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h index 96f1494e05a2..e00bbcfa2ff2 100644 --- a/include/grub/arm64/linux.h +++ b/include/grub/arm64/linux.h @@ -21,6 +21,8 @@ #include +#include + #define GRUB_LINUX_ARM64_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */ /* From linux/Documentation/arm64/booting.txt */ @@ -36,6 +38,8 @@ 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_coff_image_header coff_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 From patchwork Thu Aug 18 08:55:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598283 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B691FC00140 for ; Thu, 18 Aug 2022 08:56:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243575AbiHRI4E (ORCPT ); Thu, 18 Aug 2022 04:56:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243498AbiHRI4D (ORCPT ); Thu, 18 Aug 2022 04:56:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE1AB50703 for ; Thu, 18 Aug 2022 01:56:01 -0700 (PDT) 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 110A261237 for ; Thu, 18 Aug 2022 08:56:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4781DC433C1; Thu, 18 Aug 2022 08:55:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812960; bh=a+eFr/WWVWSZD8tqmOLBJll8Nv1OKzhzVxe2gHJjaUY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IaZmpsY3ST2AXJ47TQ22gRS1OfX8nJ5xh3WwOsWAt+KVf5QIV6iazGGAQrsBbU32K 4eyCmddvUCy7vHj2E1r6/OkXRSgDV7SkAWs9yMMR/WGsCHoYWqkGeEYnZhoh0k/h3k zzOGDmJdLHvm8Hu4vMJFZz9tcGMEuGnCi0HNg+3YyOIrspqib601hEmPCKHT3wHHMw jFQgHw+acpv6409i4laJBcMG40mB0C6XIZNbylg4eN3D3i35zgO6Gltz8JB6oD1+jN qcXKG+UX5bkG+2B0xj+pmKX+Ma+G4B+FiEUthsc9wl/y3iWIu1IsiojnLB7C0/m8J+ mRWSgIPYLsg8g== 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 5/9] linux/arm: account for COFF headers appearing at unexpected offsets Date: Thu, 18 Aug 2022 10:55:35 +0200 Message-Id: <20220818085540.2075028-6-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=1728; i=ardb@kernel.org; h=from:subject; bh=a+eFr/WWVWSZD8tqmOLBJll8Nv1OKzhzVxe2gHJjaUY=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f6BfuSyB/c1hJQvT4WLgOeT3CQgUVdcUMdP0I4U 6vQwkCWJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+gQAKCRDDTyI5ktmPJHTZDA COKTzz8eiwQda7wzX7CEGwq1qjPrYtILKOH2IKixjq15gbO287HgaCNVdYfH+zN3CAQB3nAGoA6sR7 vzlXzxLrhB6WicLirVyxS9k6in6K0RtUqBSZcmYhBuJMj0HScMEn+/G2Vf29plHyYrl31/6hJP9TnD wBP4vbWZtfpiCMn9DJLq63vL4dLxwkLN5VUjT8eZ5zNywogzc6Fe2/zTTUjNPIReK66XF5vuSI7fB0 8vlnlRqrSMewSmisEi2D1SVO355x3kYOW6fotRk2gsCQ35naUnDVzlW04mLyE6uI0/DHXlvn0fv2hS vD+khlodNoDEAN9r7HAU3iXeycR138PouwfwJpS82znkehHV5mNXZcGoDb3ftrVa6f476FyMF1hVwh ACDzYc/qKELufH/XH66bAV9kHH46H0xszWJBtF0Rzlq+07VkO6eRTQfIUzxtO9eCRMbzXqan6IbHyU la3msqCx2JlDHezocBBzUbdrdH3MhzRUuGrOwIGSF9uEs= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org The way we load the Linux and PE/COFF image headers depends on a fixed placement of the COFF header at offset 0x40 into the file. This is a reasonable default, given that this is where Linux emits it today. However, in order to comply with the PE/COFF spec, which allows this header to appear anywhere in the file, let's ensure that we read the header from where it actually appears in the file if it is not located at offset 0x40. Signed-off-by: Ard Biesheuvel --- grub-core/loader/arm64/linux.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 7c0f17cf933d..56ba8d0a6ea3 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -63,6 +63,21 @@ grub_arch_efi_linux_load_image_header (grub_file_t file, grub_dprintf ("linux", "UEFI stub kernel:\n"); grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); + /* + * The PE/COFF spec permits the COFF header to appear anywhere in the file, so + * we need to double check whether it was where we expected it, and if not, we + * must load it from the correct offset into the coff_image_header field of + * struct linux_arch_kernel_header. + */ + if ((grub_uint8_t *) lh + lh->hdr_offset != (grub_uint8_t *) &lh->coff_image_header) + { + grub_file_seek (file, lh->hdr_offset); + + if (grub_file_read (file, &lh->coff_image_header, sizeof(struct grub_coff_image_header)) + != sizeof(struct grub_coff_image_header)) + return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header"); + } + return GRUB_ERR_NONE; } From patchwork Thu Aug 18 08:55:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598620 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC5D6C00140 for ; Thu, 18 Aug 2022 08:56:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243498AbiHRI4H (ORCPT ); Thu, 18 Aug 2022 04:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235263AbiHRI4H (ORCPT ); Thu, 18 Aug 2022 04:56:07 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE5F0985AD for ; Thu, 18 Aug 2022 01:56:05 -0700 (PDT) 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 99434B82140 for ; Thu, 18 Aug 2022 08:56:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2575C433D6; Thu, 18 Aug 2022 08:56:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812963; bh=5nfyW/WvXmrskb2t/kxpxzuMQeCiRSixX3YDgubLvQE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fLWV6iD9L01rPiTq0TLBiwT9khqG9ZkTchUTKUvbVOXU4CGAmem7rZRNgr9oHRSoI 0e1SB7IlEPufWWF8uB8BIO+eKIYM8pFCTSsfwviTrtwGzpQ1X27aKwcMOC+X47sjw4 3cFNAM6Lk7cEL1UMFj1h0eFqCYhT4u8aF3pvqiyYbAlJxzNjmYM1mqlRLArYrVYdxt mkBcNNdSXZfvUweNWyBLQhRQv9Xf6IDE1w06rrmHLlQEVXgbV4xqhEGyH013b7tSYk VcFIIiZEdsoXP1q9N8gpwktUtwbOs45FMa/8BNhmwEwk2M7x5CKy4/quu4XQLAzr+V GOqmOB99ZNuFA== 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 , Heinrich Schuchardt Subject: [PATCH resend 6/9] efi: add definition of LoadFile2 protocol Date: Thu, 18 Aug 2022 10:55:36 +0200 Message-Id: <20220818085540.2075028-7-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=2286; i=ardb@kernel.org; h=from:subject; bh=5nfyW/WvXmrskb2t/kxpxzuMQeCiRSixX3YDgubLvQE=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f6DAPw2MoB/uTn5i+6CZT2mvdSU/hfwn1hRWdMn VCq6arSJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+gwAKCRDDTyI5ktmPJOaxC/ 99hIBX0vA48YqcGvCa6tmcmv7ikPSukgHEbnxN5ANQgiAwWoqmjzXLR29f1jiEnZAfAcpkJMUZlGvR Ctg5oZRt0mKJ7YErJXe8ojUU44T3kBoRjBFaW/WuvUv068Ls+Q3muNmH5Hy2QgJHt7trMFM39os68j xxUbHotHQzwAxk+lDIDIvQR9hhwEQtmVayPLPgFEVaBzA3sNZ2B5qj4ODAvm5Jy9H92ZGKWCwkRAiZ qvfyI6Nu6wpI3aqvNeV5eaukwP/Zug8LtaO2ninqoTh0ROFqw78+8y1E/WEF4diVfBXeyEax8nVZlf 4YI/yQumHDxt7Nzvg74gAll2LQdTQHtFdIz2qhXjEB0eqAaFqh1RFuMkj93N1xiTKDk0qxVbLJYlgs uw3B2mCsRUMji9YovXeAcLnqGKEgu658ONFC4zD2Rwun22sJGROE5TPBtdBlW5BrxlZyHaAke8XjVn 3GwixN3Vx6XnfvBkSwZWwwaW2Xtk7ZxXIZ1jQ8z2Q7dZI= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Incorporate the EFI_LOAD_FILE2_PROTOCOL GUID and C types from the UEFI spec. Reviewed-by: Heinrich Schuchardt Signed-off-by: Ard Biesheuvel --- grub-core/commands/efi/lsefi.c | 1 + include/grub/efi/api.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/grub-core/commands/efi/lsefi.c b/grub-core/commands/efi/lsefi.c index f46ba3b49384..c304d25ccdd6 100644 --- a/grub-core/commands/efi/lsefi.c +++ b/grub-core/commands/efi/lsefi.c @@ -55,6 +55,7 @@ struct known_protocol { GRUB_EFI_ABSOLUTE_POINTER_PROTOCOL_GUID, "absolute pointer" }, { GRUB_EFI_DRIVER_BINDING_PROTOCOL_GUID, "EFI driver binding" }, { GRUB_EFI_LOAD_FILE_PROTOCOL_GUID, "load file" }, + { GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID, "load file2" }, { GRUB_EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID, "simple FS" }, { GRUB_EFI_TAPE_IO_PROTOCOL_GUID, "tape I/O" }, { GRUB_EFI_UNICODE_COLLATION_PROTOCOL_GUID, "unicode collation" }, diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h index 1ef4046225cb..1077826a3ca1 100644 --- a/include/grub/efi/api.h +++ b/include/grub/efi/api.h @@ -149,6 +149,11 @@ { 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B } \ } +#define GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID \ + { 0x4006c0c1, 0xfcb3, 0x403e, \ + { 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d } \ + } + #define GRUB_EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID \ { 0x0964e5b22, 0x6459, 0x11d2, \ { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \ @@ -1749,6 +1754,16 @@ struct grub_efi_rng_protocol }; typedef struct grub_efi_rng_protocol grub_efi_rng_protocol_t; +struct grub_efi_load_file2 +{ + grub_efi_status_t (*load_file)(struct grub_efi_load_file2 *this, + grub_efi_device_path_t *file_path, + grub_efi_boolean_t boot_policy, + grub_efi_uintn_t *buffer_size, + void *buffer); +}; +typedef struct grub_efi_load_file2 grub_efi_load_file2_t; + #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \ || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__) \ || defined(__riscv) From patchwork Thu Aug 18 08:55:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598282 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4A68C00140 for ; Thu, 18 Aug 2022 08:56:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243538AbiHRI4K (ORCPT ); Thu, 18 Aug 2022 04:56:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243512AbiHRI4J (ORCPT ); Thu, 18 Aug 2022 04:56:09 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 659D8985AD for ; Thu, 18 Aug 2022 01:56:08 -0700 (PDT) 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 2409EB82140 for ; Thu, 18 Aug 2022 08:56:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3A87C433D7; Thu, 18 Aug 2022 08:56:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812965; bh=HJxKuoLDX+gus1QsZw0e5C3ucrkSADKgGdBh7ekO2I0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PPhn1ir9n3IUvtOpEdnoYPDnockJQVEbXE2cWaqc2RiuA9m0c8BvyDc331nObD56/ lwxuGU+Rq85uTL/7q6YWgYFbipWfbK2+mAgSURHpFCjCan2cxESB+ZjF3Fut/5qJNd snPy2vlAKCSV5kIZVhclU3nnYOAOrehAxZZvf0OF9XKk9V2fH5Mhpc/LuWqcN7E0Gy uDXdYR1n/mN08abHu/l+tMqvXl/MOFrltKxAZl9q4b+vBjOazr5g20su3YFsy8XvA9 vds6/BPGxAoe8ITp4ZPM2s2A7/3rAb4WlKsob5se/mUu0/SO+vMvE+AwQdP4fFkRcE PuwXH0h3XKd8g== 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 7/9] efi/efinet: Don't close connections at fini_hw() time Date: Thu, 18 Aug 2022 10:55:37 +0200 Message-Id: <20220818085540.2075028-8-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=3263; i=ardb@kernel.org; h=from:subject; bh=HJxKuoLDX+gus1QsZw0e5C3ucrkSADKgGdBh7ekO2I0=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f6FMJQNAk7wnyHw01tA11+4wUmXwt63+lKRx7AO iHW6zJ+JAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+hQAKCRDDTyI5ktmPJNk+C/ 9PVgYLFVaLhpPmXYa9PjoKNArXeCQHde4X4iP1dI8U+oJCqB17M3zbXy7Y3QAR4eMVuSIlC1vmrMJW IUFCfdFa3BAR5ZFnPwZrNwbgkoEWpAkdr9anbp2SfRrNdxuSGB5Niv8dBBa3qYFb7ZwuQoYa6y48x6 FKMf5on3bKk4weGdLS2PFEd2phUqa0nT5b2JmiiYMsrwT4lvUEgDEYdLZ/XDJaWZn/J9kpnZG/YGN3 1PX+w0LHb/J5YX81XQjd2kLyrWn6Lb7vYatZ7KzT/QbB2hLlCHpVWsaDb1OGDQxqbmwrRxaLghAJzc Othj6S9rqj9mvEyORv5v5kYSAOOtIkZoDpqczcrd3kKmQYkfPSfP5HpReEXVtb0CenXriScRQfsd0n ktr4j47e2HSwtZ0lc1MvnCsyYJ1olbMx2lIk9w65MIHQ/Ln7+Ned+el7F+kfdVGz4scxnTa2NttKww aId2BY65kEbk9udcSJpU8phWnj2GvpGdZLrTkmPocwAM8= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org When GRUB runs on top of EFI firmware, it only has access to block and network device abstractions exposed by the firmware, and it is up to the firmware to quiesce the underlying hardware when handing over to the OS. This is especially important for network devices, to prevent incoming packets from being DMA'd straight into memory after the OS has taken over but before it has managed to reconfigure the network hardware. GRUB handles this by means of the grub_net_fini_hw() preboot hook, which is executed before calling into the booted image. This means that all network devices disappear or become inoperable before the EFI stub executes on EFI targeted builds. This is problematic as it prevents the EFI stub from calling back into GRUB provided protocols such as LoadFile2 for the initrd, which we will provide in a subsequent patch. So add a flag that indicates to the network core that EFI network devices should not be closed when grub_net_fini_hw() is called. Signed-off-by: Ard Biesheuvel --- grub-core/net/drivers/efi/efinet.c | 10 +++++++++- grub-core/net/net.c | 2 +- include/grub/net.h | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c index 73343d26d9e1..5adf5f40f492 100644 --- a/grub-core/net/drivers/efi/efinet.c +++ b/grub-core/net/drivers/efi/efinet.c @@ -320,7 +320,15 @@ grub_efinet_findcards (void) card->name = grub_xasprintf ("efinet%d", i++); card->driver = &efidriver; - card->flags = 0; + /* + * EFI network devices are abstract SNP protocol instances, and the + * firmware is in charge of ensuring that they will be torn down when the + * OS loader hands off to the OS proper. Closing them as part of the + * preboot cleanup is therefore unnecessary, and undesirable, as it + * prevents us from using the network connection in a protocal callback + * such as LoadFile2 for initrd loading. + */ + card->flags = GRUB_NET_CARD_NO_CLOSE_ON_FINI_HW; card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET; grub_memcpy (card->default_address.mac, net->mode->current_address, diff --git a/grub-core/net/net.c b/grub-core/net/net.c index 064e7114e012..7046dc57890a 100644 --- a/grub-core/net/net.c +++ b/grub-core/net/net.c @@ -1787,7 +1787,7 @@ grub_net_fini_hw (int noreturn __attribute__ ((unused))) { struct grub_net_card *card; FOR_NET_CARDS (card) - if (card->opened) + if (card->opened && !(card->flags & GRUB_NET_CARD_NO_CLOSE_ON_FINI_HW)) { if (card->driver->close) card->driver->close (card); diff --git a/include/grub/net.h b/include/grub/net.h index a64a04cc80b1..79cba357ae6a 100644 --- a/include/grub/net.h +++ b/include/grub/net.h @@ -64,7 +64,8 @@ typedef enum grub_net_interface_flags typedef enum grub_net_card_flags { GRUB_NET_CARD_HWADDRESS_IMMUTABLE = 1, - GRUB_NET_CARD_NO_MANUAL_INTERFACES = 2 + GRUB_NET_CARD_NO_MANUAL_INTERFACES = 2, + GRUB_NET_CARD_NO_CLOSE_ON_FINI_HW = 4 } grub_net_card_flags_t; struct grub_net_card; From patchwork Thu Aug 18 08:55:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598619 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F718C28B2B for ; Thu, 18 Aug 2022 08:56:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235263AbiHRI4M (ORCPT ); Thu, 18 Aug 2022 04:56:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243591AbiHRI4K (ORCPT ); Thu, 18 Aug 2022 04:56:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E7B5B07F6 for ; Thu, 18 Aug 2022 01:56:09 -0700 (PDT) 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 25D7F61313 for ; Thu, 18 Aug 2022 08:56:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5AE8FC43140; Thu, 18 Aug 2022 08:56:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812968; bh=E4aJPGS29hqrXtOm2Rk8yZNBA1ShxPq6XnAQv0Yzdgo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pGjOnN8PI/DBiZRpR0DFQSN3Tc6TFo8n7RiLBcsksYTKIlDsSKQHdq9mQweHetldU BgPYEOdv5Y8gidQS/J+Yq0K/XLdh0LR6mOLrB0dLy4rMl/jugvGiVJtmI26+6VLVDg m8wH7YNeBosKbcfwwIfhioeVHLpQKUQpyTsvg1Oj6p0I6zpNZf4Dw68WqCOwZE+Moy WqbGDyFFkWYnzOtPyxIFtN8KFNxiinytVXOEdiRujeNoaCVVGb3Jy164SaKZ8v7WIB U0uB7s/yEosGWYivVrOBWMfq3fbzGSgZ0q2RETPFGY8ab9NshbbacVPRximJzxFpMk S7qZsjNmMwcyQ== 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 8/9] efi: implement LoadFile2 initrd loading protocol for Linux Date: Thu, 18 Aug 2022 10:55:38 +0200 Message-Id: <20220818085540.2075028-9-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=7202; i=ardb@kernel.org; h=from:subject; bh=E4aJPGS29hqrXtOm2Rk8yZNBA1ShxPq6XnAQv0Yzdgo=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f6Hzl0gppHvBOfmhXqtA5gFYm+ewwTuCZwrrFvz zuPY55SJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+hwAKCRDDTyI5ktmPJHIRC/ 0Zdod2xED5yDq3AmetnuExsEacDpDuIlX1tgQoJjIVUehQeCMopwYHl5SgopAk7ep2u9iAqpSGPQOb D04IQOEPe5FQdkt1XpvwS4Vu0fkc5HW4GgMWfW5irXSoBlD7tDhpZKYwFLDhwKQJGQzmnX8d8i3Wct 1pS+xMIV82alG0c9Xwtz6KOjcwyHtDEk8B92c6AWh2W9xVdDq1MGMcNyxf0pEZvq1DHTB4ZN5dQfAT swhv7Nc4zf5ky2e0EChG50WW05u+HnbUouC0ES2R623u5173YWIjZZ9eq+zsuFDF6x71JdpXzmb6Og K+5ti/HaVwa32eF5i1b4JsHA62NTmgj18BDRas3939yUCCYnmxdGLCZ2HIHoZOWMa/jxn1s9tsRI9W k2AOMLAMwioKTOocL5bREv+OCFw0b9lP81NHQVKdFBjrJNE7qFmjzn6KBNXqzRb4rKl4dp/srwnX5v HsY155z4pgqNrYpR5OS0t1qTJLpo6wH6TpsAfNMca/9uA= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Recent Linux kernels will invoke the LoadFile2 protocol installed on a well-known vendor media path to load the initrd if it is exposed by the firmware. Using this method is preferred for two reasons: - the Linux kernel is in charge of allocating the memory, and so it can implement any placement policy it wants (given that these tend to change between kernel versions), - it is no longer necessary to modify the device tree provided by the firmware. So let's install this protocol when handling the 'initrd' command if such a recent kernel was detected (based on the PE/COFF image version), and defer loading the initrd contents until the point where the kernel invokes the LoadFile2 protocol. Signed-off-by: Ard Biesheuvel Tested-by: Ilias Apalodimas Reviewed-by: Ilias Apalodimas --- grub-core/loader/arm64/linux.c | 129 +++++++++++++++++++- 1 file changed, 128 insertions(+), 1 deletion(-) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 56ba8d0a6ea3..82c7558b4c4c 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -48,6 +48,48 @@ static grub_uint32_t cmdline_size; static grub_addr_t initrd_start; static grub_addr_t initrd_end; +static grub_efi_guid_t load_file2_guid = GRUB_EFI_LOAD_FILE2_PROTOCOL_GUID; +static grub_efi_guid_t device_path_guid = GRUB_EFI_DEVICE_PATH_GUID; +static struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; +static grub_efi_handle_t initrd_lf2_handle; +static int initrd_use_loadfile2; + +struct initrd_media_device_path { + grub_efi_vendor_media_device_path_t vendor; + grub_efi_device_path_t end; +} GRUB_PACKED; + +#define LINUX_EFI_INITRD_MEDIA_GUID \ + { 0x5568e427, 0x68fc, 0x4f3d, \ + { 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68 } \ + } + +static struct initrd_media_device_path initrd_lf2_device_path = { + { + { + GRUB_EFI_MEDIA_DEVICE_PATH_TYPE, + GRUB_EFI_VENDOR_MEDIA_DEVICE_PATH_SUBTYPE, + sizeof(grub_efi_vendor_media_device_path_t), + }, + LINUX_EFI_INITRD_MEDIA_GUID + }, { + GRUB_EFI_END_DEVICE_PATH_TYPE, + GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE, + sizeof(grub_efi_device_path_t) + } +}; + +static grub_efi_status_t +grub_efi_initrd_load_file2(grub_efi_load_file2_t *this, + grub_efi_device_path_t *device_path, + grub_efi_boolean_t boot_policy, + grub_efi_uintn_t *buffer_size, + void *buffer); + +static grub_efi_load_file2_t initrd_lf2 = { + grub_efi_initrd_load_file2 +}; + grub_err_t grub_arch_efi_linux_load_image_header (grub_file_t file, struct linux_arch_kernel_header * lh) @@ -78,6 +120,18 @@ grub_arch_efi_linux_load_image_header (grub_file_t file, return grub_error(GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header"); } + /* + * Linux kernels built for any architecture are guaranteed to support the + * LoadFile2 based initrd loading protocol if the image version is >= 1. + */ + if (lh->coff_image_header.optional_header.major_image_version >= 1) + initrd_use_loadfile2 = 1; + else + initrd_use_loadfile2 = 0; + + grub_dprintf ("linux", "LoadFile2 initrd loading %sabled\n", + initrd_use_loadfile2 ? "en" : "dis"); + return GRUB_ERR_NONE; } @@ -197,6 +251,8 @@ grub_linux_boot (void) static grub_err_t grub_linux_unload (void) { + grub_efi_boot_services_t *b; + grub_dl_unref (my_mod); loaded = 0; if (initrd_start) @@ -208,6 +264,19 @@ grub_linux_unload (void) grub_efi_free_pages ((grub_addr_t) kernel_addr, GRUB_EFI_BYTES_TO_PAGES (kernel_size)); grub_fdt_unload (); + + if (initrd_lf2_handle) + { + b = grub_efi_system_table->boot_services; + b->uninstall_multiple_protocol_interfaces (initrd_lf2_handle, + &load_file2_guid, + &initrd_lf2, + &device_path_guid, + &initrd_lf2_device_path, + NULL); + initrd_lf2_handle = NULL; + initrd_use_loadfile2 = 0; + } return GRUB_ERR_NONE; } @@ -247,13 +316,50 @@ allocate_initrd_mem (int initrd_pages) GRUB_EFI_LOADER_DATA); } +static grub_efi_status_t +grub_efi_initrd_load_file2(grub_efi_load_file2_t *this, + grub_efi_device_path_t *device_path, + grub_efi_boolean_t boot_policy, + grub_efi_uintn_t *buffer_size, + void *buffer) +{ + grub_efi_status_t status = GRUB_EFI_SUCCESS; + grub_efi_uintn_t initrd_size; + + if (!this || this != &initrd_lf2 || !buffer_size) + return GRUB_EFI_INVALID_PARAMETER; + + if (device_path->type != GRUB_EFI_END_DEVICE_PATH_TYPE || + device_path->subtype != GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE) + return GRUB_EFI_NOT_FOUND; + + if (boot_policy) + return GRUB_EFI_UNSUPPORTED; + + initrd_size = grub_get_initrd_size (&initrd_ctx); + if (!buffer || *buffer_size < initrd_size) + { + *buffer_size = initrd_size; + return GRUB_EFI_BUFFER_TOO_SMALL; + } + + grub_dprintf ("linux", "Providing initrd via LOAD_FILE2_PROTOCOL\n"); + + if (grub_initrd_load (&initrd_ctx, buffer)) + status = GRUB_EFI_LOAD_ERROR; + + grub_initrd_close (&initrd_ctx); + return status; +} + static grub_err_t grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { - struct grub_linux_initrd_context initrd_ctx = { 0, 0, 0 }; int initrd_size, initrd_pages; void *initrd_mem = NULL; + grub_efi_boot_services_t *b; + grub_efi_status_t status; if (argc == 0) { @@ -271,6 +377,27 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), if (grub_initrd_init (argc, argv, &initrd_ctx)) goto fail; + if (initrd_use_loadfile2) + { + if (!initrd_lf2_handle) + { + b = grub_efi_system_table->boot_services; + status = b->install_multiple_protocol_interfaces (&initrd_lf2_handle, + &load_file2_guid, + &initrd_lf2, + &device_path_guid, + &initrd_lf2_device_path, + NULL); + if (status == GRUB_EFI_OUT_OF_RESOURCES) + { + grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory")); + return grub_errno; + } + } + grub_dprintf ("linux", "Using LoadFile2 initrd loading protocol\n"); + return GRUB_ERR_NONE; + } + initrd_size = grub_get_initrd_size (&initrd_ctx); grub_dprintf ("linux", "Loading initrd\n"); From patchwork Thu Aug 18 08:55:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 598281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C893C00140 for ; Thu, 18 Aug 2022 08:56:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243588AbiHRI4P (ORCPT ); Thu, 18 Aug 2022 04:56:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239820AbiHRI4O (ORCPT ); Thu, 18 Aug 2022 04:56:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF08FB07F0 for ; Thu, 18 Aug 2022 01:56:13 -0700 (PDT) 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 9222EB81DEB for ; Thu, 18 Aug 2022 08:56:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01D8DC433D6; Thu, 18 Aug 2022 08:56:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660812971; bh=qcwQefREXPmKgrFWHMrnNH4iddUEkHLg9MKUfINhGnE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KK2Y3Li9gbm+9FqhUtcJAlaPGbz5TxGqxWQRRIvAVsAZESubAB261L0UAnMBBb1g2 V7eTkTK26VXW2ktelR8IAVGARC0pz8JMfRDOBRswuofottahq89R8FNnlfaBRqhhbz vUU9fMnyb3u32m0/1f+/rFusfAbZvr+uq4Ab/5arrT6kqXdviq923BCyyU2aU5XxNO oXxAiOMIWD+UOx7CqmVQL7gTIn/mPw4lvArrnHnSM9T+87q80cuqwVSvahYPus/Dsk Zx+zmqmFXaxMQUMRBZFa+Z+gPs+OlrLkueJiiN/PVliPG/Wk5ed82uDdjEVpXVDvWG U7FzLQs2X0kHw== 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 , Leif Lindholm Subject: [PATCH resend 9/9] linux: ignore FDT unless we need to modify it Date: Thu, 18 Aug 2022 10:55:39 +0200 Message-Id: <20220818085540.2075028-10-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=2654; i=ardb@kernel.org; h=from:subject; bh=qcwQefREXPmKgrFWHMrnNH4iddUEkHLg9MKUfINhGnE=; b=owEB7QES/pANAwAKAcNPIjmS2Y8kAcsmYgBi/f6KK1CRae61+/Iolc7eh6UzH7SGMFIoyBvop4T/ a2gB3+OJAbMEAAEKAB0WIQT72WJ8QGnJQhU3VynDTyI5ktmPJAUCYv3+igAKCRDDTyI5ktmPJOikDA CHGZvj03/pzJJdxwOHb78R8JOV/Exd1Ce84KNmgdNIUZSbQvD+nVzs9j9sh6niHLLDHTUJ3F7tVLlD F3Il8YItbUiBPhn2nDdCslGCVfJl/clY5b8CWk3cqUP2OIr+jxPT/NhTusaJTuzLNJtx+InKe081N6 4B/023wJ2xyFHW5VwOXwgOLNf8WddkbEHRL1tWAgK/evlZgmdBVbCiPqVqYJ5Wu0bT2dz6wUk7vzdP 36sCay8xbWaZkKlB9NISDqQVlmSo4yfBc1jnNHZ+g36Mst+NZouhH9qb+jwOzyMfcdjXdqLBLh9YRi LpVhIoTv8jpEomx7w/dKavoYnZ2y9+GHAuxshj4HsTJRQkHuHl1YnkJv50Z8JjcFB2MHwgULXNGjEC /R8cWBfLWmQQxE0SH/qZO65j58mjyBSaqZH7Iexb34AZ1tHeIOoRpKZQ5be+LscALPscg5IRCjbqM9 zPjDXG+0i1Zxtbn31W8AdY+pBykUcDMCX9Qv3fGw4tB4Y= X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Now that we implemented supported for the LoadFile2 protocol for initrd loading, there is no longer a need to pass the initrd parameters via the device tree. This means there is no longer a reason to update the device tree in the first place, and so we can ignore it entirely. The only remaining reason to deal with the devicetree is if we are using the 'devicetree' command to load one from disk, so tweak the logic in grub_fdt_install() to take that into account. Signed-off-by: Ard Biesheuvel Reviewed-by: Leif Lindholm --- grub-core/loader/arm64/linux.c | 22 ++++++++++---------- grub-core/loader/efi/fdt.c | 7 +++++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index 82c7558b4c4c..68ed1502c68a 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -142,21 +142,21 @@ finalize_params_linux (void) void *fdt; - fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE); + /* Set initrd info */ + if (initrd_start && initrd_end > initrd_start) + { + fdt = grub_fdt_load (GRUB_EFI_LINUX_FDT_EXTRA_SPACE); - if (!fdt) - goto failure; + if (!fdt) + goto failure; - node = grub_fdt_find_subnode (fdt, 0, "chosen"); - if (node < 0) - node = grub_fdt_add_subnode (fdt, 0, "chosen"); + node = grub_fdt_find_subnode (fdt, 0, "chosen"); + if (node < 0) + node = grub_fdt_add_subnode (fdt, 0, "chosen"); - if (node < 1) - goto failure; + if (node < 1) + goto failure; - /* Set initrd info */ - if (initrd_start && initrd_end > initrd_start) - { grub_dprintf ("linux", "Initrd @ %p-%p\n", (void *) initrd_start, (void *) initrd_end); diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c index c86f283d756b..771d455c7319 100644 --- a/grub-core/loader/efi/fdt.c +++ b/grub-core/loader/efi/fdt.c @@ -89,13 +89,16 @@ grub_fdt_install (void) grub_efi_guid_t fdt_guid = GRUB_EFI_DEVICE_TREE_GUID; grub_efi_status_t status; + if (!fdt && !loaded_fdt) + return GRUB_ERR_NONE; + b = grub_efi_system_table->boot_services; - status = b->install_configuration_table (&fdt_guid, fdt); + status = b->install_configuration_table (&fdt_guid, fdt ?: loaded_fdt); if (status != GRUB_EFI_SUCCESS) return grub_error (GRUB_ERR_IO, "failed to install FDT"); grub_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n", - fdt); + fdt ?: loaded_fdt); return GRUB_ERR_NONE; }