Add the ability to revert a crypto mapping when unmounting a volume

Add the force_and_revert option to the unmount command which will force
the unmount, and revert a crypto mapping.  This is used during factory
reset so that when the internal sdcard volume is formatted, it formats
the raw device, not the encrypted mapping.

Change-Id: I36b6ff9bb54863b121de635472a303bf4a2334a9
diff --git a/DirectVolume.cpp b/DirectVolume.cpp
index 2ddd5ab..4acee76 100644
--- a/DirectVolume.cpp
+++ b/DirectVolume.cpp
@@ -317,7 +317,7 @@
             SLOGE("Failed to cleanup ASEC - unmount will probably fail!");
         }
 
-        if (Volume::unmountVol(true)) {
+        if (Volume::unmountVol(true, false)) {
             SLOGE("Failed to unmount volume on bad removal (%s)", 
                  strerror(errno));
             // XXX: At this point we're screwed for now
@@ -392,6 +392,16 @@
     mPaths->erase(it); /* Remove it from the list */
     addPath(new_path); /* Put the new path on the list */
 
+    /* Save away original info so we can restore it when doing factory reset.
+     * Then, when doing the format, it will format the original device in the
+     * clear, otherwise it just formats the encrypted device which is not
+     * readable when the device boots unencrypted after the reset.
+     */
+    mOrigDiskMajor = mDiskMajor;
+    mOrigDiskMinor = mDiskMinor;
+    mOrigPartIdx = mPartIdx;
+    memcpy(mOrigPartMinors, mPartMinors, sizeof(mPartMinors));
+
     mDiskMajor = new_major;
     mDiskMinor = new_minor;
     /* Ugh, virual block devices don't use minor 0 for whole disk and minor > 0 for
@@ -410,6 +420,24 @@
 }
 
 /*
+ * Called from base to revert device info to the way it was before a
+ * crypto mapping was created for it.
+ */
+void DirectVolume::revertDeviceInfo(void)
+{
+    if (mIsDecrypted) {
+        mDiskMajor = mOrigDiskMajor;
+        mDiskMinor = mOrigDiskMinor;
+        mPartIdx = mOrigPartIdx;
+        memcpy(mPartMinors, mOrigPartMinors, sizeof(mPartMinors));
+
+        mIsDecrypted = 0;
+    }
+
+    return;
+}
+
+/*
  * Called from base to give cryptfs all the info it needs to encrypt eligible volumes
  */
 int DirectVolume::getVolInfo(struct volume_info *v)