Reinstall stock firmware on a Sony Xperia SP
This is a checklist for reinstalling Sony firmware on the Sony Xperia SP (Huashan) Android phone – model C5303. Much of this guide will work for other Xperia SP models but I have only linked the FTF file (Sony firmware) for the C5303.
This is not a guide for dummies – it assumes that the reader is familiar with the concepts of flashing mobile phones but needs a check-list for the Xperia SP.
This procedure is intended for phones that are either playing dead or you wish to restore to stock firmware for any reason. It assumes you have no (Nandroid) backups of the phone’s system and boot partitions. If you do have backups, then you might be able to skip all this grief and find a tutorial on flashing TWRP directly to your boot partition, and from there restore your boot and system partitions from your TWRP backups. If you have the tools however, I would still use this method. It takes me about 5 minutes to reflash an Xperia SP to stock firmware using Flashtool and then I at least know that all the partitions have been formatted correctly and are read-write. If you read the known issues below, you will see that there are some problems that can arise with filestore permissions that cannot be easily fixed by TWRP (or CWM) restores.
This procedure requires a PC – I’m using Windows 10 64-bit with 4Gb RAM. I’ve done this with <2Gb RAM – the system requirement is not high.
I found many things did not work smoothly during this process and took some time to solve, so best not forget these:
- The phone fails to enter Flashmode (green LED) when plugged in with volume down button pressed. Sometimes this is accompanied by a red LED lighting up when plugged in, or 2 red LED pulses when attempting to power on the phone with the power button. This is usually indicative of a flat battery. Charge the phone for at least 30 minutes before trying again. If the Sony logo comes up during charging then let it charge another 10 minutes or so, then remove the back cover and reset the phone with the orange/yellow button (near the bottom of the phone). Hold the button down until the phone vibrates 3 times – this confirms that the phone has switched off.
- When booting into recovery running TWRP 2.6, it does not seem to have reliable write access to the system partition. This causes TWRP system partition restores to fail and any scripts (such as debloat) scripts that write to the system partition may appear to work, but do not perform write (or delete) functions on the system partition. I found that if I run SuperSU.zip after loading TWRP 2.6 then I seem to gain write/delete permissions to the system volume and all works well again.
- Running versions of TWRP 3.0.2 to 3.2.2 (and possibly higher) seem to cause the data partition to become read-only to the Sony Jellybean firmware (Android 4.1). Both Nougat and Oreo versions of AOSP seem unaffected. I have run AOSP Extended (Oreo) on the Xperia SP for some time with TWRP 3.2.2 with no issue writing to the data partition. As soon as I did a TWRP restore back to Sony firmware (Android 4.1) the data partition become read-only. Only reflashing the entire phone back to stock Sony firmware with Flashtool made the data partition read/write again.
Flashing the Sony firmware
The first step in rebuilding the phone is actually the easiest. We need to do the following:
Download software tools, drivers and firmware
- Make sure the phone’s bootloader is unlocked.
- Download and install ADB / Fastboot drivers for Windows.
- Download and install Androxyde’s Flashtool 0.9.18.6, mirrored here. Although it’s generally recommended that you use the latest copy of Flashtool, I found newer versions failed with a SIN error, so I’d use Flashtool 0.9.18.6 to restore this phone.
- Download Sony’s stock firmware for the Xperia SP, model C5303. If you have a different model Xperia SP, then search for Sony stock firmware for your specific model (possibly a C5302 or a C5306). You are wanting to get a firmware file with an extension of FTF.
Flash firmware with Flashtool
Use Flashtool to flash the standard Sony firmware to your Xperia SP
- Click on the icon to start Flashtool – I use FlashTool64 and it works fine.
- To start the flash process, click on the black lightning bolt and select Flashmode
- Click on the … box and change the folder to the folder which holds the Sony firmware that you’ve downloaded.
- The firmware box on the left will now list firmware images that it can see in the folder you selected. There will probably be only one, but if you have more than one FTF image, then it’s easiest to keep them in separate folders so you don’t have trouble telling them apart in Flashtool.
- Depending on the firmware file you may or may not need to select or deselect any firmware components. With the firmware file I’ve linked, I keep these options as they are.
- Click on Flash to start compiling the flash image for the phone.
- When the image has been compiled, you will be asked to plug in the phone. Follow the Flashtool instructions for booting your phone in Flashmode (usually this is to hold the volume down key whilst you plug the USB cable into the phone). The firmware will then be flashed by Flashtool to the phone.
- My Xperia SP usually takes about 6 minutes to flash. When it’s finished, you will see a message to disconnect the phone and power it on. The message is not that obvious – it will appear after about 5 minutes of flashing system.sin.
When the phone has been flashed, you can turn it on and it will boot as if it has been factory reset. You now have an Xperia SP working as it was when first sold. You could walk away now thinking you’re pretty good at this stuff.
Setup new Android phone
With your phone freshly flashed, you might it useful to follow this procedure for setting it up https://kell.co.nz/configure-sony-android/
Rooting Xperia SP with TowelRoot
Rooting Android gives us full access to the system partition and is usually necessary if we wish to merge a new recovery environment into our boot partition to replace Sony’s standard recovery environment (which doesn’t do a bunch for us).
There are many ways to root the Xperia SP – I found the towelroot app the most reliable, working on both Sony firmware versions I tested it with.
Towelroot is a small app that gives sufficient root privilege to be able to flash a new recovery environment, which is what I want it for. After that it is recommended that you use the recovery environment to run an updated SuperSU script (one of the most recommended rooting methods) in recovery mode. I didn’t research the reasons why I needed SuperSU after towelroot, I just did what I was told and it worked out well.
Download TowelRoot app and SuperSU
- Open your browser of choice on your Xperia and download TowelRoot from https://towelroot.com. Click on the reddish brown Lambda and tr.apk will download to your Downloads folder.
- Scroll down the notifications area and click on TowelRoot to install it, then run it.
- When towelroot runs up, press make it ra1n to root the device. If the root is successful you will see a confirmation message in only a few seconds and the phone is now rooted with no reboot necessary. If your phone reboots during the root, it probably didn’t take and you will need to find another method to root the phone.
- Download Chainfire’s UPDATE-SuperSU-v1.94.zip for rooting Android on the phone, mirrored here.
Flash Recovery Partition
The standard recovery partition in Sony firmware is not very functional. It allows you to wipe the data partition and the caches and that’s about it. Flashing third party recovery ROMs allows us to increase the functionality of the recovery partitions to include:
- Creating Nandroid backups of the most common Android partitions and restoring from them.
- Wipe any of the common Android partitions
- Run scripts from any folder (stock firmware is only able to run scripts from the root of sdcard and that doesn’t even work well much of the time).
- Mount common partitions with MTP, so they can be read and written to from a USB-connected PC.
By far, the most useful function to me of 3rd party recovery ROMs is the ability to backup the boot and system partitions of the phone so it can be restored to a known state at any time in the future.
On Sony phones the recovery environment is not in a separate partition, as it is with most other Android phones. The recovery environment is usually merged into the boot partition, which makes updating the recovery environment a little trickier than other brand phones.
Either the bootloader needs to be unlocked so that we can re-flash a specially customised boot ROM with the new recovery environment, or the phone must be rooted so that we can merge the new recovery environment into the boot partition using ADB with root privileges.
Install Recovery mode (TWRP/TeamWin Recovery Project + CWM/ClockWorkMod)
If your phone has been rooted then you can use a script via ADB to merge TWRP and CWM (the recovery environments that we are going to use) into the boot partition. This process is as simple as running a script in Windows.
- Installing TWRP by this method will only work if your phone is rooted. If it’s not rooted the script will still run and appear to work, but TWRP will not be copied to the system partition. Ensure that your phone is rooted before proceeding.
- On the phone, enable Developer Options and enable USB debugging.
- You could download the TWRP/CWM installation package for Xperia SP from https://forum.xda-developers.com/showthread.php?t=2649923 however, although I appreciate the work by the guys at XDA-developers, I find their download link time consuming. I’ve downloaded the installation package and updated ADB to work with Windows 10. You can download my updated package here.
- Unpack the install package to a folder on your computer and run the install,bat script, or run_me_first.bat if you downloaded my modified package. Keep an eye on the phone after you run the script, you may be required to confirm the RSA key for the computer. If so – tick Always allow from this computer and OK to continue.
Once the script has finished (about 20-30 seconds) you will now have a boot partition on your phone that has CWM (ClockWorkMod) and TWRP (TeamWin Recovery Project) recovery environments merged into it.
When you boot now three LEDs will run along the bottom (green, cyan then pink). When the blue LED comes on next start tapping volume up to load CWM (LED will turn green to confirm CWM is loading) or volume down to select TWRP (LED will turn red to confirm that TWRP is loading).
How to Boot into Recovery Mode
When you power on or reboot an Android phone it will ordinarily boot into Android. You can direct the phone to boot into either of the two recovery environments by tapping on the volume keys during restart.
- Power on or restart the phone. About 13 seconds after the phone restarts, the LED bar at the bottom will light up green > cyan > red then change to blue on the right. When the LED changes to blue…
- Tap the Volume Down key for a few seconds until the blue LED goes out. The LED will change to RED, then the phone will then boot into TWRP 2.6.3 recovery. Or…
- Tap the Volume Up key for a few seconds until the blue LED goes out. The LED will change to GREEN, then the phone will then boot into CMW (ClockWordMod) recovery.
- If you tap no keys during the boot process (or the phone doesn’t detect your taps), then after the blue LED goes out you will see an Xperia logo and the phone will boot into Sony’s build of Android 4.1 (Jellybean).
Download SuperSU and flash in Recovery
- Download Chainfire’s UPDATE-SuperSU-v1.94r4.zip for rooting Android to the phone, mirrored here.
Configure TWRP and make a backup
Once you’ve got TWRP flashed as your recovery partition we should use it for what it was intended and create backups of your boot and system partitions (and your data partition separately) so that we never have to rebuild the phone like this again.
Using TWRP to create a Nandroid backup of the boot and system partitions allows us to restore the system as it is right now with the same version of Android you’re running (whether it be stock or customised), rooted and with TWRP as your recovery environment.
If we also backup your data partition (I usually make this a separate backup) then we can save whatever customisations you’ve made to Android, including any apps that you may have installed and their configurations.
Make sure to copy backups of the phone to your computer or wherever you would normally keep your backups. It would be shame to have to do all this again if your phone ever got trashed and you hadn’t copied your backups to a safe place.
We have now reflashed your Xperia SP with Sony firmware, rooted it and merged CWM and TWRP into the bootloader. You now have a working, rooted Xperia SP, hopefully with a backup.
There are AOSP builds for the Xperia SP for Nougat and Oreo as well as an Oreo build for AEX (AOSP Extended). You can test these OSs on your phone and restore back to stock firmware again by using TWRP backups.
If you wish to try installing AEX Oreo, my procedure is Upgrading Xperia SP to AEX Oreo