1 // SPDX-License-Identifier: GPL-2.0-only
3 * ppc64 code to implement the kexec_file_load syscall
5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
6 * Copyright (C) 2004 IBM Corp.
7 * Copyright (C) 2004,2005 Milton D Miller II, IBM Corporation
8 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
9 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
10 * Copyright (C) 2016 IBM Corporation
12 * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
13 * Heavily modified for the kernel by
14 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
17 #include <linux/slab.h>
18 #include <linux/kexec.h>
19 #include <linux/of_fdt.h>
20 #include <linux/libfdt.h>
23 #define SLAVE_CODE_SIZE 256
25 const struct kexec_file_ops
* const kexec_file_loaders
[] = {
30 int arch_kexec_kernel_image_probe(struct kimage
*image
, void *buf
,
31 unsigned long buf_len
)
33 /* We don't support crash kernels yet. */
34 if (image
->type
== KEXEC_TYPE_CRASH
)
37 return kexec_image_probe_default(image
, buf
, buf_len
);
41 * setup_purgatory - initialize the purgatory's global variables
42 * @image: kexec image.
43 * @slave_code: Slave code for the purgatory.
44 * @fdt: Flattened device tree for the next kernel.
45 * @kernel_load_addr: Address where the kernel is loaded.
46 * @fdt_load_addr: Address where the flattened device tree is loaded.
48 * Return: 0 on success, or negative errno on error.
50 int setup_purgatory(struct kimage
*image
, const void *slave_code
,
51 const void *fdt
, unsigned long kernel_load_addr
,
52 unsigned long fdt_load_addr
)
54 unsigned int *slave_code_buf
, master_entry
;
57 slave_code_buf
= kmalloc(SLAVE_CODE_SIZE
, GFP_KERNEL
);
61 /* Get the slave code from the new kernel and put it in purgatory. */
62 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
63 slave_code_buf
, SLAVE_CODE_SIZE
,
66 kfree(slave_code_buf
);
70 master_entry
= slave_code_buf
[0];
71 memcpy(slave_code_buf
, slave_code
, SLAVE_CODE_SIZE
);
72 slave_code_buf
[0] = master_entry
;
73 ret
= kexec_purgatory_get_set_symbol(image
, "purgatory_start",
74 slave_code_buf
, SLAVE_CODE_SIZE
,
76 kfree(slave_code_buf
);
78 ret
= kexec_purgatory_get_set_symbol(image
, "kernel", &kernel_load_addr
,
79 sizeof(kernel_load_addr
), false);
82 ret
= kexec_purgatory_get_set_symbol(image
, "dt_offset", &fdt_load_addr
,
83 sizeof(fdt_load_addr
), false);
91 * delete_fdt_mem_rsv - delete memory reservation with given address and size
93 * Return: 0 on success, or negative errno on error.
95 int delete_fdt_mem_rsv(void *fdt
, unsigned long start
, unsigned long size
)
97 int i
, ret
, num_rsvs
= fdt_num_mem_rsv(fdt
);
99 for (i
= 0; i
< num_rsvs
; i
++) {
100 uint64_t rsv_start
, rsv_size
;
102 ret
= fdt_get_mem_rsv(fdt
, i
, &rsv_start
, &rsv_size
);
104 pr_err("Malformed device tree.\n");
108 if (rsv_start
== start
&& rsv_size
== size
) {
109 ret
= fdt_del_mem_rsv(fdt
, i
);
111 pr_err("Error deleting device tree reservation.\n");
123 * setup_new_fdt - modify /chosen and memory reservation for the next kernel
124 * @image: kexec image being loaded.
125 * @fdt: Flattened device tree for the next kernel.
126 * @initrd_load_addr: Address where the next initrd will be loaded.
127 * @initrd_len: Size of the next initrd, or 0 if there will be none.
128 * @cmdline: Command line for the next kernel, or NULL if there will
131 * Return: 0 on success, or negative errno on error.
133 int setup_new_fdt(const struct kimage
*image
, void *fdt
,
134 unsigned long initrd_load_addr
, unsigned long initrd_len
,
137 int ret
, chosen_node
;
140 /* Remove memory reservation for the current device tree. */
141 ret
= delete_fdt_mem_rsv(fdt
, __pa(initial_boot_params
),
142 fdt_totalsize(initial_boot_params
));
144 pr_debug("Removed old device tree reservation.\n");
145 else if (ret
!= -ENOENT
)
148 chosen_node
= fdt_path_offset(fdt
, "/chosen");
149 if (chosen_node
== -FDT_ERR_NOTFOUND
) {
150 chosen_node
= fdt_add_subnode(fdt
, fdt_path_offset(fdt
, "/"),
152 if (chosen_node
< 0) {
153 pr_err("Error creating /chosen.\n");
156 } else if (chosen_node
< 0) {
157 pr_err("Malformed device tree: error reading /chosen.\n");
161 /* Did we boot using an initrd? */
162 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-start", NULL
);
164 uint64_t tmp_start
, tmp_end
, tmp_size
;
166 tmp_start
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
168 prop
= fdt_getprop(fdt
, chosen_node
, "linux,initrd-end", NULL
);
170 pr_err("Malformed device tree.\n");
173 tmp_end
= fdt64_to_cpu(*((const fdt64_t
*) prop
));
176 * kexec reserves exact initrd size, while firmware may
177 * reserve a multiple of PAGE_SIZE, so check for both.
179 tmp_size
= tmp_end
- tmp_start
;
180 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
, tmp_size
);
182 ret
= delete_fdt_mem_rsv(fdt
, tmp_start
,
183 round_up(tmp_size
, PAGE_SIZE
));
185 pr_debug("Removed old initrd reservation.\n");
186 else if (ret
!= -ENOENT
)
189 /* If there's no new initrd, delete the old initrd's info. */
190 if (initrd_len
== 0) {
191 ret
= fdt_delprop(fdt
, chosen_node
,
192 "linux,initrd-start");
194 pr_err("Error deleting linux,initrd-start.\n");
198 ret
= fdt_delprop(fdt
, chosen_node
, "linux,initrd-end");
200 pr_err("Error deleting linux,initrd-end.\n");
207 ret
= fdt_setprop_u64(fdt
, chosen_node
,
208 "linux,initrd-start",
213 /* initrd-end is the first address after the initrd image. */
214 ret
= fdt_setprop_u64(fdt
, chosen_node
, "linux,initrd-end",
215 initrd_load_addr
+ initrd_len
);
219 ret
= fdt_add_mem_rsv(fdt
, initrd_load_addr
, initrd_len
);
221 pr_err("Error reserving initrd memory: %s\n",
227 if (cmdline
!= NULL
) {
228 ret
= fdt_setprop_string(fdt
, chosen_node
, "bootargs", cmdline
);
232 ret
= fdt_delprop(fdt
, chosen_node
, "bootargs");
233 if (ret
&& ret
!= -FDT_ERR_NOTFOUND
) {
234 pr_err("Error deleting bootargs.\n");
239 ret
= setup_ima_buffer(image
, fdt
, chosen_node
);
241 pr_err("Error setting up the new device tree.\n");
245 ret
= fdt_setprop(fdt
, chosen_node
, "linux,booted-from-kexec", NULL
, 0);
252 pr_err("Error setting up the new device tree.\n");