recovery: Autodetection of device flash type

Detect flash type at runtime rather than requiring this to be set in the
device configuration. The detection is based on the existence of /proc/mtd,
/proc/emmc, or /dev/block/bml1.

Change-Id: I464962a567022c5862c249f06d36c2d1cddeacba
diff --git a/Android.mk b/Android.mk
index ce17ff3..1c575c2 100644
--- a/Android.mk
+++ b/Android.mk
@@ -73,16 +73,7 @@
 endif
 LOCAL_STATIC_LIBRARIES += libbusybox libclearsilverregex libmkyaffs2image libunyaffs liberase_image libdump_image libflash_image
 
-ifdef BOARD_USES_BMLUTILS
-	BOARD_FLASH_LIBRARY := libbmlutils
-else ifdef BOARD_USES_MMCUTILS
-	BOARD_FLASH_LIBRARY := libmmcutils
-else
-    LOCAL_CFLAGS += -DBOARD_USES_MTDUTILS
-	BOARD_FLASH_LIBRARY := libmtdutils
-endif
-
-LOCAL_STATIC_LIBRARIES += $(BOARD_FLASH_LIBRARY)
+LOCAL_STATIC_LIBRARIES += libflashutils libmtdutils libmmcutils libbmlutils
 
 LOCAL_STATIC_LIBRARIES += libamend
 LOCAL_STATIC_LIBRARIES += libminzip libunz libmincrypt
diff --git a/bmlutils/bmlutils.c b/bmlutils/bmlutils.c
index db3f16a..5709cc5 100644
--- a/bmlutils/bmlutils.c
+++ b/bmlutils/bmlutils.c
@@ -20,40 +20,39 @@
 #include <signal.h>
 #include <sys/wait.h>
 
-int
-__system(const char *command);
+extern int __system(const char *command);
 
-int restore_raw_partition(const char *partition, const char *filename)
+int cmd_bml_restore_raw_partition(const char *partition, const char *filename)
 {
     char tmp[PATH_MAX];
     sprintf("dd if=%s of=/dev/block/bml7 bs=4096", filename);
     return __system(tmp);
 }
 
-int backup_raw_partition(const char *partition, const char *filename)
+int cmd_bml_backup_raw_partition(const char *partition, const char *filename)
 {
     char tmp[PATH_MAX];
     sprintf("dd of=%s if=/dev/block/bml7 bs=4096", filename);
     return __system(tmp);
 }
 
-int erase_raw_partition(const char *partition)
+int cmd_bml_erase_raw_partition(const char *partition)
 {
     // TODO: implement raw wipe
     return 0;
 }
 
-int erase_partition(const char *partition, const char *filesystem)
+int cmd_bml_erase_partition(const char *partition, const char *filesystem)
 {
     return -1;
 }
 
-int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
+int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
 {
     return -1;
 }
 
-int get_partition_device(const char *partition, char *device)
+int cmd_bml_get_partition_device(const char *partition, char *device)
 {
     return -1;
 }
diff --git a/extendedcommands.c b/extendedcommands.c
index 801f4a8..d6e5e4f 100644
--- a/extendedcommands.c
+++ b/extendedcommands.c
@@ -68,7 +68,7 @@
         ui_set_background(BACKGROUND_ICON_ERROR);
         ui_print("Installation aborted.\n");
         return 1;
-    } 
+    }
 #ifndef BOARD_HAS_NO_MISC_PARTITION
     if (firmware_update_pending()) {
         ui_print("\nReboot via menu to complete\ninstallation.\n");
@@ -93,7 +93,7 @@
 {
     static char* headers[] = {  "Apply update from .zip file on SD card",
                                 "",
-                                NULL 
+                                NULL
     };
     for (;;)
     {
@@ -118,7 +118,7 @@
             default:
                 return;
         }
-        
+
     }
 }
 
@@ -153,11 +153,11 @@
         ui_print("Couldn't open directory.\n");
         return NULL;
     }
-  
+
     int extension_length = 0;
     if (fileExtensionOrDirectory != NULL)
         extension_length = strlen(fileExtensionOrDirectory);
-  
+
     int isCounting = 1;
     i = 0;
     for (pass = 0; pass < 2; pass++) {
@@ -165,7 +165,7 @@
             // skip hidden files
             if (de->d_name[0] == '.')
                 continue;
-            
+
             // NULL means that we are gathering directories, so skip this
             if (fileExtensionOrDirectory != NULL)
             {
@@ -187,13 +187,13 @@
                 if (!(S_ISDIR(info.st_mode)))
                     continue;
             }
-            
+
             if (pass == 0)
             {
                 total++;
                 continue;
             }
-            
+
             files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
             strcpy(files[i], directory);
             strcat(files[i], de->d_name);
@@ -290,7 +290,7 @@
                     break;
                 }
                 continue;
-            } 
+            }
             strcpy(ret, files[chosen_item - numDirs]);
             return_value = ret;
             break;
@@ -312,9 +312,9 @@
 
     static char* headers[] = {  "Choose a zip to apply",
                                 "",
-                                NULL 
+                                NULL
     };
-    
+
     char* file = choose_file_menu("/sdcard/", ".zip", headers);
     if (file == NULL)
         return;
@@ -328,57 +328,16 @@
         install_zip(sdcard_package_file);
 }
 
-// This was pulled from bionic: The default system command always looks
-// for shell in /system/bin/sh. This is bad.
-#define _PATH_BSHELL "/sbin/sh"
-
-extern char **environ;
-int
-__system(const char *command)
-{
-  pid_t pid;
-    sig_t intsave, quitsave;
-    sigset_t mask, omask;
-    int pstat;
-    char *argp[] = {"sh", "-c", NULL, NULL};
-
-    if (!command)        /* just checking... */
-        return(1);
-
-    argp[2] = (char *)command;
-
-    sigemptyset(&mask);
-    sigaddset(&mask, SIGCHLD);
-    sigprocmask(SIG_BLOCK, &mask, &omask);
-    switch (pid = vfork()) {
-    case -1:            /* error */
-        sigprocmask(SIG_SETMASK, &omask, NULL);
-        return(-1);
-    case 0:                /* child */
-        sigprocmask(SIG_SETMASK, &omask, NULL);
-        execve(_PATH_BSHELL, argp, environ);
-    _exit(127);
-  }
-
-    intsave = (sig_t)  bsd_signal(SIGINT, SIG_IGN);
-    quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
-    pid = waitpid(pid, (int *)&pstat, 0);
-    sigprocmask(SIG_SETMASK, &omask, NULL);
-    (void)bsd_signal(SIGINT, intsave);
-    (void)bsd_signal(SIGQUIT, quitsave);
-    return (pid == -1 ? -1 : pstat);
-}
-
 void show_nandroid_restore_menu()
 {
     if (ensure_root_path_mounted("SDCARD:") != 0) {
         LOGE ("Can't mount /sdcard\n");
         return;
     }
-    
+
     static char* headers[] = {  "Choose an image to restore",
                                 "",
-                                NULL 
+                                NULL
     };
 
     char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
@@ -398,18 +357,18 @@
                                 "Leaving this menu unmount",
                                 "your SD card from your PC.",
                                 "",
-                                NULL 
+                                NULL
     };
-    
+
     static char* list[] = { "Unmount", NULL };
-    
+
     for (;;)
     {
         int chosen_item = get_menu_selection(headers, list, 0);
         if (chosen_item == GO_BACK || chosen_item == 0)
             break;
     }
-    
+
     __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
     __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
 }
@@ -465,7 +424,7 @@
     __system(tmp);
     sprintf(tmp, "rm -rf %s/.*", path);
     __system(tmp);
-    
+
     ensure_root_path_unmounted(root);
     return 0;
 }
@@ -478,33 +437,33 @@
 {
     static char* headers[] = {  "Mounts and Storage Menu",
                                 "",
-                                NULL 
+                                NULL
     };
 
     typedef char* string;
-    string mounts[MOUNTABLE_COUNT][3] = { 
+    string mounts[MOUNTABLE_COUNT][3] = {
         { "mount /system", "unmount /system", "SYSTEM:" },
         { "mount /data", "unmount /data", "DATA:" },
         { "mount /cache", "unmount /cache", "CACHE:" },
         { "mount /sdcard", "unmount /sdcard", "SDCARD:" },
         { "mount /sd-ext", "unmount /sd-ext", "SDEXT:" }
         };
-        
+
     string mtds[MTD_COUNT][2] = {
         { "format boot", "BOOT:" },
         { "format system", "SYSTEM:" },
         { "format data", "DATA:" },
         { "format cache", "CACHE:" },
     };
-    
+
     string mmcs[MMC_COUNT][3] = {
       { "format sdcard", "SDCARD:" },
-      { "format sd-ext", "SDEXT:" }  
+      { "format sd-ext", "SDEXT:" }
     };
-    
+
     static char* confirm_format  = "Confirm format?";
     static char* confirm = "Yes - Format";
-        
+
     for (;;)
     {
         int ismounted[MOUNTABLE_COUNT];
@@ -515,20 +474,20 @@
             ismounted[i] = is_root_path_mounted(mounts[i][2]);
             options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
         }
-        
+
         for (i = 0; i < MTD_COUNT; i++)
         {
             options[MOUNTABLE_COUNT + i] = mtds[i][0];
         }
-            
+
         for (i = 0; i < MMC_COUNT; i++)
         {
             options[MOUNTABLE_COUNT + MTD_COUNT + i] = mmcs[i][0];
         }
-    
+
         options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT] = "mount USB storage";
         options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT + 1] = NULL;
-        
+
         int chosen_item = get_menu_selection(headers, options, 0);
         if (chosen_item == GO_BACK)
             break;
@@ -607,8 +566,8 @@
         }
         printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
         return 1;
-    }    
-    
+    }
+
     return 0;
 }
 
@@ -619,7 +578,7 @@
         printf("Error executing stat on file: %s\n", filename);
         return 1;
     }
-    
+
     int script_len = file_info.st_size;
     char* script_data = (char*)malloc(script_len + 1);
     FILE *file = fopen(filename, "rb");
@@ -654,14 +613,14 @@
     if (i == 0) {
         ui_print("Timed out waiting for SD card... continuing anyways.");
     }
-    
+
     sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
     return run_script(tmp);
 }
 
 int amend_main(int argc, char** argv)
 {
-    if (argc != 2) 
+    if (argc != 2)
     {
         printf("Usage: amend <script>\n");
         return 0;
@@ -740,10 +699,10 @@
 {
     static char* headers[] = {  "Nandroid",
                                 "",
-                                NULL 
+                                NULL
     };
 
-    static char* list[] = { "Backup", 
+    static char* list[] = { "Backup",
                             "Restore",
                             "Advanced Restore",
                             NULL
@@ -875,7 +834,7 @@
                 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0);
                 if (ext_size == GO_BACK)
                     continue;
-                 
+
                 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0);
                 if (swap_size == GO_BACK)
                     continue;
@@ -925,9 +884,9 @@
     {
         fprintf(file, "%s ", info->device);
     }
-    
+
     fprintf(file, "%s ", info->mount_point);
-    fprintf(file, "%s %s\n", info->filesystem, info->filesystem_options == NULL ? "rw" : info->filesystem_options); 
+    fprintf(file, "%s %s\n", info->filesystem, info->filesystem_options == NULL ? "rw" : info->filesystem_options);
 }
 
 void create_fstab()
@@ -958,4 +917,4 @@
     mkdir("/sdcard/clockworkmod", S_IRWXU);
     __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
     ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.\n");
-}
\ No newline at end of file
+}
diff --git a/flashutils/Android.mk b/flashutils/Android.mk
index 5742af3..5f6fea2 100644
--- a/flashutils/Android.mk
+++ b/flashutils/Android.mk
@@ -4,10 +4,18 @@
 ifeq ($(TARGET_ARCH),arm)
 
 include $(CLEAR_VARS)
+LOCAL_SRC_FILES := flashutils.c
+LOCAL_MODULE := libflashutils
+LOCAL_C_INCLUDES += bootable/recovery
+LOCAL_STATIC_LIBRARIES := libmmcutils libmtdutils libbmlutils
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
 LOCAL_SRC_FILES := flash_image.c
 LOCAL_MODULE := flash_image
 LOCAL_MODULE_TAGS := eng
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY)
+#LOCAL_STATIC_LIBRARIES += $(BOARD_FLASH_LIBRARY)
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils
 LOCAL_SHARED_LIBRARIES := libcutils libc
 include $(BUILD_EXECUTABLE)
 
@@ -15,7 +23,7 @@
 LOCAL_SRC_FILES := dump_image.c
 LOCAL_MODULE := dump_image
 LOCAL_MODULE_TAGS := eng
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY)
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils
 LOCAL_SHARED_LIBRARIES := libcutils libc
 include $(BUILD_EXECUTABLE)
 
@@ -23,7 +31,7 @@
 LOCAL_SRC_FILES := erase_image.c
 LOCAL_MODULE := erase_image
 LOCAL_MODULE_TAGS := eng
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY)
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils
 LOCAL_SHARED_LIBRARIES := libcutils libc
 include $(BUILD_EXECUTABLE)
 
@@ -53,7 +61,7 @@
 LOCAL_MODULE_PATH := $(PRODUCT_OUT)/utilities
 LOCAL_UNSTRIPPED_PATH := $(PRODUCT_OUT)/symbols/utilities
 LOCAL_MODULE_STEM := dump_image
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY) libcutils libc
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils libcutils libc
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 include $(BUILD_EXECUTABLE)
 
@@ -64,7 +72,7 @@
 LOCAL_MODULE_PATH := $(PRODUCT_OUT)/utilities
 LOCAL_UNSTRIPPED_PATH := $(PRODUCT_OUT)/symbols/utilities
 LOCAL_MODULE_STEM := flash_image
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY) libcutils libc
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils libcutils libc
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 include $(BUILD_EXECUTABLE)
 
@@ -75,10 +83,9 @@
 LOCAL_MODULE_PATH := $(PRODUCT_OUT)/utilities
 LOCAL_UNSTRIPPED_PATH := $(PRODUCT_OUT)/symbols/utilities
 LOCAL_MODULE_STEM := erase_image
-LOCAL_STATIC_LIBRARIES := $(BOARD_FLASH_LIBRARY) libcutils libc
+LOCAL_STATIC_LIBRARIES := libflashutils libmtdutils libmmcutils libbmlutils libcutils libc
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 include $(BUILD_EXECUTABLE)
 
-
 endif	# TARGET_ARCH == arm
 endif	# !TARGET_SIMULATOR
diff --git a/flashutils/flashutils.c b/flashutils/flashutils.c
new file mode 100644
index 0000000..5c57756
--- /dev/null
+++ b/flashutils/flashutils.c
@@ -0,0 +1,168 @@
+#include <signal.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#include "flashutils/flashutils.h"
+
+enum flash_type {
+    UNSUPPORTED = -1,
+    UNKNOWN = 0,
+    MTD = 1,
+    MMC = 2,
+    BML = 3
+};
+
+int the_flash_type = UNKNOWN;
+
+int device_flash_type()
+{
+    if (the_flash_type == UNKNOWN) {
+        if (access("/dev/block/bml1", F_OK) == 0) {
+            the_flash_type = BML;
+        } else if (access("/proc/emmc", F_OK) == 0) {
+            the_flash_type = MMC;
+        } else if (access("/proc/mtd", F_OK) == 0) {
+            the_flash_type = MTD;
+        } else {
+            the_flash_type = UNSUPPORTED;
+        }
+    }
+    return the_flash_type;
+}
+
+char* get_default_filesystem()
+{
+    return device_flash_type() == MMC ? "ext3" : "yaffs2";
+}
+
+// This was pulled from bionic: The default system command always looks
+// for shell in /system/bin/sh. This is bad.
+#define _PATH_BSHELL "/sbin/sh"
+
+extern char **environ;
+int
+__system(const char *command)
+{
+  pid_t pid;
+    sig_t intsave, quitsave;
+    sigset_t mask, omask;
+    int pstat;
+    char *argp[] = {"sh", "-c", NULL, NULL};
+
+    if (!command)        /* just checking... */
+        return(1);
+
+    argp[2] = (char *)command;
+
+    sigemptyset(&mask);
+    sigaddset(&mask, SIGCHLD);
+    sigprocmask(SIG_BLOCK, &mask, &omask);
+    switch (pid = vfork()) {
+    case -1:            /* error */
+        sigprocmask(SIG_SETMASK, &omask, NULL);
+        return(-1);
+    case 0:                /* child */
+        sigprocmask(SIG_SETMASK, &omask, NULL);
+        execve(_PATH_BSHELL, argp, environ);
+    _exit(127);
+  }
+
+    intsave = (sig_t)  bsd_signal(SIGINT, SIG_IGN);
+    quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
+    pid = waitpid(pid, (int *)&pstat, 0);
+    sigprocmask(SIG_SETMASK, &omask, NULL);
+    (void)bsd_signal(SIGINT, intsave);
+    (void)bsd_signal(SIGQUIT, quitsave);
+    return (pid == -1 ? -1 : pstat);
+}
+
+int restore_raw_partition(const char *partition, const char *filename)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_restore_raw_partition(partition, filename);
+        case MMC:
+            return cmd_mmc_restore_raw_partition(partition, filename);
+        case BML:
+            return cmd_bml_restore_raw_partition(partition, filename);
+        default:
+            return -1;
+    }
+}
+
+int backup_raw_partition(const char *partition, const char *filename)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_backup_raw_partition(partition, filename);
+        case MMC:
+            return cmd_mmc_backup_raw_partition(partition, filename);
+        case BML:
+            return cmd_bml_backup_raw_partition(partition, filename);
+        default:
+            return -1;
+    }
+}
+
+int erase_raw_partition(const char *partition)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_erase_raw_partition(partition);
+        case MMC:
+            return cmd_mmc_erase_raw_partition(partition);
+        case BML:
+            return cmd_bml_erase_raw_partition(partition);
+        default:
+            return -1;
+    }
+}
+
+int erase_partition(const char *partition, const char *filesystem)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_erase_partition(partition, filesystem);
+        case MMC:
+            return cmd_mmc_erase_partition(partition, filesystem);
+        case BML:
+            return cmd_bml_erase_partition(partition, filesystem);
+        default:
+            return -1;
+    }
+}
+
+int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_mount_partition(partition, mount_point, filesystem, read_only);
+        case MMC:
+            return cmd_mmc_mount_partition(partition, mount_point, filesystem, read_only);
+        case BML:
+            return cmd_bml_mount_partition(partition, mount_point, filesystem, read_only);
+        default:
+            return -1;
+    }
+}
+
+int get_partition_device(const char *partition, char *device)
+{
+    int type = device_flash_type();
+    switch (type) {
+        case MTD:
+            return cmd_mtd_get_partition_device(partition, device);
+        case MMC:
+            return cmd_mmc_get_partition_device(partition, device);
+        case BML:
+            return cmd_bml_get_partition_device(partition, device);
+        default:
+            return -1;
+    }
+}
diff --git a/flashutils/flashutils.h b/flashutils/flashutils.h
index f4466f0..953dd00 100644
--- a/flashutils/flashutils.h
+++ b/flashutils/flashutils.h
@@ -3,4 +3,36 @@
 int erase_raw_partition(const char *partition);
 int erase_partition(const char *partition, const char *filesystem);
 int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only);
-int get_partition_device(const char *partition, char *device);
\ No newline at end of file
+int get_partition_device(const char *partition, char *device);
+
+#define FLASH_MTD 0
+#define FLASH_MMC 1
+#define FLASH_BML 2
+
+int is_mtd_device();
+char* get_default_filesystem();
+
+int __system(const char *command);
+
+extern int cmd_mtd_restore_raw_partition(const char *partition, const char *filename);
+extern int cmd_mtd_backup_raw_partition(const char *partition, const char *filename);
+extern int cmd_mtd_erase_raw_partition(const char *partition);
+extern int cmd_mtd_erase_partition(const char *partition, const char *filesystem);
+extern int cmd_mtd_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only);
+extern int cmd_mtd_get_partition_device(const char *partition, char *device);
+
+extern int cmd_mmc_restore_raw_partition(const char *partition, const char *filename);
+extern int cmd_mmc_backup_raw_partition(const char *partition, const char *filename);
+extern int cmd_mmc_erase_raw_partition(const char *partition);
+extern int cmd_mmc_erase_partition(const char *partition, const char *filesystem);
+extern int cmd_mmc_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only);
+extern int cmd_mmc_get_partition_device(const char *partition, char *device);
+
+extern int cmd_bml_restore_raw_partition(const char *partition, const char *filename);
+extern int cmd_bml_backup_raw_partition(const char *partition, const char *filename);
+extern int cmd_bml_erase_raw_partition(const char *partition);
+extern int cmd_bml_erase_partition(const char *partition, const char *filesystem);
+extern int cmd_bml_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only);
+extern int cmd_bml_get_partition_device(const char *partition, char *device);
+
+
diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c
index 2c10343..44fdcf2 100644
--- a/mmcutils/mmcutils.c
+++ b/mmcutils/mmcutils.c
@@ -527,7 +527,7 @@
 
 }
 
-int restore_raw_partition(const char *partition, const char *filename)
+int cmd_mmc_restore_raw_partition(const char *partition, const char *filename)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
@@ -537,7 +537,7 @@
     return mmc_raw_copy(p, filename);
 }
 
-int backup_raw_partition(const char *partition, const char *filename)
+int cmd_mmc_backup_raw_partition(const char *partition, const char *filename)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
@@ -547,19 +547,19 @@
     return mmc_raw_dump(p, filename);
 }
 
-int erase_raw_partition(const char *partition)
+int cmd_mmc_erase_raw_partition(const char *partition)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
     p = mmc_find_partition_by_name(partition);
     if (p == NULL)
         return -1;
-        
+
     // TODO: implement raw wipe
     return 0;
 }
 
-int erase_partition(const char *partition, const char *filesystem)
+int cmd_mmc_erase_partition(const char *partition, const char *filesystem)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
@@ -569,7 +569,7 @@
     return mmc_format_ext3 (p);
 }
 
-int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
+int cmd_mmc_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
@@ -579,7 +579,7 @@
     return mmc_mount_partition(p, mount_point, read_only);
 }
 
-int get_partition_device(const char *partition, char *device)
+int cmd_mmc_get_partition_device(const char *partition, char *device)
 {
     mmc_scan_partitions();
     const MmcPartition *p;
diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c
index 2daaa4c..9fd5c6c 100644
--- a/mtdutils/mtdutils.c
+++ b/mtdutils/mtdutils.c
@@ -562,7 +562,7 @@
 #define SPARE_SIZE    (BLOCK_SIZE >> 5)
 #define HEADER_SIZE 2048
 
-int restore_raw_partition(const char *partition_name, const char *filename)
+int cmd_mtd_restore_raw_partition(const char *partition_name, const char *filename)
 {
     const MtdPartition *ptn;
     MtdWriteContext *write;
@@ -577,9 +577,9 @@
     const MtdPartition *partition = mtd_find_partition_by_name(partition_name);
     if (partition == NULL)
     {
-        printf("can't find %s partition", partition_name);  
+        printf("can't find %s partition", partition_name);
         return -1;
-    } 
+    }
 
     // If the first part of the file matches the partition, skip writing
 
@@ -588,11 +588,11 @@
     {
         printf("error opening %s", filename);
         return -1;
-    } 
+    }
 
     char header[HEADER_SIZE];
     int headerlen = read(fd, header, sizeof(header));
-    if (headerlen <= 0) 
+    if (headerlen <= 0)
     {
         printf("error reading %s header", filename);
         return -1;
@@ -619,7 +619,7 @@
     printf("flashing %s from %s\n", partition_name, filename);
 
     MtdWriteContext *out = mtd_write_partition(partition);
-    if (out == NULL) 
+    if (out == NULL)
     {
        printf("error writing %s", partition_name);
        return -1;
@@ -628,7 +628,7 @@
     char buf[HEADER_SIZE];
     memset(buf, 0, headerlen);
     int wrote = mtd_write_data(out, buf, headerlen);
-    if (wrote != headerlen) 
+    if (wrote != headerlen)
     {
         printf("error writing %s", partition_name);
         return -1;
@@ -643,13 +643,13 @@
             return -1;
         }
     }
-    if (len < 0) 
+    if (len < 0)
     {
        printf("error reading %s", filename);
        return -1;
     }
 
-    if (mtd_write_close(out)) 
+    if (mtd_write_close(out))
     {
         printf("error closing %s", partition_name);
         return -1;
@@ -662,14 +662,14 @@
     {
         printf("error re-opening %s", partition_name);
         return -1;
-    } 
+    }
 
     wrote = mtd_write_data(out, header, headerlen);
     if (wrote != headerlen)
     {
         printf("error re-writing %s", partition_name);
         return -1;
-    } 
+    }
 
     // Need to write a complete block, so write the rest of the first block
     size_t block_size;
@@ -698,11 +698,11 @@
             printf("error writing %s", partition_name);
             return -1;
         }
-            
+
         left -= len;
     }
 
-    if (mtd_write_close(out)) 
+    if (mtd_write_close(out))
     {
         printf("error closing %s", partition_name);
         return -1;
@@ -711,7 +711,7 @@
 }
 
 
-int backup_raw_partition(const char *partition_name, const char *filename)
+int cmd_mtd_backup_raw_partition(const char *partition_name, const char *filename)
 {
     MtdReadContext *in;
     const MtdPartition *partition;
@@ -722,10 +722,10 @@
     int fd;
     int wrote;
     int len;
-    
+
     if (mtd_scan_partitions() <= 0)
     {
-        printf("error scanning partitions");    
+        printf("error scanning partitions");
         return -1;
     }
 
@@ -743,7 +743,7 @@
 
     if (!strcmp(filename, "-")) {
         fd = fileno(stdout);
-    } 
+    }
     else {
         fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666);
     }
@@ -784,7 +784,7 @@
     return 0;
 }
 
-int erase_raw_partition(const char *partition_name)
+int cmd_mtd_erase_raw_partition(const char *partition_name)
 {
     MtdWriteContext *out;
     size_t erased;
@@ -799,14 +799,14 @@
     const MtdPartition *p = mtd_find_partition_by_name(partition_name);
     if (p == NULL)
     {
-        printf("can't find %s partition", partition_name);   
+        printf("can't find %s partition", partition_name);
         return -1;
     }
 
     out = mtd_write_partition(p);
     if (out == NULL)
     {
-        printf("could not estabilish write context for %s", partition_name);   
+        printf("could not estabilish write context for %s", partition_name);
         return -1;
     }
 
@@ -823,13 +823,13 @@
     return 0;
 }
 
-int erase_partition(const char *partition, const char *filesystem)
+int cmd_mtd_erase_partition(const char *partition, const char *filesystem)
 {
-    return erase_raw_partition(partition);
+    return cmd_mtd_erase_raw_partition(partition);
 }
 
 
-int mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
+int cmd_mtd_mount_partition(const char *partition, const char *mount_point, const char *filesystem, int read_only)
 {
     mtd_scan_partitions();
     const MtdPartition *p;
@@ -840,7 +840,7 @@
     return mtd_mount_partition(p, mount_point, filesystem, read_only);
 }
 
-int get_partition_device(const char *partition, char *device)
+int cmd_mtd_get_partition_device(const char *partition, char *device)
 {
     mtd_scan_partitions();
     MtdPartition *p = mtd_find_partition_by_name(partition);
diff --git a/roots.h b/roots.h
index e10663d..02ec2fb 100644
--- a/roots.h
+++ b/roots.h
@@ -19,6 +19,7 @@
 #define RECOVERY_ROOTS_H_
 
 #include "minzip/Zip.h"
+#include "flashutils/flashutils.h"
 #include "mtdutils/mtdutils.h"
 #include "mmcutils/mmcutils.h"
 
diff --git a/updater/Android.mk b/updater/Android.mk
index ae90211..d312493 100644
--- a/updater/Android.mk
+++ b/updater/Android.mk
@@ -19,7 +19,7 @@
 
 LOCAL_SRC_FILES := $(updater_src_files)
 
-LOCAL_STATIC_LIBRARIES += $(BOARD_FLASH_LIBRARY)
+LOCAL_STATIC_LIBRARIES += libflashutils libmtdutils libmmcutils libbmlutils
 
 LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UPDATER_LIBS) $(TARGET_RECOVERY_UPDATER_EXTRA_LIBS)
 LOCAL_STATIC_LIBRARIES += libapplypatch libedify libminzip libz
diff --git a/updater/install.c b/updater/install.c
index c71781a..7b9aa80 100644
--- a/updater/install.c
+++ b/updater/install.c
@@ -33,17 +33,12 @@
 #include "mincrypt/sha.h"
 #include "minzip/DirUtil.h"
 #include "mounts.h"
+#include "flashutils/flashutils.h"
 #include "mtdutils/mtdutils.h"
 #include "mmcutils/mmcutils.h"
 #include "updater.h"
 #include "applypatch/applypatch.h"
 
-#ifndef BOARD_USES_MMCUTILS
-#define DEFAULT_FILESYSTEM "yaffs2"
-#else
-#define DEFAULT_FILESYSTEM "ext3"
-#endif
-
 // mount(type, location, mount_point)
 //
 //   what:  type="MTD"   location="<partition>"            to mount a yaffs2 filesystem
@@ -76,7 +71,7 @@
     mkdir(mount_point, 0755);
 
     if (strcmp(type, "MTD") == 0 || strcmp(type, "MMC") == 0) {
-        if (0 == mount_partition(location, mount_point, DEFAULT_FILESYSTEM, 0))
+        if (0 == mount_partition(location, mount_point, get_default_filesystem(), 0))
             result = mount_point;
         else
             result = strdup("");