blob: 2808c9532296901c00107c36058131b4d81657c7 [file] [log] [blame]
developerec4ebe42022-04-12 11:17:45 +08001From: Yousong Zhou <yszhou4tech@gmail.com>
2Subject: MIPS: kexec: Accept command line parameters from userspace.
3
4Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
5---
6 arch/mips/kernel/machine_kexec.c | 153 +++++++++++++++++++++++++++++++-----
7 arch/mips/kernel/machine_kexec.h | 20 +++++
8 arch/mips/kernel/relocate_kernel.S | 21 +++--
9 3 files changed, 167 insertions(+), 27 deletions(-)
10 create mode 100644 arch/mips/kernel/machine_kexec.h
11
12--- a/arch/mips/kernel/machine_kexec.c
13+++ b/arch/mips/kernel/machine_kexec.c
14@@ -9,14 +9,11 @@
15 #include <linux/delay.h>
16 #include <linux/libfdt.h>
17
18+#include <asm/bootinfo.h>
19 #include <asm/cacheflush.h>
20 #include <asm/page.h>
21-
22-extern const unsigned char relocate_new_kernel[];
23-extern const size_t relocate_new_kernel_size;
24-
25-extern unsigned long kexec_start_address;
26-extern unsigned long kexec_indirection_page;
27+#include <linux/uaccess.h>
28+#include "machine_kexec.h"
29
30 static unsigned long reboot_code_buffer;
31
32@@ -30,6 +27,101 @@ void (*_crash_smp_send_stop)(void) = NUL
33 void (*_machine_kexec_shutdown)(void) = NULL;
34 void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
35
36+static void machine_kexec_print_args(void)
37+{
38+ unsigned long argc = (int)kexec_args[0];
39+ int i;
40+
41+ pr_info("kexec_args[0] (argc): %lu\n", argc);
42+ pr_info("kexec_args[1] (argv): %p\n", (void *)kexec_args[1]);
43+ pr_info("kexec_args[2] (env ): %p\n", (void *)kexec_args[2]);
44+ pr_info("kexec_args[3] (desc): %p\n", (void *)kexec_args[3]);
45+
46+ for (i = 0; i < argc; i++) {
47+ pr_info("kexec_argv[%d] = %p, %s\n",
48+ i, kexec_argv[i], kexec_argv[i]);
49+ }
50+}
51+
52+static void machine_kexec_init_argv(struct kimage *image)
53+{
54+ void __user *buf = NULL;
55+ size_t bufsz;
56+ size_t size;
57+ int i;
58+
59+ bufsz = 0;
60+ for (i = 0; i < image->nr_segments; i++) {
61+ struct kexec_segment *seg;
62+
63+ seg = &image->segment[i];
64+ if (seg->bufsz < 6)
65+ continue;
66+
67+ if (strncmp((char *) seg->buf, "kexec ", 6))
68+ continue;
69+
70+ buf = seg->buf;
71+ bufsz = seg->bufsz;
72+ break;
73+ }
74+
75+ if (!buf)
76+ return;
77+
78+ size = KEXEC_COMMAND_LINE_SIZE;
79+ size = min(size, bufsz);
80+ if (size < bufsz)
81+ pr_warn("kexec command line truncated to %zd bytes\n", size);
82+
83+ /* Copy to kernel space */
84+ if (copy_from_user(kexec_argv_buf, buf, size))
85+ pr_warn("kexec command line copy to kernel space failed\n");
86+
87+ kexec_argv_buf[size - 1] = 0;
88+}
89+
90+static void machine_kexec_parse_argv(struct kimage *image)
91+{
92+ char *reboot_code_buffer;
93+ int reloc_delta;
94+ char *ptr;
95+ int argc;
96+ int i;
97+
98+ ptr = kexec_argv_buf;
99+ argc = 0;
100+
101+ /*
102+ * convert command line string to array of parameters
103+ * (as bootloader does).
104+ */
105+ while (ptr && *ptr && (KEXEC_MAX_ARGC > argc)) {
106+ if (*ptr == ' ') {
107+ *ptr++ = '\0';
108+ continue;
109+ }
110+
111+ kexec_argv[argc++] = ptr;
112+ ptr = strchr(ptr, ' ');
113+ }
114+
115+ if (!argc)
116+ return;
117+
118+ kexec_args[0] = argc;
119+ kexec_args[1] = (unsigned long)kexec_argv;
120+ kexec_args[2] = 0;
121+ kexec_args[3] = 0;
122+
123+ reboot_code_buffer = page_address(image->control_code_page);
124+ reloc_delta = reboot_code_buffer - (char *)kexec_relocate_new_kernel;
125+
126+ kexec_args[1] += reloc_delta;
127+ for (i = 0; i < argc; i++)
128+ kexec_argv[i] += reloc_delta;
129+}
130+
131 static void kexec_image_info(const struct kimage *kimage)
132 {
133 unsigned long i;
134@@ -99,6 +191,18 @@ machine_kexec_prepare(struct kimage *kim
135 #endif
136
137 kexec_image_info(kimage);
138+ /*
139+ * Whenever arguments passed from kexec-tools, Init the arguments as
140+ * the original ones to try avoiding booting failure.
141+ */
142+
143+ kexec_args[0] = fw_arg0;
144+ kexec_args[1] = fw_arg1;
145+ kexec_args[2] = fw_arg2;
146+ kexec_args[3] = fw_arg3;
147+
148+ machine_kexec_init_argv(kimage);
149+ machine_kexec_parse_argv(kimage);
150
151 if (_machine_kexec_prepare)
152 return _machine_kexec_prepare(kimage);
153@@ -161,7 +265,7 @@ machine_crash_shutdown(struct pt_regs *r
154 void kexec_nonboot_cpu_jump(void)
155 {
156 local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
157- reboot_code_buffer + relocate_new_kernel_size);
158+ reboot_code_buffer + KEXEC_RELOCATE_NEW_KERNEL_SIZE);
159
160 relocated_kexec_smp_wait(NULL);
161 }
162@@ -199,7 +303,7 @@ void kexec_reboot(void)
163 * machine_kexec() CPU.
164 */
165 local_flush_icache_range(reboot_code_buffer,
166- reboot_code_buffer + relocate_new_kernel_size);
167+ reboot_code_buffer + KEXEC_RELOCATE_NEW_KERNEL_SIZE);
168
169 do_kexec = (void *)reboot_code_buffer;
170 do_kexec();
171@@ -212,10 +316,12 @@ machine_kexec(struct kimage *image)
172 unsigned long *ptr;
173
174 reboot_code_buffer =
175- (unsigned long)page_address(image->control_code_page);
176+ (unsigned long)page_address(image->control_code_page);
177+ pr_info("reboot_code_buffer = %p\n", (void *)reboot_code_buffer);
178
179 kexec_start_address =
180 (unsigned long) phys_to_virt(image->start);
181+ pr_info("kexec_start_address = %p\n", (void *)kexec_start_address);
182
183 if (image->type == KEXEC_TYPE_DEFAULT) {
184 kexec_indirection_page =
185@@ -223,9 +329,19 @@ machine_kexec(struct kimage *image)
186 } else {
187 kexec_indirection_page = (unsigned long)&image->head;
188 }
189+ pr_info("kexec_indirection_page = %p\n", (void *)kexec_indirection_page);
190
191- memcpy((void*)reboot_code_buffer, relocate_new_kernel,
192- relocate_new_kernel_size);
193+ pr_info("Where is memcpy: %p\n", memcpy);
194+ pr_info("kexec_relocate_new_kernel = %p, kexec_relocate_new_kernel_end = %p\n",
195+ (void *)kexec_relocate_new_kernel, &kexec_relocate_new_kernel_end);
196+ pr_info("Copy %lu bytes from %p to %p\n", KEXEC_RELOCATE_NEW_KERNEL_SIZE,
197+ (void *)kexec_relocate_new_kernel, (void *)reboot_code_buffer);
198+ memcpy((void*)reboot_code_buffer, kexec_relocate_new_kernel,
199+ KEXEC_RELOCATE_NEW_KERNEL_SIZE);
200+
201+ pr_info("Before _print_args().\n");
202+ machine_kexec_print_args();
203+ pr_info("Before eval loop.\n");
204
205 /*
206 * The generic kexec code builds a page list with physical
207@@ -256,7 +372,7 @@ machine_kexec(struct kimage *image)
208 #ifdef CONFIG_SMP
209 /* All secondary cpus now may jump to kexec_wait cycle */
210 relocated_kexec_smp_wait = reboot_code_buffer +
211- (void *)(kexec_smp_wait - relocate_new_kernel);
212+ (void *)(kexec_smp_wait - kexec_relocate_new_kernel);
213 smp_wmb();
214 atomic_set(&kexec_ready_to_reboot, 1);
215 #endif
216--- /dev/null
217+++ b/arch/mips/kernel/machine_kexec.h
218@@ -0,0 +1,20 @@
219+#ifndef _MACHINE_KEXEC_H
220+#define _MACHINE_KEXEC_H
221+
222+#ifndef __ASSEMBLY__
223+extern const unsigned char kexec_relocate_new_kernel[];
224+extern unsigned long kexec_relocate_new_kernel_end;
225+extern unsigned long kexec_start_address;
226+extern unsigned long kexec_indirection_page;
227+
228+extern char kexec_argv_buf[];
229+extern char *kexec_argv[];
230+
231+#define KEXEC_RELOCATE_NEW_KERNEL_SIZE ((unsigned long)&kexec_relocate_new_kernel_end - (unsigned long)kexec_relocate_new_kernel)
232+#endif /* !__ASSEMBLY__ */
233+
234+#define KEXEC_COMMAND_LINE_SIZE 256
235+#define KEXEC_ARGV_SIZE (KEXEC_COMMAND_LINE_SIZE / 16)
236+#define KEXEC_MAX_ARGC (KEXEC_ARGV_SIZE / sizeof(long))
237+
238+#endif
239--- a/arch/mips/kernel/relocate_kernel.S
240+++ b/arch/mips/kernel/relocate_kernel.S
241@@ -10,8 +10,9 @@
242 #include <asm/mipsregs.h>
243 #include <asm/stackframe.h>
244 #include <asm/addrspace.h>
245+#include "machine_kexec.h"
246
247-LEAF(relocate_new_kernel)
248+LEAF(kexec_relocate_new_kernel)
249 PTR_L a0, arg0
250 PTR_L a1, arg1
251 PTR_L a2, arg2
252@@ -96,7 +97,7 @@ done:
253 #endif
254 /* jump to kexec_start_address */
255 j s1
256- END(relocate_new_kernel)
257+ END(kexec_relocate_new_kernel)
258
259 #ifdef CONFIG_SMP
260 /*
261@@ -182,9 +183,15 @@ kexec_indirection_page:
262 PTR 0
263 .size kexec_indirection_page, PTRSIZE
264
265-relocate_new_kernel_end:
266+kexec_argv_buf:
267+ EXPORT(kexec_argv_buf)
268+ .skip KEXEC_COMMAND_LINE_SIZE
269+ .size kexec_argv_buf, KEXEC_COMMAND_LINE_SIZE
270+
271+kexec_argv:
272+ EXPORT(kexec_argv)
273+ .skip KEXEC_ARGV_SIZE
274+ .size kexec_argv, KEXEC_ARGV_SIZE
275
276-relocate_new_kernel_size:
277- EXPORT(relocate_new_kernel_size)
278- PTR relocate_new_kernel_end - relocate_new_kernel
279- .size relocate_new_kernel_size, PTRSIZE
280+kexec_relocate_new_kernel_end:
281+ EXPORT(kexec_relocate_new_kernel_end)