Because I created the video series on how to build custom ROMs and kernels, I get a lot of questions. Most of those questions are exactly the same. One of the most common questions that I get a lot in regards to building custom ROMs for Android phones goes something like this:
“Where do I start? My device doesn’t have anything, no custom ROM, no TWRP, nothing.”
I’m hoping that I can write one answer here and point people to it, to help them know what to do.
I like to tell these individuals that my first recommendation is to watch some of my Youtube videos, and then download the Android source code and build for a known device that already has a working device tree and custom ROM(s). This allows them to ensure that they have properly set up their system, and if their computer is capable of building. It will also give them valuable experience of what right should look like. You can check out my threads with links and helpful information on that here:
Most don’t do this, but even if they do, then comes the next question:
“Where do I start? My device doesn’t have anything, no custom ROM, no TWRP, nothing.”
Yes, we are right back to where we started. Where do they start?
Yesterday, I decided to build something for my new phone, a CAT S42G. To the best of my knowledge, there is no custom ROM or custom recovery, such as LineageOS and TWRP, available for this phone. In fact, there is no record of it being rooted (that I could find). The only reference to flashing this phone with a GSI is in my own blog, where I managed to do so, after much experimentation, since it didn’t work right the first several times through.
So here it is, the definitive guide to go from zero to TWRP with an unrooted phone that has no device tree, custom rom, or TWRP built for it. Maybe later I can write the guide from TWRP to custom rom, but we’ll take this one step at a time.
Prerequisite:
I am using Ubuntu 22.04, and I already am set up to build, but you may not be, so do this on a Ubuntu 22.04 install (either actually installed, live cd, or virtual machine):
$ mkdir ~/bin
$ PATH=~/bin:$PATH
$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
$ chmod a+x ~/bin/repo
$ git config –global user.name “Your Name”
$ git config –global user.email “you@example.com”
$ sudo apt install bc bison build-essential ccache curl flex g++-multilib gcc-multilib git git-lfs gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev libelf-dev liblz4-tool libncurses5 libncurses5-dev libsdl1.2-dev libssl-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev
Alright, now you should be ready to proceed.
Step 1: Get a boot image or recovery image.
You need a boot image or recovery image for your phone in order to make a TWRP device tree. Both have the kernel in it, and ram-disks with supporting information and files that will facilitate making the TWRP device tree. Unless you are really, really good, but then you wouldn’t be reading this article. There are several means available to get a boot/recovery image from your phone.
- From factory provided firmware.
Factory provided firmware can usually be unziped/unrared/etc. and the boot partition or recovery partition can be extracted from it. In this case (CAT S42G), there was no factory firmware available. I even contacted their customer support, to which they said if you needed firmware reflashed, you would have to pay them $30 and send them the phone, but they will not make it available to the public. Bummer.
- Extracting it from the phone.
For security purposes, on a production build (user) you cannot use any standard tool to access the recovery or boot partition on the phone unless it is rooted. There are several means to root the phone:
a. Using KingoRoot or other exploit app. – I tried this on the CAT S42G, and it didn’t work, your mileage may vary.
b. Using Magisk. – But you need the boot image to do this, so this is a no-go for this endevour.
c. Using a GSI or firmware that is already rooted. – I used this method on the CAT S42G. I simply unlocked the bootloader, flashed a GSI with root installed. To read a step by step guide on how I did that, please check out this article: https://alaskalinuxuser3.ddns.net/2023/08/22/install-a-gsi-on-a-cat-s42g/
Once you have root on the phone, you can get the boot image by enabling USB debugging and the following commands from your computer:
$ adb shell
# Gets you into the phone.
$ su
# To become super user
$ dd if=/dev/block/by-name/boot of=/sdcard/boot.img
# Creates a backup of the boot partition on your phone.
$ exit
# Gets you out of the phone.
$ adb pull /sdcard/boot.img ./
# Downloads the boot image to your computer.
You could also do the same for the recovery partition if your phone has one:
$ adb shell
# Gets you into the phone.
$ su
# To become super user
$ dd if=/dev/block/by-name/recovery of=/sdcard/recovery.img
# Creates a backup of the boot partition on your phone.
$ exit
# Gets you out of the phone.
$ adb pull /sdcard/recovery.img ./
# Downloads the boot image to your computer.
In my case there is only a boot partition, so that is what I copied. Some phones use the boot partition for both booting and recovery, some are separated. Either way, now you have the kernel and ramdisk in a boot image.
Step 2: Install and use the TWRP device tree generator to make the initial tree.
Fortunately, it is all easy stuff now. They make a TWRP device tree generator to make the initial device tree. You can download it at:
[URL unfurl=”true”]https://github.com/twrpdtgen/twrpdtgen[/URL]
I run Ubuntu 22.04, and already had pip installed. If you don’t you can install that by following these instructions:
[URL unfurl=”true”]https://phoenixnap.com/kb/how-to-install-pip-on-ubuntu[/URL]
With pip already installed, you can install twrpdtgen with this command:
$ pip3 install twrpdtgen
Then you run it against the boot or recovery image that you got from step 1.
$ python3 -m twrpdtgen ./boot.img
or
$ python3 -m twrpdtgen ./recovery.img
After a minute (depending on your computer) you will have a new folder that is named after your device, in my case, a folder named CAT, with an S42G folder inside. The initial tree looked like this:
https://gitlab.com/alaskalinuxuser/device_cat_s42g/-/tree/e5486a4631b32a2db40bb713bbeecbcfa06239dd
Step 3: Download the minimal twrp manifest and setup for the build.
The instructions for downloading the minimal twrp manifest are here:
[URL unfurl=”true”]https://github.com/minimal-manifest-twrp/platform_manifest_twrp_aosp[/URL]
But here are the important parts:
$ mkdir minTWRP
$ cd minTWRP
$ repo init –depth=1 -u https://github.com/minimal-manifest-twrp/platform_manifest_twrp_aosp.git -b twrp-12.1
$ repo sync
Depending on your internet connection, this may take a while. This is for a shallow clone (depth=1) so it shouldn’t be too bad. Significantly less than 100 GB.
Once it is synced, you need to go into the device folder, and copy your twrpdtgen tree to it. In my case, I put the CAT folder into the device folder, so it is device –> CAT –> S42G folder.
Step 4: Edit your devices tree for TWRP build.
Here is where things get interesting. The TWRP device tree generator made a tree for OmniRom, but if you are following this guide, you need to edit some of the files for the minimal manifest. You can check out the edits I made to the CAT S42G tree here:
https://gitlab.com/alaskalinuxuser/device_cat_s42g/-/commit/ada9ebd1c35f2a5f18ba8cc501c7e539bcd8d3ec
Essentially, you need to rename any file that says omni in the title to be twrp, and change every mention of omni in the device tree files to say twrp instead. Here are some examples:
- PRODUCT_NAME := omni_S42G
- PRODUCT_NAME := twrp_S42G
and
- $(call inherit-product, vendor/omni/config/common.mk)
- $(call inherit-product, vendor/twrp/config/common.mk)
But check out the link above for the full list that I did in the CAT S42G tree, it should only be an edit to 4 or so files.
In my case I also had to edit out the PRODUCT_STATIC_BOOT_CONTROL_HAL lines, as this caused an error with the minimal TWRP for 12.1, as it is depreciated. I simply commented them out by putting a # in front of those lines.
Step 5: Build the image.
Now you are ready to build. All you have to do is run these commands:
$ cd minTWRP # To get into your minimum TWRP directory.
$ export ALLOW_MISSING_DEPENDENCIES=true # To set the flag to not error on missing files.
$ . build/envsetup.sh # To prep the build.
$ lunch twrp_-eng # In my case lunch twrp_S42G-eng
$ mka recovery # To make the recovery
$ mka bootimage # To build the boot image.
Please note that will be whatever is written on the PRODUCT_NAME line of the twrp_.mk file, or on the add_lunch_combo lines of the vendorsetup.sh file. How long this build takes depends on your computer. On mine it is less than 15 minutes.
If there are any errors, you will need to fix them. I can’t cover every possible error, but I had none on my build, except the error on PRODUCT_STATIC_BOOT_CONTROL_HAL lines I mentioned previously. the TWRP device tree generator is usually pretty good.
Step 6: Flash or boot the image.
Now that the build completed successfully, you should test it out. How you do that depends on your phone. Here is what to do if you have a fastboot bootloader:
$ cd out/target/product/ # In my case CAT/S42G/
$ ls # to list the files, you should see boot.img and maybe recovery.img, whichever your device uses.
$ adb reboot bootloader # to tell your phone to go to the bootloader/fastboot mode.
$ fastboot boot ./boot.img # This command allows you to boot it without permanently flashing it.
or
$ fastboot flash boot ./boot.img # to install it on the boot/boot_a/boot_b partition
$ fastboot reboot # to reboot.
or
$ fastboot flash recovery ./recovery.img # to install to the recovery partition, if you have one.
$ fastboot reboot # to reboot.
If you have a Qualcomm device without a fastboot bootloader, you may need to use Odin or Heimdall to flash your image. That is a tutorial in and of itself, but you can read up on it here: https://www.glassechidna.com.au/heimdall/
And after it reboots, you will hopefully see the TWRP splash screen. Note that if you did have a recovery partition, you do have to reboot to recovery to see it, but if you just booted, and not flashed it, it should show up without intervention.
Step 7: Test it out and share with others….
Self explanatory, but you should try all the usual options in your build, and then you should share your work with others, so you can help the next guy, just like I’m helping you now. I recommend putting your device tree on gitlab or github, to make it easy for others to download and use. I’ll post a follow up article with my new CAT S42G TWRP device tree and recovery image as well.
Hopefully that helps answer that “Where do I start?” question….
Linux – keep it simple.
Thanks!