1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * kexec_file for arm64
4  *
5  * Copyright (C) 2018 Linaro Limited
6  * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7  *
8  * Most code is derived from arm64 port of kexec-tools
9  */
10 
11 #define pr_fmt(fmt) "kexec_file: " fmt
12 
13 #include <linux/ioport.h>
14 #include <linux/kernel.h>
15 #include <linux/kexec.h>
16 #include <linux/libfdt.h>
17 #include <linux/memblock.h>
18 #include <linux/of_fdt.h>
19 #include <linux/random.h>
20 #include <linux/string.h>
21 #include <linux/types.h>
22 #include <linux/vmalloc.h>
23 #include <asm/byteorder.h>
24 
25 /* relevant device tree properties */
26 #define FDT_PROP_INITRD_START	"linux,initrd-start"
27 #define FDT_PROP_INITRD_END	"linux,initrd-end"
28 #define FDT_PROP_BOOTARGS	"bootargs"
29 #define FDT_PROP_KASLR_SEED	"kaslr-seed"
30 
31 const struct kexec_file_ops * const kexec_file_loaders[] = {
32 	&kexec_image_ops,
33 	NULL
34 };
35 
36 int arch_kimage_file_post_load_cleanup(struct kimage *image)
37 {
38 	vfree(image->arch.dtb);
39 	image->arch.dtb = NULL;
40 
41 	return kexec_image_post_load_cleanup_default(image);
42 }
43 
44 static int setup_dtb(struct kimage *image,
45 		     unsigned long initrd_load_addr, unsigned long initrd_len,
46 		     char *cmdline, void *dtb)
47 {
48 	int off, ret;
49 
50 	ret = fdt_path_offset(dtb, "/chosen");
51 	if (ret < 0)
52 		goto out;
53 
54 	off = ret;
55 
56 	/* add bootargs */
57 	if (cmdline) {
58 		ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
59 		if (ret)
60 			goto out;
61 	} else {
62 		ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
63 		if (ret && (ret != -FDT_ERR_NOTFOUND))
64 			goto out;
65 	}
66 
67 	/* add initrd-* */
68 	if (initrd_load_addr) {
69 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
70 				      initrd_load_addr);
71 		if (ret)
72 			goto out;
73 
74 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
75 				      initrd_load_addr + initrd_len);
76 		if (ret)
77 			goto out;
78 	} else {
79 		ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
80 		if (ret && (ret != -FDT_ERR_NOTFOUND))
81 			goto out;
82 
83 		ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
84 		if (ret && (ret != -FDT_ERR_NOTFOUND))
85 			goto out;
86 	}
87 
88 	/* add kaslr-seed */
89 	ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
90 	if  (ret == -FDT_ERR_NOTFOUND)
91 		ret = 0;
92 	else if (ret)
93 		goto out;
94 
95 	if (rng_is_initialized()) {
96 		u64 seed = get_random_u64();
97 		ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
98 		if (ret)
99 			goto out;
100 	} else {
101 		pr_notice("RNG is not initialised: omitting \"%s\" property\n",
102 				FDT_PROP_KASLR_SEED);
103 	}
104 
105 out:
106 	if (ret)
107 		return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL;
108 
109 	return 0;
110 }
111 
112 /*
113  * More space needed so that we can add initrd, bootargs and kaslr-seed.
114  */
115 #define DTB_EXTRA_SPACE 0x1000
116 
117 static int create_dtb(struct kimage *image,
118 		      unsigned long initrd_load_addr, unsigned long initrd_len,
119 		      char *cmdline, void **dtb)
120 {
121 	void *buf;
122 	size_t buf_size;
123 	size_t cmdline_len;
124 	int ret;
125 
126 	cmdline_len = cmdline ? strlen(cmdline) : 0;
127 	buf_size = fdt_totalsize(initial_boot_params)
128 			+ cmdline_len + DTB_EXTRA_SPACE;
129 
130 	for (;;) {
131 		buf = vmalloc(buf_size);
132 		if (!buf)
133 			return -ENOMEM;
134 
135 		/* duplicate a device tree blob */
136 		ret = fdt_open_into(initial_boot_params, buf, buf_size);
137 		if (ret)
138 			return -EINVAL;
139 
140 		ret = setup_dtb(image, initrd_load_addr, initrd_len,
141 				cmdline, buf);
142 		if (ret) {
143 			vfree(buf);
144 			if (ret == -ENOMEM) {
145 				/* unlikely, but just in case */
146 				buf_size += DTB_EXTRA_SPACE;
147 				continue;
148 			} else {
149 				return ret;
150 			}
151 		}
152 
153 		/* trim it */
154 		fdt_pack(buf);
155 		*dtb = buf;
156 
157 		return 0;
158 	}
159 }
160 
161 int load_other_segments(struct kimage *image,
162 			unsigned long kernel_load_addr,
163 			unsigned long kernel_size,
164 			char *initrd, unsigned long initrd_len,
165 			char *cmdline)
166 {
167 	struct kexec_buf kbuf;
168 	void *dtb = NULL;
169 	unsigned long initrd_load_addr = 0, dtb_len;
170 	int ret = 0;
171 
172 	kbuf.image = image;
173 	/* not allocate anything below the kernel */
174 	kbuf.buf_min = kernel_load_addr + kernel_size;
175 
176 	/* load initrd */
177 	if (initrd) {
178 		kbuf.buffer = initrd;
179 		kbuf.bufsz = initrd_len;
180 		kbuf.mem = 0;
181 		kbuf.memsz = initrd_len;
182 		kbuf.buf_align = 0;
183 		/* within 1GB-aligned window of up to 32GB in size */
184 		kbuf.buf_max = round_down(kernel_load_addr, SZ_1G)
185 						+ (unsigned long)SZ_1G * 32;
186 		kbuf.top_down = false;
187 
188 		ret = kexec_add_buffer(&kbuf);
189 		if (ret)
190 			goto out_err;
191 		initrd_load_addr = kbuf.mem;
192 
193 		pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
194 				initrd_load_addr, initrd_len, initrd_len);
195 	}
196 
197 	/* load dtb */
198 	ret = create_dtb(image, initrd_load_addr, initrd_len, cmdline, &dtb);
199 	if (ret) {
200 		pr_err("Preparing for new dtb failed\n");
201 		goto out_err;
202 	}
203 
204 	dtb_len = fdt_totalsize(dtb);
205 	kbuf.buffer = dtb;
206 	kbuf.bufsz = dtb_len;
207 	kbuf.mem = 0;
208 	kbuf.memsz = dtb_len;
209 	/* not across 2MB boundary */
210 	kbuf.buf_align = SZ_2M;
211 	kbuf.buf_max = ULONG_MAX;
212 	kbuf.top_down = true;
213 
214 	ret = kexec_add_buffer(&kbuf);
215 	if (ret)
216 		goto out_err;
217 	image->arch.dtb = dtb;
218 	image->arch.dtb_mem = kbuf.mem;
219 
220 	pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
221 			kbuf.mem, dtb_len, dtb_len);
222 
223 	return 0;
224 
225 out_err:
226 	vfree(dtb);
227 	return ret;
228 }
229