1 // SPDX-License-Identifier: GPL-2.0-only
3 * powerpc 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>
21 #include <asm/setup.h>
23 #define SLAVE_CODE_SIZE 256 /* First 0x100 bytes */
26 * setup_kdump_cmdline - Prepend "elfcorehdr=<addr> " to command line
27 * of kdump kernel for exporting the core.
29 * @cmdline: Command line parameters to update.
30 * @cmdline_len: Length of the cmdline parameters.
32 * kdump segment must be setup before calling this function.
34 * Returns new cmdline buffer for kdump kernel on success, NULL otherwise.
36 char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
37 unsigned long cmdline_len)
39 int elfcorehdr_strlen;
42 cmdline_ptr = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
46 elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
47 image->elf_load_addr);
49 if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
50 pr_err("Appending elfcorehdr=<addr> exceeds cmdline size\n");
55 memcpy(cmdline_ptr + elfcorehdr_strlen, cmdline, cmdline_len);
56 // Ensure it's nul terminated
57 cmdline_ptr[COMMAND_LINE_SIZE - 1] = '\0';
62 * setup_purgatory - initialize the purgatory's global variables
63 * @image: kexec image.
64 * @slave_code: Slave code for the purgatory.
65 * @fdt: Flattened device tree for the next kernel.
66 * @kernel_load_addr: Address where the kernel is loaded.
67 * @fdt_load_addr: Address where the flattened device tree is loaded.
69 * Return: 0 on success, or negative errno on error.
71 int setup_purgatory(struct kimage *image, const void *slave_code,
72 const void *fdt, unsigned long kernel_load_addr,
73 unsigned long fdt_load_addr)
75 unsigned int *slave_code_buf, master_entry;
78 slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
82 /* Get the slave code from the new kernel and put it in purgatory. */
83 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
84 slave_code_buf, SLAVE_CODE_SIZE,
87 kfree(slave_code_buf);
91 master_entry = slave_code_buf[0];
92 memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
93 slave_code_buf[0] = master_entry;
94 ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
95 slave_code_buf, SLAVE_CODE_SIZE,
97 kfree(slave_code_buf);
99 ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
100 sizeof(kernel_load_addr), false);
103 ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
104 sizeof(fdt_load_addr), false);