Skip to content

Commit e3b424a

Browse files
committed
feat: functionality to pass image as argument, better error handling, and debugging
List of changes: * Determine runtime environment by checking for Termux-specific environment variable. Otherwise, set $DEBUG variable and start shell debugging for Magisk. * Modify find_boot_image(): - Check whether $DEBUG variable was set and true, if not, assume running in Termux and further handle passed image as an argument or search for image in the current directory if no argument is provided. - Return specific error codes at various failure points, replacing the previous singular error handling for ZIP extraction failure using exit_with_error(), which was also not terminating the script correctly with the actual error message! * Enhance exit_with_error(): - Add optional parameter for custom exit status code. - Use provided status code to terminate the script instead of always exiting with 1. * Update main(): - Improve error handling using a case statement for each error code returned by find_boot_image(). - Ensure proper display of error messages and script termination. - Accept image as an argument and pass it to find_boot_image when invoked. - Display provided image name when flashing to the boot block. * Minor structural changes and better code organization, including more context-appropriate variable names and slight adjustments to initialization in device detection logic. Signed-off-by: Abhijeet <98699436+gitclone-url@users.noreply.github.com>
1 parent 8642dcf commit e3b424a

File tree

3 files changed

+138
-67
lines changed

3 files changed

+138
-67
lines changed

README.md

100644100755
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
**What is Boot Image Flasher?**
1111

12-
Boot Image Flasher is a shell script developed to simplify the process of flashing boot images on Android devices, supporting both A/B and legacy (non-A/B) devices. Typically, flashing boot images involves using custom recovery or fastboot, which can be complex and time-consuming. This script eliminates the need for those methods, offering a straightforward, efficient, and user-friendly solution.
12+
Boot Image Flasher is a shell script designed to simplify the process of flashing boot images on Android devices, supporting both A/B and legacy (non-A/B) devices. typically, flashing boot images involves using custom recovery or fastboot, which can be complex and time-consuming. This script eliminates the need for those methods, offering a straightforward, efficient, and user-friendly solution.
1313

1414
### Key Features
1515

1616
- **Automated and User-Friendly:** Simplifies the boot image flashing process with minimal user intervention.
1717
- **Broad Device Support:** Compatible with any Android device, including both A/B and legacy (non-A/B) partition styles.
1818
- **Saves Time and Effort:** Reduces the time and complexity involved in flashing boot images using fastboot or custom recoveries, making it accessible for users with varying levels of technical expertise.
19-
- **Flexible Usage:** Can be used via Termux or as a Magisk module, providing flexibility based on user preferences.
19+
- **Flexible Usage:** Can be used via Termux or flashed as a Magisk module, providing flexibility based on user preferences.
2020

2121
### Prerequisites
2222

@@ -28,17 +28,23 @@ Boot Image Flasher is a shell script developed to simplify the process of flashi
2828
#### Method 1: Via Termux
2929

3030
1. Open [Termux](https://github.com/termux/termux-app) on your Android device.
31-
2. Navigate to the directory where the boot image file is located using `cd`. For example:
31+
2. Navigate to the directory where you want to download the script using `cd`. For example:
3232
```bash
3333
cd /storage/emulated/0/Download
3434
```
35-
3. Copy and paste the following command and hit enter to start running the script:
35+
3. Download the script and necessary tools using the following command:
3636
```bash
37-
curl -s https://raw.githubusercontent.com/gitclone-url/Boot-img-flasher/Master/boot-img-flasher.sh -o boot-img-flasher.sh && { command -v tput figlet &>/dev/null || pkg install -y figlet ncurses-utils; } && { which sudo &>/dev/null || pkg install -y tsu; }; clear; sudo bash boot-img-flasher.sh
37+
curl -s https://raw.githubusercontent.com/gitclone-url/Boot-img-flasher/Master/boot-img-flasher.sh -o boot-img-flasher.sh && { command -v tput figlet &>/dev/null || pkg install -y figlet ncurses-utils; } && { which sudo &>/dev/null || pkg install -y tsu; }
3838
```
39-
> **Note:** It may take some time to run for the first time because the script will be downloaded first along with the required tools. Please be patient.
39+
> **Note:** It may take some time for the script to be downloaded first along with the required tools. Please be patient.
4040
41-
4. Restart your device after the flashing process is complete.
41+
4. Run the script by executing:
42+
```bash
43+
sudo bash boot-img-flasher.sh
44+
```
45+
> **Optional Argument:** You can also specify the path to your boot image as an argument. If you don't provide one, the script will search for the boot image in the current directory.
46+
47+
5. Restart your device after the flashing process is complete.
4248

4349
#### Method 2: Magisk
4450

@@ -72,7 +78,7 @@ This script is intended for advanced users only. Improper use of this script can
7278

7379
### Credits
7480

75-
Special thanks to [topjhonwu](https://github.com/topjohnwu) for [Magisk](https://github.com/topjohnwu/Magisk) and its general utility functions.
81+
Special thanks to [topjohnwu](https://github.com/topjohnwu) for [Magisk](https://github.com/topjohnwu/Magisk) and its general utility functions.
7682

7783
### License
7884

boot-img-flasher.sh

Lines changed: 124 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
#!/system/bin/sh
22

3+
#############################################################################################
4+
# :: Boot Image Flasher ::
5+
#############################################################################################
6+
#
7+
#══════════════════════════════════════════════════════════════════════════
8+
# Usage:
9+
# boot_img_flasher.sh [<boot_image>]
10+
#
11+
# Arguments:
12+
# <boot_image> Optional: Path to the boot image file.
13+
# If not provided, script will search in the current directory
14+
#
15+
#══════════════════════════════════════════════════════════════════════════
16+
# Features:
17+
# * Automated Flashing: Simplifies boot image flashing with minimal user intervention
18+
# * Universal Compatibility: Supports A/B and legacy partition styles devices
19+
# * Flexible Usage: Works in Termux or can be flashed as a Magisk module
20+
# * User-Friendly: Accessible for users with varying levels of technical expertise.
21+
# * Time-Saving: Streamlines the process compared to fastboot or custom recoveries
22+
#
23+
#══════════════════════════════════════════════════════════════════════════
24+
# File Structure:
25+
# boot_img_flasher.sh Main script file
26+
# *.img Boot image to be flashed (if not provided as argument)
27+
#
28+
#══════════════════════════════════════════════════════════════════════════
29+
# Author: Abhijeet
30+
# Source: @gitclone-url/Boot-img-flasher
31+
#
32+
#############################################################################################
33+
334
umask 022
435

36+
# Determine execution environment by checking for Termux-specific environment variable.
37+
# If absent, assume Magisk environment and enable debug tracing
38+
39+
[ -n "$TERMUX_VERSION" ] || [ -n "$PREFIX" ] || { export DEBUG=true; set -o xtrace; }
40+
541
# Global Variables
642
GREEN="\033[1;92m"
743
BLUE="\033[1;94m"
@@ -10,12 +46,6 @@ NC="\033[0m"
1046
OUTFD=$2
1147
ZIPFILE=$3
1248

13-
supports_color() {
14-
[ -t 1 ] && command -v tput > /dev/null && tput colors > /dev/null
15-
}
16-
17-
if ! supports_color; then GREEN= BLUE= ERR= NC=; fi
18-
1949
print_banner() {
2050
local banner_text='Boot img Flasher'
2151

@@ -31,37 +61,37 @@ print_banner() {
3161
printf "%*s%b%*s\n" $padding_width "" "$text" $padding_width "" && echo
3262
}
3363

34-
# Check if 'figlet' is available. If it is so, assume that script likely running in Termux
35-
# or a similar environment. And in this case use 'figlet' to display our ASCII art banner.
36-
# With the addition of '-c' and '-t' option, to ensure proper alignment.
37-
if command -v figlet > /dev/null; then
64+
# Check if 'figlet' is available. If it is so, assume that script likely running in Termux
65+
# or a similar environment. And in this case use 'figlet' to display our ASCII art banner.
66+
# With the addition of '-c' and '-t' option, to ensure proper alignment.
67+
if command -v figlet > /dev/null; then
3868
figlet -ct "$banner_text"
39-
center_text "${BLUE}$description${NC}"
40-
center_text "${GREEN}$author${NC}"
41-
center_text "\033[3mGit Source: $git_source${NC}"
42-
else
43-
# Fallback to default ASCII banner for magisk!
44-
echo " _____ _____"
45-
echo " ( ___ ) ( ___ )"
46-
echo " | |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| |"
47-
echo " | | ____ _ _ | |"
48-
echo " | | | __ ) ___ ___ | |_ (_) _ __ ___ __ _ | |"
49-
echo " | | | _ \ / _ \ / _ \ | __| | || '_ _ \ / _ | | |"
50-
echo " | | | |_) || (_) || (_) || |_ | || | | | | || (_| | | |"
51-
echo " | | |____/ \___/ \___/ \__| |_||_| |_| |_| \__, | | |"
52-
echo " | | _____ _ _ |___/ | |"
53-
echo " | | | ___|| | __ _ ___ | |__ ___ _ __ | |"
54-
echo " | | | |_ | | / _ |/ __|| '_ \ / _ \| '__| | |"
55-
echo " | | | _| | || (_| |\__ \| | | || __/| | | |"
56-
echo " | | |_| |_| \__,_||___/|_| |_| \___||_| | |"
57-
echo " | | | |"
58-
echo " |___|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|___|"
59-
echo " (_____) (_____)"
60-
echo -e "\n"
61-
echo -e " $description\n"
62-
echo -e " $author\n"
63-
echo -e " Git Source: $git_source\n"
64-
fi
69+
center_text "${BLUE}$description${NC}"
70+
center_text "${GREEN}$author${NC}"
71+
center_text "\033[3mGit Source: $git_source${NC}"
72+
else
73+
# Fallback to default ASCII banner for magisk!
74+
echo " _____ _____"
75+
echo " ( ___ ) ( ___ )"
76+
echo " | |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| |"
77+
echo " | | ____ _ _ | |"
78+
echo " | | | __ ) ___ ___ | |_ (_) _ __ ___ __ _ | |"
79+
echo " | | | _ \ / _ \ / _ \ | __| | || '_ _ \ / _ | | |"
80+
echo " | | | |_) || (_) || (_) || |_ | || | | | | || (_| | | |"
81+
echo " | | |____/ \___/ \___/ \__| |_||_| |_| |_| \__, | | |"
82+
echo " | | _____ _ _ |___/ | |"
83+
echo " | | | ___|| | __ _ ___ | |__ ___ _ __ | |"
84+
echo " | | | |_ | | / _ |/ __|| '_ \ / _ \| '__| | |"
85+
echo " | | | _| | || (_| |\__ \| | | || __/| | | |"
86+
echo " | | |_| |_| \__,_||___/|_| |_| \___||_| | |"
87+
echo " | | | |"
88+
echo " |___|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|___|"
89+
echo " (_____) (_____)"
90+
echo -e "\n"
91+
echo -e " $description\n"
92+
echo -e " $author\n"
93+
echo -e " Git Source: $git_source\n"
94+
fi
6595
}
6696

6797
require_new_magisk() {
@@ -90,17 +120,39 @@ grep_prop() {
90120
cat $FILES 2>/dev/null | dos2unix | sed -n "$REGEX" | head -n 1
91121
}
92122

123+
# function exit_with_error()
124+
#
125+
# Prints an error message and terminates script with the specified status code.
126+
#
127+
# Parameters:
128+
# $1 - Error message to print.
129+
# $2 - (Optional) Exit status code. Defaults to 1 if not provided.
130+
#
131+
# Example:
132+
# exit_with_error "File not found" 2
133+
93134
exit_with_error() {
94-
echo -e "\n${ERR}Error: ${NC}$1"
95-
exit 1
135+
local message="$1"
136+
local status="${2:-1}"
137+
138+
echo -e "\n${ERR}Error: ${NC}$message"
139+
exit "$status"
140+
}
141+
142+
143+
supports_color() {
144+
[ -t 1 ] && command -v tput > /dev/null && tput colors > /dev/null
96145
}
97146

98147
find_boot_image() {
99148
local boot_image
100149

101-
if [ -n "$PREFIX" ]; then
102-
# We are definitely in termux environment
103-
boot_image=$(find "$PWD" -maxdepth 1 -name '*.img' -type f -print -quit)
150+
if [ "$DEBUG" != "true" ]; then
151+
if [ -n "$1" ] && [ -f "$1" ]; then
152+
[[ "$1" == *.img ]] && boot_image=$1 || return 1
153+
else
154+
boot_image=$(find "$PWD" -maxdepth 1 -name '*.img' -type f -print -quit) || return 2
155+
fi
104156
else
105157
[ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk
106158
. /data/adb/magisk/util_functions.sh
@@ -110,18 +162,17 @@ find_boot_image() {
110162
chcon u:object_r:system_file:s0 $TMPDIR
111163
cd $TMPDIR
112164
# Extract any .img file from ZIP file
113-
unzip -o "$ZIPFILE" '*.img' -d $TMPDIR >&2 || exit_with_error "Unable to extract boot image"
165+
unzip -o "$ZIPFILE" '*.img' -d $TMPDIR >&2 || return 3
114166
boot_image=$(find "$TMPDIR" -maxdepth 1 -name '*.img' -type f -print -quit)
115167
fi
116168

117169
if [ -n "$boot_image" ]; then
118170
echo "$boot_image"
119171
return 0
120172
fi
121-
return 1
173+
return 4
122174
}
123175

124-
125176
find_boot_block() {
126177
local BLOCK DEV DEVICE DEVNAME PARTNAME UEVENT
127178
for BLOCK in "$@"; do
@@ -174,41 +225,55 @@ main() {
174225
exit_with_error "This script requires root privileges to execute. Please run as root."
175226
fi
176227

228+
if ! supports_color; then GREEN= BLUE= ERR= NC=; fi
229+
177230
mount /data 2>/dev/null
178231
print_banner
179232

180233
local boot_block boot_image
181234

182235
# Determine device type (A/B or legacy)
183-
local is_ab_device=$(grep_cmdline "androidboot.slot_suffix" || grep_cmdline "androidboot.slot" || getprop "ro.boot.slot_suffix")
184-
if [ -n "$is_ab_device" ]; then
236+
local PARTITION_INFO=$(grep_cmdline "androidboot.slot_suffix" || grep_cmdline "androidboot.slot" || getprop "ro.boot.slot_suffix")
237+
local is_ab_device=${PARTITION_INFO:-?}
238+
239+
if [ "$is_ab_device" != "?" ]; then
185240
echo "- A/B partition style detected!" && sleep 2
186-
local slot=${is_ab_device#*=}
241+
local slot=${PARTITION_INFO#*=}
187242
[ "${slot:0:1}" != "_" ] && slot="_$slot"
188243
[ "$slot" = "_normal" ] && slot=""
189244
echo "- Current boot slot: $slot" && sleep 1
190245
else
191246
echo "- Legacy (non-A/B) partition style detected!" && sleep 1
192247
fi
193248

194-
echo "- Finding the boot block, please wait..."
195-
sleep 10
196-
boot_block=$(find_boot_block "boot${slot:-}") || exit_with_error "Boot block not found. Cannot proceed with flashing."
249+
echo "- Checking for boot image, please wait..." && sleep 5
250+
boot_image=$(find_boot_image "$1")
251+
local ret=$?
252+
253+
# Handle errors based on the return code
254+
case $ret in
255+
0) ;; # Success
256+
1) exit_with_error "Provided file '$(basename "$1")' doesn't look like a boot image!" 1 ;;
257+
2) exit_with_error "Boot image not found in the current directory!" 2 ;;
258+
3) exit_with_error "Failed to extract boot image from ZIP file!" 3 ;;
259+
4) exit_with_error "Unable to find boot image file!" 4 ;;
260+
*) exit_with_error "An unknown error occurred while finding boot image file!" 99 ;;
261+
esac
197262

198-
echo "- Checking for boot image, please wait..."
199-
sleep 5
200-
boot_image=$(find_boot_image) || exit_with_error "Boot image not found. Cannot proceed with flashing."
263+
echo "- Finding the boot block, please wait..." && sleep 10
264+
boot_block=$(find_boot_block "boot${slot:-}") || exit_with_error "Boot block not found. Cannot proceed with flashing."
201265

202-
echo "- Flashing boot image to $boot_block..."
266+
echo "- Flashing '$(basename "$boot_image")' to $boot_block..."
203267
if ! flash_image "$boot_image" "$boot_block"; then
204268
case $? in
205-
1) exit_with_error "Failed to flash boot image. Boot image size is larger than the boot block size.";;
206-
2) exit_with_error "Failed to flash boot image. Boot block is read-only.";;
207-
3) exit_with_error "Failed to flash boot image. '$boot_block' is not a block or character device.";;
208-
*) exit_with_error "Failed to flash boot image due to an unknown error.";;
269+
1) exit_with_error "Boot image size exceeds boot block size!" 1 ;;
270+
2) exit_with_error "Boot block is read-only!" 2 ;;
271+
3) exit_with_error "'$boot_block' is not a valid block or character device!" 3 ;;
272+
*) exit_with_error "Unknown error occurred while flashing the boot image!" 99 ;;
209273
esac
210274
fi
275+
211276
echo -e "- ${GREEN}Boot image flashed successfully${NC}"
212277
}
213278

214-
main "$@"
279+
main "$@"

boot_flasher.zip

-168 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)