1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * RISC-V Kexec image loader 4 * 5 */ 6 7 #define pr_fmt(fmt) "kexec_file(Image): " fmt 8 9 #include <linux/err.h> 10 #include <linux/errno.h> 11 #include <linux/kernel.h> 12 #include <linux/kexec.h> 13 #include <linux/pe.h> 14 #include <linux/string.h> 15 #include <asm/byteorder.h> 16 #include <asm/image.h> 17 18 static int image_probe(const char *kernel_buf, unsigned long kernel_len) 19 { 20 const struct riscv_image_header *h = (const struct riscv_image_header *)kernel_buf; 21 22 if (!h || kernel_len < sizeof(*h)) 23 return -EINVAL; 24 25 /* According to Documentation/riscv/boot-image-header.rst, 26 * use "magic2" field to check when version >= 0.2. 27 */ 28 29 if (h->version >= RISCV_HEADER_VERSION && 30 memcmp(&h->magic2, RISCV_IMAGE_MAGIC2, sizeof(h->magic2))) 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 riscv_image_header *h; 42 u64 flags; 43 bool be_image, be_kernel; 44 struct kexec_buf kbuf; 45 int ret; 46 47 /* Check Image header */ 48 h = (struct riscv_image_header *)kernel; 49 if (!h->image_size) { 50 ret = -EINVAL; 51 goto out; 52 } 53 54 /* Check endianness */ 55 flags = le64_to_cpu(h->flags); 56 be_image = riscv_image_flag_field(flags, RISCV_IMAGE_FLAG_BE); 57 be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); 58 if (be_image != be_kernel) { 59 ret = -EINVAL; 60 goto out; 61 } 62 63 /* Load the kernel image */ 64 kbuf.image = image; 65 kbuf.buf_min = 0; 66 kbuf.buf_max = ULONG_MAX; 67 kbuf.top_down = false; 68 69 kbuf.buffer = kernel; 70 kbuf.bufsz = kernel_len; 71 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 72 kbuf.memsz = le64_to_cpu(h->image_size); 73 kbuf.buf_align = le64_to_cpu(h->text_offset); 74 75 ret = kexec_add_buffer(&kbuf); 76 if (ret) { 77 pr_err("Error add kernel image ret=%d\n", ret); 78 goto out; 79 } 80 81 image->start = kbuf.mem; 82 83 pr_info("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 84 kbuf.mem, kbuf.bufsz, kbuf.memsz); 85 86 ret = load_extra_segments(image, kbuf.mem, kbuf.memsz, 87 initrd, initrd_len, cmdline, cmdline_len); 88 89 out: 90 return ret ? ERR_PTR(ret) : NULL; 91 } 92 93 const struct kexec_file_ops image_kexec_ops = { 94 .probe = image_probe, 95 .load = image_load, 96 }; 97