Summary
I started to investigate the issue and got some debugging information and a possible solution. I think, the issue is a bug caused by wiping the userdata partition (more precisely by formatting it) and the solution is to do a factory reset or explicitly flash the userdata image in addition to the other images (see below for more details).
Cause of the issue
First of all, in adb logcat
the following two lines are relevant for the erroneous encryption process:
E/Cryptfs ( 236): Bad magic for real block device /dev/block/platform/msm_sdcc.1/by-name/userdata
E/Cryptfs ( 236): Orig filesystem overlaps crypto footer region. Cannot encrypt in place.
An internet search brought a blog post which linked to this xda forum topic. According to these sources, the problem probably is as follows: For the encryption to work, you need some free space at the end of the userdata partition right behind the userdata filesystem (the “crypto footer” from the adb error message, see this technical blog post for more information about it in general). However, when flashing another ROM, the userdata partition gets wiped, reformatted and equipped with a new filesystem. It seems that in this process no crypto footer is left and the filesystem fills the whole partition (i.e. it “overlaps crypto footer” as described in the adb error message).
CAUTION: The xda thread provides two zip files and suggests to flash them to solve the problem. I do not advice to flash these zips, as it’s not clear how trustworthy they are and they might contain malware/viruses. However, note that this is just precaution (due to the small amount of answers and userbase over there; also I don’t see the source code documented) - I have no evidence and do not claim that the zip files contain stuff they shouldn’t! If you like to try it out, go ahead, but do it on your own risk (as with all the other stuff you flash or install on your phone from some random source/website ). Anyway, there is a more natural solution below.
Usual flashing methods don’t work - indeed they’re rather causing the problem
As wiping the userdata partition seems to cause the issue, I tried out different flashing scenarios. Essentially, there are two common flashing procedures (the -w
in brackets means you can insert it or leave it out):
-
Flash all images at once: fastboot [-w] flashall
-
Flash boot, recovery and system image sequentially:
fastboot flash boot boot.img
fastboot flash recovery recovery.img
fastboot [-w] flash system system.img
fastboot flashall
flashes the boot, recovery and system images (erasing the system image before). The -w
option (in both cases) does the following with both the userdata and cache partition: it erases the image, then formats the partition and finally flashes the new image. And it seems as if this wiping process causes the problem. Don’t ask me why.
Note: Once the problem occurs, flashing a new ROM (more precisely: a new system image) without touching the userdata partition (i.e. without wiping or flashing a userdata image) won’t change anything concerning the issue. As in that case all user data is preserved, this behaviour completely makes sense - remember that encrypting the phone only encrypts the userdata partition! Therefore, to solve the issue one definitely needs to do something with the userdata partition.
Solution / Workaround
As wiping didn’t solve but rather cause the issue in the first place, I tried another variation and flashed the userdata image as well:
fastboot flash boot boot.img
fastboot flash recovery recovery.img
fastboot [-w] flash system system.img
fastboot flash userdata userdata.img
And now it worked! Note that it didn’t matter whether I used the -w
option or not before flashing the userdata image.
Moreover, if you don’t want to flash the whole ROM again, you can also just
- do a factory reset via
Settings --> Backup & reset --> Factory data reset
- or only flash the userdata image
fastboot flash userdata userdata.img
to solve the issue. But remember to carefully backup your apps and data before and to make a plan for restoring it!
I really cannot explain why this works in contrast to fastboot -w flash system system.img
- I guess it’s the formatting and flashing process that is somehow responsible - but I don’t know why.
Anyway, it works this way, I can reproduce it and I hope it will help you as well!