Build Sailfish OS for Sony Xperia™ X

Notes taken while following along the official guide “Sailfish OS Hardware Adaptation Development Kit for Sony Xperia X”…

The long awaitet blog post is finally here: Opening Sailfish OS HW Adaptation Source Code for Sony Xperia™ X.

And, guess what? Only one day prior to the announcement of the first publicly available Hardware Adaptation sources and instructions for Sailfish X, aka Sailfish OS for Sony Xperia™ X my Sony Xperia X had arrived.

Soooo, no excuses anymore, right?

GO :-)

DISCLAIMER The following are my personal notes, taken while following along the AWESOME(!) guide Sailfish OS Hardware Adaptation Development Kit for Sony Xperia X and accompanying community documentation. It is totally not my intend to duplicate documentation — it’s just a journal of the required steps when starting from scratch, compiled into a singe doc.

My host setup

1. Install Platform SDK

Setting up required environment variables, partly mixed w/ build instructions from Sailfish OS Hardware Adaptation Development Kit for Sony Xperia X

cat <<'EOF' > $HOME/.hadk.env
export PLATFORM_SDK_ROOT="/srv/mer"
export ANDROID_ROOT="$HOME/hadk"
export VENDOR="sony"
export DEVICE="f5121"
export HABUILD_DEVICE="suzu" 
# ARCH conflicts with kernel build
export PORT_ARCH="armv7hl"
cat <<'EOF' >> $HOME/.mersdkubu.profile
function hadk() { source $HOME/.hadk.env; echo "Env setup for $DEVICE"; }
export PS1="HABUILD_SDK [\${DEVICE}] $PS1"
cat <<'EOF' >> $HOME/.mersdk.profile
function hadk() { source $HOME/.hadk.env; echo "Env setup for $DEVICE"; }

Slightly adopted from

Add to ~/.bashrc:

alias sfossdk=$PLATFORM_SDK_ROOT/sdks/sfossdk/mer-sdk-chroot


source ~/.bashrc
curl -k -O ;
sudo mkdir -p $SAILFISHOS_PLATFORM_SDK_ROOT/sdks/sfossdk ;
sudo tar --numeric-owner -p -xjf Jolla-latest-SailfishOS_Platform_SDK_Chroot-i486.tar.bz2 -C $SAILFISHOS_PLATFORM_SDK_ROOT/sdks/sfossdk  ;
echo 'PS1="PlatformSDK $PS1"' > ~/.mersdk.profile ;
echo '[ -d /etc/bash_completion.d ] && for i in /etc/bash_completion.d/*;do . $i;done'  >> ~/.mersdk.profile ;

PlatformSDK matthias@debbook:~$ 

2. Preparing the Platform SDK

PlatformSDK matthias@debbook:~$ 

sudo zypper in android-tools createrepo zip

3. Setting up an Android Build Environment

Following the Sailfish OS Hardware Adaptation Development Kit Documentation

PlatformSDK matthias@debbook:~$ 

sudo mkdir -p $UBUNTU_CHROOT
sudo tar --numeric-owner -xjf $TARBALL -C $UBUNTU_CHROOT

4. Entering Ubuntu Chroot

PlatformSDK matthias@debbook:~$ 

ubu-chroot -r $PLATFORM_SDK_ROOT/sdks/ubuntu

HABUILD_SDK [f5121] matthias@debbook:~$

sudo apt-get update
sudo apt-get install bsdmainutils
sudo apt-get install openjdk-7-jdk
sudo update-java-alternatives -s java-1.7.0-openjdk-amd64
sudo apt-get install rsync vim unzip

5. Checking out CyanogenMod Source and Building…

PlatformSDK matthias@debbook:~$ 

git config --global "Matthias Geisler"
git config --global "matthias""

Install repo command

From here on we’re going to use the build instructions from Sailfish OS Hardware Adaptation Development Kit for Sony Xperia X

HABUILD_SDK [f5121] matthias@debbook:~$

sudo mkdir -p $ANDROID_ROOT
sudo chown -R $USER $ANDROID_ROOT
repo init -u git:// -b hybris-sony-aosp-6.0.1_r80-20170902
repo sync -j1000 --fetch-submodules
source build/
# Please download Sony Xperia X Software binaries for AOSP Marshmallow (Android 6.0.1) from
# and unzip its contents in this directory like this:
unzip ~/Downloads/SW_binaries_for_Xperia_AOSP_*.zip
export USE_CCACHE=1
lunch aosp_$DEVICE-userdebug
make -j10 hybris-hal

Install: out/target/product/suzu/hybris-boot.img
Install: out/target/product/suzu/hybris-recovery.img

#### make completed successfully (10:25 (mm:ss)) ####

HABUILD_SDK [f5121] matthias@debbook:~$ 

6. Se up Scratchbox2 Target

Download the latest "SailfishOS-armv7hl from

PlatformSDK matthias@debbook:~

sudo zypper in -t pattern Mer-SB2-armv7hl

sdk-assistant create $VENDOR-$DEVICE-$PORT_ARCH ~/Downloads/Jolla-

Create ~/main.c:

#include <stdlib.h>
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");

Compile and test:

sb2 -t $VENDOR-$DEVICE-$PORT_ARCH gcc main.c -o test
sb2 -t $VENDOR-$DEVICE-$PORT_ARCH ./test

7. Packaging

PlatformSDK matthias@debbook:~

rpm/dhd/helpers/ --droid-hal
git clone --recursive$DEVICE hybris/droid-configs
if [ -z "$(grep community_adaptation $ANDROID_ROOT/hybris/droid-configs/rpm/droid-config-$DEVICE.spec)" ]; then
  sed -i '/%include droid-configs-device/i%define community_adaptation 1\n' $ANDROID_ROOT/hybris/droid-configs/rpm/droid-config-$DEVICE.spec
if [ -z "$(grep patterns-sailfish-consumer-generic $ANDROID_ROOT/hybris/droid-configs/patterns/jolla-configuration-$DEVICE.yaml)" ]; then
  sed -i "/Summary: Jolla Configuration $DEVICE/i- patterns-sailfish-consumer-generic\n- patterns-sailfish-store-applications\n- pattern:sailfish-porter-tools\n" $ANDROID_ROOT/hybris/droid-configs/patterns/jolla-configuration-$DEVICE.yaml
rpm/dhd/helpers/ --configs
sb2 -t $VENDOR-$DEVICE-$PORT_ARCH -m sdk-install -R zypper in droid-hal-$DEVICE-kernel
sb2 -t $VENDOR-$DEVICE-$PORT_ARCH -m sdk-install -R zypper in --force-resolution droid-hal-$DEVICE-kernel-modules
rpm/dhd/helpers/ --mw=
rpm/dhd/helpers/ --mw=
rpm/dhd/helpers/ --mw=
git clone --recursive$DEVICE hybris/mw/droid-hal-img-boot-$DEVICE
rpm/dhd/helpers/ --mw=$DEVICE
HABUILD_SDK [f5121] matthias@debbook:~$

sudo apt-get install rsync
mkdir syspart
cd syspart
repo init -u git:// -b syspart-sony-aosp-6.0.1_r80-20170902
repo sync -j1000 --fetch-submodules
source build/
unzip ~/Downloads/SW_binaries_for_Xperia_AOSP_*.zip
export USE_CCACHE=1
lunch aosp_$DEVICE-userdebug
make -j10  libnfc-nci bluetooth.default_32 systemtarball

Target system fs tarball: out/target/product/suzu/system.tar.bz2
build/tools/ out/host/linux-x86/bin/fs_get_stats out/target/product/suzu system out/target/product/suzu/system.tar out/target/product/suzu/system.tar.bz2 out/target/product/suzu/system

#### make completed successfully (02:16:23 (hh:mm:ss)) ####
PlatformSDK matthias@debbook:~

cd $ANDROID_ROOT/../syspart
git clone$DEVICE
mb2 -t $VENDOR-$DEVICE-$PORT_ARCH -s droid-system-$DEVICE/rpm/droid-system-$DEVICE.spec build
rm -f $ANDROID_ROOT/droid-local-repo/$DEVICE/droid-system-*.rpm
mv RPMS/droid-system-$DEVICE-0.1.1-1.armv7hl.rpm $ANDROID_ROOT/droid-local-repo/$DEVICE/
createrepo "$ANDROID_ROOT/droid-local-repo/$DEVICE"
sb2 -t $VENDOR-$DEVICE-$PORT_ARCH -m sdk-install -R zypper ref
HABUILD_SDK [f5121] matthias@debbook:~$
git clone external/audioflingerglue
git clone external/droidmedia

source build/
lunch aosp_$DEVICE-userdebug
make -j10 libdroidmedia_32 minimediaservice minisfservice libaudioflingerglue_32 miniafservice
PlatformSDK matthias@debbook:~

DROIDMEDIA_VERSION=$(git --git-dir external/droidmedia/.git describe --tags | sed -r "s/\-/\+/g")
mkdir -p hybris/mw/droidmedia-localbuild/rpm
cp rpm/dhd/helpers/droidmedia-localbuild.spec hybris/mw/droidmedia-localbuild/rpm/droidmedia.spec
sed -ie "s/0.0.0/$DROIDMEDIA_VERSION/" hybris/mw/droidmedia-localbuild/rpm/droidmedia.spec
mv hybris/mw/droidmedia-$DROIDMEDIA_VERSION.tgz hybris/mw/droidmedia-localbuild
rpm/dhd/helpers/ --build=hybris/mw/droidmedia-localbuild
rpm/dhd/helpers/ --mw=

DEVICE=$HABUILD_DEVICE rpm/dhd/helpers/
mkdir -p hybris/mw/audioflingerglue-localbuild/rpm
cp rpm/dhd/helpers/audioflingerglue-localbuild.spec hybris/mw/audioflingerglue-localbuild/rpm/audioflingerglue.spec
mv hybris/mw/audioflingerglue-0.0.1.tgz hybris/mw/audioflingerglue-localbuild
rpm/dhd/helpers/ --build=hybris/mw/audioflingerglue-localbuild
rpm/dhd/helpers/ --mw=

git clone --recursive$DEVICE hybris/droid-hal-version-$DEVICE
rpm/dhd/helpers/ --version

----------------------DONE! Now proceed on creating the rootfs------------------

8. Create the Sailfish OS root filesystem

PlatformSDK matthias@debbook:~

HA_REPO="repo --name=adaptation-community-common-$DEVICE-@RELEASE@"
HA_DEV="repo --name=adaptation-community-$DEVICE-@RELEASE@"
sed \
"/$HA_REPO/i$HA_DEV --baseurl=file:\/\/$ANDROID_ROOT\/droid-local-repo\/$DEVICE" \
$ANDROID_ROOT/hybris/droid-configs/installroot/usr/share/kickstarts/$KS \
> $KS
PlatformSDK matthias@debbook:~

rpm/dhd/helpers/ --configs

Save the contents of Mic-loop.patch as ~/Downloads/mic-loop.patch.

PlatformSDK matthias@debbook:~

RELEASE= # if your sb2 target is 2.1.0, do not worry about that
sudo zypper in lvm2 atruncate pigz
sudo ssu ar unbreakmic
sudo zypper ref unbreakmic
sudo zypper in droid-tools
sudo zypper in --force mic
cd /usr/lib/python2.7/site-packages/mic/
sudo patch -p1 --dry-run < ~/Downloads/mic-loop.patch
# ensure above worked, then
sudo patch -p1 < ~/Downloads/mic-loop.patch
sudo mic create loop --arch=$PORT_ARCH \
    --record-pkgs=name,url     --outdir=sfe-$DEVICE-$RELEASE$EXTRA_NAME \

Info: The new image can be found here:

Info: Finished.

9. Flashing

PlatformSDK matthias@debbook:~

mkdir flashing
cd flashing
tar -xvf $ANDROID_ROOT/sfe-$DEVICE-$RELEASE$EXTRA_NAME/SailfishOS-*.tar.bz2
cd SailfishOS-*/

Act upon instructions in the flashing-README.txt

Unlock bootloader:

fastboot -i 0x0fce oem unlock 0xUNLOCK_CODE
OKAY [  0.180s]
finished. total time: 0.180s

If the above fails w/ FAILED (remote: Command not allowed) make sure to enable OEM unlock in developer options.

…and flash the phone:


apt-get install android-tools-fastboot

cd SailfishOS-*/
bash ./

If gcc [^se]*.c sha1.c sparse_crc32.c ext4_utils.c extent.c -o make_ext4fs -lz gives error:

output_file.c:30:18: fatal error: zlib.h: No such file or directory
 #include <zlib.h>
compilation terminated.

Simply install the missing w/ sudo apt-get install libz-dev.

DONE :-)

Related Tweet

UPDATE 2017-09-23 — The installable image generated by my build can be downloaded here. DISCLAIMER: Flash it at YOUR OWN RISK (see above section 9.). I’M NOT RESPONSIBLE for you or your phone.

I like comments — generally speaking. Sharing experiences, discussing ideas, correcting my mistakes ;-)…