kernel: bump 6.1 to 6.1.64
[openwrt/staging/stintel.git] / target / linux / mvebu / patches-6.1 / 300-mvebu-Mangle-bootloader-s-kernel-arguments.patch
1 From 71270226b14733a4b1f2cde58ea9265caa50b38d Mon Sep 17 00:00:00 2001
2 From: Adrian Panella <ianchi74@outlook.com>
3 Date: Thu, 9 Mar 2017 09:37:17 +0100
4 Subject: [PATCH 67/69] generic: Mangle bootloader's kernel arguments
5
6 The command-line arguments provided by the boot loader will be
7 appended to a new device tree property: bootloader-args.
8 If there is a property "append-rootblock" in DT under /chosen
9 and a root= option in bootloaders command line it will be parsed
10 and added to DT bootargs with the form: <append-rootblock>XX.
11 Only command line ATAG will be processed, the rest of the ATAGs
12 sent by bootloader will be ignored.
13 This is usefull in dual boot systems, to get the current root partition
14 without afecting the rest of the system.
15
16 Signed-off-by: Adrian Panella <ianchi74@outlook.com>
17
18 This patch has been modified to be mvebu specific. The original patch
19 did not pass the bootloader cmdline on if no append-rootblock stanza
20 was found, resulting in blank cmdline and failure to boot.
21
22 Signed-off-by: Michael Gray <michael.gray@lantisproject.com>
23 ---
24 arch/arm/Kconfig | 11 ++++
25 arch/arm/boot/compressed/atags_to_fdt.c | 85 ++++++++++++++++++++++++-
26 init/main.c | 16 +++++
27 3 files changed, 111 insertions(+), 1 deletion(-)
28
29 --- a/arch/arm/Kconfig
30 +++ b/arch/arm/Kconfig
31 @@ -1587,6 +1587,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
32 The command-line arguments provided by the boot loader will be
33 appended to the the device tree bootargs property.
34
35 +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
36 + bool "Append rootblock parsing bootloader's kernel arguments"
37 + help
38 + The command-line arguments provided by the boot loader will be
39 + appended to a new device tree property: bootloader-args.
40 + If there is a property "append-rootblock" in DT under /chosen
41 + and a root= option in bootloaders command line it will be parsed
42 + and added to DT bootargs with the form: <append-rootblock>XX.
43 + Only command line ATAG will be processed, the rest of the ATAGs
44 + sent by bootloader will be ignored.
45 +
46 endchoice
47
48 config CMDLINE
49 --- a/arch/arm/boot/compressed/atags_to_fdt.c
50 +++ b/arch/arm/boot/compressed/atags_to_fdt.c
51 @@ -5,6 +5,8 @@
52
53 #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
54 #define do_extend_cmdline 1
55 +#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
56 +#define do_extend_cmdline 1
57 #else
58 #define do_extend_cmdline 0
59 #endif
60 @@ -20,6 +22,7 @@ static int node_offset(void *fdt, const
61 return offset;
62 }
63
64 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
65 static int setprop(void *fdt, const char *node_path, const char *property,
66 void *val_array, int size)
67 {
68 @@ -28,6 +31,7 @@ static int setprop(void *fdt, const char
69 return offset;
70 return fdt_setprop(fdt, offset, property, val_array, size);
71 }
72 +#endif
73
74 static int setprop_string(void *fdt, const char *node_path,
75 const char *property, const char *string)
76 @@ -38,6 +42,7 @@ static int setprop_string(void *fdt, con
77 return fdt_setprop_string(fdt, offset, property, string);
78 }
79
80 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
81 static int setprop_cell(void *fdt, const char *node_path,
82 const char *property, uint32_t val)
83 {
84 @@ -46,6 +51,7 @@ static int setprop_cell(void *fdt, const
85 return offset;
86 return fdt_setprop_cell(fdt, offset, property, val);
87 }
88 +#endif
89
90 static const void *getprop(const void *fdt, const char *node_path,
91 const char *property, int *len)
92 @@ -58,6 +64,7 @@ static const void *getprop(const void *f
93 return fdt_getprop(fdt, offset, property, len);
94 }
95
96 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
97 static uint32_t get_cell_size(const void *fdt)
98 {
99 int len;
100 @@ -69,6 +76,74 @@ static uint32_t get_cell_size(const void
101 return cell_size;
102 }
103
104 +#endif
105 +
106 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
107 +
108 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
109 +{
110 + const char *ptr, *end;
111 + const char *root="root=";
112 + int i, l;
113 + const char *rootblock;
114 +
115 + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
116 + ptr = str - 1;
117 +
118 + do {
119 + //first find an 'r' at the begining or after a space
120 + do {
121 + ptr++;
122 + ptr = strchr(ptr, 'r');
123 + if (!ptr)
124 + goto no_append;
125 +
126 + } while (ptr != str && *(ptr-1) != ' ');
127 +
128 + //then check for the rest
129 + for(i = 1; i <= 4; i++)
130 + if(*(ptr+i) != *(root+i)) break;
131 +
132 + } while (i != 5);
133 +
134 + end = strchr(ptr, ' ');
135 + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
136 +
137 + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
138 + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
139 + ptr = end + 1;
140 +
141 + /* if append-rootblock property is set use it to append to command line */
142 + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
143 + if (rootblock == NULL)
144 + goto no_append;
145 +
146 + if (*dest != ' ') {
147 + *dest = ' ';
148 + dest++;
149 + len++;
150 + }
151 +
152 + if (len + l + i <= COMMAND_LINE_SIZE) {
153 + memcpy(dest, rootblock, l);
154 + dest += l - 1;
155 + memcpy(dest, ptr, i);
156 + dest += i;
157 + }
158 +
159 + return dest;
160 +
161 +no_append:
162 + len = strlen(str);
163 + if (len + 1 < COMMAND_LINE_SIZE) {
164 + memcpy(dest, str, len);
165 + dest += len;
166 + }
167 +
168 + return dest;
169 +}
170 +#endif
171 +
172 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
173 {
174 char cmdline[COMMAND_LINE_SIZE];
175 @@ -88,18 +163,28 @@ static void merge_fdt_bootargs(void *fdt
176
177 /* and append the ATAG_CMDLINE */
178 if (fdt_cmdline) {
179 +
180 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
181 + //save original bootloader args
182 + //and append ubi.mtd with root partition number to current cmdline
183 + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
184 + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
185 +
186 +#else
187 len = strlen(fdt_cmdline);
188 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
189 *ptr++ = ' ';
190 memcpy(ptr, fdt_cmdline, len);
191 ptr += len;
192 }
193 +#endif
194 }
195 *ptr = '\0';
196
197 setprop_string(fdt, "/chosen", "bootargs", cmdline);
198 }
199
200 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
201 static void hex_str(char *out, uint32_t value)
202 {
203 uint32_t digit;
204 @@ -117,6 +202,7 @@ static void hex_str(char *out, uint32_t
205 }
206 *out = '\0';
207 }
208 +#endif
209
210 /*
211 * Convert and fold provided ATAGs into the provided FDT.
212 @@ -131,9 +217,11 @@ int atags_to_fdt(void *atag_list, void *
213 struct tag *atag = atag_list;
214 /* In the case of 64 bits memory size, need to reserve 2 cells for
215 * address and size for each bank */
216 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
217 __be32 mem_reg_property[2 * 2 * NR_BANKS];
218 - int memcount = 0;
219 - int ret, memsize;
220 + int memsize, memcount = 0;
221 +#endif
222 + int ret;
223
224 /* make sure we've got an aligned pointer */
225 if ((u32)atag_list & 0x3)
226 @@ -168,7 +256,9 @@ int atags_to_fdt(void *atag_list, void *
227 else
228 setprop_string(fdt, "/chosen", "bootargs",
229 atag->u.cmdline.cmdline);
230 - } else if (atag->hdr.tag == ATAG_MEM) {
231 + }
232 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
233 + else if (atag->hdr.tag == ATAG_MEM) {
234 if (memcount >= sizeof(mem_reg_property)/4)
235 continue;
236 if (!atag->u.mem.size)
237 @@ -212,6 +302,10 @@ int atags_to_fdt(void *atag_list, void *
238 setprop(fdt, "/memory", "reg", mem_reg_property,
239 4 * memcount * memsize);
240 }
241 +#else
242 +
243 + }
244 +#endif
245
246 return fdt_pack(fdt);
247 }
248 --- a/init/main.c
249 +++ b/init/main.c
250 @@ -112,6 +112,10 @@
251
252 #include <kunit/test.h>
253
254 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
255 +#include <linux/of.h>
256 +#endif
257 +
258 static int kernel_init(void *);
259
260 extern void init_IRQ(void);
261 @@ -993,6 +997,18 @@ asmlinkage __visible void __init __no_sa
262 page_alloc_init();
263
264 pr_notice("Kernel command line: %s\n", saved_command_line);
265 +
266 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
267 + //Show bootloader's original command line for reference
268 + if(of_chosen) {
269 + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
270 + if(prop)
271 + pr_notice("Bootloader command line (ignored): %s\n", prop);
272 + else
273 + pr_notice("Bootloader command line not present\n");
274 + }
275 +#endif
276 +
277 /* parameters may set static keys */
278 jump_label_init();
279 parse_early_param();