Skip to content

Commit 4d4bf60

Browse files
committed
feat: Improve error handling and logic flow throughout the script.
Signed-off-by: Abhijeet <98699436+gitclone-url@users.noreply.github.com>
1 parent 8b5da5d commit 4d4bf60

File tree

3 files changed

+31
-32
lines changed

3 files changed

+31
-32
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Boot Image Flasher is a shell script designed to simplify the process of flashin
3535
```
3636
3. Download the script and necessary tools using the following command:
3737
```bash
38-
curl -s https://raw.githubusercontent.com/gitclone-url/Boot-img-flasher/Master/boot-img-flasher.sh -o boot-img-flasher.sh && \ { command -v tput >/dev/null && command -v figlet >/dev/null && command -v file >/dev/null || pkg install -y figlet file ncurses-utils; } && \ { command -v sudo >/dev/null || pkg install -y tsu; }
38+
curl -s https://raw.githubusercontent.com/gitclone-url/Boot-img-flasher/master/boot-img-flasher.sh -o boot-img-flasher.sh && { command -v tput >/dev/null && command -v figlet >/dev/null && command -v file >/dev/null || pkg install -y figlet file ncurses-utils; } && { command -v sudo >/dev/null || pkg install -y tsu; }
3939
```
4040
> **Note:** It may take some time for the script to be downloaded, along with the required tools. Please be patient.
4141
@@ -50,12 +50,12 @@ Boot Image Flasher is a shell script designed to simplify the process of flashin
5050
```
5151

5252
Options:
53-
- `-h`, `--help`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display help message with usage information.
54-
- `-t`, `--image-type`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Manually specify the type of image to flash.
53+
- `-h`, `--help`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Display help message with usage information.
54+
- `-t`, `--image-type`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Manually specify the type of image to flash.
5555

5656
Arguments:
57-
- `<IMAGE_PATH>`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path to the boot or init_boot image file.
58-
- `<TYPE>`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Must be either `boot` or `init_boot`.
57+
- `<IMAGE_PATH>`&nbsp;&nbsp;&nbsp;&nbsp;Path to the boot or init_boot image file.
58+
- `<TYPE>`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Must be either `boot` or `init_boot`.
5959

6060
----
6161

boot-img-flasher.sh

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ print_banner() {
6161

6262
local author='Author: Abhijeet'
6363
local git_source='@gitclone-url/Boot-img-flasher'
64-
local description='A Shell script to flash boot image on any Android devices'
64+
local description='A tool for Flashing boot/init_boot images on any android devices'
6565

6666
center_text() {
6767
local text="$1"
@@ -71,8 +71,7 @@ print_banner() {
7171
printf "%*s%b%*s\n" $padding_width "" "$text" $padding_width "" && echo
7272
}
7373

74-
# Check if 'figlet' is available. If it is so, assume that script likely running in Termux
75-
# or a similar environment. And in this case use 'figlet' to display our ASCII art banner.
74+
# Check if 'figlet' is available, and use it to display our ASCII art banner.
7675
# With the addition of '-c' and '-t' option, to ensure proper alignment.
7776
if command -v figlet > /dev/null; then
7877
figlet -ct "$banner_text"
@@ -132,13 +131,13 @@ parse_arguments() {
132131
exit 0
133132
;;
134133
-t|--image-type)
135-
[[ -n "$image_type" ]] && exit_with_error "Image type already specified. Use -t or --image-type only once."
134+
[[ -n "$image_type" ]] && exit_with_error "Image type already specified. Use -t or --image-type only once"
136135
[[ -z "$2" || "$2" == -* ]] && exit_with_error "Expected image type value after -t|--image-type"
137136
image_type="$2"
138137
shift 2
139138
;;
140139
boot|init_boot)
141-
[[ -n "$image_type" ]] && exit_with_error "Please specify only one image type!"
140+
[[ -n "$image_type" ]] && exit_with_error "Please specify only one image type"
142141
image_type="$1"
143142
shift
144143
;;
@@ -147,7 +146,7 @@ parse_arguments() {
147146
;;
148147
*)
149148
[[ -n "$image" ]] && exit_with_error "Unexpected argument: $1"
150-
[[ -n "$image_type" ]] && exit_with_error "Image path must come before image type argument!"
149+
[[ -n "$image_type" ]] && exit_with_error "Image path must come before image type argument"
151150
image="$1"
152151
shift
153152
;;
@@ -164,7 +163,7 @@ validate_arguments() {
164163
[[ "$image_type" != "boot" && "$image_type" != "init_boot" ]] && exit_with_error "Invalid image type. Must be 'boot' or 'init_boot'"
165164

166165
[[ -n "$image" && ! -f "$image" ]] && exit_with_error "File does not exist: $image"
167-
[[ -n "$image" && "${image,,}" != *.img ]] && exit_with_error "Unsupported file type '$(basename "$image")'. This file cannot be flashed!"
166+
[[ -n "$image" && "${image,,}" != *.img ]] && exit_with_error "Unsupported file type '$(basename "$image")'. This file cannot be flashed"
168167
}
169168

170169
require_new_magisk() {
@@ -208,11 +207,10 @@ exit_with_error() {
208207
local message="$1"
209208
local status="${2:-1}"
210209

211-
echo -e "\n${ERR}Error: ${NC}$message"
210+
echo -e "\n${ERR}Error: ${NC}$message!" >&2
212211
exit "$status"
213212
}
214213

215-
216214
supports_color() {
217215
[ -t 1 ] && command -v tput > /dev/null && tput colors > /dev/null
218216
}
@@ -257,8 +255,8 @@ processImageFile() {
257255
mkdir -p "$TMPDIR"
258256
chcon u:object_r:system_file:s0 "$TMPDIR"
259257
cd "$TMPDIR"
260-
unzip -o "$ZIPFILE" '*.img' -d "$TMPDIR" >&2 || return 1
261-
image=$(find "$TMPDIR" -maxdepth 1 -name '*.img' -type f -print -quit)
258+
unzip -o "$ZIPFILE" '*.img' -d "$TMPDIR" >&2
259+
image=$(find "$TMPDIR" -maxdepth 1 -name '*.img' -type f -print -quit) || return 1
262260
}
263261

264262
termux_env() {
@@ -331,7 +329,7 @@ flash_image() {
331329
main() {
332330
# Check if running as root
333331
if [ "$(id -u)" -ne 0 ]; then
334-
exit_with_error "This script requires root privileges to execute. Please run as root."
332+
exit_with_error "This script requires root privileges to execute. Please run as root"
335333
fi
336334

337335
if ! supports_color; then GREEN= BLUE= ERR= NC=; fi
@@ -359,29 +357,30 @@ main() {
359357
fi
360358

361359
echo "- Checking image file, please wait..." && sleep 5
362-
read image image_type < <(processImageFile)
363-
local ret=$?
360+
output=$(processImageFile)
364361

365362
# Handle errors based on the return code
366-
case $ret in
367-
0) echo "- Provided image file: '$(basename "$image")'";;
368-
1) exit_with_error "image file not found in the current directory!" 1 ;;
369-
2) exit_with_error "Failed to extract image from ZIP file!" 2 ;;
370-
3) exit_with_error "Unable to determine image type!" 3 ;;
371-
4) exit_with_error "Unable to find image file!" 4 ;;
372-
*) exit_with_error "An unknown error occurred while finding image file!" 99 ;;
373-
esac
363+
case $? in
364+
0) read image image_type <<< "$output"
365+
echo "- Provided image file: '$(basename "$image")'" ;;
366+
367+
1) exit_with_error "Image file not found inside zip contents" 1 ;;
368+
2) exit_with_error "Image file not found in the current directory" 2 ;;
369+
3) exit_with_error "Unable to determine image type" 3 ;;
370+
4) exit_with_error "Unable to find image file" 4 ;;
371+
*) exit_with_error "An unknown error occurred while finding image file" 99 ;;
372+
esac
374373

375374
echo "- Finding the ${image_type} block, please wait..." && sleep 10
376-
block_device=$(find_partition_block "${image_type}${slot:-}") || exit_with_error "${image_type} block not found. Cannot proceed with flashing."
375+
block_device=$(find_partition_block "${image_type}${slot:-}") || exit_with_error "${image_type} block not found. Cannot proceed with flashing"
377376

378377
echo "- Flashing image to $block_device..."
379378
if ! flash_image "$image" "$block_device"; then
380379
case $? in
381-
1) exit_with_error "Failed to flash, image size exceeds block device size!" 1 ;;
382-
2) exit_with_error "Failed to flash, partition block is read-only!" 2 ;;
383-
3) exit_with_error "Failed to flash, located partition block: '${block_device}' is not a valid block or character device." 3 ;;
384-
*) exit_with_error "Unknown error occurred while flashing the image!" 99 ;;
380+
1) exit_with_error "Failed to flash, image size exceeds block device size" 1 ;;
381+
2) exit_with_error "Failed to flash, partition block is read-only" 2 ;;
382+
3) exit_with_error "Failed to flash, located partition block: '${block_device}' is not a valid block or character device" 3 ;;
383+
*) exit_with_error "Unknown error occurred while flashing the image" 99 ;;
385384
esac
386385
fi
387386

boot_flasher.zip

-54 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)