Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Kexec image loader
0004 
0005  * Copyright (C) 2018 Linaro Limited
0006  * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
0007  */
0008 
0009 #define pr_fmt(fmt) "kexec_file(Image): " fmt
0010 
0011 #include <linux/err.h>
0012 #include <linux/errno.h>
0013 #include <linux/kernel.h>
0014 #include <linux/kexec.h>
0015 #include <linux/pe.h>
0016 #include <linux/string.h>
0017 #include <asm/byteorder.h>
0018 #include <asm/cpufeature.h>
0019 #include <asm/image.h>
0020 #include <asm/memory.h>
0021 
0022 static int image_probe(const char *kernel_buf, unsigned long kernel_len)
0023 {
0024     const struct arm64_image_header *h =
0025         (const struct arm64_image_header *)(kernel_buf);
0026 
0027     if (!h || (kernel_len < sizeof(*h)))
0028         return -EINVAL;
0029 
0030     if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
0031         return -EINVAL;
0032 
0033     return 0;
0034 }
0035 
0036 static void *image_load(struct kimage *image,
0037                 char *kernel, unsigned long kernel_len,
0038                 char *initrd, unsigned long initrd_len,
0039                 char *cmdline, unsigned long cmdline_len)
0040 {
0041     struct arm64_image_header *h;
0042     u64 flags, value;
0043     bool be_image, be_kernel;
0044     struct kexec_buf kbuf;
0045     unsigned long text_offset, kernel_segment_number;
0046     struct kexec_segment *kernel_segment;
0047     int ret;
0048 
0049     /*
0050      * We require a kernel with an unambiguous Image header. Per
0051      * Documentation/arm64/booting.rst, this is the case when image_size
0052      * is non-zero (practically speaking, since v3.17).
0053      */
0054     h = (struct arm64_image_header *)kernel;
0055     if (!h->image_size)
0056         return ERR_PTR(-EINVAL);
0057 
0058     /* Check cpu features */
0059     flags = le64_to_cpu(h->flags);
0060     be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE);
0061     be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
0062     if ((be_image != be_kernel) && !system_supports_mixed_endian())
0063         return ERR_PTR(-EINVAL);
0064 
0065     value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE);
0066     if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) &&
0067             !system_supports_4kb_granule()) ||
0068         ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) &&
0069             !system_supports_64kb_granule()) ||
0070         ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) &&
0071             !system_supports_16kb_granule()))
0072         return ERR_PTR(-EINVAL);
0073 
0074     /* Load the kernel */
0075     kbuf.image = image;
0076     kbuf.buf_min = 0;
0077     kbuf.buf_max = ULONG_MAX;
0078     kbuf.top_down = false;
0079 
0080     kbuf.buffer = kernel;
0081     kbuf.bufsz = kernel_len;
0082     kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
0083     kbuf.memsz = le64_to_cpu(h->image_size);
0084     text_offset = le64_to_cpu(h->text_offset);
0085     kbuf.buf_align = MIN_KIMG_ALIGN;
0086 
0087     /* Adjust kernel segment with TEXT_OFFSET */
0088     kbuf.memsz += text_offset;
0089 
0090     kernel_segment_number = image->nr_segments;
0091 
0092     /*
0093      * The location of the kernel segment may make it impossible to satisfy
0094      * the other segment requirements, so we try repeatedly to find a
0095      * location that will work.
0096      */
0097     while ((ret = kexec_add_buffer(&kbuf)) == 0) {
0098         /* Try to load additional data */
0099         kernel_segment = &image->segment[kernel_segment_number];
0100         ret = load_other_segments(image, kernel_segment->mem,
0101                       kernel_segment->memsz, initrd,
0102                       initrd_len, cmdline);
0103         if (!ret)
0104             break;
0105 
0106         /*
0107          * We couldn't find space for the other segments; erase the
0108          * kernel segment and try the next available hole.
0109          */
0110         image->nr_segments -= 1;
0111         kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
0112         kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
0113     }
0114 
0115     if (ret) {
0116         pr_err("Could not find any suitable kernel location!");
0117         return ERR_PTR(ret);
0118     }
0119 
0120     kernel_segment = &image->segment[kernel_segment_number];
0121     kernel_segment->mem += text_offset;
0122     kernel_segment->memsz -= text_offset;
0123     image->start = kernel_segment->mem;
0124 
0125     pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
0126                 kernel_segment->mem, kbuf.bufsz,
0127                 kernel_segment->memsz);
0128 
0129     return NULL;
0130 }
0131 
0132 const struct kexec_file_ops kexec_image_ops = {
0133     .probe = image_probe,
0134     .load = image_load,
0135 #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
0136     .verify_sig = kexec_kernel_verify_pe_sig,
0137 #endif
0138 };