AKASHI Takahiro | 3ddd999 | 2018-11-15 14:52:48 +0900 | [diff] [blame] | 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 | * |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 8 | * Most code is derived from arm64 port of kexec-tools |
AKASHI Takahiro | 3ddd999 | 2018-11-15 14:52:48 +0900 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) "kexec_file: " fmt |
| 12 | |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 13 | #include <linux/ioport.h> |
| 14 | #include <linux/kernel.h> |
AKASHI Takahiro | 3ddd999 | 2018-11-15 14:52:48 +0900 | [diff] [blame] | 15 | #include <linux/kexec.h> |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 16 | #include <linux/libfdt.h> |
| 17 | #include <linux/memblock.h> |
| 18 | #include <linux/of_fdt.h> |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 19 | #include <linux/random.h> |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 20 | #include <linux/string.h> |
| 21 | #include <linux/types.h> |
Arnd Bergmann | 732291c | 2018-12-11 11:05:46 +0100 | [diff] [blame] | 22 | #include <linux/vmalloc.h> |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 23 | #include <asm/byteorder.h> |
| 24 | |
| 25 | /* relevant device tree properties */ |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 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" |
AKASHI Takahiro | 3ddd999 | 2018-11-15 14:52:48 +0900 | [diff] [blame] | 30 | |
| 31 | const struct kexec_file_ops * const kexec_file_loaders[] = { |
AKASHI Takahiro | f3b70e5 | 2018-11-15 14:52:50 +0900 | [diff] [blame] | 32 | &kexec_image_ops, |
AKASHI Takahiro | 3ddd999 | 2018-11-15 14:52:48 +0900 | [diff] [blame] | 33 | NULL |
| 34 | }; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 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 | { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 48 | int off, ret; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 49 | |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 50 | ret = fdt_path_offset(dtb, "/chosen"); |
| 51 | if (ret < 0) |
| 52 | goto out; |
| 53 | |
| 54 | off = ret; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 55 | |
| 56 | /* add bootargs */ |
| 57 | if (cmdline) { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 58 | ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 59 | if (ret) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 60 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 61 | } else { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 62 | ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 63 | if (ret && (ret != -FDT_ERR_NOTFOUND)) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 64 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* add initrd-* */ |
| 68 | if (initrd_load_addr) { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 69 | ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START, |
| 70 | initrd_load_addr); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 71 | if (ret) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 72 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 73 | |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 74 | ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END, |
| 75 | initrd_load_addr + initrd_len); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 76 | if (ret) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 77 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 78 | } else { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 79 | ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 80 | if (ret && (ret != -FDT_ERR_NOTFOUND)) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 81 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 82 | |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 83 | ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END); |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 84 | if (ret && (ret != -FDT_ERR_NOTFOUND)) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 85 | goto out; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 86 | } |
| 87 | |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 88 | /* add kaslr-seed */ |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 89 | ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED); |
AKASHI Takahiro | 2796672 | 2019-01-11 16:40:21 +0900 | [diff] [blame] | 90 | if (ret == -FDT_ERR_NOTFOUND) |
| 91 | ret = 0; |
| 92 | else if (ret) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 93 | goto out; |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 94 | |
| 95 | if (rng_is_initialized()) { |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 96 | u64 seed = get_random_u64(); |
| 97 | ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed); |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 98 | if (ret) |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 99 | goto out; |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 100 | } else { |
| 101 | pr_notice("RNG is not initialised: omitting \"%s\" property\n", |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 102 | FDT_PROP_KASLR_SEED); |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 103 | } |
| 104 | |
Will Deacon | 121ca8e | 2018-12-06 15:02:45 +0000 | [diff] [blame] | 105 | out: |
| 106 | if (ret) |
| 107 | return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL; |
| 108 | |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* |
AKASHI Takahiro | 884143f | 2018-11-15 14:52:55 +0900 | [diff] [blame] | 113 | * More space needed so that we can add initrd, bootargs and kaslr-seed. |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 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; |
Jean-Philippe Brucker | ea57368 | 2019-02-01 16:26:08 +0000 | [diff] [blame] | 123 | size_t cmdline_len; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 124 | int ret; |
| 125 | |
Jean-Philippe Brucker | ea57368 | 2019-02-01 16:26:08 +0000 | [diff] [blame] | 126 | cmdline_len = cmdline ? strlen(cmdline) : 0; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 127 | buf_size = fdt_totalsize(initial_boot_params) |
Jean-Philippe Brucker | ea57368 | 2019-02-01 16:26:08 +0000 | [diff] [blame] | 128 | + cmdline_len + DTB_EXTRA_SPACE; |
AKASHI Takahiro | 52b2a8a | 2018-11-15 14:52:49 +0900 | [diff] [blame] | 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 | } |