app: aboot: add target boot params

Make aboot to get target specific boot params so that boot params can be
appended to kernel command line.
Also fix mdmkrypton to provide 'root=' as target boot param to get rid of
hardcoded root device name from boot image.

Change-Id: I10129a0249438b8bc7da1ea8a8c0097489e6e289
Signed-off-by: Joonwoo Park <joonwoop@codeaurora.org>
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index fa41388..53bcb56 100755
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -72,6 +72,8 @@
 extern  bool target_use_signed_kernel(void);
 extern void platform_uninit(void);
 extern void target_uninit(void);
+extern int get_target_boot_params(const char *cmdline, const char *part,
+				  char *buf, int buflen);
 
 void write_device_info_mmc(device_info *dev);
 void write_device_info_flash(device_info *dev);
@@ -126,6 +128,7 @@
 static unsigned page_mask = 0;
 static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
 static bool boot_into_ffbm;
+static char target_boot_params[64];
 
 /* Assuming unauthorized kernel image by default */
 static int auth_kernel_img = 0;
@@ -232,6 +235,7 @@
 #endif
 	int pause_at_bootup = 0;
 	bool gpt_exists = partition_gpt_exists();
+	int have_target_boot_params = 0;
 
 	if (cmdline && cmdline[0]) {
 		cmdline_len = strlen(cmdline);
@@ -267,6 +271,14 @@
 		cmdline_len += strlen(auth_kernel);
 	}
 
+	if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
+								 "system",
+				   target_boot_params,
+				   sizeof(target_boot_params)) == 0) {
+		have_target_boot_params = 1;
+		cmdline_len += strlen(target_boot_params);
+	}
+
 	/* Determine correct androidboot.baseband to use */
 	switch(target_baseband())
 	{
@@ -438,6 +450,12 @@
 			if (have_cmdline) --dst;
 			while ((*dst++ = *src++));
 		}
+
+		if (have_target_boot_params) {
+			if (have_cmdline) --dst;
+			src = target_boot_params;
+			while ((*dst++ = *src++));
+		}
 	}
 
 
diff --git a/platform/init.c b/platform/init.c
index 5001373..7efea46 100644
--- a/platform/init.c
+++ b/platform/init.c
@@ -105,3 +105,9 @@
 __WEAK void clock_config_cdc(uint8_t slot)
 {
 }
+
+__WEAK int get_target_boot_params(const char *cmdline, const char *part,
+				  char *buf, int buflen)
+{
+	return -1;
+}
diff --git a/target/mdmkrypton/init.c b/target/mdmkrypton/init.c
index d362633..89e17ad 100644
--- a/target/mdmkrypton/init.c
+++ b/target/mdmkrypton/init.c
@@ -223,3 +223,41 @@
 	return restart_reason;
 }
 
+int get_target_boot_params(const char *cmdline, const char *part, char *buf,
+			   int buflen)
+{
+	struct ptable *ptable;
+	int system_ptn_index = -1;
+
+	if (!cmdline || !part || !buf || buflen < 0) {
+		dprintf(CRITICAL, "WARN: Invalid input param\n");
+		return -1;
+	}
+
+	ptable = flash_get_ptable();
+	if (!ptable) {
+		dprintf(CRITICAL,
+			"WARN: Cannot get flash partition table\n");
+		return -1;
+	}
+
+	system_ptn_index = ptable_get_index(ptable, part);
+	if (system_ptn_index < 0) {
+		dprintf(CRITICAL,
+			"WARN: Cannot get partition index for %s\n", part);
+		return -1;
+	}
+
+	/*
+	 * check if cmdline contains "root=" at the beginning of buffer or
+	 * " root=" in the middle of buffer.
+	 */
+	if (((!strncmp(cmdline, "root=", strlen("root="))) ||
+	     (strstr(cmdline, " root="))))
+		dprintf(DEBUG, "DEBUG: cmdline has root=\n");
+	else
+		snprintf(buf, buflen, " root=/dev/mtdblock%d",
+			 system_ptn_index);
+
+	return 0;
+}