kernel: Move append-dtb to common image-commands
[librecmc/librecmc.git] / target / linux / ipq806x / patches-4.4 / 996-ATAG_DTB_COMPAT_CMDLINE_MANGLE.patch
1 Author: Adrian Panella <ianchi74@outlook.com>
2 Date:   Fri Jun 10 19:10:15 2016 -0500
3
4 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
17 Signed-off-by: Adrian Panella <ianchi74@outlook.com>
18
19 --- a/arch/arm/Kconfig
20 +++ b/arch/arm/Kconfig
21 @@ -1927,6 +1927,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
22           The command-line arguments provided by the boot loader will be
23           appended to the the device tree bootargs property.
24  
25 +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
26 +       bool "Append rootblock parsing bootloader's kernel arguments"
27 +       help
28 +         The command-line arguments provided by the boot loader will be
29 +         appended to a new device tree property: bootloader-args.
30 +         If there is a property "append-rootblock" in DT under /chosen 
31 +         and a root= option in bootloaders command line it will be parsed 
32 +         and added to DT bootargs with the form: <append-rootblock>XX.
33 +         Only command line ATAG will be processed, the rest of the ATAGs
34 +         sent by bootloader will be ignored.
35 +
36  endchoice
37  
38  config CMDLINE
39 --- a/arch/arm/boot/compressed/atags_to_fdt.c
40 +++ b/arch/arm/boot/compressed/atags_to_fdt.c
41 @@ -3,6 +3,8 @@
42  
43  #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
44  #define do_extend_cmdline 1
45 +#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
46 +#define do_extend_cmdline 1
47  #else
48  #define do_extend_cmdline 0
49  #endif
50 @@ -66,6 +68,59 @@ static uint32_t get_cell_size(const void
51         return cell_size;
52  }
53  
54 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
55 +
56 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
57 +{
58 +       char *ptr, *end;
59 +       char *root="root=";
60 +       int i, l;
61 +       const char *rootblock;
62 +
63 +       //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
64 +       ptr = str - 1;
65 +
66 +       do {
67 +               ptr++;
68 +               //first find an 'r' at the begining or after a space
69 +               do {
70 +                       ptr = strchr(ptr, 'r');
71 +                       if(!ptr) return dest;
72 +
73 +               } while (ptr != str && *(ptr-1) != ' ');
74 +
75 +               //then check for the rest
76 +               for(i = 1; i <= 4; i++)
77 +                       if(*(ptr+i) != *(root+i)) break;
78 +
79 +       } while (i != 5);
80 +
81 +       end = strchr(ptr, ' ');
82 +       end = end ? (end - 1) : (strchr(ptr, 0) - 1);
83 +
84 +       //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
85 +       for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
86 +       ptr = end + 1;
87 +
88 +       /* if append-rootblock property is set use it to append to command line */
89 +       rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
90 +       if(rootblock != NULL) {
91 +               if(*dest != ' ') {
92 +                       *dest = ' ';
93 +                       dest++;
94 +                       len++;
95 +               }
96 +               if (len + l + i <= COMMAND_LINE_SIZE) {
97 +                       memcpy(dest, rootblock, l);
98 +                       dest += l - 1;
99 +                       memcpy(dest, ptr, i);
100 +                       dest += i;
101 +               }
102 +       }
103 +       return dest;
104 +}
105 +#endif
106 +
107  static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
108  {
109         char cmdline[COMMAND_LINE_SIZE];
110 @@ -85,12 +134,21 @@ static void merge_fdt_bootargs(void *fdt
111  
112         /* and append the ATAG_CMDLINE */
113         if (fdt_cmdline) {
114 +
115 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
116 +               //save original bootloader args
117 +               //and append ubi.mtd with root partition number to current cmdline
118 +               setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
119 +               ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
120 +
121 +#else
122                 len = strlen(fdt_cmdline);
123                 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
124                         *ptr++ = ' ';
125                         memcpy(ptr, fdt_cmdline, len);
126                         ptr += len;
127                 }
128 +#endif
129         }
130         *ptr = '\0';
131  
132 @@ -147,7 +205,9 @@ int atags_to_fdt(void *atag_list, void *
133                         else
134                                 setprop_string(fdt, "/chosen", "bootargs",
135                                                atag->u.cmdline.cmdline);
136 -               } else if (atag->hdr.tag == ATAG_MEM) {
137 +               }
138 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
139 +               else if (atag->hdr.tag == ATAG_MEM) {
140                         if (memcount >= sizeof(mem_reg_property)/4)
141                                 continue;
142                         if (!atag->u.mem.size)
143 @@ -186,6 +246,10 @@ int atags_to_fdt(void *atag_list, void *
144                 setprop(fdt, "/memory", "reg", mem_reg_property,
145                         4 * memcount * memsize);
146         }
147 +#else
148 +
149 +       }
150 +#endif
151  
152         return fdt_pack(fdt);
153  }
154 --- a/init/main.c
155 +++ b/init/main.c
156 @@ -88,6 +88,10 @@
157  #include <asm/sections.h>
158  #include <asm/cacheflush.h>
159  
160 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
161 +#include <linux/of.h>
162 +#endif
163 +
164  static int kernel_init(void *);
165  
166  extern void init_IRQ(void);
167 @@ -585,6 +589,18 @@ asmlinkage __visible void __init start_k
168         page_alloc_init();
169  
170         pr_notice("Kernel command line: %s\n", boot_command_line);
171 +
172 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
173 +       //Show bootloader's original command line for reference
174 +       if(of_chosen) {
175 +               const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
176 +               if(prop)
177 +                       pr_notice("Bootloader command line (ignored): %s\n", prop);
178 +               else
179 +                       pr_notice("Bootloader command line not present\n");
180 +       }
181 +#endif
182 +
183         parse_early_param();
184         after_dashes = parse_args("Booting kernel",
185                                   static_command_line, __start___param,