kernel: bump 4.9 to 4.9.138
[openwrt/openwrt.git] / target / linux / generic / pending-4.9 / 330-MIPS-kexec-Accept-command-line-parameters-from-users.patch
1 From: Yousong Zhou <yszhou4tech@gmail.com>
2 Subject: MIPS: kexec: Accept command line parameters from userspace.
3
4 Signed-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 @@ -10,14 +10,11 @@
15 #include <linux/mm.h>
16 #include <linux/delay.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 <asm/uaccess.h>
28 +#include "machine_kexec.h"
29
30 int (*_machine_kexec_prepare)(struct kimage *) = NULL;
31 void (*_machine_kexec_shutdown)(void) = NULL;
32 @@ -28,9 +25,115 @@ atomic_t kexec_ready_to_reboot = ATOMIC_
33 void (*_crash_smp_send_stop)(void) = NULL;
34 #endif
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 + copy_from_user(kexec_argv_buf, buf, size);
85 + kexec_argv_buf[size - 1] = 0;
86 +}
87 +
88 +static void machine_kexec_parse_argv(struct kimage *image)
89 +{
90 + char *reboot_code_buffer;
91 + int reloc_delta;
92 + char *ptr;
93 + int argc;
94 + int i;
95 +
96 + ptr = kexec_argv_buf;
97 + argc = 0;
98 +
99 + /*
100 + * convert command line string to array of parameters
101 + * (as bootloader does).
102 + */
103 + while (ptr && *ptr && (KEXEC_MAX_ARGC > argc)) {
104 + if (*ptr == ' ') {
105 + *ptr++ = '\0';
106 + continue;
107 + }
108 +
109 + kexec_argv[argc++] = ptr;
110 + ptr = strchr(ptr, ' ');
111 + }
112 +
113 + if (!argc)
114 + return;
115 +
116 + kexec_args[0] = argc;
117 + kexec_args[1] = (unsigned long)kexec_argv;
118 + kexec_args[2] = 0;
119 + kexec_args[3] = 0;
120 +
121 + reboot_code_buffer = page_address(image->control_code_page);
122 + reloc_delta = reboot_code_buffer - (char *)kexec_relocate_new_kernel;
123 +
124 + kexec_args[1] += reloc_delta;
125 + for (i = 0; i < argc; i++)
126 + kexec_argv[i] += reloc_delta;
127 +}
128 +
129 int
130 machine_kexec_prepare(struct kimage *kimage)
131 {
132 + /*
133 + * Whenever arguments passed from kexec-tools, Init the arguments as
134 + * the original ones to try avoiding booting failure.
135 + */
136 +
137 + kexec_args[0] = fw_arg0;
138 + kexec_args[1] = fw_arg1;
139 + kexec_args[2] = fw_arg2;
140 + kexec_args[3] = fw_arg3;
141 +
142 + machine_kexec_init_argv(kimage);
143 + machine_kexec_parse_argv(kimage);
144 +
145 if (_machine_kexec_prepare)
146 return _machine_kexec_prepare(kimage);
147 return 0;
148 @@ -67,10 +170,12 @@ machine_kexec(struct kimage *image)
149 unsigned long *ptr;
150
151 reboot_code_buffer =
152 - (unsigned long)page_address(image->control_code_page);
153 + (unsigned long)page_address(image->control_code_page);
154 + pr_info("reboot_code_buffer = %p\n", (void *)reboot_code_buffer);
155
156 kexec_start_address =
157 (unsigned long) phys_to_virt(image->start);
158 + pr_info("kexec_start_address = %p\n", (void *)kexec_start_address);
159
160 if (image->type == KEXEC_TYPE_DEFAULT) {
161 kexec_indirection_page =
162 @@ -78,9 +183,19 @@ machine_kexec(struct kimage *image)
163 } else {
164 kexec_indirection_page = (unsigned long)&image->head;
165 }
166 + pr_info("kexec_indirection_page = %p\n", (void *)kexec_indirection_page);
167
168 - memcpy((void*)reboot_code_buffer, relocate_new_kernel,
169 - relocate_new_kernel_size);
170 + pr_info("Where is memcpy: %p\n", memcpy);
171 + pr_info("kexec_relocate_new_kernel = %p, kexec_relocate_new_kernel_end = %p\n",
172 + (void *)kexec_relocate_new_kernel, &kexec_relocate_new_kernel_end);
173 + pr_info("Copy %lu bytes from %p to %p\n", KEXEC_RELOCATE_NEW_KERNEL_SIZE,
174 + (void *)kexec_relocate_new_kernel, (void *)reboot_code_buffer);
175 + memcpy((void*)reboot_code_buffer, kexec_relocate_new_kernel,
176 + KEXEC_RELOCATE_NEW_KERNEL_SIZE);
177 +
178 + pr_info("Before _print_args().\n");
179 + machine_kexec_print_args();
180 + pr_info("Before eval loop.\n");
181
182 /*
183 * The generic kexec code builds a page list with physical
184 @@ -102,15 +217,16 @@ machine_kexec(struct kimage *image)
185 /*
186 * we do not want to be bothered.
187 */
188 + pr_info("Before irq_disable.\n");
189 local_irq_disable();
190
191 - printk("Will call new kernel at %08lx\n", image->start);
192 - printk("Bye ...\n");
193 + pr_info("Will call new kernel at %08lx\n", image->start);
194 + pr_info("Bye ...\n");
195 __flush_cache_all();
196 #ifdef CONFIG_SMP
197 /* All secondary cpus now may jump to kexec_wait cycle */
198 relocated_kexec_smp_wait = reboot_code_buffer +
199 - (void *)(kexec_smp_wait - relocate_new_kernel);
200 + (void *)(kexec_smp_wait - kexec_relocate_new_kernel);
201 smp_wmb();
202 atomic_set(&kexec_ready_to_reboot, 1);
203 #endif
204 --- /dev/null
205 +++ b/arch/mips/kernel/machine_kexec.h
206 @@ -0,0 +1,20 @@
207 +#ifndef _MACHINE_KEXEC_H
208 +#define _MACHINE_KEXEC_H
209 +
210 +#ifndef __ASSEMBLY__
211 +extern const unsigned char kexec_relocate_new_kernel[];
212 +extern unsigned long kexec_relocate_new_kernel_end;
213 +extern unsigned long kexec_start_address;
214 +extern unsigned long kexec_indirection_page;
215 +
216 +extern char kexec_argv_buf[];
217 +extern char *kexec_argv[];
218 +
219 +#define KEXEC_RELOCATE_NEW_KERNEL_SIZE ((unsigned long)&kexec_relocate_new_kernel_end - (unsigned long)kexec_relocate_new_kernel)
220 +#endif /* !__ASSEMBLY__ */
221 +
222 +#define KEXEC_COMMAND_LINE_SIZE 256
223 +#define KEXEC_ARGV_SIZE (KEXEC_COMMAND_LINE_SIZE / 16)
224 +#define KEXEC_MAX_ARGC (KEXEC_ARGV_SIZE / sizeof(long))
225 +
226 +#endif
227 --- a/arch/mips/kernel/relocate_kernel.S
228 +++ b/arch/mips/kernel/relocate_kernel.S
229 @@ -12,8 +12,9 @@
230 #include <asm/mipsregs.h>
231 #include <asm/stackframe.h>
232 #include <asm/addrspace.h>
233 +#include "machine_kexec.h"
234
235 -LEAF(relocate_new_kernel)
236 +LEAF(kexec_relocate_new_kernel)
237 PTR_L a0, arg0
238 PTR_L a1, arg1
239 PTR_L a2, arg2
240 @@ -98,7 +99,7 @@ done:
241 #endif
242 /* jump to kexec_start_address */
243 j s1
244 - END(relocate_new_kernel)
245 + END(kexec_relocate_new_kernel)
246
247 #ifdef CONFIG_SMP
248 /*
249 @@ -184,9 +185,15 @@ kexec_indirection_page:
250 PTR 0
251 .size kexec_indirection_page, PTRSIZE
252
253 -relocate_new_kernel_end:
254 +kexec_argv_buf:
255 + EXPORT(kexec_argv_buf)
256 + .skip KEXEC_COMMAND_LINE_SIZE
257 + .size kexec_argv_buf, KEXEC_COMMAND_LINE_SIZE
258 +
259 +kexec_argv:
260 + EXPORT(kexec_argv)
261 + .skip KEXEC_ARGV_SIZE
262 + .size kexec_argv, KEXEC_ARGV_SIZE
263
264 -relocate_new_kernel_size:
265 - EXPORT(relocate_new_kernel_size)
266 - PTR relocate_new_kernel_end - relocate_new_kernel
267 - .size relocate_new_kernel_size, PTRSIZE
268 +kexec_relocate_new_kernel_end:
269 + EXPORT(kexec_relocate_new_kernel_end)