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(¶ms->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 = ¶ms->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->runtime = efi.runtime; 146 esd->tables = efi.config_table; 147 esd->smbios = efi.smbios; 148 149 sd->type = SETUP_EFI; 150 sd->len = sizeof(struct efi_setup_data); 151 152 /* Add setup data */ 153 setup_data_phys = params_load_addr + efi_setup_data_offset; 154 sd->next = params->hdr.setup_data; 155 params->hdr.setup_data = setup_data_phys; 156 157 return 0; 158 } 159 160 static int 161 setup_efi_state(struct boot_params *params, unsigned long params_load_addr, 162 unsigned int efi_map_offset, unsigned int efi_map_sz, 163 unsigned int efi_setup_data_offset) 164 { 165 struct efi_info *current_ei = &boot_params.efi_info; 166 struct efi_info *ei = ¶ms->efi_info; 167 168 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 169 return 0; 170 171 if (!current_ei->efi_memmap_size) 172 return 0; 173 174 /* 175 * If 1:1 mapping is not enabled, second kernel can not setup EFI 176 * and use EFI run time services. User space will have to pass 177 * acpi_rsdp=<addr> on kernel command line to make second kernel boot 178 * without efi. 179 */ 180 if (efi_enabled(EFI_OLD_MEMMAP)) 181 return 0; 182 183 params->secure_boot = boot_params.secure_boot; 184 ei->efi_loader_signature = current_ei->efi_loader_signature; 185 ei->efi_systab = current_ei->efi_systab; 186 ei->efi_systab_hi = current_ei->efi_systab_hi; 187 188 ei->efi_memdesc_version = current_ei->efi_memdesc_version; 189 ei->efi_memdesc_size = efi_get_runtime_map_desc_size(); 190 191 setup_efi_info_memmap(params, params_load_addr, efi_map_offset, 192 efi_map_sz); 193 prepare_add_efi_setup_data(params, params_load_addr, 194 efi_setup_data_offset); 195 return 0; 196 } 197 #endif /* CONFIG_EFI */ 198 199 static int 200 setup_boot_parameters(struct kimage *image, struct boot_params *params, 201 unsigned long params_load_addr, 202 unsigned int efi_map_offset, unsigned int efi_map_sz, 203 unsigned int efi_setup_data_offset) 204 { 205 unsigned int nr_e820_entries; 206 unsigned long long mem_k, start, end; 207 int i, ret = 0; 208 209 /* Get subarch from existing bootparams */ 210 params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch; 211 212 /* Copying screen_info will do? */ 213 memcpy(¶ms->screen_info, &boot_params.screen_info, 214 sizeof(struct screen_info)); 215 216 /* Fill in memsize later */ 217 params->screen_info.ext_mem_k = 0; 218 params->alt_mem_k = 0; 219 220 /* Always fill in RSDP: it is either 0 or a valid value */ 221 params->acpi_rsdp_addr = boot_params.acpi_rsdp_addr; 222 223 /* Default APM info */ 224 memset(¶ms->apm_bios_info, 0, sizeof(params->apm_bios_info)); 225 226 /* Default drive info */ 227 memset(¶ms->hd0_info, 0, sizeof(params->hd0_info)); 228 memset(¶ms->hd1_info, 0, sizeof(params->hd1_info)); 229 230 if (image->type == KEXEC_TYPE_CRASH) { 231 ret = crash_setup_memmap_entries(image, params); 232 if (ret) 233 return ret; 234 } else 235 setup_e820_entries(params); 236 237 nr_e820_entries = params->e820_entries; 238 239 for (i = 0; i < nr_e820_entries; i++) { 240 if (params->e820_table[i].type != E820_TYPE_RAM) 241 continue; 242 start = params->e820_table[i].addr; 243 end = params->e820_table[i].addr + params->e820_table[i].size - 1; 244 245 if ((start <= 0x100000) && end > 0x100000) { 246 mem_k = (end >> 10) - (0x100000 >> 10); 247 params->screen_info.ext_mem_k = mem_k; 248 params->alt_mem_k = mem_k; 249 if (mem_k > 0xfc00) 250 params->screen_info.ext_mem_k = 0xfc00; /* 64M*/ 251 if (mem_k > 0xffffffff) 252 params->alt_mem_k = 0xffffffff; 253 } 254 } 255 256 #ifdef CONFIG_EFI 257 /* Setup EFI state */ 258 setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz, 259 efi_setup_data_offset); 260 #endif 261 /* Setup EDD info */ 262 memcpy(params->eddbuf, boot_params.eddbuf, 263 EDDMAXNR * sizeof(struct edd_info)); 264 params->eddbuf_entries = boot_params.eddbuf_entries; 265 266 memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer, 267 EDD_MBR_SIG_MAX * sizeof(unsigned int)); 268 269 return ret; 270 } 271 272 static int bzImage64_probe(const char *buf, unsigned long len) 273 { 274 int ret = -ENOEXEC; 275 struct setup_header *header; 276 277 /* kernel should be at least two sectors long */ 278 if (len < 2 * 512) { 279 pr_err("File is too short to be a bzImage\n"); 280 return ret; 281 } 282 283 header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr)); 284 if (memcmp((char *)&header->header, "HdrS", 4) != 0) { 285 pr_err("Not a bzImage\n"); 286 return ret; 287 } 288 289 if (header->boot_flag != 0xAA55) { 290 pr_err("No x86 boot sector present\n"); 291 return ret; 292 } 293 294 if (header->version < 0x020C) { 295 pr_err("Must be at least protocol version 2.12\n"); 296 return ret; 297 } 298 299 if (!(header->loadflags & LOADED_HIGH)) { 300 pr_err("zImage not a bzImage\n"); 301 return ret; 302 } 303 304 if (!(header->xloadflags & XLF_KERNEL_64)) { 305 pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n"); 306 return ret; 307 } 308 309 if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) { 310 pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n"); 311 return ret; 312 } 313 314 /* 315 * Can't handle 32bit EFI as it does not allow loading kernel 316 * above 4G. This should be handled by 32bit bzImage loader 317 */ 318 if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) { 319 pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n"); 320 return ret; 321 } 322 323 if (!(header->xloadflags & XLF_5LEVEL) && pgtable_l5_enabled()) { 324 pr_err("bzImage cannot handle 5-level paging mode.\n"); 325 return ret; 326 } 327 328 /* I've got a bzImage */ 329 pr_debug("It's a relocatable bzImage64\n"); 330 ret = 0; 331 332 return ret; 333 } 334 335 static void *bzImage64_load(struct kimage *image, char *kernel, 336 unsigned long kernel_len, char *initrd, 337 unsigned long initrd_len, char *cmdline, 338 unsigned long cmdline_len) 339 { 340 341 struct setup_header *header; 342 int setup_sects, kern16_size, ret = 0; 343 unsigned long setup_header_size, params_cmdline_sz; 344 struct boot_params *params; 345 unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr; 346 struct bzimage64_data *ldata; 347 struct kexec_entry64_regs regs64; 348 void *stack; 349 unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr); 350 unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset; 351 struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX, 352 .top_down = true }; 353 struct kexec_buf pbuf = { .image = image, .buf_min = MIN_PURGATORY_ADDR, 354 .buf_max = ULONG_MAX, .top_down = true }; 355 356 header = (struct setup_header *)(kernel + setup_hdr_offset); 357 setup_sects = header->setup_sects; 358 if (setup_sects == 0) 359 setup_sects = 4; 360 361 kern16_size = (setup_sects + 1) * 512; 362 if (kernel_len < kern16_size) { 363 pr_err("bzImage truncated\n"); 364 return ERR_PTR(-ENOEXEC); 365 } 366 367 if (cmdline_len > header->cmdline_size) { 368 pr_err("Kernel command line too long\n"); 369 return ERR_PTR(-EINVAL); 370 } 371 372 /* 373 * In case of crash dump, we will append elfcorehdr=<addr> to 374 * command line. Make sure it does not overflow 375 */ 376 if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) { 377 pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n"); 378 return ERR_PTR(-EINVAL); 379 } 380 381 /* Allocate and load backup region */ 382 if (image->type == KEXEC_TYPE_CRASH) { 383 ret = crash_load_segments(image); 384 if (ret) 385 return ERR_PTR(ret); 386 } 387 388 /* 389 * Load purgatory. For 64bit entry point, purgatory code can be 390 * anywhere. 391 */ 392 ret = kexec_load_purgatory(image, &pbuf); 393 if (ret) { 394 pr_err("Loading purgatory failed\n"); 395 return ERR_PTR(ret); 396 } 397 398 pr_debug("Loaded purgatory at 0x%lx\n", pbuf.mem); 399 400 401 /* 402 * Load Bootparams and cmdline and space for efi stuff. 403 * 404 * Allocate memory together for multiple data structures so 405 * that they all can go in single area/segment and we don't 406 * have to create separate segment for each. Keeps things 407 * little bit simple 408 */ 409 efi_map_sz = efi_get_runtime_map_size(); 410 params_cmdline_sz = sizeof(struct boot_params) + cmdline_len + 411 MAX_ELFCOREHDR_STR_LEN; 412 params_cmdline_sz = ALIGN(params_cmdline_sz, 16); 413 kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) + 414 sizeof(struct setup_data) + 415 sizeof(struct efi_setup_data); 416 417 params = kzalloc(kbuf.bufsz, GFP_KERNEL); 418 if (!params) 419 return ERR_PTR(-ENOMEM); 420 efi_map_offset = params_cmdline_sz; 421 efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16); 422 423 /* Copy setup header onto bootparams. Documentation/x86/boot.rst */ 424 setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset; 425 426 /* Is there a limit on setup header size? */ 427 memcpy(¶ms->hdr, (kernel + setup_hdr_offset), setup_header_size); 428 429 kbuf.buffer = params; 430 kbuf.memsz = kbuf.bufsz; 431 kbuf.buf_align = 16; 432 kbuf.buf_min = MIN_BOOTPARAM_ADDR; 433 ret = kexec_add_buffer(&kbuf); 434 if (ret) 435 goto out_free_params; 436 bootparam_load_addr = kbuf.mem; 437 pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 438 bootparam_load_addr, kbuf.bufsz, kbuf.bufsz); 439 440 /* Load kernel */ 441 kbuf.buffer = kernel + kern16_size; 442 kbuf.bufsz = kernel_len - kern16_size; 443 kbuf.memsz = PAGE_ALIGN(header->init_size); 444 kbuf.buf_align = header->kernel_alignment; 445 kbuf.buf_min = MIN_KERNEL_LOAD_ADDR; 446 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 447 ret = kexec_add_buffer(&kbuf); 448 if (ret) 449 goto out_free_params; 450 kernel_load_addr = kbuf.mem; 451 452 pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 453 kernel_load_addr, kbuf.bufsz, kbuf.memsz); 454 455 /* Load initrd high */ 456 if (initrd) { 457 kbuf.buffer = initrd; 458 kbuf.bufsz = kbuf.memsz = initrd_len; 459 kbuf.buf_align = PAGE_SIZE; 460 kbuf.buf_min = MIN_INITRD_LOAD_ADDR; 461 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 462 ret = kexec_add_buffer(&kbuf); 463 if (ret) 464 goto out_free_params; 465 initrd_load_addr = kbuf.mem; 466 467 pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 468 initrd_load_addr, initrd_len, initrd_len); 469 470 setup_initrd(params, initrd_load_addr, initrd_len); 471 } 472 473 setup_cmdline(image, params, bootparam_load_addr, 474 sizeof(struct boot_params), cmdline, cmdline_len); 475 476 /* bootloader info. Do we need a separate ID for kexec kernel loader? */ 477 params->hdr.type_of_loader = 0x0D << 4; 478 params->hdr.loadflags = 0; 479 480 /* Setup purgatory regs for entry */ 481 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64, 482 sizeof(regs64), 1); 483 if (ret) 484 goto out_free_params; 485 486 regs64.rbx = 0; /* Bootstrap Processor */ 487 regs64.rsi = bootparam_load_addr; 488 regs64.rip = kernel_load_addr + 0x200; 489 stack = kexec_purgatory_get_symbol_addr(image, "stack_end"); 490 if (IS_ERR(stack)) { 491 pr_err("Could not find address of symbol stack_end\n"); 492 ret = -EINVAL; 493 goto out_free_params; 494 } 495 496 regs64.rsp = (unsigned long)stack; 497 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64, 498 sizeof(regs64), 0); 499 if (ret) 500 goto out_free_params; 501 502 ret = setup_boot_parameters(image, params, bootparam_load_addr, 503 efi_map_offset, efi_map_sz, 504 efi_setup_data_offset); 505 if (ret) 506 goto out_free_params; 507 508 /* Allocate loader specific data */ 509 ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL); 510 if (!ldata) { 511 ret = -ENOMEM; 512 goto out_free_params; 513 } 514 515 /* 516 * Store pointer to params so that it could be freed after loading 517 * params segment has been loaded and contents have been copied 518 * somewhere else. 519 */ 520 ldata->bootparams_buf = params; 521 return ldata; 522 523 out_free_params: 524 kfree(params); 525 return ERR_PTR(ret); 526 } 527 528 /* This cleanup function is called after various segments have been loaded */ 529 static int bzImage64_cleanup(void *loader_data) 530 { 531 struct bzimage64_data *ldata = loader_data; 532 533 if (!ldata) 534 return 0; 535 536 kfree(ldata->bootparams_buf); 537 ldata->bootparams_buf = NULL; 538 539 return 0; 540 } 541 542 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG 543 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len) 544 { 545 int ret; 546 547 ret = verify_pefile_signature(kernel, kernel_len, 548 VERIFY_USE_SECONDARY_KEYRING, 549 VERIFYING_KEXEC_PE_SIGNATURE); 550 if (ret == -ENOKEY && IS_ENABLED(CONFIG_INTEGRITY_PLATFORM_KEYRING)) { 551 ret = verify_pefile_signature(kernel, kernel_len, 552 VERIFY_USE_PLATFORM_KEYRING, 553 VERIFYING_KEXEC_PE_SIGNATURE); 554 } 555 return ret; 556 } 557 #endif 558 559 const struct kexec_file_ops kexec_bzImage64_ops = { 560 .probe = bzImage64_probe, 561 .load = bzImage64_load, 562 .cleanup = bzImage64_cleanup, 563 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG 564 .verify_sig = bzImage64_verify_sig, 565 #endif 566 }; 567