1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kexec_file for arm64
4  *
5  * Copyright (C) 2018 Linaro Limited
6  * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7  *
8  * Most code is derived from arm64 port of kexec-tools
9  */
10 
11 #define pr_fmt(fmt) "kexec_file: " fmt
12 
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>
18 #include <linux/of_fdt.h>
19 #include <linux/random.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/types.h>
23 #include <linux/vmalloc.h>
24 #include <asm/byteorder.h>
25 
26 /* relevant device tree properties */
27 #define FDT_PROP_KEXEC_ELFHDR	"linux,elfcorehdr"
28 #define FDT_PROP_MEM_RANGE	"linux,usable-memory-range"
29 #define FDT_PROP_INITRD_START	"linux,initrd-start"
30 #define FDT_PROP_INITRD_END	"linux,initrd-end"
31 #define FDT_PROP_BOOTARGS	"bootargs"
32 #define FDT_PROP_KASLR_SEED	"kaslr-seed"
33 #define FDT_PROP_RNG_SEED	"rng-seed"
34 #define RNG_SEED_SIZE		128
35 
36 const struct kexec_file_ops * const kexec_file_loaders[] = {
37 	&kexec_image_ops,
38 	NULL
39 };
40 
41 int arch_kimage_file_post_load_cleanup(struct kimage *image)
42 {
43 	vfree(image->arch.dtb);
44 	image->arch.dtb = NULL;
45 
46 	vfree(image->arch.elf_headers);
47 	image->arch.elf_headers = NULL;
48 	image->arch.elf_headers_sz = 0;
49 
50 	return kexec_image_post_load_cleanup_default(image);
51 }
52 
53 static int setup_dtb(struct kimage *image,
54 		     unsigned long initrd_load_addr, unsigned long initrd_len,
55 		     char *cmdline, void *dtb)
56 {
57 	int off, ret;
58 
59 	ret = fdt_path_offset(dtb, "/chosen");
60 	if (ret < 0)
61 		goto out;
62 
63 	off = ret;
64 
65 	ret = fdt_delprop(dtb, off, FDT_PROP_KEXEC_ELFHDR);
66 	if (ret && ret != -FDT_ERR_NOTFOUND)
67 		goto out;
68 	ret = fdt_delprop(dtb, off, FDT_PROP_MEM_RANGE);
69 	if (ret && ret != -FDT_ERR_NOTFOUND)
70 		goto out;
71 
72 	if (image->type == KEXEC_TYPE_CRASH) {
73 		/* add linux,elfcorehdr */
74 		ret = fdt_appendprop_addrrange(dtb, 0, off,
75 				FDT_PROP_KEXEC_ELFHDR,
76 				image->arch.elf_headers_mem,
77 				image->arch.elf_headers_sz);
78 		if (ret)
79 			return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
80 
81 		/* add linux,usable-memory-range */
82 		ret = fdt_appendprop_addrrange(dtb, 0, off,
83 				FDT_PROP_MEM_RANGE,
84 				crashk_res.start,
85 				crashk_res.end - crashk_res.start + 1);
86 		if (ret)
87 			return (ret == -FDT_ERR_NOSPACE ? -ENOMEM : -EINVAL);
88 	}
89 
90 	/* add bootargs */
91 	if (cmdline) {
92 		ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
93 		if (ret)
94 			goto out;
95 	} else {
96 		ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
97 		if (ret && (ret != -FDT_ERR_NOTFOUND))
98 			goto out;
99 	}
100 
101 	/* add initrd-* */
102 	if (initrd_load_addr) {
103 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
104 				      initrd_load_addr);
105 		if (ret)
106 			goto out;
107 
108 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
109 				      initrd_load_addr + initrd_len);
110 		if (ret)
111 			goto out;
112 	} else {
113 		ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
114 		if (ret && (ret != -FDT_ERR_NOTFOUND))
115 			goto out;
116 
117 		ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
118 		if (ret && (ret != -FDT_ERR_NOTFOUND))
119 			goto out;
120 	}
121 
122 	/* add kaslr-seed */
123 	ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
124 	if (ret == -FDT_ERR_NOTFOUND)
125 		ret = 0;
126 	else if (ret)
127 		goto out;
128 
129 	if (rng_is_initialized()) {
130 		u64 seed = get_random_u64();
131 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
132 		if (ret)
133 			goto out;
134 	} else {
135 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
136 				FDT_PROP_KASLR_SEED);
137 	}
138 
139 	/* add rng-seed */
140 	if (rng_is_initialized()) {
141 		void *rng_seed;
142 		ret = fdt_setprop_placeholder(dtb, off, FDT_PROP_RNG_SEED,
143 				RNG_SEED_SIZE, &rng_seed);
144 		if (ret)
145 			goto out;
146 		get_random_bytes(rng_seed, RNG_SEED_SIZE);
147 	} else {
148 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
149 				FDT_PROP_RNG_SEED);
150 	}
151 
152 out:
153 	if (ret)
154 		return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL;
155 
156 	return 0;
157 }
158 
159 /*
160  * More space needed so that we can add initrd, bootargs, kaslr-seed,
161  * rng-seed, userable-memory-range and elfcorehdr.
162  */
163 #define DTB_EXTRA_SPACE 0x1000
164 
165 static int create_dtb(struct kimage *image,
166 		      unsigned long initrd_load_addr, unsigned long initrd_len,
167 		      char *cmdline, void **dtb)
168 {
169 	void *buf;
170 	size_t buf_size;
171 	size_t cmdline_len;
172 	int ret;
173 
174 	cmdline_len = cmdline ? strlen(cmdline) : 0;
175 	buf_size = fdt_totalsize(initial_boot_params)
176 			+ cmdline_len + DTB_EXTRA_SPACE;
177 
178 	for (;;) {
179 		buf = vmalloc(buf_size);
180 		if (!buf)
181 			return -ENOMEM;
182 
183 		/* duplicate a device tree blob */
184 		ret = fdt_open_into(initial_boot_params, buf, buf_size);
185 		if (ret) {
186 			vfree(buf);
187 			return -EINVAL;
188 		}
189 
190 		ret = setup_dtb(image, initrd_load_addr, initrd_len,
191 				cmdline, buf);
192 		if (ret) {
193 			vfree(buf);
194 			if (ret == -ENOMEM) {
195 				/* unlikely, but just in case */
196 				buf_size += DTB_EXTRA_SPACE;
197 				continue;
198 			} else {
199 				return ret;
200 			}
201 		}
202 
203 		/* trim it */
204 		fdt_pack(buf);
205 		*dtb = buf;
206 
207 		return 0;
208 	}
209 }
210 
211 static int prepare_elf_headers(void **addr, unsigned long *sz)
212 {
213 	struct crash_mem *cmem;
214 	unsigned int nr_ranges;
215 	int ret;
216 	u64 i;
217 	phys_addr_t start, end;
218 
219 	nr_ranges = 1; /* for exclusion of crashkernel region */
220 	for_each_mem_range(i, &start, &end)
221 		nr_ranges++;
222 
223 	cmem = kmalloc(struct_size(cmem, ranges, nr_ranges), GFP_KERNEL);
224 	if (!cmem)
225 		return -ENOMEM;
226 
227 	cmem->max_nr_ranges = nr_ranges;
228 	cmem->nr_ranges = 0;
229 	for_each_mem_range(i, &start, &end) {
230 		cmem->ranges[cmem->nr_ranges].start = start;
231 		cmem->ranges[cmem->nr_ranges].end = end - 1;
232 		cmem->nr_ranges++;
233 	}
234 
235 	/* Exclude crashkernel region */
236 	ret = crash_exclude_mem_range(cmem, crashk_res.start, crashk_res.end);
237 
238 	if (!ret)
239 		ret =  crash_prepare_elf64_headers(cmem, true, addr, sz);
240 
241 	kfree(cmem);
242 	return ret;
243 }
244 
245 /*
246  * Tries to add the initrd and DTB to the image. If it is not possible to find
247  * valid locations, this function will undo changes to the image and return non
248  * zero.
249  */
250 int load_other_segments(struct kimage *image,
251 			unsigned long kernel_load_addr,
252 			unsigned long kernel_size,
253 			char *initrd, unsigned long initrd_len,
254 			char *cmdline)
255 {
256 	struct kexec_buf kbuf;
257 	void *headers, *dtb = NULL;
258 	unsigned long headers_sz, initrd_load_addr = 0, dtb_len,
259 		      orig_segments = image->nr_segments;
260 	int ret = 0;
261 
262 	kbuf.image = image;
263 	/* not allocate anything below the kernel */
264 	kbuf.buf_min = kernel_load_addr + kernel_size;
265 
266 	/* load elf core header */
267 	if (image->type == KEXEC_TYPE_CRASH) {
268 		ret = prepare_elf_headers(&headers, &headers_sz);
269 		if (ret) {
270 			pr_err("Preparing elf core header failed\n");
271 			goto out_err;
272 		}
273 
274 		kbuf.buffer = headers;
275 		kbuf.bufsz = headers_sz;
276 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
277 		kbuf.memsz = headers_sz;
278 		kbuf.buf_align = SZ_64K; /* largest supported page size */
279 		kbuf.buf_max = ULONG_MAX;
280 		kbuf.top_down = true;
281 
282 		ret = kexec_add_buffer(&kbuf);
283 		if (ret) {
284 			vfree(headers);
285 			goto out_err;
286 		}
287 		image->arch.elf_headers = headers;
288 		image->arch.elf_headers_mem = kbuf.mem;
289 		image->arch.elf_headers_sz = headers_sz;
290 
291 		pr_debug("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
292 			 image->arch.elf_headers_mem, kbuf.bufsz, kbuf.memsz);
293 	}
294 
295 	/* load initrd */
296 	if (initrd) {
297 		kbuf.buffer = initrd;
298 		kbuf.bufsz = initrd_len;
299 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
300 		kbuf.memsz = initrd_len;
301 		kbuf.buf_align = 0;
302 		/* within 1GB-aligned window of up to 32GB in size */
303 		kbuf.buf_max = round_down(kernel_load_addr, SZ_1G)
304 						+ (unsigned long)SZ_1G * 32;
305 		kbuf.top_down = false;
306 
307 		ret = kexec_add_buffer(&kbuf);
308 		if (ret)
309 			goto out_err;
310 		initrd_load_addr = kbuf.mem;
311 
312 		pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
313 				initrd_load_addr, kbuf.bufsz, kbuf.memsz);
314 	}
315 
316 	/* load dtb */
317 	ret = create_dtb(image, initrd_load_addr, initrd_len, cmdline, &dtb);
318 	if (ret) {
319 		pr_err("Preparing for new dtb failed\n");
320 		goto out_err;
321 	}
322 
323 	dtb_len = fdt_totalsize(dtb);
324 	kbuf.buffer = dtb;
325 	kbuf.bufsz = dtb_len;
326 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
327 	kbuf.memsz = dtb_len;
328 	/* not across 2MB boundary */
329 	kbuf.buf_align = SZ_2M;
330 	kbuf.buf_max = ULONG_MAX;
331 	kbuf.top_down = true;
332 
333 	ret = kexec_add_buffer(&kbuf);
334 	if (ret)
335 		goto out_err;
336 	image->arch.dtb = dtb;
337 	image->arch.dtb_mem = kbuf.mem;
338 
339 	pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
340 			kbuf.mem, kbuf.bufsz, kbuf.memsz);
341 
342 	return 0;
343 
344 out_err:
345 	image->nr_segments = orig_segments;
346 	vfree(dtb);
347 	return ret;
348 }
349