How To Guide How to install an OS image using the TWRP binary twrp - ASUS ZenFone 8

The most important step to automate the installation and configuration of the Android OS is the installation of an OS image via script without user intervention. One method to do that is using the binary twrp from the recovery TWRP .
The twrp command to install an OS image with twrp is
Code:
/system/bin/twrp install /path/to/update.zip
The usage for the binary twrp is (see also here)
Spoiler: twrp usage help
Code:
I006D:/ # /system/bin/twrp --help
TWRP openrecoveryscript command line tool, TWRP version 3.7.0_12-0
Allows command line usage of TWRP via openrecoveryscript commands.
Some common commands include:
install /path/to/update.zip
backup <SDCRBAEM> [backupname]
restore <SDCRBAEM> [backupname]
wipe <partition name>
format data
sideload
set <variable> [value]
decrypt <password> [USER ID]
remountrw
fixperms
mount <path>
unmount <path>
listmounts
print <value>
mkdir <directory>
reboot [recovery|poweroff|bootloader|download|edl]
See more documentation at https://twrp.me/faq/openrecoveryscript.html
I006D:/ #
The binary twrp can also be used to wipe the data from the phone using the parameter wipe <partition_name>.
The command twrp install will not change the active boot slot for the next reboot so this must be done manually after installing the OS image. This can be done using the binary bootctl which is also part of TWRP.
The usage for the command bootctl is:
Spoiler: bootctl usage help
Code:
ASUS_I006D:/ # /system/bin/bootctl -h
/system/bin/bootctl - command-line wrapper for the boot HAL.
Usage:
/system/bin/bootctl COMMAND
Commands:
hal-info - Show info about boot_control HAL used.
get-number-slots - Prints number of slots.
get-current-slot - Prints currently running SLOT.
mark-boot-successful - Mark current slot as GOOD.
get-active-boot-slot - Prints the SLOT to load on next boot.
set-active-boot-slot SLOT - On next boot, load and execute SLOT.
set-slot-as-unbootable SLOT - Mark SLOT as invalid.
is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.
is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
get-suffix SLOT - Prints suffix for SLOT.
set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
partition is in progress. Valid STAT values
are: none, unknown, snapshotted, merging,
or cancelled.
get-snapshot-merge-status - Prints the current snapshot-merge status.
SLOT parameter is the zero-based slot-number.
64|ASUS_I006D:/ #
Afterwards the phone can be rebooted to activate the new installed OS
So the commands for installing an OS image via twrp are
- boot the phone from the TWRP image
- (optional) use twrp to wipe the data
- copy the OS image file via adb push to the phone
- install the OS image file via the twrp binary
- change the next active boot slot via the binary bootctl.
- reboot the phone
I wrote a simple script to just do that
install_os_via_twrp.sh
The usage of the script is :
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh -h
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Use the parameter --help or -H to view the detailed usage help
Spoiler: Detailed usage help for install_os_via_twrp.sh
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./install_os_via_twrp.sh -H
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Usage
install_os_via_twrp.sh [-h|help|-H] [--reboot|--noreboot] [force] [wipe|wipeall] [wipe_cache] [wipe_data] [wipe_dalvik] [format_data] [format_metadata] [factory_reset] [os_image_file]
All parameter are optional, except the parameter for the OS image to install "os_image_file".
The parameter can be used in any order.
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
If the parameter "--reboot" is used the script will reboot the phone after successfully installing the OS image; to disable the automatic reboot use the parameter "--noreboot".
Default is to ask the user for confirmation to reboot the phone.
Use the parameter "wipe" or "wipeall" to wipe /data, /cache, and Dalvik before installing the OS image.
Use one or more of the parameter "wipe_cache", "wipe_data", or "wipe_dalvik" to only wipe some of the partitions used.
Use the parameter "format_metadata" to format the meta data partition; this wipe is NOT included in "wipe_all"
Use the parameter "format_data" to format the data partition; this wipe is NOT included in "wipe_all"
Use the parameter "factory_reset" to do a factory reset before installing the OS image; a factory reset is done by formatting the data and the metadata partitions.
Use the parameter "force" to ignore errors while wiping or formatting the data; without this parameter the script will abort if one of the wipe or format commands fails
To change some of the values used by the script these environment variables can be set before starting the script:
Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access
Set the environment variable UPLOAD_DIR_ON_THE_PHONE to set the upload directory for the OS image file on the phone (default dir is /tmp; /tmp is mounted on a ramdiks)
See also the source code of the script
boot_phone_from_twrp.sh
for environment variables supported by this script
Prerequisites
- the phone must be connected via USB
- there must be a working connection to the phone using fastboot or adb
- a working TWRP image for the phone must exist
The script boot_phone_from_twrp.sh is required by this script -- see the source code of the script
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Note:
The script needs the helper script boot_phone_from_twrp.sh.sh (see How to boot a phone from a twrp recovery image)
Spoiler: Sample output of the script installing a new version of the same OS
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ REBOOT=yes /data/develop/android/scripts_on_linux/install_os_via_twrp.sh /data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.833s]
Booting OKAY [ 10.359s]
Finished. Total time: 13.243s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready
... the adb daemon is ready after second(s)
Coyping the OS image file "/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip" to "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" on the phone ...
adb: error: failed to read copy response8-MICROG.zip
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 201.8 MB/s (1261116637 bytes in 5.959s)
adb: error: failed to read copy response
adb: device 'M6AIB760D0939LX' not found
Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/omnirom/omni-13/omni-13-202212180505-zenfone8-MICROG.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1261116637 bytes in 37.056s)
Installing the OS image "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" into the slot "_b" ...
Installing zip file '/tmp/omni-13-202212180505-zenfone8-MICROG.zip'
Flashing A/B zip to inactive slot: B
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/omni-13-202212180505-zenfone8-MICROG.zip" successfully installed.
Now changing the next active slot to _b ...
... successfully changed the active slot for the next reboot
Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Spoiler: Sample output of the script installing another OS in the inactive slot
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ /data/develop/android/scripts_on_linux/install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip
install_os_via_twrp.sh version - v1.0.0.0 - install an OS image via the TWRP command twrp
Using the TWRP helper script "/data/develop/android/scripts_on_linux/boot_phone_from_twrp.sh"
The OS image to install is: "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip"
Reading the helper script "boot_phone_from_twrp.sh" ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
Retrieving the current status of the phone ...
The phone is currently booted into the Android OS
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
Booting the phone from the TWRP image "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img" now ...
Sending 'boot.img' (98304 KB) OKAY [ 2.840s]
Booting OKAY [ 10.406s]
Finished. Total time: 13.286s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
Waiting up to 10 seconds for the adb daemon to get ready
... the adb daemon is ready after second(s)
Wiping all data including media ...
Done.
You may need to reboot recovery to be able to use /data again.
TWRP will not recreate /data/media on an FBE device. Please reboot into your rom to create /data/media
Formatting Metadata using mke2fs...
Done.
Done processing script file
Wiping the metadata ...
++ /usr/bin/adb -s M6AIB760D0939LX shell umount /dev/block/by-name/metadata
umount: /dev/block/sda22: Invalid argument
++ /usr/bin/adb -s M6AIB760D0939LX shell mke2fs -F -t ext4 /dev/block/by-name/metadata
mke2fs 1.45.4 (23-Sep-2019)
Discarding device blocks: done
Creating filesystem with 4096 4k blocks and 4096 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
Coyping the OS image file "/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip" to "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" on the phone ...
adb: error: failed to read copy responsey-sake-signed.zip
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 313.7 MB/s (1114300349 bytes in 3.387s)
adb: error: failed to read copy response
adb: device 'M6AIB760D0939LX' not found
Copying the image failed -- we will do another try in 5 seconds ...
/data/backup/ASUS_ZENFONE8/Lineage-19_Android12/lineage-19.1-20221216-nightly-sake-signed.zip: 1 file pushed, 0 skipped. 32.5 MB/s (1114300349 bytes in 32.719s)
Installing the OS image "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" into the slot "_a" ...
Installing zip file '/tmp/lineage-19.1-20221216-nightly-sake-signed.zip'
Flashing A/B zip to inactive slot: A
Step 1/2Step 2/2Updating partition details...
...done
Devices on super may not mount until rebooting recovery.
To flash additional zips, please reboot recovery to switch to the updated slot.
Done processing script file
OS image file "/tmp/lineage-19.1-20221216-nightly-sake-signed.zip" successfully installed.
Now changing the next active slot to _a ...
... successfully changed the active slot for the next reboot
*** Press return to reboot the phone now
Rebooting the phone now ...
Booting the Android OS using the command "adb reboot " now ...
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Testenvironment
I tested the script using this hardware and software:
Hardware:
ASUS Zenfone 8
TWRP
TWRP 3.7.0.12
OS Images installed via script
OmniROM 12
OmniROM 13
LineageOS 19
The script does not work for the images with the original Android from ASUS; the OS installation works but the phone does not boot from the new installation
The script install_os_via_twrp.sh is also available on my home page:
install_os_via_twrp.sh
Trouble Shooting
To update the installed OS to a new version in most cases neither a wipe nor a factory reset is necessary.
To install a different distribution on the phone in most cases a factory reset is required. (e.g installing LIneageOS if the current OS is OmniROM)

Related

How To Guide How to install TWRP via script

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:
Bash:
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
Bash:
adb reboot recovery
or
Bash:
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:
Code:
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:
Code:
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:
Code:
[[email protected] /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"
[[email protected] /data/develop/android/scripts_on_linux]$
Without any parameter the script install_twrp.sh will install TWRP into the active boot partition.
Example:
Code:
[[email protected] /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 ...
[[email protected] /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) :
Bash:
# 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:
Bash:
# 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.:
Bash:
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
Bash:
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:
Code:
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 :
Bash:
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:
Code:
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

How To Guide Some hints using TWRP

This HowTo contains some hints for using TWRP in general and some tips for using CLI commands in TWRP.
I will update this post in case I find something new & useful regarding TWRP and update the history section at the end of this post.
The test environment for the instructions below was
TWRP 3.6.2.12 for the ASUS Zenfone 8
TWRP 3.7.0_12-0 for the ASUS Zenfone 8
Note:
This is NOT a general HowTo for TWRP.
How to test a TWRP image?
It's recommended to boot from the TWRP image file before installing it into the boot or recovery partition of your phone. To do this do
Bash:
# connect the phone via USB to your PC
# boot the phone into the bootloader - e.g. via
#
adb reboot bootloader
# boot the phone from the TWRP image (this will not change anything on the phone)
#
sudo fastboot boot <twrpimage_file>
If everything works you can install the TWRP image into the boot or recovery partition using the approbiate menu entry in TWRP.
How to connect via adb to an TWRP session?
TWRP supports adb session as user root, therefor an
Bash:
adb shell
on your PC is sufficient to open an adb shell as user root on the phone booted into TWRP.
In case you get a permission denied error kill the running adb daemon on the PC and restart the adb command. If that does not fix the error disconnect and reconnect the USB cable between the PC and the Phone.
Be aware that TWRP starts the adb daemon twice after booting. Therefor an adb session to the phone started to early after the boot into TWRP might be killed. In this case just open a new adb shell.
How to check if the phone is booted into TWRP ?
Check the value of the property ro.twrp.boot; this property is only set if TWRP is running
e.g:
Code:
ASUS_I006D:/ # getprop ro.twrp.boot
1
ASUS_I006D:/ #
How to check if the running TWRP was booted from an TWRP image or from a boot or recovery partition?
Check the value of the property ro.bootmode; the value of this property is "unknown" if TWRP was booted from an image file via "fastboot boot <twrp_image_file>", e.g.
Code:
ASUS_I006D:/ # getprop ro.bootmode
unknown
ASUS_I006D:/ #
-> TWRP was booted from an TWRP image file
Code:
ASUS_I006D:/ # getprop ro.bootmode
recovery
ASUS_I006D:/ #
-> TWRP was booted from the boot or recovery partition
How to get the version of the running TWRP?
Check the value of the property getprop ro.twrp.version, e.g.:
Code:
ASUS_I006D:/ # getprop ro.twrp.version
3.7.0_12-0
ASUS_I006D:/ #
Where is the log file from TWRP?
The logfile with the messages written while booting TWRP is /data/recovery/log.gz.
e.g.
Code:
130|ASUS_I006D:/ # ls -ltr /data/recovery/l*gz
-rw------- 1 system system 8022 2022-12-26 14:03 /data/recovery/log.gz
ASUS_I006D:/ #
ASUS_I006D:/ # gzip -cd /data/recovery/log.gz | head -10
Starting TWRP 3.7.0_12-0-d07fdb3c on Sat Jan 10 00:53:15 1970
(pid 403)
I:Lun file '/config/usb_gadget/g1/functions/mass_storage.0/lun.0/file'
PRODUCT_USE_DYNAMIC_PARTITIONS := true
TW_INCLUDE_CRYPTO := true
I:TW_BRIGHTNESS_PATH := /proc/lcd_brightness
I:Found brightness file at '/proc/lcd_brightness'
I:TWFunc::Set_Brightness: Setting brightness control to 420
I:TW_EXCLUDE_ENCRYPTED_BACKUPS := true
I:LANG: en
ASUS_I006D:/ #
The current logfile of the running TWRP is /tmp/recovery.log, e.g.:
Code:
ASUS_I006D:/ # ls -l /tmp/recovery.log
-rw-rw-rw- 1 root root 43303 2023-05-19 20:11 /tmp/recovery.log
ASUS_I006D:/ #
Where are the settings from TWRP stored?
The settings from TWRP are stored in the file
/data/media/0/TWRP/.twrps
This is a binary file.
Note that on phones with active encryption that file is only accessible after /data is decrypted.
How to edit a file while booted into TWRP?
The editor nano is part of TWRP and can be used to edit files in an adb session.
How to mount a partition while inside TWRP?
Use
twrp mount <mountpoint>
to mount a partition while TWRP is running.
Note that you should use this command with caution in adb sessions because the adb session may be killed after the mount
How to decrypt an encrypted /data partition
Use
twrp decrypt <password>
to decrypt an encrypted /data partition, e.g.
Code:
ASUS_I006D:/ # ls /data/media/0
+lnodCAAAAAnYFLObBoWS5ZNb5yVMpze 8KdHMCAAAAQOgtDHq,XdSfFPW+XURA2M KlfTJCAAAAwL93jkH2ugnU4eUG3Zzm7M W9W9SCAAAAgh6lwoP0mNiEK9VIWJoH6L
0nUvhAAAAAQ9ptz+L5qPQrpzcAt4G06K DWk4KDAAAAA,B1xVDmT3dZI87g2C0IS8 M6jcmCAAAAAU,4gn9o0zoBZmkysyXy49 bE7t0CAAAAQ2YuHL9styNaf,iSf2cCO,
4nFjMBAAAAwv2oUczXEpKUDfRIOi+nR5 EcHrdAAAAAglBtbhNWl0NvyX8aW+3CHN O5ZVrDAAAAAlKd7PPpo7CaQmkFbZzKRm haPcnBAAAAw9o2lLF97zpPTaC+byP92s
5yL8gCAAAAgekBDBUi4,W78rJAkgFcMF FA1aZAAAAAg2XNF+M859lpPY,3rUC2cY Vda+LCAAAAwR+g6Tav35+h,Epc8latGR mSKc8BAAAAwm+12sMClHPf+zwtHAeW+Q
8CsF+DAAAAwI3tImfOqIS9aHCQ3S7Cdg JpuFgAAAAAwdEzuQerx4681dzGLjlK3X VvEV4DAAAAgRgoqCfrypNqryZxRACnfr rMJEjAAAAAgwTgx,vA5ewWMVizc2LAos
ASUS_I006D:/ #
ASUS_I006D:/ # twrp decrypt 12345
Attempting to decrypt data partition or user data via command line.
Attempting to decrypt FBE for user 0...
User 0 Decrypted Successfully
Data successfully decrypted
Updating partition details...
...done
ASUS_I006D:/ #
ASUS_I006D:/ # ls /data/media/0
Alarms Android Audiobooks DCIM Documents Download Movies Music Notifications Pictures Podcasts Recordings Ringtones TWRP
ASUS_I006D:/ #
see the TWRP Commandline Guide for the known parameter for this command
Note that existing adb sessions will be stopped after sucessfully executing this command.
How to check if the data partition is encrypted from within a booted TWRP
To check if the data partition is encrypted in a script executed in a running TWRP use code like this:
Spoiler: # check if the data partition is encrypted
Bash:
PASSWORD="1234"
CRYPTO_READY=$( getprop crypto.ready )
TWRP_DECRYPT_DONE=$( getprop twrp.decrypt.done )
if [ "${TWRP_DECRYPT_DONE}"x = "true"x ] ; then
echo "The data partition is not encrypted or already decrypted"
elif [ "${CRYPTO_READY}"x = "1"x ] ; then
echo "The data partition is encrypted"
# sample commands to decrypt the data partition
#
# echo "Decryptiong the data partition now ..."
# twrp decrypt "${PASSWORD}"
elif [ "${CRYPTO_READY}"x = "0"x ] ; then
echo "The data partition is not encrypted"
else
echo "ERROR: The value of the property crypto.ready \"${CRYPTO_READY}\" is not known"
fi
How to install a ZIP file via cli command from within a running TWRP?
Copy the zip file to the phone via adb push and then use
twrp install /path/to/update.zip
to install a ZIP file from within TWRP
Note that the parameter must be a fully qualified filename.
Using this method to update the OS only works in a terminal session on the phone; it does not work in a adb session (at least in my tests until now ...)
What commands are available in TWRP?
Most of the Unix commands are available in TWRP
Be aware that a lot of Unix commands in TWRP are implemented using a general binary called toybox (toybox is something like busybox) and do not support all parameter like the original Linux commands
Code:
ASUS_I006D:/ # ls -l /bin/ | wc -l
261
ASUS_I006D:/ # ls -l /bin/ | grep toybox | wc -l
175
ASUS_I006D:/ #
What other TRWP specific commands can be used while in a running TWRP?
Spoiler: The twrp command
Code:
ASUS_I006D:/ # twrp
TWRP openrecoveryscript command line tool, TWRP version 3.7.0_12-0
Allows command line usage of TWRP via openrecoveryscript commands.
Some common commands include:
install /path/to/update.zip
backup <SDCRBAEM> [backupname]
restore <SDCRBAEM> [backupname]
wipe <partition name>
format data
sideload
set <variable> [value]
decrypt <password> [USER ID]
remountrw
fixperms
mount <path>
unmount <path>
listmounts
print <value>
mkdir <directory>
reboot [recovery|poweroff|bootloader|download|edl]
See more documentation at https://twrp.me/faq/openrecoveryscript.html
ASUS_I006D:/ #
See more documentation at https://twrp.me/faq/openrecoveryscript.html
https://twrp.me/faq/openrecoveryscript.html
and a few others like bootctl (see below) or magiskboot (see below).
How to change the active slot for the next reboot while in TWRP?
Use bootctl, e.g.
Code:
ASUS_I006D:/ # bootctl get-current-slot
1
ASUS_I006D:/ # bootctl set-active-boot-slot 0
The usage for the command bootctl is
Spoiler: bootctl usage
Code:
ASUS_I006D:/ # /bin/bootctl
/bin/bootctl - command-line wrapper for the boot HAL.
Usage:
/bin/bootctl COMMAND
Commands:
hal-info - Show info about boot_control HAL used.
get-number-slots - Prints number of slots.
get-current-slot - Prints currently running SLOT.
mark-boot-successful - Mark current slot as GOOD.
get-active-boot-slot - Prints the SLOT to load on next boot.
set-active-boot-slot SLOT - On next boot, load and execute SLOT.
set-slot-as-unbootable SLOT - Mark SLOT as invalid.
is-slot-bootable SLOT - Returns 0 only if SLOT is bootable.
is-slot-marked-successful SLOT - Returns 0 only if SLOT is marked GOOD.
get-suffix SLOT - Prints suffix for SLOT.
set-snapshot-merge-status STAT - Sets whether a snapshot-merge of any dynamic
partition is in progress. Valid STAT values
are: none, unknown, snapshotted, merging,
or cancelled.
get-snapshot-merge-status - Prints the current snapshot-merge status.
SLOT parameter is the zero-based slot-number.
ASUS_I006D:/ #
see also How to switch the active slot
How to use Magisk binaries while booted into TWRP
Magisk is another great tool for Android phones -- for details about Magisk see here:
https://github.com/topjohnwu/Magisk
The Magisk binary for manipulating the boot partition, magiskboot, is already part of TWRP. This binary can be used to unpack and repack boot partition (or boot partition images), ramdisks, or compress/decompress files. The binary works without problems while booted into TWRP.
Spoiler: magiskboot usage
Code:
1|ASUS_I006D:/ # magiskboot
MagiskBoot - Boot Image Modification Tool
Usage: magiskboot <action> [args...]
Supported actions:
unpack [-n] [-h] <bootimg>
Unpack <bootimg> to, if available, kernel, kernel_dtb, ramdisk.cpio,
second, dtb, extra, and recovery_dtbo into current directory.
If '-n' is provided, it will not attempt to decompress kernel or
ramdisk.cpio from their original formats.
If '-h' is provided, it will dump header info to 'header',
which will be parsed when repacking.
Return values:
0:valid 1:error 2:chromeos
repack [-n] <origbootimg> [outbootimg]
Repack boot image components from current directory
to [outbootimg], or new-boot.img if not specified.
If '-n' is provided, it will not attempt to recompress ramdisk.cpio,
otherwise it will compress ramdisk.cpio and kernel with the same format
as in <origbootimg> if the file provided is not already compressed.
If env variable PATCHVBMETAFLAG is set to true, all disable flags will
be set in the vbmeta header.
hexpatch <file> <hexpattern1> <hexpattern2>
Search <hexpattern1> in <file>, and replace with <hexpattern2>
cpio <incpio> [commands...]
Do cpio commands to <incpio> (modifications are done in-place)
Each command is a single argument, add quotes for each command
Supported commands:
exists ENTRY
Return 0 if ENTRY exists, else return 1
rm [-r] ENTRY
Remove ENTRY, specify [-r] to remove recursively
mkdir MODE ENTRY
Create directory ENTRY in permissions MODE
ln TARGET ENTRY
Create a symlink to TARGET with the name ENTRY
mv SOURCE DEST
Move SOURCE to DEST
add MODE ENTRY INFILE
Add INFILE as ENTRY in permissions MODE; replaces ENTRY if exists
extract [ENTRY OUT]
Extract ENTRY to OUT, or extract all entries to current directory
test
Test the current cpio's status
Return value is 0 or bitwise or-ed of following values:
0x1:Magisk 0x2:unsupported 0x4:Sony
patch
Apply ramdisk patches
Configure with env variables: KEEPVERITY KEEPFORCEENCRYPT
backup ORIG
Create ramdisk backups from ORIG
restore
Restore ramdisk from ramdisk backup stored within incpio
sha1
Print stock boot SHA1 if previously backed up in ramdisk
dtb <input> <action> [args...]
Do dtb related actions to <input>
Supported actions:
print [-f]
Print all contents of dtb for debugging
Specify [-f] to only print fstab nodes
patch
Search for fstab and remove verity/avb
Modifications are done directly to the file in-place
Configure with env variables: KEEPVERITY
split <input>
Split image.*-dtb into kernel + kernel_dtb
sha1 <file>
Print the SHA1 checksum for <file>
cleanup
Cleanup the current working directory
compress[=format] <infile> [outfile]
Compress <infile> with [format] (default: gzip), optionally to [outfile]
<infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg
decompress <infile> [outfile]
Detect format and decompress <infile>, optionally to [outfile]
<infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported formats: gzip zopfli xz lzma bzip2 lz4 lz4_legacy lz4_lg
1|ASUS_I006D:/ #
If Magisk is installed in the Android OS on the phone and the booted TWRP is able to mount the volume for /data the Magisk binary magisk can also be used in an session in TWRP.
For details see the section Using the magisk binary while the phone is booted into TWRP in the post Some hints for using Magisk on Android phones
How to install TWRP into the boot partition using CLI commands from within a booted TWRP?
Use the script
install_twrp_from_within_twrp.sh
Download from here: http://bnsmb.de/files/public/Android/install_twrp_from_within_twrp.sh
How to install TWRP after an OS Upgrade from within the running Android OS?
see How to Install TWRP into the boot partition again after the installation of an OS update
How to add additional files to an existing TWRP image?
see How to add additional files to an TWRP image
How to install TWRP into the boot partition via script?
see How to install TWRP via script
How to build a TWRP image?
seee How to compile TWRP for the ASUS Zenfone 8
How to install an OS image using the twrp command in a script
see How to install an OS image using the TWRP binary twrp?
How to use TWRP if LineageOS 20.x is installed
As of 26.06.2023 booting the phone from the TWRP recovery image using the command
sudo fastboot boot <twrp_image>
Does not work anymore if LineageOS 20.x is installed on the phone.
see How to use TWRP if LineageOS 20.x is installed for a workaroujnd
Spoiler: History / Change log of this entry
02.12.2022 /bs
initial release
17.12.2022 /bs
added infos about how to use the binary twrp to install an OS image
26.12.2022 /bs
added infos about the logile used by TWRP
23.04.2023 /bs
added info about how to decrypt the data partition
30.04.2023 /bs
added infos about the file used for the TWRP settings
19.05.2023 /bs
added infos about how to check if the data partition is encrypted
updated the infos about the log file used by TWRP
added infos about how to install TWRP from within a running Android OS
26.06.2023 /bs
added the infos about how to use TWRP with LineageOS 20.x

How To Guide How to boot a phone from a TWRP recovery image via script

For installing and configuring an Android phone via script it is often required to boot the phone from an TWRP recovery image
Because of this I wrote a Linux shell script to reboot an Android phone from an TWRP recovery image:
boot_phone_from_twrp.sh
The script boot_phone_from_trwp.sh will detect the current status of the attached phone and use the approbiate commands (fastboot and/or adb) to reboot the phone from the TWRP recovery image.
The usage for the script is:
Code:
boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image
boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Use the parameter -H to get a detailed usage help:
Spoiler: Detailed Usage
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./boot_phone_from_twrp.sh -H
boot_phone_from_twrp.sh v2.2.0.0 - boot a phone from a TWRP image
Usage
boot_phone_from_twrp.sh [-h|help|-H] [serial=#sn|s=#sn] [wait=n] [force|noforce] [reboot|noreboot] [checkonly] [status] [twrp_image]
All parameter are optional
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
Use the parameter "serial=#sn" to define the serialnumber of the phone to process. This parameter is only necessary if there
are more then one phones connected via USB and the environment variable SERIAL_NUMBER is not set.
The value for the parameter "wait" is the maximum number of seconds to wait after booting from the TWRP image until
the adb deamon is ready to use. The default value is 10 seconds.
Use the parameter "force" to reboot the phone from the TWRP image even if it's booted from a TWRP installed in
the boot or recovery partition.
Use the parameter "noforce" to disable rebooting the phone from the TWRP image if it's booted from a TWRP installed in
the boot or recovery partition. This is the default setting.
Use the parameter "reboot" to reboot the phone from the TWRP image even if it's already booted from an TWRP image.
Use the parameter "noreboot" to disable rebooting the phone from the TWRP image if it's already booted from an TWRP image
This is the default setting.
Use the parameter "checkonly" to only retrieve the current boot mode of the phone; the script will also return
the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.
Use the parameter "status" to only print the current boot mode of the phone; the script will return
the current status via return code; the possible values are 1 to 6 or one of the return codes indicating an error.
The parameter "twrp_image" can be used to define another TWRP image to use. The parameter is optional - the
default for "twrp_image" is the value of the environment variable TWRP_IMAGE; the default for the variable
TWRP_IMAGE is hardcoded in the script in the variable DEFAULT_TWRP_IMAGE (= the TWRP image for the ASUS Zenfone 8)
Parameter overwrite the values defined via environment variables.
Returncodes:
0 - the phone was successfully booted from the TWRP image
1 - the phone is already booted from the TWRP image
2 - the phone is booted from TWRP installed in the boot or recovery partition
3 - the phone is booted into the Android OS
4 - the phone is booted into bootloader
5 - the phone is booted into the fastbootd
6 - the phone is booted into the safe mode of the Android OS
7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition
9 - too many phones connected
10 - usage help printed and exited
100 - invalid parameter found
101 - TWRP image not found
102 - booting the phone into the bootloader failed
103 - booting the phone into the TWRP failed
252 - access to the phone failed
253 - requirement check failed (e.g. one or more required executables not found, etc)
254 - unknown error
The phone to boot must be attached via USB.
The phone can be either in fastboot mode, in normal mode with enabled adb support, in the boot loader, or already booted from an installed TWRP or an TWRP image
To change some of the values used by the script you can set environment variables before starting the script:
Set the environment variable TWRP_IMAGE to the name of the TWRP image file that should be used (the parameter of the script will overwrite the variable)
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 FAST_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the bootloader (default are 60 seconds)
Set the environment variable ADB_BOOT_WAIT_TIME to the maximum number of seconds to wait after booting the phone from the TWRP image (default are 60 seconds)
Set the environment variable ADB to the adb executable that should be used; default: search adb in the PATH
Set the environment variable FASTBOOT to the fastboot executable that should be used; default: search fastboot in the PATH
Set the environment variable TIMEOUT to the timeout executable that should be used; default: search timeout in the PATH
The default TWRP image to use is "/data/backup/ASUS_ZENFONE8/twrp/current_twrp.img"
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
When more than one phone is connected via USB use the parameter serial=#sn or the environment variable SERIAL_NUMBER to define the serial number of the phone to use.
To only retrieve the current status of the attached phone execute the script with the parameter checkonly or status.
The script will then only check the status of the phone and return the current status in the return code:
1 - the phone is booted from the TWRP image
2 - the phone is booted from TWRP installed in the boot or recovery partition
3 - the phone is booted into the Android OS
4 - the phone is booted into bootloader
5 - the phone is booted into the fastbootd
6 - the phone is booted into the safe mode of the Android OS
7 - the phone is booted into the LineageOS recovery installed in the boot or recovery partition
If the parameter status is used the script will also print a message with the current status of the phone,e.g:
Spoiler: example output
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh checkonly ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
RC is 6
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$ ./boot_phone_from_twrp.sh status ; echo "RC is $?"
boot_phone_from_twrp.sh v2.1.0.0 - boot a phone from a TWRP image
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
The phone is currently booted into the safe-mode of the Android OS
RC is 6
[[email protected] /data/develop/android/scripts_on_linux]$
The script boot_phone_from_twrp.sh can also be used as include file using
. ./boot_phone_from_twrp.sh
Using this method these functions are available in the script that includes boot_phone_from_twrp.sh:
retrieve_phone_status
print_phone_status
boot_phone_from_the_TWRP_image
reboot_phone
wait_for_phone_to_be_in_the_bootloader
wait_for_phone_with_a_working_adb_connection
wait_for_the_adb_daemon
Please check the comments at the start of the script for details about using the script in this way; the command
Code:
grep "#S#" boot_phone_from_twrp.sh | cut -c4-
prins the hints for using the script as include file
Spoiler: Details for using the script as include
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ grep "#S#"
boot_phone_from_twrp.sh | cut -c4-
---------------------------------------------------------------------
To use the script as include file add the statement
. boot_phone_from_twrp.sh
to your file
If used as include file the script will
- define these constants if not already defined:
__TRUE
__FALSE
- define these functions if not already defined:
LogMsg
LogError
isNumber
wait_some_seconds
- define these functions (overwriting existing functions with the same name)
check_prereqs_for_boot_phone_from_twrp
init_global_vars_for_boot_phone_from_twrp
set_serial_number
retrieve_phone_status
print_phone_status
boot_phone_from_the_TWRP_image
reboot_phone
wait_for_phone_to_be_in_the_bootloader
wait_for_phone_with_a_working_adb_connection
wait_for_the_adb_daemon
- the variable PHONE_BOOT_ERROR contains the error code (102 or 103) if rebooting the phone fails
The script including this script can optional define these variables before
including this script (see the source code below for the meaning of these variables):
SUDO_PREFIX
CUR_USER
FORCE_BOOT_INTO_TWRP_IMAGE
FORCE_REBOOT_INTO_TWRP_IMAGE
TWRP_IMAGE
SERIAL_NUMBER
ADB_DAEMON_WAIT_TIME
FAST_BOOT_WAIT_TIME
ADB_BOOT_WAIT_TIME
FASTBOOT_OPTIONS
ADB_OPTIONS
ADB
FASTBOOT
TIMEOUT
CHECKONLY
The global variables listed above will be defined after including this script
[[email protected] /data/develop/android/scripts_on_linux]$
Note:
After including the script you should use the command
${ADB} ${ADB_OPTIONS}
to run the adb binary and the command
${FASTBOOT} ${FASTBOOT_OPTIONS}
to run the fastboot binary.
Examples:
To boot the phone from the TWRP image use this code:
Code:
. ./boot_phone_from_twrp.sh
retrieve_phone_status
boot_phone_from_the_TWRP_image
To boot the phone into the Android OS use this code:
Code:
. ./boot_phone_from_twrp.sh
retrieve_phone_status
reboot_phone
Spoiler: Simple test script
Code:
[[email protected] /data/develop/android/scripts_on_linux]$ cat ./test.sh
echo "*** Loading the functions to boot the phone ..."
. ./boot_phone_from_twrp.sh
echo ""
echo "*** Checking the status of the phone"
retrieve_phone_status
print_phone_status
echo ""
echo "*** Booting the phone into the TWRP image ..!"
boot_phone_from_the_TWRP_image
echo ""
echo "*** Press return to boot the phone into the Android OS ..."
read USER_INPUT
reboot_phone
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$
[[email protected] /data/develop/android/scripts_on_linux]$ ./test.sh
*** Loading the functions to boot the phone ...
The script is running as user "xtrnaw7" -- will use "sudo" for the fastboot commands ...
Will use the attached phone with the serial number "M6AIB760D0939LX"
Using the default TWRP image: "/data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D.img"
Using the options " -s M6AIB760D0939LX " for the adb commands
Using the options " -s M6AIB760D0939LX " for the fastboot commands
Checking the script prerequisites ...
Will wait up to 60 second(s) after booting the phone into the bootloader
Will wait up to 60 second(s) after booting the phone from the TWRP image
Will wait up to 10 second(s) until the adb daemon is ready to use
*** Checking the status of the phone
The phone is currently booted from the Android OS
*** Booting the phone into the TWRP image ..!
The phone is booted into the Android OS
Booting the phone into the bootloader now ...
Waiting up to 60 seconds for the boot into the fastboot mode ...
The phone is booted into the bootloader
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.761s]
Booting OKAY [ 10.361s]
Finished. Total time: 13.167s
Waiting up to 60 seconds for the boot of the phone ....
Waiting up to 10 seconds for the adb daemon to get ready ..
... the adb daemon is ready after second(s)
The phone is booted into TWRP: "twrp_I006D" version "3.7.0_12-0"
*** Press return to boot the phone into the Android OS ...
Booting the Android OS using the command "adb reboot " now ...
Waiting up to 60 seconds for the boot of the phone .....
Waiting up to 10 seconds for the adb daemon to get ready ..........
[[email protected] /data/develop/android/scripts_on_linux]$
The script boot_phone_from_twrp.sh is used as include script in the script init_magisk_db.sh - see this post How to enable root access using Magisk in a script. The script is also used in the script install_os_via_twrp.sh see this post How to install an OS image using the twrp command in a script .
The script boot_phone_from_twrp.sh is also available on my homepage:
http://bnsmb.de/files/public/Android/boot_phone_from_twrp.sh
Spoiler: Changelog for the script boot_phone_from_twrp.sh
Bash:
# History
# 05.11.2022 v1.0.0.0 /bs #VERSION
# inital release
#
# 01.12.2022 v2.0.0.0 /bs #VERSION
# script rewritten using functions
# added the parameter --serial=#sn
#
# 07.12.2022 v2.1.0.0 /bs #VERSION
# added a check for the Android OS safe mode
# missing shift command in the code to process the parameter for the serial number -- fixed
#
# 17.12.2022 v2.2.0.0 /bs #VERSION
# added support for the LineageOS recovery
Spoiler: History
17.12.2022 /bs
Updated the post with the infos for the script version 2.2.0.0
18.12.2022 /bs
Added some details for using the script as include file
Added a link to another script using this script

How To Guide How to enable access via adb on a new installed OS

For automatic installation and configuration of a phone running the Android OS the access via adb is required. "Unfortunately" the access via adb is disabled in most Android distributions (in reality it is of course good that the access via adb is disabled in the default setting! See also the notes regarding the LineageOS at the end of this post))
There are (at least) two ways to enable access via adb:
Manually via the GUI of Android on the phone or via the Android command settings executed by the user root in a shell ,e.g.:
Bash:
settings put global development_settings_enabled 1
settings put global adb_enabled 1
The latter can also be done in a script in an adb shell -- but of course we need a working adb connection ....
One way around this problem is to use Magisk:
In principle this could be done using an init script for Magisk:
Magisk allows to run scripts while booting the phone (see How to run a script at every boot using Magisk); this feature could be used to enable adb via script.
Unfortunately, this method no longer works in the latest Android versions (Androd 12 and Android 13; Android 11 not tested):
Android now uses encryption for most of the files in the subdirectories in /data and to initiate the encryption keys for the files in these directories, the phone must boot once from the installed OS.
To use a Magisk init script to enable the access via adb, we need to create files in the directories /data/adb and /data/misc/adb. If you create these files while booting into TWRP recovery without a configured Android operating system, these files will be created unencrypted, making it impossible to boot the installed Android operating system:
If the Android operating system finds unencrpyted files in these directories at the first reboot, it boots into the installed recovery with the famous error message "Cannot load Android system. Your data may be corrupt" (see How to fix the error "Cannot load Android system. Your data may be corrupt" for details)
Fortunately, the Android operating system does not encrypt files in the directory /data/recovery (and therefore continues booting if there are already files in that directory) and the part of Magisk in the boot partition does not need the Magisk files /data/adb.
So to enable the access via adb using Magisk, we can create a script in the directory /data/recovery and a new Init .rc file for the Android operating system to execute that script while the Android OS is booting - for details about how to do that see How to trigger an action when a property is changed.
The working method to enable adb via Magisk is as follows:
- Boot the phone from a recovery (like TWRP)
- Install the new OS image but do not reboot the phone (see How to install an OS image using the TWRP binary twrp for how to install the OS via the cli commands from TWRP)
- Install Magisk into the boot partition of the phone and delete all files in /data/adb afterwards (see How to install Magisk into the boot partition using a script for how to install Magisk via script)
- copy a script to the directory /data/recovery on the phone that will enable access via adb using the Android command settings and copy the public ssl keys for the access via adb to the directory /data/misc/adb on the phone
- install a new init .rc file via Magisk which executes the script in the direcory /data/recovery to enable the access via adb - this script will be executed while doing the 1st reboot of the new installed Android OS.
(see How to trigger an action when a property is changed for how to add a new init .rc file using Magisk)
- reboot the phone
I wrote two scripts to enable adb using this method:
enable_adb_using_magisk.sh
The script enable_adb_using_magisk.sh must run in an adb session in TWRP after the new OS was installed and after Magisk has been installed into the boot partition but before the first reboot of the new installed operating system.
The script enable_adb_using_magisk.sh
- creates the script /data/recover/enable_adb_via_service.sh on the phone to enable adb and install the public ssl key for access via adb
- creates a new init *rc file using Magisk that will run the script /data/recover/enable_adb_via_service.sh on the phone while doing the 1st reboot (after the file encryption for the files /data is initalized by the Android OS)
To use the script enable_adb_using_magisk.sh manually after installing the new OS and Magisk do while the phone is booted into TWRP:
Bash:
#
# copy the script enable_adb_using_magisk.sh to the directory /tmp on the phone
#
adb push enable_adb_using_magisk.sh /tmp/enable_adb_using_magisk.sh
# copy the public ssl key for access via adb to the directory /tmp on the phone
#
adb push $HOME/.android/adbkey.pub /tmp/adbkey.pub
# execute the script /tmp/enable_adb_using_magisk.sh on the phone
#
adb shell sh /tmp/enable_adb_using_magisk.sh
# reboot the phone into the new installed OS
#
adb reboot
Notes:
The script enable_adb_using_magisk.sh installs the init *rc file into the boot partition from the slot for the next reboot: In this scenario this is the slot with the new installed OS.
To specify the boot partition to use add the boot partition as parameter, e.g.
Bash:
adb shell sh /tmp/enable_adb_using_magisk.sh _a
The init *rc service used is:
Spoiler: init *rc service to enable adb
Code:
#
# additional RC service to enable the access via adb after the 1st boot into the new installed Android OS
#
service bnsmb_enable_adb /system/bin/sh /data/recovery/enable_adb_via_service.sh
user root
group root
seclabel u:r:magisk:s0
disabled
oneshot
on zygote-start
setprop sys.bnsmb_enable_adb_done 0
start bnsmb_enable_adb
#
# Note: the following entries are for testing only!
#
on zygote-start
write /data/recovery/semfile Here_I_am
setprop sys.bnsmb.test.okay 0
The script enable_adb_using_magisk.sh creates the files adb_keys and adb_temp_keys.xml in the directory /data/misc/adb if the public ssl key /tmp/adbkey.pub exists on the phone.
Note that the file /data/misc/adb/db_temp_keys.xml is an Android Binary XML fIle (see How to change files in Android XML Binary format for details about Android Binary XML files)
To avoid the manual steps listed above I wrote another script:
enable_access_via_adb.sh
The script enable_access_via_adb.sh does all the steps listed above; the usage for enable_access_via_adb.sh is
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -h
enable_access_via_adb.sh version - v1.0.0.0 - shell script to enable adb access via an init .rc file configured via Magisk
enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
Spoiler: Detailed Usage
Code:
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $ ./enable_access_via_adb.sh -H
enable_access_via_adb.sh version - v1.0.0.0 - shell script to enable adb access via an init .rc file configured via Magisk
Usage
enable_access_via_adb.sh [-h|help|-H] [--reboot|--noreboot] [--nopubkey|--pubkey]
All parameter are optional. The parameter can be used in any order.
Use the parameter "help" or "-H" to print the detailed usage help; use the parameter "-h" to print only the short usage help
If the parameter "--reboot" is used the script will reboot the phone after installing Init .rc file; to disable the automatic reboot use the parameter "--noreboot".
Default is to ask the user for confirmation to reboot the phone.
Use the parameter "--nopubkey" to disable configuring the public key of the current user on the PC for the access via adb; use the parameter "--pubkey" to configure
the public key; default is to configure the public key.
The default public ssl key used is the key in the file "${HOME}/.android/adbkey.pub".
To change some of the values used by the script these environment variables can be set before starting the script:
Set the environment variable PUBLIC_KEY_ON_PC to the file with the public ssl key to use for the access via adb if another public key should be used
Set the environment variable REBOOT to "yes" before starting the script to automatically reboot the phone after enabling the root access
Set the environment variable REBOOT to "no" before starting the script to disable the automatic reboot after enabling the root access
See also the source code of the script
boot_phone_from_twrp.sh
for environment variables supported by this script
Prerequisites
- the phone must be connected via USB
- Magisk must be already installed in the boot partition of the phone
- the phone must be either booted into the fastbootd or bootloader with a working fastboot connection
or already booted into a recovery image with working adb connnection
The scripts boot_phone_from_twrp.sh and enable_adb_using_magisk.sh are required by this script -- see the source code of the script
[ OmniRom 13 Dev - [email protected] /data/develop/android/scripts_on_linux ] $
So installing a new OS and enabling access via adb in a script without user intervention can be done using these commands:
Bash:
#
# do a factory reset and install the OS image using the TRWP binary twrp
#
REBOOT=no ./install_os_via_twrp.sh factory_reset /data/backup/ASUS_ZENFONE8/omnirom_local/omni-13/omni-13-20221211-zenfone8-MICROG.zip
# install Magisk into the boot sector of the partition with the new OS and delete all files in /data/adb afterwards
#
# The parameter delete_adb_dir for the script "install_magisk_via_twrp.sh" is only available in version 2.2.0.0 or newer of the script.
#
REBOOT=no ./install_magisk_via_twrp.sh next copy_apk delete_adb_dir
# enable adb and copy the public keys for adb via new init *rc file using Magisk in the boot partition
#
REBOOT=no ./enable_access_via_adb.sh
# reboot the phone
#
adb reboot
# wait until the new OS is booted and adb is enabled
#
adb wait-for-device
# install the Magisk App
#
./install_apk.sh /data/backup/Android/EssentialApps/Magisk-v25.2.apk
# install the Magisk directories and binaries in /data/adb
#
REBOOT=no ./install_magisk_via_twrp.sh adb_only
# (optional) enable root access for the adb shell
#
REBOOT=yes ./init_magisk_db.sh
Notes:
The script enable_access_via_adb.sh needs the helper script boot_phone_from_twrp.sh.sh and the script enable_adb_using_magisk.sh .
The scripts can be downloaded from my homepage:
enable_access_via_adb.sh
enable_adb_using_magisk.sh
boot_phone_from_twrp.sh
The other scripts mentioned in ths post can also be downloaded from my home page:
install_os_via_twrp.sh (see How to install an OS image using the TWRP binary twrp for a descripton of this script)
install_magisk_via_twrp.sh (see How to install Magisk into the boot partition using a script for a description for this script)
install_apk.sh (see How to install packages (apk files) for Android via script for a description for this script)
init_magisk_db.sh (see How to enable root access using Magisk in a script for a description for this script)
Disabling the service to enable access via adb
To only run once, the script enable_adb_using_magisk.sh creates the file /data/recovery/adb_initialized exists on the phone. If that file already exists when enable_adb_using_magisk.sh starts, the script will do nothing and end immediately.
Therefor, it's not necessary to delete the new init *rc service.
It might even be useful to leave the service installed, since it can be used for other purposes later by modifying the script
However, if necessary, you can either delete the script used by the init *rc service enable_adb_using_magisk.sh or reinstall the original boot partition to get rid of the new service completely:
Bash:
#
# while the phone is booted into the Android OS with enabled root access or while the phone is booted into TWRP
#
# use the parameter "yes" for the script to run without requesting user input
#
adb shell su - -c /data/recovery/work/restore_boot_partition.sh
Directories and files used
Name
Type
Content
Comment
/tmp/adbkey.pubfilessl public key that should be configured/tmp is on a ramdisk so this file is lost after booting the phone/data/recovery/workDirectorytemporary files used for enabling adb/data/recovery/work/enable_adb_via_service.rcfileinit *rc file added to the boot partition via Magiks/data/recovery/work/boot_a.imgfileimage of the original boot partition/data/recovery/work/original_boot_partition.imgSymLinklink to the image with the original boot partition/data/recovery/work/restore_boot_partition.shfileScript to restore the boot partition using the image of the original boot partition/data/recovery/work/header
/data/recovery/work/kernel
/data/recovery/work/ramdisk.cpiofilescontents of the boot partition/data/recovery/adbkey.pubfilepublic ssl key to be configured for access via adb/data/recovery/adb_temp_keys.xml.humanfileXML file with the configuration for the access via adb
(will be converted to an XML file in Android binary XML format in /data/misc/adb)/data/recovery/enable_adb_via_service.shfilescript to enable the access via adb and installl the public ssl key for the access via adb
The script is configured in the init*rc file /data/recovery/work/enable_adb_via_service.rc/data/recovery/enable_adb_via_service.logfilelog file created by the script /data/recovery/enable_adb_via_service.sh/data/recovery/adb_initializedfilesemphor file for the script /data/recovery/enable_adb_via_service.sh - if this file exists the script will do nothing/data/recovery/semfilefiletest file created by the new init *rc service/data/recovery/log.gz
/data/recovery/last_log.gz
/data/recovery/recovery.fstab
/data/recovery/storage.fstabfileFiles created by TWRPonly for information
Enabling access via adb in the LineageOS (Update 11.01.2023)
In the LineageOS 19 for the ASUS Zenfone 8 access via adb is enabled in the default configuration.
Therefor these instructions are not necessary. The only configuration that must be done on a phone running the LineageOS is to copy the public ssl keys to /data/misc/adb to enable the access via adb.
Unfortunately the method used for this described in this post seems not to work in the LineageOS.
In addition, root access via adb can be enabled in the developer settings in the LineageOS. Until now I did not find out how to enable the plain root access from the LineageOS from within a script.
userdebug builds
In userdebug builds of Android the access via adb (without authentification) is enabled in the default.
Trouble Shooting
The script enable_adb_using_magisk.sh uses the log file /data/recovery/enable_adb_via_service.log if executed via init *rc service.
To check if the init *rc file was configured successfully check the properties
sys.bnsmb_enable_adb_done
sys.bnsmb.test.okay
in the running Android OS. Both properties should be defined with the value 0:
Code:
ASUS_I006D:/ # getprop sys.bnsmb_enable_adb_done
0
ASUS_I006D:/ #
ASUS_I006D:/ # getprop sys.bnsmb.test.okay
0
ASUS_I006D:/ #
In addition, if everything worked there should exist the file /data/recovery/semfile.
Code:
130|ASUS_I006D:/ # ls -l /data/recovery/semfile
-rw------- 1 root root 9 2022-12-29 11:44 /data/recovery/semfile
ASUS_I006D:/ #
Use the Android command start to check if the service exists and can be started, e.g.
Spoiler: start bnsmb_enable_adb
Code:
ASUS_I006D:/ # start bnsmb_enable_adb ; dmesg | tail -10
[ 3069.861365] [ 3069.861365] (CPU:2-pid:1:init) [12:35:31.711593934] init: Service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) exited with status 0 waiting took 0.024000 seconds
[ 3069.861402] [ 3069.861402] (CPU:2-pid:1:init) [12:35:31.711630236] init: Sending signal 9 to service 'exec 613 (/system/bin/flags_health_check UPDATABLE_CRASHING)' (pid 5734) process group...
[ 3069.861618] [ 3069.861618] (CPU:2-pid:1:init) [12:35:31.711846069] libprocessgroup: Successfully killed process cgroup uid 1000 pid 5734 in 0ms
[ 3071.202534] [ 3071.202486] (CPU:2-pid:5077:wk:0xffffffe40) [12:35:33.052716433] [BAT][CHG]asus_jeita_cc_worker set BATTMAN_OEM_WORK_EVENT : WORK_JEITA_CC
[ 3071.220807] [ 3071.220807] (CPU:0-pid:350:wk:0xffffffe40) [12:35:33.071038100] [BAT][CHG]handle_message set property:16 successfully
[ 3071.570753] [ 3071.570753] (CPU:2-pid:1:init) [12:35:33.420982631] init: starting service 'bnsmb_enable_adb'...
[ 3071.575971] [ 3071.575971] (CPU:3-pid:1:init) [12:35:33.426199819] init: Control message: Processed ctl.start for 'bnsmb_enable_adb' from pid: 5735 (start bnsmb_enable_adb)
[ 3071.614570] [ 3071.614570] (CPU:3-pid:1:init) [12:35:33.464799298] init: Service 'bnsmb_enable_adb' (pid 5736) exited with status 0 oneshot service took 0.040000 seconds in background
[ 3071.614609] [ 3071.614609] (CPU:3-pid:1:init) [12:35:33.464837267] init: Sending signal 9 to service 'bnsmb_enable_adb' (pid 5736) process group...
[ 3071.614788] [ 3071.614788] (CPU:3-pid:1:init) [12:35:33.465016277] libprocessgroup: Successfully killed process cgroup uid 0 pid 5736 in 0ms
ASUS_I006D:/ #
If something does not work execute the script manually in an adb session to check :
Spoiler: /data/recovery/enable_adb_via_service.sh
Code:
ASUS_I006D:/ # rm -f /data/recovery/adb_initialized
ASUS_I006D:/ #
ASUS_I006D:/ #
ASUS_I006D:/ # sh -x /data/recovery/enable_adb_via_service.sh
+ tty -s
+ '[' 0 -ne 0 ']'
+ PATH=/system/bin:/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin
+ export PATH
+ '[' ! -r /data/recovery/adb_initialized ']'
+ echo 'Sleeping 30 seconds now ...'
Sleeping 30 seconds now ...
+ sleep 30
+ touch /data/recovery/adb_initialized
+ settings put global development_settings_enabled 1
+ settings put global adb_enabled 1
+ '[' ! -d /data/misc/adb ']'
+ '[' ! -r /data/misc/adb/adb_keys ']'
+ '[' -r /data/recovery/adbkey.pub ']'
+ >>/data/misc/adb/adb_keys
+ cat /data/recovery/adbkey.pub
+ '[' -r /data/recovery/adb_temp_keys.xml.human ']'
+ xml2abx /data/recovery/adb_temp_keys.xml.human /data/misc/adb/adb_temp_keys.xml
+ chmod 0600 /data/misc/adb/adb_temp_keys.xml
+ chown system:shell /data/misc/adb/adb_temp_keys.xml
+ chcon -v u:object_r:adb_keys_file:s0 /data/misc/adb/adb_temp_keys.xml
chcon '/data/misc/adb/adb_temp_keys.xml' to u:object_r:adb_keys_file:s0
ASUS_I006D:/ #
Thank you for your explanation, I leave my footprint, I need this sometimes.

How To Guide How to Install TWRP into the boot partition again after the installation of an OS update

After installing an operating system update on a phone with the Android operating system and A/B slots, Magisk can be installed in the boot partition of the inactive slot with the new operating system version using the "Install to inactive slot" installation option in the Magisk App.
Unfortunately this option is not available for TWRP.
Therefor I wrote a litlle script to install TWRP into the boot partition of the inactive slot with the new operating system version after an OS upgrade.
The method works on my ASUS Zenfone 8 and should in principle also work on other phones with the same architecture (Qualcomm CPU, A/B slots, and boot partitions with ram disks)
Looking at the source code for TWRP (https://github.com/TeamWin/android_bootable_recovery/blob/android-12.1/twrpRepacker.cpp) it can be seen that the installation of TWRP is in principle only the installation of a new ramdisk:
Spoiler: TWRP source code excerpt
C++:
bool twrpRepacker::Flash_Current_Twrp() {
if (!TWFunc::Path_Exists("/ramdisk-files.txt")) {
LOGERR("can not find ramdisk-files.txt");
return false;
}
PartitionManager.Unlock_Block_Partitions();
Repack_Options_struct Repack_Options;
Repack_Options.Disable_Verity = false;
Repack_Options.Disable_Force_Encrypt = false;
Repack_Options.Type = REPLACE_RAMDISK_UNPACKED;
Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
std::string verifyfiles = "cd / && sha256sum --status -c ramdisk-files.sha256sum";
if (TWFunc::Exec_Cmd(verifyfiles) != 0) {
gui_msg(Msg(msg::kError, "modified_ramdisk_error=ramdisk files have been modified, unable to create ramdisk to flash, fastboot boot twrp and try this option again or use the Install Recovery Ramdisk option."));
return false;
}
std::string command = "cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/currentramdisk.cpio && /system/bin/gzip -f /tmp/currentramdisk.cpio";
if (TWFunc::Exec_Cmd(command) != 0) {
gui_msg(Msg(msg::kError, "create_ramdisk_error=failed to create ramdisk to flash."));
return false;
}
if (!Repack_Image_And_Flash("/tmp/currentramdisk.cpio.gz", Repack_Options))
return false;
else
return true;
}
Therefor to install TWRP via script after an OS version update two steps are neccessary:
Step one
First we must create the ramdisk with TWRP; this step must only be done once for each TWRP version.
Boot the phone from the TWRP image (NOT from a TWRP installed in the recovery!), e.g:
Bash:
adb rebboot bootloader
sudo fastboot boot /data/backup/ASUS_ZENFONE8/twrp/twrp-3.7.0_12-0-I006D-enhanced.img
Then open an adb session to the running TWRP on the phone and execute:
Bash:
# create the RAM disk
#
cd / && sha256sum --status -c ramdisk-files.sha256sum
if [ $? -eq 0 ] ; then
cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/ramdisk_with_twrp.cpio && /system/bin/gzip -f /tmp/ramdisk_with_twrp.cpio
fi
# copy the RAM disk and magiskboot to a directory on the internal storage that supports the execution permission bit (not /sdcard/<something>)
#
mkdir -p /data/develop/twrp
cp /tmp/ramdisk_with_twrp.cpio.gz /data/develop/twrp/ramdisk_with_twrp.cpio
cp /bin/magiskboot /data/develop/twrp/
Notes:
It is recommended to store the files ramdisk_with_twrp.cpio and magiskboot also on the PC.
The executable magiskboot is only required if Magisk is not installed on the phone.
Now copy the script install_twrp_ramdisk.sh.sh to the phone; this script can be copied to any filesystem with write access on the phone.
That's it .
Step two
The next time an OS update should be done using these steps:
1. install the new OS version either manually or via OTA but do NOT reboot afterwards.
2. execute the script install_twrp_ramdisk.sh.sh to install TWRP into the boot partition for the inactive slot either in an shell on the phone or in an adb shell, e.g. (sh is only necessary if the script is on a filesystem that does not support the execution permission)
Bash:
sh /sdcard/Download/install_twrp_ramdisk.sh
Note that the script needs root access to read and write the boot partition
3. optional: Install Magisk into the new boot partition using the install option "Install to inactive slot" in the Magisk App
4. reboot the phone to activate the new OS version
Test Environment
ASUS Zenfone 8
OmniROM 13
Magisk v25.2
TWRP 3.7.0
Notes
Set the environment variable WORK_DIR before executing the script to use another directory with the image
Set the environment variable TWRP_RAMDISK_NAME before executing the script to use another filename for the ram disk image.
see How to add additional files to an TWRP image for how to add additional files to the TWRP image
see How to install TWRP via script for how to install TWRP with a script running on the PC
Usage for the script install_twrp_ramdisk.sh:
Spoiler: ./install_twrp_ramdisk.sh -H
Code:
./install_twrp_ramdisk.sh -H
Usage:
install_twrp_ramdisk.sh [-h|-H]
This script must run in a shell on the phone (or in an adb shell) after an OS update was installed (either manual or via OTA) but before rebooting the phone.
The script will then install TWRP into the inactive slot.
The script must be executed by the user "root"; if it's executed by a non-root user it will restart itself using "su - -c scriptname"
Supported environment variables
WORK_DIR
directory with the ram disk image and probably the executable magiskboot
TWRP_RAMDISK_NAME
name of the file with the ram disk image (without path)
Prerequisites
The script requires these files:
/data/develop/twrp/ramdisk_with_twrp.cpio
- the ram disk with TWRP
/data/develop/twrp/magiskboot or /data/adb/magisk/magiskboot
- the magiskboot executable
To create the ram disk with TWRP do:
Boot the phone from the TWRP image (NOT from a TWRP installed in the recovery!),
open an adb session to the running TWRP on the phone and execute:
cd / && sha256sum --status -c ramdisk-files.sha256sum
if [ $? -eq 0 ] ; then
cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/ramdisk_with_twrp.cpio && /system/bin/gzip -f /tmp/ramdisk_with_twrp.cpio
fi
mkdir -p /data/develop/twrp
cp /tmp/ramdisk_with_twrp.cpio.gz /data/develop/twrp/ramdisk_with_twrp.cpio
cp /bin/magiskboot /data/develop/twrp/
Note:
It is recommended to store the files ramdisk_with_twrp.cpio and magiskboot also on the PC.
The file magiskboot is only required if Magisk is not installed on the phone.
The directory used for the files can be any directory on the phone on a filesystem supporting the execute permission bit.
But do not forget to change the directory used in the script below (variable WORK_DIR) also.
Update 04.06.2023:
see https://github.com/Magisk-Modules-Repo/twrp-keep for a Magisk module with a script to reinstall TWRP into the inactive boot partition after an OS Udate
Update 11.06.2023:
I successfully tested the Magisk Module twrp-keep on an ASUS Zenfone 8 with OmniROM 13, TRWP 3.7.0_12, and Magisk v26.2.
ToDo:
Create an App with GUI to execute the script

Categories

Resources