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.h> 19 #include <linux/of_fdt.h> 20 #include <linux/slab.h> 21 #include <linux/string.h> 22 #include <linux/types.h> 23 #include <linux/vmalloc.h> 24 25 const struct kexec_file_ops * const kexec_file_loaders[] = { 26 &kexec_image_ops, 27 NULL 28 }; 29 30 int arch_kimage_file_post_load_cleanup(struct kimage *image) 31 { 32 kvfree(image->arch.dtb); 33 image->arch.dtb = NULL; 34 35 vfree(image->elf_headers); 36 image->elf_headers = NULL; 37 image->elf_headers_sz = 0; 38 39 return kexec_image_post_load_cleanup_default(image); 40 } 41 42 #ifdef CONFIG_CRASH_DUMP 43 unsigned int arch_get_system_nr_ranges(void) 44 { 45 unsigned int nr_ranges = 2 + crashk_cma_cnt; /* for exclusion of crashkernel region */ 46 phys_addr_t start, end; 47 u64 i; 48 49 for_each_mem_range(i, &start, &end) 50 nr_ranges++; 51 52 return nr_ranges; 53 } 54 55 int arch_crash_populate_cmem(struct crash_mem *cmem) 56 { 57 phys_addr_t start, end; 58 u64 i; 59 60 for_each_mem_range(i, &start, &end) { 61 cmem->ranges[cmem->nr_ranges].start = start; 62 cmem->ranges[cmem->nr_ranges].end = end - 1; 63 cmem->nr_ranges++; 64 } 65 66 return 0; 67 } 68 #endif 69 70 /* 71 * Tries to add the initrd and DTB to the image. If it is not possible to find 72 * valid locations, this function will undo changes to the image and return non 73 * zero. 74 */ 75 int load_other_segments(struct kimage *image, 76 unsigned long kernel_load_addr, 77 unsigned long kernel_size, 78 char *initrd, unsigned long initrd_len, 79 char *cmdline) 80 { 81 struct kexec_buf kbuf = {}; 82 void *dtb = NULL; 83 unsigned long initrd_load_addr = 0, dtb_len, 84 orig_segments = image->nr_segments; 85 int ret = 0; 86 87 kbuf.image = image; 88 /* not allocate anything below the kernel */ 89 kbuf.buf_min = kernel_load_addr + kernel_size; 90 91 #ifdef CONFIG_CRASH_DUMP 92 /* load elf core header */ 93 void *headers; 94 unsigned long headers_sz; 95 if (image->type == KEXEC_TYPE_CRASH) { 96 ret = crash_prepare_headers(true, &headers, &headers_sz, NULL); 97 if (ret) { 98 pr_err("Preparing elf core header failed\n"); 99 goto out_err; 100 } 101 102 kbuf.buffer = headers; 103 kbuf.bufsz = headers_sz; 104 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 105 kbuf.memsz = headers_sz; 106 kbuf.buf_align = SZ_64K; /* largest supported page size */ 107 kbuf.buf_max = ULONG_MAX; 108 kbuf.top_down = true; 109 110 ret = kexec_add_buffer(&kbuf); 111 if (ret) { 112 vfree(headers); 113 goto out_err; 114 } 115 image->elf_headers = headers; 116 image->elf_load_addr = kbuf.mem; 117 image->elf_headers_sz = headers_sz; 118 119 kexec_dprintk("Loaded elf core header at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 120 image->elf_load_addr, kbuf.bufsz, kbuf.memsz); 121 122 ret = crash_load_dm_crypt_keys(image); 123 if (ret) 124 goto out_err; 125 } 126 #endif 127 128 /* load initrd */ 129 if (initrd) { 130 kbuf.buffer = initrd; 131 kbuf.bufsz = initrd_len; 132 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 133 kbuf.memsz = initrd_len; 134 kbuf.buf_align = 0; 135 /* within 1GB-aligned window of up to 32GB in size */ 136 kbuf.buf_max = round_down(kernel_load_addr, SZ_1G) 137 + (unsigned long)SZ_1G * 32; 138 kbuf.top_down = false; 139 140 ret = kexec_add_buffer(&kbuf); 141 if (ret) 142 goto out_err; 143 initrd_load_addr = kbuf.mem; 144 145 kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 146 initrd_load_addr, kbuf.bufsz, kbuf.memsz); 147 } 148 149 /* load dtb */ 150 dtb = of_kexec_alloc_and_setup_fdt(image, initrd_load_addr, 151 initrd_len, cmdline, 0); 152 if (!dtb) { 153 pr_err("Preparing for new dtb failed\n"); 154 ret = -EINVAL; 155 goto out_err; 156 } 157 158 /* trim it */ 159 fdt_pack(dtb); 160 dtb_len = fdt_totalsize(dtb); 161 kbuf.buffer = dtb; 162 kbuf.bufsz = dtb_len; 163 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 164 kbuf.memsz = dtb_len; 165 /* not across 2MB boundary */ 166 kbuf.buf_align = SZ_2M; 167 kbuf.buf_max = ULONG_MAX; 168 kbuf.top_down = true; 169 170 ret = kexec_add_buffer(&kbuf); 171 if (ret) 172 goto out_err; 173 image->arch.dtb = dtb; 174 image->arch.dtb_mem = kbuf.mem; 175 176 kexec_dprintk("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 177 kbuf.mem, kbuf.bufsz, kbuf.memsz); 178 179 return 0; 180 181 out_err: 182 image->nr_segments = orig_segments; 183 kvfree(dtb); 184 return ret; 185 } 186