1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 Arm Limited
5 * Based on arch/arm64/kernel/machine_kexec_file.c:
6 * Copyright (C) 2018 Linaro Limited
8 * And arch/powerpc/kexec/file_load.c:
9 * Copyright (C) 2016 IBM Corporation
12 #include <linux/kernel.h>
13 #include <linux/kexec.h>
14 #include <linux/memblock.h>
15 #include <linux/libfdt.h>
17 #include <linux/of_fdt.h>
18 #include <linux/random.h>
19 #include <linux/types.h>
21 #define RNG_SEED_SIZE 128
24 * Additional space needed for the FDT buffer so that we can add initrd,
25 * bootargs, kaslr-seed, rng-seed, useable-memory-range and elfcorehdr.
27 #define FDT_EXTRA_SPACE 0x1000
30 * fdt_find_and_del_mem_rsv - delete memory reservation with given address and size
32 * @fdt: Flattened device tree for the current kernel.
33 * @start: Starting address of the reserved memory.
34 * @size: Size of the reserved memory.
36 * Return: 0 on success, or negative errno on error.
38 static int fdt_find_and_del_mem_rsv(void *fdt, unsigned long start, unsigned long size)
40 int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
42 for (i = 0; i < num_rsvs; i++) {
43 u64 rsv_start, rsv_size;
45 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
47 pr_err("Malformed device tree.\n");
51 if (rsv_start == start && rsv_size == size) {
52 ret = fdt_del_mem_rsv(fdt, i);
54 pr_err("Error deleting device tree reservation.\n");
66 * get_addr_size_cells - Get address and size of root node
68 * @addr_cells: Return address of the root node
69 * @size_cells: Return size of the root node
71 * Return: 0 on success, or negative errno on error.
73 static int get_addr_size_cells(int *addr_cells, int *size_cells)
75 struct device_node *root;
77 root = of_find_node_by_path("/");
81 *addr_cells = of_n_addr_cells(root);
82 *size_cells = of_n_size_cells(root);
90 * do_get_kexec_buffer - Get address and size of device tree property
92 * @prop: Device tree property
94 * @addr: Return address of the node
95 * @size: Return size of the node
97 * Return: 0 on success, or negative errno on error.
99 static int do_get_kexec_buffer(const void *prop, int len, unsigned long *addr,
102 int ret, addr_cells, size_cells;
104 ret = get_addr_size_cells(&addr_cells, &size_cells);
108 if (len < 4 * (addr_cells + size_cells))
111 *addr = of_read_number(prop, addr_cells);
112 *size = of_read_number(prop + 4 * addr_cells, size_cells);
118 * ima_get_kexec_buffer - get IMA buffer from the previous kernel
119 * @addr: On successful return, set to point to the buffer contents.
120 * @size: On successful return, set to the buffer size.
122 * Return: 0 on success, negative errno on error.
124 int ima_get_kexec_buffer(void **addr, size_t *size)
127 unsigned long tmp_addr;
131 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
134 prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", &len);
138 ret = do_get_kexec_buffer(prop, len, &tmp_addr, &tmp_size);
142 *addr = __va(tmp_addr);
149 * ima_free_kexec_buffer - free memory used by the IMA buffer
151 int ima_free_kexec_buffer(void)
156 struct property *prop;
158 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
161 prop = of_find_property(of_chosen, "linux,ima-kexec-buffer", NULL);
165 ret = do_get_kexec_buffer(prop->value, prop->length, &addr, &size);
169 ret = of_remove_property(of_chosen, prop);
173 return memblock_free(addr, size);
178 * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt
180 * @fdt: Flattened Device Tree to update
181 * @chosen_node: Offset to the chosen node in the device tree
183 * The IMA measurement buffer is of no use to a subsequent kernel, so we always
184 * remove it from the device tree.
186 static void remove_ima_buffer(void *fdt, int chosen_node)
193 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
196 prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len);
200 ret = do_get_kexec_buffer(prop, len, &addr, &size);
201 fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer");
205 ret = fdt_find_and_del_mem_rsv(fdt, addr, size);
207 pr_debug("Removed old IMA buffer reservation.\n");
210 #ifdef CONFIG_IMA_KEXEC
212 * setup_ima_buffer - add IMA buffer information to the fdt
213 * @image: kexec image being loaded.
214 * @fdt: Flattened device tree for the next kernel.
215 * @chosen_node: Offset to the chosen node.
217 * Return: 0 on success, or negative errno on error.
219 static int setup_ima_buffer(const struct kimage *image, void *fdt,
224 if (!image->ima_buffer_size)
227 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
228 "linux,ima-kexec-buffer",
229 image->ima_buffer_addr,
230 image->ima_buffer_size);
234 ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr,
235 image->ima_buffer_size);
239 pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n",
240 image->ima_buffer_addr, image->ima_buffer_size);
244 #else /* CONFIG_IMA_KEXEC */
245 static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
250 #endif /* CONFIG_IMA_KEXEC */
253 * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
255 * @image: kexec image being loaded.
256 * @initrd_load_addr: Address where the next initrd will be loaded.
257 * @initrd_len: Size of the next initrd, or 0 if there will be none.
258 * @cmdline: Command line for the next kernel, or NULL if there will
260 * @extra_fdt_size: Additional size for the new FDT buffer.
262 * Return: fdt on success, or NULL errno on error.
264 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
265 unsigned long initrd_load_addr,
266 unsigned long initrd_len,
267 const char *cmdline, size_t extra_fdt_size)
270 int ret, chosen_node;
274 fdt_size = fdt_totalsize(initial_boot_params) +
275 (cmdline ? strlen(cmdline) : 0) +
278 fdt = kvmalloc(fdt_size, GFP_KERNEL);
282 ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
284 pr_err("Error %d setting up the new device tree.\n", ret);
288 /* Remove memory reservation for the current device tree. */
289 ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
290 fdt_totalsize(initial_boot_params));
291 if (ret == -EINVAL) {
292 pr_err("Error removing memory reservation.\n");
296 chosen_node = fdt_path_offset(fdt, "/chosen");
297 if (chosen_node == -FDT_ERR_NOTFOUND)
298 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
300 if (chosen_node < 0) {
305 ret = fdt_delprop(fdt, chosen_node, "linux,elfcorehdr");
306 if (ret && ret != -FDT_ERR_NOTFOUND)
308 ret = fdt_delprop(fdt, chosen_node, "linux,usable-memory-range");
309 if (ret && ret != -FDT_ERR_NOTFOUND)
312 /* Did we boot using an initrd? */
313 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
315 u64 tmp_start, tmp_end, tmp_size;
317 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
319 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
325 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
328 * kexec reserves exact initrd size, while firmware may
329 * reserve a multiple of PAGE_SIZE, so check for both.
331 tmp_size = tmp_end - tmp_start;
332 ret = fdt_find_and_del_mem_rsv(fdt, tmp_start, tmp_size);
334 ret = fdt_find_and_del_mem_rsv(fdt, tmp_start,
335 round_up(tmp_size, PAGE_SIZE));
341 if (initrd_load_addr) {
342 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-start",
347 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
348 initrd_load_addr + initrd_len);
352 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
357 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-start");
358 if (ret && (ret != -FDT_ERR_NOTFOUND))
361 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
362 if (ret && (ret != -FDT_ERR_NOTFOUND))
366 if (image->type == KEXEC_TYPE_CRASH) {
367 /* add linux,elfcorehdr */
368 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
369 "linux,elfcorehdr", image->elf_load_addr,
370 image->elf_headers_sz);
375 * Avoid elfcorehdr from being stomped on in kdump kernel by
376 * setting up memory reserve map.
378 ret = fdt_add_mem_rsv(fdt, image->elf_load_addr,
379 image->elf_headers_sz);
383 /* add linux,usable-memory-range */
384 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
385 "linux,usable-memory-range", crashk_res.start,
386 crashk_res.end - crashk_res.start + 1);
393 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
397 ret = fdt_delprop(fdt, chosen_node, "bootargs");
398 if (ret && (ret != -FDT_ERR_NOTFOUND))
403 ret = fdt_delprop(fdt, chosen_node, "kaslr-seed");
404 if (ret == -FDT_ERR_NOTFOUND)
409 if (rng_is_initialized()) {
410 u64 seed = get_random_u64();
412 ret = fdt_setprop_u64(fdt, chosen_node, "kaslr-seed", seed);
416 pr_notice("RNG is not initialised: omitting \"%s\" property\n",
421 if (rng_is_initialized()) {
424 ret = fdt_setprop_placeholder(fdt, chosen_node, "rng-seed",
425 RNG_SEED_SIZE, &rng_seed);
428 get_random_bytes(rng_seed, RNG_SEED_SIZE);
430 pr_notice("RNG is not initialised: omitting \"%s\" property\n",
434 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
438 remove_ima_buffer(fdt, chosen_node);
439 ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen"));