(DEV) How To Compile TWRP Touch Recovery - Android Development and Hacking
(DEV) How To Compile TWRP Touch Recovery - Android Development and Hacking
# Honor 9X # Snapdragon # Xiaomi # OnePlus 7T Pro # Honor # Redmi Note 8 Pro # Pixel 4 # Galaxy Note 10 # ZenFone 6 # Samsung # 5G # Galaxy S20 # Android 11
xda-developers Android Development and Hacking Android Software Development [DEV]How to compile TWRP touch recovery by Dees_Troy
1,705 posts
Thanks:
Thanks: 13,615 Tweet Like
Reply
All of TWRP 3.x source is public. You can compile it on your own. This guide isn't going to be a step-by-step, word-for- Android Software Development
word type of guide. If you're not familiar with basic Linux commands and/or building in AOSP then you probably won't
Android General
be able to do this.
If you are using CM, you'll need to place TWRP in the CM/bootable/recovery-twrp folder and set RECOVERY_VARIANT
Miscellaneous Android Development
:= twrp in your BoardCon g.mk le.
le. TWRP source code can be found here:
https://github.com/omnirom/android_bootable_recovery [More]
Select the newest branch available. This step is not necessary with Omni because Omni already includes TWRP source
by default, however, if you are using an older version of Omni, you will probably want to pull from the latest branch
(the latest branch will compile successfully in older build trees)
If you are only interested in building TWRP, you may want to try working with a smaller tree. You can try using this
manifest. It should work in most cases but there may be some situations where you will need more repos in your tree
than this manifest provides:
https://github.com/minimal-manifest-twrp
Your board con g will need to include architecture and platform settings. Usually these are already included if you're
using device con gs that someone else created, but if you created your own, you may need to add them. Without
them, recovery may seg fault during startup and you'll just see the teamwin curtain ash on the screen over and over.
We usually put all of our ags at the bottom of the BoardCon g.mk under a heading of #twrp For all devices you'll
need to tell TWRP what theme to use. This TW_THEME ag replaces the older DEVICE_RESOLUTION ag. TWRP now
uses scaling to stretch any theme to t the screen resolution. There are currently 5 settings which are: portrait_hdpi,
portrait_mdpi, landscape_hdpi, landscape_mdpi, and watch_mdpi. For portrait, you should probably select the hdpi
theme for resolutions of 720x1280 and higher. For landscape devices,
devices, use the hdpi theme for 1280x720 or higher.
TW_THEME := portrait_hdpi
Note that themes do not rotate 90 degrees and there currently is no option to rotate a theme. If you nd that the
https://forum.xda-developers.com/showthread.php?t=1943625 1/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
touchscreen is rotated relative to the screen,
screen, then you can use some ags (discussed later in this guide) to rotate the
MOST THANKED
touch input to match the screen's orientation.
ALL-TIME RECENT
TW_NO_REBOOT_RECOVERY := true -- removes the reboot recovery button from the reboot menu building with an …
RECOVERY_TOUCHSCREEN_SWAP_XY := true -- swaps the mapping of touches between the X and Y axis Dees_Troy OP 2016-Feb-23 13:24
There are other build ags which you can locate by scanning the Android.mk les in the recovery source. Most of the
other build ags are not often used and thus I won't document them all here.
*RECOVERY.FSTAB*
TWRP 2.5 and higher supports some new recovery.fstab features that you can use to extend TWRP's backup/restore
capabilities. You do not have to add fstab ags as most partitions are handled automatically.
Note that TWRP only supports v2 fstabs in version 3.2.0 and higher. You will still need to use the "old" format of fstab
for older TWRP (example of that format is below), and even TWRP 3.2.0 still supports the v1 format in addition to the v2
format.
format. To maximize TWRP's compatibility with your build tree, you can create a twrp.fstab and use
PRODUCT_COPY_FILES to place the le in /etc/twrp.fstab When TWRP boots, if it nds a twrp.fstab in the ramdisk it
will rename /etc/recovery.fstab to /etc/recovery.fstab.bak and then rename /etc/twrp.fstab to /etc/recovery.fstab.
E ectively this will "replace" the fstab 2 le that your device les are providing with the TWRP fstab allowing you to
maintain compatibility within your device les and with other recoveries. SUGGESTED APPS
Code:
Code: Official XDA Forum App
The XDA App is the fastest
PRODUCT_COPY_FILES += device/lge/hammerhead/twrp.f
way to access the forums on
mobile.
The fstab in TWRP can contain some " ags" for each partition listed in the fstab.
fstab.
Navigation Gestures
Customizable gesture control
Here's a sample TWRP fstab for the Galaxy S4 that we will use for reference: for any Android device.
Code:
Code:
in the backup/restore list while 0 ensures that this partition will not show up in the backup list. NFCScreenOff mod enables scanning NFC tags
wipeingui -- makes the partition show up in the GUI to allow the user to select it for wiping in the advanced wipe when your Android phone is locked
menu January 22, 2020
wipeduringfactoryreset -- the partition will be wiped during a factory reset
1Controller Magisk Module adds PlayStation
ignoreblkid -- blkid is used to determine what le system is in use by TWRP, this ag will cause TWRP to skip/ignore
Dualshock and Xbox game controller support to
the results of blkid and use the le system speci ed in the fstab only rooted Android devices
retainlayoutversion -- causes TWRP to retain the .layoutversion le in /data on devices like Sony Xperia S which sort of January 22, 2020
uses /data/media but still has a separate /sdcard partition
symlink= -- causes TWRP to run an additional mount command when mounting the partition, generally used with More Forum Links
/data/media to create /sdcard
display=
display= -- sets a display name for the partition for listing in the GUI
storagename= -- sets a storage name for the partition for listing in the GUI storage list
backupname= -- sets a backup name for the partition for listing in the GUI backup/restore list
length= -- usually used to reserve empty space at the end of the /data partition for storing the decryption key when
Android's full device encryption is present, not setting this may lead to the inability to encrypt the device
canencryptbackup= -- 1 or 0 to enable/disable, makes TWRP encrypt the backup of this partition if the user chooses
encryption (only applies to tar backups, not images)
userdataencryptbackup= -- 1 or 0 to enable/disable, makes TWRP encrypt only the userdata portion of this partition,
certain subfuldes like /data/app would not be encrypted to save time
subpartitionof= -- must be succeeded by the equals sign and the path of the partition it is a subpartition of. A
subpartition is treated as "part" of the main partition so for instance, TWRP automatically makes /datadata a
subpartition of /data. This means that /datadata will not show up in the GUI listings, but /datadata would be wiped,
backed up, restored, mounted, and unmounted anytime those operations are performed on /data. A good example of
the use of subpartitions is the 3x efs partitions on the LG Optimus G:
Code:
Code:
/efs1 emmc /dev/block/mmcblk0p12 flags=b
/efs2 emmc /dev/block/mmcblk0p13 flags=b
/efs3 emmc /dev/block/mmcblk0p14 flags=b
This lumps all 3 partitions into a single "EFS" entry in the TWRP GUI allowing all three to be backed up and restored
together under a single entry.
As of TWRP 3.2.0, TWRP now supports a version 2 fstab like those that have been found in Android devices for years.
Yes, I know we're really slow to adopt this one, but I also saw no major advantage to v2 and the v2 fstab was being
used in regular Android as well as recovery and I didn't want full ROM builds crashing or doing other weird things
because of TWRP ags being present in the fstab.
fstab. Version 2 fstab support is automatic. You don’t need to add any
build ags. The regular version 1 fstab format is also still valid and it’s possible to use both v1 and v2 types in the same
fstab.
fstab. TWRP 3.2.0 also supports using wildcards via the asterisk in v1 format which can be useful for USB OTG and micro
SD cards with multiple partitions. Note also that v2 fstab formats haven’t been extensively tested so developers should
test their v2 fstabs before shipping to users (you should always be testing anyway!).
This is a v1 fstab line with a wildcard intended for a USB OTG drive.
drive. All partitions should show up in the list of available
storage devices when the user plugs in a drive:
drive:
Code:
Code:
/usb-otg vfat /dev/block/sda* flags=removable;
This line is straight from the v2 fstab for the same device and also should work. In this case the kernel will notify us that
new devices have been added or removed via uevents:
Code:
Code:
/devices/soc.0/f9200000.ssusb/f9200000.dwc3/xhci-h
Code:
Code:
# Android fstab file.
file.
#<src>
https://forum.xda-developers.com/showthread.php?t=1943625 3/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
# The filesystem that contains the filesystem chec
# specify MF_CHECK, and must come before any files
/dev/block/bootdevice/by-name/system /system
/dev/block/bootdevice/by-name/cust /cust ext
/devices/hi_mci.1/mmc_host/mmc1/*
/devices/hisi-usb-otg/usb1/*
/d /bl k/b td i /b / d t /d
Code:
Code:
/boot emmc /dev/block/platform/hi_mc
/recovery emmc /dev/block/platform/hi_mc
/cust ext4 /dev/block/platform/hi_mc
/misc emmc /dev/block/platform/hi_mc
/oeminfo emmc /dev/block/platform/hi_mc
/data f2fs /dev/block/dm 0
The rst 2 lines in twrp. ags adds the boot and recovery partitions which were not present at all in the v2 fstab.
fstab. The
/cust line in the twrp. ags le is added to tell TWRP to allow users to back up the cust partition and to give it a slightly
better display name. The /misc partition is also only present in the twrp. ags le.
le. Much like the /cust partition, the
/oeminfo partition is in the twrp. ags le to tell TWRP to allow users to back it up and give a display name. The /data
line is needed because this Huawei device,
device, like many Huawei devices,
devices, is encrypted but the encryption uses some
special Huawei binaries and is encrypted with some sort of default password that the user cannot change. We use the
Huawei binaries to decrypt the device automatically in recovery. The /data line here tells TWRP to use /dev/block/dm-
0 instead of /dev/block/bootdevice/by-name/userdata which is required for proper mounting, etc. Lastly we have the
/system_image line so that TWRP will add a system image option for backup and restore.
If you have code changes that you'd like to submit, please submit them through the Omni Gerrit server.
server. Guide is here.
here.
Once you get Omni or CM sync'ed and your TWRP ags set, you should do a source ./build/envsetup.sh We usually
lunch for the device in question, so something like "lunch omni_hammerhead-eng".
After you lunch successfully for your device this is the command used for most devices:
devices:
Code:
Code:
make clean && make -j# recoveryimage
Replace the # with the core count +1, so if you have a dual core it's -j3 and a quad core becomes -j5, etc. If you're
dealing with a "typical" Samsung device,
device, then you'll need to
Code:
Code:
make -j# bootimage
Most Samsung devices have the recovery included as an extra ramdisk in the boot image instead of a separate
recovery partition as found on most other devices.
devices.
The
The Following
Following 402
402 Users
Users Say
Say Thank
Thank You
You to
to Dees_Troy
Dees_Troy For
For This
This Useful
Useful Post:
Post: [[ View
View ]]
Dees_Troy
Thanks:
Thanks: 13,615
OP
OP Senior Recognized Developer
https://forum.xda-developers.com/showthread.php?t=1943625 4/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
So, now, hopefully you've compiled TWRP for your device and gotten it working. Now, you'd like to know how to get
TWRP o cially supported for your device so that it can be installed automatically with the TWRP app.
app. In order for us to
add "o cial support" for your device we'll need the following:
1) Device con guration les to compile TWRP from source for your device.
device. This means that you cannot have repacked a
recovery.img by hand to get it working. We need to be able to compile it from source so that we can easily release
future updates.
2) We'll build a copy of TWRP and send it to you for validation. Once you've validated that we can build a working
image for your device,
device, we'll add it to the o cial TWRP app.
app.
You can now boot TWRP in an emulator. If you're trying to help develop TWRP, this can be a huge help as you don't
have to risk your device and you can do everything directly on your computer.
computer.
Once you have your AVD and your recoveryimage, you can boot TWRP in the emulator by browsing to your android-
sdk/tools folder and run this command:
./emulator -avd TWRP -ramdisk CMFOLDER/out/target/product/twrp/ramdisk-recovery.img
Note that ADB doesn't work right away. About 10 to 15 seconds after TWRP nishes booting, ADB will come online. We
start ADB via init.rc so even if TWRP fails to boot due to some kind of code error that you may have made, ADB should
still work. Enjoy!
The
The Following
Following 119
119 Users
Users Say
Say Thank
Thank You
You to
to Dees_Troy
Dees_Troy For
For This
This Useful
Useful Post:
Post: [[ View
View ]]
Dees_Troy
Thanks:
Thanks: 13,615
OP
OP Senior Recognized Developer
From a TWRP standpoint, A/B devices aren't a whole lot di erent from regular devices,
devices, but developers seem to be shy
about working on these devices.
devices. I'm going to try to shed some light on this subject and hopefully this will serve as a
https://forum.xda-developers.com/showthread.php?t=1943625 5/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
guide for porting TWRP to A/B devices.
devices.
Firstly, let's understand what is an A/B device and how it's di erent. A/B devices have duplicates of many partitions on
the device.
device. An A/B device has 2x system partitions, 2x boot partitions, 2x vendor partitions, 2x modem / rmware
partitions, etc. Only one slot is in use at a time. During early boot,
boot, the rst stages of the bootloader read some small
amount of data called the BCB or Bootloader Control Block and decide whether to boot the A partitions or the B
partitions. When an OTA update is available, the data from the active slot is copied from the inactive slot and patched
/ updated. For example, if you're currently on slot A, your device would download the update and copy the existing
system partition from slot A and patch / update it with the new updates into slot B. Once the copying and updating is
complete, the BCB is updated and the device reboots using slot B. Next time an update is available, the system
partition in slot B is copied to slot A and updated, the BCB gets updated, and we reboot to slot A. When viewing
partitions on the device,
device, you'll see something like this:
Code:
Code:
/dev/block/bootdevice/by-name/boot_a
/dev/block/bootdevice/by-name/boot_b
/dev/block/bootdevice/by-name/system_a
/dev/block/bootdevice/by-name/system_b
/dev/block/bootdevice/by-name/userdata
/dev/block/bootdevice/by-name/vendor_a
While there is technically no requirement that I am aware of, all A/B devices shipped thus far have no separate
recovery partition. Instead, the boot image contains the recovery in its ramdisk.
ramdisk. The important thing is knowing that
the boot image now also contains the recovery. For completeness, the system partition is a full root le system.
system. During
boot,
boot, if the kernel is told to boot to recovery, it will extract the ramdisk in the boot partition. If the kernel is not told by
the bootloader to boot to recovery, then the kernel will mount the appropriate system partition (A or B) because the
system partition is a full root le system.
system. This means that the system partition on these devices is mounted to / instead
of to /system and the system partition contains all of the les that would have normally been in the boot image
ramdisk and a /system subfolder.
From a TWRP standpoint, there are 3 things that you have to do for an A/B device.
device. First, you need to set
Code:
Code:
AB_OTA_UPDATER := true
in your BoardCon g.mk. Secondly, for any partition that has an A/B option, you need to add
Code:
Code:
flags=slotselect
Lastly, once you get into TWRP, you will probably want to make sure that bootctl hal-info responds correctly with no
errors. Usually the bootctl binary requires a proprietary library or even a couple of services to work correctly. If bootctl
does not work correctly, then you will not be able to switch slots within TWRP correctly either.
In addition to setting
Code:
Code:
AB_OTA_UPDATER := true
If you set
Code:
Code:
BOARD_USES_RECOVERY_AS_BOOT := true
https://forum.xda-developers.com/showthread.php?t=1943625 6/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
then make recoveryimage will no longer work and instead you will have to make bootimage. I don't recommend
setting either of these ags for TWRP-only build trees. These ags will probably be required for developers building full
ROMs for A/B devices.
devices.
Since all known A/B devices do not have a separate recovery partition, you will eventually have to ash TWRP to the
boot partition. On the Pixel 1 and 2, we use fastboot boot to temporarily boot TWRP without ashing TWRP. We are
then supplying a zip to allow users to ash TWRP to both slots. You can download one of these zips from our website
and update the zip as needed to support your devices.
devices. Eventually we will add tools to TWRP to allow users to ash
recoveries on these devices without needing to use zips.
then use
Code:
Code:
fastboot --set-active=_a
to switch slots to the inactive slot. From here, the user can
Code:
Code:
fastboot flash boot twrp.img && fastboot reboot
to get into TWRP. Once in TWRP they can then go to the reboot page and change back to their originally active slot,
slot,
make a backup,
backup, then install TWRP. Using the inactive slot allows users to get a good, unmodi ed backup of their device
before installing TWRP.
The
The Following
Following 116
116 Users
Users Say
Say Thank
Thank You
You to
to Dees_Troy
Dees_Troy For
For This
This Useful
Useful Post:
Post: [[ View
View ]]
ViViDboarder
Thanks:
Thanks: 632
Inactive Recognized Developer
The
The Following
Following 99 Users
Users Say
Say Thank
Thank You
You to
to ViViDboarder
ViViDboarder For
For This
This Useful
Useful Post:
Post: [[ View
View ]] Gift
Gift ViViDboarder
ViViDboarder Ad-Free
Ad-Free
sgt. meow
Thanks:
Thanks: 3,014
Senior Member
https://forum.xda-developers.com/showthread.php?t=1943625 7/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
mfsr98
Thanks:
Thanks: 290
Senior Member
Quote:
Originally Posted by Dees_Troy
Once you get CM sync'ed, I suggest that you go ahead and compile a build of ClockworkMod (ClockworkMod
source is included in the CM repos). We usually lunch for the device in question, so something like "lunch
full_tenderloin-eng".
After you lunch successfully for your device this is the command used for most devices:
devices:
Code:
Code:
make clean && make -j# recoveryimage
I don't get this part. What is lunch supposed to be? is it supposed to be a individual command for a device or can I do
"lunch full_tenderloin-eng" for all devices?
devices? Because I am building for a device that doesn't run Android natively (at least
out of-the-box) so cyanogenmod doesn't support it on any way...
Also, can I use this for resistive screens? Just to make sure.
Thanks for the guide, I have been looking for this for a long time
The
The Following
Following 44 Users
Users Say
Say Thank
Thank You
You to
to mfsr98
mfsr98 For
For This
This Useful
Useful Post:
Post: [[ View
View ]] Gift
Gift mfsr98
mfsr98 Ad-Free
Ad-Free
manuel100
Thanks:
Thanks: 30
Senior Member
The
The Following
Following User
User Says
Says Thank
Thank You
You to
to manuel100
manuel100 For
For This
This Useful
Useful Post:
Post: [[ View
View ]] Gift
Gift manuel100
manuel100 Ad-Free
Ad-Free
mg2195 Thanks:
Thanks: 2,610
Senior Member
i have only a question: i'm trying to build it for galaxy 5, the problem is that the resolution is 320x240, so can you
tip me on modify the sources? (i've done this some days ago, but images were too large)
https://forum.xda-developers.com/showthread.php?t=1943625 8/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
Set the device resolution to 320x240...I'm pretty sure they have support for that resolution...and if they don't then you
can copy one of there's except resize it all to 320x240
My question: I built twrp from source a while ago using smasher's Samsung inject twrp zip...that
zip...that worked ne for a
while...but recently we switched to mtd format so that inject twrp zip doesn't work...neither does the inhect twrp
button within twrp anymore...I've seen that the galaxy s has the same problem as well...just wondering if you know of a
work around...
manuel100
Thanks:
Thanks: 30
Senior Member
Set the device resolution to 320x240...I'm pretty sure they have support for that resolution...and if they don't
then you can copy one of there's except resize it all to 320x240
My question: I built twrp from source a while ago using smasher's Samsung inject twrp zip...that
zip...that worked ne for
a while...but recently we switched to mtd format so that inject twrp zip doesn't work...neither does the inhect
twrp button within twrp anymore...I've seen that the galaxy s has the same problem as well...just wondering if
you know of a work around...
thanks for respond, however i've tried resizing images to 320x240 but i don't know what should be the size of bottoms,
because they are alway out-of-screen
mg2195
Thanks:
Thanks: 2,610
Senior Member
thanks for respond, however i've tried resizing images to 320x240 but i don't know what should be the size of
bottoms, because they are alway out-of-screen
Smashers inject twrp zip is a tool for Samsung devices that let's you get the recovery without ashing the whole
kernel. You basically put the ramdisk-recovery.img in the zip and then ash.
ash. During the ash process it pulls the
boot.img, replaces the current ramdisk-recovery.img with the new one that contains twrp. It then deletes the old
boot.img and ashes the new one...not a ecting the actual kernel...
The recovery itself has mtd support...I can use it if I ash the whole boot.img, but I don't want to release the recovery
within a whole kernel...because then I have to keep it up to date with both twrp and kernel sources, not to mention the
di erent kernels available for my device....I
device....I can't make a recovery for each kernel...don't have the time for that...that's
where the inject came in handy....the user could use any kernel he wanted while still using twrp
Recovery logs say it fails to nd the ramdisk inside the boot.img and then in parenthesis something about may not be
using gzip compression...which it is....
https://forum.xda-developers.com/showthread.php?t=1943625 9/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
Sent from my SGH-I997 using Tapatalk 2
----------
---------- Post
Post added
added at
at 07:21
07:21 AM
AM ----------
---------- Previous
Previous post
post was
was at
at 07:19
07:19 AM
AM ----------
----------
Deestroy did this same tutorial on rootzwiki...if you look at the second post you'll see info about the smasher inject
twrp I was referring to...http://rootzwiki.com/index.php?/topi...RP-from-Source
to...http://rootzwiki.com/index.php?/topi...RP-from-Source
The
The Following
Following 44 Users
Users Say
Say Thank
Thank You
You to
to mg2195
mg2195 For
For This
This Useful
Useful Post:
Post: [[ View
View ]] Gift
Gift mg2195
mg2195 Ad-Free
Ad-Free
mfsr98
Thanks:
Thanks: 290
Senior Member
I don't get this part. What is lunch supposed to be? is it supposed to be a individual command for a device or
can I do "lunch full_tenderloin-eng" for all devices?
devices? Because I am building for a device that doesn't run Android
natively (at least out of-the-box) so cyanogenmod doesn't support it on any way...
Also, can I use this for resistive screens? Just to make sure.
Thanks for the guide, I have been looking for this for a long time
The
The Following
Following User
User Says
Says Thank
Thank You
You to
to mfsr98
mfsr98 For
For This
This Useful
Useful Post:
Post: [[ View
View ]] Gift
Gift mfsr98
mfsr98 Ad-Free
Ad-Free
Message:
https://forum.xda-developers.com/showthread.php?t=1943625 10/11
2/26/2020 [DEV]How to compile TWRP touch recovery | Android Development and Hacking
[GApps][ARM64] FlameGApps for Android [9.0+10.0] 2019-12-17
[APP][MOD] Today Weather - A beautiful weather app with more 10 data sources[P-CODES] 15th September 2019
xda-developers Android Development and Hacking Android Software Development [DEV]How to compile TWRP touch recovery by Dees_Troy
We're Social
XDA Developers was founded by developers, for developers. It is now a
valuable resource for people who want to make the most of their mobile
devices, from customizing the look and feel to adding new functionality.
Are you a developer? | Terms of Service
More info
-- XDA 2015
Contact Advertise Rules Suggest Content Security Privacy Policy XDA App Remove ads on XDA
Root Any Device How To Guides XDA's Best
Devices
https://forum.xda-developers.com/showthread.php?t=1943625 11/11