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/of_fdt.h>
28 #include <linux/libfdt.h>
31 #define SLAVE_CODE_SIZE 256
33 const struct kexec_file_ops * const kexec_file_loaders[] = {
38 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
39 unsigned long buf_len)
41 /* We don't support crash kernels yet. */
42 if (image->type == KEXEC_TYPE_CRASH)
45 return kexec_image_probe_default(image, buf, buf_len);
49 * setup_purgatory - initialize the purgatory's global variables
50 * @image: kexec image.
51 * @slave_code: Slave code for the purgatory.
52 * @fdt: Flattened device tree for the next kernel.
53 * @kernel_load_addr: Address where the kernel is loaded.
54 * @fdt_load_addr: Address where the flattened device tree is loaded.
56 * Return: 0 on success, or negative errno on error.
58 int setup_purgatory(struct kimage *image, const void *slave_code,
59 const void *fdt, unsigned long kernel_load_addr,
60 unsigned long fdt_load_addr)
62 unsigned int *slave_code_buf, master_entry;
65 slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
69 /* Get the slave code from the new kernel and put it in purgatory. */
70 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
71 slave_code_buf, SLAVE_CODE_SIZE,
74 kfree(slave_code_buf);
78 master_entry = slave_code_buf[0];
79 memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
80 slave_code_buf[0] = master_entry;
81 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
82 slave_code_buf, SLAVE_CODE_SIZE,
84 kfree(slave_code_buf);
86 ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
87 sizeof(kernel_load_addr), false);
90 ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
91 sizeof(fdt_load_addr), false);
99 * delete_fdt_mem_rsv - delete memory reservation with given address and size
101 * Return: 0 on success, or negative errno on error.
103 int delete_fdt_mem_rsv(void *fdt, unsigned long start, unsigned long size)
105 int i, ret, num_rsvs = fdt_num_mem_rsv(fdt);
107 for (i = 0; i < num_rsvs; i++) {
108 uint64_t rsv_start, rsv_size;
110 ret = fdt_get_mem_rsv(fdt, i, &rsv_start, &rsv_size);
112 pr_err("Malformed device tree.\n");
116 if (rsv_start == start && rsv_size == size) {
117 ret = fdt_del_mem_rsv(fdt, i);
119 pr_err("Error deleting device tree reservation.\n");
131 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
132 * @image: kexec image being loaded.
133 * @fdt: Flattened device tree for the next kernel.
134 * @initrd_load_addr: Address where the next initrd will be loaded.
135 * @initrd_len: Size of the next initrd, or 0 if there will be none.
136 * @cmdline: Command line for the next kernel, or NULL if there will
139 * Return: 0 on success, or negative errno on error.
141 int setup_new_fdt(const struct kimage *image, void *fdt,
142 unsigned long initrd_load_addr, unsigned long initrd_len,
145 int ret, chosen_node;
148 /* Remove memory reservation for the current device tree. */
149 ret = delete_fdt_mem_rsv(fdt, __pa(initial_boot_params),
150 fdt_totalsize(initial_boot_params));
152 pr_debug("Removed old device tree reservation.\n");
153 else if (ret != -ENOENT)
156 chosen_node = fdt_path_offset(fdt, "/chosen");
157 if (chosen_node == -FDT_ERR_NOTFOUND) {
158 chosen_node = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
160 if (chosen_node < 0) {
161 pr_err("Error creating /chosen.\n");
164 } else if (chosen_node < 0) {
165 pr_err("Malformed device tree: error reading /chosen.\n");
169 /* Did we boot using an initrd? */
170 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-start", NULL);
172 uint64_t tmp_start, tmp_end, tmp_size;
174 tmp_start = fdt64_to_cpu(*((const fdt64_t *) prop));
176 prop = fdt_getprop(fdt, chosen_node, "linux,initrd-end", NULL);
178 pr_err("Malformed device tree.\n");
181 tmp_end = fdt64_to_cpu(*((const fdt64_t *) prop));
184 * kexec reserves exact initrd size, while firmware may
185 * reserve a multiple of PAGE_SIZE, so check for both.
187 tmp_size = tmp_end - tmp_start;
188 ret = delete_fdt_mem_rsv(fdt, tmp_start, tmp_size);
190 ret = delete_fdt_mem_rsv(fdt, tmp_start,
191 round_up(tmp_size, PAGE_SIZE));
193 pr_debug("Removed old initrd reservation.\n");
194 else if (ret != -ENOENT)
197 /* If there's no new initrd, delete the old initrd's info. */
198 if (initrd_len == 0) {
199 ret = fdt_delprop(fdt, chosen_node,
200 "linux,initrd-start");
202 pr_err("Error deleting linux,initrd-start.\n");
206 ret = fdt_delprop(fdt, chosen_node, "linux,initrd-end");
208 pr_err("Error deleting linux,initrd-end.\n");
215 ret = fdt_setprop_u64(fdt, chosen_node,
216 "linux,initrd-start",
221 /* initrd-end is the first address after the initrd image. */
222 ret = fdt_setprop_u64(fdt, chosen_node, "linux,initrd-end",
223 initrd_load_addr + initrd_len);
227 ret = fdt_add_mem_rsv(fdt, initrd_load_addr, initrd_len);
229 pr_err("Error reserving initrd memory: %s\n",
235 if (cmdline != NULL) {
236 ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline);
240 ret = fdt_delprop(fdt, chosen_node, "bootargs");
241 if (ret && ret != -FDT_ERR_NOTFOUND) {
242 pr_err("Error deleting bootargs.\n");
247 ret = setup_ima_buffer(image, fdt, chosen_node);
249 pr_err("Error setting up the new device tree.\n");
253 ret = fdt_setprop(fdt, chosen_node, "linux,booted-from-kexec", NULL, 0);
260 pr_err("Error setting up the new device tree.\n");