Add adb enable-verity

Note that it is *easy* to break your phone with this feature. It is
not a bug that reenabling verity after changing one byte of the system
partition stops the device booting.

(cherry-pick of 7c442e1700e6312727283db402dec6f666f1b55a.)

Bug: 18529433
Change-Id: I632e91281884471a362960f1ba30312d2669b8ff
diff --git a/adb/remount_service.c b/adb/remount_service.c
index 36367a7..05d3169 100644
--- a/adb/remount_service.c
+++ b/adb/remount_service.c
@@ -79,29 +79,57 @@
     return false;
 }
 
+static int make_block_device_writable(const char* dir)
+{
+    char *dev = 0;
+    int fd = -1;
+    int OFF = 0;
+    int rc = -1;
+
+    dev = find_mount(dir);
+    if (!dev)
+        goto errout;
+
+    fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
+    if (fd < 0)
+        goto errout;
+
+    if (ioctl(fd, BLKROSET, &OFF)) {
+        goto errout;
+    }
+
+    rc = 0;
+
+errout:
+    if (fd >= 0) {
+        adb_close(fd);
+    }
+
+    if (dev) {
+        free(dev);
+    }
+    return rc;
+}
+
 /* Init mounts /system as read only, remount to enable writes. */
 static int remount(const char* dir, int* dir_ro)
 {
     char *dev;
-    int fd;
     int OFF = 0;
 
     if (dir_ro == 0) {
         return 0;
     }
 
+    if (make_block_device_writable(dir)) {
+        return -1;
+    }
+
     dev = find_mount(dir);
 
     if (!dev)
         return -1;
 
-    fd = unix_open(dev, O_RDONLY | O_CLOEXEC);
-    if (fd < 0)
-        return -1;
-
-    ioctl(fd, BLKROSET, &OFF);
-    adb_close(fd);
-
     *dir_ro = mount(dev, dir, "none", MS_REMOUNT, NULL);
 
     free(dev);
@@ -114,6 +142,28 @@
     writex(fd, str, strlen(str));
 }
 
+int make_system_and_vendor_block_devices_writable(int fd)
+{
+    char buffer[200];
+    if (make_block_device_writable("/system")) {
+        snprintf(buffer, sizeof(buffer),
+                 "Failed to make system block device writable %s\n",
+                 strerror(errno));
+        write_string(fd, buffer);
+        return -1;
+    }
+
+    if (hasVendorPartition() && make_block_device_writable("/vendor")) {
+        snprintf(buffer, sizeof(buffer),
+                 "Failed to make vendor block device writable: %s\n",
+                 strerror(errno));
+        write_string(fd, buffer);
+        return -1;
+    }
+
+    return 0;
+}
+
 void remount_service(int fd, void *cookie)
 {
     char buffer[200];
@@ -167,4 +217,3 @@
 
     adb_close(fd);
 }
-