GPS on Replicant 6

I use Replicant on my main Samsung S3 mobile phone. Replicant is a fully free Android distribution. One consequence of the “fully free” means that some functionality is not working properly, because the hardware requires non-free software. I am in the process of upgrading my main phone to the latest beta builds of Replicant 6. Getting GPS to work on Replicant/S3 is not that difficult. I have made the decision that I am willing to compromise on freedom a bit for my Geocaching hobby. I have written before how to get GPS to work on Replicant 4.0 and GPS on Replicant 4.2. When I upgraded to Wolfgang’s Replicant 6 build back in September 2016, it took some time to figure out how to get GPS to work. I prepared notes on non-free firmware on Replicant 6 which included a section on getting GPS to work. Unfortunately, that method requires that you build your own image and has access to the build tree. Which is not for everyone. This writeup explains how to get GPS to work on a Replicant 6 without building your own image. Wolfgang already explained how to add all other non-free firmware to Replicant 6 but it did not cover GPS. The reason is that GPS requires non-free software to run on your main CPU. You should understand the consequences of this before proceeding!

The first step is to download a Replicant 6.0 image, currently they are available from the replicant 6.0 forum thread. Download the replicant-6.0-i9300.zip file and flash it to your phone as usual. Make sure everything (except GPS of course) works, after loading other non-free firmware (Wifi, Bluetooth etc) using "./firmwares.sh i9300 all" that you may want. You can install the Geocaching client c:geo via fdroid by adding fdroid.cgeo.org as a separate repository. Start the app and verify that GPS does not work. Keep the replicant-6.0-i9300.zip file around, you will need it later.

The tricky part about GPS is that the daemon is started through the init system of Android, specified by the file /init.target.rc. Replicant ships with the GPS part commented out. To modify this file, we need to bring out our little toolbox. Modifying the file on the device itself will not work, the root filesystem is extracted from a ramdisk file on every boot. Any changes made to the file will not be persistent. The file /init.target.rc is stored in the boot.img ramdisk, and that is the file we need to modify to make a persistent modification.

First we need the unpackbootimg and mkbootimg tools. If you are lucky, you might find them pre-built for your operating system. I am using Debian and I couldn’t find them easily. Building them from scratch is however not that difficult. Assuming you have a normal build environment (i.e., apt-get install build-essentials) try the following to build the tools. I was inspired by a post on unpacking and editing boot.img for some of the following instructions.

git clone https://github.com/CyanogenMod/android_system_core.git
cd android_system_core/
git checkout cm-13.0 
cd mkbootimg/
gcc -o ./mkbootimg -I ../include ../libmincrypt/*.c ./mkbootimg.c
gcc -o ./unpackbootimg -I ../include ../libmincrypt/*.c ./unpackbootimg.c
sudo cp mkbootimg unpackbootimg /usr/local/bin/

You are now ready to unpack the boot.img file. You will need the replicant ZIP file in your home directory. Also download the small patch I made for the init.target.rc file: https://gitlab.com/snippets/1639447. Save the patch as replicant-6-gps-fix.diff in your home directory.

mkdir t
cd t
unzip ~/replicant-6.0-i9300.zip 
unpackbootimg -i ./boot.img
mkdir ./ramdisk
cd ./ramdisk/
gzip -dc ../boot.img-ramdisk.gz | cpio -imd
patch < ~/replicant-6-gps-fix.diff 

Assuming the patch applied correctly (you should see output like "patching file init.target.rc" at the end) you will now need to put the ramdisk back together.

find . ! -name . | LC_ALL=C sort | cpio -o -H newc -R root:root | gzip > ../new-boot.img-ramdisk.gz
cd ..
mkbootimg --kernel ./boot.img-zImage \
--ramdisk ./new-boot.img-ramdisk.gz \
--second ./boot.img-second \
--cmdline "$(cat ./boot.img-cmdline)" \
--base "$(cat ./boot.img-base)" \
--pagesize "$(cat ./boot.img-pagesize)" \
--dt ./boot.img-dt \
--ramdisk_offset "$(cat ./boot.img-ramdisk_offset)" \
--second_offset "$(cat ./boot.img-second_offset)" \
--tags_offset "$(cat ./boot.img-tags_offset)" \
--output ./new-boot.img

Reboot your phone to the bootloader:

adb reboot bootloader

Then flash the new boot image back to your phone:

heimdall flash --BOOT new-boot.img

The phone will restart. To finalize things, you need the non-free GPS software components glgps, gps.exynos4.so and gps.cer. Before I used a complicated method involving sdat2img.py to extract these files from a CyanogenMod 13.x archive. Fortunately, Lineage OS is now offering downloads containing the relevant files too. You will need to download some files, extract them, and load them onto your phone.

wget https://mirrorbits.lineageos.org/full/i9300/20170125/lineage-14.1-20170125-experimental-i9300-signed.zip
mkdir lineage
cd lineage
unzip ../lineage-14.1-20170125-experimental-i9300-signed.zip
adb root
adb wait-for-device
adb remount
adb push system/bin/glgps /system/bin/
adb push system/lib/hw/gps.exynos4.vendor.so /system/lib/hw/gps.exynos4.so
adb push system/bin/gps.cer /system/bin/

Now reboot your phone and start c:geo and it should find some satellites. Congratulations!

22 Replies to “GPS on Replicant 6”

  1. Hi Simon, I am continuing the tread on proprietary GPS blob from the replicant wiki forum. Yesterday I was, thanks to you, able to get permanent init.target.rc file with uncommented last lines. However, the GPS still does not work. the GPS icon etc is there but no signal whatever app I tried. I remember that it worked when I was on replicant 4.2. Now I read that you were talking about the recovery image from replicant? I am confused…Does the GPS really works on your device? Thanks in advance for your feedback.

    • Hi Thomas. Yes GPS works for me with the latest replicant-6 builds. Did you install /system/bin/glgps etc? Try ‘adb shell’ and ‘ps|grep gps’. You should see ‘glgps’ running.

      You talked about not being able to flash boot.img, why was that? And you mentioned you used TWRP, so maybe you want to try the replicant recovery image instead.

      Cheers,
      Simon

      • Thanks for your quick reply.
        yes indeed glgps is running but I am not picking any gps signal with 3 different apps and even when I am on the street.
        I could not use fastboot to flash boot.img, so my idea was to copy it into the replicant6-i9300 and rezip the whole distrib and install with TWRP (because it does not ask for signature). However, I realized that I could use heimdal to inly flash the boot.img file.

        • What does ‘adb logcat|grep -i gps’ show? You should see something related to acquiring GPS information.

          Indeed fastboot does not work on the S3! I describe how to use heimdall in my post.

          /Simon

          • Yes thank you, your blog is bookmarked forever now :-).
            Now GPS is working, I did a full factory reset… I think I have played to much with directories and files permissions.
            Thanks forever!

  2. Hurray! Thanks for confirming, always good to hear that the instructions are reproducible.

    Cheers,
    /Simon

  3. Thank you very much for your effort and explanations!

    I could follow al steps until I had to download LineageOS 13 for i9300. I went to https://mirrorbits.lineageos.org/full/i9300, which led me to https://lineageos.mirrorhub.io/full/i9300/, that contains the same as https://download.lineageos.org/i9300.

    Problem is that there are no “experimental” (they were only available for 2 months since the LineageOS started) but “nightly” builds, which only have the file “build.prop” in system directory.

    I searched in those mentioned sites and in http://www.lineageosrom.com/2017/01/official-lineage-os-nighty.html. Also in https://forum.xda-developers.com/galaxy-s3, but no luck.

    Any idea where I can download a “proper” and official archived LineageOS 13 (I mean, with all files)?

    PS:I don’t trust custom/unofficial ROMS, but I guess XDA-Developers could be a good place to check (https://forum.xda-developers.com/showpost.php?p=39873449&postcount=5)

  4. Thank you very much for your instructions! They work like a charm.

    My problem is that I didn’t realize LineageOS experimental builds would be available only for two months (https://www.lineageos.org/Update-and-Build-Prep/) and as I didn’t download any, now I can’t find one. I’ve been downloading Android 6 and CM 12 ROMs for two days and no one has those files (they’re meant to flash them directly from “update zip” option in the recovery) so I can’t get the needed files.

    Does anyone know a place to download an official (or valid, at least) ROM with those files for the i9300?

  5. I think I’ve just found one: Slim 6 for i9300 (https://forum.xda-developers.com/galaxy-s3/development/rom-slimrom-i9300-t3386849). It has almost all the files (no gps.exynos4.vendor.so):

    ./Slim-i9300/system/etc/gps.conf
    ./Slim-i9300/system/etc/gps.xml
    ./Slim-i9300/system/etc/permissions/android.hardware.location.gps.xml
    ./Slim-i9300/system/lib/hw/gps.exynos4.so
    ./Slim-i9300/system/bin/glgps
    ./Slim-i9300/system/bin/gps.cer

    Copied ./system/lib/hw/gps.exynos4.so to ./system/lib/hw/gps.exynos4.vendor.so but I don’t have the proper USB cable to copy files via adb to the device. I’ll report my progresses.

  6. Trying the trick of copying ./system/lib/hw/gps.exynos4.so to ./system/lib/hw/gps.exynos4.vendor.so didn’t work my problem out.

    After 3 days searching the web I couldn’t find neither a ROM that contains that file (gps.exynos4.vendor.so) nor the file itself. 🙁

  7. I finally figured out how to get needed files. Actually, just bumped into it: https://wiki.lineageos.org/extracting_blobs_from_zips.html#extracting-proprietary-blobs-from-block-based-otas

    Downloaded last (14.1) NIGHTLY LineageOS file for i9300. At the present moment they’re releasing 7.*(Nougat), not 6.* (Marshmallow) versions, but following previous instructions got all needed files to upload them to the phone following Simon’s instructions.

    Unfortunately, I have opened SatSat and OsMand~ apps for a while now and S3 can’t find any satellites (I’m indoors, but the phone is near the window that faces a wide open space).

    Recently, my phone fell down and broke the screen glass. Don’t know if GPS sensors have been affected.

  8. Finally got it working (I guess: GPS apps work). I’ll have to try it’s accuracy outdoors, but it finds satellites pretty fast indoors.

    I MUST WARN YOU: Installing proprietary firmwares is always dangerous because you don’t know what the firmware does. I think it’s specially dangerous installing proprietary firmwares of unknown ROMs. If you do so, do it at your own risk!

    I downloaded CRDroid Marshmallow ROM (https://forum.xda-developers.com/galaxy-s3/development/rom-t3320924) and followed https://wiki.lineageos.org/extracting_blobs_from_zips.html#extracting-proprietary-blobs-from-block-based-otas instructions (I couldn’t get to work extract-files.sh but I didn’t need it) and pushed all GPS-related files I found:

    (from the mounted “system”)
    adb push ./bin/glgps /system/bin/
    4366 KB/s (1930424 bytes in 0.431s)
    adb push ./bin/gps.cer /system/bin/
    291 KB/s (15014 bytes in 0.050s)
    adb push ./bin/gps_daemon.sh /system/bin/
    2 KB/s (113 bytes in 0.046s)
    adb push ./etc/gps.conf /system/etc/
    5 KB/s (263 bytes in 0.046s)
    adb push ./etc/gps.xml /system/etc/
    33 KB/s (1591 bytes in 0.046s)
    adb push ./etc/permissions/android.hardware.location.gps.xml /system/etc/permissions/
    20 KB/s (942 bytes in 0.044s)
    adb push ./lib/hw/gps.exynos4.so /system/lib/hw/
    2494 KB/s (59032 bytes in 0.023s)

    \o/

  9. WARNING: Installing proprietary blobs is dangerous because you don’t know what those blobs do and if you install them incorrectly you can brick your phone. Even more if you don’t know or trust the people that provide those blobs. Use the following instructions at your own risk!

    As I couldn’t find any LineageOS 13 EXPERIMENTAL zip to download (those had all the needed files and I trust LineageOS project) I finally downloaded one Marshmallow ROM (Android 6.0) for i9300 I could find (I don’t know if they’re worth my trust, but took thje risk): crdroid (https://forum.xda-developers.com/galaxy-s3/development/rom-t3320924).

    Followed the instructions explained in https://wiki.lineageos.org/extracting_blobs_from_zips.html#extracting-proprietary-blobs-from-block-based-otas, executed “adb remount rw /system” (without “”) and adb “pushed” every “*gps*” file I could find in my mounted system to the same directory with the same name in the /system’s phone.

    Restarted and GPS programs finds satellites pretty fast (SatSat, for instance). I tested it indoors and it’s not as accurate as I would like. I’ll have to test it outdoors.

  10. Replicant has published a new 6.0 version (0003) to fix a memory issue that causes crashes and/or boot loops when installing apps developed/upgraded/compiled with Google’s SDK (API Level 27), so I upgraded Replicant without any issue (I even could install the other non-free firmwares), but when I try to install GPS files and trying to obtain patching file init.target.rc I get this error message (I moved previous dir “t” to “t.old” and unzipped replicant-i9300.zip 0003 version in a newly created “t” dir):

    ***@****:~/Downloads/Replicant/Firmwares/GPS-6.0/t/ramdisk> gzip -dc ../boot.img-ramdisk.gz | cpio -imd | patch < ~/Downloadss/Replicant/Firmwares/GPS-6.0/replicant-6-gps-fix.diff

    can't find file to patch at input line 3
    Perhaps you should have used the -p or –strip option?
    The text leading up to this was:
    ————————–
    |— init.target.rc.orig 2017-03-04 16:04:18.488488467 +0100
    |+++ init.target.rc 2017-03-04 16:04:22.204527828 +0100
    ————————–
    File to patch: 3001 bloques

    Skip this patch? [y]
    Skipping patch.
    1 out of 1 hunk ignored

    I'm not skilled enough to modify (even to understand) the patch to comply with new Replicant version.

    Are files generated during this process when I had Replicant 6.0 0002 images installed (in t.old), still valid to apply to a phone with Replicant 6.0 0003 or is a new patch needed?

    Thanks in advance.

  11. Unfortunately I don’t succeed with you instruction for the Galaxy S2 at the step:

    unpackbootimg -i ./boot.img

    I receive the error message “Android boot magic not found.” with every built of the Replicant 6.0 for the S2 (0001 to latest 0003).

    Do you or does anyone have an idea?

Leave a Reply to EVAnaRkISTO Cancel reply

Your email address will not be published. Required fields are marked *

*