18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Load ELF vmlinux file for the kexec_file_load syscall.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004  Adam Litke (agl@us.ibm.com)
68c2ecf20Sopenharmony_ci * Copyright (C) 2004  IBM Corp.
78c2ecf20Sopenharmony_ci * Copyright (C) 2005  R Sharada (sharada@in.ibm.com)
88c2ecf20Sopenharmony_ci * Copyright (C) 2006  Mohan Kumar M (mohan@in.ibm.com)
98c2ecf20Sopenharmony_ci * Copyright (C) 2016  IBM Corporation
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
128c2ecf20Sopenharmony_ci * Heavily modified for the kernel by
138c2ecf20Sopenharmony_ci * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define pr_fmt(fmt)	"kexec_elf: " fmt
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/elf.h>
198c2ecf20Sopenharmony_ci#include <linux/kexec.h>
208c2ecf20Sopenharmony_ci#include <linux/libfdt.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/of_fdt.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/types.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic void *elf64_load(struct kimage *image, char *kernel_buf,
278c2ecf20Sopenharmony_ci			unsigned long kernel_len, char *initrd,
288c2ecf20Sopenharmony_ci			unsigned long initrd_len, char *cmdline,
298c2ecf20Sopenharmony_ci			unsigned long cmdline_len)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	int ret;
328c2ecf20Sopenharmony_ci	unsigned int fdt_size;
338c2ecf20Sopenharmony_ci	unsigned long kernel_load_addr;
348c2ecf20Sopenharmony_ci	unsigned long initrd_load_addr = 0, fdt_load_addr;
358c2ecf20Sopenharmony_ci	void *fdt;
368c2ecf20Sopenharmony_ci	const void *slave_code;
378c2ecf20Sopenharmony_ci	struct elfhdr ehdr;
388c2ecf20Sopenharmony_ci	char *modified_cmdline = NULL;
398c2ecf20Sopenharmony_ci	struct kexec_elf_info elf_info;
408c2ecf20Sopenharmony_ci	struct kexec_buf kbuf = { .image = image, .buf_min = 0,
418c2ecf20Sopenharmony_ci				  .buf_max = ppc64_rma_size };
428c2ecf20Sopenharmony_ci	struct kexec_buf pbuf = { .image = image, .buf_min = 0,
438c2ecf20Sopenharmony_ci				  .buf_max = ppc64_rma_size, .top_down = true,
448c2ecf20Sopenharmony_ci				  .mem = KEXEC_BUF_MEM_UNKNOWN };
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
478c2ecf20Sopenharmony_ci	if (ret)
488c2ecf20Sopenharmony_ci		goto out;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	if (image->type == KEXEC_TYPE_CRASH) {
518c2ecf20Sopenharmony_ci		/* min & max buffer values for kdump case */
528c2ecf20Sopenharmony_ci		kbuf.buf_min = pbuf.buf_min = crashk_res.start;
538c2ecf20Sopenharmony_ci		kbuf.buf_max = pbuf.buf_max =
548c2ecf20Sopenharmony_ci				((crashk_res.end < ppc64_rma_size) ?
558c2ecf20Sopenharmony_ci				 crashk_res.end : (ppc64_rma_size - 1));
568c2ecf20Sopenharmony_ci	}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
598c2ecf20Sopenharmony_ci	if (ret)
608c2ecf20Sopenharmony_ci		goto out;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	ret = kexec_load_purgatory(image, &pbuf);
658c2ecf20Sopenharmony_ci	if (ret) {
668c2ecf20Sopenharmony_ci		pr_err("Loading purgatory failed.\n");
678c2ecf20Sopenharmony_ci		goto out;
688c2ecf20Sopenharmony_ci	}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* Load additional segments needed for panic kernel */
738c2ecf20Sopenharmony_ci	if (image->type == KEXEC_TYPE_CRASH) {
748c2ecf20Sopenharmony_ci		ret = load_crashdump_segments_ppc64(image, &kbuf);
758c2ecf20Sopenharmony_ci		if (ret) {
768c2ecf20Sopenharmony_ci			pr_err("Failed to load kdump kernel segments\n");
778c2ecf20Sopenharmony_ci			goto out;
788c2ecf20Sopenharmony_ci		}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		/* Setup cmdline for kdump kernel case */
818c2ecf20Sopenharmony_ci		modified_cmdline = setup_kdump_cmdline(image, cmdline,
828c2ecf20Sopenharmony_ci						       cmdline_len);
838c2ecf20Sopenharmony_ci		if (!modified_cmdline) {
848c2ecf20Sopenharmony_ci			pr_err("Setting up cmdline for kdump kernel failed\n");
858c2ecf20Sopenharmony_ci			ret = -EINVAL;
868c2ecf20Sopenharmony_ci			goto out;
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci		cmdline = modified_cmdline;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (initrd != NULL) {
928c2ecf20Sopenharmony_ci		kbuf.buffer = initrd;
938c2ecf20Sopenharmony_ci		kbuf.bufsz = kbuf.memsz = initrd_len;
948c2ecf20Sopenharmony_ci		kbuf.buf_align = PAGE_SIZE;
958c2ecf20Sopenharmony_ci		kbuf.top_down = false;
968c2ecf20Sopenharmony_ci		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
978c2ecf20Sopenharmony_ci		ret = kexec_add_buffer(&kbuf);
988c2ecf20Sopenharmony_ci		if (ret)
998c2ecf20Sopenharmony_ci			goto out;
1008c2ecf20Sopenharmony_ci		initrd_load_addr = kbuf.mem;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci		pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	fdt_size = kexec_fdt_totalsize_ppc64(image);
1068c2ecf20Sopenharmony_ci	fdt = kmalloc(fdt_size, GFP_KERNEL);
1078c2ecf20Sopenharmony_ci	if (!fdt) {
1088c2ecf20Sopenharmony_ci		pr_err("Not enough memory for the device tree.\n");
1098c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1108c2ecf20Sopenharmony_ci		goto out;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci	ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
1138c2ecf20Sopenharmony_ci	if (ret < 0) {
1148c2ecf20Sopenharmony_ci		pr_err("Error setting up the new device tree.\n");
1158c2ecf20Sopenharmony_ci		ret = -EINVAL;
1168c2ecf20Sopenharmony_ci		goto out;
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
1208c2ecf20Sopenharmony_ci				  initrd_len, cmdline);
1218c2ecf20Sopenharmony_ci	if (ret)
1228c2ecf20Sopenharmony_ci		goto out;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	fdt_pack(fdt);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	kbuf.buffer = fdt;
1278c2ecf20Sopenharmony_ci	kbuf.bufsz = kbuf.memsz = fdt_size;
1288c2ecf20Sopenharmony_ci	kbuf.buf_align = PAGE_SIZE;
1298c2ecf20Sopenharmony_ci	kbuf.top_down = true;
1308c2ecf20Sopenharmony_ci	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
1318c2ecf20Sopenharmony_ci	ret = kexec_add_buffer(&kbuf);
1328c2ecf20Sopenharmony_ci	if (ret)
1338c2ecf20Sopenharmony_ci		goto out;
1348c2ecf20Sopenharmony_ci	fdt_load_addr = kbuf.mem;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
1398c2ecf20Sopenharmony_ci	ret = setup_purgatory_ppc64(image, slave_code, fdt, kernel_load_addr,
1408c2ecf20Sopenharmony_ci				    fdt_load_addr);
1418c2ecf20Sopenharmony_ci	if (ret)
1428c2ecf20Sopenharmony_ci		pr_err("Error setting up the purgatory.\n");
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciout:
1458c2ecf20Sopenharmony_ci	kfree(modified_cmdline);
1468c2ecf20Sopenharmony_ci	kexec_free_elf_info(&elf_info);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
1498c2ecf20Sopenharmony_ci	return ret ? ERR_PTR(ret) : fdt;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ciconst struct kexec_file_ops kexec_elf64_ops = {
1538c2ecf20Sopenharmony_ci	.probe = kexec_elf_probe,
1548c2ecf20Sopenharmony_ci	.load = elf64_load,
1558c2ecf20Sopenharmony_ci};
156