Merge 5284f9b2e31c1a807f8d8035de593f52597e7ea7 on remote branch

Change-Id: I11dcfe24560fa2f4d29802ca3a852e7198b736d9
diff --git a/1.1/libboot_control_qti/libboot_control_qti.cpp b/1.1/libboot_control_qti/libboot_control_qti.cpp
index d6f5d8b..258e5fd 100644
--- a/1.1/libboot_control_qti/libboot_control_qti.cpp
+++ b/1.1/libboot_control_qti/libboot_control_qti.cpp
@@ -27,6 +27,7 @@
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+//#define LOG_NDEBUG 0
 #define LOG_TAG "bootcontrolhal"
 
 #include <libboot_control_qti.h>
@@ -47,6 +48,7 @@
 #include <limits.h>
 #include <cutils/properties.h>
 #include <gpt-utils.h>
+#include <bootloader_message/bootloader_message.h>
 #include <libboot_control/libboot_control.h>
 
 #define BOOTDEV_DIR "/dev/block/bootdevice/by-name"
@@ -425,13 +427,13 @@
 	char platform[256];
 	property_get(BOARD_PLATFORM_PROP , platform, "");
 	if (!strncmp(platform, GVMQ_PLATFORM, strlen(GVMQ_PLATFORM)))
-		mPlatform = true;
+		mGvmqPlatform = true;
 	return InitMiscVirtualAbMessageIfNeeded();
 }
 
 unsigned get_number_slots()
 {
-	if(mPlatform)
+	if (mGvmqPlatform)
 		return 2;
 
 	struct dirent *de = NULL;
@@ -490,8 +492,35 @@
 	return 0;
 }
 
-int mark_boot_successful()
-{
+int mark_boot_successful(){
+	if (mGvmqPlatform) {
+		std::string err;
+		std::string misc_blk_device = get_bootloader_message_blk_device(&err);
+		if (misc_blk_device.empty()) {
+			ALOGE("Could not find bootloader message block device: %s", err.c_str());
+			return -1;
+		}
+		bootloader_message boot;
+		if (!read_bootloader_message_from(&boot, misc_blk_device, &err)) {
+			ALOGE(" Failed to read from %s due to %s ", misc_blk_device.c_str(), err.c_str());
+			return -1;
+		}
+		ALOGV(" bootloader_message is : boot.reserved[0] = %c, boot.reserved[1] = %c",
+					boot.reserved[0], boot.reserved[1]);
+		boot.reserved[2] = 'y';
+		if (!write_bootloader_message_to(boot, misc_blk_device, &err)) {
+			ALOGE("Failed to write to %s  because : %s", misc_blk_device.c_str(), err.c_str());
+			return -1;
+		}
+		bootloader_message boot_verify;
+		if (!read_bootloader_message_from(&boot_verify, misc_blk_device, &err)) {
+			ALOGE("Failed to read from %s due to %s ", misc_blk_device.c_str(), err.c_str());
+			return -1;
+		}
+		ALOGV(" bootloader_message : boot_verify.reserved[0] = %c, boot_verify.reserved[1] = %c,boot_verify.reserved[2] = %c",
+				boot_verify.reserved[0],boot_verify.reserved[1], boot_verify.reserved[2]);
+	}
+
 	unsigned cur_slot = 0;
 	cur_slot = get_current_slot();
 	if (update_slot_attribute(slot_suffix_arr[cur_slot],
@@ -506,6 +535,41 @@
 
 int set_active_boot_slot(unsigned slot)
 {
+	if (mGvmqPlatform) {
+		std::string err;
+		std::string misc_blk_device = get_bootloader_message_blk_device(&err);
+		if (misc_blk_device.empty()) {
+			ALOGE("Could not find bootloader message block device: %s", err.c_str());
+			return -1;
+		}
+		unsigned current_slot = get_current_slot();
+		uint32_t num_slots = get_number_slots();
+		if ((num_slots < 1) || (current_slot > num_slots - 1)) {
+			ALOGE("Invalid slot number");
+			return -1;
+		}
+		bootloader_message boot;
+		if(current_slot == 0)
+			boot.reserved[0] = 'a';
+		else
+			boot.reserved[0] = 'b';
+		if(slot == 0)
+			boot.reserved[1] = 'a';
+		else
+			boot.reserved[1] = 'b';
+		boot.reserved[2] = '\0';
+		if (!write_bootloader_message_to(boot, misc_blk_device, &err)) {
+			ALOGE("Failed to write to %s  because : %s", misc_blk_device.c_str(), err.c_str());
+			return -1;
+		}
+		bootloader_message boot_verify;
+		if (!read_bootloader_message_from(&boot_verify, misc_blk_device, &err)) {
+			ALOGE("Failed to read from %s due to %s ", misc_blk_device.c_str(), err.c_str());
+			return -1;
+		}
+		ALOGV("bootloader_message is : boot_verify.reserved[0] = %c, boot_verify.reserved[1] = %c,boot_verify.reserved[2] = %c",
+			boot_verify.reserved[0],boot_verify.reserved[1], boot_verify.reserved[2]);
+	}
 	map<string, vector<string>> ptn_map;
 	vector<string> ptn_vec;
 	const char ptn_list[][MAX_GPT_NAME_SIZE] = { AB_PTN_LIST };
@@ -599,7 +663,6 @@
 	ALOGE("%s: Failed to mark slot unbootable", __func__);
 	return -1;
 }
-
 int is_slot_bootable(unsigned slot)
 {
 	int attr = 0;
diff --git a/1.1/libboot_control_qti/libboot_control_qti.h b/1.1/libboot_control_qti/libboot_control_qti.h
index 2b1d18d..c669a0e 100644
--- a/1.1/libboot_control_qti/libboot_control_qti.h
+++ b/1.1/libboot_control_qti/libboot_control_qti.h
@@ -49,4 +49,4 @@
 bool set_snapshot_merge_status(MergeStatus status);
 MergeStatus get_snapshot_merge_status();
 
-bool mPlatform = false;
+bool mGvmqPlatform = false;