DO NOT MERGE

fastboot: Change -w to format after the erase of userdata & cache

If the bootloader doesn't support formatting of those partitions
(either because it doesn't support the getvar commands needed or
the partition type is not supported), the errors are printed but
doesn't halt processing of subsequent commands.

Change-Id: I816ac2e5e7593846fcb4fd39c793a8dbdd996f6f
Signed-off-by: Mike J. Chen <mjchen@google.com>
diff --git a/fastboot/engine.c b/fastboot/engine.c
index e40db2b..345577d 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -187,6 +187,8 @@
     struct stat st;
 
     fd = fileno(tmpfile());
+    /* reset ext4fs info so we can be called multiple times */
+    reset_ext4fs_info();
     info.len = image->partition_size;
     make_ext4fs_internal(fd, NULL, NULL, 0, 0, 1, 0, 0, 0);
 
@@ -202,7 +204,7 @@
     close(fd);
 }
 
-int fb_format(Action *a, usb_handle *usb)
+int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
 {
     const char *partition = a->cmd;
     char query[256];
@@ -218,6 +220,13 @@
     snprintf(query, sizeof(query), "getvar:partition-type:%s", partition);
     status = fb_command_response(usb, query, response);
     if (status) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr,
+                    "Can't determine partition type.\n");
+            return 0;
+        }
         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
         return status;
     }
@@ -229,6 +238,13 @@
         }
     }
     if (!generator) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr,
+                    "File system type %s not supported.\n", response);
+            return 0;
+        }
         fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
                 response);
         return -1;
@@ -238,6 +254,12 @@
     snprintf(query, sizeof(query), "getvar:partition-size:%s", partition);
     status = fb_command_response(usb, query, response);
     if (status) {
+        if (skip_if_not_supported) {
+            fprintf(stderr,
+                    "Erase successful, but not automatically formatting.\n");
+            fprintf(stderr, "Unable to get partition size\n.");
+            return 0;
+        }
         fprintf(stderr,"FAILED (%s)\n", fb_get_error());
         return status;
     }
@@ -266,11 +288,12 @@
     return status;
 }
 
-void fb_queue_format(const char *partition)
+void fb_queue_format(const char *partition, int skip_if_not_supported)
 {
     Action *a;
 
     a = queue_action(OP_FORMAT, partition);
+    a->data = (void*)skip_if_not_supported;
     a->msg = mkmsg("formatting '%s' partition", partition);
 }
 
@@ -482,7 +505,7 @@
         } else if (a->op == OP_NOTICE) {
             fprintf(stderr,"%s\n",(char*)a->data);
         } else if (a->op == OP_FORMAT) {
-            status = fb_format(a, usb);
+            status = fb_format(a, usb, (int)a->data);
             status = a->func(a, status, status ? fb_get_error() : "");
             if (status) break;
         } else {
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index fad70d4..805014f 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -636,7 +636,7 @@
             skip(2);
         } else if(!strcmp(*argv, "format")) {
             require(2);
-            fb_queue_format(argv[1]);
+            fb_queue_format(argv[1], 0);
             skip(2);
         } else if(!strcmp(*argv, "signature")) {
             require(2);
@@ -723,7 +723,9 @@
 
     if (wants_wipe) {
         fb_queue_erase("userdata");
+        fb_queue_format("userdata", 1);
         fb_queue_erase("cache");
+        fb_queue_format("cache", 1);
     }
     if (wants_reboot) {
         fb_queue_reboot();
diff --git a/fastboot/fastboot.h b/fastboot/fastboot.h
index c2f97a2..b77cb19 100644
--- a/fastboot/fastboot.h
+++ b/fastboot/fastboot.h
@@ -43,7 +43,7 @@
 /* engine.c - high level command queue engine */
 void fb_queue_flash(const char *ptn, void *data, unsigned sz);;
 void fb_queue_erase(const char *ptn);
-void fb_queue_format(const char *ptn);
+void fb_queue_format(const char *ptn, int skip_if_not_supported);
 void fb_queue_require(const char *prod, const char *var, int invert,
         unsigned nvalues, const char **value);
 void fb_queue_display(const char *var, const char *prettyname);