forked from Lanchon/Flashize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashize
executable file
·184 lines (160 loc) · 5.23 KB
/
flashize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
#####################################################
# Flashize #
# Copyright 2016, Lanchon #
#####################################################
#####################################################
# Flashize is free software licensed under GNU's #
# General Public License (GPL) version 3 and any #
# later version. #
# ------------------------------------------------- #
# The Flashize Runtime is free software licensed #
# under GNU's Lesser General Public License (LGPL) #
# version 3 and any later version. #
#####################################################
version="2016-04-06"
set -e
set -o pipefail
script="$1"
zip="$2"
log="$3"
count=3
if [ "$1" == "-v" ] || [ "$1" == "--version" ] && [ $# -eq 1 ]; then
echo "$version"
exit
fi
td="/tmp/lanchon-flashize"
error() {
>&2 echo "$@"
}
fatal() {
error "$@"
exit 1
}
if [ "$zip" == "-" ]; then zip=""; fi
if [ "$log" == "-" ]; then log=""; fi
if [ -z "$script" ] || ([ "$script" == "-" ] && [ -z "$zip" ]) || [ $# -gt $count ]; then
error "Flashize ($version)"
error
error "Converts a shell script to a flashable Android recovery zip."
error
error "Usage: <input-script> [<output-zip> [<runtime-logfile>]]"
error
error "Reads the script from standard input if <input-script> is a dash (-)."
error
error "Names the output zipfile based on <input-script> if <output-zip> is null or a dash."
error
error "Can create a logfile on the device at runtime, according to the value of <runtime-logfile>:"
error " -The absolute path of the logfile to be created."
error " -A relative path or filename to be interpreted against the path of the zipfile being run."
error " -A colon (:) to use the pathname of the zipfile being run with a '.log' extension."
error " -Null or a dash to disable logging."
error
error "This setting can be overridden by creating a '/tmp/flashize-log' file on the target device:"
error " -If the file is empty then enable logging to '/tmp/flashize.log'."
error " -Otherwise override the value of <runtime-logfile> with the contents of the file."
error
error "Script debugging modes are enabled by creating dummy files on the target device:"
error " -Create '/tmp/flashize-debug' to trace the user-supplied script."
fatal
fi
if [ "$script" != "-" ] && [ ! -f "$script" ]; then
fatal "error: script not found"
fi
if [ -z "$zip" ]; then
zip="$(dirname "$script")/$(basename "$script" .sh).zip"
fi
wd="$td/META-INF/com/google/android"
ubin="$wd/update-binary"
uscr="$wd/updater-script"
rm -rf "$td"
mkdir -p "$wd"
(
cat <<EOF
#!/sbin/sh
#####################################################
# Flashize Runtime (${version}) #
# Copyright 2016, Lanchon #
#####################################################
#####################################################
# The Flashize Runtime is free software licensed #
# under GNU's Lesser General Public License (LGPL) #
# version 3 and any later version. #
# ------------------------------------------------- #
# Note: The code appended to the Flashize Runtime, #
# if any, is independently licensed. #
#####################################################
if [ "\$1" != "lanchon-magic" ]; then
export FLASHIZE_VERSION='$version'
EOF
printf ' log=%q\n' "$log"
cat <<"EOF"
if [ -f /tmp/flashize-log ]; then
log="$(cat /tmp/flashize-log)"
if [ -z "$log" ]; then
log=/tmp/flashize.log
fi
fi
if [ "$log" == "-" ]; then
log=""
fi
out="/proc/self/fd/$2"
zip="$3"
if [ -n "$log" ]; then
if [ "${log::1}" != "/" ]; then
logzip="$zip"
if [ "$logzip" == "/tmp/update.zip" ] || [ "$logzip" == "/sideload/package.zip" ]; then
# if sideloading, for relative logfile naming purposes pretend that the zip is:
logzip=/tmp/flashize.zip
fi
if [ "$log" == ":" ]; then
log="$(basename "$logzip" .zip).log"
fi
log="$(dirname "$logzip")/$log"
fi
mkdir -p "$(dirname "$log")"
fi
print() {
local s="$1"
if [ -n "$log" ]; then
>>"$log" echo "$s"
fi
if [ -z "$s" ]; then s=" "; fi
>$out echo "ui_print $s"
>$out echo "ui_print"
}
print
if [ -n "$log" ]; then
>"$log"
fi
set -o pipefail
/sbin/sh "$0" "lanchon-magic" "$zip" 2>&1 | (
IFS=""
while read -r line; do print "$line"; done
)
code=$?
print
if [ "$code" -eq "0" ]; then
print "[OK]";
else
print "[ERROR $code]";
fi
log=""
print
exit $code
fi
shift
[ ! -f /tmp/flashize-debug ] || set -x
#####################################################
EOF
cat "$script"
) >"$ubin"
cat >"$uscr" <<EOF
#####################################################
# Powered by Lanchon's Flashize #
#####################################################
EOF
zip="$(readlink -f "$zip")"
cd "$td"
zip -r9q "$zip" .
rm -rf "$td"