1 /*
2  * Kexec bzImage loader
3  *
4  * Copyright (C) 2014 Red Hat Inc.
5  * Authors:
6  *      Vivek Goyal <vgoyal@redhat.com>
7  *
8  * This source code is licensed under the GNU General Public License,
9  * Version 2.  See the file COPYING for more details.
10  */
11 
12 #define pr_fmt(fmt)	"kexec-bzImage64: " fmt
13 
14 #include <linux/string.h>
15 #include <linux/printk.h>
16 #include <linux/errno.h>
17 #include <linux/slab.h>
18 #include <linux/kexec.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/efi.h>
22 #include <linux/verification.h>
23 
24 #include <asm/bootparam.h>
25 #include <asm/setup.h>
26 #include <asm/crash.h>
27 #include <asm/efi.h>
28 #include <asm/e820/api.h>
29 #include <asm/kexec-bzimage64.h>
30 
31 #define MAX_ELFCOREHDR_STR_LEN	30	/* elfcorehdr=0x<64bit-value> */
32 
33 /*
34  * Defines lowest physical address for various segments. Not sure where
35  * exactly these limits came from. Current bzimage64 loader in kexec-tools
36  * uses these so I am retaining it. It can be changed over time as we gain
37  * more insight.
38  */
39 #define MIN_PURGATORY_ADDR	0x3000
40 #define MIN_BOOTPARAM_ADDR	0x3000
41 #define MIN_KERNEL_LOAD_ADDR	0x100000
42 #define MIN_INITRD_LOAD_ADDR	0x1000000
43 
44 /*
45  * This is a place holder for all boot loader specific data structure which
46  * gets allocated in one call but gets freed much later during cleanup
47  * time. Right now there is only one field but it can grow as need be.
48  */
49 struct bzimage64_data {
50 	/*
51 	 * Temporary buffer to hold bootparams buffer. This should be
52 	 * freed once the bootparam segment has been loaded.
53 	 */
54 	void *bootparams_buf;
55 };
56 
57 static int setup_initrd(struct boot_params *params,
58 		unsigned long initrd_load_addr, unsigned long initrd_len)
59 {
60 	params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
61 	params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
62 
63 	params->ext_ramdisk_image = initrd_load_addr >> 32;
64 	params->ext_ramdisk_size = initrd_len >> 32;
65 
66 	return 0;
67 }
68 
69 static int setup_cmdline(struct kimage *image, struct boot_params *params,
70 			 unsigned long bootparams_load_addr,
71 			 unsigned long cmdline_offset, char *cmdline,
72 			 unsigned long cmdline_len)
73 {
74 	char *cmdline_ptr = ((char *)params) + cmdline_offset;
75 	unsigned long cmdline_ptr_phys, len = 0;
76 	uint32_t cmdline_low_32, cmdline_ext_32;
77 
78 	if (image->type == KEXEC_TYPE_CRASH) {
79 		len = sprintf(cmdline_ptr,
80 			"elfcorehdr=0x%lx ", image->arch.elf_load_addr);
81 	}
82 	memcpy(cmdline_ptr + len, cmdline, cmdline_len);
83 	cmdline_len += len;
84 
85 	cmdline_ptr[cmdline_len - 1] = '\0';
86 
87 	pr_debug("Final command line is: %s\n", cmdline_ptr);
88 	cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
89 	cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
90 	cmdline_ext_32 = cmdline_ptr_phys >> 32;
91 
92 	params->hdr.cmd_line_ptr = cmdline_low_32;
93 	if (cmdline_ext_32)
94 		params->ext_cmd_line_ptr = cmdline_ext_32;
95 
96 	return 0;
97 }
98 
99 static int setup_e820_entries(struct boot_params *params)
100 {
101 	unsigned int nr_e820_entries;
102 
103 	nr_e820_entries = e820_table_kexec->nr_entries;
104 
105 	/* TODO: Pass entries more than E820_MAX_ENTRIES_ZEROPAGE in bootparams setup data */
106 	if (nr_e820_entries > E820_MAX_ENTRIES_ZEROPAGE)
107 		nr_e820_entries = E820_MAX_ENTRIES_ZEROPAGE;
108 
109 	params->e820_entries = nr_e820_entries;
110 	memcpy(&params->e820_table, &e820_table_kexec->entries, nr_e820_entries*sizeof(struct e820_entry));
111 
112 	return 0;
113 }
114 
115 #ifdef CONFIG_EFI
116 static int setup_efi_info_memmap(struct boot_params *params,
117 				  unsigned long params_load_addr,
118 				  unsigned int efi_map_offset,
119 				  unsigned int efi_map_sz)
120 {
121 	void *efi_map = (void *)params + efi_map_offset;
122 	unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
123 	struct efi_info *ei = &params->efi_info;
124 
125 	if (!efi_map_sz)
126 		return 0;
127 
128 	efi_runtime_map_copy(efi_map, efi_map_sz);
129 
130 	ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
131 	ei->efi_memmap_hi = efi_map_phys_addr >> 32;
132 	ei->efi_memmap_size = efi_map_sz;
133 
134 	return 0;
135 }
136 
137 static int
138 prepare_add_efi_setup_data(struct boot_params *params,
139 		       unsigned long params_load_addr,
140 		       unsigned int efi_setup_data_offset)
141 {
142 	unsigned long setup_data_phys;
143 	struct setup_data *sd = (void *)params + efi_setup_data_offset;
144 	struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
145 
146 	esd->fw_vendor = efi.fw_vendor;
147 	esd->runtime = efi.runtime;
148 	esd->tables = efi.config_table;
149 	esd->smbios = efi.smbios;
150 
151 	sd->type = SETUP_EFI;
152 	sd->len = sizeof(struct efi_setup_data);
153 
154 	/* Add setup data */
155 	setup_data_phys = params_load_addr + efi_setup_data_offset;
156 	sd->next = params->hdr.setup_data;
157 	params->hdr.setup_data = setup_data_phys;
158 
159 	return 0;
160 }
161 
162 static int
163 setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
164 		unsigned int efi_map_offset, unsigned int efi_map_sz,
165 		unsigned int efi_setup_data_offset)
166 {
167 	struct efi_info *current_ei = &boot_params.efi_info;
168 	struct efi_info *ei = &params->efi_info;
169 
170 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
171 		return 0;
172 
173 	if (!current_ei->efi_memmap_size)
174 		return 0;
175 
176 	/*
177 	 * If 1:1 mapping is not enabled, second kernel can not setup EFI
178 	 * and use EFI run time services. User space will have to pass
179 	 * acpi_rsdp=<addr> on kernel command line to make second kernel boot
180 	 * without efi.
181 	 */
182 	if (efi_enabled(EFI_OLD_MEMMAP))
183 		return 0;
184 
185 	ei->efi_loader_signature = current_ei->efi_loader_signature;
186 	ei->efi_systab = current_ei->efi_systab;
187 	ei->efi_systab_hi = current_ei->efi_systab_hi;
188 
189 	ei->efi_memdesc_version = current_ei->efi_memdesc_version;
190 	ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
191 
192 	setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
193 			      efi_map_sz);
194 	prepare_add_efi_setup_data(params, params_load_addr,
195 				   efi_setup_data_offset);
196 	return 0;
197 }
198 #endif /* CONFIG_EFI */
199 
200 static int
201 setup_boot_parameters(struct kimage *image, struct boot_params *params,
202 		      unsigned long params_load_addr,
203 		      unsigned int efi_map_offset, unsigned int efi_map_sz,
204 		      unsigned int efi_setup_data_offset)
205 {
206 	unsigned int nr_e820_entries;
207 	unsigned long long mem_k, start, end;
208 	int i, ret = 0;
209 
210 	/* Get subarch from existing bootparams */
211 	params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
212 
213 	/* Copying screen_info will do? */
214 	memcpy(&params->screen_info, &boot_params.screen_info,
215 				sizeof(struct screen_info));
216 
217 	/* Fill in memsize later */
218 	params->screen_info.ext_mem_k = 0;
219 	params->alt_mem_k = 0;
220 
221 	/* Always fill in RSDP: it is either 0 or a valid value */
222 	params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr;
223 
224 	/* Default APM info */
225 	memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
226 
227 	/* Default drive info */
228 	memset(&params->hd0_info, 0, sizeof(params->hd0_info));
229 	memset(&params->hd1_info, 0, sizeof(params->hd1_info));
230 
231 	if (image->type == KEXEC_TYPE_CRASH) {
232 		ret = crash_setup_memmap_entries(image, params);
233 		if (ret)
234 			return ret;
235 	} else
236 		setup_e820_entries(params);
237 
238 	nr_e820_entries = params->e820_entries;
239 
240 	for (i = 0; i < nr_e820_entries; i++) {
241 		if (params->e820_table[i].type != E820_TYPE_RAM)
242 			continue;
243 		start = params->e820_table[i].addr;
244 		end = params->e820_table[i].addr + params->e820_table[i].size - 1;
245 
246 		if ((start <= 0x100000) && end > 0x100000) {
247 			mem_k = (end >> 10) - (0x100000 >> 10);
248 			params->screen_info.ext_mem_k = mem_k;
249 			params->alt_mem_k = mem_k;
250 			if (mem_k > 0xfc00)
251 				params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
252 			if (mem_k > 0xffffffff)
253 				params->alt_mem_k = 0xffffffff;
254 		}
255 	}
256 
257 #ifdef CONFIG_EFI
258 	/* Setup EFI state */
259 	setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
260 			efi_setup_data_offset);
261 #endif
262 	/* Setup EDD info */
263 	memcpy(params->eddbuf, boot_params.eddbuf,
264 				EDDMAXNR * sizeof(struct edd_info));
265 	params->eddbuf_entries = boot_params.eddbuf_entries;
266 
267 	memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
268 	       EDD_MBR_SIG_MAX * sizeof(unsigned int));
269 
270 	return ret;
271 }
272 
273 static int bzImage64_probe(const char *buf, unsigned long len)
274 {
275 	int ret = -ENOEXEC;
276 	struct setup_header *header;
277 
278 	/* kernel should be at least two sectors long */
279 	if (len < 2 * 512) {
280 		pr_err("File is too short to be a bzImage\n");
281 		return ret;
282 	}
283 
284 	header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
285 	if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
286 		pr_err("Not a bzImage\n");
287 		return ret;
288 	}
289 
290 	if (header->boot_flag != 0xAA55) {
291 		pr_err("No x86 boot sector present\n");
292 		return ret;
293 	}
294 
295 	if (header->version < 0x020C) {
296 		pr_err("Must be at least protocol version 2.12\n");
297 		return ret;
298 	}
299 
300 	if (!(header->loadflags & LOADED_HIGH)) {
301 		pr_err("zImage not a bzImage\n");
302 		return ret;
303 	}
304 
305 	if (!(header->xloadflags & XLF_KERNEL_64)) {
306 		pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
307 		return ret;
308 	}
309 
310 	if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
311 		pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
312 		return ret;
313 	}
314 
315 	/*
316 	 * Can't handle 32bit EFI as it does not allow loading kernel
317 	 * above 4G. This should be handled by 32bit bzImage loader
318 	 */
319 	if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
320 		pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
321 		return ret;
322 	}
323 
324 	/* I've got a bzImage */
325 	pr_debug("It's a relocatable bzImage64\n");
326 	ret = 0;
327 
328 	return ret;
329 }
330 
331 static void *bzImage64_load(struct kimage *image, char *kernel,
332 			    unsigned long kernel_len, char *initrd,
333 			    unsigned long initrd_len, char *cmdline,
334 			    unsigned long cmdline_len)
335 {
336 
337 	struct setup_header *header;
338 	int setup_sects, kern16_size, ret = 0;
339 	unsigned long setup_header_size, params_cmdline_sz;
340 	struct boot_params *params;
341 	unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
342 	struct bzimage64_data *ldata;
343 	struct kexec_entry64_regs regs64;
344 	void *stack;
345 	unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
346 	unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
347 	struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
348 				  .top_down = true };
349 	struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR,
350 				  .buf_max = ULONG_MAX, .top_down = true };
351 
352 	header = (struct setup_header *)(kernel + setup_hdr_offset);
353 	setup_sects = header->setup_sects;
354 	if (setup_sects == 0)
355 		setup_sects = 4;
356 
357 	kern16_size = (setup_sects + 1) * 512;
358 	if (kernel_len < kern16_size) {
359 		pr_err("bzImage truncated\n");
360 		return ERR_PTR(-ENOEXEC);
361 	}
362 
363 	if (cmdline_len > header->cmdline_size) {
364 		pr_err("Kernel command line too long\n");
365 		return ERR_PTR(-EINVAL);
366 	}
367 
368 	/*
369 	 * In case of crash dump, we will append elfcorehdr=<addr> to
370 	 * command line. Make sure it does not overflow
371 	 */
372 	if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
373 		pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
374 		return ERR_PTR(-EINVAL);
375 	}
376 
377 	/* Allocate and load backup region */
378 	if (image->type == KEXEC_TYPE_CRASH) {
379 		ret = crash_load_segments(image);
380 		if (ret)
381 			return ERR_PTR(ret);
382 	}
383 
384 	/*
385 	 * Load purgatory. For 64bit entry point, purgatory  code can be
386 	 * anywhere.
387 	 */
388 	ret = kexec_load_purgatory(image, &pbuf);
389 	if (ret) {
390 		pr_err("Loading purgatory failed\n");
391 		return ERR_PTR(ret);
392 	}
393 
394 	pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem);
395 
396 
397 	/*
398 	 * Load Bootparams and cmdline and space for efi stuff.
399 	 *
400 	 * Allocate memory together for multiple data structures so
401 	 * that they all can go in single area/segment and we don't
402 	 * have to create separate segment for each. Keeps things
403 	 * little bit simple
404 	 */
405 	efi_map_sz = efi_get_runtime_map_size();
406 	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
407 				MAX_ELFCOREHDR_STR_LEN;
408 	params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
409 	kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) +
410 				sizeof(struct setup_data) +
411 				sizeof(struct efi_setup_data);
412 
413 	params = kzalloc(kbuf.bufsz, GFP_KERNEL);
414 	if (!params)
415 		return ERR_PTR(-ENOMEM);
416 	efi_map_offset = params_cmdline_sz;
417 	efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16);
418 
419 	/* Copy setup header onto bootparams. Documentation/x86/boot.txt */
420 	setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
421 
422 	/* Is there a limit on setup header size? */
423 	memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
424 
425 	kbuf.buffer = params;
426 	kbuf.memsz = kbuf.bufsz;
427 	kbuf.buf_align = 16;
428 	kbuf.buf_min = MIN_BOOTPARAM_ADDR;
429 	ret = kexec_add_buffer(&kbuf);
430 	if (ret)
431 		goto out_free_params;
432 	bootparam_load_addr = kbuf.mem;
433 	pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
434 		 bootparam_load_addr, kbuf.bufsz, kbuf.bufsz);
435 
436 	/* Load kernel */
437 	kbuf.buffer = kernel + kern16_size;
438 	kbuf.bufsz =  kernel_len - kern16_size;
439 	kbuf.memsz = PAGE_ALIGN(header->init_size);
440 	kbuf.buf_align = header->kernel_alignment;
441 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
442 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
443 	ret = kexec_add_buffer(&kbuf);
444 	if (ret)
445 		goto out_free_params;
446 	kernel_load_addr = kbuf.mem;
447 
448 	pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
449 		 kernel_load_addr, kbuf.bufsz, kbuf.memsz);
450 
451 	/* Load initrd high */
452 	if (initrd) {
453 		kbuf.buffer = initrd;
454 		kbuf.bufsz = kbuf.memsz = initrd_len;
455 		kbuf.buf_align = PAGE_SIZE;
456 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
457 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
458 		ret = kexec_add_buffer(&kbuf);
459 		if (ret)
460 			goto out_free_params;
461 		initrd_load_addr = kbuf.mem;
462 
463 		pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
464 				initrd_load_addr, initrd_len, initrd_len);
465 
466 		setup_initrd(params, initrd_load_addr, initrd_len);
467 	}
468 
469 	setup_cmdline(image, params, bootparam_load_addr,
470 		      sizeof(struct boot_params), cmdline, cmdline_len);
471 
472 	/* bootloader info. Do we need a separate ID for kexec kernel loader? */
473 	params->hdr.type_of_loader = 0x0D << 4;
474 	params->hdr.loadflags = 0;
475 
476 	/* Setup purgatory regs for entry */
477 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
478 					     sizeof(regs64), 1);
479 	if (ret)
480 		goto out_free_params;
481 
482 	regs64.rbx = 0; /* Bootstrap Processor */
483 	regs64.rsi = bootparam_load_addr;
484 	regs64.rip = kernel_load_addr + 0x200;
485 	stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
486 	if (IS_ERR(stack)) {
487 		pr_err("Could not find address of symbol stack_end\n");
488 		ret = -EINVAL;
489 		goto out_free_params;
490 	}
491 
492 	regs64.rsp = (unsigned long)stack;
493 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
494 					     sizeof(regs64), 0);
495 	if (ret)
496 		goto out_free_params;
497 
498 	ret = setup_boot_parameters(image, params, bootparam_load_addr,
499 				    efi_map_offset, efi_map_sz,
500 				    efi_setup_data_offset);
501 	if (ret)
502 		goto out_free_params;
503 
504 	/* Allocate loader specific data */
505 	ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
506 	if (!ldata) {
507 		ret = -ENOMEM;
508 		goto out_free_params;
509 	}
510 
511 	/*
512 	 * Store pointer to params so that it could be freed after loading
513 	 * params segment has been loaded and contents have been copied
514 	 * somewhere else.
515 	 */
516 	ldata->bootparams_buf = params;
517 	return ldata;
518 
519 out_free_params:
520 	kfree(params);
521 	return ERR_PTR(ret);
522 }
523 
524 /* This cleanup function is called after various segments have been loaded */
525 static int bzImage64_cleanup(void *loader_data)
526 {
527 	struct bzimage64_data *ldata = loader_data;
528 
529 	if (!ldata)
530 		return 0;
531 
532 	kfree(ldata->bootparams_buf);
533 	ldata->bootparams_buf = NULL;
534 
535 	return 0;
536 }
537 
538 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
539 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
540 {
541 	int ret;
542 
543 	ret = verify_pefile_signature(kernel, kernel_len,
544 				      VERIFY_USE_SECONDARY_KEYRING,
545 				      VERIFYING_KEXEC_PE_SIGNATURE);
546 	if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) {
547 		ret = verify_pefile_signature(kernel, kernel_len,
548 					      VERIFY_USE_PLATFORM_KEYRING,
549 					      VERIFYING_KEXEC_PE_SIGNATURE);
550 	}
551 	return ret;
552 }
553 #endif
554 
555 const struct kexec_file_ops kexec_bzImage64_ops = {
556 	.probe = bzImage64_probe,
557 	.load = bzImage64_load,
558 	.cleanup = bzImage64_cleanup,
559 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
560 	.verify_sig = bzImage64_verify_sig,
561 #endif
562 };
563