Merge "platform: msm_shared: Update kernel early mount dtsi node for UFS/EMMC"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index c873882..83a2c49 100755
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -185,7 +185,8 @@
 static const char *baseband_apq_nowgr   = " androidboot.baseband=baseband_apq_nowgr";
 static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
 static const char *skip_ramfs = " skip_initramfs";
-static char *sys_path_cmdline = " rootwait ro init=/init root=/dev/mmcblk0p%d"; /*This will be updated*/
+static const char *sys_path_cmdline = " rootwait ro init=/init";
+static const char *sys_path = "  root=/dev/mmcblk0p";
 
 #if VERIFIED_BOOT
 #if !VBOOT_MOTA
@@ -347,6 +348,11 @@
 	char *boot_dev_buf = NULL;
     	bool is_mdtp_activated = 0;
 	int current_active_slot = INVALID;
+	int system_ptn_index = -1;
+	unsigned int lun = 0;
+	char lun_char_base = 'a';
+	int syspath_buflen = strlen(sys_path) + sizeof(int) + 1; /*allocate buflen for largest possible string*/
+	char syspath_buf[syspath_buflen];
 
 #if USE_LE_SYSTEMD
 	is_systemd_present=true;
@@ -495,10 +501,22 @@
 		cmdline_len += (strlen(androidboot_slot_suffix)+
 					strlen(SUFFIX_SLOT(current_active_slot)));
 
-		snprintf(sys_path_cmdline, sizeof(*sys_path_cmdline),
-				sys_path_cmdline, (partition_get_index("system")+1));
-		cmdline_len += strlen(sys_path_cmdline);
+		system_ptn_index = partition_get_index("system");
+		if (platform_boot_dev_isemmc())
+		{
+			snprintf(syspath_buf, syspath_buflen, " root=/dev/mmcblk0p%d",
+				system_ptn_index + 1);
+		}
+		else
+		{
+			lun = partition_get_lun(system_ptn_index);
+			snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
+					lun_char_base + lun,
+					partition_get_index_in_lun("system", lun));
+		}
 
+		cmdline_len += strlen(sys_path_cmdline);
+		cmdline_len += strlen(syspath_buf);
 		if (!boot_into_recovery)
 			cmdline_len += strlen(skip_ramfs);
 	}
@@ -722,6 +740,10 @@
 				src = sys_path_cmdline;
 				--dst;
 				while ((*dst++ = *src++));
+
+				src = syspath_buf;
+				--dst;
+				while ((*dst++ = *src++));
 		}
 
 #if TARGET_CMDLINE_SUPPORT
@@ -3546,7 +3568,7 @@
 
 void cmd_set_active(const char *arg, void *data, unsigned sz)
 {
-	char *p = NULL;
+	char *p, *sp = NULL;
 	unsigned i,current_active_slot;
 	const char *current_slot_suffix;
 
@@ -3558,13 +3580,16 @@
 
 	if (arg)
 	{
-		p = (char *)arg;
-		if (p)
+		p = strtok_r((char *)arg, ":", &sp);
+		if (*p)
 		{
 			current_active_slot = partition_find_active_slot();
 
 			/* Check if trying to make curent slot active */
 			current_slot_suffix = SUFFIX_SLOT(current_active_slot);
+			current_slot_suffix = strtok_r((char *)current_slot_suffix,
+							(char *)suffix_delimiter, &sp);
+
 			if (!strncmp(p, current_slot_suffix, sizeof(current_slot_suffix)))
 			{
 				fastboot_okay("Slot already set active");
@@ -3575,6 +3600,8 @@
 				for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
 				{
 					current_slot_suffix = SUFFIX_SLOT(i);
+					current_slot_suffix = strtok_r((char *)current_slot_suffix,
+									(char *)suffix_delimiter, &sp);
 					if (!strncmp(p, current_slot_suffix, sizeof(current_slot_suffix)))
 					{
 						partition_switch_slots(current_active_slot, i);
@@ -4132,7 +4159,7 @@
 						{"oem disable-charger-screen", cmd_oem_disable_charger_screen},
 						{"oem off-mode-charge", cmd_oem_off_mode_charger},
 						{"oem select-display-panel", cmd_oem_select_display_panel},
-						{"oem set_active",cmd_set_active},
+						{"set_active",cmd_set_active},
 #if UNITTEST_FW_SUPPORT
 						{"oem run-tests", cmd_oem_runtests},
 #endif
diff --git a/lib/zlib_inflate/inflate.c b/lib/zlib_inflate/inflate.c
index e341300..95690e0 100644
--- a/lib/zlib_inflate/inflate.c
+++ b/lib/zlib_inflate/inflate.c
@@ -1506,7 +1506,7 @@
 {
     struct inflate_state FAR *state;
 
-    if (strm == Z_NULL || strm->state == Z_NULL) return -1L << 16;
+    if (strm == Z_NULL || strm->state == Z_NULL) return -(1L<<16);
     state = (struct inflate_state FAR *)strm->state;
     return ((long)(state->back) << 16) +
         (state->mode == COPY ? state->length :
diff --git a/platform/msm_shared/ab_partition_parser.c b/platform/msm_shared/ab_partition_parser.c
index bd900d9..c224e0f 100644
--- a/platform/msm_shared/ab_partition_parser.c
+++ b/platform/msm_shared/ab_partition_parser.c
@@ -31,12 +31,18 @@
 #include <crc32.h>
 #include <ab_partition_parser.h>
 #include <partition_parser.h>
+#include <boot_device.h>
+#if defined(MMC_SDHCI_SUPPORT) || defined(UFS_SUPPORT)
+#include <mmc_wrapper.h>
+#include <ufs.h>
+#endif
 
 //#define AB_DEBUG
 
 /* Slot suffix */
 const char *suffix_slot[] = {"_a",
 		"_b"};
+const char *suffix_delimiter = "_";
 
 /* local global variables */
 static signed active_slot = INVALID;		/* to store current active slot */
@@ -580,6 +586,11 @@
 	int ret = 0;
 	uint64_t max_gpt_size_bytes =
 		(PARTITION_ENTRY_SIZE*NUM_PARTITIONS + GPT_HEADER_BLOCKS*block_size);
+	int lun = -1;
+
+	/* Get Current LUN for UFS target */
+	if (!platform_boot_dev_isemmc())
+		lun = mmc_get_lun();
 
 	buffer = memalign(CACHE_LINE, ROUNDUP(max_gpt_size_bytes, CACHE_LINE));
 	if (!buffer)
@@ -604,6 +615,18 @@
 	tmp = gpt_entries_ptr;
 	for (i=0;i<partition_count;i++)
 	{
+		if (lun != -1)
+		{
+			/* Partition table is populated with entries from lun 0 to max lun.
+			* break out of the loop once we see the partition lun is > current lun */
+			if (partition_entries[i].lun > lun)
+				break;
+			/* Find the entry where the partition table for 'lun' starts
+			and then update the attributes */
+			if (partition_entries[i].lun != lun)
+				continue;
+		}
+
 		/* Update the partition attributes */
 		PUT_LONG_LONG(&tmp[ATTRIBUTE_FLAG_OFFSET],
 			partition_entries[i].attribute_flag);
@@ -657,32 +680,47 @@
 	unsigned max_entries_size_bytes = PARTITION_ENTRY_SIZE*NUM_PARTITIONS;
 	unsigned max_entries_blocks = max_entries_size_bytes/block_size;
 	unsigned max_gpt_blocks = GPT_HEADER_BLOCKS + max_entries_blocks;
+	int max_luns = 0, lun;
+	int cur_lun = mmc_get_lun();
 
-	/* Update Primary GPT */
-	offset = 0x01;	/*  offset is 0x1 for primary GPT */
-	gpt_start_addr = offset*block_size;
-	/* Take gpt_start_addr as start and calculate offset from that in block sz*/
-	gpt_hdr_offset = 0; /* For primary partition offset is zero */
-	gpt_entries_offset = GPT_HEADER_BLOCKS;
-
-	ret = update_gpt(gpt_start_addr, gpt_hdr_offset, gpt_entries_offset);
-	if (ret)
+#if defined(MMC_SDHCI_SUPPORT) || defined(UFS_SUPPORT)
+	if (platform_boot_dev_isemmc())
+		max_luns = 1;
+	else
+		max_luns = ufs_get_num_of_luns((struct ufs_dev*)target_mmc_device());
+#endif
+	for (lun = 0; lun < max_luns; lun++)
 	{
-		dprintf(CRITICAL, "Failed to update Primary GPT\n");
-		return;
-	}
+		/* Set current LUN */
+		mmc_set_lun(lun);
 
-	/* Update Secondary GPT */
-	offset = ((mmc_get_device_capacity()/block_size) - max_gpt_blocks);
-	gpt_start_addr = offset*block_size;
-	gpt_hdr_offset = max_entries_blocks;
-	gpt_entries_offset = 0; /* For secondary GPT entries offset is zero */
+		/* Update Primary GPT */
+		offset = 0x01;	/*  offset is 0x1 for primary GPT */
+		gpt_start_addr = offset*block_size;
+		/* Take gpt_start_addr as start and calculate offset from that in block sz*/
+		gpt_hdr_offset = 0; /* For primary partition offset is zero */
+		gpt_entries_offset = GPT_HEADER_BLOCKS;
 
-	ret = update_gpt(gpt_start_addr, gpt_hdr_offset, gpt_entries_offset);
-	if (ret)
-	{
-		dprintf(CRITICAL, "Failed to update Secondary GPT\n");
-		return;
+		ret = update_gpt(gpt_start_addr, gpt_hdr_offset, gpt_entries_offset);
+		if (ret)
+		{
+			dprintf(CRITICAL, "Failed to update Primary GPT\n");
+			return;
+		}
+
+		/* Update Secondary GPT */
+		offset = ((mmc_get_device_capacity()/block_size) - max_gpt_blocks);
+		gpt_start_addr = offset*block_size;
+		gpt_hdr_offset = max_entries_blocks;
+		gpt_entries_offset = 0; /* For secondary GPT entries offset is zero */
+
+		ret = update_gpt(gpt_start_addr, gpt_hdr_offset, gpt_entries_offset);
+		if (ret)
+		{
+			dprintf(CRITICAL, "Failed to update Secondary GPT\n");
+			return;
+		}
 	}
+	mmc_set_lun(cur_lun);
 	return;
 }
diff --git a/platform/msm_shared/include/ab_partition_parser.h b/platform/msm_shared/include/ab_partition_parser.h
index 2bbddb8..0d49441 100644
--- a/platform/msm_shared/include/ab_partition_parser.h
+++ b/platform/msm_shared/include/ab_partition_parser.h
@@ -29,6 +29,7 @@
 #include <fastboot.h>
 
 extern const char *suffix_slot[];
+extern const char *suffix_delimiter;
 
 #define SUFFIX_SLOT(part_slot) suffix_slot[(part_slot)]
 
diff --git a/target/msm8953/meminfo.c b/target/msm8953/meminfo.c
index 23e82fd..3968185 100644
--- a/target/msm8953/meminfo.c
+++ b/target/msm8953/meminfo.c
@@ -81,5 +81,5 @@
 
 unsigned target_get_max_flash_size(void)
 {
-	return (512 * 1024 * 1024);
+	return (510 * 1024 * 1024);
 }