From 6a74dcaa6e646fea8e00b7c04332fc60fe7e017c Mon Sep 17 00:00:00 2001 From: Daniel Rosenberg Date: Fri, 23 May 2014 13:47:00 -0700 Subject: [PATCH] Fixed bugs with ASEC filesystem. Changed ext4 to be 4kb aligned, and fat to be 32kb aligned. Fixed issue that could potentially cause unencrypted ext4 ASECS to overwrite the ASEC super block when filled. Change-Id: I890426c82ac9cbc65add85a8e3f5063504193c31 Signed-off-by: Daniel Rosenberg --- Ext4.cpp | 17 ++++++++++++++--- Ext4.h | 2 +- VolumeManager.cpp | 17 ++++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Ext4.cpp b/Ext4.cpp index b82b4c5..acf1777 100644 --- a/Ext4.cpp +++ b/Ext4.cpp @@ -67,9 +67,9 @@ int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remo return rc; } -int Ext4::format(const char *fsPath, const char *mountpoint) { +int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) { int fd; - const char *args[5]; + const char *args[7]; int rc; int status; @@ -77,7 +77,18 @@ int Ext4::format(const char *fsPath, const char *mountpoint) { args[1] = "-J"; args[2] = "-a"; args[3] = mountpoint; - args[4] = fsPath; + if (numSectors) { + char tmp[32]; + snprintf(tmp, sizeof(tmp), "%u", numSectors * 512); + const char *size = tmp; + args[4] = "-l"; + args[5] = size; + args[6] = fsPath; + rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false, true); + } else { + args[4] = fsPath; + rc = android_fork_execvp(5, (char **)args, &status, false, true); + } rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false, true); if (rc != 0) { diff --git a/Ext4.h b/Ext4.h index c5ab78a..54fbec3 100644 --- a/Ext4.h +++ b/Ext4.h @@ -23,7 +23,7 @@ class Ext4 { public: static int doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount, bool executable); - static int format(const char *fsPath, const char *mountpoint); + static int format(const char *fsPath, unsigned int numSectors, const char *mountpoint); }; #endif diff --git a/VolumeManager.cpp b/VolumeManager.cpp index 2049706..9889653 100644 --- a/VolumeManager.cpp +++ b/VolumeManager.cpp @@ -54,6 +54,9 @@ #define MASS_STORAGE_FILE_PATH "/sys/class/android_usb/android0/f_mass_storage/lun/file" +#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\ + + (number & (~((1U << po2) - 1)))) + VolumeManager *VolumeManager::sInstance = NULL; VolumeManager *VolumeManager::Instance() { @@ -332,13 +335,17 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors, const cha /* * Add some headroom + * This is likely off by a bit, and we may want to do something different for ext4 */ unsigned fatSize = (((numSectors * 4) / 512) + 1) * 2; unsigned numImgSectors = numSectors + fatSize + 2; - - if (numImgSectors % 63) { - numImgSectors += (63 - (numImgSectors % 63)); - } + /* + * ext4 is aligned to 4kb. fat is aligned to 32kb. Sectors are 512b. + */ + if (usingExt4) + numImgSectors = ROUND_UP_POWER_OF_2(numImgSectors, 3); + else + numImgSectors = ROUND_UP_POWER_OF_2(numImgSectors, 6); // Add +1 for our superblock which is at the end if (Loop::createImageFile(asecFileName, numImgSectors + 1)) { @@ -433,7 +440,7 @@ int VolumeManager::createAsec(const char *id, unsigned int numSectors, const cha } if (usingExt4) { - formatStatus = Ext4::format(dmDevice, mountPoint); + formatStatus = Ext4::format(dmDevice, numImgSectors, mountPoint); } else { formatStatus = Fat::format(dmDevice, numImgSectors, 0); } -- 2.11.0