1f3b70e50SAKASHI Takahiro // SPDX-License-Identifier: GPL-2.0
2f3b70e50SAKASHI Takahiro /*
3f3b70e50SAKASHI Takahiro * Kexec image loader
4f3b70e50SAKASHI Takahiro
5f3b70e50SAKASHI Takahiro * Copyright (C) 2018 Linaro Limited
6f3b70e50SAKASHI Takahiro * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7f3b70e50SAKASHI Takahiro */
8f3b70e50SAKASHI Takahiro
9f3b70e50SAKASHI Takahiro #define pr_fmt(fmt) "kexec_file(Image): " fmt
10f3b70e50SAKASHI Takahiro
11f3b70e50SAKASHI Takahiro #include <linux/err.h>
12f3b70e50SAKASHI Takahiro #include <linux/errno.h>
13f3b70e50SAKASHI Takahiro #include <linux/kernel.h>
14f3b70e50SAKASHI Takahiro #include <linux/kexec.h>
15732b7b93SAKASHI Takahiro #include <linux/pe.h>
16f3b70e50SAKASHI Takahiro #include <linux/string.h>
17f3b70e50SAKASHI Takahiro #include <asm/byteorder.h>
18f3b70e50SAKASHI Takahiro #include <asm/cpufeature.h>
19f3b70e50SAKASHI Takahiro #include <asm/image.h>
20f3b70e50SAKASHI Takahiro #include <asm/memory.h>
21f3b70e50SAKASHI Takahiro
image_probe(const char * kernel_buf,unsigned long kernel_len)22f3b70e50SAKASHI Takahiro static int image_probe(const char *kernel_buf, unsigned long kernel_len)
23f3b70e50SAKASHI Takahiro {
24732b7b93SAKASHI Takahiro const struct arm64_image_header *h =
25732b7b93SAKASHI Takahiro (const struct arm64_image_header *)(kernel_buf);
26f3b70e50SAKASHI Takahiro
27732b7b93SAKASHI Takahiro if (!h || (kernel_len < sizeof(*h)))
28732b7b93SAKASHI Takahiro return -EINVAL;
29f3b70e50SAKASHI Takahiro
30732b7b93SAKASHI Takahiro if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
31f3b70e50SAKASHI Takahiro return -EINVAL;
32f3b70e50SAKASHI Takahiro
33f3b70e50SAKASHI Takahiro return 0;
34f3b70e50SAKASHI Takahiro }
35f3b70e50SAKASHI Takahiro
image_load(struct kimage * image,char * kernel,unsigned long kernel_len,char * initrd,unsigned long initrd_len,char * cmdline,unsigned long cmdline_len)36f3b70e50SAKASHI Takahiro static void *image_load(struct kimage *image,
37f3b70e50SAKASHI Takahiro char *kernel, unsigned long kernel_len,
38f3b70e50SAKASHI Takahiro char *initrd, unsigned long initrd_len,
39f3b70e50SAKASHI Takahiro char *cmdline, unsigned long cmdline_len)
40f3b70e50SAKASHI Takahiro {
41f3b70e50SAKASHI Takahiro struct arm64_image_header *h;
42f3b70e50SAKASHI Takahiro u64 flags, value;
43f3b70e50SAKASHI Takahiro bool be_image, be_kernel;
44f3b70e50SAKASHI Takahiro struct kexec_buf kbuf;
45108aa503SBenjamin Gwin unsigned long text_offset, kernel_segment_number;
46f3b70e50SAKASHI Takahiro struct kexec_segment *kernel_segment;
47f3b70e50SAKASHI Takahiro int ret;
48f3b70e50SAKASHI Takahiro
49f3b70e50SAKASHI Takahiro /*
50f3b70e50SAKASHI Takahiro * We require a kernel with an unambiguous Image header. Per
516e4596c4SJonathan Corbet * Documentation/arch/arm64/booting.rst, this is the case when image_size
52f3b70e50SAKASHI Takahiro * is non-zero (practically speaking, since v3.17).
53f3b70e50SAKASHI Takahiro */
54f3b70e50SAKASHI Takahiro h = (struct arm64_image_header *)kernel;
55f3b70e50SAKASHI Takahiro if (!h->image_size)
56f3b70e50SAKASHI Takahiro return ERR_PTR(-EINVAL);
57f3b70e50SAKASHI Takahiro
58f3b70e50SAKASHI Takahiro /* Check cpu features */
59f3b70e50SAKASHI Takahiro flags = le64_to_cpu(h->flags);
60f3b70e50SAKASHI Takahiro be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE);
61f3b70e50SAKASHI Takahiro be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
62f3b70e50SAKASHI Takahiro if ((be_image != be_kernel) && !system_supports_mixed_endian())
63f3b70e50SAKASHI Takahiro return ERR_PTR(-EINVAL);
64f3b70e50SAKASHI Takahiro
65f3b70e50SAKASHI Takahiro value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE);
66f3b70e50SAKASHI Takahiro if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) &&
67f3b70e50SAKASHI Takahiro !system_supports_4kb_granule()) ||
68f3b70e50SAKASHI Takahiro ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) &&
69f3b70e50SAKASHI Takahiro !system_supports_64kb_granule()) ||
70f3b70e50SAKASHI Takahiro ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) &&
71f3b70e50SAKASHI Takahiro !system_supports_16kb_granule()))
72f3b70e50SAKASHI Takahiro return ERR_PTR(-EINVAL);
73f3b70e50SAKASHI Takahiro
74f3b70e50SAKASHI Takahiro /* Load the kernel */
75f3b70e50SAKASHI Takahiro kbuf.image = image;
76f3b70e50SAKASHI Takahiro kbuf.buf_min = 0;
77f3b70e50SAKASHI Takahiro kbuf.buf_max = ULONG_MAX;
78f3b70e50SAKASHI Takahiro kbuf.top_down = false;
79f3b70e50SAKASHI Takahiro
80f3b70e50SAKASHI Takahiro kbuf.buffer = kernel;
81f3b70e50SAKASHI Takahiro kbuf.bufsz = kernel_len;
82c19d050fSBhupesh Sharma kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
83f3b70e50SAKASHI Takahiro kbuf.memsz = le64_to_cpu(h->image_size);
84f3b70e50SAKASHI Takahiro text_offset = le64_to_cpu(h->text_offset);
85f3b70e50SAKASHI Takahiro kbuf.buf_align = MIN_KIMG_ALIGN;
86f3b70e50SAKASHI Takahiro
87f3b70e50SAKASHI Takahiro /* Adjust kernel segment with TEXT_OFFSET */
88f3b70e50SAKASHI Takahiro kbuf.memsz += text_offset;
89f3b70e50SAKASHI Takahiro
90108aa503SBenjamin Gwin kernel_segment_number = image->nr_segments;
91f3b70e50SAKASHI Takahiro
92108aa503SBenjamin Gwin /*
93108aa503SBenjamin Gwin * The location of the kernel segment may make it impossible to satisfy
94108aa503SBenjamin Gwin * the other segment requirements, so we try repeatedly to find a
95108aa503SBenjamin Gwin * location that will work.
96108aa503SBenjamin Gwin */
97108aa503SBenjamin Gwin while ((ret = kexec_add_buffer(&kbuf)) == 0) {
98108aa503SBenjamin Gwin /* Try to load additional data */
99108aa503SBenjamin Gwin kernel_segment = &image->segment[kernel_segment_number];
100108aa503SBenjamin Gwin ret = load_other_segments(image, kernel_segment->mem,
101108aa503SBenjamin Gwin kernel_segment->memsz, initrd,
102108aa503SBenjamin Gwin initrd_len, cmdline);
103108aa503SBenjamin Gwin if (!ret)
104108aa503SBenjamin Gwin break;
105108aa503SBenjamin Gwin
106108aa503SBenjamin Gwin /*
107108aa503SBenjamin Gwin * We couldn't find space for the other segments; erase the
108108aa503SBenjamin Gwin * kernel segment and try the next available hole.
109108aa503SBenjamin Gwin */
110108aa503SBenjamin Gwin image->nr_segments -= 1;
111108aa503SBenjamin Gwin kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
112108aa503SBenjamin Gwin kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
113108aa503SBenjamin Gwin }
114108aa503SBenjamin Gwin
115108aa503SBenjamin Gwin if (ret) {
116108aa503SBenjamin Gwin pr_err("Could not find any suitable kernel location!");
117108aa503SBenjamin Gwin return ERR_PTR(ret);
118108aa503SBenjamin Gwin }
119108aa503SBenjamin Gwin
120108aa503SBenjamin Gwin kernel_segment = &image->segment[kernel_segment_number];
121f3b70e50SAKASHI Takahiro kernel_segment->mem += text_offset;
122f3b70e50SAKASHI Takahiro kernel_segment->memsz -= text_offset;
123f3b70e50SAKASHI Takahiro image->start = kernel_segment->mem;
124f3b70e50SAKASHI Takahiro
125*6f8c1da0SBaoquan He kexec_dprintk("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
126f3b70e50SAKASHI Takahiro kernel_segment->mem, kbuf.bufsz,
127f3b70e50SAKASHI Takahiro kernel_segment->memsz);
128f3b70e50SAKASHI Takahiro
12985f0b2fcSWill Deacon return NULL;
130f3b70e50SAKASHI Takahiro }
131f3b70e50SAKASHI Takahiro
132f3b70e50SAKASHI Takahiro const struct kexec_file_ops kexec_image_ops = {
133f3b70e50SAKASHI Takahiro .probe = image_probe,
134f3b70e50SAKASHI Takahiro .load = image_load,
135732b7b93SAKASHI Takahiro #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
1360d519cadSCoiby Xu .verify_sig = kexec_kernel_verify_pe_sig,
137732b7b93SAKASHI Takahiro #endif
138f3b70e50SAKASHI Takahiro };
139