1 From 13bb6d8dd9138927950a520a288401db82871dc9 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Sun, 21 Jan 2024 23:36:57 +0100
4 Subject: [PATCH] ARM: decompressor: support for ATAGs rootblock parsing
6 The command-line arguments provided by the boot loader will be
7 appended to a new device tree property: bootloader-args.
9 If there is a property "append-rootblock" in DT under /chosen
10 and a root= option in bootloaders command line it will be parsed
11 and added to DT bootargs with the form: <append-rootblock>XX.
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>
17 [ reworked to a cleaner patch ]
18 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
20 arch/arm/Kconfig | 10 +++
21 arch/arm/boot/compressed/atags_to_fdt.c | 102 ++++++++++++++++++++++--
23 3 files changed, 117 insertions(+), 7 deletions(-)
25 --- a/arch/arm/Kconfig
26 +++ b/arch/arm/Kconfig
27 @@ -1599,6 +1599,16 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
28 The command-line arguments provided by the boot loader will be
29 appended to the the device tree bootargs property.
31 +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE
32 + bool "Append rootblock parsing bootloader's kernel arguments"
34 + The command-line arguments provided by the boot loader will be
35 + appended to a new device tree property: bootloader-args.
37 + If there is a property "append-rootblock" in DT under /chosen
38 + and a root= option in bootloaders command line it will be parsed
39 + and added to DT bootargs with the form: <append-rootblock>XX.
43 config CMDLINE_OVERRIDE
44 --- a/arch/arm/boot/compressed/atags_to_fdt.c
45 +++ b/arch/arm/boot/compressed/atags_to_fdt.c
47 #include <asm/setup.h>
50 -#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND)
51 +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND) || \
52 + defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
53 #define do_extend_cmdline 1
55 #define do_extend_cmdline 0
56 @@ -69,6 +70,83 @@ static uint32_t get_cell_size(const void
61 + * taken from arch/x86/boot/string.c
62 + * local_strstr - Find the first substring in a %NUL terminated string
63 + * @s1: The string to be searched
64 + * @s2: The string to search for
66 +static char *local_strstr(const char *s1, const char *s2)
76 + if (!memcmp(s1, s2, l2))
83 +static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
85 + char *ptr, *end, *tmp;
86 + const char *root="root=";
87 + const char *find_rootblock;
89 + const char *rootblock;
91 + find_rootblock = getprop(fdt, "/chosen", "find-rootblock", &l);
92 + if (!find_rootblock)
93 + find_rootblock = root;
95 + /* ARM doesn't have __HAVE_ARCH_STRSTR, so it was copied from x86 */
96 + ptr = local_strstr(str, find_rootblock);
100 + end = strchr(ptr, ' ');
101 + end = end ? (end - 1) : (strchr(ptr, 0) - 1);
103 + /* Some boards ubi.mtd=XX,ZZZZ, so let's check for '," too. */
104 + tmp = strchr(ptr, ',');
106 + end = end < tmp ? end : tmp - 1;
109 + * find partition number
110 + * (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX | ubi.mtd=XX,ZZZZ )
112 + for (i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++);
116 + /* if append-rootblock property is set use it to append to command line */
117 + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l);
118 + if (rootblock != NULL) {
119 + if (*dest != ' ') {
125 + if (len + l + i <= COMMAND_LINE_SIZE) {
126 + memcpy(dest, rootblock, l);
129 + memcpy(dest, ptr, i);
137 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
139 char cmdline[COMMAND_LINE_SIZE];
140 @@ -86,13 +164,23 @@ static void merge_fdt_bootargs(void *fdt
144 - /* and append the ATAG_CMDLINE */
146 - len = strlen(fdt_cmdline);
147 - if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
149 - memcpy(ptr, fdt_cmdline, len);
151 + if (IS_ENABLED(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)) {
153 + * save original bootloader args
154 + * and append ubi.mtd with root partition number
155 + * to current cmdline
157 + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline);
158 + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt);
160 + /* and append the ATAG_CMDLINE */
161 + len = strlen(fdt_cmdline);
162 + if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) {
164 + memcpy(ptr, fdt_cmdline, len);
173 #include <linux/initrd.h>
174 #include <linux/memblock.h>
175 #include <linux/acpi.h>
176 +#include <linux/of.h>
177 #include <linux/bootconfig.h>
178 #include <linux/console.h>
179 #include <linux/nmi.h>
180 @@ -995,6 +996,17 @@ asmlinkage __visible void __init __no_sa
181 pr_notice("Kernel command line: %s\n", saved_command_line);
182 /* parameters may set static keys */
185 + /* Show bootloader's original command line for reference */
186 + if (IS_ENABLED(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) && of_chosen) {
187 + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL);
190 + pr_notice("Bootloader command line (ignored): %s\n", prop);
192 + pr_notice("Bootloader command line not present\n");
196 after_dashes = parse_args("Booting kernel",
197 static_command_line, __start___param,