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 #define FDT_PROP_RNG_SEED "rng-seed" 31 #define RNG_SEED_SIZE 128 32 33 const struct kexec_file_ops * const kexec_file_loaders[] = { 34 &kexec_image_ops, 35 NULL 36 }; 37 38 int arch_kimage_file_post_load_cleanup(struct kimage *image) 39 { 40 vfree(image->arch.dtb); 41 image->arch.dtb = NULL; 42 43 return kexec_image_post_load_cleanup_default(image); 44 } 45 46 static int setup_dtb(struct kimage *image, 47 unsigned long initrd_load_addr, unsigned long initrd_len, 48 char *cmdline, void *dtb) 49 { 50 int off, ret; 51 52 ret = fdt_path_offset(dtb, "/chosen"); 53 if (ret < 0) 54 goto out; 55 56 off = ret; 57 58 /* add bootargs */ 59 if (cmdline) { 60 ret = fdt_setprop_string(dtb, off, FDT_PROP_BOOTARGS, cmdline); 61 if (ret) 62 goto out; 63 } else { 64 ret = fdt_delprop(dtb, off, FDT_PROP_BOOTARGS); 65 if (ret && (ret != -FDT_ERR_NOTFOUND)) 66 goto out; 67 } 68 69 /* add initrd-* */ 70 if (initrd_load_addr) { 71 ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_START, 72 initrd_load_addr); 73 if (ret) 74 goto out; 75 76 ret = fdt_setprop_u64(dtb, off, FDT_PROP_INITRD_END, 77 initrd_load_addr + initrd_len); 78 if (ret) 79 goto out; 80 } else { 81 ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_START); 82 if (ret && (ret != -FDT_ERR_NOTFOUND)) 83 goto out; 84 85 ret = fdt_delprop(dtb, off, FDT_PROP_INITRD_END); 86 if (ret && (ret != -FDT_ERR_NOTFOUND)) 87 goto out; 88 } 89 90 /* add kaslr-seed */ 91 ret = fdt_delprop(dtb, off, FDT_PROP_KASLR_SEED); 92 if (ret == -FDT_ERR_NOTFOUND) 93 ret = 0; 94 else if (ret) 95 goto out; 96 97 if (rng_is_initialized()) { 98 u64 seed = get_random_u64(); 99 ret = fdt_setprop_u64(dtb, off, FDT_PROP_KASLR_SEED, seed); 100 if (ret) 101 goto out; 102 } else { 103 pr_notice("RNG is not initialised: omitting \"%s\" property\n", 104 FDT_PROP_KASLR_SEED); 105 } 106 107 /* add rng-seed */ 108 if (rng_is_initialized()) { 109 u8 rng_seed[RNG_SEED_SIZE]; 110 get_random_bytes(rng_seed, RNG_SEED_SIZE); 111 ret = fdt_setprop(dtb, off, FDT_PROP_RNG_SEED, rng_seed, 112 RNG_SEED_SIZE); 113 if (ret) 114 goto out; 115 } else { 116 pr_notice("RNG is not initialised: omitting \"%s\" property\n", 117 FDT_PROP_RNG_SEED); 118 } 119 120 out: 121 if (ret) 122 return (ret == -FDT_ERR_NOSPACE) ? -ENOMEM : -EINVAL; 123 124 return 0; 125 } 126 127 /* 128 * More space needed so that we can add initrd, bootargs, kaslr-seed, and 129 * rng-seed. 130 */ 131 #define DTB_EXTRA_SPACE 0x1000 132 133 static int create_dtb(struct kimage *image, 134 unsigned long initrd_load_addr, unsigned long initrd_len, 135 char *cmdline, void **dtb) 136 { 137 void *buf; 138 size_t buf_size; 139 size_t cmdline_len; 140 int ret; 141 142 cmdline_len = cmdline ? strlen(cmdline) : 0; 143 buf_size = fdt_totalsize(initial_boot_params) 144 + cmdline_len + DTB_EXTRA_SPACE; 145 146 for (;;) { 147 buf = vmalloc(buf_size); 148 if (!buf) 149 return -ENOMEM; 150 151 /* duplicate a device tree blob */ 152 ret = fdt_open_into(initial_boot_params, buf, buf_size); 153 if (ret) 154 return -EINVAL; 155 156 ret = setup_dtb(image, initrd_load_addr, initrd_len, 157 cmdline, buf); 158 if (ret) { 159 vfree(buf); 160 if (ret == -ENOMEM) { 161 /* unlikely, but just in case */ 162 buf_size += DTB_EXTRA_SPACE; 163 continue; 164 } else { 165 return ret; 166 } 167 } 168 169 /* trim it */ 170 fdt_pack(buf); 171 *dtb = buf; 172 173 return 0; 174 } 175 } 176 177 int load_other_segments(struct kimage *image, 178 unsigned long kernel_load_addr, 179 unsigned long kernel_size, 180 char *initrd, unsigned long initrd_len, 181 char *cmdline) 182 { 183 struct kexec_buf kbuf; 184 void *dtb = NULL; 185 unsigned long initrd_load_addr = 0, dtb_len; 186 int ret = 0; 187 188 kbuf.image = image; 189 /* not allocate anything below the kernel */ 190 kbuf.buf_min = kernel_load_addr + kernel_size; 191 192 /* load initrd */ 193 if (initrd) { 194 kbuf.buffer = initrd; 195 kbuf.bufsz = initrd_len; 196 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 197 kbuf.memsz = initrd_len; 198 kbuf.buf_align = 0; 199 /* within 1GB-aligned window of up to 32GB in size */ 200 kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) 201 + (unsigned long)SZ_1G * 32; 202 kbuf.top_down = false; 203 204 ret = kexec_add_buffer(&kbuf); 205 if (ret) 206 goto out_err; 207 initrd_load_addr = kbuf.mem; 208 209 pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 210 initrd_load_addr, initrd_len, initrd_len); 211 } 212 213 /* load dtb */ 214 ret = create_dtb(image, initrd_load_addr, initrd_len, cmdline, &dtb); 215 if (ret) { 216 pr_err("Preparing for new dtb failed\n"); 217 goto out_err; 218 } 219 220 dtb_len = fdt_totalsize(dtb); 221 kbuf.buffer = dtb; 222 kbuf.bufsz = dtb_len; 223 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 224 kbuf.memsz = dtb_len; 225 /* not across 2MB boundary */ 226 kbuf.buf_align = SZ_2M; 227 kbuf.buf_max = ULONG_MAX; 228 kbuf.top_down = true; 229 230 ret = kexec_add_buffer(&kbuf); 231 if (ret) 232 goto out_err; 233 image->arch.dtb = dtb; 234 image->arch.dtb_mem = kbuf.mem; 235 236 pr_debug("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 237 kbuf.mem, dtb_len, dtb_len); 238 239 return 0; 240 241 out_err: 242 vfree(dtb); 243 return ret; 244 } 245