From patchwork Mon Jun 12 14:53:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 103633 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp233780qgd; Mon, 12 Jun 2017 07:59:13 -0700 (PDT) X-Received: by 10.237.38.193 with SMTP id q59mr63459948qtd.69.1497279553404; Mon, 12 Jun 2017 07:59:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1497279553; cv=none; d=google.com; s=arc-20160816; b=ixDMzP8VWku5uoqqz7h1FmkBn5ByKNwcgaJR4ZjpHgZNGjoi6d+k1ZkgHUscXWTjb4 gLfRt8d+fhe2Pw73oaEpD7j4poMAWhJacLg553QRC5ZvP+NWYAtlFBq9Z3eKhQTHeNts h1Reo8aC5bMNs2SPzeWV4conzXBsqs+BERxl+jOS6Vka89EyjZA13Zl+MW7WdCIkEebj 2LHEhJ9QIjiDP7zbdog1TC0UAL9D+CS8IXOpWblc7eCqWd7XuMwKpPlewuhD41bLXFWL T0cgVmhOPdCXWwGdIto26qyMOONHHtEMRrbuW4OH4mtaKU/IgJzWxeLjEl+GFy5oLRmn jwkA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:references:in-reply-to:message-id:date:subject :to:from:dkim-signature:arc-authentication-results; bh=MZfAsxhunoPMts1lqUWWa+ZdJhTg38Hd1BcyuQxUJ2k=; b=CU3fxZcTRcnz/PZvnCqymrFlwuaONbOEty8RIlJiTvm0Ha2uUi5unihEOQpDdfv6jg 8V5VM50dgMrmfnHqececkV2Sm8FSuYL2D2GwfjxEiIuQl//uZ6sJFBedn2gkPAoD3LbG 6Digc43o/wiJib128ai+4eBPTmB/bRuVh3e1nBuDUsh7D16h9Q6WoLkbgAy8nzU2t3mk Lm0OMfkMOdrZ1FQMHrzqh6AOFV0SCxPDC2FWjynij7L1ls1HSGihhwz0EE29Gcp9spvV fFC5fB7oS/lCSURQeErhyGTMTrvasc8IqlofLbdZvwPD0nrx+6Gr3O30PPI7tuv/JSbq oBlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id h64si8809992qtd.205.2017.06.12.07.59.12 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 12 Jun 2017 07:59:13 -0700 (PDT) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:38532 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKQoA-0003UM-If for patch@linaro.org; Mon, 12 Jun 2017 10:59:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dKQl4-00010J-Fu for grub-devel@gnu.org; Mon, 12 Jun 2017 10:56:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dKQkz-0000vb-M5 for grub-devel@gnu.org; Mon, 12 Jun 2017 10:55:58 -0400 Received: from mail-wr0-x22f.google.com ([2a00:1450:400c:c0c::22f]:34101) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dKQkz-0000v9-CK for grub-devel@gnu.org; Mon, 12 Jun 2017 10:55:53 -0400 Received: by mail-wr0-x22f.google.com with SMTP id g76so98959730wrd.1 for ; Mon, 12 Jun 2017 07:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=iCIpw4e86qzOQPVBLlBq0TZCFr9fqFMPEgxcEKoYylI=; b=VqIWFW9sagDpIADtTffZMgnvdUZFTNr/on2gteYO0f7Xe5dMZDEZUgUYFcBJ4P4Egf 1pSP3XWFkrgGPWQHAECnJRZUZUd92/EDTrfXUtEgMLqcI3VLNHxiMQ0CvSFYGV970psG DkhzZaUysWso4TpAAFBuPRwspUJcBi+yP42vc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=iCIpw4e86qzOQPVBLlBq0TZCFr9fqFMPEgxcEKoYylI=; b=TjMtuqkFcy+U4WLZ3FT/4d4BuHvkNWplKEmkceVsltjcHsJM94efCU8Kvp2+HxYaeu ig81Q7WD7uYr7fL0KI1xkTY1CjrY6+S69cFpw6KyNLV2Bn0io4v5wxEt4XdeJCws5hE/ 9aqHgrtjzraUmN5ccTar32qi3iYG8rmJvo2p7fgbEvONV6+ah5SMBbleItIoBZY7XqOE 1PhdGusjZ19oOvdFqE4TwM3wzD/0JCaMo9R7T62azZBQ6R1P7CzXkz+zDNN67MPp9YHq 64anYCxPh+ApSYY1AdwMwQ1gDwDhtKXbnwC/Qz78sImVXbULTHDneT0Y+AnKxeFg7Hof T2cg== X-Gm-Message-State: AKS2vOz1GtP+qYGTl2BpXwi5QZX75IeLFE0s7Jc7pRrc+ZRsiAe1NzrZ SfD+FZhccGfkBNoUCRF6mg== X-Received: by 10.223.150.39 with SMTP id b36mr8571999wra.149.1497279347405; Mon, 12 Jun 2017 07:55:47 -0700 (PDT) Received: from mohikan.hemma.eciton.net (cpc92316-cmbg19-2-0-cust118.5-4.cable.virginm.net. [82.12.0.119]) by smtp.gmail.com with ESMTPSA id s140sm7722wmd.1.2017.06.12.07.55.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Jun 2017 07:55:46 -0700 (PDT) From: Leif Lindholm To: grub-devel@gnu.org Subject: [PATCH 4/7] arm64: make efi linux loader more generic Date: Mon, 12 Jun 2017 15:53:38 +0100 Message-Id: <20170612145341.3351-5-leif.lindholm@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170612145341.3351-1-leif.lindholm@linaro.org> References: <20170612145341.3351-1-leif.lindholm@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::22f X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The development of GNU GRUB Cc: ard.biesheuvel@linaro.org, lersek@redhat.com, agraf@suse.de, kraxel@redhat.com MIME-Version: 1.0 Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: "Grub-devel" In order to enable reuse of the arm64 efi linux loader for arm, change a few function names and macros. Add a global definition of GRUB_PE32_MAGIC in grub/efi/pe32.h. Make the arm64 efi loader (and fdt helpers) 32/64-bit safe. Also update the arm64 xen loader, since it depends on some of the functions in the linux loader. Signed-off-by: Leif Lindholm --- grub-core/loader/arm64/linux.c | 31 ++++++++++++++----------------- grub-core/loader/arm64/xen_boot.c | 12 ++++++------ grub-core/loader/efi/fdt.c | 8 ++++---- include/grub/arm64/linux.h | 13 ++----------- include/grub/efi/efi.h | 4 ++++ include/grub/efi/pe32.h | 2 ++ 6 files changed, 32 insertions(+), 38 deletions(-) -- 2.11.0 _______________________________________________ Grub-devel mailing list Grub-devel@gnu.org https://lists.gnu.org/mailman/listinfo/grub-devel diff --git a/grub-core/loader/arm64/linux.c b/grub-core/loader/arm64/linux.c index cac94d53d..8cd44230d 100644 --- a/grub-core/loader/arm64/linux.c +++ b/grub-core/loader/arm64/linux.c @@ -48,18 +48,16 @@ static grub_addr_t initrd_start; static grub_addr_t initrd_end; grub_err_t -grub_arm64_uefi_check_image (struct grub_arm64_linux_kernel_header * lh) +grub_efi_linux_check_image (struct grub_linux_kernel_header * lh) { - if (lh->magic != GRUB_ARM64_LINUX_MAGIC) + if (lh->magic != GRUB_LINUX_MAGIC_SIGNATURE) return grub_error(GRUB_ERR_BAD_OS, "invalid magic number"); - if ((lh->code0 & 0xffff) != GRUB_EFI_PE_MAGIC) + 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")); grub_dprintf ("linux", "UEFI stub kernel:\n"); - grub_dprintf ("linux", "text_offset = 0x%012llx\n", - (long long unsigned) lh->text_offset); grub_dprintf ("linux", "PE/COFF header @ %08x\n", lh->hdr_offset); return GRUB_ERR_NONE; @@ -87,8 +85,8 @@ finalize_params_linux (void) /* Set initrd info */ if (initrd_start && initrd_end > initrd_start) { - grub_dprintf ("linux", "Initrd @ 0x%012lx-0x%012lx\n", - initrd_start, initrd_end); + grub_dprintf ("linux", "Initrd @ %p-%p\n", + (void *) initrd_start, (void *) initrd_end); retval = grub_fdt_set_prop64 (fdt, node, "linux,initrd-start", initrd_start); @@ -111,7 +109,7 @@ failure: } grub_err_t -grub_arm64_uefi_boot_image (grub_addr_t addr, grub_size_t size, char *args) +grub_efi_linux_boot_image (grub_addr_t addr, grub_size_t size, char *args) { grub_efi_memory_mapped_device_path_t *mempath; grub_efi_handle_t image_handle; @@ -163,7 +161,7 @@ grub_arm64_uefi_boot_image (grub_addr_t addr, grub_size_t size, char *args) /* When successful, not reached */ b->unload_image (image_handle); - grub_efi_free_pages ((grub_efi_physical_address_t) loaded_image->load_options, + grub_efi_free_pages ((grub_addr_t) loaded_image->load_options, GRUB_EFI_BYTES_TO_PAGES (loaded_image->load_options_size)); return grub_errno; @@ -175,8 +173,8 @@ grub_linux_boot (void) if (finalize_params_linux () != GRUB_ERR_NONE) return grub_errno; - return (grub_arm64_uefi_boot_image((grub_addr_t)kernel_addr, - kernel_size, linux_args)); + return (grub_efi_linux_boot_image((grub_addr_t)kernel_addr, + kernel_size, linux_args)); } static grub_err_t @@ -190,7 +188,7 @@ grub_linux_unload (void) initrd_start = initrd_end = 0; grub_free (linux_args); if (kernel_addr) - grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr, + grub_efi_free_pages ((grub_addr_t) kernel_addr, GRUB_EFI_BYTES_TO_PAGES (kernel_size)); grub_fdt_unload (); return GRUB_ERR_NONE; @@ -242,8 +240,7 @@ grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), fail: grub_initrd_close (&initrd_ctx); if (initrd_mem && !initrd_start) - grub_efi_free_pages ((grub_efi_physical_address_t) initrd_mem, - initrd_pages); + grub_efi_free_pages ((grub_addr_t) initrd_mem, initrd_pages); return grub_errno; } @@ -253,7 +250,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { grub_file_t file = 0; - struct grub_arm64_linux_kernel_header lh; + struct grub_linux_kernel_header lh; grub_dl_ref (my_mod); @@ -272,7 +269,7 @@ grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), if (grub_file_read (file, &lh, sizeof (lh)) < (long) sizeof (lh)) return grub_errno; - if (grub_arm64_uefi_check_image (&lh) != GRUB_ERR_NONE) + if (grub_efi_linux_check_image (&lh) != GRUB_ERR_NONE) goto fail; grub_loader_unset(); @@ -330,7 +327,7 @@ fail: grub_free (linux_args); if (kernel_addr && !loaded) - grub_efi_free_pages ((grub_efi_physical_address_t) kernel_addr, + grub_efi_free_pages ((grub_addr_t) kernel_addr, GRUB_EFI_BYTES_TO_PAGES (kernel_size)); return grub_errno; diff --git a/grub-core/loader/arm64/xen_boot.c b/grub-core/loader/arm64/xen_boot.c index d092a53ed..e8720584b 100644 --- a/grub-core/loader/arm64/xen_boot.c +++ b/grub-core/loader/arm64/xen_boot.c @@ -67,7 +67,7 @@ typedef enum module_type module_type_t; struct xen_hypervisor_header { - struct grub_arm64_linux_kernel_header efi_head; + struct grub_linux_kernel_header efi_head; /* This is always PE\0\0. */ grub_uint8_t signature[GRUB_PE32_SIGNATURE_SIZE]; @@ -254,9 +254,9 @@ xen_boot (void) if (err) return err; - return grub_arm64_uefi_boot_image (xen_hypervisor->start, - xen_hypervisor->size, - xen_hypervisor->cmdline); + return grub_efi_linux_boot_image (xen_hypervisor->start, + xen_hypervisor->size, + xen_hypervisor->cmdline); } static void @@ -458,8 +458,8 @@ grub_cmd_xen_hypervisor (grub_command_t cmd __attribute__ ((unused)), if (grub_file_read (file, &sh, sizeof (sh)) != (long) sizeof (sh)) goto fail; - if (grub_arm64_uefi_check_image - ((struct grub_arm64_linux_kernel_header *) &sh) != GRUB_ERR_NONE) + if (grub_efi_linux_check_image + ((struct grub_linux_kernel_header *) &sh) != GRUB_ERR_NONE) goto fail; grub_file_seek (file, 0); diff --git a/grub-core/loader/efi/fdt.c b/grub-core/loader/efi/fdt.c index be369fd9d..e2899c47b 100644 --- a/grub-core/loader/efi/fdt.c +++ b/grub-core/loader/efi/fdt.c @@ -33,12 +33,12 @@ void * grub_fdt_load (grub_size_t additional_size) { void *raw_fdt; - grub_size_t size; + unsigned int size; if (fdt) { size = GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt)); - grub_efi_free_pages ((grub_efi_physical_address_t) fdt, size); + grub_efi_free_pages ((grub_addr_t) fdt, size); } if (loaded_fdt) @@ -50,7 +50,7 @@ grub_fdt_load (grub_size_t additional_size) raw_fdt ? grub_fdt_get_totalsize (raw_fdt) : GRUB_FDT_EMPTY_TREE_SZ; size += additional_size; - grub_dprintf ("linux", "allocating %ld bytes for fdt\n", size); + grub_dprintf ("linux", "allocating %d bytes for fdt\n", size); fdt = grub_efi_allocate_pages (0, GRUB_EFI_BYTES_TO_PAGES (size)); if (!fdt) return NULL; @@ -89,7 +89,7 @@ grub_fdt_unload (void) { if (!fdt) { return; } - grub_efi_free_pages ((grub_efi_physical_address_t) fdt, + grub_efi_free_pages ((grub_addr_t) fdt, GRUB_EFI_BYTES_TO_PAGES (grub_fdt_get_totalsize (fdt))); fdt = NULL; } diff --git a/include/grub/arm64/linux.h b/include/grub/arm64/linux.h index 1ea23696e..e53be83b1 100644 --- a/include/grub/arm64/linux.h +++ b/include/grub/arm64/linux.h @@ -19,14 +19,10 @@ #ifndef GRUB_LINUX_CPU_HEADER #define GRUB_LINUX_CPU_HEADER 1 -#include - -#define GRUB_ARM64_LINUX_MAGIC 0x644d5241 /* 'ARM\x64' */ - -#define GRUB_EFI_PE_MAGIC 0x5A4D +#define GRUB_LINUX_MAGIC_SIGNATURE 0x644d5241 /* 'ARM\x64' */ /* From linux/Documentation/arm64/booting.txt */ -struct grub_arm64_linux_kernel_header +struct grub_linux_kernel_header { grub_uint32_t code0; /* Executable code */ grub_uint32_t code1; /* Executable code */ @@ -40,9 +36,4 @@ struct grub_arm64_linux_kernel_header grub_uint32_t hdr_offset; /* Offset of PE/COFF header */ }; -grub_err_t grub_arm64_uefi_check_image (struct grub_arm64_linux_kernel_header - *lh); -grub_err_t grub_arm64_uefi_boot_image (grub_addr_t addr, grub_size_t size, - char *args); - #endif /* ! GRUB_LINUX_CPU_HEADER */ diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h index 904722174..395daeae3 100644 --- a/include/grub/efi/efi.h +++ b/include/grub/efi/efi.h @@ -89,6 +89,10 @@ extern void (*EXPORT_VAR(grub_efi_net_config)) (grub_efi_handle_t hnd, #if defined(__arm__) || defined(__aarch64__) void *EXPORT_FUNC(grub_efi_get_firmware_fdt)(void); grub_err_t EXPORT_FUNC(grub_efi_get_dram_base)(grub_addr_t *); +#include +grub_err_t grub_efi_linux_check_image(struct grub_linux_kernel_header *lh); +grub_err_t grub_efi_linux_boot_image(grub_addr_t addr, grub_size_t size, + char *args); #endif grub_addr_t grub_efi_modules_addr (void); diff --git a/include/grub/efi/pe32.h b/include/grub/efi/pe32.h index f79c36c02..7d44732d2 100644 --- a/include/grub/efi/pe32.h +++ b/include/grub/efi/pe32.h @@ -45,6 +45,8 @@ #define GRUB_PE32_MSDOS_STUB_SIZE 0x80 +#define GRUB_PE32_MAGIC 0x5a4d + /* 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