Back to home page

LXR

 
 

    


0001 /*
0002  * Kexec bzImage loader
0003  *
0004  * Copyright (C) 2014 Red Hat Inc.
0005  * Authors:
0006  *      Vivek Goyal <vgoyal@redhat.com>
0007  *
0008  * This source code is licensed under the GNU General Public License,
0009  * Version 2.  See the file COPYING for more details.
0010  */
0011 
0012 #define pr_fmt(fmt) "kexec-bzImage64: " fmt
0013 
0014 #include <linux/string.h>
0015 #include <linux/printk.h>
0016 #include <linux/errno.h>
0017 #include <linux/slab.h>
0018 #include <linux/kexec.h>
0019 #include <linux/kernel.h>
0020 #include <linux/mm.h>
0021 #include <linux/efi.h>
0022 #include <linux/verification.h>
0023 
0024 #include <asm/bootparam.h>
0025 #include <asm/setup.h>
0026 #include <asm/crash.h>
0027 #include <asm/efi.h>
0028 #include <asm/kexec-bzimage64.h>
0029 
0030 #define MAX_ELFCOREHDR_STR_LEN  30  /* elfcorehdr=0x<64bit-value> */
0031 
0032 /*
0033  * Defines lowest physical address for various segments. Not sure where
0034  * exactly these limits came from. Current bzimage64 loader in kexec-tools
0035  * uses these so I am retaining it. It can be changed over time as we gain
0036  * more insight.
0037  */
0038 #define MIN_PURGATORY_ADDR  0x3000
0039 #define MIN_BOOTPARAM_ADDR  0x3000
0040 #define MIN_KERNEL_LOAD_ADDR    0x100000
0041 #define MIN_INITRD_LOAD_ADDR    0x1000000
0042 
0043 /*
0044  * This is a place holder for all boot loader specific data structure which
0045  * gets allocated in one call but gets freed much later during cleanup
0046  * time. Right now there is only one field but it can grow as need be.
0047  */
0048 struct bzimage64_data {
0049     /*
0050      * Temporary buffer to hold bootparams buffer. This should be
0051      * freed once the bootparam segment has been loaded.
0052      */
0053     void *bootparams_buf;
0054 };
0055 
0056 static int setup_initrd(struct boot_params *params,
0057         unsigned long initrd_load_addr, unsigned long initrd_len)
0058 {
0059     params->hdr.ramdisk_image = initrd_load_addr & 0xffffffffUL;
0060     params->hdr.ramdisk_size = initrd_len & 0xffffffffUL;
0061 
0062     params->ext_ramdisk_image = initrd_load_addr >> 32;
0063     params->ext_ramdisk_size = initrd_len >> 32;
0064 
0065     return 0;
0066 }
0067 
0068 static int setup_cmdline(struct kimage *image, struct boot_params *params,
0069              unsigned long bootparams_load_addr,
0070              unsigned long cmdline_offset, char *cmdline,
0071              unsigned long cmdline_len)
0072 {
0073     char *cmdline_ptr = ((char *)params) + cmdline_offset;
0074     unsigned long cmdline_ptr_phys, len = 0;
0075     uint32_t cmdline_low_32, cmdline_ext_32;
0076 
0077     if (image->type == KEXEC_TYPE_CRASH) {
0078         len = sprintf(cmdline_ptr,
0079             "elfcorehdr=0x%lx ", image->arch.elf_load_addr);
0080     }
0081     memcpy(cmdline_ptr + len, cmdline, cmdline_len);
0082     cmdline_len += len;
0083 
0084     cmdline_ptr[cmdline_len - 1] = '\0';
0085 
0086     pr_debug("Final command line is: %s\n", cmdline_ptr);
0087     cmdline_ptr_phys = bootparams_load_addr + cmdline_offset;
0088     cmdline_low_32 = cmdline_ptr_phys & 0xffffffffUL;
0089     cmdline_ext_32 = cmdline_ptr_phys >> 32;
0090 
0091     params->hdr.cmd_line_ptr = cmdline_low_32;
0092     if (cmdline_ext_32)
0093         params->ext_cmd_line_ptr = cmdline_ext_32;
0094 
0095     return 0;
0096 }
0097 
0098 static int setup_e820_entries(struct boot_params *params)
0099 {
0100     unsigned int nr_e820_entries;
0101 
0102     nr_e820_entries = e820_saved->nr_map;
0103 
0104     /* TODO: Pass entries more than E820MAX in bootparams setup data */
0105     if (nr_e820_entries > E820MAX)
0106         nr_e820_entries = E820MAX;
0107 
0108     params->e820_entries = nr_e820_entries;
0109     memcpy(&params->e820_map, &e820_saved->map,
0110            nr_e820_entries * sizeof(struct e820entry));
0111 
0112     return 0;
0113 }
0114 
0115 #ifdef CONFIG_EFI
0116 static int setup_efi_info_memmap(struct boot_params *params,
0117                   unsigned long params_load_addr,
0118                   unsigned int efi_map_offset,
0119                   unsigned int efi_map_sz)
0120 {
0121     void *efi_map = (void *)params + efi_map_offset;
0122     unsigned long efi_map_phys_addr = params_load_addr + efi_map_offset;
0123     struct efi_info *ei = &params->efi_info;
0124 
0125     if (!efi_map_sz)
0126         return 0;
0127 
0128     efi_runtime_map_copy(efi_map, efi_map_sz);
0129 
0130     ei->efi_memmap = efi_map_phys_addr & 0xffffffff;
0131     ei->efi_memmap_hi = efi_map_phys_addr >> 32;
0132     ei->efi_memmap_size = efi_map_sz;
0133 
0134     return 0;
0135 }
0136 
0137 static int
0138 prepare_add_efi_setup_data(struct boot_params *params,
0139                unsigned long params_load_addr,
0140                unsigned int efi_setup_data_offset)
0141 {
0142     unsigned long setup_data_phys;
0143     struct setup_data *sd = (void *)params + efi_setup_data_offset;
0144     struct efi_setup_data *esd = (void *)sd + sizeof(struct setup_data);
0145 
0146     esd->fw_vendor = efi.fw_vendor;
0147     esd->runtime = efi.runtime;
0148     esd->tables = efi.config_table;
0149     esd->smbios = efi.smbios;
0150 
0151     sd->type = SETUP_EFI;
0152     sd->len = sizeof(struct efi_setup_data);
0153 
0154     /* Add setup data */
0155     setup_data_phys = params_load_addr + efi_setup_data_offset;
0156     sd->next = params->hdr.setup_data;
0157     params->hdr.setup_data = setup_data_phys;
0158 
0159     return 0;
0160 }
0161 
0162 static int
0163 setup_efi_state(struct boot_params *params, unsigned long params_load_addr,
0164         unsigned int efi_map_offset, unsigned int efi_map_sz,
0165         unsigned int efi_setup_data_offset)
0166 {
0167     struct efi_info *current_ei = &boot_params.efi_info;
0168     struct efi_info *ei = &params->efi_info;
0169 
0170     if (!current_ei->efi_memmap_size)
0171         return 0;
0172 
0173     /*
0174      * If 1:1 mapping is not enabled, second kernel can not setup EFI
0175      * and use EFI run time services. User space will have to pass
0176      * acpi_rsdp=<addr> on kernel command line to make second kernel boot
0177      * without efi.
0178      */
0179     if (efi_enabled(EFI_OLD_MEMMAP))
0180         return 0;
0181 
0182     ei->efi_loader_signature = current_ei->efi_loader_signature;
0183     ei->efi_systab = current_ei->efi_systab;
0184     ei->efi_systab_hi = current_ei->efi_systab_hi;
0185 
0186     ei->efi_memdesc_version = current_ei->efi_memdesc_version;
0187     ei->efi_memdesc_size = efi_get_runtime_map_desc_size();
0188 
0189     setup_efi_info_memmap(params, params_load_addr, efi_map_offset,
0190                   efi_map_sz);
0191     prepare_add_efi_setup_data(params, params_load_addr,
0192                    efi_setup_data_offset);
0193     return 0;
0194 }
0195 #endif /* CONFIG_EFI */
0196 
0197 static int
0198 setup_boot_parameters(struct kimage *image, struct boot_params *params,
0199               unsigned long params_load_addr,
0200               unsigned int efi_map_offset, unsigned int efi_map_sz,
0201               unsigned int efi_setup_data_offset)
0202 {
0203     unsigned int nr_e820_entries;
0204     unsigned long long mem_k, start, end;
0205     int i, ret = 0;
0206 
0207     /* Get subarch from existing bootparams */
0208     params->hdr.hardware_subarch = boot_params.hdr.hardware_subarch;
0209 
0210     /* Copying screen_info will do? */
0211     memcpy(&params->screen_info, &boot_params.screen_info,
0212                 sizeof(struct screen_info));
0213 
0214     /* Fill in memsize later */
0215     params->screen_info.ext_mem_k = 0;
0216     params->alt_mem_k = 0;
0217 
0218     /* Default APM info */
0219     memset(&params->apm_bios_info, 0, sizeof(params->apm_bios_info));
0220 
0221     /* Default drive info */
0222     memset(&params->hd0_info, 0, sizeof(params->hd0_info));
0223     memset(&params->hd1_info, 0, sizeof(params->hd1_info));
0224 
0225     if (image->type == KEXEC_TYPE_CRASH) {
0226         ret = crash_setup_memmap_entries(image, params);
0227         if (ret)
0228             return ret;
0229     } else
0230         setup_e820_entries(params);
0231 
0232     nr_e820_entries = params->e820_entries;
0233 
0234     for (i = 0; i < nr_e820_entries; i++) {
0235         if (params->e820_map[i].type != E820_RAM)
0236             continue;
0237         start = params->e820_map[i].addr;
0238         end = params->e820_map[i].addr + params->e820_map[i].size - 1;
0239 
0240         if ((start <= 0x100000) && end > 0x100000) {
0241             mem_k = (end >> 10) - (0x100000 >> 10);
0242             params->screen_info.ext_mem_k = mem_k;
0243             params->alt_mem_k = mem_k;
0244             if (mem_k > 0xfc00)
0245                 params->screen_info.ext_mem_k = 0xfc00; /* 64M*/
0246             if (mem_k > 0xffffffff)
0247                 params->alt_mem_k = 0xffffffff;
0248         }
0249     }
0250 
0251 #ifdef CONFIG_EFI
0252     /* Setup EFI state */
0253     setup_efi_state(params, params_load_addr, efi_map_offset, efi_map_sz,
0254             efi_setup_data_offset);
0255 #endif
0256 
0257     /* Setup EDD info */
0258     memcpy(params->eddbuf, boot_params.eddbuf,
0259                 EDDMAXNR * sizeof(struct edd_info));
0260     params->eddbuf_entries = boot_params.eddbuf_entries;
0261 
0262     memcpy(params->edd_mbr_sig_buffer, boot_params.edd_mbr_sig_buffer,
0263            EDD_MBR_SIG_MAX * sizeof(unsigned int));
0264 
0265     return ret;
0266 }
0267 
0268 static int bzImage64_probe(const char *buf, unsigned long len)
0269 {
0270     int ret = -ENOEXEC;
0271     struct setup_header *header;
0272 
0273     /* kernel should be at least two sectors long */
0274     if (len < 2 * 512) {
0275         pr_err("File is too short to be a bzImage\n");
0276         return ret;
0277     }
0278 
0279     header = (struct setup_header *)(buf + offsetof(struct boot_params, hdr));
0280     if (memcmp((char *)&header->header, "HdrS", 4) != 0) {
0281         pr_err("Not a bzImage\n");
0282         return ret;
0283     }
0284 
0285     if (header->boot_flag != 0xAA55) {
0286         pr_err("No x86 boot sector present\n");
0287         return ret;
0288     }
0289 
0290     if (header->version < 0x020C) {
0291         pr_err("Must be at least protocol version 2.12\n");
0292         return ret;
0293     }
0294 
0295     if (!(header->loadflags & LOADED_HIGH)) {
0296         pr_err("zImage not a bzImage\n");
0297         return ret;
0298     }
0299 
0300     if (!(header->xloadflags & XLF_KERNEL_64)) {
0301         pr_err("Not a bzImage64. XLF_KERNEL_64 is not set.\n");
0302         return ret;
0303     }
0304 
0305     if (!(header->xloadflags & XLF_CAN_BE_LOADED_ABOVE_4G)) {
0306         pr_err("XLF_CAN_BE_LOADED_ABOVE_4G is not set.\n");
0307         return ret;
0308     }
0309 
0310     /*
0311      * Can't handle 32bit EFI as it does not allow loading kernel
0312      * above 4G. This should be handled by 32bit bzImage loader
0313      */
0314     if (efi_enabled(EFI_RUNTIME_SERVICES) && !efi_enabled(EFI_64BIT)) {
0315         pr_debug("EFI is 32 bit. Can't load kernel above 4G.\n");
0316         return ret;
0317     }
0318 
0319     /* I've got a bzImage */
0320     pr_debug("It's a relocatable bzImage64\n");
0321     ret = 0;
0322 
0323     return ret;
0324 }
0325 
0326 static void *bzImage64_load(struct kimage *image, char *kernel,
0327                 unsigned long kernel_len, char *initrd,
0328                 unsigned long initrd_len, char *cmdline,
0329                 unsigned long cmdline_len)
0330 {
0331 
0332     struct setup_header *header;
0333     int setup_sects, kern16_size, ret = 0;
0334     unsigned long setup_header_size, params_cmdline_sz;
0335     struct boot_params *params;
0336     unsigned long bootparam_load_addr, kernel_load_addr, initrd_load_addr;
0337     unsigned long purgatory_load_addr;
0338     struct bzimage64_data *ldata;
0339     struct kexec_entry64_regs regs64;
0340     void *stack;
0341     unsigned int setup_hdr_offset = offsetof(struct boot_params, hdr);
0342     unsigned int efi_map_offset, efi_map_sz, efi_setup_data_offset;
0343     struct kexec_buf kbuf = { .image = image, .buf_max = ULONG_MAX,
0344                   .top_down = true };
0345 
0346     header = (struct setup_header *)(kernel + setup_hdr_offset);
0347     setup_sects = header->setup_sects;
0348     if (setup_sects == 0)
0349         setup_sects = 4;
0350 
0351     kern16_size = (setup_sects + 1) * 512;
0352     if (kernel_len < kern16_size) {
0353         pr_err("bzImage truncated\n");
0354         return ERR_PTR(-ENOEXEC);
0355     }
0356 
0357     if (cmdline_len > header->cmdline_size) {
0358         pr_err("Kernel command line too long\n");
0359         return ERR_PTR(-EINVAL);
0360     }
0361 
0362     /*
0363      * In case of crash dump, we will append elfcorehdr=<addr> to
0364      * command line. Make sure it does not overflow
0365      */
0366     if (cmdline_len + MAX_ELFCOREHDR_STR_LEN > header->cmdline_size) {
0367         pr_debug("Appending elfcorehdr=<addr> to command line exceeds maximum allowed length\n");
0368         return ERR_PTR(-EINVAL);
0369     }
0370 
0371     /* Allocate and load backup region */
0372     if (image->type == KEXEC_TYPE_CRASH) {
0373         ret = crash_load_segments(image);
0374         if (ret)
0375             return ERR_PTR(ret);
0376     }
0377 
0378     /*
0379      * Load purgatory. For 64bit entry point, purgatory  code can be
0380      * anywhere.
0381      */
0382     ret = kexec_load_purgatory(image, MIN_PURGATORY_ADDR, ULONG_MAX, 1,
0383                    &purgatory_load_addr);
0384     if (ret) {
0385         pr_err("Loading purgatory failed\n");
0386         return ERR_PTR(ret);
0387     }
0388 
0389     pr_debug("Loaded purgatory at 0x%lx\n", purgatory_load_addr);
0390 
0391 
0392     /*
0393      * Load Bootparams and cmdline and space for efi stuff.
0394      *
0395      * Allocate memory together for multiple data structures so
0396      * that they all can go in single area/segment and we don't
0397      * have to create separate segment for each. Keeps things
0398      * little bit simple
0399      */
0400     efi_map_sz = efi_get_runtime_map_size();
0401     efi_map_sz = ALIGN(efi_map_sz, 16);
0402     params_cmdline_sz = sizeof(struct boot_params) + cmdline_len +
0403                 MAX_ELFCOREHDR_STR_LEN;
0404     params_cmdline_sz = ALIGN(params_cmdline_sz, 16);
0405     kbuf.bufsz = params_cmdline_sz + efi_map_sz +
0406                 sizeof(struct setup_data) +
0407                 sizeof(struct efi_setup_data);
0408 
0409     params = kzalloc(kbuf.bufsz, GFP_KERNEL);
0410     if (!params)
0411         return ERR_PTR(-ENOMEM);
0412     efi_map_offset = params_cmdline_sz;
0413     efi_setup_data_offset = efi_map_offset + efi_map_sz;
0414 
0415     /* Copy setup header onto bootparams. Documentation/x86/boot.txt */
0416     setup_header_size = 0x0202 + kernel[0x0201] - setup_hdr_offset;
0417 
0418     /* Is there a limit on setup header size? */
0419     memcpy(&params->hdr, (kernel + setup_hdr_offset), setup_header_size);
0420 
0421     kbuf.buffer = params;
0422     kbuf.memsz = kbuf.bufsz;
0423     kbuf.buf_align = 16;
0424     kbuf.buf_min = MIN_BOOTPARAM_ADDR;
0425     ret = kexec_add_buffer(&kbuf);
0426     if (ret)
0427         goto out_free_params;
0428     bootparam_load_addr = kbuf.mem;
0429     pr_debug("Loaded boot_param, command line and misc at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
0430          bootparam_load_addr, kbuf.bufsz, kbuf.bufsz);
0431 
0432     /* Load kernel */
0433     kbuf.buffer = kernel + kern16_size;
0434     kbuf.bufsz =  kernel_len - kern16_size;
0435     kbuf.memsz = PAGE_ALIGN(header->init_size);
0436     kbuf.buf_align = header->kernel_alignment;
0437     kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
0438     ret = kexec_add_buffer(&kbuf);
0439     if (ret)
0440         goto out_free_params;
0441     kernel_load_addr = kbuf.mem;
0442 
0443     pr_debug("Loaded 64bit kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
0444          kernel_load_addr, kbuf.bufsz, kbuf.memsz);
0445 
0446     /* Load initrd high */
0447     if (initrd) {
0448         kbuf.buffer = initrd;
0449         kbuf.bufsz = kbuf.memsz = initrd_len;
0450         kbuf.buf_align = PAGE_SIZE;
0451         kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
0452         ret = kexec_add_buffer(&kbuf);
0453         if (ret)
0454             goto out_free_params;
0455         initrd_load_addr = kbuf.mem;
0456 
0457         pr_debug("Loaded initrd at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
0458                 initrd_load_addr, initrd_len, initrd_len);
0459 
0460         setup_initrd(params, initrd_load_addr, initrd_len);
0461     }
0462 
0463     setup_cmdline(image, params, bootparam_load_addr,
0464               sizeof(struct boot_params), cmdline, cmdline_len);
0465 
0466     /* bootloader info. Do we need a separate ID for kexec kernel loader? */
0467     params->hdr.type_of_loader = 0x0D << 4;
0468     params->hdr.loadflags = 0;
0469 
0470     /* Setup purgatory regs for entry */
0471     ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
0472                          sizeof(regs64), 1);
0473     if (ret)
0474         goto out_free_params;
0475 
0476     regs64.rbx = 0; /* Bootstrap Processor */
0477     regs64.rsi = bootparam_load_addr;
0478     regs64.rip = kernel_load_addr + 0x200;
0479     stack = kexec_purgatory_get_symbol_addr(image, "stack_end");
0480     if (IS_ERR(stack)) {
0481         pr_err("Could not find address of symbol stack_end\n");
0482         ret = -EINVAL;
0483         goto out_free_params;
0484     }
0485 
0486     regs64.rsp = (unsigned long)stack;
0487     ret = kexec_purgatory_get_set_symbol(image, "entry64_regs", &regs64,
0488                          sizeof(regs64), 0);
0489     if (ret)
0490         goto out_free_params;
0491 
0492     ret = setup_boot_parameters(image, params, bootparam_load_addr,
0493                     efi_map_offset, efi_map_sz,
0494                     efi_setup_data_offset);
0495     if (ret)
0496         goto out_free_params;
0497 
0498     /* Allocate loader specific data */
0499     ldata = kzalloc(sizeof(struct bzimage64_data), GFP_KERNEL);
0500     if (!ldata) {
0501         ret = -ENOMEM;
0502         goto out_free_params;
0503     }
0504 
0505     /*
0506      * Store pointer to params so that it could be freed after loading
0507      * params segment has been loaded and contents have been copied
0508      * somewhere else.
0509      */
0510     ldata->bootparams_buf = params;
0511     return ldata;
0512 
0513 out_free_params:
0514     kfree(params);
0515     return ERR_PTR(ret);
0516 }
0517 
0518 /* This cleanup function is called after various segments have been loaded */
0519 static int bzImage64_cleanup(void *loader_data)
0520 {
0521     struct bzimage64_data *ldata = loader_data;
0522 
0523     if (!ldata)
0524         return 0;
0525 
0526     kfree(ldata->bootparams_buf);
0527     ldata->bootparams_buf = NULL;
0528 
0529     return 0;
0530 }
0531 
0532 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
0533 static int bzImage64_verify_sig(const char *kernel, unsigned long kernel_len)
0534 {
0535     return verify_pefile_signature(kernel, kernel_len,
0536                        NULL,
0537                        VERIFYING_KEXEC_PE_SIGNATURE);
0538 }
0539 #endif
0540 
0541 struct kexec_file_ops kexec_bzImage64_ops = {
0542     .probe = bzImage64_probe,
0543     .load = bzImage64_load,
0544     .cleanup = bzImage64_cleanup,
0545 #ifdef CONFIG_KEXEC_BZIMAGE_VERIFY_SIG
0546     .verify_sig = bzImage64_verify_sig,
0547 #endif
0548 };