Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * powerpc code to implement the kexec_file_load syscall
0004  *
0005  * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
0006  * Copyright (C) 2004  IBM Corp.
0007  * Copyright (C) 2004,2005  Milton D Miller II, IBM Corporation
0008  * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
0009  * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
0010  * Copyright (C) 2016  IBM Corporation
0011  *
0012  * Based on kexec-tools' kexec-elf-ppc64.c, fs2dt.c.
0013  * Heavily modified for the kernel by
0014  * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
0015  */
0016 
0017 #include <linux/slab.h>
0018 #include <linux/kexec.h>
0019 #include <linux/of_fdt.h>
0020 #include <linux/libfdt.h>
0021 #include <asm/setup.h>
0022 
0023 #define SLAVE_CODE_SIZE     256 /* First 0x100 bytes */
0024 
0025 /**
0026  * setup_kdump_cmdline - Prepend "elfcorehdr=<addr> " to command line
0027  *                       of kdump kernel for exporting the core.
0028  * @image:               Kexec image
0029  * @cmdline:             Command line parameters to update.
0030  * @cmdline_len:         Length of the cmdline parameters.
0031  *
0032  * kdump segment must be setup before calling this function.
0033  *
0034  * Returns new cmdline buffer for kdump kernel on success, NULL otherwise.
0035  */
0036 char *setup_kdump_cmdline(struct kimage *image, char *cmdline,
0037               unsigned long cmdline_len)
0038 {
0039     int elfcorehdr_strlen;
0040     char *cmdline_ptr;
0041 
0042     cmdline_ptr = kzalloc(COMMAND_LINE_SIZE, GFP_KERNEL);
0043     if (!cmdline_ptr)
0044         return NULL;
0045 
0046     elfcorehdr_strlen = sprintf(cmdline_ptr, "elfcorehdr=0x%lx ",
0047                     image->elf_load_addr);
0048 
0049     if (elfcorehdr_strlen + cmdline_len > COMMAND_LINE_SIZE) {
0050         pr_err("Appending elfcorehdr=<addr> exceeds cmdline size\n");
0051         kfree(cmdline_ptr);
0052         return NULL;
0053     }
0054 
0055     memcpy(cmdline_ptr + elfcorehdr_strlen, cmdline, cmdline_len);
0056     // Ensure it's nul terminated
0057     cmdline_ptr[COMMAND_LINE_SIZE - 1] = '\0';
0058     return cmdline_ptr;
0059 }
0060 
0061 /**
0062  * setup_purgatory - initialize the purgatory's global variables
0063  * @image:      kexec image.
0064  * @slave_code:     Slave code for the purgatory.
0065  * @fdt:        Flattened device tree for the next kernel.
0066  * @kernel_load_addr:   Address where the kernel is loaded.
0067  * @fdt_load_addr:  Address where the flattened device tree is loaded.
0068  *
0069  * Return: 0 on success, or negative errno on error.
0070  */
0071 int setup_purgatory(struct kimage *image, const void *slave_code,
0072             const void *fdt, unsigned long kernel_load_addr,
0073             unsigned long fdt_load_addr)
0074 {
0075     unsigned int *slave_code_buf, master_entry;
0076     int ret;
0077 
0078     slave_code_buf = kmalloc(SLAVE_CODE_SIZE, GFP_KERNEL);
0079     if (!slave_code_buf)
0080         return -ENOMEM;
0081 
0082     /* Get the slave code from the new kernel and put it in purgatory. */
0083     ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
0084                          slave_code_buf, SLAVE_CODE_SIZE,
0085                          true);
0086     if (ret) {
0087         kfree(slave_code_buf);
0088         return ret;
0089     }
0090 
0091     master_entry = slave_code_buf[0];
0092     memcpy(slave_code_buf, slave_code, SLAVE_CODE_SIZE);
0093     slave_code_buf[0] = master_entry;
0094     ret = kexec_purgatory_get_set_symbol(image, "purgatory_start",
0095                          slave_code_buf, SLAVE_CODE_SIZE,
0096                          false);
0097     kfree(slave_code_buf);
0098 
0099     ret = kexec_purgatory_get_set_symbol(image, "kernel", &kernel_load_addr,
0100                          sizeof(kernel_load_addr), false);
0101     if (ret)
0102         return ret;
0103     ret = kexec_purgatory_get_set_symbol(image, "dt_offset", &fdt_load_addr,
0104                          sizeof(fdt_load_addr), false);
0105     if (ret)
0106         return ret;
0107 
0108     return 0;
0109 }