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(¶ms->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 = ¶ms->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 = ¶ms->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(¶ms->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(¶ms->apm_bios_info, 0, sizeof(params->apm_bios_info)); 326 327 /* Default drive info */ 328 memset(¶ms->hd0_info, 0, sizeof(params->hd0_info)); 329 memset(¶ms->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 == -ENOENT) { 529 kexec_dprintk("No dm crypt key to load\n"); 530 } else if (ret) { 531 pr_err("Failed to load dm crypt keys\n"); 532 return ERR_PTR(ret); 533 } 534 if (image->dm_crypt_keys_addr && 535 cmdline_len + MAX_ELFCOREHDR_STR_LEN + MAX_DMCRYPTKEYS_STR_LEN > 536 header->cmdline_size) { 537 pr_err("Appending dmcryptkeys=<addr> to command line exceeds maximum allowed length\n"); 538 return ERR_PTR(-EINVAL); 539 } 540 } 541 #endif 542 543 /* 544 * Load purgatory. For 64bit entry point, purgatory code can be 545 * anywhere. 546 */ 547 ret = kexec_load_purgatory(image, &pbuf); 548 if (ret) { 549 pr_err("Loading purgatory failed\n"); 550 return ERR_PTR(ret); 551 } 552 553 kexec_dprintk("Loaded purgatory at 0x%lx\n", pbuf.mem); 554 555 556 /* 557 * Load Bootparams and cmdline and space for efi stuff. 558 * 559 * Allocate memory together for multiple data structures so 560 * that they all can go in single area/segment and we don't 561 * have to create separate segment for each. Keeps things 562 * little bit simple 563 */ 564 efi_map_sz = efi_get_runtime_map_size(); 565 params_cmdline_sz = sizeof(struct boot_params) + cmdline_len + 566 MAX_ELFCOREHDR_STR_LEN; 567 if (image->dm_crypt_keys_addr) 568 params_cmdline_sz += MAX_DMCRYPTKEYS_STR_LEN; 569 params_cmdline_sz = ALIGN(params_cmdline_sz, 16); 570 kbuf.bufsz = params_cmdline_sz + ALIGN(efi_map_sz, 16) + 571 sizeof(struct setup_data) + 572 sizeof(struct efi_setup_data) + 573 sizeof(struct setup_data) + 574 RNG_SEED_LENGTH; 575 576 #ifdef CONFIG_OF_FLATTREE 577 if (image->force_dtb && initial_boot_params) 578 kbuf.bufsz += sizeof(struct setup_data) + 579 fdt_totalsize(initial_boot_params); 580 #endif 581 582 if (IS_ENABLED(CONFIG_IMA_KEXEC)) 583 kbuf.bufsz += sizeof(struct setup_data) + 584 sizeof(struct ima_setup_data); 585 586 if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) 587 kbuf.bufsz += sizeof(struct setup_data) + 588 sizeof(struct kho_data); 589 590 params = kvzalloc(kbuf.bufsz, GFP_KERNEL); 591 if (!params) 592 return ERR_PTR(-ENOMEM); 593 efi_map_offset = params_cmdline_sz; 594 efi_setup_data_offset = efi_map_offset + ALIGN(efi_map_sz, 16); 595 596 /* Copy setup header onto bootparams. Documentation/arch/x86/boot.rst */ 597 setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset; 598 599 /* Is there a limit on setup header size? */ 600 memcpy(¶ms->hdr, (kernel + setup_hdr_offset), setup_header_size); 601 602 kbuf.buffer = params; 603 kbuf.memsz = kbuf.bufsz; 604 kbuf.buf_align = 16; 605 kbuf.buf_min = MIN_BOOTPARAM_ADDR; 606 ret = kexec_add_buffer(&kbuf); 607 if (ret) 608 goto out_free_params; 609 bootparam_load_addr = kbuf.mem; 610 kexec_dprintk("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 611 bootparam_load_addr, kbuf.bufsz, kbuf.memsz); 612 613 /* Load kernel */ 614 kbuf.buffer = kernel + kern16_size; 615 kbuf.bufsz = kernel_len - kern16_size; 616 kbuf.memsz = PAGE_ALIGN(header->init_size); 617 kbuf.buf_align = header->kernel_alignment; 618 if (header->pref_address < MIN_KERNEL_LOAD_ADDR) 619 kbuf.buf_min = MIN_KERNEL_LOAD_ADDR; 620 else 621 kbuf.buf_min = header->pref_address; 622 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 623 ret = kexec_add_buffer(&kbuf); 624 if (ret) 625 goto out_free_params; 626 kernel_load_addr = kbuf.mem; 627 628 kexec_dprintk("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 629 kernel_load_addr, kbuf.bufsz, kbuf.memsz); 630 631 /* Load initrd high */ 632 if (initrd) { 633 kbuf.buffer = initrd; 634 kbuf.bufsz = kbuf.memsz = initrd_len; 635 kbuf.buf_align = PAGE_SIZE; 636 kbuf.buf_min = MIN_INITRD_LOAD_ADDR; 637 kbuf.mem = KEXEC_BUF_MEM_UNKNOWN; 638 ret = kexec_add_buffer(&kbuf); 639 if (ret) 640 goto out_free_params; 641 initrd_load_addr = kbuf.mem; 642 643 kexec_dprintk("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 644 initrd_load_addr, initrd_len, initrd_len); 645 646 setup_initrd(params, initrd_load_addr, initrd_len); 647 } 648 649 setup_cmdline(image, params, bootparam_load_addr, 650 sizeof(struct boot_params), cmdline, cmdline_len); 651 652 /* bootloader info. Do we need a separate ID for kexec kernel loader? */ 653 params->hdr.type_of_loader = 0x0D << 4; 654 params->hdr.loadflags = 0; 655 656 /* Setup purgatory regs for entry */ 657 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64, 658 sizeof(regs64), 1); 659 if (ret) 660 goto out_free_params; 661 662 regs64.rbx = 0; /* Bootstrap Processor */ 663 regs64.rsi = bootparam_load_addr; 664 regs64.rip = kernel_load_addr + 0x200; 665 stack = kexec_purgatory_get_symbol_addr(image, "stack_end"); 666 if (IS_ERR(stack)) { 667 pr_err("Could not find address of symbol stack_end\n"); 668 ret = -EINVAL; 669 goto out_free_params; 670 } 671 672 regs64.rsp = (unsigned long)stack; 673 ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", ®s64, 674 sizeof(regs64), 0); 675 if (ret) 676 goto out_free_params; 677 678 ret = setup_boot_parameters(image, params, bootparam_load_addr, 679 efi_map_offset, efi_map_sz, 680 efi_setup_data_offset); 681 if (ret) 682 goto out_free_params; 683 684 /* Allocate loader specific data */ 685 ldata = kzalloc_obj(struct bzimage64_data); 686 if (!ldata) { 687 ret = -ENOMEM; 688 goto out_free_params; 689 } 690 691 /* 692 * Store pointer to params so that it could be freed after loading 693 * params segment has been loaded and contents have been copied 694 * somewhere else. 695 */ 696 ldata->bootparams_buf = params; 697 return ldata; 698 699 out_free_params: 700 kvfree(params); 701 return ERR_PTR(ret); 702 } 703 704 /* This cleanup function is called after various segments have been loaded */ 705 static int bzImage64_cleanup(void *loader_data) 706 { 707 struct bzimage64_data *ldata = loader_data; 708 709 if (!ldata) 710 return 0; 711 712 kvfree(ldata->bootparams_buf); 713 ldata->bootparams_buf = NULL; 714 715 return 0; 716 } 717 718 const struct kexec_file_ops kexec_bzImage64_ops = { 719 .probe = bzImage64_probe, 720 .load = bzImage64_load, 721 .cleanup = bzImage64_cleanup, 722 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG 723 .verify_sig = kexec_kernel_verify_pe_sig, 724 #endif 725 }; 726