More fixes for internal FAT partitions:

Fix formatting partitions beyond the first partition.
Do not try to initialize the MBR when formatting only a single partition.

Change-Id: Ifbbd279b1c288b7b1b884a1a89248e3086ed735f
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/Volume.cpp b/Volume.cpp
index 1577ba9..d2b87b6 100644
--- a/Volume.cpp
+++ b/Volume.cpp
@@ -110,6 +110,7 @@
     mMountpoint = strdup(mount_point);
     mState = Volume::State_Init;
     mCurrentlyMountedKdev = -1;
+    mPartIdx = -1;
 }
 
 Volume::~Volume() {
@@ -210,28 +211,31 @@
         return -1;
     }
 
+    bool formatEntireDevice = (mPartIdx == -1);
     char devicePath[255];
     dev_t diskNode = getDiskDevice();
-    dev_t partNode = MKDEV(MAJOR(diskNode), 1); // XXX: Hmmm
+    dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx));
 
-    sprintf(devicePath, "/dev/block/vold/%d:%d",
-            MAJOR(diskNode), MINOR(diskNode));
-
-    if (mDebug) {
-        SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
-    }
     setState(Volume::State_Formatting);
 
-    if (initializeMbr(devicePath)) {
-        SLOGE("Failed to initialize MBR (%s)", strerror(errno));
-//        goto err;
-// don't treat this as a fatal error
-// lets continue on and format the partition
+    // Only initialize the MBR if we are formatting the entire device
+    if (formatEntireDevice) {
+        sprintf(devicePath, "/dev/block/vold/%d:%d",
+                MAJOR(diskNode), MINOR(diskNode));
+
+        if (initializeMbr(devicePath)) {
+            SLOGE("Failed to initialize MBR (%s)", strerror(errno));
+            goto err;
+        }
     }
 
     sprintf(devicePath, "/dev/block/vold/%d:%d",
             MAJOR(partNode), MINOR(partNode));
 
+    if (mDebug) {
+        SLOGI("Formatting volume %s (%s)", getLabel(), devicePath);
+    }
+
     if (Fat::format(devicePath, 0)) {
         SLOGE("Failed to format (%s)", strerror(errno));
         goto err;
@@ -582,7 +586,6 @@
     setState(Volume::State_NoMedia);
     return -1;
 }
-
 int Volume::initializeMbr(const char *deviceNode) {
     struct disk_info dinfo;