mvebu: Add basic support for WRT1900AC (v1) and Turris Omnia (pre 2019)
[librecmc/librecmc.git] / target / linux / mvebu / patches-4.14 / 006-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 | 72 ++++++++++++++++++++++++++++++++-
26  init/main.c                             | 16 ++++++++
27  3 files changed, 98 insertions(+), 1 deletion(-)
28
29 --- a/arch/arm/Kconfig
30 +++ b/arch/arm/Kconfig
31 @@ -1934,6 +1934,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 @@ -4,6 +4,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 @@ -67,6 +69,65 @@ static uint32_t get_cell_size(const void
61         return cell_size;
62  }
63  
64 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
65 +
66 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
67 +{
68 +       char *ptr, *end;
69 +       char *root="root=";
70 +       int i, l;
71 +       const char *rootblock;
72 +
73 +       //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
74 +       ptr = str - 1;
75 +
76 +       do {
77 +               //first find an 'r' at the begining or after a space
78 +               do {
79 +                       ptr++;
80 +                       ptr = strchr(ptr, 'r');
81 +                       if(!ptr) return dest;
82 +
83 +               } while (ptr != str && *(ptr-1) != ' ');
84 +
85 +               //then check for the rest
86 +               for(i = 1; i <= 4; i++)
87 +                       if(*(ptr+i) != *(root+i)) break;
88 +
89 +       } while (i != 5);
90 +
91 +       end = strchr(ptr, ' ');
92 +       end = end ? (end - 1) : (strchr(ptr, 0) - 1);
93 +
94 +       //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
95 +       for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
96 +       ptr = end + 1;
97 +
98 +       /* if append-rootblock property is set use it to append to command line */
99 +       rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
100 +       if(rootblock != NULL) {
101 +               if(*dest != ' ') {
102 +                       *dest = ' ';
103 +                       dest++;
104 +                       len++;
105 +               }
106 +               if (len + l + i <= COMMAND_LINE_SIZE) {
107 +                       memcpy(dest, rootblock, l);
108 +                       dest += l - 1;
109 +                       memcpy(dest, ptr, i);
110 +                       dest += i;
111 +               }
112 +       } else {
113 +               len = strlen(str);
114 +               if (len + 1 < COMMAND_LINE_SIZE) {
115 +                       memcpy(dest, str, len);
116 +                       dest += len;
117 +               }
118 +       }
119 +       return dest;
120 +}
121 +#endif
122 +
123  static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
124  {
125         char cmdline[COMMAND_LINE_SIZE];
126 @@ -86,12 +147,21 @@ static void merge_fdt_bootargs(void *fdt
127  
128         /* and append the ATAG_CMDLINE */
129         if (fdt_cmdline) {
130 +
131 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
132 +               //save original bootloader args
133 +               //and append ubi.mtd with root partition number to current cmdline
134 +               setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
135 +               ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
136 +
137 +#else
138                 len = strlen(fdt_cmdline);
139                 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
140                         *ptr++ = ' ';
141                         memcpy(ptr, fdt_cmdline, len);
142                         ptr += len;
143                 }
144 +#endif
145         }
146         *ptr = '\0';
147  
148 @@ -148,7 +218,9 @@ int atags_to_fdt(void *atag_list, void *
149                         else
150                                 setprop_string(fdt, "/chosen", "bootargs",
151                                                atag->u.cmdline.cmdline);
152 -               } else if (atag->hdr.tag == ATAG_MEM) {
153 +               }
154 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
155 +               else if (atag->hdr.tag == ATAG_MEM) {
156                         if (memcount >= sizeof(mem_reg_property)/4)
157                                 continue;
158                         if (!atag->u.mem.size)
159 @@ -187,6 +259,10 @@ int atags_to_fdt(void *atag_list, void *
160                 setprop(fdt, "/memory", "reg", mem_reg_property,
161                         4 * memcount * memsize);
162         }
163 +#else
164 +
165 +       }
166 +#endif
167  
168         return fdt_pack(fdt);
169  }
170 --- a/init/main.c
171 +++ b/init/main.c
172 @@ -95,6 +95,10 @@
173  #include <asm/sections.h>
174  #include <asm/cacheflush.h>
175  
176 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
177 +#include <linux/of.h>
178 +#endif
179 +
180  static int kernel_init(void *);
181  
182  extern void init_IRQ(void);
183 @@ -573,6 +577,18 @@ asmlinkage __visible void __init start_k
184         page_alloc_init();
185  
186         pr_notice("Kernel command line: %s\n", boot_command_line);
187 +
188 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
189 +       //Show bootloader's original command line for reference
190 +       if(of_chosen) {
191 +               const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
192 +               if(prop)
193 +                       pr_notice("Bootloader command line (ignored): %s\n", prop);
194 +               else
195 +                       pr_notice("Bootloader command line not present\n");
196 +       }
197 +#endif
198 +
199         /* parameters may set static keys */
200         jump_label_init();
201         parse_early_param();