Changes to support Vision recovery. Fixing up a lot of bugs related to the CodeAurora mmc commit.

Change-Id: I9b71070fe41559a5d93d3c35efc3a511b7088e8e
diff --git a/Android.mk b/Android.mk
index 80fa6d3..4786c79 100644
--- a/Android.mk
+++ b/Android.mk
@@ -44,7 +44,7 @@
 
 LOCAL_FORCE_STATIC_EXECUTABLE := true
 
-RECOVERY_VERSION := ClockworkMod Recovery v2.5.1.1
+RECOVERY_VERSION := ClockworkMod Recovery v2.5.1.2
 LOCAL_CFLAGS += -DRECOVERY_VERSION="$(RECOVERY_VERSION)"
 RECOVERY_API_VERSION := 2
 LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION)
diff --git a/extendedcommands.c b/extendedcommands.c
index d70e0fd..cc7fb01 100644
--- a/extendedcommands.c
+++ b/extendedcommands.c
@@ -34,6 +34,7 @@
 #include "amend/amend.h"
 
 #include "mtdutils/mtdutils.h"
+#include "mmcutils/mmcutils.h"
 #include "mtdutils/dump_image.h"
 #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
 #include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
@@ -924,7 +925,15 @@
     }
     else
     {
-        fprintf(file, "%s ", info->device);
+        MmcPartition *mmc = get_root_mmc_partition(root_path);
+        if (mmc != NULL)
+        {
+            fprintf(file, "%s ", mmc->device_index);
+        }
+        else
+        {
+            fprintf(file, "%s ", info->device);
+        }
     }
     
     fprintf(file, "%s ", info->mount_point);
diff --git a/mmcutils/mmcutils.c b/mmcutils/mmcutils.c
index d7e459a..f7df95d 100644
--- a/mmcutils/mmcutils.c
+++ b/mmcutils/mmcutils.c
@@ -47,16 +47,6 @@
 unsigned vfat_count = 0;
 char *vfat_partitions[] = {"modem", "NONE"};
 
-struct MmcPartition {
-    char *device_index;
-    char *filesystem;
-    char *name;
-    unsigned dstatus;
-    unsigned dtype ;
-    unsigned dfirstsec;
-    unsigned dsize;
-};
-
 typedef struct {
     MmcPartition *partitions;
     int partitions_allocd;
@@ -80,6 +70,10 @@
             sprintf(name,"boot");
             mbr->name = strdup(name);
             break;
+        case MMC_RECOVERY_TYPE:
+            sprintf(name,"recovery");
+            mbr->name = strdup(name);
+            break;
         case MMC_EXT3_TYPE:
             if (strcmp("NONE", ext3_partitions[ext3_count])) {
                 strcpy((char *)name,(const char *)ext3_partitions[ext3_count]);
@@ -291,9 +285,9 @@
     return NULL;
 }
 
-#define MKE2FS_BIN      "/sbin/mke2fs_static"
-#define TUNE2FS_BIN     "/sbin/tune2fs_static"
-#define E2FSCK_BIN      "/sbin/e2fsck_static"
+#define MKE2FS_BIN      "/sbin/mke2fs"
+#define TUNE2FS_BIN     "/sbin/tune2fs"
+#define E2FSCK_BIN      "/sbin/e2fsck"
 
 static int
 run_exec_process ( char **argv) {
@@ -323,7 +317,7 @@
         return -1;
 
     // Run tune2fs
-    char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL};
+    char *const tune2fs[] = {TUNE2FS_BIN, "-j", "-C", "1", device, NULL};
     if(run_exec_process(tune2fs))
         return -1;
 
@@ -413,3 +407,55 @@
 
 }
 
+
+int
+mmc_raw_dump (const MmcPartition *partition, char *out_file) {
+    int ch;
+    FILE *in;
+    FILE *out;
+    int val = 0;
+    char buf[512];
+    unsigned sz = 0;
+    unsigned i;
+    int ret = -1;
+    char *in_file = partition->device_index;
+
+    in  = fopen ( in_file,  "r" );
+    if (in == NULL)
+        goto ERROR3;
+
+    out = fopen ( out_file,  "w" );
+    if (out == NULL)
+        goto ERROR2;
+
+    fseek(in, 0L, SEEK_END);
+    sz = ftell(in);
+    fseek(in, 0L, SEEK_SET);
+
+    if (sz % 512)
+    {
+        while ( ( ch = fgetc ( in ) ) != EOF )
+            fputc ( ch, out );
+    }
+    else
+    {
+        for (i=0; i< (sz/512); i++)
+        {
+            if ((fread(buf, 512, 1, in)) != 1)
+                goto ERROR1;
+            if ((fwrite(buf, 512, 1, out)) != 1)
+                goto ERROR1;
+        }
+    }
+
+    fsync(out);
+    ret = 0;
+ERROR1:
+    fclose ( out );
+ERROR2:
+    fclose ( in );
+ERROR3:
+    return ret;
+
+}
+
diff --git a/mmcutils/mmcutils.h b/mmcutils/mmcutils.h
index bab494a..6a3070d 100644
--- a/mmcutils/mmcutils.h
+++ b/mmcutils/mmcutils.h
@@ -50,6 +50,7 @@
 #define MMC_BOOT_TYPE 0x48
 #define MMC_SYSTEM_TYPE 0x82
 #define MMC_USERDATA_TYPE 0x83
+#define MMC_RECOVERY_TYPE 0x71
 
 #define MMC_RCA 2
 
@@ -70,7 +71,15 @@
 #define MMC_BOOT_TYPE 0x48
 #define MMC_EXT3_TYPE 0x83
 #define MMC_VFAT_TYPE 0xC
-typedef struct MmcPartition MmcPartition;
+typedef struct MmcPartition {
+    char *device_index;
+    char *filesystem;
+    char *name;
+    unsigned dstatus;
+    unsigned dtype ;
+    unsigned dfirstsec;
+    unsigned dsize;
+} MmcPartition;
 
 /* Functions */
 int mmc_scan_partitions();
@@ -79,6 +88,7 @@
 int mmc_mount_partition(const MmcPartition *partition, const char *mount_point, \
                         int read_only);
 int mmc_raw_copy (const MmcPartition *partition, char *in_file);
+int mmc_raw_dump (const MmcPartition *partition, char *out_file);
 
 #endif  // MMCUTILS_H_
 
diff --git a/nandroid.c b/nandroid.c
index b47a269..dae3713 100644
--- a/nandroid.c
+++ b/nandroid.c
@@ -34,6 +34,7 @@
 #include "amend/amend.h"
 
 #include "mtdutils/dump_image.h"
+#include "mmcutils/mmcutils.h"
 #include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
 #include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
 
@@ -42,7 +43,22 @@
 #include "extendedcommands.h"
 #include "nandroid.h"
 
-#ifndef BOARD_USES_BMLUTILS
+#ifdef BOARD_USES_BMLUTILS
+#elif BOARD_USES_MMCUTILS
+int write_raw_image(const char* partition, const char* filename) {
+    mmc_scan_partitions();
+    const MmcPartition *p;
+    p = mmc_find_partition_by_name(partition);
+    return mmc_raw_copy (p, filename);
+}
+
+int read_raw_image(const char* partition, const char* filename) {
+    mmc_scan_partitions();
+    const MmcPartition *p;
+    p = mmc_find_partition_by_name(partition);
+    return mmc_raw_dump (p, filename);
+}
+#else
 int write_raw_image(const char* partition, const char* filename) {
     char tmp[PATH_MAX];
     sprintf(tmp, "flash_image boot %s", filename);
diff --git a/roots.c b/roots.c
index c7bd16d..92b89a4 100644
--- a/roots.c
+++ b/roots.c
@@ -42,19 +42,19 @@
 static const char g_package_file[] = "@\0g_package_file";
 
 static RootInfo g_roots[] = {
-    { "BOOT:", g_mtd_device, NULL, "boot", NULL, g_raw, NULL },
+    { "BOOT:", DEFAULT_DEVICE, NULL, "boot", NULL, g_raw, NULL },
     { "CACHE:", CACHE_DEVICE, NULL, "cache", "/cache", CACHE_FILESYSTEM, CACHE_FILESYSTEM_OPTIONS },
     { "DATA:", DATA_DEVICE, NULL, "userdata", "/data", DATA_FILESYSTEM, DATA_FILESYSTEM_OPTIONS },
 #ifdef HAS_DATADATA
     { "DATADATA:", DATADATA_DEVICE, NULL, "datadata", "/datadata", DATADATA_FILESYSTEM, DATADATA_FILESYSTEM_OPTIONS },
 #endif
-    { "MISC:", g_mtd_device, NULL, "misc", NULL, g_raw, NULL },
+    { "MISC:", DEFAULT_DEVICE, NULL, "misc", NULL, g_raw, NULL },
     { "PACKAGE:", NULL, NULL, NULL, NULL, g_package_file, NULL },
-    { "RECOVERY:", g_mtd_device, NULL, "recovery", "/", g_raw, NULL },
+    { "RECOVERY:", DEFAULT_DEVICE, NULL, "recovery", "/", g_raw, NULL },
     { "SDCARD:", SDCARD_DEVICE_PRIMARY, SDCARD_DEVICE_SECONDARY, NULL, "/sdcard", "vfat", NULL },
     { "SDEXT:", SDEXT_DEVICE, NULL, NULL, "/sd-ext", SDEXT_FILESYSTEM, NULL },
     { "SYSTEM:", SYSTEM_DEVICE, NULL, "system", "/system", SYSTEM_FILESYSTEM, SYSTEM_FILESYSTEM_OPTIONS },
-    { "MBM:", g_mtd_device, NULL, "mbm", NULL, g_raw, NULL },
+    { "MBM:", DEFAULT_DEVICE, NULL, "mbm", NULL, g_raw, NULL },
     { "TMP:", NULL, NULL, NULL, "/tmp", NULL, NULL },
 };
 #define NUM_ROOTS (sizeof(g_roots) / sizeof(g_roots[0]))
@@ -255,6 +255,20 @@
                 info->filesystem, 0);
     }
 
+    if (info->device == g_mmc_device) {
+        if (info->partition_name == NULL) {
+            return -1;
+        }
+//TODO: make the mtd stuff scan once when it needs to
+        mmc_scan_partitions();
+        const MmcPartition *partition;
+        partition = mmc_find_partition_by_name(info->partition_name);
+        if (partition == NULL) {
+            return -1;
+        }
+        return mmc_mount_partition(partition, info->mount_point, 0);
+    }
+
     if (info->device == NULL || info->mount_point == NULL ||
         info->filesystem == NULL ||
         info->filesystem == g_raw ||
@@ -326,6 +340,19 @@
     return mtd_find_partition_by_name(info->partition_name);
 }
 
+const MmcPartition *
+get_root_mmc_partition(const char *root_path)
+{
+    const RootInfo *info = get_root_info_for_path(root_path);
+    if (info == NULL || info->device != g_mmc_device ||
+            info->partition_name == NULL)
+    {
+        return NULL;
+    }
+    mmc_scan_partitions();
+    return mmc_find_partition_by_name(info->partition_name);
+}
+
 int
 format_root_device(const char *root)
 {
@@ -348,7 +375,7 @@
         LOGW("format_root_device: can't resolve \"%s\"\n", root);
         return -1;
     }
-    if (info->mount_point != NULL && info->device == g_mtd_device) {
+    if (info->mount_point != NULL && (info->device == g_mtd_device || info->device == g_mmc_device)) {
         /* Don't try to format a mounted device.
          */
         int ret = ensure_root_path_unmounted(root);
@@ -398,9 +425,13 @@
             return -1;
         }
         if (!strcmp(info->filesystem, "ext3")) {
-            if(mmc_format_ext3(partition))
-                LOGE("\n\"%s\" wipe failed!\n", info->partition_name);
+            if(0 == mmc_format_ext3(partition))
+                return 0;
+            LOGE("\n\"%s\" wipe failed!\n", info->partition_name);
+            return -1;
         }
+        LOGW("\n\"%s\" wipe skipped!\n", info->partition_name);
+        return 0;
     }
 
     return format_non_mtd_device(root);
diff --git a/roots.h b/roots.h
index 5018e9f..ee78363 100644
--- a/roots.h
+++ b/roots.h
@@ -20,11 +20,14 @@
 
 #include "minzip/Zip.h"
 #include "mtdutils/mtdutils.h"
+#include "mmcutils/mmcutils.h"
 
-#ifndef BOARD_USES_BMLUTILS
+#ifndef BOARD_USES_MMCUTILS
 #define DEFAULT_DEVICE g_mtd_device
+#define DEFAULT_FILESYSTEM "yaffs2"
 #else
 #define DEFAULT_DEVICE g_mmc_device
+#define DEFAULT_FILESYSTEM "ext3"
 #endif
 
 #ifndef SDCARD_DEVICE_PRIMARY
@@ -48,7 +51,7 @@
 #endif
 
 #ifndef DATA_FILESYSTEM
-#define DATA_FILESYSTEM "yaffs2"
+#define DATA_FILESYSTEM DEFAULT_FILESYSTEM
 #endif
 
 #ifndef DATADATA_DEVICE
@@ -56,7 +59,7 @@
 #endif
 
 #ifndef DATADATA_FILESYSTEM
-#define DATADATA_FILESYSTEM "yaffs2"
+#define DATADATA_FILESYSTEM DEFAULT_FILESYSTEM
 #endif
 
 #ifndef CACHE_DEVICE
@@ -64,7 +67,7 @@
 #endif
 
 #ifndef CACHE_FILESYSTEM
-#define CACHE_FILESYSTEM "yaffs2"
+#define CACHE_FILESYSTEM DEFAULT_FILESYSTEM
 #endif
 
 #ifndef SYSTEM_DEVICE
@@ -72,7 +75,7 @@
 #endif
 
 #ifndef SYSTEM_FILESYSTEM
-#define SYSTEM_FILESYSTEM "yaffs2"
+#define SYSTEM_FILESYSTEM DEFAULT_FILESYSTEM
 #endif
 
 #ifndef DATA_FILESYSTEM_OPTIONS
@@ -126,6 +129,7 @@
 int ensure_root_path_unmounted(const char *root_path);
 
 const MtdPartition *get_root_mtd_partition(const char *root_path);
+const MmcPartition *get_root_mmc_partition(const char *root_path);
 
 /* "root" must be the exact name of the root; no relative path is permitted.
  * If the named root is mounted, this will attempt to unmount it first.