99a64ccadc7ce943aa113f14d7a9137c12d8ddd7
[openwrt/openwrt.git] / target / linux / ipq806x / patches-5.4 / 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 @@ -1825,6 +1825,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 @@ -4,6 +4,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 @@ -67,6 +69,80 @@ static uint32_t get_cell_size(const void
55 return cell_size;
56 }
57
58 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
59 +/**
60 + * taken from arch/x86/boot/string.c
61 + * local_strstr - Find the first substring in a %NUL terminated string
62 + * @s1: The string to be searched
63 + * @s2: The string to search for
64 + */
65 +static char *local_strstr(const char *s1, const char *s2)
66 +{
67 + size_t l1, l2;
68 +
69 + l2 = strlen(s2);
70 + if (!l2)
71 + return (char *)s1;
72 + l1 = strlen(s1);
73 + while (l1 >= l2) {
74 + l1--;
75 + if (!memcmp(s1, s2, l2))
76 + return (char *)s1;
77 + s1++;
78 + }
79 + return NULL;
80 +}
81 +
82 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
83 +{
84 + char *ptr, *end, *tmp;
85 + char *root="root=";
86 + char *find_rootblock;
87 + int i, l;
88 + const char *rootblock;
89 +
90 + find_rootblock = getprop(fdt, "/chosen", "find-rootblock", &l);
91 + if(!find_rootblock)
92 + find_rootblock = root;
93 +
94 + //ARM doesn't have __HAVE_ARCH_STRSTR, so it was copied from x86
95 + ptr = local_strstr(str, find_rootblock);
96 +
97 + if(!ptr)
98 + return dest;
99 +
100 + end = strchr(ptr, ' ');
101 + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
102 +
103 + // Some boards ubi.mtd=XX,ZZZZ, so let's check for '," too.
104 + tmp = strchr(ptr, ',');
105 +
106 + if(tmp)
107 + end = end < tmp ? end : tmp - 1;
108 +
109 + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX | ubi.mtd=XX,ZZZZ )
110 + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
111 + ptr = end + 1;
112 +
113 + /* if append-rootblock property is set use it to append to command line */
114 + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
115 + if(rootblock != NULL) {
116 + if(*dest != ' ') {
117 + *dest = ' ';
118 + dest++;
119 + len++;
120 + }
121 + if (len + l + i <= COMMAND_LINE_SIZE) {
122 + memcpy(dest, rootblock, l);
123 + dest += l - 1;
124 + memcpy(dest, ptr, i);
125 + dest += i;
126 + }
127 + }
128 + return dest;
129 +}
130 +#endif
131 +
132 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
133 {
134 char cmdline[COMMAND_LINE_SIZE];
135 @@ -86,12 +162,21 @@ static void merge_fdt_bootargs(void *fdt
136
137 /* and append the ATAG_CMDLINE */
138 if (fdt_cmdline) {
139 +
140 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
141 + //save original bootloader args
142 + //and append ubi.mtd with root partition number to current cmdline
143 + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
144 + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
145 +
146 +#else
147 len = strlen(fdt_cmdline);
148 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
149 *ptr++ = ' ';
150 memcpy(ptr, fdt_cmdline, len);
151 ptr += len;
152 }
153 +#endif
154 }
155 *ptr = '\0';
156
157 @@ -166,7 +251,9 @@ int atags_to_fdt(void *atag_list, void *
158 else
159 setprop_string(fdt, "/chosen", "bootargs",
160 atag->u.cmdline.cmdline);
161 - } else if (atag->hdr.tag == ATAG_MEM) {
162 + }
163 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
164 + else if (atag->hdr.tag == ATAG_MEM) {
165 if (memcount >= sizeof(mem_reg_property)/4)
166 continue;
167 if (!atag->u.mem.size)
168 @@ -210,6 +297,10 @@ int atags_to_fdt(void *atag_list, void *
169 setprop(fdt, "/memory", "reg", mem_reg_property,
170 4 * memcount * memsize);
171 }
172 +#else
173 +
174 + }
175 +#endif
176
177 return fdt_pack(fdt);
178 }
179 --- a/init/main.c
180 +++ b/init/main.c
181 @@ -103,6 +103,10 @@
182 #define CREATE_TRACE_POINTS
183 #include <trace/events/initcall.h>
184
185 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
186 +#include <linux/of.h>
187 +#endif
188 +
189 static int kernel_init(void *);
190
191 extern void init_IRQ(void);
192 @@ -632,6 +636,18 @@ asmlinkage __visible void __init start_k
193 pr_notice("Kernel command line: %s\n", boot_command_line);
194 /* parameters may set static keys */
195 jump_label_init();
196 +
197 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
198 + //Show bootloader's original command line for reference
199 + if(of_chosen) {
200 + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
201 + if(prop)
202 + pr_notice("Bootloader command line (ignored): %s\n", prop);
203 + else
204 + pr_notice("Bootloader command line not present\n");
205 + }
206 +#endif
207 +
208 parse_early_param();
209 after_dashes = parse_args("Booting kernel",
210 static_command_line, __start___param,