blob: eeb258002d1e020f3cb2402d7ae81ff7ce07de30 [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +11002/*
3 * Load ELF vmlinux file for the kexec_file_load syscall.
4 *
5 * Copyright (C) 2004 Adam Litke (agl@us.ibm.com)
6 * Copyright (C) 2004 IBM Corp.
7 * Copyright (C) 2005 R Sharada (sharada@in.ibm.com)
8 * Copyright (C) 2006 Mohan Kumar M (mohan@in.ibm.com)
9 * Copyright (C) 2016 IBM Corporation
10 *
11 * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
12 * Heavily modified for the kernel by
13 * Thiago Jung Bauermann <bauerman@linux.vnet.ibm.com>.
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110014 */
15
16#define pr_fmt(fmt) "kexec_elf: " fmt
17
18#include <linux/elf.h>
19#include <linux/kexec.h>
20#include <linux/libfdt.h>
21#include <linux/module.h>
Rob Herring3c985d32021-02-21 09:49:24 -080022#include <linux/of.h>
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110023#include <linux/of_fdt.h>
24#include <linux/slab.h>
25#include <linux/types.h>
26
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110027static void *elf64_load(struct kimage *image, char *kernel_buf,
28 unsigned long kernel_len, char *initrd,
29 unsigned long initrd_len, char *cmdline,
30 unsigned long cmdline_len)
31{
32 int ret;
Philipp Rudo3be3f612018-04-13 15:36:43 -070033 unsigned long kernel_load_addr;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110034 unsigned long initrd_load_addr = 0, fdt_load_addr;
35 void *fdt;
36 const void *slave_code;
37 struct elfhdr ehdr;
Hari Bathinicb350c12020-07-29 17:13:14 +053038 char *modified_cmdline = NULL;
Sven Schnelle175fca32019-08-23 21:49:13 +020039 struct kexec_elf_info elf_info;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110040 struct kexec_buf kbuf = { .image = image, .buf_min = 0,
41 .buf_max = ppc64_rma_size };
Philipp Rudo3be3f612018-04-13 15:36:43 -070042 struct kexec_buf pbuf = { .image = image, .buf_min = 0,
Thiago Jung Bauermann8b909e32019-05-22 19:01:58 -030043 .buf_max = ppc64_rma_size, .top_down = true,
44 .mem = KEXEC_BUF_MEM_UNKNOWN };
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110045
Sven Schnelle175fca32019-08-23 21:49:13 +020046 ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110047 if (ret)
Lakshmi Ramasubramanian031cc262021-04-21 09:36:10 -070048 return ERR_PTR(ret);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110049
Hari Bathinib8e55a32020-07-29 17:10:16 +053050 if (image->type == KEXEC_TYPE_CRASH) {
51 /* min & max buffer values for kdump case */
52 kbuf.buf_min = pbuf.buf_min = crashk_res.start;
53 kbuf.buf_max = pbuf.buf_max =
54 ((crashk_res.end < ppc64_rma_size) ?
55 crashk_res.end : (ppc64_rma_size - 1));
56 }
57
Sven Schnelle175fca32019-08-23 21:49:13 +020058 ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110059 if (ret)
60 goto out;
61
62 pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
63
Philipp Rudo3be3f612018-04-13 15:36:43 -070064 ret = kexec_load_purgatory(image, &pbuf);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110065 if (ret) {
66 pr_err("Loading purgatory failed.\n");
67 goto out;
68 }
69
Philipp Rudo3be3f612018-04-13 15:36:43 -070070 pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110071
Hari Bathini1a1cf932020-07-29 17:12:58 +053072 /* Load additional segments needed for panic kernel */
73 if (image->type == KEXEC_TYPE_CRASH) {
74 ret = load_crashdump_segments_ppc64(image, &kbuf);
75 if (ret) {
76 pr_err("Failed to load kdump kernel segments\n");
77 goto out;
78 }
Hari Bathinicb350c12020-07-29 17:13:14 +053079
80 /* Setup cmdline for kdump kernel case */
81 modified_cmdline = setup_kdump_cmdline(image, cmdline,
82 cmdline_len);
83 if (!modified_cmdline) {
84 pr_err("Setting up cmdline for kdump kernel failed\n");
85 ret = -EINVAL;
86 goto out;
87 }
88 cmdline = modified_cmdline;
Hari Bathini1a1cf932020-07-29 17:12:58 +053089 }
90
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110091 if (initrd != NULL) {
92 kbuf.buffer = initrd;
93 kbuf.bufsz = kbuf.memsz = initrd_len;
94 kbuf.buf_align = PAGE_SIZE;
95 kbuf.top_down = false;
Thiago Jung Bauermann8b909e32019-05-22 19:01:58 -030096 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +110097 ret = kexec_add_buffer(&kbuf);
98 if (ret)
99 goto out;
100 initrd_load_addr = kbuf.mem;
101
102 pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
103 }
104
Rob Herring3c985d32021-02-21 09:49:24 -0800105 fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
106 initrd_len, cmdline,
Thiago Jung Bauermann886db322021-02-19 21:52:04 -0300107 kexec_extra_fdt_size_ppc64(image));
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100108 if (!fdt) {
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100109 pr_err("Error setting up the new device tree.\n");
110 ret = -EINVAL;
111 goto out;
112 }
113
Hari Bathini19031272020-07-29 17:09:41 +0530114 ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
115 initrd_len, cmdline);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100116 if (ret)
Lakshmi Ramasubramaniana45dd982021-04-21 09:36:09 -0700117 goto out_free_fdt;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100118
119 fdt_pack(fdt);
120
121 kbuf.buffer = fdt;
Rob Herring3c985d32021-02-21 09:49:24 -0800122 kbuf.bufsz = kbuf.memsz = fdt_totalsize(fdt);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100123 kbuf.buf_align = PAGE_SIZE;
124 kbuf.top_down = true;
Thiago Jung Bauermann8b909e32019-05-22 19:01:58 -0300125 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100126 ret = kexec_add_buffer(&kbuf);
127 if (ret)
Lakshmi Ramasubramaniana45dd982021-04-21 09:36:09 -0700128 goto out_free_fdt;
Rob Herring3c985d32021-02-21 09:49:24 -0800129
130 /* FDT will be freed in arch_kimage_file_post_load_cleanup */
131 image->arch.fdt = fdt;
132
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100133 fdt_load_addr = kbuf.mem;
134
135 pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
136
137 slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
Hari Bathini19031272020-07-29 17:09:41 +0530138 ret = setup_purgatory_ppc64(image, slave_code, fdt, kernel_load_addr,
139 fdt_load_addr);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100140 if (ret)
141 pr_err("Error setting up the purgatory.\n");
142
Lakshmi Ramasubramaniana45dd982021-04-21 09:36:09 -0700143 goto out;
144
145out_free_fdt:
146 kvfree(fdt);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100147out:
Hari Bathinicb350c12020-07-29 17:13:14 +0530148 kfree(modified_cmdline);
Sven Schnelle175fca32019-08-23 21:49:13 +0200149 kexec_free_elf_info(&elf_info);
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100150
Rob Herring3c985d32021-02-21 09:49:24 -0800151 return ret ? ERR_PTR(ret) : NULL;
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100152}
153
AKASHI Takahiro9ec4ece2018-04-13 15:35:49 -0700154const struct kexec_file_ops kexec_elf64_ops = {
Sven Schnelle175fca32019-08-23 21:49:13 +0200155 .probe = kexec_elf_probe,
Thiago Jung Bauermanna0458282016-11-29 23:45:51 +1100156 .load = elf64_load,
157};