Skip to content

Commit

Permalink
Adopted Storage support
Browse files Browse the repository at this point in the history
 -Detects, decrypts, and mounts an adopted SD card if a
  secondary block device is defined (usually mmcblk1)
 -Handles unified storage
 -Displays the adopted storage in MTP along with internal
 -Factory Reset - wiped just like a data media device, we
  retain the keys folder and the storage.xml during a
  factory reset
 -Backup / Restore
 -Disable mass storage when adopted storage is present
 -Read storage nickname from storage.xml and apply it to
  display names in the GUI
 -Read storage.xml and determine what storage location is in
  use for /sdcard and remap accordingly

libgpt_twrp is source code mostly kanged from an efimanager
project. It is GPL v2 or higher, so we will opt for GPL v3.

Change-Id: Ieda0030bec5155ba8d2b9167dc0016cebbf39d55
  • Loading branch information
Dees-Troy committed Jan 25, 2016
1 parent 56a7a99 commit 66a1949
Show file tree
Hide file tree
Showing 18 changed files with 1,427 additions and 53 deletions.
3 changes: 2 additions & 1 deletion Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ ifeq ($(TW_INCLUDE_L_CRYPTO), true)
endif
ifeq ($(TW_INCLUDE_CRYPTO), true)
LOCAL_CFLAGS += -DTW_INCLUDE_CRYPTO
LOCAL_SHARED_LIBRARIES += libcryptfslollipop
LOCAL_SHARED_LIBRARIES += libcryptfslollipop libgpt_twrp
LOCAL_C_INCLUDES += external/boringssl/src/include
endif
ifeq ($(TW_USE_MODEL_HARDWARE_ID_FOR_DEVICE_ID), true)
Expand Down Expand Up @@ -592,6 +592,7 @@ include $(commands_recovery_local_path)/injecttwrp/Android.mk \
ifeq ($(TW_INCLUDE_CRYPTO), true)
include $(commands_recovery_local_path)/crypto/lollipop/Android.mk
include $(commands_recovery_local_path)/crypto/scrypt/Android.mk
include $(commands_recovery_local_path)/gpt/Android.mk
endif
ifeq ($(BUILD_ID), GINGERBREAD)
TW_NO_EXFAT := true
Expand Down
45 changes: 44 additions & 1 deletion crypto/lollipop/cryptfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ static int load_crypto_mapping_table(struct crypt_mnt_ftr *crypt_ftr, unsigned c
if (! ioctl(fd, DM_TABLE_LOAD, io)) {
break;
}
printf("%i\n", errno);
usleep(500000);
}

Expand Down Expand Up @@ -1145,7 +1146,7 @@ static int create_crypto_blk_dev(struct crypt_mnt_ftr *crypt_ftr, unsigned char

ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
if (ioctl(fd, DM_DEV_CREATE, io)) {
printf("Cannot create dm-crypt device\n");
printf("Cannot create dm-crypt device %i\n", errno);
goto errout;
}

Expand Down Expand Up @@ -2017,3 +2018,45 @@ int cryptfs_get_password_type(void)

return crypt_ftr.crypt_type;
}

/*
* Called by vold when it's asked to mount an encrypted external
* storage volume. The incoming partition has no crypto header/footer,
* as any metadata is been stored in a separate, small partition.
*
* out_crypto_blkdev must be MAXPATHLEN.
*/
int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
const unsigned char* key, int keysize, char* out_crypto_blkdev) {
int fd = open(real_blkdev, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
printf("Failed to open %s: %s", real_blkdev, strerror(errno));
return -1;
}

unsigned long nr_sec = 0;
nr_sec = get_blkdev_size(fd);
close(fd);

if (nr_sec == 0) {
printf("Failed to get size of %s: %s", real_blkdev, strerror(errno));
return -1;
}

struct crypt_mnt_ftr ext_crypt_ftr;
memset(&ext_crypt_ftr, 0, sizeof(ext_crypt_ftr));
ext_crypt_ftr.fs_size = nr_sec;
ext_crypt_ftr.keysize = keysize;
strcpy((char*) ext_crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256");

return create_crypto_blk_dev(&ext_crypt_ftr, key, real_blkdev,
out_crypto_blkdev, label);
}

/*
* Called by vold when it's asked to unmount an encrypted external
* storage volume.
*/
int cryptfs_revert_ext_volume(const char* label) {
return delete_crypto_blk_dev((char*) label);
}
3 changes: 3 additions & 0 deletions crypto/lollipop/cryptfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ extern "C" {
int cryptfs_verify_passwd(char *newpw);
int cryptfs_get_password_type(void);
int delete_crypto_blk_dev(char *name);
int cryptfs_setup_ext_volume(const char* label, const char* real_blkdev,
const unsigned char* key, int keysize, char* out_crypto_blkdev);
int cryptfs_revert_ext_volume(const char* label);
#ifdef __cplusplus
}
#endif
4 changes: 3 additions & 1 deletion data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ void DataManager::SetDefaultValues()
mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "0"));
} else {
LOGINFO("Lun file '%s'\n", Lun_File_str.c_str());
mConstValues.insert(make_pair(TW_HAS_USB_STORAGE, "1"));
mValues.insert(make_pair(TW_HAS_USB_STORAGE, make_pair("1", 0)));
}
#endif
#ifdef TW_INCLUDE_INJECTTWRP
Expand Down Expand Up @@ -923,6 +923,8 @@ void DataManager::SetDefaultValues()
mValues.insert(make_pair("tw_language", make_pair(EXPAND(TW_DEFAULT_LANGUAGE), 1)));
LOGINFO("LANG: %s\n", EXPAND(TW_DEFAULT_LANGUAGE));

mValues.insert(make_pair("tw_has_adopted_storage", make_pair("0", 0)));

pthread_mutex_unlock(&m_valuesLock);
}

Expand Down
15 changes: 15 additions & 0 deletions gpt/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LOCAL_PATH := $(call my-dir)

# Build libgpt_twrp library

include $(CLEAR_VARS)
LOCAL_CLANG := false
LOCAL_MODULE := libgpt_twrp
LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES = \
gpt.c \
gptcrc32.c

LOCAL_SHARED_LIBRARIES := libc
include $(BUILD_SHARED_LIBRARY)
Loading

0 comments on commit 66a1949

Please sign in to comment.