1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2018 Linaro Limited
8 * Most code is derived from arm64 port of kexec-tools
11 #define pr_fmt(fmt) "kexec_file: " fmt
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/kexec.h>
16 #include <linux/libfdt.h>
17 #include <linux/memblock.h>
19 #include <linux/of_fdt.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
25 const struct kexec_file_ops * const kexec_file_loaders[] = {
30 int arch_kimage_file_post_load_cleanup(struct kimage *image)
32 kvfree(image->arch.dtb);
33 image->arch.dtb = NULL;
35 vfree(image->elf_headers);
36 image->elf_headers = NULL;
37 image->elf_headers_sz = 0;
39 return kexec_image_post_load_cleanup_default(image);
42 static int prepare_elf_headers(void **addr, unsigned long *sz)
44 struct crash_mem *cmem;
45 unsigned int nr_ranges;
48 phys_addr_t start, end;
50 nr_ranges = 1; /* for exclusion of crashkernel region */
51 for_each_mem_range(i, &start, &end)
54 cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL);
58 cmem->max_nr_ranges = nr_ranges;
60 for_each_mem_range(i, &start, &end) {
61 cmem->ranges[cmem->nr_ranges].start = start;
62 cmem->ranges[cmem->nr_ranges].end = end - 1;
66 /* Exclude crashkernel region */
67 ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
71 if (crashk_low_res.end) {
72 ret = crash_exclude_mem_range(cmem, crashk_low_res.start, crashk_low_res.end);
77 ret = crash_prepare_elf64_headers(cmem, true, addr, sz);
85 * Tries to add the initrd and DTB to the image. If it is not possible to find
86 * valid locations, this function will undo changes to the image and return non
89 int load_other_segments(struct kimage *image,
90 unsigned long kernel_load_addr,
91 unsigned long kernel_size,
92 char *initrd, unsigned long initrd_len,
95 struct kexec_buf kbuf;
96 void *headers, *dtb = NULL;
97 unsigned long headers_sz, initrd_load_addr = 0, dtb_len,
98 orig_segments = image->nr_segments;
102 /* not allocate anything below the kernel */
103 kbuf.buf_min = kernel_load_addr + kernel_size;
105 /* load elf core header */
106 if (image->type == KEXEC_TYPE_CRASH) {
107 ret = prepare_elf_headers(&headers, &headers_sz);
109 pr_err("Preparing elf core header failed\n");
113 kbuf.buffer = headers;
114 kbuf.bufsz = headers_sz;
115 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
116 kbuf.memsz = headers_sz;
117 kbuf.buf_align = SZ_64K; /* largest supported page size */
118 kbuf.buf_max = ULONG_MAX;
119 kbuf.top_down = true;
121 ret = kexec_add_buffer(&kbuf);
126 image->elf_headers = headers;
127 image->elf_load_addr = kbuf.mem;
128 image->elf_headers_sz = headers_sz;
130 pr_debug("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
131 image->elf_load_addr, kbuf.bufsz, kbuf.memsz);
136 kbuf.buffer = initrd;
137 kbuf.bufsz = initrd_len;
138 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
139 kbuf.memsz = initrd_len;
141 /* within 1GB-aligned window of up to 32GB in size */
142 kbuf.buf_max = round_down(kernel_load_addr, SZ_1G)
143 + (unsigned long)SZ_1G * 32;
144 kbuf.top_down = false;
146 ret = kexec_add_buffer(&kbuf);
149 initrd_load_addr = kbuf.mem;
151 pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
152 initrd_load_addr, kbuf.bufsz, kbuf.memsz);
156 dtb = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
157 initrd_len, cmdline, 0);
159 pr_err("Preparing for new dtb failed\n");
166 dtb_len = fdt_totalsize(dtb);
168 kbuf.bufsz = dtb_len;
169 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
170 kbuf.memsz = dtb_len;
171 /* not across 2MB boundary */
172 kbuf.buf_align = SZ_2M;
173 kbuf.buf_max = ULONG_MAX;
174 kbuf.top_down = true;
176 ret = kexec_add_buffer(&kbuf);
179 image->arch.dtb = dtb;
180 image->arch.dtb_mem = kbuf.mem;
182 pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
183 kbuf.mem, kbuf.bufsz, kbuf.memsz);
188 image->nr_segments = orig_segments;