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