1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Linaro Limited
9 #define pr_fmt(fmt) "kexec_file(Image): " fmt
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/kexec.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>
22 static int image_probe(const char *kernel_buf, unsigned long kernel_len)
24 const struct arm64_image_header *h =
25 (const struct arm64_image_header *)(kernel_buf);
27 if (!h || (kernel_len < sizeof(*h)))
30 if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
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)
41 struct arm64_image_header *h;
43 bool be_image, be_kernel;
44 struct kexec_buf kbuf;
45 unsigned long text_offset, kernel_segment_number;
46 struct kexec_segment *kernel_segment;
50 * We require a kernel with an unambiguous Image header. Per
51 * Documentation/arm64/booting.rst, this is the case when image_size
52 * is non-zero (practically speaking, since v3.17).
54 h = (struct arm64_image_header *)kernel;
56 return ERR_PTR(-EINVAL);
58 /* Check cpu features */
59 flags = le64_to_cpu(h->flags);
60 be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE);
61 be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
62 if ((be_image != be_kernel) && !system_supports_mixed_endian())
63 return ERR_PTR(-EINVAL);
65 value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE);
66 if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) &&
67 !system_supports_4kb_granule()) ||
68 ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) &&
69 !system_supports_64kb_granule()) ||
70 ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) &&
71 !system_supports_16kb_granule()))
72 return ERR_PTR(-EINVAL);
77 kbuf.buf_max = ULONG_MAX;
78 kbuf.top_down = false;
81 kbuf.bufsz = kernel_len;
82 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
83 kbuf.memsz = le64_to_cpu(h->image_size);
84 text_offset = le64_to_cpu(h->text_offset);
85 kbuf.buf_align = MIN_KIMG_ALIGN;
87 /* Adjust kernel segment with TEXT_OFFSET */
88 kbuf.memsz += text_offset;
90 kernel_segment_number = image->nr_segments;
93 * The location of the kernel segment may make it impossible to satisfy
94 * the other segment requirements, so we try repeatedly to find a
95 * location that will work.
97 while ((ret = kexec_add_buffer(&kbuf)) == 0) {
98 /* Try to load additional data */
99 kernel_segment = &image->segment[kernel_segment_number];
100 ret = load_other_segments(image, kernel_segment->mem,
101 kernel_segment->memsz, initrd,
102 initrd_len, cmdline);
107 * We couldn't find space for the other segments; erase the
108 * kernel segment and try the next available hole.
110 image->nr_segments -= 1;
111 kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
112 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
116 pr_err("Could not find any suitable kernel location!");
120 kernel_segment = &image->segment[kernel_segment_number];
121 kernel_segment->mem += text_offset;
122 kernel_segment->memsz -= text_offset;
123 image->start = kernel_segment->mem;
125 pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
126 kernel_segment->mem, kbuf.bufsz,
127 kernel_segment->memsz);
132 const struct kexec_file_ops kexec_image_ops = {
133 .probe = image_probe,
135 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
136 .verify_sig = kexec_kernel_verify_pe_sig,