xref: /linux/arch/x86/kernel/kexec-bzimage64.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Kexec bzImage loader
4  *
5  * Copyright (C) 2014 Red Hat Inc.
6  * Authors:
7  *      Vivek Goyal <vgoyal@redhat.com>
8  */
9 
10 #define pr_fmt(fmt)	"kexec-bzImage64: " fmt
11 
12 #include <linux/string.h>
13 #include <linux/printk.h>
14 #include <linux/errno.h>
15 #include <linux/slab.h>
16 #include <linux/kexec.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/libfdt.h>
20 #include <linux/of_fdt.h>
21 #include <linux/efi.h>
22 #include <linux/random.h>
23 #include <linux/sysfb.h>
24 
25 #include <asm/bootparam.h>
26 #include <asm/setup.h>
27 #include <asm/crash.h>
28 #include <asm/efi.h>
29 #include <asm/e820/api.h>
30 #include <asm/kexec-bzimage64.h>
31 
32 #define MAX_ELFCOREHDR_STR_LEN	30	/* elfcorehdr=0x<64bit-value> */
33 #define MAX_DMCRYPTKEYS_STR_LEN	31	/* dmcryptkeys=0x<64bit-value> */
34 
35 
36 /*
37  * Defines lowest physical address for various segments. Not sure where
38  * exactly these limits came from. Current bzimage64 loader in kexec-tools
39  * uses these so I am retaining it. It can be changed over time as we gain
40  * more insight.
41  */
42 #define MIN_PURGATORY_ADDR	0x3000
43 #define MIN_BOOTPARAM_ADDR	0x3000
44 #define MIN_KERNEL_LOAD_ADDR	0x100000
45 #define MIN_INITRD_LOAD_ADDR	0x1000000
46 
47 /*
48  * This is a place holder for all boot loader specific data structure which
49  * gets allocated in one call but gets freed much later during cleanup
50  * time. Right now there is only one field but it can grow as need be.
51  */
52 struct bzimage64_data {
53 	/*
54 	 * Temporary buffer to hold bootparams buffer. This should be
55 	 * freed once the bootparam segment has been loaded.
56 	 */
57 	void *bootparams_buf;
58 };
59 
60 static int setup_initrd(struct boot_params *params,
61 		unsigned long initrd_load_addr, unsigned long initrd_len)
62 {
63 	params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
64 	params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
65 
66 	params->ext_ramdisk_image = initrd_load_addr >> 32;
67 	params->ext_ramdisk_size = initrd_len >> 32;
68 
69 	return 0;
70 }
71 
72 static int setup_cmdline(struct kimage *image, struct boot_params *params,
73 			 unsigned long bootparams_load_addr,
74 			 unsigned long cmdline_offset, char *cmdline,
75 			 unsigned long cmdline_len)
76 {
77 	char *cmdline_ptr = ((char *)params) + cmdline_offset;
78 	unsigned long cmdline_ptr_phys, len = 0;
79 	uint32_t cmdline_low_32, cmdline_ext_32;
80 
81 	if (image->type == KEXEC_TYPE_CRASH) {
82 		len = sprintf(cmdline_ptr,
83 			"elfcorehdr=0x%lx ", image->elf_load_addr);
84 
85 		if (image->dm_crypt_keys_addr != 0)
86 			len += sprintf(cmdline_ptr + len,
87 					"dmcryptkeys=0x%lx ", image->dm_crypt_keys_addr);
88 	}
89 	memcpy(cmdline_ptr + len, cmdline, cmdline_len);
90 	cmdline_len += len;
91 
92 	cmdline_ptr[cmdline_len - 1] = '\0';
93 
94 	kexec_dprintk("Final command line is: %s\n", cmdline_ptr);
95 	cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
96 	cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
97 	cmdline_ext_32 = cmdline_ptr_phys >> 32;
98 
99 	params->hdr.cmd_line_ptr = cmdline_low_32;
100 	if (cmdline_ext_32)
101 		params->ext_cmd_line_ptr = cmdline_ext_32;
102 
103 	return 0;
104 }
105 
106 static int setup_e820_entries(struct boot_params *params)
107 {
108 	unsigned int nr_e820_entries;
109 
110 	nr_e820_entries = e820_table_kexec->nr_entries;
111 
112 	/* TODO: Pass entries more than E820_MAX_ENTRIES_ZEROPAGE in bootparams setup data */
113 	if (nr_e820_entries > E820_MAX_ENTRIES_ZEROPAGE)
114 		nr_e820_entries = E820_MAX_ENTRIES_ZEROPAGE;
115 
116 	params->e820_entries = nr_e820_entries;
117 	memcpy(&params->e820_table, &e820_table_kexec->entries, nr_e820_entries*sizeof(struct e820_entry));
118 
119 	return 0;
120 }
121 
122 enum { RNG_SEED_LENGTH = 32 };
123 
124 static void
125 setup_rng_seed(struct boot_params *params, unsigned long params_load_addr,
126 	       unsigned int rng_seed_setup_data_offset)
127 {
128 	struct setup_data *sd = (void *)params + rng_seed_setup_data_offset;
129 	unsigned long setup_data_phys;
130 
131 	if (!rng_is_initialized())
132 		return;
133 
134 	sd->type = SETUP_RNG_SEED;
135 	sd->len = RNG_SEED_LENGTH;
136 	get_random_bytes(sd->data, RNG_SEED_LENGTH);
137 	setup_data_phys = params_load_addr + rng_seed_setup_data_offset;
138 	sd->next = params->hdr.setup_data;
139 	params->hdr.setup_data = setup_data_phys;
140 }
141 
142 #ifdef CONFIG_EFI
143 static int setup_efi_info_memmap(struct boot_params *params,
144 				  unsigned long params_load_addr,
145 				  unsigned int efi_map_offset,
146 				  unsigned int efi_map_sz)
147 {
148 	void *efi_map = (void *)params + efi_map_offset;
149 	unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
150 	struct efi_info *ei = &params->efi_info;
151 
152 	if (!efi_map_sz)
153 		return 0;
154 
155 	efi_runtime_map_copy(efi_map, efi_map_sz);
156 
157 	ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
158 	ei->efi_memmap_hi = efi_map_phys_addr >> 32;
159 	ei->efi_memmap_size = efi_map_sz;
160 
161 	return 0;
162 }
163 
164 static int
165 prepare_add_efi_setup_data(struct boot_params *params,
166 		       unsigned long params_load_addr,
167 		       unsigned int efi_setup_data_offset)
168 {
169 	unsigned long setup_data_phys;
170 	struct setup_data *sd = (void *)params + efi_setup_data_offset;
171 	struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
172 
173 	esd->fw_vendor = efi_fw_vendor;
174 	esd->tables = efi_config_table;
175 	esd->smbios = efi.smbios;
176 
177 	sd->type = SETUP_EFI;
178 	sd->len = sizeof(struct efi_setup_data);
179 
180 	/* Add setup data */
181 	setup_data_phys = params_load_addr + efi_setup_data_offset;
182 	sd->next = params->hdr.setup_data;
183 	params->hdr.setup_data = setup_data_phys;
184 
185 	return 0;
186 }
187 
188 static int
189 setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
190 		unsigned int efi_map_offset, unsigned int efi_map_sz,
191 		unsigned int efi_setup_data_offset)
192 {
193 	struct efi_info *current_ei = &boot_params.efi_info;
194 	struct efi_info *ei = &params->efi_info;
195 
196 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
197 		return 0;
198 
199 	if (!current_ei->efi_memmap_size)
200 		return 0;
201 
202 	params->secure_boot = boot_params.secure_boot;
203 	ei->efi_loader_signature = current_ei->efi_loader_signature;
204 	ei->efi_systab = current_ei->efi_systab;
205 	ei->efi_systab_hi = current_ei->efi_systab_hi;
206 
207 	ei->efi_memdesc_version = current_ei->efi_memdesc_version;
208 	ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
209 
210 	setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
211 			      efi_map_sz);
212 	prepare_add_efi_setup_data(params, params_load_addr,
213 				   efi_setup_data_offset);
214 	return 0;
215 }
216 #endif /* CONFIG_EFI */
217 
218 #ifdef CONFIG_OF_FLATTREE
219 static void setup_dtb(struct boot_params *params,
220 		      unsigned long params_load_addr,
221 		      unsigned int dtb_setup_data_offset)
222 {
223 	struct setup_data *sd = (void *)params + dtb_setup_data_offset;
224 	unsigned long setup_data_phys, dtb_len;
225 
226 	dtb_len = fdt_totalsize(initial_boot_params);
227 	sd->type = SETUP_DTB;
228 	sd->len = dtb_len;
229 
230 	/* Carry over current boot DTB with setup_data */
231 	memcpy(sd->data, initial_boot_params, dtb_len);
232 
233 	/* Add setup data */
234 	setup_data_phys = params_load_addr + dtb_setup_data_offset;
235 	sd->next = params->hdr.setup_data;
236 	params->hdr.setup_data = setup_data_phys;
237 }
238 #endif /* CONFIG_OF_FLATTREE */
239 
240 static void
241 setup_ima_state(const struct kimage *image, struct boot_params *params,
242 		unsigned long params_load_addr,
243 		unsigned int ima_setup_data_offset)
244 {
245 #ifdef CONFIG_IMA_KEXEC
246 	struct setup_data *sd = (void *)params + ima_setup_data_offset;
247 	unsigned long setup_data_phys;
248 	struct ima_setup_data *ima;
249 
250 	if (!image->ima_buffer_size)
251 		return;
252 
253 	sd->type = SETUP_IMA;
254 	sd->len = sizeof(*ima);
255 
256 	ima = (void *)sd + sizeof(struct setup_data);
257 	ima->addr = image->ima_buffer_addr;
258 	ima->size = image->ima_buffer_size;
259 
260 	/* Add setup data */
261 	setup_data_phys = params_load_addr + ima_setup_data_offset;
262 	sd->next = params->hdr.setup_data;
263 	params->hdr.setup_data = setup_data_phys;
264 #endif /* CONFIG_IMA_KEXEC */
265 }
266 
267 static void setup_kho(const struct kimage *image, struct boot_params *params,
268 		      unsigned long params_load_addr,
269 		      unsigned int setup_data_offset)
270 {
271 	struct setup_data *sd = (void *)params + setup_data_offset;
272 	struct kho_data *kho = (void *)sd + sizeof(*sd);
273 
274 	if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER))
275 		return;
276 
277 	sd->type = SETUP_KEXEC_KHO;
278 	sd->len = sizeof(struct kho_data);
279 
280 	/* Only add if we have all KHO images in place */
281 	if (!image->kho.fdt || !image->kho.scratch)
282 		return;
283 
284 	/* Add setup data */
285 	kho->fdt_addr = image->kho.fdt;
286 	kho->fdt_size = PAGE_SIZE;
287 	kho->scratch_addr = image->kho.scratch->mem;
288 	kho->scratch_size = image->kho.scratch->bufsz;
289 	sd->next = params->hdr.setup_data;
290 	params->hdr.setup_data = params_load_addr + setup_data_offset;
291 }
292 
293 static int
294 setup_boot_parameters(struct kimage *image, struct boot_params *params,
295 		      unsigned long params_load_addr,
296 		      unsigned int efi_map_offset, unsigned int efi_map_sz,
297 		      unsigned int setup_data_offset)
298 {
299 	unsigned int nr_e820_entries;
300 	unsigned long long mem_k, start, end;
301 	int i, ret = 0;
302 
303 	/* Get subarch from existing bootparams */
304 	params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
305 
306 	/* Copying screen_info will do? */
307 	memcpy(&params->screen_info, &sysfb_primary_display.screen,
308 	       sizeof(sysfb_primary_display.screen));
309 
310 	/* Fill in memsize later */
311 	params->screen_info.ext_mem_k = 0;
312 	params->alt_mem_k = 0;
313 
314 	/* Always fill in RSDP: it is either 0 or a valid value */
315 	params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr;
316 
317 	/* Default APM info */
318 	memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
319 
320 	/* Default drive info */
321 	memset(&params->hd0_info, 0, sizeof(params->hd0_info));
322 	memset(&params->hd1_info, 0, sizeof(params->hd1_info));
323 
324 #ifdef CONFIG_CRASH_DUMP
325 	if (image->type == KEXEC_TYPE_CRASH) {
326 		ret = crash_setup_memmap_entries(image, params);
327 		if (ret)
328 			return ret;
329 	} else
330 #endif
331 		setup_e820_entries(params);
332 
333 	nr_e820_entries = params->e820_entries;
334 
335 	kexec_dprintk("E820 memmap:\n");
336 	for (i = 0; i < nr_e820_entries; i++) {
337 		kexec_dprintk("%016llx-%016llx (%d)\n",
338 			      params->e820_table[i].addr,
339 			      params->e820_table[i].addr + params->e820_table[i].size - 1,
340 			      params->e820_table[i].type);
341 		if (params->e820_table[i].type != E820_TYPE_RAM)
342 			continue;
343 		start = params->e820_table[i].addr;
344 		end = params->e820_table[i].addr + params->e820_table[i].size - 1;
345 
346 		if ((start <= 0x100000) && end > 0x100000) {
347 			mem_k = (end >> 10) - (0x100000 >> 10);
348 			params->screen_info.ext_mem_k = mem_k;
349 			params->alt_mem_k = mem_k;
350 			if (mem_k > 0xfc00)
351 				params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
352 			if (mem_k > 0xffffffff)
353 				params->alt_mem_k = 0xffffffff;
354 		}
355 	}
356 
357 #ifdef CONFIG_EFI
358 	/* Setup EFI state */
359 	setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
360 			setup_data_offset);
361 	setup_data_offset += sizeof(struct setup_data) +
362 			sizeof(struct efi_setup_data);
363 #endif
364 
365 #ifdef CONFIG_OF_FLATTREE
366 	if (image->force_dtb && initial_boot_params) {
367 		setup_dtb(params, params_load_addr, setup_data_offset);
368 		setup_data_offset += sizeof(struct setup_data) +
369 				     fdt_totalsize(initial_boot_params);
370 	} else {
371 		pr_debug("Not carrying over DTB, force_dtb = %d\n",
372 			 image->force_dtb);
373 	}
374 #endif
375 
376 	if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
377 		/* Setup IMA log buffer state */
378 		setup_ima_state(image, params, params_load_addr,
379 				setup_data_offset);
380 		setup_data_offset += sizeof(struct setup_data) +
381 				     sizeof(struct ima_setup_data);
382 	}
383 
384 	if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
385 		/* Setup space to store preservation metadata */
386 		setup_kho(image, params, params_load_addr, setup_data_offset);
387 		setup_data_offset += sizeof(struct setup_data) +
388 				     sizeof(struct kho_data);
389 	}
390 
391 	/* Setup RNG seed */
392 	setup_rng_seed(params, params_load_addr, setup_data_offset);
393 
394 	/* Setup EDD info */
395 	memcpy(params->eddbuf, boot_params.eddbuf,
396 				EDDMAXNR * sizeof(struct edd_info));
397 	params->eddbuf_entries = boot_params.eddbuf_entries;
398 
399 	memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
400 	       EDD_MBR_SIG_MAX * sizeof(unsigned int));
401 
402 	return ret;
403 }
404 
405 static int bzImage64_probe(const char *buf, unsigned long len)
406 {
407 	int ret = -ENOEXEC;
408 	struct setup_header *header;
409 
410 	/* kernel should be at least two sectors long */
411 	if (len < 2 * 512) {
412 		pr_err("File is too short to be a bzImage\n");
413 		return ret;
414 	}
415 
416 	header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
417 	if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
418 		pr_err("Not a bzImage\n");
419 		return ret;
420 	}
421 
422 	if (header->boot_flag != 0xAA55) {
423 		pr_err("No x86 boot sector present\n");
424 		return ret;
425 	}
426 
427 	if (header->version < 0x020C) {
428 		pr_err("Must be at least protocol version 2.12\n");
429 		return ret;
430 	}
431 
432 	if (!(header->loadflags & LOADED_HIGH)) {
433 		pr_err("zImage not a bzImage\n");
434 		return ret;
435 	}
436 
437 	if (!(header->xloadflags & XLF_KERNEL_64)) {
438 		pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
439 		return ret;
440 	}
441 
442 	if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
443 		pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
444 		return ret;
445 	}
446 
447 	/*
448 	 * Can't handle 32bit EFI as it does not allow loading kernel
449 	 * above 4G. This should be handled by 32bit bzImage loader
450 	 */
451 	if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
452 		pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
453 		return ret;
454 	}
455 
456 	if (!(header->xloadflags & XLF_5LEVEL) && pgtable_l5_enabled()) {
457 		pr_err("bzImage cannot handle 5-level paging mode.\n");
458 		return ret;
459 	}
460 
461 	/* I've got a bzImage */
462 	pr_debug("It's a relocatable bzImage64\n");
463 	ret = 0;
464 
465 	return ret;
466 }
467 
468 static void *bzImage64_load(struct kimage *image, char *kernel,
469 			    unsigned long kernel_len, char *initrd,
470 			    unsigned long initrd_len, char *cmdline,
471 			    unsigned long cmdline_len)
472 {
473 
474 	struct setup_header *header;
475 	int setup_sects, kern16_size, ret = 0;
476 	unsigned long setup_header_size, params_cmdline_sz;
477 	struct boot_params *params;
478 	unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
479 	struct bzimage64_data *ldata;
480 	struct kexec_entry64_regs regs64;
481 	void *stack;
482 	unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
483 	unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
484 	struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
485 				  .top_down = true };
486 	struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR,
487 				  .buf_max = ULONG_MAX, .top_down = true };
488 
489 	header = (struct setup_header *)(kernel + setup_hdr_offset);
490 	setup_sects = header->setup_sects;
491 	if (setup_sects == 0)
492 		setup_sects = 4;
493 
494 	kern16_size = (setup_sects + 1) * 512;
495 	if (kernel_len < kern16_size) {
496 		pr_err("bzImage truncated\n");
497 		return ERR_PTR(-ENOEXEC);
498 	}
499 
500 	if (cmdline_len > header->cmdline_size) {
501 		pr_err("Kernel command line too long\n");
502 		return ERR_PTR(-EINVAL);
503 	}
504 
505 	/*
506 	 * In case of crash dump, we will append elfcorehdr=<addr> to
507 	 * command line. Make sure it does not overflow
508 	 */
509 	if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
510 		pr_err("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
511 		return ERR_PTR(-EINVAL);
512 	}
513 
514 #ifdef CONFIG_CRASH_DUMP
515 	/* Allocate and load backup region */
516 	if (image->type == KEXEC_TYPE_CRASH) {
517 		ret = crash_load_segments(image);
518 		if (ret)
519 			return ERR_PTR(ret);
520 		ret = crash_load_dm_crypt_keys(image);
521 		if (ret == -ENOENT) {
522 			kexec_dprintk("No dm crypt key to load\n");
523 		} else if (ret) {
524 			pr_err("Failed to load dm crypt keys\n");
525 			return ERR_PTR(ret);
526 		}
527 		if (image->dm_crypt_keys_addr &&
528 		    cmdline_len + MAX_ELFCOREHDR_STR_LEN + MAX_DMCRYPTKEYS_STR_LEN >
529 			    header->cmdline_size) {
530 			pr_err("Appending dmcryptkeys=<addr> to command line exceeds maximum allowed length\n");
531 			return ERR_PTR(-EINVAL);
532 		}
533 	}
534 #endif
535 
536 	/*
537 	 * Load purgatory. For 64bit entry point, purgatory  code can be
538 	 * anywhere.
539 	 */
540 	ret = kexec_load_purgatory(image, &pbuf);
541 	if (ret) {
542 		pr_err("Loading purgatory failed\n");
543 		return ERR_PTR(ret);
544 	}
545 
546 	kexec_dprintk("Loaded purgatory at 0x%lx\n", pbuf.mem);
547 
548 
549 	/*
550 	 * Load Bootparams and cmdline and space for efi stuff.
551 	 *
552 	 * Allocate memory together for multiple data structures so
553 	 * that they all can go in single area/segment and we don't
554 	 * have to create separate segment for each. Keeps things
555 	 * little bit simple
556 	 */
557 	efi_map_sz = efi_get_runtime_map_size();
558 	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
559 				MAX_ELFCOREHDR_STR_LEN;
560 	if (image->dm_crypt_keys_addr)
561 		params_cmdline_sz += MAX_DMCRYPTKEYS_STR_LEN;
562 	params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
563 	kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) +
564 				sizeof(struct setup_data) +
565 				sizeof(struct efi_setup_data) +
566 				sizeof(struct setup_data) +
567 				RNG_SEED_LENGTH;
568 
569 #ifdef CONFIG_OF_FLATTREE
570 	if (image->force_dtb && initial_boot_params)
571 		kbuf.bufsz += sizeof(struct setup_data) +
572 			      fdt_totalsize(initial_boot_params);
573 #endif
574 
575 	if (IS_ENABLED(CONFIG_IMA_KEXEC))
576 		kbuf.bufsz += sizeof(struct setup_data) +
577 			      sizeof(struct ima_setup_data);
578 
579 	if (IS_ENABLED(CONFIG_KEXEC_HANDOVER))
580 		kbuf.bufsz += sizeof(struct setup_data) +
581 			      sizeof(struct kho_data);
582 
583 	params = kvzalloc(kbuf.bufsz, GFP_KERNEL);
584 	if (!params)
585 		return ERR_PTR(-ENOMEM);
586 	efi_map_offset = params_cmdline_sz;
587 	efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16);
588 
589 	/* Copy setup header onto bootparams. Documentation/arch/x86/boot.rst */
590 	setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
591 
592 	/* Is there a limit on setup header size? */
593 	memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
594 
595 	kbuf.buffer = params;
596 	kbuf.memsz = kbuf.bufsz;
597 	kbuf.buf_align = 16;
598 	kbuf.buf_min = MIN_BOOTPARAM_ADDR;
599 	ret = kexec_add_buffer(&kbuf);
600 	if (ret)
601 		goto out_free_params;
602 	bootparam_load_addr = kbuf.mem;
603 	kexec_dprintk("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
604 		      bootparam_load_addr, kbuf.bufsz, kbuf.memsz);
605 
606 	/* Load kernel */
607 	kbuf.buffer = kernel + kern16_size;
608 	kbuf.bufsz =  kernel_len - kern16_size;
609 	kbuf.memsz = PAGE_ALIGN(header->init_size);
610 	kbuf.buf_align = header->kernel_alignment;
611 	if (header->pref_address < MIN_KERNEL_LOAD_ADDR)
612 		kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
613 	else
614 		kbuf.buf_min = header->pref_address;
615 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
616 	ret = kexec_add_buffer(&kbuf);
617 	if (ret)
618 		goto out_free_params;
619 	kernel_load_addr = kbuf.mem;
620 
621 	kexec_dprintk("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
622 		      kernel_load_addr, kbuf.bufsz, kbuf.memsz);
623 
624 	/* Load initrd high */
625 	if (initrd) {
626 		kbuf.buffer = initrd;
627 		kbuf.bufsz = kbuf.memsz = initrd_len;
628 		kbuf.buf_align = PAGE_SIZE;
629 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
630 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
631 		ret = kexec_add_buffer(&kbuf);
632 		if (ret)
633 			goto out_free_params;
634 		initrd_load_addr = kbuf.mem;
635 
636 		kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
637 			      initrd_load_addr, initrd_len, initrd_len);
638 
639 		setup_initrd(params, initrd_load_addr, initrd_len);
640 	}
641 
642 	setup_cmdline(image, params, bootparam_load_addr,
643 		      sizeof(struct boot_params), cmdline, cmdline_len);
644 
645 	/* bootloader info. Do we need a separate ID for kexec kernel loader? */
646 	params->hdr.type_of_loader = 0x0D << 4;
647 	params->hdr.loadflags = 0;
648 
649 	/* Setup purgatory regs for entry */
650 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
651 					     sizeof(regs64), 1);
652 	if (ret)
653 		goto out_free_params;
654 
655 	regs64.rbx = 0; /* Bootstrap Processor */
656 	regs64.rsi = bootparam_load_addr;
657 	regs64.rip = kernel_load_addr + 0x200;
658 	stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
659 	if (IS_ERR(stack)) {
660 		pr_err("Could not find address of symbol stack_end\n");
661 		ret = -EINVAL;
662 		goto out_free_params;
663 	}
664 
665 	regs64.rsp = (unsigned long)stack;
666 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
667 					     sizeof(regs64), 0);
668 	if (ret)
669 		goto out_free_params;
670 
671 	ret = setup_boot_parameters(image, params, bootparam_load_addr,
672 				    efi_map_offset, efi_map_sz,
673 				    efi_setup_data_offset);
674 	if (ret)
675 		goto out_free_params;
676 
677 	/* Allocate loader specific data */
678 	ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
679 	if (!ldata) {
680 		ret = -ENOMEM;
681 		goto out_free_params;
682 	}
683 
684 	/*
685 	 * Store pointer to params so that it could be freed after loading
686 	 * params segment has been loaded and contents have been copied
687 	 * somewhere else.
688 	 */
689 	ldata->bootparams_buf = params;
690 	return ldata;
691 
692 out_free_params:
693 	kvfree(params);
694 	return ERR_PTR(ret);
695 }
696 
697 /* This cleanup function is called after various segments have been loaded */
698 static int bzImage64_cleanup(void *loader_data)
699 {
700 	struct bzimage64_data *ldata = loader_data;
701 
702 	if (!ldata)
703 		return 0;
704 
705 	kvfree(ldata->bootparams_buf);
706 	ldata->bootparams_buf = NULL;
707 
708 	return 0;
709 }
710 
711 const struct kexec_file_ops kexec_bzImage64_ops = {
712 	.probe = bzImage64_probe,
713 	.load = bzImage64_load,
714 	.cleanup = bzImage64_cleanup,
715 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
716 	.verify_sig = kexec_kernel_verify_pe_sig,
717 #endif
718 };
719