English

TWRP recovery for the Fairphone 1

Tags: #<Tag:0x00007f05f78e1120> #<Tag:0x00007f05f78e0900>

It is my pleasure to present to you a Team Win Recovery Project (TWRP) 3.0 recovery for the Fairphone 1. Unfortunately, this recovery is compatible ONLY with the Fairphone 1 with the original storage layout. Do NOT use it on a Fairphone 1 with the unified storage layout, no matter if it is a Fairphone 1 or a Fairphone 1 (U).

The TWRP recovery can be built on different flavors of Android; this one was tested on Omni 4.4, but I have no plans to update it to newer TWRP or Omni versions. I am basically done with this project; although I may change my mind in the future, for now, I am not investing any more of my free time on it.

You can use most TWRP features, but NEVER install an official Fairphone 1 update with this TWRP. The reason is that the updater script inside the official Faiphone 1 update zips associates /system to the wrong partition. I suppose that the official Fairphone 1 recovery (most likely provided by MediaTek) ignores the partitions stated in the updater script and uses those provided by the kernel in /proc/dumchar_info; I do not really know as I have no access to that source code. But what I know for sure is that the updater script is wrong, so installing an official Fairphone 1 update would give you a very big headache.

In the same way, NEVER update your system through the Fairphone Updater app when the recovery installed in the recovery partition is TWRP. Although I do not know for sure I suppose that the Fairphone Updater app would just delegate in the recovery the installation of the update, so it would be the same situation.

If you want to use this TWRP recovery in your Fairphone 1 anyway below you will find the needed instructions to build and install it.

You may notice that, unlike the commands used in the build instructions, the commands used in the installation instructions are rather obscured by being interleaved in the explanations themselves and without any highlighting. This is on purpose. I want you to carefully read the instructions instead of just blindly execute the commands. When you read below about the bug in the original Fairphone 1 kernel I think that you will understand why :wink:

In any case, please note that I am publishing these instructions and the recovery in the hope that it will be useful, but WITHOUT ANY WARRANTY. I have reviewed the instructions several times, and I think that I have not missed anything, but I could be wrong. In the same way, I have built the recovery and used it myself without problems, but if there is any bug or forbidden use that I am not aware of (or I have forgotten to mention) that damages your phone, the data in your phone or whatever… I am really sorry! But I am not buying you a new Fairphone :stuck_out_tongue:

How to build

These instructions are a quick reference of the needed steps in a GNU/Linux system. Please refer to the official Android documentation or any of the guides available on the Internet about building Android for further information.

First of all, create a directory to download all the source code and build it. For example, /home/$USER/TWRP/. Now open a terminal in that directory. All the following commands are expected to be executed in the TWRP directory.

Get repo, which is the command used in Android to manage the repositories with the source code:

curl https://storage.googleapis.com/git-repo-downloads/repo > repo

And make it executable:

chmod a+x repo

Now initialize the TWRP repositories. Instead of using the full set of Omni repositories you can use a reduced set that contains only those needed to build TWRP (as explained by Dees_Troy, the lead developer of TWRP, in his TWRP compilation guide).

./repo init -u https://github.com/lj50036/platform_manifest_twrp_omni -b twrp-4.4

If you have never used git before, that command will fail. You can ignore the failure and continue with the next step (at least, if you want to download the repositories just to build the recovery and you do not intend to develop anything), or you can configure your name and email address. Do not worry, unless you send any change to a public server it will not be used anywhere out of your computer. Set your name and email address as explained in the failure message (that is, executing git config --global user.email "you@example.com" and git config --global user.name “Your Name”) and then run again the same ./repo init command as before.

Once repo was initialized, create the file /home/$USER/TWRP/.repo/local_manifests/fairphone_fp1.xml and add to it the following contents so repo knows where to get the sources for the Fairphone 1:

<?xml version="1.0" encoding="UTF-8"?>
<manifest>
    <remote name="fairphone" fetch="https://gitlab.com/danxuliu/" />

    <project name="android_device_fairphone_fp1.git" path="device/fairphone/fp1" remote="fairphone" revision="android-4.4" />
    <project name="android_kernel_fairphone_fp1.git" path="kernel/fairphone/fp1" remote="fairphone" revision="mediatek-mt6589-3.4" />
</manifest>

And now tell repo to download all the needed repositories. This will take time and you will need around 14 GBs of space in your hard disk:

./repo sync --current-branch

If you are following these instructions some time after they were written you should check that the version of the downloaded recovery source code is near 3.0.2 (as the Fairphone files were created for that version). To do this, open the file /home/$USER/TWRP/bootable/recovery/variables.h and look for the value to the right of TW_VERSION_STR. If the version is lower than 3.1.0 you should have no problem. If it is 3.1.0 or greater it may still work flawlessly, or it may fail to build, or it may damage your phone when run (I have no idea, as at the time of this writing the latest version is the 3.0.2 so I do not know what problems could appear with future versions :wink: ).

Now that you have the code and that you have checked that it is the right version you can start the build process itself.

First, prepare the build environment.

. build/envsetup.sh

Then, state that you want to build TWRP for the Fairphone 1, engineering variant (other variants, like the user variant, user, should work, but I have not tested them):

lunch twrp_fp1-eng

Finally, build the recovery image. This will take several minutes or even hours, depending on your computer. It will also require around 5 GBs of additional space in your hard disk. The -j4 parameter tells make how many files to build in parallel; it should be set to the number of cores of your CPU (in this example, a quad-core CPU):

make -j4 recoveryimage

How to install

Remember that this recovery is ONLY for the Fairphone 1 with the original storage layout. Do NOT install it on a Fairphone 1 with the unified storage layout, no matter if it is a Fairphone 1 or a Fairphone 1 (U).

In order to install the recovery we need to run some commands in the Fairphone 1. You can use a terminal emulator for Android or, a better option, you can use ADB (Android Debug Bridge). ADB makes possible to type the commands in the computer and execute them on the device.

To install ADB please refer to the official Android Studio guide. Note that you do not really need Android Studio, but just the command line tools. However, installing Android Studio is probably the easiest way to get ADB. If you prefer to install just the command line tools then remember to follow the instructions included inside the downloaded package.

Once you have installed ADB in your computer, in your device go to Settings->Developer options and then enable USB debugging. Now connect your device to your computer; ADB should recognize that your device is connected. To check it, in the directory where you have the ADB executable (for example, in GNU/Linux it could be /home/$USER/android-sdks/platform-tools/), execute ./adb devices. You should see your device in the List of devices attached shown. If the list is empty and you are using GNU/Linux you may need to adjust your udev rules (something like adding SUBSYSTEM==“usb”, ATTR{idVendor}==“0bb4”, ATTR{idProduct}==“0c03”, GROUP=“usb” to the file /etc/udev/rules.d/99-Fairphone.rules; please refer to the documentation or support channels of your distribution for further information).

Does ADB recognize your Fairphone 1? Great, then let’s see how you can install the recovery. Let’s suppose that you downloaded the Android repositories in the directory /home/$USER/TWRP/. When the building finished the file recovery.img should have been created in the directory /home/$USER/TWRP/out/target/product/fp1/. Copy that file to the internal SD partition of your Fairphone 1. You can copy it with ADB by executing <path to ADB executable>/adb push recovery.img /sdcard/recovery.img from the /home/$USER/TWRP/out/target/product/fp1/ directory.

Now we will, at last, perform the installation itself of the recovery.

The following instructions assume that you are using ADB; if you decided to use a terminal emulator for Android instead then you first have to become root (by executing su -) and then execute the same commands explained below, although without the leading <path to ADB executable>/adb shell.

In any case, you must be EXTREMELY careful. The reason is a severe bug in the original Fairphone 1 kernel (it is not exclusive of the Fairphone 1; the bug is in the code provided by MediaTek, so it probably happens on most MT6589 devices). If you have a partition with a size of X bytes and write X+Y bytes to it those extra Y bytes should be discarded. However, due to the bug, those extra Y bytes are written to the next partition (and if they do not fit, the remaining bytes are written to the next partition, and so on). Therefore, if you write the wrong file to the recovery partition you may overwrite unintended partitions and thus damage your phone.

Therefore, before writting the image to the partition, let’s ensure that its size is smaller than the size of the partition (6291456 bytes). Execute <path to ADB executable>/adb shell ls -al /sdcard/recovery.img. Is the size of the image (the value in the fourth column) smaller than 6291456?. If and only if yes, you can continue. To write the image, execute <path to ADB executable>/adb shell dd if=/sdcard/recovery.img of=/dev/recovery (you are just saying to your phone “copy all the bytes from /sdcard/recovery.img to /dev/recovery”).

Congratulations, you have installed TWRP recovery in your Fairphone 1! If you now rebooted in recovery mode you would enter in TWRP. However, as soon as you started the normal system again TWRP would be replaced by the original Fairphone 1 recovery. The Fairphone 1 uses a feature of Android that restores the original recovery if needed when the system is started. You can disable this feature by removing the execution permission for the script that restores the recovery. This can be done just with <path to ADB executable>/adb shell “mount -o remount,rw /system && chmod 444 /system/etc/install-recovery.sh && mount -o remount,ro /system”.

How to enter in recovery mode

There are two main ways:
  • With your device turned off, press and hold the Volume up button and, while holding that button, press and hold the Power button. Keep holding both buttons until the screen turns on. You are now in recovery mode!
  • With your device turned on, connect it through USB to your computer and then reboot it with ADB by executing <path to ADB executable>/adb reboot recovery. The device will then turn off and then automatically turn on in recovery mode.

Once in recovery mode, remember: with great power comes great responsibility :wink: Be very careful!

NEVER install an official Fairphone 1 update through TWRP, and NEVER update your system through the Fairphone Updater app when the recovery installed in the recovery partition is TWRP. Yes, I am very annoying, I know that it was clear the first time I said it… but just in case :wink:

Also, you may want to ensure that you can use ADB when in recovery mode before trying anything. If something goes wrong and the main system stops being able to boot the only way to fix it would be from the recovery. Even if TWRP provides its own terminal emulator you may find ADB more comfortable in that situation (specially due to the larger screen of your computer).

How to restore the original recovery

Download the appropriate flashable image zip file for your phone, extract the recovery.img from the zip, copy it to your internal SD card partition and write it to the recovery partition just like done with the TWRP recovery.

Or, probably an easier way, just give the execution permission back to /system/etc/install-recovery.sh (you removed it with a-x, you add it with a+x, that is, executing <path to ADB executable>/adb shell chmod a+x /system/etc/install-recovery.sh). The next time that your system starts it should automatically write the original recovery back into the recovery partition. So if you add the execution permission to that file, reboot your phone (a normal reboot, not a reboot to recovery mode) and then reboot once again but this time to recovery mode you should end in front of the original Fairphone 1 recovery.

This article is licensed under a Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license. For the TWRP license please check the source files themselves; those written by me are typically licensed under the GNU General Public License version 3 or later, but in some cases I had to use a different license (for example, in the case of kernel patches they have to be licensed under the GNU General Public License version 2).

12 Likes

Yeah, I am amazed. Thanks for the cool work.

3 Likes

Thank you. I hope that you will be able to make a good use of it :wink:

3 posts were split to a new topic: Need a powerful recovery to backup from broken FP1U to SD card

This #TWRP recovery should make FP1 ready to install LineageOS if it is made available to the project and reverse engineered?

1 Like

I do not know the requirements of LineageOS with respect to its installation, but probably yes. However, you should be aware of the complexities of porting newer Android versions to the Fairphone 1 (in short, do not hold your breath :wink: ).

2 Likes

OK. I totally understand. I’m just considering the option that you (and other potential community contributors) could e.g. get special access (not open source) the 4.4.4 source tree from Fairphone to do a nice donation-based update of the long awaited and promised updated of the Android version.

I won’t hold my breath. I’m just considering gathering the available interest in FP1 to support you and potential others in fixing this promise and showing that it is the community in the end can make the Fairphone really fair and not necessarily Fairphone which has proven to do otherwise (of course with the chance to change).

The current way chosen has probably struck some thorns into the image, brand and credibility of Fairphone. I for one wanted to really ensure running this phone until the very last usable battery capacity was out of the window. That would have been a long-term sustainable use of this phone. And the chance for that has been greatly reduced now.

2 Likes

Yes, please!

I would love to be able to enjoy my FP1, which I still use (with unified storage) with Lineage OS!! :wave:

2 Likes

Likewise. I’m trying now to fix Unified Storage but the Fairphone Updater app doesn’t show different OS options…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.