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