Merge "Format formattable partitions if mount fails" am: cb76857147
am: 2f5cfdbb9d

Change-Id: I7c0848776028caf766f33895a010349ccabf13bf
diff --git a/roots.cpp b/roots.cpp
index 2fec9ae..4936947 100644
--- a/roots.cpp
+++ b/roots.cpp
@@ -102,7 +102,20 @@
     if (strcmp(v->fs_type, "ext4") == 0 ||
                strcmp(v->fs_type, "squashfs") == 0 ||
                strcmp(v->fs_type, "vfat") == 0) {
-        if (mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options) == -1) {
+        int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
+        if (result == -1 && fs_mgr_is_formattable(v)) {
+            LOGE("failed to mount %s (%s), formatting ...\n",
+                    mount_point, strerror(errno));
+            bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
+            if (fs_mgr_do_format(v, crypt_footer) == 0) {
+                result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
+            } else {
+                LOGE("failed to format %s (%s)\n", mount_point, strerror(errno));
+                return -1;
+            }
+        }
+
+        if (result == -1) {
             LOGE("failed to mount %s (%s)\n", mount_point, strerror(errno));
             return -1;
         }