Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1paQBJ-0003QV-JT for mharc-grub-devel@gnu.org; Thu, 09 Mar 2023 19:00:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1paQBF-00030Z-4x for grub-devel@gnu.org; Thu, 09 Mar 2023 19:00:17 -0500 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1paQB9-0000MW-2e for grub-devel@gnu.org; Thu, 09 Mar 2023 19:00:16 -0500 Received: by mail-pj1-x102d.google.com with SMTP id x34so3746380pjj.0 for ; Thu, 09 Mar 2023 16:00:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678406409; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=gIMF4iXlJjE6g5aH5wxyyb4QtcG3CaIWGpYX8Oe8M8Q=; b=O5eHYDmU+W5b55fNH3dqhZGXcqyBGAhELVl7gDiZrSwC6oD1osZaozG4ZDQmzuXdr7 Fje1v/4mH9nt9UUglttcNzq84m9/EKxnvbWr+DV4ulWhZo3nVkCMJamkw62RPS1CnN2P rpkiPnHM+BkHf3x2HgNL5P9F/uS4yTuWCWsRA1RqnoBjFsxaSd/Hda5bq+z485fBvClU C6uMgzX0g4MENwchEEaN+aBvn7ZIeGjm2a4pfCi6+ZquPE6rfUJuNLnXv06nLrWE7w4z 3ya75nU3b+fgDID4VgYdnkgAjI51l67FSCMV1/GMluBglLZ40MD6D0KECz+2pA4PvaJ7 kZMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678406409; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gIMF4iXlJjE6g5aH5wxyyb4QtcG3CaIWGpYX8Oe8M8Q=; b=eBGCiYyvg8Po229dW3zlsF42qR9T4n0ppM5oh0i4lzh0FXXbrH/CqHz+XQIZDPMmtM Zghg6jsF2MiDJaJUeogwqhHtr18gFwrgY4RrGeeWJXkwKfl5fkeEZaAcTIcrqypqE1oD X2XBmtR6+K0+5igZh/r+BODADgrp7d1ccAUi+h1cjCDjCb+iA7IVjzh2ZkRyr63nI9XY JoIrxvD7frHT1DNvj3dfEkU7JCNg06/phG5iSyrYmAdXt0nKcBBRAXlEtIidGCO4+OJo yVqpQSZ6V4fspcAmRIO18Ud3kpzAHTiA9MBDSGyFfgZDyExloFBsQzRBDWg7bYJTgean 2P+A== X-Gm-Message-State: AO0yUKXjGRJ1w/PqJ5PjBRVVTpwAnQ9Oimlrz/5ZtEqZznj4+us9v0z/ i3J5NX+/sGy4NZkvl3wxCo4wrUqPNbJmEkRN9dw= X-Google-Smtp-Source: AK7set8m9tHNn+/gYWrHR4S7VUkAK8xWB7Dhlla1V0DTBdQcPbDYI0OqkpRDbK1NyR8U5KjMbB+i9A== X-Received: by 2002:a17:902:e746:b0:19e:6bc5:8769 with SMTP id p6-20020a170902e74600b0019e6bc58769mr30957394plf.69.1678406409300; Thu, 09 Mar 2023 16:00:09 -0800 (PST) Received: from atishp.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id kb4-20020a170903338400b0019ee0afcc24sm193725plb.94.2023.03.09.16.00.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Mar 2023 16:00:09 -0800 (PST) From: Atish Patra To: grub-devel@gnu.org Cc: Atish Patra , Ard Biesheuvel , Daniel Kiper , Fu Wei , Leif Lindholm , Nikita Ermakov , Atish Patra , Heinrich Schuchardt , Julian Andres Klode , Ilias Apalodimas , fvogt@suse.de, mchang@suse.com, Robbie Harwood Subject: [v8 PATCH 2/3] efi: Remove arch specific image headers for RISC-V, ARM64, ARM Date: Thu, 9 Mar 2023 15:59:52 -0800 Message-Id: <20230309235953.1882811-3-atishp@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230309235953.1882811-1-atishp@rivosinc.com> References: <20230309235953.1882811-1-atishp@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::102d; envelope-from=atishp@rivosinc.com; helo=mail-pj1-x102d.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: Fri, 10 Mar 2023 00:00:17 -0000 The arch specific image header details are not very useful as most of the grub just looks at the PE/COFF spec parameters (PE32 magic and header offset). Remove the arch specific images headers and define a generic arch headers that provide enough PE/COFF fields for grub to parse kernel images correctly. Signed-off-by: Atish Patra --- grub-core/commands/file.c | 23 +++++++++++---- grub-core/loader/arm/linux.c | 3 +- grub-core/loader/arm64/xen_boot.c | 3 +- grub-core/loader/efi/linux.c | 1 - include/grub/arm/linux.h | 20 ------------- include/grub/arm64/linux.h | 48 ------------------------------- include/grub/efi/efi.h | 13 ++++++++- include/grub/riscv32/linux.h | 41 -------------------------- include/grub/riscv64/linux.h | 43 --------------------------- 9 files changed, 33 insertions(+), 162 deletions(-) delete mode 100644 include/grub/arm64/linux.h delete mode 100644 include/grub/riscv32/linux.h delete mode 100644 include/grub/riscv64/linux.h diff --git a/grub-core/commands/file.c b/grub-core/commands/file.c index 9de0006..62ddec4 100644 --- a/grub-core/commands/file.c +++ b/grub-core/commands/file.c @@ -25,10 +25,10 @@ #include #include #include +#include #include #include #include -#include #include #include #include @@ -391,7 +391,7 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args) } case IS_ARM_LINUX: { - struct linux_arm_kernel_header lh; + struct linux_arch_kernel_header lh; if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) break; @@ -412,13 +412,26 @@ grub_cmd_file (grub_extcmd_context_t ctxt, int argc, char **args) } case IS_ARM64_LINUX: { - struct linux_arm64_kernel_header lh; + struct linux_arch_kernel_header lh; if (grub_file_read (file, &lh, sizeof (lh)) != sizeof (lh)) break; - if (lh.magic == - grub_cpu_to_le32_compile_time (GRUB_LINUX_ARM64_MAGIC_SIGNATURE)) + /* + * The PE/COFF header can be anywhere in the file. Load it from the correct + * offset if it is not where it is expected. + */ + if ((grub_uint8_t *) &lh + lh.hdr_offset != (grub_uint8_t *) &lh.pe_image_header) + { + if (grub_file_seek (file, lh.hdr_offset) == (grub_off_t) -1 + || grub_file_read (file, &lh.pe_image_header, + sizeof (struct grub_pe_image_header)) + != sizeof (struct grub_pe_image_header)) + return grub_error (GRUB_ERR_FILE_READ_ERROR, "failed to read COFF image header"); + } + + if (lh.pe_image_header.coff_header.machine == + grub_cpu_to_le32_compile_time (GRUB_PE32_MACHINE_ARM64)) { ret = 1; break; diff --git a/grub-core/loader/arm/linux.c b/grub-core/loader/arm/linux.c index f00b538..19ddedb 100644 --- a/grub-core/loader/arm/linux.c +++ b/grub-core/loader/arm/linux.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -304,7 +305,7 @@ linux_boot (void) static grub_err_t linux_load (const char *filename, grub_file_t file) { - struct linux_arm_kernel_header *lh; + struct linux_arch_kernel_header *lh; int size; size = grub_file_size (file); diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c index 763d87d..26e1472 100644 --- a/grub-core/loader/arm64/xen_boot.c +++ b/grub-core/loader/arm64/xen_boot.c @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -439,7 +438,7 @@ static grub_err_t grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { - struct linux_arm64_kernel_header lh; + struct linux_arch_kernel_header lh; grub_file_t file = NULL; grub_dl_ref (my_mod); diff --git a/grub-core/loader/efi/linux.c b/grub-core/loader/efi/linux.c index 48ab34a..15e0686 100644 --- a/grub-core/loader/efi/linux.c +++ b/grub-core/loader/efi/linux.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include diff --git a/include/grub/arm/linux.h b/include/grub/arm/linux.h index f38e695..5b8fb14 100644 --- a/include/grub/arm/linux.h +++ b/include/grub/arm/linux.h @@ -24,26 +24,6 @@ #include -#define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818 - -struct linux_arm_kernel_header { - grub_uint32_t code0; - grub_uint32_t reserved1[8]; - grub_uint32_t magic; - grub_uint32_t start; /* _start */ - 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__) -# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM_MAGIC_SIGNATURE -# define linux_arch_kernel_header linux_arm_kernel_header -#endif - #if defined GRUB_MACHINE_UBOOT # include # define LINUX_ADDRESS (start_of_ram + 0x8000) diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h deleted file mode 100644 index 3da71a5..0000000 --- a/include/grub/arm64/linux.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2013 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#ifndef GRUB_ARM64_LINUX_HEADER -#define GRUB_ARM64_LINUX_HEADER 1 - -#include -#include - -#define GRUB_LINUX_ARM64_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */ - -/* From linux/Documentation/arm64/booting.txt */ -struct linux_arm64_kernel_header -{ - grub_uint32_t code0; /* Executable code */ - grub_uint32_t code1; /* Executable code */ - grub_uint64_t text_offset; /* Image load offset */ - grub_uint64_t res0; /* reserved */ - grub_uint64_t res1; /* reserved */ - grub_uint64_t res2; /* reserved */ - grub_uint64_t res3; /* reserved */ - 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__) -# define GRUB_LINUX_ARMXX_MAGIC_SIGNATURE GRUB_LINUX_ARM64_MAGIC_SIGNATURE -# define linux_arch_kernel_header linux_arm64_kernel_header -#endif - -#endif /* ! GRUB_ARM64_LINUX_HEADER */ diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index e61272d..329c4f9 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -23,6 +23,18 @@ #include #include #include +#include + +#define GRUB_LINUX_ARM_MAGIC_SIGNATURE 0x016f2818 + +struct linux_arch_kernel_header { + grub_uint32_t code0; + grub_uint32_t code1; + grub_uint64_t reserved[6]; + grub_uint32_t magic; + grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ + struct grub_pe_image_header pe_image_header; +}; /* Functions. */ void *EXPORT_FUNC(grub_efi_locate_protocol) (grub_efi_guid_t *protocol, @@ -101,7 +113,6 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd, #if defined(__arm__) || defined(__aarch64__) || defined(__riscv) void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); grub_err_t EXPORT_FUNC(grub_efi_get_ram_base)(grub_addr_t *); -#include #include grub_err_t grub_arch_efi_linux_load_image_header(grub_file_t file, struct linux_arch_kernel_header *lh); diff --git a/include/grub/riscv32/linux.h b/include/grub/riscv32/linux.h deleted file mode 100644 index 512b777..0000000 --- a/include/grub/riscv32/linux.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2018 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#ifndef GRUB_RISCV32_LINUX_HEADER -#define GRUB_RISCV32_LINUX_HEADER 1 - -#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */ - -/* From linux/Documentation/riscv/booting.txt */ -struct linux_riscv_kernel_header -{ - grub_uint32_t code0; /* Executable code */ - grub_uint32_t code1; /* Executable code */ - grub_uint64_t text_offset; /* Image load offset */ - grub_uint64_t res0; /* reserved */ - grub_uint64_t res1; /* reserved */ - grub_uint64_t res2; /* reserved */ - grub_uint64_t res3; /* reserved */ - grub_uint64_t res4; /* reserved */ - grub_uint32_t magic; /* Magic number, little endian, "RSCV" */ - grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ -}; - -#define linux_arch_kernel_header linux_riscv_kernel_header - -#endif /* ! GRUB_RISCV32_LINUX_HEADER */ diff --git a/include/grub/riscv64/linux.h b/include/grub/riscv64/linux.h deleted file mode 100644 index 3630c30..0000000 --- a/include/grub/riscv64/linux.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2018 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#ifndef GRUB_RISCV64_LINUX_HEADER -#define GRUB_RISCV64_LINUX_HEADER 1 - -#define GRUB_LINUX_RISCV_MAGIC_SIGNATURE 0x52534356 /* 'RSCV' */ - -#define GRUB_EFI_PE_MAGIC 0x5A4D - -/* From linux/Documentation/riscv/booting.txt */ -struct linux_riscv_kernel_header -{ - grub_uint32_t code0; /* Executable code */ - grub_uint32_t code1; /* Executable code */ - grub_uint64_t text_offset; /* Image load offset */ - grub_uint64_t res0; /* reserved */ - grub_uint64_t res1; /* reserved */ - grub_uint64_t res2; /* reserved */ - grub_uint64_t res3; /* reserved */ - grub_uint64_t res4; /* reserved */ - grub_uint32_t magic; /* Magic number, little endian, "RSCV" */ - grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ -}; - -#define linux_arch_kernel_header linux_riscv_kernel_header - -#endif /* ! GRUB_RISCV64_LINUX_HEADER */ -- 2.25.1