1 // SPDX-License-Identifier: GPL-2.0-only
3 * Load ELF vmlinux file for the kexec_file_load syscall.
6 * Copyright (C) 2004 IBM Corp.
9 * Copyright (C) 2016 IBM Corporation
11 * Based on kexec-tools' kexec-elf-exec.c and kexec-elf-ppc64.c.
12 * Heavily modified for the kernel by
16 #define pr_fmt(fmt) "kexec_elf: " fmt
18 #include <linux/elf.h>
19 #include <linux/kexec.h>
20 #include <linux/libfdt.h>
21 #include <linux/module.h>
23 #include <linux/of_fdt.h>
24 #include <linux/slab.h>
25 #include <linux/types.h>
27 static 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)
33 unsigned long kernel_load_addr;
34 unsigned long initrd_load_addr = 0, fdt_load_addr;
36 const void *slave_code;
38 char *modified_cmdline = NULL;
39 struct kexec_elf_info elf_info;
40 struct kexec_buf kbuf = { .image = image, .buf_min = 0,
41 .buf_max = ppc64_rma_size };
42 struct kexec_buf pbuf = { .image = image, .buf_min = 0,
43 .buf_max = ppc64_rma_size, .top_down = true,
44 .mem = KEXEC_BUF_MEM_UNKNOWN };
46 ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
50 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));
58 ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
62 pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
64 ret = kexec_load_purgatory(image, &pbuf);
66 pr_err("Loading purgatory failed.\n");
70 pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
72 /* Load additional segments needed for panic kernel */
73 if (image->type == KEXEC_TYPE_CRASH) {
74 ret = load_crashdump_segments_ppc64(image, &kbuf);
76 pr_err("Failed to load kdump kernel segments\n");
80 /* Setup cmdline for kdump kernel case */
81 modified_cmdline = setup_kdump_cmdline(image, cmdline,
83 if (!modified_cmdline) {
84 pr_err("Setting up cmdline for kdump kernel failed\n");
88 cmdline = modified_cmdline;
93 kbuf.bufsz = kbuf.memsz = initrd_len;
94 kbuf.buf_align = PAGE_SIZE;
95 kbuf.top_down = false;
96 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
97 ret = kexec_add_buffer(&kbuf);
100 initrd_load_addr = kbuf.mem;
102 pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
105 fdt = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr,
107 kexec_extra_fdt_size_ppc64(image));
109 pr_err("Error setting up the new device tree.\n");
114 ret = setup_new_fdt_ppc64(image, fdt, initrd_load_addr,
115 initrd_len, cmdline);
122 kbuf.bufsz = kbuf.memsz = fdt_totalsize(fdt);
123 kbuf.buf_align = PAGE_SIZE;
124 kbuf.top_down = true;
125 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
126 ret = kexec_add_buffer(&kbuf);
130 /* FDT will be freed in arch_kimage_file_post_load_cleanup */
131 image->arch.fdt = fdt;
133 fdt_load_addr = kbuf.mem;
135 pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
137 slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
138 ret = setup_purgatory_ppc64(image, slave_code, fdt, kernel_load_addr,
141 pr_err("Error setting up the purgatory.\n");
148 kfree(modified_cmdline);
149 kexec_free_elf_info(&elf_info);
151 return ret ? ERR_PTR(ret) : NULL;
154 const struct kexec_file_ops kexec_elf64_ops = {
155 .probe = kexec_elf_probe,