Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Kexec image loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2018 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define pr_fmt(fmt)	"kexec_file(Image): " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/pe.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/verification.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/cpufeature.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/image.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int image_probe(const char *kernel_buf, unsigned long kernel_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	const struct arm64_image_header *h =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		(const struct arm64_image_header *)(kernel_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (!h || (kernel_len < sizeof(*h)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static void *image_load(struct kimage *image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				char *kernel, unsigned long kernel_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				char *initrd, unsigned long initrd_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				char *cmdline, unsigned long cmdline_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct arm64_image_header *h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u64 flags, value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	bool be_image, be_kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct kexec_buf kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned long text_offset, kernel_segment_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct kexec_segment *kernel_segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * We require a kernel with an unambiguous Image header. Per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * Documentation/arm64/booting.rst, this is the case when image_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 * is non-zero (practically speaking, since v3.17).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	h = (struct arm64_image_header *)kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (!h->image_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	/* Check cpu features */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	flags = le64_to_cpu(h->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if ((be_image != be_kernel) && !system_supports_mixed_endian())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			!system_supports_4kb_granule()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	    ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			!system_supports_64kb_granule()) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	    ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			!system_supports_16kb_granule()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/* Load the kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	kbuf.image = image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	kbuf.buf_min = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	kbuf.buf_max = ULONG_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	kbuf.top_down = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	kbuf.buffer = kernel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	kbuf.bufsz = kernel_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	kbuf.memsz = le64_to_cpu(h->image_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	text_offset = le64_to_cpu(h->text_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	kbuf.buf_align = MIN_KIMG_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/* Adjust kernel segment with TEXT_OFFSET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	kbuf.memsz += text_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	kernel_segment_number = image->nr_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	 * The location of the kernel segment may make it impossible to satisfy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * the other segment requirements, so we try repeatedly to find a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * location that will work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	while ((ret = kexec_add_buffer(&kbuf)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* Try to load additional data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		kernel_segment = &image->segment[kernel_segment_number];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ret = load_other_segments(image, kernel_segment->mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 					  kernel_segment->memsz, initrd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 					  initrd_len, cmdline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		 * We couldn't find space for the other segments; erase the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		 * kernel segment and try the next available hole.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		image->nr_segments -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		pr_err("Could not find any suitable kernel location!");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	kernel_segment = &image->segment[kernel_segment_number];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	kernel_segment->mem += text_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	kernel_segment->memsz -= text_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	image->start = kernel_segment->mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				kernel_segment->mem, kbuf.bufsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				kernel_segment->memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static int image_verify_sig(const char *kernel, unsigned long kernel_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return verify_pefile_signature(kernel, kernel_len, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				       VERIFYING_KEXEC_PE_SIGNATURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) const struct kexec_file_ops kexec_image_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	.probe = image_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	.load = image_load,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	.verify_sig = image_verify_sig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) };