Add in ext4 support for ASEC containers

Now forward locked applications will be in ASEC containers both internal
to the system and externally.

This change adds support for putting applications in ext4-based ASECs.

Change-Id: I8d6765b72dd2606e429c067b47a2dbcaa8bef37d
diff --git a/Loop.cpp b/Loop.cpp
index dad2c3f..78df132 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <string.h>
 
+#include <sys/mount.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
@@ -33,6 +34,7 @@
 
 #include <sysutils/SocketClient.h>
 #include "Loop.h"
+#include "Asec.h"
 
 int Loop::dumpState(SocketClient *c) {
     int i;
@@ -246,3 +248,45 @@
     close(fd);
     return 0;
 }
+
+int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
+    int fd;
+    struct asec_superblock buffer;
+
+    if ((fd = open(loopDevice, O_RDONLY)) < 0) {
+        SLOGE("Failed to open loopdevice (%s)", strerror(errno));
+        destroyByDevice(loopDevice);
+        return -1;
+    }
+
+    if (ioctl(fd, BLKGETSIZE, nr_sec)) {
+        SLOGE("Failed to get loop size (%s)", strerror(errno));
+        destroyByDevice(loopDevice);
+        close(fd);
+        return -1;
+    }
+
+    /*
+     * Try to read superblock.
+     */
+    memset(&buffer, 0, sizeof(struct asec_superblock));
+    if (lseek(fd, ((*nr_sec - 1) * 512), SEEK_SET) < 0) {
+        SLOGE("lseek failed (%s)", strerror(errno));
+        close(fd);
+        destroyByDevice(loopDevice);
+        return -1;
+    }
+    if (read(fd, &buffer, sizeof(struct asec_superblock)) != sizeof(struct asec_superblock)) {
+        SLOGE("superblock read failed (%s)", strerror(errno));
+        close(fd);
+        destroyByDevice(loopDevice);
+        return -1;
+    }
+    close(fd);
+
+    /*
+     * Superblock successfully read. Copy to caller's struct.
+     */
+    memcpy(sb, &buffer, sizeof(struct asec_superblock));
+    return 0;
+}