forked from Lanchon/Flashize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashize-env
executable file
·218 lines (191 loc) · 6.35 KB
/
flashize-env
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
#####################################################
# Flashize-Env #
# 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. #
#####################################################
set -e
set -o pipefail
script="$1"
env="$2"
zip="$3"
log="$4"
count=4
dir="$(dirname "$(readlink -f "$0")")"
version="$("$dir/flashize" "--version")"
if [ "$1" == "-v" ] || [ "$1" == "--version" ] && [ $# -eq 1 ]; then
echo "$version"
exit
fi
envdir="$dir/env"
td="/tmp/lanchon-flashize-env"
error() {
>&2 echo "$@"
}
fatal() {
error "$@"
exit 1
}
if [ "$env" == "-" ]; then env=""; fi
if [ "$zip" == "-" ]; then zip=""; fi
if [ "$log" == "-" ]; then log=""; fi
if [ -z "$script" ] || ([ "$script" == "-" ] && [ -z "$zip" ]) || [ $# -gt $count ]; then
error "Flashize-Env ($version)"
error
error "Converts a shell script to a flashable Android recovery zip. The resulting flashable zip"
error "can temporarily add binaries to or otherwise alter the runtime environment offered by the"
error "recovery before invoking the script."
error
error "Usage: <input-script> [<env-spec> [<output-zip> [<runtime-logfile>]]]"
error
error "Reads the script from standard input if <input-script> is a dash (-)."
error
error "Can temporarily alter the runtime environment, according to the value of <env-spec>:"
error " -The name (without extension) of a configuration provided in Flashize's 'env' directory."
error " -The path of a user-provided zipfile containing the environment configuration."
# error " -Null or a dash to disable alteration of the environment."
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-env-debug' to trace the user-supplied script."
error " -Create '/tmp/flashize-ext-debug' to trace environment setup."
error " -Create '/tmp/flashize-debug' to trace file extraction."
fatal
fi
if [ "$script" != "-" ] && [ ! -f "$script" ]; then
fatal "error: script not found"
fi
if [ -n "$env" ]; then
if [ "${env::1}" != "/" ] && [ "${env::1}" != "." ] && [ "$(basename "$env")" == "$(basename "$env" .zip)" ]; then
env="$envdir/$env.zip"
fi
if [ ! -f "$env" ]; then
fatal "error: environment not found ($env)"
fi
fi
if [ -z "$zip" ]; then
zip="$(dirname "$script")/$(basename "$script" .sh).zip"
fi
tmpzip="$zip.tmp"
rm -f "$zip" "$tmpzip"
#if [ -z "$env" ]; then
# "$dir/flashize" "$script" "$zip" "$log"
# exit
#fi
base="/tmp/flashize"
# 'source' fails on CM13 recovery:
# https://jira.cyanogenmod.org/browse/CYAN-7576
# workaround: '.'
(
cat <<EOF
#####################################################
# Flashize-Env Runtime (${version}) #
# Copyright 2016, Lanchon #
#####################################################
export FLASHIZE_ENV_VERSION='$version'
EOF
printf 'base=%q\n' "$base"
cat <<"EOF"
if [ -f env/init ]; then
. env/init
exit
fi
# CM 13.0 recovery does not allow switching SELinux to permissive mode.
#sestatus=""
#if [ -f env/selinux-set ]; then
# . env/selinux-set
#else
# if [ -n "$(which setenforce)" ]; then
# sestatus="$(getenforce)"
# setenforce permissive
# fi
#fi
mkdir -p env
if [ ! -f env/setup ]; then
EOF
printf ' echo %q >env/setup\n' "$(printf 'PATH=%q:"$PATH"' "$base/bin")"
printf ' echo %q >>env/setup\n' "$(printf 'LD_LIBRARY_PATH=%q:"$LD_LIBRARY_PATH"' "$base/bin")"
printf ' echo %q >>env/setup\n' "export PATH LD_LIBRARY_PATH"
cat <<"EOF"
fi
if [ -f env/pre-setup ]; then
. env/pre-setup
fi
if [ -f env/setup-bin ]; then
. env/setup-bin
else
mkdir -p bin
chmod -R +rx bin
fi
if [ -f env/setup-busybox-symlink ]; then
# CM 13.0 recovery does not allow symlinking.
"$base/bin/busybox" --install -s bin || exit
#for tool in $(bin/busybox --list); do
# if [ ! -e "bin/$tool" ]; then
# ln -s busybox "bin/$tool"
# fi
#done
#unset tool
fi
if [ -f env/setup ]; then
. env/setup
fi
if [ -f env/setup-busybox-alias ]; then
# Workaround for CM 13.0 recovery.
bin/busybox --list >/dev/null || exit
for tool in $(bin/busybox --list); do
if [ ! -e "bin/$tool" ]; then
alias "$tool"="busybox $tool"
fi
done
unset tool
fi
if [ -f env/post-setup ]; then
. env/post-setup
fi
[ ! -f /tmp/flashize-env-debug ] && set +x || set -x
. env/script
# CM 13.0 recovery does not allow switching SELinux to permissive mode.
#code=$?
#[ ! -f /tmp/flashize-ext-debug ] && set +x || set -x
#if [ -f env/selinux-restore ]; then
# . env/selinux-restore
#else
# if [ -n "$sestatus" ]; then
# setenforce "$sestatus"
# fi
#fi
#exit $code
#####################################################
EOF
) | "$dir/flashize-ext" - "$env" "$tmpzip" "$log" "$base"
wd="$td/env"
wdscr="$wd/script"
rm -rf "$td"
mkdir -p "$wd"
cat "$script" >"$wdscr"
zip="$(readlink -f "$zip")"
tmpzip="$(readlink -f "$tmpzip")"
cd "$td"
zip -r9q "$tmpzip" .
rm -rf "$td"
mv "$tmpzip" "$zip"