From 7f478ddf690dfccd8aea406a9697e1448920bf0f Mon Sep 17 00:00:00 2001 From: Adrian Panella Date: Thu, 9 Mar 2017 09:37:17 +0100 Subject: [PATCH 05/10] generic: Mangle bootloader's kernel arguments The command-line arguments provided by the boot loader will be appended to a new device tree property: bootloader-args. If there is a property "append-rootblock" in DT under /chosen and a root= option in bootloaders command line it will be parsed and added to DT bootargs with the form: XX. Only command line ATAG will be processed, the rest of the ATAGs sent by bootloader will be ignored. This is usefull in dual boot systems, to get the current root partition without afecting the rest of the system. Signed-off-by: Adrian Panella This patch has been modified to be mvebu specific. The original patch did not pass the bootloader cmdline on if no append-rootblock stanza was found, resulting in blank cmdline and failure to boot. Signed-off-by: Michael Gray --- arch/arm/Kconfig | 11 ++++ arch/arm/boot/compressed/atags_to_fdt.c | 85 ++++++++++++++++++++++++- init/main.c | 16 +++++ 3 files changed, 111 insertions(+), 1 deletion(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 7630ba9cb6cc..dae040cc2a4f 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1638,6 +1638,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND The command-line arguments provided by the boot loader will be appended to the the device tree bootargs property. +config ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE + bool "Append rootblock parsing bootloader's kernel arguments" + help + The command-line arguments provided by the boot loader will be + appended to a new device tree property: bootloader-args. + If there is a property "append-rootblock" in DT under /chosen + and a root= option in bootloaders command line it will be parsed + and added to DT bootargs with the form: XX. + Only command line ATAG will be processed, the rest of the ATAGs + sent by bootloader will be ignored. + endchoice config CMDLINE diff --git a/arch/arm/boot/compressed/atags_to_fdt.c b/arch/arm/boot/compressed/atags_to_fdt.c index 1feb6b0f7a1f..fb8cd4f07e60 100644 --- a/arch/arm/boot/compressed/atags_to_fdt.c +++ b/arch/arm/boot/compressed/atags_to_fdt.c @@ -5,6 +5,8 @@ #if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEND) #define do_extend_cmdline 1 +#elif defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) +#define do_extend_cmdline 1 #else #define do_extend_cmdline 0 #endif @@ -69,6 +71,72 @@ static uint32_t get_cell_size(const void *fdt) return cell_size; } +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) + +static char *append_rootblock(char *dest, const char *str, int len, void *fdt) +{ + char *ptr, *end; + char *root="root="; + int i, l; + const char *rootblock; + + //ARM doesn't have __HAVE_ARCH_STRSTR, so search manually + ptr = str - 1; + + do { + //first find an 'r' at the begining or after a space + do { + ptr++; + ptr = strchr(ptr, 'r'); + if (!ptr) + goto no_append; + + } while (ptr != str && *(ptr-1) != ' '); + + //then check for the rest + for(i = 1; i <= 4; i++) + if(*(ptr+i) != *(root+i)) break; + + } while (i != 5); + + end = strchr(ptr, ' '); + end = end ? (end - 1) : (strchr(ptr, 0) - 1); + + //find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX ) + for( i = 0; end >= ptr && *end >= '0' && *end <= '9'; end--, i++); + ptr = end + 1; + + /* if append-rootblock property is set use it to append to command line */ + rootblock = getprop(fdt, "/chosen", "append-rootblock", &l); + if (rootblock == NULL) + goto no_append; + + if (*dest != ' ') { + *dest = ' '; + dest++; + len++; + } + + if (len + l + i <= COMMAND_LINE_SIZE) { + memcpy(dest, rootblock, l); + dest += l - 1; + memcpy(dest, ptr, i); + dest += i; + } + + return dest; + +no_append: + len = strlen(str); + if (len + 1 < COMMAND_LINE_SIZE) { + memcpy(dest, str, len); + dest += len; + } + + return dest; +} +#endif + static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline) { char cmdline[COMMAND_LINE_SIZE]; @@ -88,12 +156,21 @@ static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline) /* and append the ATAG_CMDLINE */ if (fdt_cmdline) { + +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) + //save original bootloader args + //and append ubi.mtd with root partition number to current cmdline + setprop_string(fdt, "/chosen", "bootloader-args", fdt_cmdline); + ptr = append_rootblock(ptr, fdt_cmdline, len, fdt); + +#else len = strlen(fdt_cmdline); if (ptr - cmdline + len + 2 < COMMAND_LINE_SIZE) { *ptr++ = ' '; memcpy(ptr, fdt_cmdline, len); ptr += len; } +#endif } *ptr = '\0'; @@ -168,7 +245,9 @@ int atags_to_fdt(void *atag_list, void *fdt, int total_space) else setprop_string(fdt, "/chosen", "bootargs", atag->u.cmdline.cmdline); - } else if (atag->hdr.tag == ATAG_MEM) { + } +#ifndef CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE + else if (atag->hdr.tag == ATAG_MEM) { if (memcount >= sizeof(mem_reg_property)/4) continue; if (!atag->u.mem.size) @@ -212,6 +291,10 @@ int atags_to_fdt(void *atag_list, void *fdt, int total_space) setprop(fdt, "/memory", "reg", mem_reg_property, 4 * memcount * memsize); } +#else + + } +#endif return fdt_pack(fdt); } diff --git a/init/main.c b/init/main.c index 1fe7942f5d4a..fa08c129d3e1 100644 --- a/init/main.c +++ b/init/main.c @@ -113,6 +113,10 @@ #include +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) +#include +#endif + static int kernel_init(void *); extern void init_IRQ(void); @@ -961,6 +965,18 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void) page_alloc_init(); pr_notice("Kernel command line: %s\n", saved_command_line); + +#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE) + //Show bootloader's original command line for reference + if(of_chosen) { + const char *prop = of_get_property(of_chosen, "bootloader-args", NULL); + if(prop) + pr_notice("Bootloader command line (ignored): %s\n", prop); + else + pr_notice("Bootloader command line not present\n"); + } +#endif + /* parameters may set static keys */ jump_label_init(); parse_early_param(); -- 2.37.2