Blame view

arch/arm64/kernel/kexec_image.c 3.98 KB
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  // SPDX-License-Identifier: GPL-2.0
  /*
   * Kexec image loader
  
   * Copyright (C) 2018 Linaro Limited
   * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
   */
  
  #define pr_fmt(fmt)	"kexec_file(Image): " fmt
  
  #include <linux/err.h>
  #include <linux/errno.h>
  #include <linux/kernel.h>
  #include <linux/kexec.h>
732b7b93d   AKASHI Takahiro   arm64: kexec_file...
15
  #include <linux/pe.h>
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
16
  #include <linux/string.h>
732b7b93d   AKASHI Takahiro   arm64: kexec_file...
17
  #include <linux/verification.h>
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
18
19
20
21
22
23
24
  #include <asm/byteorder.h>
  #include <asm/cpufeature.h>
  #include <asm/image.h>
  #include <asm/memory.h>
  
  static int image_probe(const char *kernel_buf, unsigned long kernel_len)
  {
732b7b93d   AKASHI Takahiro   arm64: kexec_file...
25
26
  	const struct arm64_image_header *h =
  		(const struct arm64_image_header *)(kernel_buf);
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
27

732b7b93d   AKASHI Takahiro   arm64: kexec_file...
28
29
  	if (!h || (kernel_len < sizeof(*h)))
  		return -EINVAL;
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
30

732b7b93d   AKASHI Takahiro   arm64: kexec_file...
31
  	if (memcmp(&h->magic, ARM64_IMAGE_MAGIC, sizeof(h->magic)))
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  		return -EINVAL;
  
  	return 0;
  }
  
  static void *image_load(struct kimage *image,
  				char *kernel, unsigned long kernel_len,
  				char *initrd, unsigned long initrd_len,
  				char *cmdline, unsigned long cmdline_len)
  {
  	struct arm64_image_header *h;
  	u64 flags, value;
  	bool be_image, be_kernel;
  	struct kexec_buf kbuf;
108aa5036   Benjamin Gwin   arm64: kexec_file...
46
  	unsigned long text_offset, kernel_segment_number;
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
47
48
49
50
51
  	struct kexec_segment *kernel_segment;
  	int ret;
  
  	/*
  	 * We require a kernel with an unambiguous Image header. Per
b693d0b37   Mauro Carvalho Chehab   docs: arm64: conv...
52
  	 * Documentation/arm64/booting.rst, this is the case when image_size
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  	 * is non-zero (practically speaking, since v3.17).
  	 */
  	h = (struct arm64_image_header *)kernel;
  	if (!h->image_size)
  		return ERR_PTR(-EINVAL);
  
  	/* Check cpu features */
  	flags = le64_to_cpu(h->flags);
  	be_image = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_BE);
  	be_kernel = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN);
  	if ((be_image != be_kernel) && !system_supports_mixed_endian())
  		return ERR_PTR(-EINVAL);
  
  	value = arm64_image_flag_field(flags, ARM64_IMAGE_FLAG_PAGE_SIZE);
  	if (((value == ARM64_IMAGE_FLAG_PAGE_SIZE_4K) &&
  			!system_supports_4kb_granule()) ||
  	    ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_64K) &&
  			!system_supports_64kb_granule()) ||
  	    ((value == ARM64_IMAGE_FLAG_PAGE_SIZE_16K) &&
  			!system_supports_16kb_granule()))
  		return ERR_PTR(-EINVAL);
  
  	/* Load the kernel */
  	kbuf.image = image;
  	kbuf.buf_min = 0;
  	kbuf.buf_max = ULONG_MAX;
  	kbuf.top_down = false;
  
  	kbuf.buffer = kernel;
  	kbuf.bufsz = kernel_len;
c19d050f8   Bhupesh Sharma   arm64/kexec: Use ...
83
  	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
84
85
86
87
88
89
  	kbuf.memsz = le64_to_cpu(h->image_size);
  	text_offset = le64_to_cpu(h->text_offset);
  	kbuf.buf_align = MIN_KIMG_ALIGN;
  
  	/* Adjust kernel segment with TEXT_OFFSET */
  	kbuf.memsz += text_offset;
108aa5036   Benjamin Gwin   arm64: kexec_file...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  	kernel_segment_number = image->nr_segments;
  
  	/*
  	 * The location of the kernel segment may make it impossible to satisfy
  	 * the other segment requirements, so we try repeatedly to find a
  	 * location that will work.
  	 */
  	while ((ret = kexec_add_buffer(&kbuf)) == 0) {
  		/* Try to load additional data */
  		kernel_segment = &image->segment[kernel_segment_number];
  		ret = load_other_segments(image, kernel_segment->mem,
  					  kernel_segment->memsz, initrd,
  					  initrd_len, cmdline);
  		if (!ret)
  			break;
  
  		/*
  		 * We couldn't find space for the other segments; erase the
  		 * kernel segment and try the next available hole.
  		 */
  		image->nr_segments -= 1;
  		kbuf.buf_min = kernel_segment->mem + kernel_segment->memsz;
  		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
  	}
  
  	if (ret) {
  		pr_err("Could not find any suitable kernel location!");
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
117
  		return ERR_PTR(ret);
108aa5036   Benjamin Gwin   arm64: kexec_file...
118
  	}
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
119

108aa5036   Benjamin Gwin   arm64: kexec_file...
120
  	kernel_segment = &image->segment[kernel_segment_number];
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
121
122
123
124
125
126
127
128
  	kernel_segment->mem += text_offset;
  	kernel_segment->memsz -= text_offset;
  	image->start = kernel_segment->mem;
  
  	pr_debug("Loaded kernel at 0x%lx bufsz=0x%lx memsz=0x%lx
  ",
  				kernel_segment->mem, kbuf.bufsz,
  				kernel_segment->memsz);
85f0b2fc9   Will Deacon   arm64: kexec_file...
129
  	return NULL;
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
130
  }
732b7b93d   AKASHI Takahiro   arm64: kexec_file...
131
132
133
134
135
136
137
  #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
  static int image_verify_sig(const char *kernel, unsigned long kernel_len)
  {
  	return verify_pefile_signature(kernel, kernel_len, NULL,
  				       VERIFYING_KEXEC_PE_SIGNATURE);
  }
  #endif
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
138
139
140
  const struct kexec_file_ops kexec_image_ops = {
  	.probe = image_probe,
  	.load = image_load,
732b7b93d   AKASHI Takahiro   arm64: kexec_file...
141
142
143
  #ifdef CONFIG_KEXEC_IMAGE_VERIFY_SIG
  	.verify_sig = image_verify_sig,
  #endif
f3b70e509   AKASHI Takahiro   arm64: kexec_file...
144
  };