blob: 58871333737a4e5458d723dcb0fd6e5e609548da [file] [log] [blame]
AKASHI Takahiro3ddd9992018-11-15 14:52:48 +09001// 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 *
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +09008 * Most code is derived from arm64 port of kexec-tools
AKASHI Takahiro3ddd9992018-11-15 14:52:48 +09009 */
10
11#define pr_fmt(fmt) "kexec_file: " fmt
12
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090013#include <linux/ioport.h>
14#include <linux/kernel.h>
AKASHI Takahiro3ddd9992018-11-15 14:52:48 +090015#include <linux/kexec.h>
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090016#include <linux/libfdt.h>
17#include <linux/memblock.h>
18#include <linux/of_fdt.h>
AKASHI Takahiro884143f2018-11-15 14:52:55 +090019#include <linux/random.h>
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090020#include <linux/string.h>
21#include <linux/types.h>
Arnd Bergmann732291c2018-12-11 11:05:46 +010022#include <linux/vmalloc.h>
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090023#include <asm/byteorder.h>
24
25/* relevant device tree properties */
Will Deacon121ca8e2018-12-06 15:02:45 +000026#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"
AKASHI Takahiro3ddd9992018-11-15 14:52:48 +090030
31const struct kexec_file_ops * const kexec_file_loaders[] = {
AKASHI Takahirof3b70e52018-11-15 14:52:50 +090032 &kexec_image_ops,
AKASHI Takahiro3ddd9992018-11-15 14:52:48 +090033 NULL
34};
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090035
36int 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
44static int setup_dtb(struct kimage *image,
45 unsigned long initrd_load_addr, unsigned long initrd_len,
46 char *cmdline, void *dtb)
47{
Will Deacon121ca8e2018-12-06 15:02:45 +000048 int off, ret;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090049
Will Deacon121ca8e2018-12-06 15:02:45 +000050 ret = fdt_path_offset(dtb, "/chosen");
51 if (ret < 0)
52 goto out;
53
54 off = ret;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090055
56 /* add bootargs */
57 if (cmdline) {
Will Deacon121ca8e2018-12-06 15:02:45 +000058 ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090059 if (ret)
Will Deacon121ca8e2018-12-06 15:02:45 +000060 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090061 } else {
Will Deacon121ca8e2018-12-06 15:02:45 +000062 ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090063 if (ret && (ret != -FDT_ERR_NOTFOUND))
Will Deacon121ca8e2018-12-06 15:02:45 +000064 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090065 }
66
67 /* add initrd-* */
68 if (initrd_load_addr) {
Will Deacon121ca8e2018-12-06 15:02:45 +000069 ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START,
70 initrd_load_addr);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090071 if (ret)
Will Deacon121ca8e2018-12-06 15:02:45 +000072 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090073
Will Deacon121ca8e2018-12-06 15:02:45 +000074 ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END,
75 initrd_load_addr + initrd_len);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090076 if (ret)
Will Deacon121ca8e2018-12-06 15:02:45 +000077 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090078 } else {
Will Deacon121ca8e2018-12-06 15:02:45 +000079 ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090080 if (ret && (ret != -FDT_ERR_NOTFOUND))
Will Deacon121ca8e2018-12-06 15:02:45 +000081 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090082
Will Deacon121ca8e2018-12-06 15:02:45 +000083 ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END);
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090084 if (ret && (ret != -FDT_ERR_NOTFOUND))
Will Deacon121ca8e2018-12-06 15:02:45 +000085 goto out;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +090086 }
87
AKASHI Takahiro884143f2018-11-15 14:52:55 +090088 /* add kaslr-seed */
Will Deacon121ca8e2018-12-06 15:02:45 +000089 ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED);
AKASHI Takahiro27966722019-01-11 16:40:21 +090090 if (ret == -FDT_ERR_NOTFOUND)
91 ret = 0;
92 else if (ret)
Will Deacon121ca8e2018-12-06 15:02:45 +000093 goto out;
AKASHI Takahiro884143f2018-11-15 14:52:55 +090094
95 if (rng_is_initialized()) {
Will Deacon121ca8e2018-12-06 15:02:45 +000096 u64 seed = get_random_u64();
97 ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed);
AKASHI Takahiro884143f2018-11-15 14:52:55 +090098 if (ret)
Will Deacon121ca8e2018-12-06 15:02:45 +000099 goto out;
AKASHI Takahiro884143f2018-11-15 14:52:55 +0900100 } else {
101 pr_notice("RNG is not initialised: omitting \"%s\" property\n",
Will Deacon121ca8e2018-12-06 15:02:45 +0000102 FDT_PROP_KASLR_SEED);
AKASHI Takahiro884143f2018-11-15 14:52:55 +0900103 }
104
Will Deacon121ca8e2018-12-06 15:02:45 +0000105out:
106 if (ret)
107 return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL;
108
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +0900109 return 0;
110}
111
112/*
AKASHI Takahiro884143f2018-11-15 14:52:55 +0900113 * More space needed so that we can add initrd, bootargs and kaslr-seed.
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +0900114 */
115#define DTB_EXTRA_SPACE 0x1000
116
117static 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;
Jean-Philippe Bruckerea573682019-02-01 16:26:08 +0000123 size_t cmdline_len;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +0900124 int ret;
125
Jean-Philippe Bruckerea573682019-02-01 16:26:08 +0000126 cmdline_len = cmdline ? strlen(cmdline) : 0;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +0900127 buf_size = fdt_totalsize(initial_boot_params)
Jean-Philippe Bruckerea573682019-02-01 16:26:08 +0000128 + cmdline_len + DTB_EXTRA_SPACE;
AKASHI Takahiro52b2a8a2018-11-15 14:52:49 +0900129
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
161int 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
225out_err:
226 vfree(dtb);
227 return ret;
228}