Merge "app: aboot: add upload command support to fastboot"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 0545788..834fe07 100755
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -1192,6 +1192,7 @@
 	unsigned int kernel_size = 0;
 	unsigned int patched_kernel_hdr_size = 0;
 	int rc;
+	char *ptn_name = NULL;
 #if DEVICE_TREE
 	struct dt_table *table;
 	struct dt_entry dt_entry;
@@ -1223,22 +1224,22 @@
 		hdr = uhdr;
 		goto unified_boot;
 	}
-	if (!boot_into_recovery) {
-		index = partition_get_index("boot");
-		ptn = partition_get_offset(index);
-		if(ptn == 0) {
-			dprintf(CRITICAL, "ERROR: No boot partition found\n");
-                    return -1;
-		}
+
+	/* For a/b recovery image code is on boot partition.
+	   If we support multislot, always use boot partition. */
+	if (boot_into_recovery &&
+		(!partition_multislot_is_supported()))
+			ptn_name = "recovery";
+	else
+			ptn_name = "boot";
+
+	index = partition_get_index(ptn_name);
+	ptn = partition_get_offset(index);
+	if(ptn == 0) {
+		dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
+		return -1;
 	}
-	else {
-		index = partition_get_index("recovery");
-		ptn = partition_get_offset(index);
-		if(ptn == 0) {
-			dprintf(CRITICAL, "ERROR: No recovery partition found\n");
-                    return -1;
-		}
-	}
+
 	/* Set Lun for boot & recovery partitions */
 	mmc_set_lun(partition_get_lun(index));
 
diff --git a/platform/msm_shared/ab_partition_parser.c b/platform/msm_shared/ab_partition_parser.c
index 9e64ec1..5f3a384 100644
--- a/platform/msm_shared/ab_partition_parser.c
+++ b/platform/msm_shared/ab_partition_parser.c
@@ -239,9 +239,12 @@
 	unsigned boot_priority;
 	struct partition_entry *partition_entries = partition_get_partition_entries();
 
+#ifdef AB_DEBUG
+	dprintf(INFO, "partition_find_active_slot() called\n");
+#endif
 	/* Return current active slot if already found */
 	if (active_slot != INVALID)
-		return active_slot;
+		goto out;
 
 	for (boot_priority = MAX_PRIORITY;
 			boot_priority > 0; boot_priority--)
@@ -277,7 +280,8 @@
 #ifdef AB_DEBUG
 	dprintf(INFO, "Slot (%s) is Valid High Priority Slot\n", SUFFIX_SLOT(i));
 #endif
-					return i;
+					active_slot = i;
+					goto out;
 				}
 			}
 		}
@@ -293,13 +297,20 @@
 							PART_ATT_MAX_RETRY_COUNT_VAL) &
 							(~PART_ATT_SUCCESSFUL_VAL &
 							~PART_ATT_UNBOOTABLE_VAL));
+
+			active_slot = SLOT_A;
+			/* This is required to mark all bits as active,
+			for fresh boot post fresh flash */
+			partition_mark_active_slot(active_slot);
+
 			if (!attributes_updated)
 				attributes_updated = true;
-			return SLOT_A;
+
+			goto out;
 		}
 	}
-	/* If no valid slot */
-	return INVALID;
+out:
+	return active_slot;
 }
 
 static int
@@ -384,6 +395,11 @@
 {
 	unsigned char tmp_guid[PARTITION_TYPE_GUID_SIZE];
 
+#ifdef AB_DEBUG
+	dprintf(INFO, "Swapping GUID (%s) --> (%s) \n",
+			partition_entries[old_index].name,
+			partition_entries[new_index].name);
+#endif
 	memcpy(tmp_guid, partition_entries[old_index].type_guid,
 				PARTITION_TYPE_GUID_SIZE);
 	memcpy(partition_entries[old_index].type_guid,
@@ -487,22 +503,19 @@
 	if (active_slot == slot)
 		goto out;
 
-	switch (active_slot)
+	if(slot != INVALID)
 	{
-		case INVALID:
-			if (slot != SLOT_A)
-				swap_guid(SLOT_A, slot);
-		default:
-			if (slot == INVALID)
-				swap_guid(active_slot, SLOT_A);
-			else
-				swap_guid(active_slot, slot);
+		dprintf(INFO, "Marking (%s) as active\n", SUFFIX_SLOT(slot));
+
+		/* 1. Swap GUID's to new slot */
+		swap_guid(active_slot, slot);
+
+		/* 2. Set Active bit for all partitions of active slot */
+		mark_all_partitions_active(slot);
 	}
+
 	active_slot = slot;
 out:
-	/* Set Active bit for all partitions of active slot */
-	mark_all_partitions_active(slot);
-
 	if (attributes_updated)
 		attributes_update();