[Xperia Z5 Compact General] mhaha:
Why yet another guide? Why should you follow this guide?

This is more of a future reference for myself, but might be beneficial to some other people, hence why I decided to post it.

Thanks to the "dirty cow" exploit (CVE-2016-5195), we now have a more simple way of backing up the TA partition compared to existing guides, which all relied on another exploit that involved downgrading the firmware, thus making the whole process a bit more complicated.

Unfortunately, as you can probably tell, it's still not a painless ordeal, just "less" painful. But hey, at least you have a choice.

To keep this step-by-step walkthrough guide simple and straightforward, I am focusing on just the steps and skipping the explanations.

Let's get started!

Prerequisites

Important: Your phone must be vulnerable to the "dirty cow" exploit, any firmware version with security patch level 2016-12-01 or earlier should work, such as 32.2.A.5.11. Basically, any MM or LP firmware.

Currently there is no way of backing up the TA partition on Android 7.0 Nougat firmware, you will need to downgrade to MM or earlier firmware first.

Remember to backup all your data before you downgrade, since a downgrade is akin to a factory reset. Downgrade instructions:

Follow Step 0, skip Step 1 and 2

In Step 3, download a MM or LP version firmware. For the sake of simplicity, I suggest you download the one labeled Storefront.

Follow Step 4 and 5

Done, your phone is now downgraded, skip the remaining steps. Continue with the guide as usual from Step 1

Note: If the DRM keys are irrelevant to you (TA partition already backed up/restored/lost) and would just like to root, then you can root using this guide on all firmware versions including Android 7.0 Nougat without downgrading. In this case, you can skip Step 1 and Step 7. Depending on your situation, you can also skip other parts of this guide, such as Step 2 if your bootloader is already unlocked, or in Step 5 don't wipe anything (leave all boxes unchecked) to retain your apps and data for a firmware upgrade.

Computer setup with correct drivers and adb/fastboot connection ability.

Enable USB debugging on the phone:

Settings > About phone > Tap multiple times on Build number until Developer options is enabled

Settings > Developer options > USB debugging > Toggle ON

XperiFirm for downloading official Sony firmware directly from Sony's servers

Flashtool for flashing firmware

Battery preferably more than 80% remaining, loss of power while flashing can brick your phone

Step 0

- Backup your phone (optional)

Move all your data to your SD card using the native tool: Settings > Storage > Transfer data to SD card

Use Sony's Backup & restore app to make a backup: Settings > Backup & reset > Xperia Backup & restore > More > Manual backup > Select SD card > Select content to backup

Personally, I choose everything except apps, they can be re-downloaded from the Play Store at any time, plus they take a long time to backup and restore not to mention eats up free space

Step 1

- Backup the TA partition

Download this wonderful TA Backup tool, unzip to desired location

With the phone powered on, connect the USB cable to the computer, then plug the micro USB end to the phone

Open command prompt (Start > Run > cmd), navigate to the directory where you unzipped the TA Backup tool

Run the following command:

backupTA

The tool will run and if successful, you should see at the bottom the words "TA Successfully pulled to TA_E5823.img".

TA_E5823.img is the TA partition image file, actual filename will be longer and differ for everyone so to keep it simple we'll just refer to it as TA_E5823.img in this guide.

Please check that the file is

2,097,152 bytes

. If not or file is missing, then the backup has failed.

If it fails, just run the tool again a few times, or try rebooting your phone then rerun the tool. If you just can't get it to work, seek help from the tool thread.

Step 2

- Unlock the boot loader

Visit Sony's website and follow the instructions carefully

Important: Pay attention to the warnings. No, really, unlocking the boot loader will wipe your DRM keys (hence the need to back them up in Step 1) and trigger a factory reset, erasing everything on your phone.

After you've successfully unlocked the boot loader, unplug your phone and leave it powered off

Step 3

- Download stock firmware

Run XperiFirm, locate the correct Xperia Z5 Compact version of your phone (E5803/E5823) and download the firmware of your choice.

General recommendation is to download the "Customized" version that's available for your country.

Step 4

- Create a FTF file from the firmware

Open Flashtool

Tools > Bundle > Create

Select source file > Locate the folder where you saved the firmware from XperiFirm in step 3 > You should see your device name appear automatically in Device and the folder list below populated

Branding > The same as Operator in XperiFirm, e.g. Customized AU

Version > The version you downloaded, e.g. 32.2.A.5.11

Hint: Both Branding and Version information is in the folder name

In folder list, select the first item, hold SHIFT and select the last item so that all items are highlighted, then click the "- >" button > The folder list should now be empty, and the Firmware content field populated

Click Create, if successful you will see "Bundle creation finished"

Note: The FTF file is saved to %userprofile%\.flashTool\firmwares by default

Step 5

- Flash the FTF file

Click the lightning symbol in Flashtool > Flashmode

Source folder should be pre-selected to the default location mentioned above and "Sony Xperia Z5 Compact" listed in Firmwares

Expand all the arrows and select the version number

Under Wipe/Sin check all options (APPS_LOG, DIAG, SSD, USERDATA), leave all other options unchecked

Click Flash, wait for an instruction prompt to pop up

Plug the USB cable to your computer if it isn't already plugged

Turn off your phone if it isn't powered off, hold the VOLUME DOWN button while connecting the micro USB end to your phone. Keep the VOL DOWN button held until the prompt disappears, which indicates that Flashtool has detected it and is correctly in flashmode.

Flashtool will begin flashing the firmware automatically. It can take a long time, 10~15 minutes is normal, wait until completed

Unplug your phone and do not power it on yet

Step 6

- Patch the kernel

Download this awesome rootkernel tool, unzip to desired location.

Extract the kernel image file kernel.sin from the FTF file using any file compression program such as 7zip, WinZip, WinRAR, etc.

Open Flashtool > Tools > Sin Editor > Sin file > Locate the kernel you just extracted > Click Extract data. You should now have a file named kernel.elf in the same folder

Copy kernel.elf to the rootkernel folder

Download the latest stable version of the SuperSU ZIP file and copy the entire ZIP file to the rootkernel folder. Do NOT unzip it!

Rename the SuperSU ZIP file name so that it starts with SuperSU (case sensitive) instead of UPDATE-SuperSU, e.g. SuperSU-v2.79-20161211114519.zip

Note: if you're patching Nougat firmware, you may want to use phh's superuser instead of SuperSU due to potential battery drain. See rootkernel thread for more info. If you use phh's superuser, you will need to install the apk from the Play store after Step 8.

(optional) Download the latest Xposed framework ZIP file and copy it to the rootkernel folder. Do NOT unzip it!

Note: this only works with rootkernel v5.0 or later, if you're using an earlier version of the rootkernel tool, skip this and skip Step 9

At time of this edit (2017/2/22), Xposed does not support Android 7.0 (Nougat), so if you're trying to patch a Nougat kernel, do not include xposed. You can check the official Xposed thread for latest announcements to see if it's supported

Open command prompt and navigate to the rootkernel directory

Enter the following command:

rootkernel kernel.elf boot.img

You will be prompted to make a series of choices, including whether you want to install SuperSU and Xposed. Type Y for all of them.

If you didn't see the prompt for SuperSU (required) or Xposed (optional), check the filename of the ZIP files. Remember they're case sensitive.

You should now have a boot.img file in the rootkernel folder, that is your rooted kernel

Step 7

- Flash the DRM keys (one-time procedure)

Copy the TA backup file TA_E5823.img from Step 1 to the rootkernel folder, then enter the following command in command prompt:

flash_dk TA_E5823.img DK.ftf

This will create a DK.ftf file in the rootkernel folder

Open Flashtool > Click the lightning symbol > Flashmode > Source folder > Locate the rootkernel folder

"Sony Xperia Z5 Compact" should appear under Firmwares, expand the arrows until you see DeviceKey and 1.0. Select 1.0, click Flash and wait for an instruction prompt to pop up

Hold the VOLUME DOWN button while connecting the micro USB end to your phone. Keep the VOL DOWN button held until the prompt disappears, which indicates that Flashtool has detected it and is correctly in flashmode.

Wait until operation is completed, then unplug the cable from your phone. Do not power on yet

Step 8

- Flash the kernel

Hold the VOLUME UP button and plug in the micro USB cable. Wait until the LED indicator turns blue, then release the VOL UP button.

Enter the following command in command prompt to flash the rooted kernel:

fastboot flash boot boot.img

After completion, congratulations, you've successfully rooted your phone!

Now you can turn on your phone, the first boot will take a very long time to initialize, don't worry.

If you see the boot animation for more than 30 minutes, then it's time to worry. First try again from Step 6, and if you still can't complete boot, go up a step starting from Step 5. If that still fails, start over from Step 3.

Step 9

- Finish installation of Xposed (optional)

In Step 6, if you chose to include the Xposed ZIP file, which means you want to install Xposed, then there's one more step to complete the installation.

Download the unofficial Material Design Xposed Installer.

Currently, this is the only Xposed Installer that works with the systemless Xposed integration method used by the rootkernel tool. Using the official Xposed Installer will not work.

Go to Settings > Security > Unknown sources > Toggle ON

Copy the Xposed Installer apk to your phone and install it.

Done!

Personal list of xposed modules to install for self reference. This is in no way a recommendation list, your preferences will vary.

Amplify

Android Phone Vibrator

App Locale

BootManager

Disable Low Battery Notifications

DynamicAlarmIcon 2

HideBatteryLowAlert

Keep Trash (broken at the moment)

Lockscreen Album Art Remover

MinMinGuard

NeoPowerMenu

NoHeadsetNotification

Ringer and Notification Volume Unlink

RootCloak

Use USB for Marshmallow V 1.2

XperiaFMwoHS

Xposed Torch: Physical keys

Youtube AdAway

Bonus - How to unroot/fully restore DRM/return to 100% factory state