1 // SPDX-License-Identifier: GPL-2.0
3 * s390 code for kexec_file_load system call
5 * Copyright IBM Corp. 2018
7 * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com>
10 #define pr_fmt(fmt) "kexec: " fmt
12 #include <linux/elf.h>
13 #include <linux/errno.h>
14 #include <linux/kexec.h>
15 #include <linux/module_signature.h>
16 #include <linux/verification.h>
17 #include <linux/vmalloc.h>
18 #include <asm/boot_data.h>
20 #include <asm/setup.h>
22 const struct kexec_file_ops * const kexec_file_loaders[] = {
24 &s390_kexec_image_ops,
28 #ifdef CONFIG_KEXEC_SIG
29 int s390_verify_sig(const char *kernel, unsigned long kernel_len)
31 const unsigned long marker_len = sizeof(MODULE_SIG_STRING) - 1;
32 struct module_signature *ms;
33 unsigned long sig_len;
36 /* Skip signature verification when not secure IPLed. */
40 if (marker_len > kernel_len)
43 if (memcmp(kernel + kernel_len - marker_len, MODULE_SIG_STRING,
46 kernel_len -= marker_len;
48 ms = (void *)kernel + kernel_len - sizeof(*ms);
49 kernel_len -= sizeof(*ms);
51 sig_len = be32_to_cpu(ms->sig_len);
52 if (sig_len >= kernel_len)
54 kernel_len -= sig_len;
56 if (ms->id_type != PKEY_ID_PKCS7)
61 ms->signer_len != 0 ||
62 ms->key_id_len != 0 ||
69 ret = verify_pkcs7_signature(kernel, kernel_len,
70 kernel + kernel_len, sig_len,
71 VERIFY_USE_SECONDARY_KEYRING,
72 VERIFYING_MODULE_SIGNATURE,
74 if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING))
75 ret = verify_pkcs7_signature(kernel, kernel_len,
76 kernel + kernel_len, sig_len,
77 VERIFY_USE_PLATFORM_KEYRING,
78 VERIFYING_MODULE_SIGNATURE,
82 #endif /* CONFIG_KEXEC_SIG */
84 static int kexec_file_update_purgatory(struct kimage *image,
85 struct s390_load_data *data)
90 if (image->type == KEXEC_TYPE_CRASH) {
91 entry = STARTUP_KDUMP_OFFSET;
92 type = KEXEC_TYPE_CRASH;
94 entry = STARTUP_NORMAL_OFFSET;
95 type = KEXEC_TYPE_DEFAULT;
98 ret = kexec_purgatory_get_set_symbol(image, "kernel_entry", &entry,
99 sizeof(entry), false);
103 ret = kexec_purgatory_get_set_symbol(image, "kernel_type", &type,
104 sizeof(type), false);
108 if (image->type == KEXEC_TYPE_CRASH) {
111 ret = kexec_purgatory_get_set_symbol(image, "crash_start",
113 sizeof(crashk_res.start),
118 crash_size = crashk_res.end - crashk_res.start + 1;
119 ret = kexec_purgatory_get_set_symbol(image, "crash_size",
127 static int kexec_file_add_purgatory(struct kimage *image,
128 struct s390_load_data *data)
130 struct kexec_buf buf;
135 data->memsz = ALIGN(data->memsz, PAGE_SIZE);
136 buf.mem = data->memsz;
137 if (image->type == KEXEC_TYPE_CRASH)
138 buf.mem += crashk_res.start;
140 ret = kexec_load_purgatory(image, &buf);
143 data->memsz += buf.memsz;
145 return kexec_file_update_purgatory(image, data);
148 static int kexec_file_add_initrd(struct kimage *image,
149 struct s390_load_data *data)
151 struct kexec_buf buf;
156 buf.buffer = image->initrd_buf;
157 buf.bufsz = image->initrd_buf_len;
159 data->memsz = ALIGN(data->memsz, PAGE_SIZE);
160 buf.mem = data->memsz;
161 if (image->type == KEXEC_TYPE_CRASH)
162 buf.mem += crashk_res.start;
163 buf.memsz = buf.bufsz;
165 data->parm->initrd_start = data->memsz;
166 data->parm->initrd_size = buf.memsz;
167 data->memsz += buf.memsz;
169 ret = kexec_add_buffer(&buf);
173 return ipl_report_add_component(data->report, &buf, 0, 0);
176 static int kexec_file_add_ipl_report(struct kimage *image,
177 struct s390_load_data *data)
179 __u32 *lc_ipl_parmblock_ptr;
180 unsigned int len, ncerts;
181 struct kexec_buf buf;
188 data->memsz = ALIGN(data->memsz, PAGE_SIZE);
189 buf.mem = data->memsz;
190 if (image->type == KEXEC_TYPE_CRASH)
191 buf.mem += crashk_res.start;
193 ptr = (void *)ipl_cert_list_addr;
194 end = ptr + ipl_cert_list_size;
198 len = *(unsigned int *)ptr;
203 addr = data->memsz + data->report->size;
204 addr += ncerts * sizeof(struct ipl_rb_certificate_entry);
205 ptr = (void *)ipl_cert_list_addr;
207 len = *(unsigned int *)ptr;
209 ipl_report_add_certificate(data->report, ptr, addr, len);
215 buf.buffer = ipl_report_finish(data->report);
218 buf.bufsz = data->report->size;
219 buf.memsz = buf.bufsz;
220 image->arch.ipl_buf = buf.buffer;
222 data->memsz += buf.memsz;
224 lc_ipl_parmblock_ptr =
225 data->kernel_buf + offsetof(struct lowcore, ipl_parmblock_ptr);
226 *lc_ipl_parmblock_ptr = (__u32)buf.mem;
228 ret = kexec_add_buffer(&buf);
233 void *kexec_file_add_components(struct kimage *image,
234 int (*add_kernel)(struct kimage *image,
235 struct s390_load_data *data))
237 unsigned long max_command_line_size = LEGACY_COMMAND_LINE_SIZE;
238 struct s390_load_data data = {0};
239 unsigned long minsize;
242 data.report = ipl_report_init(&ipl_block);
243 if (IS_ERR(data.report))
246 ret = add_kernel(image, &data);
251 minsize = PARMAREA + offsetof(struct parmarea, command_line);
252 if (image->kernel_buf_len < minsize)
255 if (data.parm->max_command_line_size)
256 max_command_line_size = data.parm->max_command_line_size;
258 if (minsize + max_command_line_size < minsize)
261 if (image->kernel_buf_len < minsize + max_command_line_size)
264 if (image->cmdline_buf_len >= max_command_line_size)
267 memcpy(data.parm->command_line, image->cmdline_buf,
268 image->cmdline_buf_len);
270 if (image->type == KEXEC_TYPE_CRASH) {
271 data.parm->oldmem_base = crashk_res.start;
272 data.parm->oldmem_size = crashk_res.end - crashk_res.start + 1;
275 if (image->initrd_buf) {
276 ret = kexec_file_add_initrd(image, &data);
281 ret = kexec_file_add_purgatory(image, &data);
285 if (data.kernel_mem == 0) {
286 unsigned long restart_psw = 0x0008000080000000UL;
287 restart_psw += image->start;
288 memcpy(data.kernel_buf, &restart_psw, sizeof(restart_psw));
292 ret = kexec_file_add_ipl_report(image, &data);
294 ipl_report_free(data.report);
298 int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
300 const Elf_Shdr *relsec,
301 const Elf_Shdr *symtab)
303 const char *strtab, *name, *shstrtab;
304 const Elf_Shdr *sechdrs;
309 /* String & section header string table */
310 sechdrs = (void *)pi->ehdr + pi->ehdr->e_shoff;
311 strtab = (char *)pi->ehdr + sechdrs[symtab->sh_link].sh_offset;
312 shstrtab = (char *)pi->ehdr + sechdrs[pi->ehdr->e_shstrndx].sh_offset;
314 relas = (void *)pi->ehdr + relsec->sh_offset;
316 for (i = 0; i < relsec->sh_size / sizeof(*relas); i++) {
317 const Elf_Sym *sym; /* symbol to relocate */
318 unsigned long addr; /* final location after relocation */
319 unsigned long val; /* relocated symbol value */
320 void *loc; /* tmp location to modify */
322 sym = (void *)pi->ehdr + symtab->sh_offset;
323 sym += ELF64_R_SYM(relas[i].r_info);
326 name = strtab + sym->st_name;
328 name = shstrtab + sechdrs[sym->st_shndx].sh_name;
330 if (sym->st_shndx == SHN_UNDEF) {
331 pr_err("Undefined symbol: %s\n", name);
335 if (sym->st_shndx == SHN_COMMON) {
336 pr_err("symbol '%s' in common section\n", name);
340 if (sym->st_shndx >= pi->ehdr->e_shnum &&
341 sym->st_shndx != SHN_ABS) {
342 pr_err("Invalid section %d for symbol %s\n",
343 sym->st_shndx, name);
347 loc = pi->purgatory_buf;
348 loc += section->sh_offset;
349 loc += relas[i].r_offset;
352 if (sym->st_shndx != SHN_ABS)
353 val += pi->sechdrs[sym->st_shndx].sh_addr;
354 val += relas[i].r_addend;
356 addr = section->sh_addr + relas[i].r_offset;
358 r_type = ELF64_R_TYPE(relas[i].r_info);
360 if (r_type == R_390_PLT32DBL)
361 r_type = R_390_PC32DBL;
363 ret = arch_kexec_do_relocs(r_type, loc, val, addr);
365 pr_err("Unknown rela relocation: %d\n", r_type);
372 int arch_kimage_file_post_load_cleanup(struct kimage *image)
374 vfree(image->arch.ipl_buf);
375 image->arch.ipl_buf = NULL;
377 return kexec_image_post_load_cleanup_default(image);