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
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.
16 Signed-off-by: Adrian Panella <ianchi74@outlook.com>
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(-)
23 --- a/arch/arm/Kconfig
24 +++ b/arch/arm/Kconfig
25 @@ -1777,6 +1777,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.
29 +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
30 + bool "Append rootblock parsing bootloader's kernel arguments"
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.
43 --- a/arch/arm/boot/compressed/atags_to_fdt.c
44 +++ b/arch/arm/boot/compressed/atags_to_fdt.c
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
52 #define do_extend_cmdline 0
54 @@ -69,6 +71,59 @@ static uint32_t get_cell_size(const void
58 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
60 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
65 + const char *rootblock;
67 + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually
71 + //first find an 'r' at the begining or after a space
74 + ptr = strchr(ptr, 'r');
75 + if(!ptr) return dest;
77 + } while (ptr != str && *(ptr-1) != ' ');
79 + //then check for the rest
80 + for(i = 1; i <= 4; i++)
81 + if(*(ptr+i) != *(root+i)) break;
85 + end = strchr(ptr, ' ');
86 + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
88 + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX )
89 + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
92 + /* if append-rootblock property is set use it to append to command line */
93 + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
94 + if(rootblock != NULL) {
100 + if (len + l + i <= COMMAND_LINE_SIZE) {
101 + memcpy(dest, rootblock, l);
103 + memcpy(dest, ptr, i);
111 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
113 char cmdline[COMMAND_LINE_SIZE];
114 @@ -88,12 +143,21 @@ static void merge_fdt_bootargs(void *fdt
116 /* and append the ATAG_CMDLINE */
119 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
120 + //save original bootloader args
121 + //and append ubi.mtd with root partition number to current cmdline
122 + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
123 + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
126 len = strlen(fdt_cmdline);
127 if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
129 memcpy(ptr, fdt_cmdline, len);
136 @@ -168,7 +232,9 @@ int atags_to_fdt(void *atag_list, void *
138 setprop_string(fdt, "/chosen", "bootargs",
139 atag->u.cmdline.cmdline);
140 - } else if (atag->hdr.tag == ATAG_MEM) {
142 +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
143 + else if (atag->hdr.tag == ATAG_MEM) {
144 if (memcount >= sizeof(mem_reg_property)/4)
146 if (!atag->u.mem.size)
147 @@ -212,6 +278,10 @@ int atags_to_fdt(void *atag_list, void *
148 setprop(fdt, "/memory", "reg", mem_reg_property,
149 4 * memcount * memsize);
156 return fdt_pack(fdt);
162 #include <kunit/test.h>
164 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
165 +#include <linux/of.h>
168 static int kernel_init(void *);
170 extern void init_IRQ(void);
171 @@ -904,6 +908,18 @@ asmlinkage __visible void __init __no_sa
174 pr_notice("Kernel command line: %s\n", saved_command_line);
176 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
177 + //Show bootloader's original command line for reference
179 + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
181 + pr_notice("Bootloader command line (ignored): %s\n", prop);
183 + pr_notice("Bootloader command line not present\n");
187 /* parameters may set static keys */