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;
128 unsigned long start_pfn, end_pfn;
132 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
135 prop = of_get_property(of_chosen, "linux,ima-kexec-buffer", &len);
139 ret = do_get_kexec_buffer(prop, len, &tmp_addr, &tmp_size);
143 /* Do some sanity on the returned size for the ima-kexec buffer */
148 * Calculate the PFNs for the buffer and ensure
149 * they are with in addressable memory.
151 start_pfn = PHYS_PFN(tmp_addr);
152 end_pfn = PHYS_PFN(tmp_addr + tmp_size - 1);
153 if (!page_is_ram(start_pfn) || !page_is_ram(end_pfn)) {
154 pr_warn("IMA buffer at 0x%lx, size = 0x%zx beyond memory\n",
159 *addr = __va(tmp_addr);
166 * ima_free_kexec_buffer - free memory used by the IMA buffer
168 int ima_free_kexec_buffer(void)
173 struct property *prop;
175 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
178 prop = of_find_property(of_chosen, "linux,ima-kexec-buffer", NULL);
182 ret = do_get_kexec_buffer(prop->value, prop->length, &addr, &size);
186 ret = of_remove_property(of_chosen, prop);
190 return memblock_free(addr, size);
195 * remove_ima_buffer - remove the IMA buffer property and reservation from @fdt
197 * @fdt: Flattened Device Tree to update
198 * @chosen_node: Offset to the chosen node in the device tree
200 * The IMA measurement buffer is of no use to a subsequent kernel, so we always
201 * remove it from the device tree.
203 static void remove_ima_buffer(void *fdt, int chosen_node)
210 if (!IS_ENABLED(CONFIG_HAVE_IMA_KEXEC))
213 prop = fdt_getprop(fdt, chosen_node, "linux,ima-kexec-buffer", &len);
217 ret = do_get_kexec_buffer(prop, len, &addr, &size);
218 fdt_delprop(fdt, chosen_node, "linux,ima-kexec-buffer");
222 ret = fdt_find_and_del_mem_rsv(fdt, addr, size);
224 pr_debug("Removed old IMA buffer reservation.\n");
227 #ifdef CONFIG_IMA_KEXEC
229 * setup_ima_buffer - add IMA buffer information to the fdt
230 * @image: kexec image being loaded.
231 * @fdt: Flattened device tree for the next kernel.
232 * @chosen_node: Offset to the chosen node.
234 * Return: 0 on success, or negative errno on error.
236 static int setup_ima_buffer(const struct kimage *image, void *fdt,
241 if (!image->ima_buffer_size)
244 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
245 "linux,ima-kexec-buffer",
246 image->ima_buffer_addr,
247 image->ima_buffer_size);
251 ret = fdt_add_mem_rsv(fdt, image->ima_buffer_addr,
252 image->ima_buffer_size);
256 pr_debug("IMA buffer at 0x%llx, size = 0x%zx\n",
257 image->ima_buffer_addr, image->ima_buffer_size);
261 #else /* CONFIG_IMA_KEXEC */
262 static inline int setup_ima_buffer(const struct kimage *image, void *fdt,
267 #endif /* CONFIG_IMA_KEXEC */
270 * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree
272 * @image: kexec image being loaded.
273 * @initrd_load_addr: Address where the next initrd will be loaded.
274 * @initrd_len: Size of the next initrd, or 0 if there will be none.
275 * @cmdline: Command line for the next kernel, or NULL if there will
277 * @extra_fdt_size: Additional size for the new FDT buffer.
279 * Return: fdt on success, or NULL errno on error.
281 void *of_kexec_alloc_and_setup_fdt(const struct kimage *image,
282 unsigned long initrd_load_addr,
283 unsigned long initrd_len,
284 const char *cmdline, size_t extra_fdt_size)
287 int ret, chosen_node, len;
291 fdt_size = fdt_totalsize(initial_boot_params) +
292 (cmdline ? strlen(cmdline) : 0) +
295 fdt = kvmalloc(fdt_size, GFP_KERNEL);
299 ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
301 pr_err("Error %d setting up the new device tree.\n", ret);
305 /* Remove memory reservation for the current device tree. */
306 ret = fdt_find_and_del_mem_rsv(fdt, __pa(initial_boot_params),
307 fdt_totalsize(initial_boot_params));
308 if (ret == -EINVAL) {
309 pr_err("Error removing memory reservation.\n");
313 chosen_node = fdt_path_offset(fdt, "/chosen");
314 if (chosen_node == -FDT_ERR_NOTFOUND)
315 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
317 if (chosen_node < 0) {
322 ret = fdt_delprop(fdt, chosen_node, "linux,elfcorehdr");
323 if (ret && ret != -FDT_ERR_NOTFOUND)
325 ret = fdt_delprop(fdt, chosen_node, "linux,usable-memory-range");
326 if (ret && ret != -FDT_ERR_NOTFOUND)
329 /* Did we boot using an initrd? */
330 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", &len);
332 u64 tmp_start, tmp_end, tmp_size;
334 tmp_start = of_read_number(prop, len / 4);
336 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", &len);
342 tmp_end = of_read_number(prop, len / 4);
345 * kexec reserves exact initrd size, while firmware may
346 * reserve a multiple of PAGE_SIZE, so check for both.
348 tmp_size = tmp_end - tmp_start;
349 ret = fdt_find_and_del_mem_rsv(fdt, tmp_start, tmp_size);
351 ret = fdt_find_and_del_mem_rsv(fdt, tmp_start,
352 round_up(tmp_size, PAGE_SIZE));
358 if (initrd_load_addr) {
359 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-start",
364 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
365 initrd_load_addr + initrd_len);
369 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
374 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-start");
375 if (ret && (ret != -FDT_ERR_NOTFOUND))
378 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
379 if (ret && (ret != -FDT_ERR_NOTFOUND))
383 if (image->type == KEXEC_TYPE_CRASH) {
384 /* add linux,elfcorehdr */
385 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
386 "linux,elfcorehdr", image->elf_load_addr,
387 image->elf_headers_sz);
392 * Avoid elfcorehdr from being stomped on in kdump kernel by
393 * setting up memory reserve map.
395 ret = fdt_add_mem_rsv(fdt, image->elf_load_addr,
396 image->elf_headers_sz);
400 /* add linux,usable-memory-range */
401 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
402 "linux,usable-memory-range", crashk_res.start,
403 crashk_res.end - crashk_res.start + 1);
407 if (crashk_low_res.end) {
408 ret = fdt_appendprop_addrrange(fdt, 0, chosen_node,
409 "linux,usable-memory-range",
410 crashk_low_res.start,
411 crashk_low_res.end - crashk_low_res.start + 1);
419 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
423 ret = fdt_delprop(fdt, chosen_node, "bootargs");
424 if (ret && (ret != -FDT_ERR_NOTFOUND))
429 ret = fdt_delprop(fdt, chosen_node, "kaslr-seed");
430 if (ret == -FDT_ERR_NOTFOUND)
435 if (rng_is_initialized()) {
436 u64 seed = get_random_u64();
438 ret = fdt_setprop_u64(fdt, chosen_node, "kaslr-seed", seed);
442 pr_notice("RNG is not initialised: omitting \"%s\" property\n",
447 if (rng_is_initialized()) {
450 ret = fdt_setprop_placeholder(fdt, chosen_node, "rng-seed",
451 RNG_SEED_SIZE, &rng_seed);
454 get_random_bytes(rng_seed, RNG_SEED_SIZE);
456 pr_notice("RNG is not initialised: omitting \"%s\" property\n",
460 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
464 remove_ima_buffer(fdt, chosen_node);
465 ret = setup_ima_buffer(image, fdt, fdt_path_offset(fdt, "/chosen"));