1 Author: Adrian Panella <ianchi74@outlook.com>
2 Date: Fri Jun 10 19:10:15 2016 -0500
4 generic: Mangle bootloader's kernel arguments
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.
17 Signed-off-by: Adrian Panella <ianchi74@outlook.com>
19 --- a/arch/arm/Kconfig
20 +++ b/arch/arm/Kconfig
21 @@ -1928,6 +1928,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.
25 +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
26 + bool "Append rootblock parsing bootloader's kernel arguments"
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.
39 --- a/arch/arm/boot/compressed/atags_to_fdt.c
40 +++ b/arch/arm/boot/compressed/atags_to_fdt.c
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
48 #define do_extend_cmdline 0
50 @@ -66,6 +68,59 @@ static uint32_t get_cell_size(const void
54 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
56 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
61 + const char *rootblock;
63 + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
68 + //first find an 'r' at the begining or after a space
70 + ptr = strchr(ptr, 'r');
71 + if(!ptr) return dest;
73 + } while (ptr != str && *(ptr-1) != ' ');
75 + //then check for the rest
76 + for(i = 1; i <= 4; i++)
77 + if(*(ptr+i) != *(root+i)) break;
81 + end = strchr(ptr, ' ');
82 + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
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++);
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) {
96 + if (len + l + i <= COMMAND_LINE_SIZE) {
97 + memcpy(dest, rootblock, l);
99 + memcpy(dest, ptr, i);
107 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
109 char cmdline[COMMAND_LINE_SIZE];
110 @@ -85,12 +140,21 @@ static void merge_fdt_bootargs(void *fdt
112 /* and append the ATAG_CMDLINE */
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);
122 len = strlen(fdt_cmdline);
123 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
125 memcpy(ptr, fdt_cmdline, len);
132 @@ -147,7 +211,9 @@ int atags_to_fdt(void *atag_list, void *
134 setprop_string(fdt, "/chosen", "bootargs",
135 atag->u.cmdline.cmdline);
136 - } else if (atag->hdr.tag == ATAG_MEM) {
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)
142 if (!atag->u.mem.size)
143 @@ -186,6 +252,10 @@ int atags_to_fdt(void *atag_list, void *
144 setprop(fdt, "/memory", "reg", mem_reg_property,
145 4 * memcount * memsize);
152 return fdt_pack(fdt);
157 #include <asm/sections.h>
158 #include <asm/cacheflush.h>
160 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
161 +#include <linux/of.h>
164 static int kernel_init(void *);
166 extern void init_IRQ(void);
167 @@ -560,6 +564,18 @@ asmlinkage __visible void __init start_k
170 pr_notice("Kernel command line: %s\n", boot_command_line);
172 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
173 + //Show bootloader's original command line for reference
175 + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
177 + pr_notice("Bootloader command line (ignored): %s\n", prop);
179 + pr_notice("Bootloader command line not present\n");
184 after_dashes = parse_args("Booting kernel",
185 static_command_line, __start___param,