Installing Android TWRP recovery image using Heimdall on Linux
Introduction
Cellular phones are like small computers, they have a CPU, RAM and disk space. Disk space is divided into partitions, each housing some kind of a file system. Regardless of the file system type each partition can be saved as a binary image, a collection of zeros and ones. If you can get hold of an image compatible with your phone/operating system you can replace each of those partitions using Heimdall.
TWRP recovery image is a custom partition image built for many phone models that provides useful functionality. Visit their website here.
Why do this?
In my case my phone (Samsung Galaxy S6) stopped booting up and froze on "Powered by Android" screen. I also lost ability to download data over USB cable. Replacing recovery partition with TWRP recovery image allowed me to download user data from the phone.
Prerequisites
Download TWRP recovery image
Find make and model of your phone. On Android phone open Settings application and scroll down to "About device" item. In there look for "Device name", "Model number" and "Android version", write those down.
Download TWRP recovery image for your phone.
Using Samsung Galaxy S6 model SM-G920F as an example:
Go to TWRP Samsung Galaxy S6 page.
Open "Download Links", look for "International GSM (SM-G920F, SM-G920FD, SM-G920I)", "Primary (Americas)". Download "twrp-3.3.1-0-zeroflte.img.tar".
Extract downloaded tar file, you should see "recovery.img" file:
tar xf twrp-3.3.1-0-zeroflte.img.tar
Write down location of extracted "recovery.img" file (folder name).
Install Heimdall program
On Linux command line run this command as root user:
apt install heimdall
Get partition information from your phone
Connect your phone to computer using USB cable. Put phone in image upload ("Odin") mode by pressing "Volume Down", "Power" and "Home" buttons simultaneously and holding them until screen asking about image upload appears, then release all buttons and press "Volume Up" once. Screen with "Uploading" message should show up.
Run these commands as root user on Linux command line:
heimdall detect --verbose
Should print "device detected", that's all.
Now display partition table of your phone with this command:
heimdall print-pit
You should see a list of entries similar to this one:
--- Entry #9 ---
Binary Type: 0 (AP)
Device Type: 8 (Unknown)
Identifier: 6
Attributes: 5 (Read/Write)
Update Attributes: 1 (FOTA)
Partition Block Size/Offset: 17408
Partition Block Count: 8704
File Offset (Obsolete): 0
File Size (Obsolete): 0
Partition Name: RECOVERY
Flash Filename: recovery.img
FOTA Filename:
That is the partition you're looking for, with "Partition Name" = "RECOVERY".
Now download partition table to a file (here called "phone.pit"):
heimdall download-pit --no-reboot --output phone.pit
Replace recovery partition with TWRP image
You should backup your phone data before this step, although replacing recovery partition only should not cause any data loss. User data is stored on another partition. Phone should be still connected to your computer with USB cable and in upload ("Odin") mode (see above).
Replace recovery partition with data from recovery image file:
heimdall flash --RECOVERY recovery.img --pit phone.pit
That should be quick, you can now enter recovery mode by pressing "Home", "Power" and "Volume Up" buttons simultaneously That should give you access to TWRP recovery console, plus now you can copy files from your phone to your computer over USB when in recovery mode.
Here is an additional example of writing partition data, this one for installing "wanam lite" mod on SM-G920F:
heimdall flash --RADIO modem.bin --BOOTLOADER sboot.bin --CM cm.bin
Where "RADIO" and "BOOTLOADER" are partition names obtained from partition table list.