1793b08e2SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-only
2793b08e2SChristophe Leroy /*
3793b08e2SChristophe Leroy * Load ELF vmlinux file for the kexec_file_load syscall.
4793b08e2SChristophe Leroy *
5793b08e2SChristophe Leroy * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
6793b08e2SChristophe Leroy * Copyright (C) 2004 IBM Corp.
7793b08e2SChristophe Leroy * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
8793b08e2SChristophe Leroy * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
9793b08e2SChristophe Leroy * Copyright (C) 2016 IBM Corporation
10793b08e2SChristophe Leroy *
11793b08e2SChristophe Leroy * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
12793b08e2SChristophe Leroy * Heavily modified for the kernel by
13793b08e2SChristophe Leroy * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
14793b08e2SChristophe Leroy */
15793b08e2SChristophe Leroy
16793b08e2SChristophe Leroy #define pr_fmt(fmt) "kexec_elf: " fmt
17793b08e2SChristophe Leroy
18793b08e2SChristophe Leroy #include <linux/elf.h>
19793b08e2SChristophe Leroy #include <linux/kexec.h>
20793b08e2SChristophe Leroy #include <linux/libfdt.h>
21793b08e2SChristophe Leroy #include <linux/module.h>
223c985d31SRob Herring #include <linux/of.h>
23793b08e2SChristophe Leroy #include <linux/of_fdt.h>
24793b08e2SChristophe Leroy #include <linux/slab.h>
25793b08e2SChristophe Leroy #include <linux/types.h>
26793b08e2SChristophe Leroy
elf64_load(struct kimage * image,char * kernel_buf,unsigned long kernel_len,char * initrd,unsigned long initrd_len,char * cmdline,unsigned long cmdline_len)27793b08e2SChristophe Leroy static void *elf64_load(struct kimage *image, char *kernel_buf,
28793b08e2SChristophe Leroy unsigned long kernel_len, char *initrd,
29793b08e2SChristophe Leroy unsigned long initrd_len, char *cmdline,
30793b08e2SChristophe Leroy unsigned long cmdline_len)
31793b08e2SChristophe Leroy {
32793b08e2SChristophe Leroy int ret;
33793b08e2SChristophe Leroy unsigned long kernel_load_addr;
34793b08e2SChristophe Leroy unsigned long initrd_load_addr = 0, fdt_load_addr;
35793b08e2SChristophe Leroy void *fdt;
36793b08e2SChristophe Leroy const void *slave_code;
37793b08e2SChristophe Leroy struct elfhdr ehdr;
38cb350c1fSHari Bathini char *modified_cmdline = NULL;
39793b08e2SChristophe Leroy struct kexec_elf_info elf_info;
40793b08e2SChristophe Leroy struct kexec_buf kbuf = { .image = image, .buf_min = 0,
41793b08e2SChristophe Leroy .buf_max = ppc64_rma_size };
42793b08e2SChristophe Leroy struct kexec_buf pbuf = { .image = image, .buf_min = 0,
43793b08e2SChristophe Leroy .buf_max = ppc64_rma_size, .top_down = true,
44793b08e2SChristophe Leroy .mem = KEXEC_BUF_MEM_UNKNOWN };
45793b08e2SChristophe Leroy
46793b08e2SChristophe Leroy ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
47793b08e2SChristophe Leroy if (ret)
48*031cc263SLakshmi Ramasubramanian return ERR_PTR(ret);
49793b08e2SChristophe Leroy
50b8e55a3eSHari Bathini if (image->type == KEXEC_TYPE_CRASH) {
51b8e55a3eSHari Bathini /* min & max buffer values for kdump case */
52b8e55a3eSHari Bathini kbuf.buf_min = pbuf.buf_min = crashk_res.start;
53b8e55a3eSHari Bathini kbuf.buf_max = pbuf.buf_max =
54b8e55a3eSHari Bathini ((crashk_res.end < ppc64_rma_size) ?
55b8e55a3eSHari Bathini crashk_res.end : (ppc64_rma_size - 1));
56b8e55a3eSHari Bathini }
57b8e55a3eSHari Bathini
58793b08e2SChristophe Leroy ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
59793b08e2SChristophe Leroy if (ret)
60793b08e2SChristophe Leroy goto out;
61793b08e2SChristophe Leroy
62793b08e2SChristophe Leroy pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
63793b08e2SChristophe Leroy
64793b08e2SChristophe Leroy ret = kexec_load_purgatory(image, &pbuf);
65793b08e2SChristophe Leroy if (ret) {
66793b08e2SChristophe Leroy pr_err("Loading purgatory failed.\n");
67793b08e2SChristophe Leroy goto out;
68793b08e2SChristophe Leroy }
69793b08e2SChristophe Leroy
70793b08e2SChristophe Leroy pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
71793b08e2SChristophe Leroy
721a1cf93cSHari Bathini /* Load additional segments needed for panic kernel */
731a1cf93cSHari Bathini if (image->type == KEXEC_TYPE_CRASH) {
741a1cf93cSHari Bathini ret = load_crashdump_segments_ppc64(image, &kbuf);
751a1cf93cSHari Bathini if (ret) {
761a1cf93cSHari Bathini pr_err("Failed to load kdump kernel segments\n");
771a1cf93cSHari Bathini goto out;
781a1cf93cSHari Bathini }
79cb350c1fSHari Bathini
80cb350c1fSHari Bathini /* Setup cmdline for kdump kernel case */
81cb350c1fSHari Bathini modified_cmdline = setup_kdump_cmdline(image, cmdline,
82cb350c1fSHari Bathini cmdline_len);
83cb350c1fSHari Bathini if (!modified_cmdline) {
84cb350c1fSHari Bathini pr_err("Setting up cmdline for kdump kernel failed\n");
85cb350c1fSHari Bathini ret = -EINVAL;
86cb350c1fSHari Bathini goto out;
87cb350c1fSHari Bathini }
88cb350c1fSHari Bathini cmdline = modified_cmdline;
891a1cf93cSHari Bathini }
901a1cf93cSHari Bathini
91793b08e2SChristophe Leroy if (initrd != NULL) {
92793b08e2SChristophe Leroy kbuf.buffer = initrd;
93793b08e2SChristophe Leroy kbuf.bufsz = kbuf.memsz = initrd_len;
94793b08e2SChristophe Leroy kbuf.buf_align = PAGE_SIZE;
95793b08e2SChristophe Leroy kbuf.top_down = false;
96793b08e2SChristophe Leroy kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
97793b08e2SChristophe Leroy ret = kexec_add_buffer(&kbuf);
98793b08e2SChristophe Leroy if (ret)
99793b08e2SChristophe Leroy goto out;
100793b08e2SChristophe Leroy initrd_load_addr = kbuf.mem;
101793b08e2SChristophe Leroy
102793b08e2SChristophe Leroy pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
103793b08e2SChristophe Leroy }
104793b08e2SChristophe Leroy
1053c985d31SRob Herring fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
1063c985d31SRob Herring initrd_len, cmdline,
107886db323SThiago Jung Bauermann kexec_extra_fdt_size_ppc64(image));
108793b08e2SChristophe Leroy if (!fdt) {
109793b08e2SChristophe Leroy pr_err("Error setting up the new device tree.\n");
110793b08e2SChristophe Leroy ret = -EINVAL;
111793b08e2SChristophe Leroy goto out;
112793b08e2SChristophe Leroy }
113793b08e2SChristophe Leroy
11419031275SHari Bathini ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
11519031275SHari Bathini initrd_len, cmdline);
116793b08e2SChristophe Leroy if (ret)
117a45dd984SLakshmi Ramasubramanian goto out_free_fdt;
118793b08e2SChristophe Leroy
119793b08e2SChristophe Leroy fdt_pack(fdt);
120793b08e2SChristophe Leroy
121793b08e2SChristophe Leroy kbuf.buffer = fdt;
1223c985d31SRob Herring kbuf.bufsz = kbuf.memsz = fdt_totalsize(fdt);
123793b08e2SChristophe Leroy kbuf.buf_align = PAGE_SIZE;
124793b08e2SChristophe Leroy kbuf.top_down = true;
125793b08e2SChristophe Leroy kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
126793b08e2SChristophe Leroy ret = kexec_add_buffer(&kbuf);
127793b08e2SChristophe Leroy if (ret)
128a45dd984SLakshmi Ramasubramanian goto out_free_fdt;
1293c985d31SRob Herring
1303c985d31SRob Herring /* FDT will be freed in arch_kimage_file_post_load_cleanup */
1313c985d31SRob Herring image->arch.fdt = fdt;
1323c985d31SRob Herring
133793b08e2SChristophe Leroy fdt_load_addr = kbuf.mem;
134793b08e2SChristophe Leroy
135793b08e2SChristophe Leroy pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
136793b08e2SChristophe Leroy
137793b08e2SChristophe Leroy slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
13819031275SHari Bathini ret = setup_purgatory_ppc64(image, slave_code, fdt, kernel_load_addr,
139793b08e2SChristophe Leroy fdt_load_addr);
140793b08e2SChristophe Leroy if (ret)
141793b08e2SChristophe Leroy pr_err("Error setting up the purgatory.\n");
142793b08e2SChristophe Leroy
143a45dd984SLakshmi Ramasubramanian goto out;
144a45dd984SLakshmi Ramasubramanian
145a45dd984SLakshmi Ramasubramanian out_free_fdt:
146a45dd984SLakshmi Ramasubramanian kvfree(fdt);
147793b08e2SChristophe Leroy out:
148cb350c1fSHari Bathini kfree(modified_cmdline);
149793b08e2SChristophe Leroy kexec_free_elf_info(&elf_info);
150793b08e2SChristophe Leroy
1513c985d31SRob Herring return ret ? ERR_PTR(ret) : NULL;
152793b08e2SChristophe Leroy }
153793b08e2SChristophe Leroy
154793b08e2SChristophe Leroy const struct kexec_file_ops kexec_elf64_ops = {
155793b08e2SChristophe Leroy .probe = kexec_elf_probe,
156793b08e2SChristophe Leroy .load = elf64_load,
157793b08e2SChristophe Leroy };
158