[Xiaomi Redmi Note 3 Questions & Answers] jsteed:
Hi,

I am currently building a CM 12.1 ROM for the Redmi Note 3, and most of the stuff works, but I have problems getting the device encryption to work, and was hoping somebody could help me figure this out.

Device encryption is built-in, but enabling it leads to an error. Specifically, when telling Android to encrypt the device (i.e., selecting "Encrypt phone" in the settings), it starts the process, but after less than one minute shows a screen saying that the encryption failed, and that the phone needs to be factory reset.

The logcat shows the following output:

05-15 19:47:49.694 I/Cryptfs ( 254): Using scrypt for cryptfs KDF
05-15 19:47:50.680 I/Cryptfs ( 254): Enabling support for allow_discards in dmcrypt.
05-15 19:47:50.680 I/Cryptfs ( 254): load_crypto_mapping_table: target_type = crypt
05-15 19:47:50.680 I/Cryptfs ( 254): load_crypto_mapping_table: real_blk_name = /dev/block/platform/mtk-msdc.0/by-name/userdata, extra_params = 1 allow_discards
05-15 19:47:55.706 E/Cryptfs ( 254): Cannot load dm-crypt mapping table.
05-15 19:47:55.707 I/Cryptfs ( 254): Encrypting ext4 filesystem in place...
05-15 19:47:55.707 I/Cryptfs ( 254): Encrypting group 0
05-15 19:47:55.710 E/Cryptfs ( 254): Error writing crypto_blkdev /dev/block/dm-0 for inplace encrypt
05-15 19:47:55.710 I/Cryptfs ( 254): Encrypted to sector 0
05-15 19:47:55.710 E/Cryptfs ( 254): Error encrypting groups
05-15 19:47:55.710 D/Cryptfs ( 254): cryptfs_enable_inplace_ext4()=-1
05-15 19:47:55.710 E/Cryptfs ( 254): Failed to encrypt f2fs filesystem on /dev/block/platform/mtk-msdc.0/by-name/userdata
05-15 19:47:55.710 I/Cryptfs ( 254): Encrypted to block -1
05-15 19:47:55.710 D/Cryptfs ( 254): cryptfs_enable_inplace_f2fs()=-1
05-15 19:47:55.710 E/Cryptfs ( 254): Encrypting filesystem in place...
05-15 19:47:55.710 E/Cryptfs ( 254): Cannot seek to previously encrypted point on /dev/block/dm-0
05-15 19:47:55.710 D/Cryptfs ( 254): cryptfs_enable_inplace_full()=-1

with the most pertinent line probably this one:

05-15 19:47:55.706 E/Cryptfs ( 254): Cannot load dm-crypt mapping table.

At the same time, the kernel log prints the following:

<3>[ 1196.971648] (3)[324:vold]device-mapper: table: 253:0: crypt: Device lookup failed
<4>[ 1196.971659] (3)[324:vold]device-mapper: ioctl: error adding target to table

I have traced this problem through the source code, and I suspect it is somewhere in dm-crypt.c of the Linux kernel, in the crypt_ctr() function:

if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
ti->error = "Device lookup failed";
goto bad;
}

Presumably, dm_get_device() fails for some reason (of course, I might be mistaken and the problem is somewhere else, but tracing the function calls in the source code seems to suggest that this is where the error comes from).

Unfortunately, the kernel sources for the device in question are not available, so I cannot perform any more detailed testing on the kernel.

An additional piece of the puzzle is that there is an AOSP 5.0 ROM for the same device, which uses exactly the same kernel (as in, the same binary), and there the phone encryption works. Unfortunately, I am not familiar enough with dm-crypt, the device mapper and other related things around it to figure out what the difference is, and why it fails on CM-12.1 (e.g., whether there is for example any device mapper configuration somewhere that could be different, etc.).

Even more strangely, if I encrypt the user partition on the AOSP ROM, then use TWRP recovery to flash my CM-12.1 ROM, wipe the data partition (which does not remove encryption, as TWRP can correctly decrypt and mount the data partition), then my CM-12.1 ROM boots normally and decrypts the data partition (presumably then not having a problem with the device-mapper).

I have been banging my head against this problem for a bit too long, so if anybody has any ideas or knows that the problem could be and how to solve it, that would be very much appreciated.