# # Copyright (C) 2006-2011 OpenWrt.org # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # define KernelPackage/nls-base SUBMENU:=Native Language Support TITLE:=Native Language Support KCONFIG:=CONFIG_NLS FILES:=$(LINUX_DIR)/fs/nls/nls_base.ko endef define KernelPackage/nls-base/description Kernel module for NLS (Native Language Support) endef $(eval $(call KernelPackage,nls-base)) define KernelPackage/nls-cp437 SUBMENU:=Native Language Support TITLE:=Codepage 437 (United States, Canada) KCONFIG:=CONFIG_NLS_CODEPAGE_437 FILES:=$(LINUX_DIR)/fs/nls/nls_cp437.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp437) $(call AddDepends/nls) endef define KernelPackage/nls-cp437/description Kernel module for NLS Codepage 437 (United States, Canada) endef $(eval $(call KernelPackage,nls-cp437)) define KernelPackage/nls-cp775 SUBMENU:=Native Language Support TITLE:=Codepage 775 (Baltic Rim) KCONFIG:=CONFIG_NLS_CODEPAGE_775 FILES:=$(LINUX_DIR)/fs/nls/nls_cp775.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp775) $(call AddDepends/nls) endef define KernelPackage/nls-cp775/description Kernel module for NLS Codepage 775 (Baltic Rim) endef $(eval $(call KernelPackage,nls-cp775)) define KernelPackage/nls-cp850 SUBMENU:=Native Language Support TITLE:=Codepage 850 (Europe) KCONFIG:=CONFIG_NLS_CODEPAGE_850 FILES:=$(LINUX_DIR)/fs/nls/nls_cp850.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp850) $(call AddDepends/nls) endef define KernelPackage/nls-cp850/description Kernel module for NLS Codepage 850 (Europe) endef $(eval $(call KernelPackage,nls-cp850)) define KernelPackage/nls-cp852 SUBMENU:=Native Language Support TITLE:=Codepage 852 (Europe) KCONFIG:=CONFIG_NLS_CODEPAGE_852 FILES:=$(LINUX_DIR)/fs/nls/nls_cp852.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp852) $(call AddDepends/nls) endef define KernelPackage/nls-cp852/description Kernel module for NLS Codepage 852 (Europe) endef $(eval $(call KernelPackage,nls-cp852)) define KernelPackage/nls-cp862 SUBMENU:=Native Language Support TITLE:=Codepage 862 (Hebrew) KCONFIG:=CONFIG_NLS_CODEPAGE_862 FILES:=$(LINUX_DIR)/fs/nls/nls_cp862.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp862) $(call AddDepends/nls) endef define KernelPackage/nls-cp862/description Kernel module for NLS Codepage 862 (Hebrew) endef $(eval $(call KernelPackage,nls-cp862)) define KernelPackage/nls-cp864 SUBMENU:=Native Language Support TITLE:=Codepage 864 (Arabic) KCONFIG:=CONFIG_NLS_CODEPAGE_864 FILES:=$(LINUX_DIR)/fs/nls/nls_cp864.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp864) $(call AddDepends/nls) endef define KernelPackage/nls-cp864/description Kernel module for NLS Codepage 864 (Arabic) endef $(eval $(call KernelPackage,nls-cp864)) define KernelPackage/nls-cp866 SUBMENU:=Native Language Support TITLE:=Codepage 866 (Cyrillic) KCONFIG:=CONFIG_NLS_CODEPAGE_866 FILES:=$(LINUX_DIR)/fs/nls/nls_cp866.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp866) $(call AddDepends/nls) endef define KernelPackage/nls-cp866/description Kernel module for NLS Codepage 866 (Cyrillic) endef $(eval $(call KernelPackage,nls-cp866)) define KernelPackage/nls-cp932 SUBMENU:=Native Language Support TITLE:=Codepage 932 (Japanese) KCONFIG:=CONFIG_NLS_CODEPAGE_932 FILES:=$(LINUX_DIR)/fs/nls/nls_cp932.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp932) $(call AddDepends/nls) endef define KernelPackage/nls-cp932/description Kernel module for NLS Codepage 932 (Japanese) endef $(eval $(call KernelPackage,nls-cp932)) define KernelPackage/nls-cp1250 SUBMENU:=Native Language Support TITLE:=Codepage 1250 (Eastern Europe) KCONFIG:=CONFIG_NLS_CODEPAGE_1250 FILES:=$(LINUX_DIR)/fs/nls/nls_cp1250.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp1250) $(call AddDepends/nls) endef define KernelPackage/nls-cp1250/description Kernel module for NLS Codepage 1250 (Eastern Europe) endef $(eval $(call KernelPackage,nls-cp1250)) define KernelPackage/nls-cp1251 SUBMENU:=Native Language Support TITLE:=Codepage 1251 (Russian) KCONFIG:=CONFIG_NLS_CODEPAGE_1251 FILES:=$(LINUX_DIR)/fs/nls/nls_cp1251.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp1251) $(call AddDepends/nls) endef define KernelPackage/nls-cp1251/description Kernel module for NLS Codepage 1251 (Russian) endef $(eval $(call KernelPackage,nls-cp1251)) define KernelPackage/nls-iso8859-1 SUBMENU:=Native Language Support TITLE:=ISO 8859-1 (Latin 1; Western European Languages) KCONFIG:=CONFIG_NLS_ISO8859_1 FILES:=$(LINUX_DIR)/fs/nls/nls_iso8859-1.ko AUTOLOAD:=$(call AutoLoad,25,nls_iso8859-1) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-1/description Kernel module for NLS ISO 8859-1 (Latin 1) endef $(eval $(call KernelPackage,nls-iso8859-1)) define KernelPackage/nls-iso8859-2 SUBMENU:=Native Language Support TITLE:=ISO 8859-2 (Latin 2; Central European Languages) KCONFIG:=CONFIG_NLS_ISO8859_2 FILES:=$(LINUX_DIR)/fs/nls/nls_iso8859-2.ko AUTOLOAD:=$(call AutoLoad,25,nls_iso8859-2) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-2/description Kernel module for NLS ISO 8859-2 (Latin 2) endef $(eval $(call KernelPackage,nls-iso8859-2)) define KernelPackage/nls-iso8859-6 SUBMENU:=Native Language Support TITLE:=ISO 8859-6 (Arabic) KCONFIG:=CONFIG_NLS_ISO8859_6 FILES:=$(LINUX_DIR)/fs/nls/nls_iso8859-6.ko AUTOLOAD:=$(call AutoLoad,25,nls_iso8859-6) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-6/description Kernel module for NLS ISO 8859-6 (Arabic) endef $(eval $(call KernelPackage,nls-iso8859-6)) define KernelPackage/nls-iso8859-8 SUBMENU:=Native Language Support TITLE:=ISO 8859-8, CP1255 (Hebrew) KCONFIG:=CONFIG_NLS_ISO8859_8 FILES:=$(LINUX_DIR)/fs/nls/nls_cp1255.ko AUTOLOAD:=$(call AutoLoad,25,nls_cp1255) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-8/description Kernel module for Hebrew charsets (ISO-8859-8, CP1255) endef $(eval $(call KernelPackage,nls-iso8859-8)) define KernelPackage/nls-iso8859-13 SUBMENU:=Native Language Support TITLE:=ISO 8859-13 (Latin 7; Baltic) KCONFIG:=CONFIG_NLS_ISO8859_13 FILES:=$(LINUX_DIR)/fs/nls/nls_iso8859-13.ko AUTOLOAD:=$(call AutoLoad,25,nls_iso8859-13) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-13/description Kernel module for NLS ISO 8859-13 (Latin 7; Baltic) endef $(eval $(call KernelPackage,nls-iso8859-13)) define KernelPackage/nls-iso8859-15 SUBMENU:=Native Language Support TITLE:=ISO 8859-15 (Latin 9; Western, with Euro symbol) KCONFIG:=CONFIG_NLS_ISO8859_15 FILES:=$(LINUX_DIR)/fs/nls/nls_iso8859-15.ko AUTOLOAD:=$(call AutoLoad,25,nls_iso8859-15) $(call AddDepends/nls) endef define KernelPackage/nls-iso8859-15/description Kernel module for NLS ISO 8859-15 (Latin 9) endef $(eval $(call KernelPackage,nls-iso8859-15)) define KernelPackage/nls-koi8r SUBMENU:=Native Language Support TITLE:=KOI8-R (Russian) KCONFIG:=CONFIG_NLS_KOI8_R FILES:=$(LINUX_DIR)/fs/nls/nls_koi8-r.ko AUTOLOAD:=$(call AutoLoad,25,nls_koi8-r) $(call AddDepends/nls) endef define KernelPackage/nls-koi8r/description Kernel module for NLS KOI8-R (Russian) endef $(eval $(call KernelPackage,nls-koi8r)) define KernelPackage/nls-utf8 SUBMENU:=Native Language Support TITLE:=UTF-8 KCONFIG:=CONFIG_NLS_UTF8 FILES:=$(LINUX_DIR)/fs/nls/nls_utf8.ko AUTOLOAD:=$(call AutoLoad,25,nls_utf8) $(call AddDepends/nls) endef define KernelPackage/nls-utf8/description Kernel module for NLS UTF-8 endef $(eval $(call KernelPackage,nls-utf8)) 0' href='#n100'>100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
From 71270226b14733a4b1f2cde58ea9265caa50b38d Mon Sep 17 00:00:00 2001
From: Adrian Panella <ianchi74@outlook.com>
Date: Thu, 9 Mar 2017 09:37:17 +0100
Subject: [PATCH 67/69] 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: <append-rootblock>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 <ianchi74@outlook.com>
---
 arch/arm/Kconfig                        | 11 +++++
 arch/arm/boot/compressed/atags_to_fdt.c | 72 ++++++++++++++++++++++++++++++++-
 init/main.c                             | 16 ++++++++
 3 files changed, 98 insertions(+), 1 deletion(-)

--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1780,6 +1780,17 @@ config ARM_ATAG_DTB_COMPAT_CMDLINE_EXTEN
 	  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: <append-rootblock>XX.
+	  Only command line ATAG will be processed, the rest of the ATAGs
+	  sent by bootloader will be ignored.
+
 endchoice
 
 config CMDLINE
--- 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,80 @@ static uint32_t get_cell_size(const void
 	return cell_size;
 }
 
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+/**
+ * taken from arch/x86/boot/string.c
+ * local_strstr - Find the first substring in a %NUL terminated string
+ * @s1: The string to be searched
+ * @s2: The string to search for
+ */
+static char *local_strstr(const char *s1, const char *s2)
+{
+	size_t l1, l2;
+
+	l2 = strlen(s2);
+	if (!l2)
+		return (char *)s1;
+	l1 = strlen(s1);
+	while (l1 >= l2) {
+		l1--;
+		if (!memcmp(s1, s2, l2))
+			return (char *)s1;
+		s1++;
+	}
+	return NULL;
+}
+
+static char *append_rootblock(char *dest, const char *str, int len, void *fdt)
+{
+	char *ptr, *end, *tmp;
+	char *root="root=";
+	char *find_rootblock;
+	int i, l;
+	const char *rootblock;
+
+	find_rootblock = getprop(fdt, "/chosen", "find-rootblock", &l);
+	if(!find_rootblock)
+		find_rootblock = root;
+
+	//ARM doesn't have __HAVE_ARCH_STRSTR, so it was copied from x86
+	ptr = local_strstr(str, find_rootblock);
+
+	if(!ptr)
+		return dest;
+
+	end = strchr(ptr, ' ');
+	end = end ? (end - 1) : (strchr(ptr, 0) - 1);
+
+	// Some boards ubi.mtd=XX,ZZZZ, so let's check for '," too.
+	tmp = strchr(ptr, ',');
+
+	if(tmp)
+		end = end < tmp ? end : tmp - 1;
+
+	//find partition number (assumes format root=/dev/mtdXX | /dev/mtdblockXX | yy:XX | ubi.mtd=XX,ZZZZ )
+	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) {
+		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;
+}
+#endif
+
 static void merge_fdt_bootargs(void *fdt, const char *fdt_cmdline)
 {
 	char cmdline[COMMAND_LINE_SIZE];
@@ -88,12 +164,21 @@ static void merge_fdt_bootargs(void *fdt
 
 	/* 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 +253,9 @@ int atags_to_fdt(void *atag_list, void *
 			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 +299,10 @@ int atags_to_fdt(void *atag_list, void *
 		setprop(fdt, "/memory", "reg", mem_reg_property,
 			4 * memcount * memsize);
 	}
+#else
+
+	}
+#endif
 
 	return fdt_pack(fdt);
 }
--- a/init/main.c
+++ b/init/main.c
@@ -110,6 +110,10 @@
 
 #include <kunit/test.h>
 
+#if defined(CONFIG_ARM_ATAG_DTB_COMPAT_CMDLINE_MANGLE)
+#include <linux/of.h>
+#endif
+
 static int kernel_init(void *);
 
 extern void init_IRQ(void);
@@ -905,6 +909,18 @@ asmlinkage __visible void __init __no_sa
 	pr_notice("Kernel command line: %s\n", saved_command_line);
 	/* parameters may set static keys */
 	jump_label_init();
+
+#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
+
 	parse_early_param();
 	after_dashes = parse_args("Booting kernel",
 				  static_command_line, __start___param,