xref: /linux/arch/x86/kernel/kexec-bzimage64.c (revision 03738dd159db60a27378e9b5b93fd187218519ba)
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 (!params->acpi_rsdp_addr) {
197 		if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
198 			params->acpi_rsdp_addr = efi.acpi20;
199 		else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
200 			params->acpi_rsdp_addr = efi.acpi;
201 	}
202 
203 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
204 		return 0;
205 
206 	if (!current_ei->efi_memmap_size)
207 		return 0;
208 
209 	params->secure_boot = boot_params.secure_boot;
210 	ei->efi_loader_signature = current_ei->efi_loader_signature;
211 	ei->efi_systab = current_ei->efi_systab;
212 	ei->efi_systab_hi = current_ei->efi_systab_hi;
213 
214 	ei->efi_memdesc_version = current_ei->efi_memdesc_version;
215 	ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
216 
217 	setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
218 			      efi_map_sz);
219 	prepare_add_efi_setup_data(params, params_load_addr,
220 				   efi_setup_data_offset);
221 	return 0;
222 }
223 #endif /* CONFIG_EFI */
224 
225 #ifdef CONFIG_OF_FLATTREE
226 static void setup_dtb(struct boot_params *params,
227 		      unsigned long params_load_addr,
228 		      unsigned int dtb_setup_data_offset)
229 {
230 	struct setup_data *sd = (void *)params + dtb_setup_data_offset;
231 	unsigned long setup_data_phys, dtb_len;
232 
233 	dtb_len = fdt_totalsize(initial_boot_params);
234 	sd->type = SETUP_DTB;
235 	sd->len = dtb_len;
236 
237 	/* Carry over current boot DTB with setup_data */
238 	memcpy(sd->data, initial_boot_params, dtb_len);
239 
240 	/* Add setup data */
241 	setup_data_phys = params_load_addr + dtb_setup_data_offset;
242 	sd->next = params->hdr.setup_data;
243 	params->hdr.setup_data = setup_data_phys;
244 }
245 #endif /* CONFIG_OF_FLATTREE */
246 
247 static void
248 setup_ima_state(const struct kimage *image, struct boot_params *params,
249 		unsigned long params_load_addr,
250 		unsigned int ima_setup_data_offset)
251 {
252 #ifdef CONFIG_IMA_KEXEC
253 	struct setup_data *sd = (void *)params + ima_setup_data_offset;
254 	unsigned long setup_data_phys;
255 	struct ima_setup_data *ima;
256 
257 	if (!image->ima_buffer_size)
258 		return;
259 
260 	sd->type = SETUP_IMA;
261 	sd->len = sizeof(*ima);
262 
263 	ima = (void *)sd + sizeof(struct setup_data);
264 	ima->addr = image->ima_buffer_addr;
265 	ima->size = image->ima_buffer_size;
266 
267 	/* Add setup data */
268 	setup_data_phys = params_load_addr + ima_setup_data_offset;
269 	sd->next = params->hdr.setup_data;
270 	params->hdr.setup_data = setup_data_phys;
271 #endif /* CONFIG_IMA_KEXEC */
272 }
273 
274 static void setup_kho(const struct kimage *image, struct boot_params *params,
275 		      unsigned long params_load_addr,
276 		      unsigned int setup_data_offset)
277 {
278 	struct setup_data *sd = (void *)params + setup_data_offset;
279 	struct kho_data *kho = (void *)sd + sizeof(*sd);
280 
281 	if (!IS_ENABLED(CONFIG_KEXEC_HANDOVER))
282 		return;
283 
284 	sd->type = SETUP_KEXEC_KHO;
285 	sd->len = sizeof(struct kho_data);
286 
287 	/* Only add if we have all KHO images in place */
288 	if (!image->kho.fdt || !image->kho.scratch)
289 		return;
290 
291 	/* Add setup data */
292 	kho->fdt_addr = image->kho.fdt;
293 	kho->fdt_size = PAGE_SIZE;
294 	kho->scratch_addr = image->kho.scratch->mem;
295 	kho->scratch_size = image->kho.scratch->bufsz;
296 	sd->next = params->hdr.setup_data;
297 	params->hdr.setup_data = params_load_addr + setup_data_offset;
298 }
299 
300 static int
301 setup_boot_parameters(struct kimage *image, struct boot_params *params,
302 		      unsigned long params_load_addr,
303 		      unsigned int efi_map_offset, unsigned int efi_map_sz,
304 		      unsigned int setup_data_offset)
305 {
306 	unsigned int nr_e820_entries;
307 	unsigned long long mem_k, start, end;
308 	int i, ret = 0;
309 
310 	/* Get subarch from existing bootparams */
311 	params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
312 
313 	/* Copying screen_info will do? */
314 	memcpy(&params->screen_info, &sysfb_primary_display.screen,
315 	       sizeof(sysfb_primary_display.screen));
316 
317 	/* Fill in memsize later */
318 	params->screen_info.ext_mem_k = 0;
319 	params->alt_mem_k = 0;
320 
321 	/* Always fill in RSDP: it is either 0 or a valid value */
322 	params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr;
323 
324 	/* Default APM info */
325 	memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
326 
327 	/* Default drive info */
328 	memset(&params->hd0_info, 0, sizeof(params->hd0_info));
329 	memset(&params->hd1_info, 0, sizeof(params->hd1_info));
330 
331 #ifdef CONFIG_CRASH_DUMP
332 	if (image->type == KEXEC_TYPE_CRASH) {
333 		ret = crash_setup_memmap_entries(image, params);
334 		if (ret)
335 			return ret;
336 	} else
337 #endif
338 		setup_e820_entries(params);
339 
340 	nr_e820_entries = params->e820_entries;
341 
342 	kexec_dprintk("E820 memmap:\n");
343 	for (i = 0; i < nr_e820_entries; i++) {
344 		kexec_dprintk("%016llx-%016llx (%d)\n",
345 			      params->e820_table[i].addr,
346 			      params->e820_table[i].addr + params->e820_table[i].size - 1,
347 			      params->e820_table[i].type);
348 		if (params->e820_table[i].type != E820_TYPE_RAM)
349 			continue;
350 		start = params->e820_table[i].addr;
351 		end = params->e820_table[i].addr + params->e820_table[i].size - 1;
352 
353 		if ((start <= 0x100000) && end > 0x100000) {
354 			mem_k = (end >> 10) - (0x100000 >> 10);
355 			params->screen_info.ext_mem_k = mem_k;
356 			params->alt_mem_k = mem_k;
357 			if (mem_k > 0xfc00)
358 				params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
359 			if (mem_k > 0xffffffff)
360 				params->alt_mem_k = 0xffffffff;
361 		}
362 	}
363 
364 #ifdef CONFIG_EFI
365 	/* Setup EFI state */
366 	setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
367 			setup_data_offset);
368 	setup_data_offset += sizeof(struct setup_data) +
369 			sizeof(struct efi_setup_data);
370 #endif
371 
372 #ifdef CONFIG_OF_FLATTREE
373 	if (image->force_dtb && initial_boot_params) {
374 		setup_dtb(params, params_load_addr, setup_data_offset);
375 		setup_data_offset += sizeof(struct setup_data) +
376 				     fdt_totalsize(initial_boot_params);
377 	} else {
378 		pr_debug("Not carrying over DTB, force_dtb = %d\n",
379 			 image->force_dtb);
380 	}
381 #endif
382 
383 	if (IS_ENABLED(CONFIG_IMA_KEXEC)) {
384 		/* Setup IMA log buffer state */
385 		setup_ima_state(image, params, params_load_addr,
386 				setup_data_offset);
387 		setup_data_offset += sizeof(struct setup_data) +
388 				     sizeof(struct ima_setup_data);
389 	}
390 
391 	if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
392 		/* Setup space to store preservation metadata */
393 		setup_kho(image, params, params_load_addr, setup_data_offset);
394 		setup_data_offset += sizeof(struct setup_data) +
395 				     sizeof(struct kho_data);
396 	}
397 
398 	/* Setup RNG seed */
399 	setup_rng_seed(params, params_load_addr, setup_data_offset);
400 
401 	/* Setup EDD info */
402 	memcpy(params->eddbuf, boot_params.eddbuf,
403 				EDDMAXNR * sizeof(struct edd_info));
404 	params->eddbuf_entries = boot_params.eddbuf_entries;
405 
406 	memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
407 	       EDD_MBR_SIG_MAX * sizeof(unsigned int));
408 
409 	return ret;
410 }
411 
412 static int bzImage64_probe(const char *buf, unsigned long len)
413 {
414 	int ret = -ENOEXEC;
415 	struct setup_header *header;
416 
417 	/* kernel should be at least two sectors long */
418 	if (len < 2 * 512) {
419 		pr_err("File is too short to be a bzImage\n");
420 		return ret;
421 	}
422 
423 	header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
424 	if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
425 		pr_err("Not a bzImage\n");
426 		return ret;
427 	}
428 
429 	if (header->boot_flag != 0xAA55) {
430 		pr_err("No x86 boot sector present\n");
431 		return ret;
432 	}
433 
434 	if (header->version < 0x020C) {
435 		pr_err("Must be at least protocol version 2.12\n");
436 		return ret;
437 	}
438 
439 	if (!(header->loadflags & LOADED_HIGH)) {
440 		pr_err("zImage not a bzImage\n");
441 		return ret;
442 	}
443 
444 	if (!(header->xloadflags & XLF_KERNEL_64)) {
445 		pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
446 		return ret;
447 	}
448 
449 	if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
450 		pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
451 		return ret;
452 	}
453 
454 	/*
455 	 * Can't handle 32bit EFI as it does not allow loading kernel
456 	 * above 4G. This should be handled by 32bit bzImage loader
457 	 */
458 	if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
459 		pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
460 		return ret;
461 	}
462 
463 	if (!(header->xloadflags & XLF_5LEVEL) && pgtable_l5_enabled()) {
464 		pr_err("bzImage cannot handle 5-level paging mode.\n");
465 		return ret;
466 	}
467 
468 	/* I've got a bzImage */
469 	pr_debug("It's a relocatable bzImage64\n");
470 	ret = 0;
471 
472 	return ret;
473 }
474 
475 static void *bzImage64_load(struct kimage *image, char *kernel,
476 			    unsigned long kernel_len, char *initrd,
477 			    unsigned long initrd_len, char *cmdline,
478 			    unsigned long cmdline_len)
479 {
480 
481 	struct setup_header *header;
482 	int setup_sects, kern16_size, ret = 0;
483 	unsigned long setup_header_size, params_cmdline_sz;
484 	struct boot_params *params;
485 	unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
486 	struct bzimage64_data *ldata;
487 	struct kexec_entry64_regs regs64;
488 	void *stack;
489 	unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
490 	unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
491 	struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
492 				  .top_down = true };
493 	struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR,
494 				  .buf_max = ULONG_MAX, .top_down = true };
495 
496 	header = (struct setup_header *)(kernel + setup_hdr_offset);
497 	setup_sects = header->setup_sects;
498 	if (setup_sects == 0)
499 		setup_sects = 4;
500 
501 	kern16_size = (setup_sects + 1) * 512;
502 	if (kernel_len < kern16_size) {
503 		pr_err("bzImage truncated\n");
504 		return ERR_PTR(-ENOEXEC);
505 	}
506 
507 	if (cmdline_len > header->cmdline_size) {
508 		pr_err("Kernel command line too long\n");
509 		return ERR_PTR(-EINVAL);
510 	}
511 
512 	/*
513 	 * In case of crash dump, we will append elfcorehdr=<addr> to
514 	 * command line. Make sure it does not overflow
515 	 */
516 	if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
517 		pr_err("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
518 		return ERR_PTR(-EINVAL);
519 	}
520 
521 #ifdef CONFIG_CRASH_DUMP
522 	/* Allocate and load backup region */
523 	if (image->type == KEXEC_TYPE_CRASH) {
524 		ret = crash_load_segments(image);
525 		if (ret)
526 			return ERR_PTR(ret);
527 		ret = crash_load_dm_crypt_keys(image);
528 		if (ret)
529 			return ERR_PTR(ret);
530 		if (image->dm_crypt_keys_addr &&
531 		    cmdline_len + MAX_ELFCOREHDR_STR_LEN + MAX_DMCRYPTKEYS_STR_LEN >
532 			    header->cmdline_size) {
533 			pr_err("Appending dmcryptkeys=<addr> to command line exceeds maximum allowed length\n");
534 			return ERR_PTR(-EINVAL);
535 		}
536 	}
537 #endif
538 
539 	/*
540 	 * Load purgatory. For 64bit entry point, purgatory  code can be
541 	 * anywhere.
542 	 */
543 	ret = kexec_load_purgatory(image, &pbuf);
544 	if (ret) {
545 		pr_err("Loading purgatory failed\n");
546 		return ERR_PTR(ret);
547 	}
548 
549 	kexec_dprintk("Loaded purgatory at 0x%lx\n", pbuf.mem);
550 
551 
552 	/*
553 	 * Load Bootparams and cmdline and space for efi stuff.
554 	 *
555 	 * Allocate memory together for multiple data structures so
556 	 * that they all can go in single area/segment and we don't
557 	 * have to create separate segment for each. Keeps things
558 	 * little bit simple
559 	 */
560 	efi_map_sz = efi_get_runtime_map_size();
561 	params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
562 				MAX_ELFCOREHDR_STR_LEN;
563 	if (image->dm_crypt_keys_addr)
564 		params_cmdline_sz += MAX_DMCRYPTKEYS_STR_LEN;
565 	params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
566 	kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) +
567 				sizeof(struct setup_data) +
568 				sizeof(struct efi_setup_data) +
569 				sizeof(struct setup_data) +
570 				RNG_SEED_LENGTH;
571 
572 #ifdef CONFIG_OF_FLATTREE
573 	if (image->force_dtb && initial_boot_params)
574 		kbuf.bufsz += sizeof(struct setup_data) +
575 			      fdt_totalsize(initial_boot_params);
576 #endif
577 
578 	if (IS_ENABLED(CONFIG_IMA_KEXEC))
579 		kbuf.bufsz += sizeof(struct setup_data) +
580 			      sizeof(struct ima_setup_data);
581 
582 	if (IS_ENABLED(CONFIG_KEXEC_HANDOVER))
583 		kbuf.bufsz += sizeof(struct setup_data) +
584 			      sizeof(struct kho_data);
585 
586 	params = kvzalloc(kbuf.bufsz, GFP_KERNEL);
587 	if (!params)
588 		return ERR_PTR(-ENOMEM);
589 	efi_map_offset = params_cmdline_sz;
590 	efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16);
591 
592 	/* Copy setup header onto bootparams. Documentation/arch/x86/boot.rst */
593 	setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
594 
595 	/* Is there a limit on setup header size? */
596 	memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
597 
598 	kbuf.buffer = params;
599 	kbuf.memsz = kbuf.bufsz;
600 	kbuf.buf_align = 16;
601 	kbuf.buf_min = MIN_BOOTPARAM_ADDR;
602 	ret = kexec_add_buffer(&kbuf);
603 	if (ret)
604 		goto out_free_params;
605 	bootparam_load_addr = kbuf.mem;
606 	kexec_dprintk("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
607 		      bootparam_load_addr, kbuf.bufsz, kbuf.memsz);
608 
609 	/* Load kernel */
610 	kbuf.buffer = kernel + kern16_size;
611 	kbuf.bufsz =  kernel_len - kern16_size;
612 	kbuf.memsz = PAGE_ALIGN(header->init_size);
613 	kbuf.buf_align = header->kernel_alignment;
614 	if (header->pref_address < MIN_KERNEL_LOAD_ADDR)
615 		kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
616 	else
617 		kbuf.buf_min = header->pref_address;
618 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
619 	ret = kexec_add_buffer(&kbuf);
620 	if (ret)
621 		goto out_free_params;
622 	kernel_load_addr = kbuf.mem;
623 
624 	kexec_dprintk("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
625 		      kernel_load_addr, kbuf.bufsz, kbuf.memsz);
626 
627 	/* Load initrd high */
628 	if (initrd) {
629 		kbuf.buffer = initrd;
630 		kbuf.bufsz = kbuf.memsz = initrd_len;
631 		kbuf.buf_align = PAGE_SIZE;
632 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
633 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
634 		ret = kexec_add_buffer(&kbuf);
635 		if (ret)
636 			goto out_free_params;
637 		initrd_load_addr = kbuf.mem;
638 
639 		kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
640 			      initrd_load_addr, initrd_len, initrd_len);
641 
642 		setup_initrd(params, initrd_load_addr, initrd_len);
643 	}
644 
645 	setup_cmdline(image, params, bootparam_load_addr,
646 		      sizeof(struct boot_params), cmdline, cmdline_len);
647 
648 	/* bootloader info. Do we need a separate ID for kexec kernel loader? */
649 	params->hdr.type_of_loader = 0x0D << 4;
650 	params->hdr.loadflags = 0;
651 
652 	/* Setup purgatory regs for entry */
653 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
654 					     sizeof(regs64), 1);
655 	if (ret)
656 		goto out_free_params;
657 
658 	regs64.rbx = 0; /* Bootstrap Processor */
659 	regs64.rsi = bootparam_load_addr;
660 	regs64.rip = kernel_load_addr + 0x200;
661 	stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
662 	if (IS_ERR(stack)) {
663 		pr_err("Could not find address of symbol stack_end\n");
664 		ret = -EINVAL;
665 		goto out_free_params;
666 	}
667 
668 	regs64.rsp = (unsigned long)stack;
669 	ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
670 					     sizeof(regs64), 0);
671 	if (ret)
672 		goto out_free_params;
673 
674 	ret = setup_boot_parameters(image, params, bootparam_load_addr,
675 				    efi_map_offset, efi_map_sz,
676 				    efi_setup_data_offset);
677 	if (ret)
678 		goto out_free_params;
679 
680 	/* Allocate loader specific data */
681 	ldata = kzalloc_obj(struct bzimage64_data);
682 	if (!ldata) {
683 		ret = -ENOMEM;
684 		goto out_free_params;
685 	}
686 
687 	/*
688 	 * Store pointer to params so that it could be freed after loading
689 	 * params segment has been loaded and contents have been copied
690 	 * somewhere else.
691 	 */
692 	ldata->bootparams_buf = params;
693 	return ldata;
694 
695 out_free_params:
696 	kvfree(params);
697 	return ERR_PTR(ret);
698 }
699 
700 /* This cleanup function is called after various segments have been loaded */
701 static int bzImage64_cleanup(void *loader_data)
702 {
703 	struct bzimage64_data *ldata = loader_data;
704 
705 	if (!ldata)
706 		return 0;
707 
708 	kvfree(ldata->bootparams_buf);
709 	ldata->bootparams_buf = NULL;
710 
711 	return 0;
712 }
713 
714 const struct kexec_file_ops kexec_bzImage64_ops = {
715 	.probe = bzImage64_probe,
716 	.load = bzImage64_load,
717 	.cleanup = bzImage64_cleanup,
718 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
719 	.verify_sig = kexec_kernel_verify_pe_sig,
720 #endif
721 };
722