2 * ppc64 code to implement the kexec_file_load syscall
4 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
5 * Copyright (C) 2004 IBM Corp.
6 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
7 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
8 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
9 * Copyright (C) 2016 IBM Corporation
11 * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
12 * Heavily modified for the kernel by
13 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation (version 2 of the License).
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
25 #include <linux/slab.h>
26 #include <linux/kexec.h>
27 #include <linux/memblock.h>
28 #include <linux/of_fdt.h>
29 #include <linux/libfdt.h>
32 #define SLAVE_CODE_SIZE 256
34 const struct kexec_file_ops * const kexec_file_loaders[] = {
39 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
40 unsigned long buf_len)
42 /* We don't support crash kernels yet. */
43 if (image->type == KEXEC_TYPE_CRASH)
46 return kexec_image_probe_default(image, buf, buf_len);
50 * arch_kexec_walk_mem - call func(data) for each unreserved memory block
51 * @kbuf: Context info for the search. Also passed to @func.
52 * @func: Function to call for each memory block.
54 * This function is used by kexec_add_buffer and kexec_locate_mem_hole
55 * to find unreserved memory to load kexec segments into.
57 * Return: The memory walk will stop when func returns a non-zero value
58 * and that value will be returned. If all free regions are visited without
59 * func returning non-zero, then zero will be returned.
61 int arch_kexec_walk_mem(struct kexec_buf *kbuf,
62 int (*func)(struct resource *, void *))
66 phys_addr_t mstart, mend;
67 struct resource res = { };
70 for_each_free_mem_range_reverse(i, NUMA_NO_NODE, 0,
71 &mstart, &mend, NULL) {
73 * In memblock, end points to the first byte after the
74 * range while in kexec, end points to the last byte
79 ret = func(&res, kbuf);
84 for_each_free_mem_range(i, NUMA_NO_NODE, 0, &mstart, &mend,
87 * In memblock, end points to the first byte after the
88 * range while in kexec, end points to the last byte
93 ret = func(&res, kbuf);
103 * setup_purgatory - initialize the purgatory's global variables
104 * @image: kexec image.
105 * @slave_code: Slave code for the purgatory.
106 * @fdt: Flattened device tree for the next kernel.
107 * @kernel_load_addr: Address where the kernel is loaded.
108 * @fdt_load_addr: Address where the flattened device tree is loaded.
110 * Return: 0 on success, or negative errno on error.
112 int setup_purgatory(struct kimage *image, const void *slave_code,
113 const void *fdt, unsigned long kernel_load_addr,
114 unsigned long fdt_load_addr)
116 unsigned int *slave_code_buf, master_entry;
119 slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
123 /* Get the slave code from the new kernel and put it in purgatory. */
124 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
125 slave_code_buf, SLAVE_CODE_SIZE,
128 kfree(slave_code_buf);
132 master_entry = slave_code_buf[0];
133 memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
134 slave_code_buf[0] = master_entry;
135 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
136 slave_code_buf, SLAVE_CODE_SIZE,
138 kfree(slave_code_buf);
140 ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
141 sizeof(kernel_load_addr), false);
144 ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
145 sizeof(fdt_load_addr), false);
153 * delete_fdt_mem_rsv - delete memory reservation with given address and size
155 * Return: 0 on success, or negative errno on error.
157 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
159 int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
161 for (i = 0; i < num_rsvs; i++) {
162 uint64_t rsv_start, rsv_size;
164 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
166 pr_err("Malformed device tree.\n");
170 if (rsv_start == start && rsv_size == size) {
171 ret = fdt_del_mem_rsv(fdt, i);
173 pr_err("Error deleting device tree reservation.\n");
185 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
186 * @image: kexec image being loaded.
187 * @fdt: Flattened device tree for the next kernel.
188 * @initrd_load_addr: Address where the next initrd will be loaded.
189 * @initrd_len: Size of the next initrd, or 0 if there will be none.
190 * @cmdline: Command line for the next kernel, or NULL if there will
193 * Return: 0 on success, or negative errno on error.
195 int setup_new_fdt(const struct kimage *image, void *fdt,
196 unsigned long initrd_load_addr, unsigned long initrd_len,
199 int ret, chosen_node;
202 /* Remove memory reservation for the current device tree. */
203 ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
204 fdt_totalsize(initial_boot_params));
206 pr_debug("Removed old device tree reservation.\n");
207 else if (ret != -ENOENT)
210 chosen_node = fdt_path_offset(fdt, "/chosen");
211 if (chosen_node == -FDT_ERR_NOTFOUND) {
212 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
214 if (chosen_node < 0) {
215 pr_err("Error creating /chosen.\n");
218 } else if (chosen_node < 0) {
219 pr_err("Malformed device tree: error reading /chosen.\n");
223 /* Did we boot using an initrd? */
224 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
226 uint64_t tmp_start, tmp_end, tmp_size;
228 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
230 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
232 pr_err("Malformed device tree.\n");
235 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
238 * kexec reserves exact initrd size, while firmware may
239 * reserve a multiple of PAGE_SIZE, so check for both.
241 tmp_size = tmp_end - tmp_start;
242 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
244 ret = delete_fdt_mem_rsv(fdt, tmp_start,
245 round_up(tmp_size, PAGE_SIZE));
247 pr_debug("Removed old initrd reservation.\n");
248 else if (ret != -ENOENT)
251 /* If there's no new initrd, delete the old initrd's info. */
252 if (initrd_len == 0) {
253 ret = fdt_delprop(fdt, chosen_node,
254 "linux,initrd-start");
256 pr_err("Error deleting linux,initrd-start.\n");
260 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
262 pr_err("Error deleting linux,initrd-end.\n");
269 ret = fdt_setprop_u64(fdt, chosen_node,
270 "linux,initrd-start",
275 /* initrd-end is the first address after the initrd image. */
276 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
277 initrd_load_addr + initrd_len);
281 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
283 pr_err("Error reserving initrd memory: %s\n",
289 if (cmdline != NULL) {
290 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
294 ret = fdt_delprop(fdt, chosen_node, "bootargs");
295 if (ret && ret != -FDT_ERR_NOTFOUND) {
296 pr_err("Error deleting bootargs.\n");
301 ret = setup_ima_buffer(image, fdt, chosen_node);
303 pr_err("Error setting up the new device tree.\n");
307 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
314 pr_err("Error setting up the new device tree.\n");