summaryrefslogtreecommitdiffstats
path: root/target/linux/mvebu/patches-6.6/300-mvebu-Mangle-bootloader-s-kernel-arguments.patch
blob: 0cb1e7559129070bb70fe705b8a4dc68561d8f41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
From 71270226b14733a4b1f2cde58ea9265caa50b38d Mon Sep 17 00:00:00 2001
From: Adrian Panella <ianchi74@outlook.com>
Date: Thu, 9 Mar 2017 09:37:17 +0100
Subject: [PATCH 67/69] generic: Mangle bootloader's kernel arguments

The command-line arguments provided by the boot loader will be
appended to a new device tree property: bootloader-args.
If there is a property "append-rootblock" in DT under /chosen
and a root= option in bootloaders command line it will be parsed
and added to DT bootargs with the form: <append-rootblock>XX.
Only command line ATAG will be processed, the rest of the ATAGs
sent by bootloader will be ignored.
This is usefull in dual boot systems, to get the current root partition
without afecting the rest of the system.

Signed-off-by: Adrian Panella <ianchi74@outlook.com>

This patch has been modified to be mvebu specific. The original patch 
did not pass the bootloader cmdline on if no append-rootblock stanza 
was found, resulting in blank cmdline and failure to boot.

Signed-off-by: Michael Gray <michael.gray@lantisproject.com>
---
 arch/arm/Kconfig                        | 11 ++++
 arch/arm/boot/compressed/atags_to_fdt.c | 85 ++++++++++++++++++++++++-
 init/main.c                             | 16 +++++
 3 files changed, 111 insertions(+), 1 deletion(-)

--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1566,6 +1566,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
 	  The command-line arguments provided by the boot loader will be
 	  appended to the the device tree bootargs property.
 
+config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
+	bool "Append rootblock parsing bootloader's kernel arguments"
+	help
+	  The command-line arguments provided by the boot loader will be
+	  appended to a new device tree property: bootloader-args.
+	  If there is a property "append-rootblock" in DT under /chosen 
+	  and a root= option in bootloaders command line it will be parsed 
+	  and added to DT bootargs with the form: <append-rootblock>XX.
+	  Only command line ATAG will be processed, the rest of the ATAGs
+	  sent by bootloader will be ignored.
+
 endchoice
 
 config CMDLINE
--- a/arch/arm/boot/compressed/atags_to_fdt.c
+++ b/arch/arm/boot/compressed/atags_to_fdt.c
@@ -6,6 +6,8 @@
 
 #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
 #define do_extend_cmdline 1
+#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+#define do_extend_cmdline 1
 #else
 #define do_extend_cmdline 0
 #endif
@@ -21,6 +23,7 @@ static int node_offset(void *fdt, const
 	return offset;
 }
 
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
 static int setprop(void *fdt, const char *node_path, const char *property,
 		   void *val_array, int size)
 {
@@ -29,6 +32,7 @@ static int setprop(void *fdt, const char
 		return offset;
 	return fdt_setprop(fdt, offset, property, val_array, size);
 }
+#endif
 
 static int setprop_string(void *fdt, const char *node_path,
 			  const char *property, const char *string)
@@ -39,6 +43,7 @@ static int setprop_string(void *fdt, con
 	return fdt_setprop_string(fdt, offset, property, string);
 }
 
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
 static int setprop_cell(void *fdt, const char *node_path,
 			const char *property, uint32_t val)
 {
@@ -47,6 +52,7 @@ static int setprop_cell(void *fdt, const
 		return offset;
 	return fdt_setprop_cell(fdt, offset, property, val);
 }
+#endif
 
 static const void *getprop(const void *fdt, const char *node_path,
 			   const char *property, int *len)
@@ -59,6 +65,7 @@ static const void *getprop(const void *f
 	return fdt_getprop(fdt, offset, property, len);
 }
 
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
 static uint32_t get_cell_size(const void *fdt)
 {
 	int len;
@@ -70,6 +77,74 @@ static uint32_t get_cell_size(const void
 	return cell_size;
 }
 
+#endif
+
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+
+static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
+{
+	const char *ptr, *end;
+	const char *root="root=";
+	int i, l;
+	const char *rootblock;
+
+	//ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
+	ptr = str - 1;
+
+	do {
+		//first find an 'r' at the begining or after a space
+		do {
+			ptr++;
+			ptr = strchr(ptr, 'r');
+			if (!ptr)
+				goto no_append;
+
+		} while (ptr != str && *(ptr-1) != ' ');
+
+		//then check for the rest
+		for(i = 1; i <= 4; i++)
+			if(*(ptr+i) != *(root+i)) break;
+
+	} while (i != 5);
+
+	end = strchr(ptr, ' ');
+	end = end ? (end - 1) : (strchr(ptr, 0) - 1);
+
+	//find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
+	for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
+	ptr = end + 1;
+
+	/* if append-rootblock property is set use it to append to command line */
+	rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
+	if (rootblock == NULL)
+		goto no_append;
+
+	if (*dest != ' ') {
+		*dest = ' ';
+		dest++;
+		len++;
+	}
+
+	if (len + l + i <= COMMAND_LINE_SIZE) {
+		memcpy(dest, rootblock, l);
+		dest += l - 1;
+		memcpy(dest, ptr, i);
+		dest += i;
+	}
+
+	return dest;
+
+no_append:
+	len = strlen(str);
+	if (len + 1 < COMMAND_LINE_SIZE) {
+		memcpy(dest, str, len);
+		dest += len;
+	}
+
+	return dest;
+}
+#endif
+
 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
 {
 	char cmdline[COMMAND_LINE_SIZE];
@@ -89,18 +164,28 @@ static void merge_fdt_bootargs(void *fdt
 
 	/* and append the ATAG_CMDLINE */
 	if (fdt_cmdline) {
+
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+		//save original bootloader args
+		//and append ubi.mtd with root partition number to current cmdline
+		setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
+		ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
+
+#else
 		len = strlen(fdt_cmdline);
 		if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
 			*ptr++ = ' ';
 			memcpy(ptr, fdt_cmdline, len);
 			ptr += len;
 		}
+#endif
 	}
 	*ptr = '\0';
 
 	setprop_string(fdt, "/chosen", "bootargs", cmdline);
 }
 
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
 static void hex_str(char *out, uint32_t value)
 {
 	uint32_t digit;
@@ -118,6 +203,7 @@ static void hex_str(char *out, uint32_t
 	}
 	*out = '\0';
 }
+#endif
 
 /*
  * Convert and fold provided ATAGs into the provided FDT.
@@ -132,9 +218,11 @@ int atags_to_fdt(void *atag_list, void *
 	struct tag *atag = atag_list;
 	/* In the case of 64 bits memory size, need to reserve 2 cells for
 	 * address and size for each bank */
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
 	__be32 mem_reg_property[2 * 2 * NR_BANKS];
-	int memcount = 0;
-	int ret, memsize;
+	int memsize, memcount = 0;
+#endif
+	int ret;
 
 	/* make sure we've got an aligned pointer */
 	if ((u32)atag_list & 0x3)
@@ -169,7 +257,9 @@ int atags_to_fdt(void *atag_list, void *
 			else
 				setprop_string(fdt, "/chosen", "bootargs",
 					       atag->u.cmdline.cmdline);
-		} else if (atag->hdr.tag == ATAG_MEM) {
+		}
+#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
+		else if (atag->hdr.tag == ATAG_MEM) {
 			if (memcount >= sizeof(mem_reg_property)/4)
 				continue;
 			if (!atag->u.mem.size)
@@ -213,6 +303,10 @@ int atags_to_fdt(void *atag_list, void *
 		setprop(fdt, "/memory", "reg", mem_reg_property,
 			4 * memcount * memsize);
 	}
+#else
+
+	}
+#endif
 
 	return fdt_pack(fdt);
 }
--- a/init/main.c
+++ b/init/main.c
@@ -112,6 +112,10 @@
 
 #include <kunit/test.h>
 
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+#include <linux/of.h>
+#endif
+
 static int kernel_init(void *);
 
 /*
@@ -928,6 +932,18 @@ void start_kernel(void)
 	boot_cpu_hotplug_init();
 
 	pr_notice("Kernel command line: %s\n", saved_command_line);
+
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+	//Show bootloader's original command line for reference
+	if(of_chosen) {
+		const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
+		if(prop)
+			pr_notice("Bootloader command line (ignored): %s\n", prop);
+		else
+			pr_notice("Bootloader command line not present\n");
+	}
+#endif
+
 	/* parameters may set static keys */
 	jump_label_init();
 	parse_early_param();