Add support for Virtual A/B

For Virtual A/B OTA feature, change to
  * add intial support for feature.
  * parse misc partition for virtual a/b merge status.
  * publish snapshot merge status in fastboot variables.

Change-Id: I454972cc2be673054c50e5de6de5b3abafbdf2fd
Signed-off-by: Rahul Shahare <rshaha@codeaurora.org>
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
index b66ea51..4644514 100644
--- a/AndroidBoot.mk
+++ b/AndroidBoot.mk
@@ -65,6 +65,12 @@
   DYNAMIC_PARTITION_SUPPORT := DYNAMIC_PARTITION_SUPPORT=0
 endif
 
+ifeq ($(PRODUCT_VIRTUAL_AB_OTA),true)
+  VIRTUAL_AB_OTA := VIRTUAL_AB_OTA=1
+else
+  VIRTUAL_AB_OTA := VIRTUAL_AB_OTA=0
+endif
+
 ifeq ($(BOARD_DTBO_NOT_SUPPORTED),true)
   TARGET_DTBO_NOT_SUPPORTED := TARGET_DTBO_NOT_SUPPORTED=1
 else
@@ -141,7 +147,7 @@
 # ELF binary for ABOOT
 TARGET_ABOOT_ELF := $(PRODUCT_OUT)/aboot.elf
 $(TARGET_ABOOT_ELF): $(ABOOT_CLEAN) | $(ABOOT_OUT)
-	$(MAKE) -C $(LK_PATH) TOOLCHAIN_PREFIX=$(CROSS_COMPILE) BOOTLOADER_OUT=$(CROOT_DIR)/$(ABOOT_OUT) $(BOOTLOADER_PLATFORM) $(EMMC_BOOT) $(SIGNED_KERNEL) $(VERIFIED_BOOT) $(VERIFIED_BOOT_2) $(VB1_KEY_USED) $(TARGET_DTBO_NOT_SUPPORTED) $(ENABLE_DISPLAY) $(ENABLE_KASLRSEED) $(ENABLE_BOOTDEVICE_MOUNT) $(DEVICE_STATUS) $(BUILD_VARIANT) $(BOARD_NAME) $(ENABLE_VB_ATTEST) $(OSVERSION_IN_BOOTIMAGE) $(QSEECOM_SECAPP_REGION_2MB) $(TARGET_USE_SYSTEM_AS_ROOT_IMAGE) $(DYNAMIC_PARTITION_SUPPORT)
+	$(MAKE) -C $(LK_PATH) TOOLCHAIN_PREFIX=$(CROSS_COMPILE) BOOTLOADER_OUT=$(CROOT_DIR)/$(ABOOT_OUT) $(BOOTLOADER_PLATFORM) $(EMMC_BOOT) $(SIGNED_KERNEL) $(VERIFIED_BOOT) $(VERIFIED_BOOT_2) $(VB1_KEY_USED) $(TARGET_DTBO_NOT_SUPPORTED) $(ENABLE_DISPLAY) $(ENABLE_KASLRSEED) $(ENABLE_BOOTDEVICE_MOUNT) $(DEVICE_STATUS) $(BUILD_VARIANT) $(BOARD_NAME) $(ENABLE_VB_ATTEST) $(OSVERSION_IN_BOOTIMAGE) $(QSEECOM_SECAPP_REGION_2MB) $(TARGET_USE_SYSTEM_AS_ROOT_IMAGE) $(DYNAMIC_PARTITION_SUPPORT) $(VIRTUAL_AB_OTA)
 
 # NAND variant output
 TARGET_NAND_BOOTLOADER := $(PRODUCT_OUT)/appsboot.mbn
@@ -175,7 +181,7 @@
 
 # Top level for eMMC variant targets
 $(TARGET_EMMC_BOOTLOADER): $(emmc_appsbootldr_clean) | $(EMMC_BOOTLOADER_OUT) $(INSTALLED_KEYSTOREIMAGE_TARGET)
-	$(MAKE) -C $(LK_PATH) TOOLCHAIN_PREFIX=$(CROSS_COMPILE) BOOTLOADER_OUT=$(CROOT_DIR)/$(EMMC_BOOTLOADER_OUT) $(BOOTLOADER_PLATFORM) EMMC_BOOT=1 $(SIGNED_KERNEL) $(VERIFIED_BOOT) $(VERIFIED_BOOT_2) $(VB1_KEY_USED) $(TARGET_DTBO_NOT_SUPPORTED) $(ENABLE_DISPLAY) $(ENABLE_KASLRSEED) $(ENABLE_BOOTDEVICE_MOUNT) $(DEVICE_STATUS) $(BUILD_VARIANT) $(BOARD_NAME) $(ENABLE_VB_ATTEST) $(OSVERSION_IN_BOOTIMAGE) $(ENABLE_BG_SUPPORT) $(QSEECOM_SECAPP_REGION_2MB) $(TARGET_USE_SYSTEM_AS_ROOT_IMAGE) $(DYNAMIC_PARTITION_SUPPORT)
+	$(MAKE) -C $(LK_PATH) TOOLCHAIN_PREFIX=$(CROSS_COMPILE) BOOTLOADER_OUT=$(CROOT_DIR)/$(EMMC_BOOTLOADER_OUT) $(BOOTLOADER_PLATFORM) EMMC_BOOT=1 $(SIGNED_KERNEL) $(VERIFIED_BOOT) $(VERIFIED_BOOT_2) $(VB1_KEY_USED) $(TARGET_DTBO_NOT_SUPPORTED) $(ENABLE_DISPLAY) $(ENABLE_KASLRSEED) $(ENABLE_BOOTDEVICE_MOUNT) $(DEVICE_STATUS) $(BUILD_VARIANT) $(BOARD_NAME) $(ENABLE_VB_ATTEST) $(OSVERSION_IN_BOOTIMAGE) $(ENABLE_BG_SUPPORT) $(QSEECOM_SECAPP_REGION_2MB) $(TARGET_USE_SYSTEM_AS_ROOT_IMAGE) $(DYNAMIC_PARTITION_SUPPORT) $(VIRTUAL_AB_OTA)
 	$(hide) rm -f $(emmc_appsbootldr_clean)
 
 # Keep build NAND & eMMC as default for targets still using TARGET_BOOTLOADER
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 690e606..cc7a90b 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -2,7 +2,7 @@
  * Copyright (c) 2009, Google Inc.
  * All rights reserved.
  *
- * Copyright (c) 2009-2020, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2009-2021, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -285,6 +285,13 @@
 	"devinfo",
 	"partition"};
 
+static const char *VabSnapshotMergeStatus[] = {
+	"none",
+	"unknown",
+	"snapshotted",
+	"merging",
+	"cancelled"};
+
 struct atag_ptbl_entry
 {
 	char name[16];
@@ -340,6 +347,7 @@
 char panel_display_mode[MAX_RSP_SIZE];
 char soc_version_str[MAX_RSP_SIZE];
 char block_size_string[MAX_RSP_SIZE];
+static char SnapshotMergeState[MAX_RSP_SIZE];
 #if PRODUCT_IOT
 
 /* For IOT we are using custom version */
@@ -5013,6 +5021,7 @@
 {
 	int i;
 	char hw_platform_buf[MAX_RSP_SIZE];
+	VirtualAbMergeStatus SnapshotMergeStatus;
 
 	struct fastboot_cmd_desc cmd_list[] = {
 						/* By default the enabled list is empty. */
@@ -5133,6 +5142,27 @@
 #endif
         if (target_dynamic_partition_supported())
 		fastboot_publish("is-userspace", "no");
+
+	if (target_virtual_ab_supported()) {
+		SnapshotMergeStatus = GetSnapshotMergeStatus ();
+
+		switch (SnapshotMergeStatus) {
+			case SNAPSHOTTED:
+				SnapshotMergeStatus = SNAPSHOTTED;
+				break;
+			case MERGING:
+				SnapshotMergeStatus = MERGING;
+				break;
+			default:
+				SnapshotMergeStatus = NONE_MERGE_STATUS;
+				break;
+		}
+
+		snprintf(SnapshotMergeState,
+				strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1,
+				"%s", VabSnapshotMergeStatus[SnapshotMergeStatus]);
+		fastboot_publish("snapshot-update-state", SnapshotMergeState);
+	}
 }
 
 void aboot_init(const struct app_descriptor *app)
diff --git a/app/aboot/recovery.c b/app/aboot/recovery.c
index 389b91d..a79f670 100644
--- a/app/aboot/recovery.c
+++ b/app/aboot/recovery.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2017,2019 The Linux Foundation. All rights reserved.
+/* Copyright (c) 2010-2017,2019,2021 The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -617,6 +617,44 @@
 	return 0;
 }
 
+static MiscVirtualABMessage *VirtualAbMsg = NULL;
+
+VirtualAbMergeStatus GetSnapshotMergeStatus (void)
+{
+	VirtualAbMergeStatus MergeStatus = NONE_MERGE_STATUS;
+	uint32_t pagesize = get_page_size();
+
+	if (target_is_emmc_boot())
+	{
+	    if (VirtualAbMsg == NULL) {
+	        if(read_misc(MISC_VIRTUALAB_OFFSET, (void *)&VirtualAbMsg,
+					pagesize))
+		{
+			dprintf(CRITICAL,"Error reading virtualab msg from misc partition\n");
+			return MergeStatus;
+		}
+
+		if (VirtualAbMsg->Magic != MISC_VIRTUAL_AB_MAGIC_HEADER ||
+			VirtualAbMsg->Version != MISC_VIRTUAL_AB_MESSAGE_VERSION) {
+
+			dprintf(CRITICAL,"Error read virtualab msg version:%u magic:%u not valid\n",
+					VirtualAbMsg->Version,VirtualAbMsg->Magic);
+
+			free(VirtualAbMsg);
+			VirtualAbMsg = NULL;
+		}
+		else
+		{
+			dprintf(CRITICAL,"read virtualab MergeStatus:%x\n", VirtualAbMsg->MergeStatus);
+		}
+	    }
+
+	    if (VirtualAbMsg)
+	        MergeStatus = VirtualAbMsg->MergeStatus;
+	}
+	return MergeStatus;
+}
+
 int get_ffbm(char *ffbm, unsigned size)
 {
 	const char *ffbm_cmd = "ffbm-";
diff --git a/app/aboot/recovery.h b/app/aboot/recovery.h
index da7d400..81ceb32 100644
--- a/app/aboot/recovery.h
+++ b/app/aboot/recovery.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2010-2017,2019 The Linux Foundation. All rights reserved.
+/* Copyright (c) 2010-2017,2019,2021 The Linux Foundation. All rights reserved.
 
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -56,6 +56,34 @@
 	char recovery[1024];
 };
 
+#define MISC_VIRTUAL_AB_MESSAGE_VERSION 2
+#define MISC_VIRTUAL_AB_MAGIC_HEADER 0x56740AB0
+
+/** MISC Partition usage as per AOSP implementation.
+  * 0   - 2K     For bootloader_message
+  * 2K  - 16K    Used by Vendor's bootloader (the 2K - 4K range may be
+  *              optionally used as bootloader_message_ab struct)
+  * 16K - 32K    Used by uncrypt and recovery to store wipe_package
+  *              for A/B devices
+  * 32K - 64K    System space, used for miscellanious AOSP features.
+  **/
+#define MISC_VIRTUALAB_OFFSET (32 * 1024)
+
+typedef enum VirtualAbMergeStatus{
+  NONE_MERGE_STATUS,
+  UNKNOWN_MERGE_STATUS,
+  SNAPSHOTTED,
+  MERGING,
+  CANCELLED
+} VirtualAbMergeStatus;
+
+typedef struct {
+  uint8_t Version;
+  uint32_t Magic;
+  uint8_t MergeStatus;  // IBootControl 1.1, MergeStatus enum.
+  uint8_t SourceStatus;   // Slot number when merge_status was written.
+  uint8_t Reserved[57];
+} __attribute__ ((packed)) MiscVirtualABMessage;
 
 struct update_header {
 	unsigned char MAGIC[UPDATE_MAGIC_SIZE];
@@ -91,7 +119,7 @@
  * invalid.
  */
 int get_ffbm(char *ffbm, unsigned size);
-
+VirtualAbMergeStatus GetSnapshotMergeStatus(void);
 extern unsigned boot_into_recovery;
 
 #endif
diff --git a/include/target.h b/include/target.h
index d4b2416..6886bdf 100644
--- a/include/target.h
+++ b/include/target.h
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2008 Travis Geiselbrecht
  *
- * Copyright (c) 2013-2017,2019 The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2017,2019,2021 The Linux Foundation. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files
@@ -105,6 +105,7 @@
 bool is_display_disabled(void);
 bool target_uses_system_as_root(void);
 bool target_dynamic_partition_supported(void);
+bool target_virtual_ab_supported(void);
 struct qmp_reg *target_get_qmp_settings();
 int target_get_qmp_regsize();
 uint32_t target_ddr_cfg_reg();
diff --git a/target/init.c b/target/init.c
index d7b923f..43d5327 100755
--- a/target/init.c
+++ b/target/init.c
@@ -1,7 +1,7 @@
 /*
  * Copyright (c) 2008 Travis Geiselbrecht
  *
- * Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
  * a copy of this software and associated documentation files
@@ -298,6 +298,18 @@
 #endif
 }
 
+#if VIRTUAL_AB_OTA
+bool target_virtual_ab_supported(void)
+{
+	return true;
+}
+#else
+bool target_virtual_ab_supported(void)
+{
+	return false;
+}
+#endif
+
 /* Default CFG register value */
 uint32_t target_ddr_cfg_reg()
 {