[Xperia Z4/Z3+ Android Development] DiscMan55:
Hello xda forums,

After a lot of experimentation I have been able to get TWRP working with /data decryption support (compatible with stock Sony ROM encryption) using LineageOS 14.1 sources. It is able to mount encrypted stock Sony ROM /data and install Magisk on it.

Credits / Special Thanks:

- LineageOS and [MENTION=513620]Quarx[/MENTION], [MENTION=4329876]Olivier[/MENTION], [MENTION=3506409]cdesai[/MENTION], Myself5, kali-, banmeifyouwant for android device ivy source tree (which I forked)

- [MENTION=6979267]nailyk[/MENTION] for help and ideas from effort on Sony Z3

- AndroPlus Kernel for providing ideas on things to try.

- [MENTION=1162986]Captain_Throwback[/MENTION] for help with hw crypto.

Warning:

Do not enable "Encrypt my phone" if you are running Magisk already on stock ROM! Encryption will hang because android will not be able to unmount /data to encrypt it and you will be forced to reboot and then you will be forced to wipe /data. If you plan to use Magisk, first uninstall it completely from recovery, THEN encrypt your phone, THEN install Magisk and whatever modules you want.

Developed on Sony E6533 (Z3+ Dual) but should work on both variants due to the efforts of [MENTION=513620]Quarx[/MENTION] in unifying the device tree. Built from LineageOS 14.1 android ivy device tree.

How to install:

- Power off completely, then hold volume up key, then plug in USB to PC. If done correctly the front LED will light up BLUE.

- Make sure fastboot drivers are installed. If you have generic android drivers, you can install driver "Android bootloader interface" manually for the "S1Boot" entry in device manager on windows when phone is in fastboot mode.

- Download attachment and unzip it to get the img file.

- fastboot flash FOTAKernel recovery.img

How to enter:

- Power off completely.

- Hold volume down key, then push power also.

- Do not release volume down and power until sony logo appears.

- It will enter recovery flashed in FOTAKernel partition.

Help wanted:

- Testing all scenarios on encrypted /data such as backup / restore.

- Charging LED does not work (I don't know the changes needed to make it work in recovery).

Known issues:

- Battery charge indicator only updates once every minute but charging is working fine.

- Reboot to bootloader from within TWRP doesn't work (seems to be Sony bootloader issue, will remove in next build) (use power off, then hold vol. up + plug in usb instead).

- Reboot to recovery from within TWRP doesn't always work (sony bootloader issue, fixed by updating to sony nougat bootloader). (use power off, then hold vol. down + power until sony logo instead).

- "Hotbooting" (ie: fastboot boot recovery.img) this recovery does not work. Phone will hang on Sony logo and heat up and require hard power off using power off button inside side flap (or press Power + Vol Up + Vol Down simultaneously for 3 seconds).

Source:

Device tree: https://github.com/rwasef1830/android_device_sony_ivy/tree/cm-14.1-twrp

TWRP: https://github.com/omnirom/android_bootable_recovery/ (cherry pick https://gerrit.omnirom.org/#/c/22096/ thanks to [MENTION=1162986]Captain_Throwback[/MENTION])