1 /* 2 * Kexec bzImage loader 3 * 4 * Copyright (C) 2014 Red Hat Inc. 5 * Authors: 6 * Vivek Goyal <vgoyal@redhat.com> 7 * 8 * This source code is licensed under the GNU General Public License, 9 * Version 2. See the file COPYING for more details. 10 */ 11 12 #define pr_fmt(fmt) "kexec-bzImage64: " fmt 13 14 #include <linux/string.h> 15 #include <linux/printk.h> 16 #include <linux/errno.h> 17 #include <linux/slab.h> 18 #include <linux/kexec.h> 19 #include <linux/kernel.h> 20 #include <linux/mm.h> 21 #include <linux/efi.h> 22 #include <linux/verify_pefile.h> 23 #include <keys/system_keyring.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/kexec-bzimage64.h> 30 31 #define MAX_ELFCOREHDR_STR_LEN 30 /* elfcorehdr=0x<64bit-value> */ 32 33 /* 34 * Defines lowest physical address for various segments. Not sure where 35 * exactly these limits came from. Current bzimage64 loader in kexec-tools 36 * uses these so I am retaining it. It can be changed over time as we gain 37 * more insight. 38 */ 39 #define MIN_PURGATORY_ADDR 0x3000 40 #define MIN_BOOTPARAM_ADDR 0x3000 41 #define MIN_KERNEL_LOAD_ADDR 0x100000 42 #define MIN_INITRD_LOAD_ADDR 0x1000000 43 44 /* 45 * This is a place holder for all boot loader specific data structure which 46 * gets allocated in one call but gets freed much later during cleanup 47 * time. Right now there is only one field but it can grow as need be. 48 */ 49 struct bzimage64_data { 50 /* 51 * Temporary buffer to hold bootparams buffer. This should be 52 * freed once the bootparam segment has been loaded. 53 */ 54 void *bootparams_buf; 55 }; 56 57 static int setup_initrd(struct boot_params *params, 58 unsigned long initrd_load_addr, unsigned long initrd_len) 59 { 60 params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL; 61 params->hdr.ramdisk_size = initrd_len & 0xffffffffUL; 62 63 params->ext_ramdisk_image = initrd_load_addr >> 32; 64 params->ext_ramdisk_size = initrd_len >> 32; 65 66 return 0; 67 } 68 69 static int setup_cmdline(struct kimage *image, struct boot_params *params, 70 unsigned long bootparams_load_addr, 71 unsigned long cmdline_offset, char *cmdline, 72 unsigned long cmdline_len) 73 { 74 char *cmdline_ptr = ((char *)params) + cmdline_offset; 75 unsigned long cmdline_ptr_phys, len = 0; 76 uint32_t cmdline_low_32, cmdline_ext_32; 77 78 if (image->type == KEXEC_TYPE_CRASH) { 79 len = sprintf(cmdline_ptr, 80 "elfcorehdr=0x%lx ", image->arch.elf_load_addr); 81 } 82 memcpy(cmdline_ptr + len, cmdline, cmdline_len); 83 cmdline_len += len; 84 85 cmdline_ptr[cmdline_len - 1] = '\0'; 86 87 pr_debug("Final command line is: %s\n", cmdline_ptr); 88 cmdline_ptr_phys = bootparams_load_addr + cmdline_offset; 89 cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL; 90 cmdline_ext_32 = cmdline_ptr_phys >> 32; 91 92 params->hdr.cmd_line_ptr = cmdline_low_32; 93 if (cmdline_ext_32) 94 params->ext_cmd_line_ptr = cmdline_ext_32; 95 96 return 0; 97 } 98 99 static int setup_e820_entries(struct boot_params *params) 100 { 101 unsigned int nr_e820_entries; 102 103 nr_e820_entries = e820_saved.nr_map; 104 105 /* TODO: Pass entries more than E820MAX in bootparams setup data */ 106 if (nr_e820_entries > E820MAX) 107 nr_e820_entries = E820MAX; 108 109 params->e820_entries = nr_e820_entries; 110 memcpy(¶ms->e820_map, &e820_saved.map, 111 nr_e820_entries * sizeof(struct e820entry)); 112 113 return 0; 114 } 115 116 #ifdef CONFIG_EFI 117 static int setup_efi_info_memmap(struct boot_params *params, 118 unsigned long params_load_addr, 119 unsigned int efi_map_offset, 120 unsigned int efi_map_sz) 121 { 122 void *efi_map = (void *)params + efi_map_offset; 123 unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset; 124 struct efi_info *ei = ¶ms->efi_info; 125 126 if (!efi_map_sz) 127 return 0; 128 129 efi_runtime_map_copy(efi_map, efi_map_sz); 130 131 ei->efi_memmap = efi_map_phys_addr & 0xffffffff; 132 ei->efi_memmap_hi = efi_map_phys_addr >> 32; 133 ei->efi_memmap_size = efi_map_sz; 134 135 return 0; 136 } 137 138 static int 139 prepare_add_efi_setup_data(struct boot_params *params, 140 unsigned long params_load_addr, 141 unsigned int efi_setup_data_offset) 142 { 143 unsigned long setup_data_phys; 144 struct setup_data *sd = (void *)params + efi_setup_data_offset; 145 struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data); 146 147 esd->fw_vendor = efi.fw_vendor; 148 esd->runtime = efi.runtime; 149 esd->tables = efi.config_table; 150 esd->smbios = efi.smbios; 151 152 sd->type = SETUP_EFI; 153 sd->len = sizeof(struct efi_setup_data); 154 155 /* Add setup data */ 156 setup_data_phys = params_load_addr + efi_setup_data_offset; 157 sd->next = params->hdr.setup_data; 158 params->hdr.setup_data = setup_data_phys; 159 160 return 0; 161 } 162 163 static int 164 setup_efi_state(struct boot_params *params, unsigned long params_load_addr, 165 unsigned int efi_map_offset, unsigned int efi_map_sz, 166 unsigned int efi_setup_data_offset) 167 { 168 struct efi_info *current_ei = &boot_params.efi_info; 169 struct efi_info *ei = ¶ms->efi_info; 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 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 /* Default APM info */ 220 memset(¶ms->apm_bios_info, 0, sizeof(params->apm_bios_info)); 221 222 /* Default drive info */ 223 memset(¶ms->hd0_info, 0, sizeof(params->hd0_info)); 224 memset(¶ms->hd1_info, 0, sizeof(params->hd1_info)); 225 226 /* Default sysdesc table */ 227 params->sys_desc_table.length = 0; 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_map[i].type != E820_RAM) 240 continue; 241 start = params->e820_map[i].addr; 242 end = params->e820_map[i].addr + params->e820_map[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 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 atleast 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 /* I've got a bzImage */ 324 pr_debug("It's a relocatable bzImage64\n"); 325 ret = 0; 326 327 return ret; 328 } 329 330 static void *bzImage64_load(struct kimage *image, char *kernel, 331 unsigned long kernel_len, char *initrd, 332 unsigned long initrd_len, char *cmdline, 333 unsigned long cmdline_len) 334 { 335 336 struct setup_header *header; 337 int setup_sects, kern16_size, ret = 0; 338 unsigned long setup_header_size, params_cmdline_sz, params_misc_sz; 339 struct boot_params *params; 340 unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr; 341 unsigned long purgatory_load_addr; 342 unsigned long kernel_bufsz, kernel_memsz, kernel_align; 343 char *kernel_buf; 344 struct bzimage64_data *ldata; 345 struct kexec_entry64_regs regs64; 346 void *stack; 347 unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr); 348 unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset; 349 350 header = (struct setup_header *)(kernel + setup_hdr_offset); 351 setup_sects = header->setup_sects; 352 if (setup_sects == 0) 353 setup_sects = 4; 354 355 kern16_size = (setup_sects + 1) * 512; 356 if (kernel_len < kern16_size) { 357 pr_err("bzImage truncated\n"); 358 return ERR_PTR(-ENOEXEC); 359 } 360 361 if (cmdline_len > header->cmdline_size) { 362 pr_err("Kernel command line too long\n"); 363 return ERR_PTR(-EINVAL); 364 } 365 366 /* 367 * In case of crash dump, we will append elfcorehdr=<addr> to 368 * command line. Make sure it does not overflow 369 */ 370 if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) { 371 pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n"); 372 return ERR_PTR(-EINVAL); 373 } 374 375 /* Allocate and load backup region */ 376 if (image->type == KEXEC_TYPE_CRASH) { 377 ret = crash_load_segments(image); 378 if (ret) 379 return ERR_PTR(ret); 380 } 381 382 /* 383 * Load purgatory. For 64bit entry point, purgatory code can be 384 * anywhere. 385 */ 386 ret = kexec_load_purgatory(image, MIN_PURGATORY_ADDR, ULONG_MAX, 1, 387 &purgatory_load_addr); 388 if (ret) { 389 pr_err("Loading purgatory failed\n"); 390 return ERR_PTR(ret); 391 } 392 393 pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr); 394 395 396 /* 397 * Load Bootparams and cmdline and space for efi stuff. 398 * 399 * Allocate memory together for multiple data structures so 400 * that they all can go in single area/segment and we don't 401 * have to create separate segment for each. Keeps things 402 * little bit simple 403 */ 404 efi_map_sz = efi_get_runtime_map_size(); 405 efi_map_sz = ALIGN(efi_map_sz, 16); 406 params_cmdline_sz = sizeof(struct boot_params) + cmdline_len + 407 MAX_ELFCOREHDR_STR_LEN; 408 params_cmdline_sz = ALIGN(params_cmdline_sz, 16); 409 params_misc_sz = params_cmdline_sz + efi_map_sz + 410 sizeof(struct setup_data) + 411 sizeof(struct efi_setup_data); 412 413 params = kzalloc(params_misc_sz, GFP_KERNEL); 414 if (!params) 415 return ERR_PTR(-ENOMEM); 416 efi_map_offset = params_cmdline_sz; 417 efi_setup_data_offset = efi_map_offset + efi_map_sz; 418 419 /* Copy setup header onto bootparams. Documentation/x86/boot.txt */ 420 setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset; 421 422 /* Is there a limit on setup header size? */ 423 memcpy(¶ms->hdr, (kernel + setup_hdr_offset), setup_header_size); 424 425 ret = kexec_add_buffer(image, (char *)params, params_misc_sz, 426 params_misc_sz, 16, MIN_BOOTPARAM_ADDR, 427 ULONG_MAX, 1, &bootparam_load_addr); 428 if (ret) 429 goto out_free_params; 430 pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 431 bootparam_load_addr, params_misc_sz, params_misc_sz); 432 433 /* Load kernel */ 434 kernel_buf = kernel + kern16_size; 435 kernel_bufsz = kernel_len - kern16_size; 436 kernel_memsz = PAGE_ALIGN(header->init_size); 437 kernel_align = header->kernel_alignment; 438 439 ret = kexec_add_buffer(image, kernel_buf, 440 kernel_bufsz, kernel_memsz, kernel_align, 441 MIN_KERNEL_LOAD_ADDR, ULONG_MAX, 1, 442 &kernel_load_addr); 443 if (ret) 444 goto out_free_params; 445 446 pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n", 447 kernel_load_addr, kernel_memsz, kernel_memsz); 448 449 /* Load initrd high */ 450 if (initrd) { 451 ret = kexec_add_buffer(image, initrd, initrd_len, initrd_len, 452 PAGE_SIZE, MIN_INITRD_LOAD_ADDR, 453 ULONG_MAX, 1, &initrd_load_addr); 454 if (ret) 455 goto out_free_params; 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", ®s64, 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", ®s64, 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 bool trusted; 536 int ret; 537 538 ret = verify_pefile_signature(kernel, kernel_len, 539 system_trusted_keyring, &trusted); 540 if (ret < 0) 541 return ret; 542 if (!trusted) 543 return -EKEYREJECTED; 544 return 0; 545 } 546 #endif 547 548 struct kexec_file_ops kexec_bzImage64_ops = { 549 .probe = bzImage64_probe, 550 .load = bzImage64_load, 551 .cleanup = bzImage64_cleanup, 552 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG 553 .verify_sig = bzImage64_verify_sig, 554 #endif 555 }; 556