Skip to content

Commit 809a11e

Browse files
Song Shuaipalmer-dabbelt
authored andcommitted
riscv: kexec_file: Support loading Image binary file
This patch creates image_kexec_ops to load Image binary file for kexec_file_load() syscall. Signed-off-by: Song Shuai <[email protected]> Signed-off-by: Björn Töpel <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexandre Ghiti <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 1df45f8 commit 809a11e

File tree

5 files changed

+101
-1
lines changed

5 files changed

+101
-1
lines changed

arch/riscv/include/asm/image.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
RISCV_HEADER_VERSION_MINOR)
3131

3232
#ifndef __ASSEMBLY__
33+
#define riscv_image_flag_field(flags, field)\
34+
(((flags) >> field##_SHIFT) & field##_MASK)
3335
/**
3436
* struct riscv_image_header - riscv kernel image header
3537
* @code0: Executable code

arch/riscv/include/asm/kexec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extern riscv_kexec_method riscv_kexec_norelocate;
5656

5757
#ifdef CONFIG_KEXEC_FILE
5858
extern const struct kexec_file_ops elf_kexec_ops;
59+
extern const struct kexec_file_ops image_kexec_ops;
5960

6061
struct purgatory_info;
6162
int arch_kexec_apply_relocations_add(struct purgatory_info *pi,

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o
107107
obj-$(CONFIG_PARAVIRT) += paravirt.o
108108
obj-$(CONFIG_KGDB) += kgdb.o
109109
obj-$(CONFIG_KEXEC_CORE) += kexec_relocate.o crash_save_regs.o machine_kexec.o
110-
obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o machine_kexec_file.o
110+
obj-$(CONFIG_KEXEC_FILE) += kexec_elf.o kexec_image.o machine_kexec_file.o
111111
obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
112112
obj-$(CONFIG_VMCORE_INFO) += vmcore_info.o
113113

arch/riscv/kernel/kexec_image.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
};

arch/riscv/kernel/machine_kexec_file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const struct kexec_file_ops * const kexec_file_loaders[] = {
2020
&elf_kexec_ops,
21+
&image_kexec_ops,
2122
NULL
2223
};
2324

0 commit comments

Comments
 (0)