Nandroid restore of sd-ext.
diff --git a/Android.mk b/Android.mk
index ff63bd4..cdd5ffa 100644
--- a/Android.mk
+++ b/Android.mk
@@ -76,6 +76,14 @@
 
 ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS)
 
+include $(CLEAR_VARS)
+LOCAL_MODULE := nandroid-md5.sh
+LOCAL_MODULE_TAGS := eng
+LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+LOCAL_SRC_FILES := nandroid-md5.sh
+include $(BUILD_PREBUILT)
+
 include $(commands_recovery_local_path)/amend/Android.mk
 include $(commands_recovery_local_path)/minui/Android.mk
 include $(commands_recovery_local_path)/minzip/Android.mk
diff --git a/commands.c b/commands.c
index 3a85bda..5024c08 100644
--- a/commands.c
+++ b/commands.c
@@ -811,6 +811,7 @@
     int restoresystem = 1;
     int restoredata = 1;
     int restorecache = 1;
+    int restoresdext = 1;
     int i;
     for (i = 0; i < argc; i++)
     {
@@ -822,9 +823,11 @@
             restoredata = 0;
         else if (strcmp(argv[i], "nocache") == 0)
             restorecache = 0;
+        else if (strcmp(argv[i], "nosd-ext") == 0)
+            restorecache = 0;
     }
 
-    return nandroid_restore(argv[0], restoreboot, restoresystem, restoredata, restorecache);
+    return nandroid_restore(argv[0], restoreboot, restoresystem, restoredata, restorecache, restoresdext);
 }
 
 static int
diff --git a/extendedcommands.c b/extendedcommands.c
index a180d06..e53aca1 100644
--- a/extendedcommands.c
+++ b/extendedcommands.c
@@ -347,7 +347,7 @@
     char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
     if (file == NULL)
         return;
-    nandroid_restore(file, 1, 1, 1, 1);
+    nandroid_restore(file, 1, 1, 1, 1, 1);
 }
 
 void show_mount_usb_storage_menu()
@@ -641,6 +641,7 @@
                             "Restore system",
                             "Restore data",
                             "Restore cache",
+                            "Restore sd-ext",
                             NULL
     };
 
@@ -648,16 +649,16 @@
     switch (chosen_item)
     {
         case 0:
-            nandroid_restore(file, 1, 0, 0, 0);
+            nandroid_restore(file, 1, 0, 0, 0, 0);
             break;
         case 1:
-            nandroid_restore(file, 0, 1, 0, 0);
+            nandroid_restore(file, 0, 1, 0, 0, 0);
             break;
         case 2:
-            nandroid_restore(file, 0, 0, 1, 0);
+            nandroid_restore(file, 0, 0, 1, 0, 0);
             break;
         case 3:
-            nandroid_restore(file, 0, 0, 0, 1);
+            nandroid_restore(file, 0, 0, 0, 1, 0);
             break;
     }
 }
diff --git a/extendedcommands.h b/extendedcommands.h
index 1223fcd..0683fd6 100644
--- a/extendedcommands.h
+++ b/extendedcommands.h
@@ -36,3 +36,6 @@
 
 void
 show_advanced_menu();
+
+int
+format_mmc_device(char* root);
diff --git a/nandroid.c b/nandroid.c
index 9c683a3..b1d6eec 100644
--- a/nandroid.c
+++ b/nandroid.c
@@ -75,10 +75,11 @@
     ui_show_progress(1, 0);
 }
 
-int nandroid_backup_partition(char* backup_path, char* root, char* name) {
+int nandroid_backup_partition(char* backup_path, char* root) {
     int ret = 0;
     char mount_point[PATH_MAX];
-    sprintf(mount_point, "/%s", name);
+    translate_root_path(root, mount_point, PATH_MAX);
+    char* name = basename(mount_point);
     
     ui_print("Backing up %s...\n", name);
     if (0 != (ret = ensure_root_path_mounted(root) != 0)) {
@@ -108,10 +109,13 @@
 	struct statfs s;
     if (0 != (ret = statfs("/sdcard", &s)))
         return print_and_error("Unable to stat /sdcard\n");
-    long sdcard_free = s.f_bavail * s.f_bsize;
-    if (sdcard_free < 150000000L)
+    uint64_t bavail = s.f_bavail;
+    uint64_t bsize = s.f_bsize;
+    uint64_t sdcard_free = bavail * bsize;
+    uint64_t sdcard_free_mb = sdcard_free / (uint64_t)(1024 * 1024);
+    ui_print("SD Card space free: %lluMB\n", sdcard_free_mb);
+    if (sdcard_free_mb < 150)
         ui_print("There may not be enough free space to complete backup... continuing...\n");
-    //    return print_and_error("There is not enough free space on the SD Card! At least 150MB is required to create a backup.\n");
     
     char tmp[PATH_MAX];
     sprintf(tmp, "mkdir -p %s", backup_path);
@@ -129,22 +133,22 @@
     if (0 != ret)
         return print_and_error("Error while dumping boot image!\n");
 
-    if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:", "system")))
+    if (0 != (ret = nandroid_backup_partition(backup_path, "SYSTEM:")))
         return ret;
 
-    if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:", "data")))
+    if (0 != (ret = nandroid_backup_partition(backup_path, "DATA:")))
         return ret;
 
-    if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:", "cache")))
+    if (0 != (ret = nandroid_backup_partition(backup_path, "CACHE:")))
         return ret;
 
     if (0 != ensure_root_path_mounted("SDEXT:"))
         ui_print("No sd-ext found. Skipping backup.\n");
-    else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:", "sd-ext")))
+    else if (0 != (ret = nandroid_backup_partition(backup_path, "SDEXT:")))
         return ret;
     
     ui_print("Generating md5 sum...\n");
-    sprintf(tmp, "cd %s && (md5sum *img > nandroid.md5)", backup_path);
+    sprintf(tmp, "nandroid-md5.sh %s", backup_path);
     if (0 != (ret = __system(tmp))) {
         ui_print("Error while generating md5 sum!\n");
         return ret;
@@ -157,17 +161,20 @@
     return 0;
 }
 
-int nandroid_restore_partition(char* backup_path, char* root, char* name) {
+typedef int (*format_function)(char* root);
+
+int nandroid_restore_partition_internal(char* backup_path, char* root, format_function formatter) {
     int ret = 0;
     char mount_point[PATH_MAX];
-    sprintf(mount_point, "/%s", name);
+    translate_root_path(root, mount_point, PATH_MAX);
+    char* name = basename(mount_point);
     
     ui_print("Restoring %s...\n", name);
     if (0 != (ret = ensure_root_path_unmounted(root))) {
         ui_print("Can't unmount %s!\n", mount_point);
         return ret;
     }
-    if (0 != (ret = format_root_device(root))) {
+    if (0 != (ret = formatter(root))) {
         ui_print("Error while formatting %s!\n", root);
         return ret;
     }
@@ -186,7 +193,11 @@
     return 0;
 }
 
-int nandroid_restore(char* backup_path, int restore_boot, int restore_system, int restore_data, int restore_cache)
+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)
 {
     ui_set_background(BACKGROUND_ICON_INSTALLING);
     ui_show_indeterminate_progress();
@@ -213,15 +224,25 @@
         }
     }
     
-    if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:", "system")))
+    if (restore_system && 0 != (ret = nandroid_restore_partition(backup_path, "SYSTEM:")))
         return ret;
 
-    if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:", "data")))
+    if (restore_data && 0 != (ret = nandroid_restore_partition(backup_path, "DATA:")))
         return ret;
 
-    if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:", "cache")))
+    if (restore_cache && 0 != (ret = nandroid_restore_partition(backup_path, "CACHE:")))
         return ret;
 
+    if (restore_sdext)
+    {
+        struct statfs s;
+        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)))
+            return ret;
+    }
+
     sync();
     ui_set_background(BACKGROUND_ICON_NONE);
     ui_reset_progress();
diff --git a/nandroid.h b/nandroid.h
index 2ab0308..0d88131 100644
--- a/nandroid.h
+++ b/nandroid.h
@@ -3,6 +3,6 @@
 
 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 nandroid_restore(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