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