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