Unmount partitions after restore (except for CACHE:).
Wipe sd-ext on data wipe.
Fix bug where wiping SDEXT: did not work.
diff --git a/extendedcommands.c b/extendedcommands.c
index 1500500..613ad05 100644
--- a/extendedcommands.c
+++ b/extendedcommands.c
@@ -410,8 +410,19 @@
     return chosen_item == 7;
 }
 
-int format_mmc_device(char* root)
+int format_non_mtd_device(const char* root)
 {
+	// if this is SDEXT:, don't worry about it.
+	if (0 == strcmp(root, "SDEXT:"))
+	{
+		struct stat st;
+	    if (0 != stat("/dev/block/mmcblk0p2", &st))
+	    {
+	        ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
+			return 0;
+	    }
+	}
+
     char path[PATH_MAX];
     translate_root_path(root, path, PATH_MAX);
     if (0 != ensure_root_path_mounted(root))
@@ -523,7 +534,7 @@
             if (!confirm_format())
                 continue;
             ui_print("Formatting %s...\n", mmcs[chosen_item][1]);
-            if (0 != format_mmc_device(mmcs[chosen_item][1]))
+            if (0 != format_non_mtd_device(mmcs[chosen_item][1]))
                 ui_print("Error formatting %s!\n", mmcs[chosen_item][1]);
             else
                 ui_print("Done.\n");
@@ -676,6 +687,9 @@
         case 3:
             nandroid_restore(file, 0, 0, 0, 1, 0);
             break;
+        case 4:
+            nandroid_restore(file, 0, 0, 0, 0, 1);
+            break;
     }
 }
 
diff --git a/extendedcommands.h b/extendedcommands.h
index 0683fd6..0f958dd 100644
--- a/extendedcommands.h
+++ b/extendedcommands.h
@@ -11,7 +11,7 @@
 show_choose_zip_menu();
 
 int
-do_nandroid_backup(char* backup_name);
+do_nandroid_backup(const char* backup_name);
 
 int
 do_nandroid_restore();
@@ -38,4 +38,4 @@
 show_advanced_menu();
 
 int
-format_mmc_device(char* root);
+format_non_mtd_device(const char* root);
diff --git a/nandroid.c b/nandroid.c
index 8485f47..c4c92d8 100644
--- a/nandroid.c
+++ b/nandroid.c
@@ -75,7 +75,7 @@
     ui_show_progress(1, 0);
 }
 
-int nandroid_backup_partition(char* backup_path, char* root) {
+int nandroid_backup_partition(const char* backup_path, char* root) {
     int ret = 0;
     char mount_point[PATH_MAX];
     translate_root_path(root, mount_point, PATH_MAX);
@@ -98,7 +98,7 @@
     return 0;
 }
 
-int nandroid_backup(char* backup_path)
+int nandroid_backup(const char* backup_path)
 {
     ui_set_background(BACKGROUND_ICON_INSTALLING);
     
@@ -171,7 +171,7 @@
 
 typedef int (*format_function)(char* root);
 
-int nandroid_restore_partition_internal(char* backup_path, char* root, format_function formatter) {
+int nandroid_restore_partition(const char* backup_path, const char* root) {
     int ret = 0;
     char mount_point[PATH_MAX];
     translate_root_path(root, mount_point, PATH_MAX);
@@ -182,7 +182,7 @@
         ui_print("Can't unmount %s!\n", mount_point);
         return ret;
     }
-    if (0 != (ret = formatter(root))) {
+    if (0 != (ret = format_root_device(root))) {
         ui_print("Error while formatting %s!\n", root);
         return ret;
     }
@@ -198,14 +198,14 @@
         ui_print("Error while restoring %s!\n", mount_point);
         return ret;
     }
+
+    if (0 != strcmp(root, "CACHE")) {
+        ensure_root_path_unmounted(root);
+    }
     return 0;
 }
 
-int nandroid_restore_partition(char* backup_path, char* root) {
-    return nandroid_restore_partition_internal(backup_path, root, format_root_device);
-}
-
-int nandroid_restore(char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext)
+int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext)
 {
     ui_set_background(BACKGROUND_ICON_INSTALLING);
     ui_show_indeterminate_progress();
@@ -247,7 +247,7 @@
         sprintf(tmp, "%s/sd-ext.img", backup_path);
         if (0 != (ret = statfs(tmp, &s)))
             ui_print("sd-ext.img not found. Skipping restore of /sd-ext.");
-        else if (0 != (ret = nandroid_restore_partition_internal(backup_path, "SDEXT:", format_mmc_device)))
+        else if (0 != (ret = nandroid_restore_partition(backup_path, "SDEXT:")))
             return ret;
     }
 
diff --git a/nandroid.h b/nandroid.h
index 0d88131..30fee01 100644
--- a/nandroid.h
+++ b/nandroid.h
@@ -2,7 +2,7 @@
 #define NANDROID_H
 
 int nandroid_main(int argc, char** argv);
-int nandroid_backup(char* backup_path);
-int nandroid_restore(char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext);
+int nandroid_backup(const char* backup_path);
+int nandroid_restore(const char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache, int restore_sdext);
 
 #endif
\ No newline at end of file
diff --git a/recovery.c b/recovery.c
index 5743bd3..6972564 100644
--- a/recovery.c
+++ b/recovery.c
@@ -385,6 +385,7 @@
     device_wipe_data();
     erase_root("DATA:");
     erase_root("CACHE:");
+    erase_root("SDEXT:");
     ui_print("Data wipe complete.\n");
 }
 
diff --git a/roots.c b/roots.c
index 31c7bdd..abfb4ce 100644
--- a/roots.c
+++ b/roots.c
@@ -27,6 +27,8 @@
 #include "roots.h"
 #include "common.h"
 
+#include "extendedcommands.h"
+
 typedef struct {
     const char *name;
     const char *device;
@@ -366,7 +368,6 @@
             }
         }
     }
-//TODO: handle other device types (sdcard, etc.)
-    LOGW("format_root_device: can't handle non-mtd device \"%s\"\n", root);
-    return -1;
+
+    return format_non_mtd_device(root);
 }