msm8953 & msm8996 : Append "rootfstype=" and "root=" to kernel command line

This change is to get the system partition index and append it to kernel
command line instead of hardcoding the value in .conf files
used during the LE build process

Change-Id: I1b9e7744e0b95246e45911682cbb5c3ff61e9dab
diff --git a/makefile b/makefile
index 277a714..7082311 100644
--- a/makefile
+++ b/makefile
@@ -64,6 +64,10 @@
   CFLAGS += -DDISABLE_FASTBOOT_CMDS=1
 endif
 
+ifeq ($(APPEND_CMDLINE),1)
+  CFLAGS += -D_APPEND_CMDLINE=1
+endif
+
 # setup toolchain prefix
 TOOLCHAIN_PREFIX ?= arm-eabi-
 CFLAGS += -fstack-protector-all
diff --git a/project/msm8953.mk b/project/msm8953.mk
index 9582017..126c7ba 100644
--- a/project/msm8953.mk
+++ b/project/msm8953.mk
@@ -84,6 +84,10 @@
 DEFINES += USB30_SUPPORT=1
 endif
 
+ifeq ($(APPEND_CMDLINE),1)
+DEFINES += _APPEND_CMDLINE=1
+endif
+
 #SCM call before entering DLOAD mode
 DEFINES += PLATFORM_USE_SCM_DLOAD=1
 
diff --git a/project/msm8996.mk b/project/msm8996.mk
index baefdf8..de818bb 100644
--- a/project/msm8996.mk
+++ b/project/msm8996.mk
@@ -109,6 +109,10 @@
 DEFINES += LPAE=1
 endif
 
+ifeq ($(APPEND_CMDLINE),1)
+DEFINES += _APPEND_CMDLINE=1
+endif
+
 CFLAGS += -Werror
 
 #enable user force reset feature
diff --git a/target/msm8953/init.c b/target/msm8953/init.c
index 3d26442..91e36cb 100644
--- a/target/msm8953/init.c
+++ b/target/msm8953/init.c
@@ -76,6 +76,7 @@
 #define FASTBOOT_MODE           0x77665500
 #define RECOVERY_MODE           0x77665502
 #define PON_SOFT_RB_SPARE       0x88F
+#define EXT4_CMDLINE  " rootfstype=ext4 root=/dev/mmcblk0p"
 
 #define CE1_INSTANCE            1
 #define CE_EE                   1
@@ -107,6 +108,44 @@
 #endif
 }
 
+#if _APPEND_CMDLINE
+int get_target_boot_params(const char *cmdline, const char *part, char **buf)
+{
+	int system_ptn_index = -1;
+	uint32_t buflen;
+	int ret = -1;
+
+	if (!cmdline || !part ) {
+		dprintf(CRITICAL, "WARN: Invalid input param\n");
+		return -1;
+	}
+
+	if (!strstr(cmdline, "root=/dev/ram")) /* This check is to handle kdev boot */
+	{
+		if (target_is_emmc_boot()) {
+			buflen = strlen(EXT4_CMDLINE) + sizeof(int) +1;
+			*buf = (char *)malloc(buflen);
+			if(!(*buf)) {
+				dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
+				return -1;
+			}
+			/* Below is for emmc boot */
+			system_ptn_index = partition_get_index(part) + 1; /* Adding +1 as offsets for eMMC start at 1 and NAND at 0 */
+			if (system_ptn_index < 0) {
+				dprintf(CRITICAL,
+						"WARN: Cannot get partition index for %s\n", part);
+				free(*buf);
+				return -1;
+			}
+			snprintf(*buf, buflen, EXT4_CMDLINE"%d", system_ptn_index);
+			ret = 0;
+		}
+	}
+	/*in success case buf will be freed in the calling function of this*/
+	return ret;
+}
+#endif
+
 static void set_sdc_power_ctrl()
 {
 	/* Drive strength configs for sdc pins */
diff --git a/target/msm8996/init.c b/target/msm8996/init.c
index 20a4caf..dc3186d 100644
--- a/target/msm8996/init.c
+++ b/target/msm8996/init.c
@@ -668,3 +668,59 @@
 
 	return len;
 }
+
+#if _APPEND_CMDLINE
+int get_target_boot_params(const char *cmdline, const char *part, char **buf)
+{
+	int system_ptn_index = -1;
+	unsigned int lun = 0;
+	char lun_char_base = 'a', lun_char_limit = 'h';
+
+	/*allocate buflen for largest possible string*/
+	uint32_t buflen = strlen(" root=/dev/mmcblock0p") + sizeof(int) + 1; /*1 character for null termination*/
+
+	if (!cmdline || !part ) {
+		dprintf(CRITICAL, "WARN: Invalid input param\n");
+		return -1;
+	}
+
+	system_ptn_index = partition_get_index(part);
+	if (system_ptn_index == -1)
+	{
+		dprintf(CRITICAL,"Unable to find partition %s\n",part);
+		return -1;
+	}
+
+	*buf = (char *)malloc(buflen);
+	if(!(*buf)) {
+		dprintf(CRITICAL,"Unable to allocate memory for boot params\n");
+		return -1;
+	}
+
+	/*
+	 * check if cmdline contains "root="/"" at the beginning of buffer or
+	 * " root="/"ubi.mtd" in the middle of buffer.
+	 */
+	if ((strncmp(cmdline," root=",strlen(" root=")) == 0) ||
+		strstr(cmdline, " root="))
+		dprintf(DEBUG, "DEBUG: cmdline has root=\n");
+	else
+	{
+		if (platform_boot_dev_isemmc()) {
+			snprintf(*buf, buflen, " root=/dev/mmcblock0p%d",
+					system_ptn_index + 1);
+		} else {
+			lun = partition_get_lun(system_ptn_index);
+			if ((lun_char_base + lun) > lun_char_limit) {
+				dprintf(CRITICAL, "lun value exceeds limit\n");
+				return -1;
+			}
+			snprintf(*buf, buflen, " root=/dev/sd%c%d",
+					lun_char_base + lun,
+					partition_get_index_in_lun(part, lun));
+		}
+	}
+	/*in success case buf will be freed in the calling function of this*/
+	return 0;
+}
+#endif