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