A small linux script for updating your FP2 with CWM/TWRP

There is a handy guide with steps for updating the FP2 when you are using an alternative recovery rom: ✏ How to update a rooted FP2 with TWRP?

The only difference in the steps I take is that I sideload the update. Since I’m lazy I created a small script that downloads and sideloads the latest version and a second script for downloading and flashing the rooted boot image. So for the linux user here I thought I should share the scripts. You’ll need to have lynx installed and the android framework.

Disclaimer: use this at you own risk. Please don’t use the script if you don’t know what you are doing. If any of the used terminology doesn’t mean anything to you: please don’t use the scripts.

Any additions or improvements are welcome :slight_smile:

Update: Find the latest version on github

Sideloading the update:
#!/bin/sh
function error_exit {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
fi

exit "${code}"
}
trap 'error_exit ${LINENO}' ERR

echo "Connect phone in recovery mode with sideload option on!"
echo "Press <enter> to continue"
read

# Get newest firmware downloadlink=$(lynx --dump $1 'https://fairphone.zendesk.com/hc/en-us/articles/213290023-Fairphone-OS-downloads-for-Fairphone-2’|awk ‘/http/{print $2}’|grep ‘.zip’|head -n1)

echo "-= Downloading update =-"
wget $link

fileName=$(echo $link|rev|cut -d/ -f1|rev)
androiddir=$(locate platform-tools|head -n1)

echo "-= Sideloading update =-"
sudo $androiddir/adb sideload $fileName

echo "-= Removing update =-"
sudo rm -f $fileName

echo "-= Done! Reboot the phone =-"

Flashing the rooted boot image
#!/bin/sh

function error_exit {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
echo "Error on or near line ${parent_lineno}: ${message}; exiting with status ${code}"
else
echo "Error on or near line ${parent_lineno}; exiting with status ${code}"
fi

exit "${code}"
}
trap 'error_exit ${LINENO}' ERR

echo "Connect phone with usb debugging on!"
echo "Press <enter> to continue"
read

# Get newest bootimage download
link=$(lynx --dump $1 'https://fairphone.zendesk.com/hc/en-us/articles/213290023-Fairphone-OS-downloads-for-Fairphone-2'|awk '/http/{print $2}'|grep '.zip'|head -n1)
fileName=$(echo $link|rev|cut -d/ -f1|rev)
version=$(echo $fileName|cut -d- -f3)

linkImage=$(lynx --dump $1 'https://fp2.retsifp.de/'|awk '/http/{print $2}'|grep "$version/"|head -n1)
linkImage=$(lynx --dump $1 "$linkImage"|awk '/http/{print $2}'|grep '.img'|grep '-eng-'|head -n1)

echo "-= Downloading root image =-"
wget $linkImage

fileName=$(echo $linkImage|rev|cut -d/ -f1|rev)
androiddir=$(locate platform-tools|head -n1)

echo "-= Reboot phone =-"
sudo $androiddir/adb reboot bootloader
sleep 30

echo "-= Push boot.img =-"
sudo $androiddir/fastboot flash boot $fileName
sleep 10

echo "-= Reboot phone =-"
sudo $androiddir/fastboot reboot

echo "-= Removing file =-"
sudo rm -f $fileName

7 Likes

You may add reflashing Xposed framework too?

1 Like

I’m impressed! But not positive. So much work only to make the update very more complicated as via TWRP. Why? I don’t recommend anyone to use this script - the risk of an error is very high and the chance to make a fatal error even more.

Sorry!

Spielmops

I hadn’t gotten around to that part yet. I flash the xposed framework manually now. Is it possible to sideload this?

Hi Spielmops,

Well the reason is that I do these steps manually over and over again… every time with some time between them (and I have to remember every time what to do exactly).
I think the chance that I make a mistake is far greater doing things by hand than when automating them. Of course anyone is free to do things in the way they feel comfortable with.

I really don’t see how this is complicating things? Could you elaborate on that?

But you’re right. Some error checking would be a good idea. I’ve added some basic error handling.

1 Like

Thanks and good job !

[quote=“dib0, post:1, topic:23033”]```

Get newest firmware download

link=$(lynx --dump $1 'https://fairphone.zendesk.com/hc/en-us/articles/213290023-Fairphone-OS-downloads-for-Fairphone-2’|awk ‘/http/{print $2}’|grep ‘.zip’|head -n1)


I personally don't trust that part to catch the latest firmware trustworthy. Be more specific, use some regex for the zip filename, at least. Or prompt the user to review the filename before proceed to install it.

---

[quote="dib0, post:1, topic:23033"]```
sudo $androiddir/adb reboot bootloader
```[/quote]

`sudo`! Configure your `udev` permissions and you won't need that. That's the way to go:

[quote="NicoM, post:1, topic:20254"]
Configuring USB Access

I am not sure it is needed in Ubuntu 16.04
As the root user, run (don't forget to replace  by your user name):

wget -S -O - http://source.android.com/source/51-android.rules | sed “s/<username>/$USER/” | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules


(or just install the `android-sdk-platform-tools-common` package on an Ubuntu/Debian sid machine)

---

[quote="dib0, post:1, topic:23033"]
sleep 30
[/quote]

`adb wait-for-device`. Next, check `sideload` mode with `adb devices`.

Work something for fastboot. Wait until `fastboot devices` has a device, for example. 

---

[quote="dib0, post:1, topic:23033"]```
link=$(lynx --dump $1 'https://fairphone.zendesk.com/hc/en-us/articles/213290023-Fairphone-OS-downloads-for-Fairphone-2'|awk '/http/{print $2}'|grep '.zip'|head -n1)
```[/quote]

Also... do you really need `lynx`? What about `wget -O - $URL`?

---

I won't use it (among other things, because I use FP Open) but I appreciate your share, :slight_smile:
I'd like to see it iproved with Xposed install and other [handy things](https://forum.fairphone.com/t/pencil2-update-guide-for-a-modified-fp-open-os-with-gapps-xposed-and-more/21117), and hopefully, a FP Open version. If you update the script to [GitHub](https://github.com/), [GitLab](https://gitlab.com/), or other collaborative space, I'm sure someone could give a hand, ;)

Since I’m hosting the fp2.retsifp.de-Site for the rooted boot.imgs, I have to add some words on it:

This downloads the wrong version, because you first get the current release-Version from Fairphone and then download a beta bootimage. Will most propably work, but nobody knows. :wink:
So just remove the beta and it’ll download the release-version.

In addition to that, you need to add some error handling. I’m currently working on an autobuild-script (correct: I work on it, whenever I feel like it) for the boot.img but you can’t be sure, that I always create the images before you want to update your phone and name the folder correctly. :wink:
So it is a good idea to add a “panic”-mode, which stops the script with an error message, when any error occurs.

2 Likes

Thanks! You obviously have more experience than me on the android framework. Personally I don mind using sudo (because the occasional update is about the only thing I do with the android framework), but with more general use youŕe absolutely right that it is better to configure the udev permissions the right way.

As most scripts (at least in my case) they start out building… error handling isn’t the first thing that’s being build (and yes… in my time as a solution architect I told my developers otherwise… so I know that I now apply a double standard :wink:). I like the adb wait-for-device. I’ll change that when I get the chance. It is far better.

And on the lynx part, well wget does the trick. The difference is that lynx outputs the formatted page, not the html. That’s a bit less data to process. It is probably the certainty you seek using the regex?
I’m adding the user validation on the right version. That’s at least a smart thing to do.

Thanks!

Hi!

Thank you for hosting the boot images site. I’m really happy you do this. Also the speed in which you add new images. I’m on the beta myself, so that’s why I had the beta directory in there. I removed it from the post.

I already added some crude error handling that stops the script if any command exits with an error. I’ll try to find the time to add some better error handling and checks.

thanks!

1 Like

This is a nice start ! thanks.

But I’m thinking there could be more elegant ways ?

Like instructing TWRP to install the ZIP THEN the rooted boot in one pass

Or patch the ZIP with the rooted boot.img (“zip -u update.zip boot.img”, and inject “package_extract_file(“boot.img”, “/dev/block/platform/msm_sdcc.1/by-name/boot”);” line in the updater_script if not already present)
(that’s how i’m doing it by hand actually)

I will try to work on this when i have more time for geeking :slight_smile:

1 Like

I’m willing to pick up the gauntlet for fairphone open :slight_smile:

My first goals will be to install the OS, make fdroid a system app, install some CA’s as trusted on system level.

Next I’m thinking of adding Exposed installation (not using it right now but want do do that).

During and after that, I’m all open for suggestions :slight_smile: :slight_smile:

I started working on this. You can find it under my github account.

1 Like

Related to this. Does anyone know if you have to follow the updates in order or whether you can immediately upgrade to the latest release ?

E.g. I’m on FPoOS 16.07. Do I first need to upgrade to 16.08, 16.09 and then 16.10 ? Or is it safe to immediately upgrade to 16.10 ?

You can always update to the latest version directly.
For FPOS, there are incremental updates (e.g. FP2-gms66-1.8.1-ota-from-1.6.2.zip), but if you are coming from a previous version, you simply get FP2-gms66-1.8.1-ota.zip.

Nice!

I added the original posted scripts to my github.

I would suggest removing the sudos, none of them should be needed.

1 Like

I have adjusted the script + refactored to update FPoOS.

With it, I have updated from 16.07.1 -> 16.08.0 and from 16.08.0 -> 16.10.0.

That’s a start but not the end :wink:

I noticed that I seem to have started from a ‘fresh’ slate after the first update. Not sure if it’s to do with the trials I did or with the update process itself.

Thankfully I don’t have much on my phone yet.

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