1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Kexec image loader 4 5 * Copyright (C) 2018 Linaro Limited 6 * Author: AKASHI Takahiro <takahiro.akashi@linaro.org> 7 */ 8 9 #define pr_fmt(fmt) "kexec_file(Image): " fmt 10 11 #include <linux/err.h> 12 #include <linux/errno.h> 13 #include <linux/kernel.h> 14 #include <linux/kexec.h> 15 #include <linux/pe.h> 16 #include <linux/string.h> 17 #include <asm/byteorder.h> 18 #include <asm/cpufeature.h> 19 #include <asm/image.h> 20 #include <asm/memory.h> 21 22 static int image_probe(const char *kernel_buf, unsigned long kernel_len) 23 { 24 const struct arm64_image_header *h = 25 (const struct arm64_image_header *)(kernel_buf); 26 27 if (!h || (kernel_len < sizeof(*h))) 28 return -EINVAL; 29 30 if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic))) 31 return -EINVAL; 32 33 return 0; 34 } 35 36 static void *image_load(struct kimage *image, 37 char *kernel, unsigned long kernel_len, 38 char *initrd, unsigned long initrd_len, 39 char *cmdline, unsigned long cmdline_len) 40 { 41 struct arm64_image_header *h; 42 u64 flags, value; 43 bool be_image; 44 struct kexec_buf kbuf = {}; 45 unsigned long text_offset, kernel_segment_number; 46 struct kexec_segment *kernel_segment; 47 int ret; 48 49 /* 50 * We require a kernel with an unambiguous Image header. Per 51 * Documentation/arch/arm64/booting.rst, this is the case when image_size 52 * is non-zero (practically speaking, since v3.17). 53 */ 54 h = (struct arm64_image_header *)kernel; 55 if (!h->image_size) 56 return ERR_PTR(-EINVAL); 57 58 /* Check cpu features */ 59 flags = le64_to_cpu(h->flags); 60 be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE); 61 if (be_image && !system_supports_mixed_endian()) 62 return ERR_PTR(-EINVAL); 63 64 value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE); 65 if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) && 66 !system_supports_4kb_granule()) || 67 ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) && 68 !system_supports_64kb_granule()) || 69 ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) && 70 !system_supports_16kb_granule())) 71 return ERR_PTR(-EINVAL); 72 73 /* Load the kernel */ 74 kbuf.image = image; 75 kbuf.buf_min = 0; 76 kbuf.buf_max = ULONG_MAX; 77 kbuf.top_down = false; 78 79 kbuf.buffer = kernel; 80 kbuf.bufsz = kernel_len; 81 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 82 kbuf.memsz = le64_to_cpu(h->image_size); 83 text_offset = le64_to_cpu(h->text_offset); 84 kbuf.buf_align = MIN_KIMG_ALIGN; 85 86 /* Adjust kernel segment with TEXT_OFFSET */ 87 kbuf.memsz += text_offset; 88 89 kernel_segment_number = image->nr_segments; 90 91 /* 92 * The location of the kernel segment may make it impossible to satisfy 93 * the other segment requirements, so we try repeatedly to find a 94 * location that will work. 95 */ 96 while ((ret = kexec_add_buffer(&kbuf)) == 0) { 97 /* Try to load additional data */ 98 kernel_segment = &image->segment[kernel_segment_number]; 99 ret = load_other_segments(image, kernel_segment->mem, 100 kernel_segment->memsz, initrd, 101 initrd_len, cmdline); 102 if (!ret) 103 break; 104 105 /* 106 * We couldn't find space for the other segments; erase the 107 * kernel segment and try the next available hole. 108 */ 109 image->nr_segments -= 1; 110 kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz; 111 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 112 } 113 114 if (ret) { 115 pr_err("Could not find any suitable kernel location!"); 116 return ERR_PTR(ret); 117 } 118 119 kernel_segment = &image->segment[kernel_segment_number]; 120 kernel_segment->mem += text_offset; 121 kernel_segment->memsz -= text_offset; 122 image->start = kernel_segment->mem; 123 124 kexec_dprintk("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 125 kernel_segment->mem, kbuf.bufsz, 126 kernel_segment->memsz); 127 128 return NULL; 129 } 130 131 const struct kexec_file_ops kexec_image_ops = { 132 .probe = image_probe, 133 .load = image_load, 134 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG 135 .verify_sig = kexec_kernel_verify_pe_sig, 136 #endif 137 }; 138