[ASUS ZenFone 8] bnsmb:
How to install TWRP via script

The recovery image TWRP is very useful for the maintenance of the Android OS. You can either boot the phone from the TWRP image without installing it using these commands:

adb reboot bootloader
sudo fastboot boot ./twrp-3.7.0_12-0-I006D.img

or install the TWRP image into the boot partition and use the command

adb reboot recovery

or

sudo fastboot boot recovery

to boot into TWRP. Depending on the OS used there is also an option in the reboot dialog in the GUI to reboot into the recovery.

While the phone is booted from TWRP you can open a shell as user root via adb.

The only official method to install TWRP into the boot partition of the phone is via the TWRP GUI, e.g.:

boot the phone from the TWRP image (see above)

install TWRP into the boot partition using the menu entry "Advanced" and then "Flash Current TWRP". TWRP will then install itself into both boot partitions.

There is no method to install TWRP without user interaction via a script (at least I did not found any ...). Therefor I wrote a shell script to install TWRP without user input:

install_twrp_from_within_twrp.sh

The script install_twrp_from_within_twrp.sh can be used to install TWRP either into a boot partition or into an image file for the boot partition. The script must be executed in a shell on the phone while the phone is booted from the TWRP image that should be installed.

The usage of the script is:

ASUS_I006D:/data/local/tmp $ ./install_twrp_from_within_twrp.sh -h

install_twrp_from_within_twrp.sh - script to install TWRP into a boot partition or into a file

Usage:

install_twrp_from_within_twrp.sh [-f] [boot_image_file] [new_boot_image_file]

boot_image can be either an image file from the boot partition or a boot partition block device
(e.g. /dev/block/by-name/boot_a)
new_boot_partition_file must be the absolute name of a file.

Both parameter are optional
If the script is called without a parameter TWRP will be installed in the current boot partition.
If boot_image_file is a block device and new_boot_image_file is missing TWRP will be installed in the block device.
If the parameter new_boot_image_file is used the script will only write a boot partition image with TWRP into that file.

Use the parameter -f to ignore missing or invalid checksums.

This script must run in a shell in TWRP

Environment variables used by the script if set:

BIN_DIR (Current value: "/system/bin")
TMPDIR (Current value: "/data/local/tmp")

|ASUS_I006D:/data/local/tmp $

The commands used in the script install_twrp_from_within_twrp.sh to install TWRP are copied from the TWRP source file with the code to install TWRP:

https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp

Note that TWRP contains a copy of the binary magiskboot to unpack and repack the boot partition. Therefor the installation should work on all phones that are supported by Magisk.

Be aware that the installation of TWRP will wipe out the installation of Magisk (or any other tool for enabling root access). So for installing TWRP and Magisk they must be installed in this order:

install TWRP

install Magisk (or a similar tool)

Example usage:

ASUS_I006D:/ # /cache/install_twrp_from_within_twrp.sh /dev/block/by-name/boot_b

Installing TWRP into a boot image or boot partition via script

Checking the running OS ...
OK, running a shell in TWRP: "twrp_I006D" version "3.7.0_12-0"
Checking the prerequisites for installing TWRP ...
Using the partition "/dev/block/by-name/boot_b" as source for the installation of TWRP
Creating the new boot image with TWRP in the file "/data/local/tmp/img.568/new-boot.img"
Checking if the required executables exist ...
OK, the file "/system/bin/magiskboot" exists and is executable
OK, the file "/system/bin/cpio" exists and is executable
OK, the file "/system/bin/sha256sum" exists and is executable
OK, the file "/system/bin/getprop" exists and is executable
OK, the file "/system/bin/dd" exists and is executable
Checking if the requried data files exist ...
OK, the file "/ramdisk-files.sha256sum" exists
OK, the file "/ramdisk-files.txt" exists
OK, the file "/dev/block/by-name/boot_b" exists
Checking the check sums of the files for the new ramdisk ...
OK, the check sums of the files for the new ramdisk are okay
Directory "/data/local/tmp" successfully created
Directory "/data/local/tmp/img.568" successfully created
Directory "/data/local/tmp/img_files.568" successfully created
The temporary directory to unpack the boot image is "/data/local/tmp/img_files.568"
Unpacking the boot image from "/dev/block/by-name/boot_b" ...
Parsing boot image: [/dev/block/by-name/boot_b]
HEADER_VER [3]
KERNEL_SZ [42027520]
RAMDISK_SZ [34080747]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
OK, "/dev/block/by-name/boot_b" successfully unpacked to "/data/local/tmp/img_files.568" :
Creating a backup of the original ramdisk ...
total 74412
-rw-rw-rw- 1 root root 50 2022-10-24 19:35 header
-rw-r--r-- 1 root root 42027520 2022-10-24 19:35 kernel
-rw-r--r-- 1 root root 34080747 2022-10-24 19:35 ramdisk.cpio.org
Creating the new ramdisk with TWRP ...
New ramdisk with TWRP "/data/local/tmp/img_files.568/ramdisk.cpio" successfully created.

-rw-rw-rw- 1 root root 85852380 2022-10-24 19:35 /data/local/tmp/img_files.568/ramdisk.cpio
-rw-r--r-- 1 root root 34080747 2022-10-24 19:35 /data/local/tmp/img_files.568/ramdisk.cpio.org

Repacking the boot image into the file "/data/local/tmp/img.568/new-boot.img" ...
+ cd /data/local/tmp/img_files.568
+ /system/bin/magiskboot repack /dev/block/by-name/boot_b /data/local/tmp/img.568/new-boot.img
Parsing boot image: [/dev/block/by-name/boot_b]
HEADER_VER [3]
KERNEL_SZ [42027520]
RAMDISK_SZ [34080747]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
Repack to boot image: [/data/local/tmp/img.568/new-boot.img]
HEADER_VER [3]
KERNEL_SZ [42027520]
RAMDISK_SZ [34080769]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
+ TEMPRC=0
+ set +x
OK, the new boot image "/data/local/tmp/img.568/new-boot.img" successfully created
-rw-r--r-- 1 root root 100663296 2022-10-24 19:35 /data/local/tmp/img.568/new-boot.img
Now rewriting "/dev/block/by-name/boot_b" using "/data/local/tmp/img.568/new-boot.img" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 4.498630 s, 21 M/s
TWRP successfully installed in "/dev/block/by-name/boot_b"
ASUS_I006D:/ #

To install TWRP using the script without user input these steps are neccessary:

reboot the phone into the bootloader

boot the phone from the TWRP image that should be installed on the phone

copy the script install_twrp_from_within_twrp.sh to the phone

execute the script install_twrp_from_within_twrp.sh on the phone in an adb shell

To automate these steps I wrote a wrapper script :

install_twrp.sh

The wrapper script install_twrp.sh must be executed on a machine running Linux with the phone attached via USB.

The wrapper script will check the current status of the phone, boot the phone from the TWRP image, copy the script install_twrp_from_within_twrp.sh to the phone and execute it on the phone to install TWRP into either the active or the inactive boot partition.

The script install_twrp_from_within_twrp.sh must be in the same directory as install_twrp.sh (if not set the variable TWRP_INSTALL_SCRIPT before executing the script -- see the script usage below).

The usage for the script install_twrp.sh is:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./install_twrp.sh -h
./install_twrp.sh version - 1.0.0.0 - install TWRP to the boot partition of a phone running Android using TWRP

Usage

install_twrp_via_twrp.sh [boot_slot] [wait=n] [dd|fastboot] [twrp_image]

All parameter are optional

The parameter "boot_slot" can be a, b, active, inactive, next, current; default is the current boot slot of the phone

Use the parameter "dd" to request repatching via dd in an adb session ; use the parameter "fastboot" to request repatching via "fastboot"
Default is to use "dd" to flash the patched boot image

The value for the parameter "wait" is the number of seconds to wait before starting the script "install_twrp_from_within_twrp.sh" on the phone
This seems to be necessary to avoid errors while repacking the boot image. The default wait time is 10 seconds.

The parameter "twrp_image" can be used to define another TWRP image to use. The parameter is optional - the
default for "twrp_image" is hardcoded in the script (variable TWRP_IMAGE)

The default TWRP image of the script is the TWRP for the ASUS Zenfone 8.

The phone to patch must be attached via USB.
The phone can be either in fastboot mode, in normal mode with enabled adb support, or already booted from the TWRP image

The script uses the script "install_twrp_from_within_twrp.sh" to install TWRP. The script install_twrp_from_within_twrp.sh must
be in the same directory as this script. The script will be copied to the phone and then executed on the phone.
Set the variable TWRP_INSTALL_SCRIPT to the name of the script to use before starting this script if another script should be used .

To change some of the values used by the script you can set environment variables before starting the script:

Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after patching the new image
Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after patching the new image.

Set the environment variable SERIAL_NUMBER to the serial number of the phone to patch if there is more then one phone connected via USB

Set the environment variable ADB_OPTIONS to the options to be used with the adb command

Set the environment variable FASTBOOT_OPTIONS to the options to be used with the fastboot command

Set the environment variable TMP_DIR_ON_THE_PHONE to the temporary directory to use on the phone

The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
The default script executed on the phone to install TWRP is "./install_twrp_from_within_twrp.sh"

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$

Without any parameter the script install_twrp.sh will install TWRP into the active boot partition.

Example:

[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$ ./install_twrp.sh
./install_twrp.sh version - 1.0.0.0 - install TWRP to the boot partition of a phone running Android using TWRP

Using the TWRP image hardcoded in the script: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Checking the script prerequisites ...
Will wait 10 second(s) before starting the script on the phone
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will patch the boot partition on the attached phone with the serial number "M6AIB760D0939LX"
Using the options "-d -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking for a connected phone booted into fastboot mode ...
No attached phone in fastboot mode found
Checking for an attached phone with working access via adb (USB) ...
Linux localhost 5.4.147-Omni-qgki-perf-gea6f815f5af9-dirty #46 SMP PREEMPT Sat Oct 15 08:38:07 CEST 2022 aarch64
... found a phone connected via USB with working adb access
The phone is booted in normal mode
Booting the phone into the fastboot mode now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.500s]
Booting OKAY [ 10.440s]
Finished. Total time: 12.983s
Waiting up to 60 seconds for the boot of the phone ....
Retrieving the current boot slot from the phone ...
The current boot slot is "_a"
The boot slot to patch is "_a"
The boot partition to patch is "boot_a"
Checking if the directory "/data/local/tmp" exists on the phone ....
/data/local/tmp
Copying the script "./install_twrp_from_within_twrp.sh" to the phone ...
./install_twrp_from_within_twrp.sh: 1 file pushed, 0 skipped. 216.6 MB/s (13141 bytes in 0.000s)
Creating the boot image file "/data/local/tmp/boot_a.327964.img" from the partition "/dev/block/by-name/boot_a" ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 0.311720 s, 308 M/s

Checking the result ...
-rw-rw-rw- 1 root root 100663296 2022-10-24 18:02 /data/local/tmp/boot_a.327964.img
The check sums are:
The check sum of the boot partition "/dev/block/by-name/boot_a" on the phohe is "728455636"
The check sum of the boot image file on the phone is "/data/local/tmp/boot_a.327964.img" is "728455636"
Installing TWRP using the boot image file "/data/local/tmp/boot_a.327964.img" ...

----------------------------------------------------------------------
[2022.10.24 20:02:43] Waiting now 10 seconds ...
+ adb -d -s M6AIB760D0939LX shell /data/local/tmp/install_twrp_from_within_twrp.sh /data/local/tmp/boot_a.327964.img /data/local/tmp/boot_a_witn_twrp.327964.img

Installing TWRP into a boot image or boot partition via script

Checking the running OS ...
OK, running a shell in TWRP: "twrp_I006D" version "3.7.0_12-0"
Checking the prerequisites for installing TWRP ...
Using the file "/data/local/tmp/boot_a.327964.img" as source for the installation of TWRP
Creating the boot image with TWRP in the file "/data/local/tmp/boot_a_witn_twrp.327964.img"
Checking if the required executables exist ...
OK, the file "/system/bin/magiskboot" exists and is executable
OK, the file "/system/bin/cpio" exists and is executable
OK, the file "/system/bin/sha256sum" exists and is executable
OK, the file "/system/bin/getprop" exists and is executable
Checking if the requried data files exist ...
OK, the file "/ramdisk-files.sha256sum" exists
OK, the file "/ramdisk-files.txt" exists
OK, the file "/data/local/tmp/boot_a.327964.img" exists
Checking the check sums of the files for the new ramdisk ...
OK, the check sums of the files for the new ramdisk are okay
Directory "/data/local/tmp" successfully created
Directory "/data/local/tmp/img.583" successfully created
Directory "/data/local/tmp/img_files.583" successfully created
The temporary directory to unpack the boot image is "/data/local/tmp/img_files.583"
Unpacking the boot image from "/data/local/tmp/boot_a.327964.img" ...
Parsing boot image: [/data/local/tmp/boot_a.327964.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34080743]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
OK, "/data/local/tmp/boot_a.327964.img" successfully unpacked to "/data/local/tmp/img_files.583" :
Creating a backup of the original ramdisk ...
total 74412
-rw-rw-rw- 1 root root 50 2022-10-24 18:02 header
-rw-r--r-- 1 root root 42025472 2022-10-24 18:02 kernel
-rw-r--r-- 1 root root 34080743 2022-10-24 18:02 ramdisk.cpio.org
Creating the new ramdisk with TWRP ...
New ramdisk with TWRP "/data/local/tmp/img_files.583/ramdisk.cpio" successfully created.

-rw-rw-rw- 1 root root 85852380 2022-10-24 18:02 /data/local/tmp/img_files.583/ramdisk.cpio
-rw-r--r-- 1 root root 34080743 2022-10-24 18:02 /data/local/tmp/img_files.583/ramdisk.cpio.org

Repacking the boot image into the file "/data/local/tmp/boot_a_witn_twrp.327964.img" ...
+ cd /data/local/tmp/img_files.583
+ /system/bin/magiskboot repack /data/local/tmp/boot_a.327964.img /data/local/tmp/boot_a_witn_twrp.327964.img
Parsing boot image: [/data/local/tmp/boot_a.327964.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34080743]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
KERNEL_FMT [raw]
RAMDISK_FMT [gzip]
VBMETA
Repack to boot image: [/data/local/tmp/boot_a_witn_twrp.327964.img]
HEADER_VER [3]
KERNEL_SZ [42025472]
RAMDISK_SZ [34080758]
OS_VERSION [12.0.0]
OS_PATCH_LEVEL [2022-05]
PAGESIZE [4096]
CMDLINE []
+ TEMPRC=0
+ set +x
OK, the new boot image "/data/local/tmp/boot_a_witn_twrp.327964.img" successfully created
-rw-r--r-- 1 root root 100663296 2022-10-24 18:03 /data/local/tmp/boot_a_witn_twrp.327964.img
Note: Flashing the new image to the boot partition was not requested
+ set +x
----------------------------------------------------------------------
Checking the result ...
The patched boot image is "/data/local/tmp/boot_a_witn_twrp.327964.img"
-rw-r--r-- 1 root root 100663296 2022-10-24 18:03 /data/local/tmp/boot_a_witn_twrp.327964.img
[2022.10.24 20:03:05] Waiting now 5 seconds ...
Patching the partition "/dev/block/by-name/boot_a" from the patched boot image file "/data/local/tmp/boot_a_witn_twrp.327964.img" via dd ...
196608+0 records in
196608+0 records out
100663296 bytes (96 M) copied, 4.865411 s, 20 M/s
Checking the result ....
The check sums for the images and devices on the phone are:

Checksum Size File/Device name
-------------------------------------
728455636 100663296 /data/local/tmp/boot_a.327964.img
2360878166 100663296 /data/local/tmp/boot_a_witn_twrp.327964.img
2360878166 100663296 /dev/block/by-name/boot_a

OK, patching the boot partition "/dev/block/by-name/boot_a" was successfull

*** Press return to reboot the phone now

Rebooting the phone now ...
[xtrnaw7@t15g /data/develop/android/scripts_on_linux]$

Advanced Usage

To install TWRP and Magisk via script these commands can be used (for example after an OS upgrade) :

# install the Magisk App (if not already done ...)
#
cat Magisk-25.1_25100.apk | adb shell pm install -S "$( ls -l Magisk-25.1_25100.apk | awk '{ print $5}' )"

# install TWRP and Magisk into the boot partition
#
REBOOT=no ./install_twrp.sh ; REBOOT=yes ./install_magisk_via_twrp.sh

To install a new OS image including TWRP and Magisk these commands can be used:

# boot the phone into the boot loader
#
adb reboot bootloader

# boot the phone from the TWRP image
#
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img

# copy the OS image to install to the phone
#
adb push /data/develop/android/OmniROM/out/target/product/zenfone8/omni-12-20221015-zenfone8-MICROG.zip /sdcard/Download/

# install the OS image
#
adb shell twrp install /sdcard/Download/omni-12-20221015-zenfone8-MICROG.zip

# reboot into the new OS
#
adb shell twrp reboot

# install the Magisk app
#
cat Magisk-v25.2.apk | adb shell pm install -S "$( ls -l Magisk-v25.2.apk | awk '{ print $5}' )"

# install TWRP and Magisk
#
REBOOT=no ./install_twrp.sh ; REBOOT=yes ./install_magisk_via_twrp.sh

The documentation for the script install_magisk_via_twrp.sh can be found here:

How to install Magisk into the boot partition using a script.

Details

The script install_twrp_from_within_twrp.sh was tested on an ASUS Zenfone 8 with

OmniROM 12 (Android 12) and TWRP 3.7.0.12

OmniROM 12 (Android 12) and TWRP 3.6.1.12

Original Android 12 from ASUS and TWRP 3.7.0.12

OmniROM 13 (Android 13) and TWRP 3.7.0.12 (see note below)

Important Update 04.12.2022 /bs

In TWRP installed in the boot partition of an ASUS Zenfone 8 running early versions of OmniROM 13 the touch screen does not work. You can only use TWRP in an adb session in this configuration.

This bug was fixed in OmniROM 13 from 04.12.2022.

To get around this bug boot the phone from the image file with TWRP, e.g.:

sudo fastboot boot twrp-3.7.0_12-0-I006D.img

or, better, update OmniROM 13 to the image from 04.12.2022 or later.

The script install_twrp_from_within_twrp.sh does not create a backup of the boot partition before rewriting it.

The script install_twrp_from_within_twrp.sh creates temporary files in the directory ${TMPDIR} on the phone; the default value for this variable in the script is

TMPDIR="${TMPDIR:=/cache/install_twrp.$$}"

The default value for the variable in the tested TWRP versions is /data/local/tmp so without changes the temporary files will be created in that directory.

The script does not delete the temporary files so a manual cleanup of the directory might be necessary .

Trouble Shooting

For unknown reasons repacking the boot image fails sometimes and the script ends with an error message like this:

ERROR: The size of the new boot image file is 56713216 but the size of the original image file is 100663296 -- that does not match (RC=33)

To avoid this error increase the wait time using the script parameter "wait=n" (the default wait time is 10 seconds).

If increasing the wait time does not help just restart the wrapper script install_twrp.sh again while the phone is still booted from the TWRP image. So a workaround for this problem is :

install_twrp.sh ; [ $? = 33 ] && install_twrp.sh

The following error message appears when executing the script install_twrp_from_within_twrp.sh after booting the already installed TWRP:

ERROR: Error checking the check sums of the files for the new ramdisk

./system/etc/fstab.postinstall: FAILED

Installing TWRP via script only works if the phone is booted from the TWRP image.

To ignore this error the parameter -f can be used - but this is NOT recommended.

Notes:

see here on How to install Magisk into the boot partition via script

[added 29.10.2022/bs]

see here for How to add additional files to the TWRP image