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