xref: /openbmc/linux/arch/powerpc/kexec/elf_64.c (revision 5edb7691)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>.
14  */
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>
22 #include <linux/of_fdt.h>
23 #include <linux/slab.h>
24 #include <linux/types.h>
25 
26 static void *elf64_load(struct kimage *image, char *kernel_buf,
27 			unsigned long kernel_len, char *initrd,
28 			unsigned long initrd_len, char *cmdline,
29 			unsigned long cmdline_len)
30 {
31 	int ret;
32 	unsigned int fdt_size;
33 	unsigned long kernel_load_addr;
34 	unsigned long initrd_load_addr = 0, fdt_load_addr;
35 	void *fdt;
36 	const void *slave_code;
37 	struct elfhdr ehdr;
38 	struct kexec_elf_info elf_info;
39 	struct kexec_buf kbuf = { .image = image, .buf_min = 0,
40 				  .buf_max = ppc64_rma_size };
41 	struct kexec_buf pbuf = { .image = image, .buf_min = 0,
42 				  .buf_max = ppc64_rma_size, .top_down = true,
43 				  .mem = KEXEC_BUF_MEM_UNKNOWN };
44 
45 	ret = kexec_build_elf_info(kernel_buf, kernel_len, &ehdr, &elf_info);
46 	if (ret)
47 		goto out;
48 
49 	ret = kexec_elf_load(image, &ehdr, &elf_info, &kbuf, &kernel_load_addr);
50 	if (ret)
51 		goto out;
52 
53 	pr_debug("Loaded the kernel at 0x%lx\n", kernel_load_addr);
54 
55 	ret = kexec_load_purgatory(image, &pbuf);
56 	if (ret) {
57 		pr_err("Loading purgatory failed.\n");
58 		goto out;
59 	}
60 
61 	pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
62 
63 	if (initrd != NULL) {
64 		kbuf.buffer = initrd;
65 		kbuf.bufsz = kbuf.memsz = initrd_len;
66 		kbuf.buf_align = PAGE_SIZE;
67 		kbuf.top_down = false;
68 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
69 		ret = kexec_add_buffer(&kbuf);
70 		if (ret)
71 			goto out;
72 		initrd_load_addr = kbuf.mem;
73 
74 		pr_debug("Loaded initrd at 0x%lx\n", initrd_load_addr);
75 	}
76 
77 	fdt_size = fdt_totalsize(initial_boot_params) * 2;
78 	fdt = kmalloc(fdt_size, GFP_KERNEL);
79 	if (!fdt) {
80 		pr_err("Not enough memory for the device tree.\n");
81 		ret = -ENOMEM;
82 		goto out;
83 	}
84 	ret = fdt_open_into(initial_boot_params, fdt, fdt_size);
85 	if (ret < 0) {
86 		pr_err("Error setting up the new device tree.\n");
87 		ret = -EINVAL;
88 		goto out;
89 	}
90 
91 	ret = setup_new_fdt(image, fdt, initrd_load_addr, initrd_len, cmdline);
92 	if (ret)
93 		goto out;
94 
95 	fdt_pack(fdt);
96 
97 	kbuf.buffer = fdt;
98 	kbuf.bufsz = kbuf.memsz = fdt_size;
99 	kbuf.buf_align = PAGE_SIZE;
100 	kbuf.top_down = true;
101 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
102 	ret = kexec_add_buffer(&kbuf);
103 	if (ret)
104 		goto out;
105 	fdt_load_addr = kbuf.mem;
106 
107 	pr_debug("Loaded device tree at 0x%lx\n", fdt_load_addr);
108 
109 	slave_code = elf_info.buffer + elf_info.proghdrs[0].p_offset;
110 	ret = setup_purgatory(image, slave_code, fdt, kernel_load_addr,
111 			      fdt_load_addr);
112 	if (ret)
113 		pr_err("Error setting up the purgatory.\n");
114 
115 out:
116 	kexec_free_elf_info(&elf_info);
117 
118 	/* Make kimage_file_post_load_cleanup free the fdt buffer for us. */
119 	return ret ? ERR_PTR(ret) : fdt;
120 }
121 
122 const struct kexec_file_ops kexec_elf64_ops = {
123 	.probe = kexec_elf_probe,
124 	.load = elf64_load,
125 };
126