BootControl: blow AR on boot complete

Bug: 232277507
Signed-off-by: Jidong Sun <jidong@google.com>
Change-Id: I684bc259407b3cbaa5dda9fdc16158d7bd4b4e02
diff --git a/interfaces/boot/1.2/BootControl.cpp b/interfaces/boot/1.2/BootControl.cpp
index a28be05..6c480bb 100644
--- a/interfaces/boot/1.2/BootControl.cpp
+++ b/interfaces/boot/1.2/BootControl.cpp
@@ -49,6 +49,8 @@
 #define BOOT_B_PATH     "/dev/block/by-name/boot_b"
 #define DEVINFO_PATH    "/dev/block/by-name/devinfo"
 
+#define BLOW_AR_PATH    "/sys/kernel/boot_control/blow_ar"
+
 // slot flags
 #define AB_ATTR_PRIORITY_SHIFT      52
 #define AB_ATTR_PRIORITY_MASK       (3UL << AB_ATTR_PRIORITY_SHIFT)
@@ -174,6 +176,11 @@
     slot_data.fastboot_ok = 0;
 }
 
+static bool blowAR() {
+    android::base::unique_fd fd(open(BLOW_AR_PATH, O_WRONLY | O_DSYNC));
+    return android::base::WriteStringToFd("1", fd);
+}
+
 }  // namespace
 
 // Methods from ::android::hardware::boot::V1_0::IBootControl follow.
@@ -211,7 +218,17 @@
         ret = setSlotFlag(getCurrentSlot(), AB_ATTR_SUCCESSFUL);
     }
 
-    !ret ? _hidl_cb({false, "Failed to set successful flag"}) : _hidl_cb({true, ""});
+    if (!ret) {
+        _hidl_cb({false, "Failed to set successful flag"});
+        return Void();
+    }
+
+    if (!blowAR()) {
+        ALOGE("Failed to blow anti-rollback counter");
+        // Ignore the error, since ABL will re-trigger it on reboot
+    }
+
+    _hidl_cb({true, ""});
     return Void();
 }