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