File Index
Files and directories
Sign in
FILE

kexec-bzimage64.c

File preview with syntax highlighting.

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