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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#!/sbin/sh
export PATH=$PATH:${PWD}
UPDATE_FOLDER=${PWD}
PRIVATE_DIR=$2
set -e
if [ ! -e "$1" ]; then
echo "Command file doesn't exist: $1"
exit 1
fi
mv $1 $1.applying
COMMAND_FILE=$1.applying
REMOVE_LIST="$COMMAND_FILE"
TMP=/cache/tmp
echo "Starting image upgrader: $(date)"
# Functions
verify_signature() {
return 0
# $1 => validation keyring name
# $2 => path to validate
if [ ! -e $2 ]; then
echo "File doesn't exist: $2"
return 1
fi
# Check against the blacklist
if [ -e ${TMP}/system-image/blacklist/pubring.gpg ]; then
export GNUPGHOME=${TMP}/system-image/blacklist/
if gpg --verify $2 >/dev/null 2>&1; then
echo "File signed by a blacklisted key: $2"
return 1
fi
fi
# Check against the keyring
export GNUPGHOME=${TMP}/system-image/$1/
if [ ! -e "$GNUPGHOME" ]; then
echo "Keyring doesn't exist: $1"
return 1
fi
if gpg --verify $2 >/dev/null 2>&1; then
return 0
fi
}
install_keyring() {
# $1 => full path to tarball
# $2 => full path to signature
# Some basic checks
if [ ! -e "$1" ] || [ ! -e "$2" ]; then
echo "Missing keyring files: $1 => $2"
return 1
fi
# Unpacking
TMPDIR=$(busybox mktemp -d ${TMP}/tempdir.XXXXXXXX)
cd $TMPDIR
cat $1 | busybox unxz | tar xf -
if [ ! -e keyring.json ] || [ ! -e keyring.gpg ]; then
rm -Rf $TMPDIR
echo "Invalid keyring: $1"
return 1
fi
# Extract the expiry
keyring_expiry=$(grep "^ \"expiry\": " keyring.json | busybox cut -d: -f2 | busybox sed -e "s/[ \",]//g")
if [ -n "$keyring_expiry" ] && [ "$keyring_expiry" -lt "$(date +%s)" ]; then
rm -Rf $TMPDIR
echo "Keyring expired: $1"
return 1
fi
# Extract the keyring type
keyring_type=$(grep "^ \"type\": " keyring.json | busybox cut -d: -f2 | busybox sed -e "s/[, \"]//g")
if [ -z "$keyring_type" ]; then
rm -Rf $TMPDIR
echo "Missing keyring type: $1"
return 1
fi
if [ -e ${TMP}/system-image/$keyring_type ]; then
rm -Rf $TMPDIR
echo "Keyring already loaded: $1"
return 1
fi
signer="unknown"
case "$keyring_type" in
archive-master)
signer=""
;;
image-master)
signer="archive-master"
;;
image-signing|blacklist)
signer="image-master"
;;
device-signing)
signer="image-signing"
;;
esac
if [ -n "$signer" ] && ! verify_signature $signer $2; then
rm -Rf $TMPDIR
echo "Invalid signature: $1"
return 1
fi
mkdir ${TMP}/system-image/$keyring_type
chmod 700 ${TMP}/system-image/$keyring_type
mv $TMPDIR/keyring.gpg ${TMP}/system-image/$keyring_type/pubring.gpg
chmod 600 ${TMP}/system-image/$keyring_type/pubring.gpg
chown 0:0 ${TMP}/system-image/$keyring_type/pubring.gpg
rm -Rf $TMPDIR
return 0
}
# print out if we will need to create SWAP file
if [ ! -e /data/SWAP.img ]; then
echo "SWAP-file-missing" >&2
fi
# Initialize GPG
rm -Rf ${TMP}/system-image
mkdir -p ${TMP}/system-image
if [ -e ${PWD}/archive-master.tar.xz ]; then
echo "Loading keyring: archive-master.tar.xz"
install_keyring ${PWD}/archive-master.tar.xz ${PWD}/archive-master.tar.xz.asc
fi
# Process the command file
FULL_IMAGE=0
echo "Processing the command file"
while read line
do
set -- $line
case "$1" in
format)
echo "Formating: $2"
case "$2" in
system)
FULL_IMAGE=1
rm -f /data/system.img
rm -f /data/.layout_version
busybox dd if=/dev/zero of=/data/system.img seek=500K bs=4096 count=1
busybox mkfs.ext2 -F /data/system.img
;;
data)
rm -f /data/SWAP.img
rm -Rf /data/system-data
rm -Rf /data/user-data
;;
*)
echo "Unknown format target: $2"
;;
esac
;;
load_keyring)
if [ ! -e "$UPDATE_FOLDER/$2" ] || [ ! -e "$UPDATE_FOLDER/$3" ]; then
echo "Skipping missing file: $2"
continue
fi
REMOVE_LIST="$REMOVE_LIST $UPDATE_FOLDER/$2 $UPDATE_FOLDER/$3"
echo "Loading keyring: $2"
install_keyring $UPDATE_FOLDER/$2 $UPDATE_FOLDER/$3
if [ -e ${TMP}/system-image/image-master/pubring.gpg ] && \
[ ! -e ${TMP}/system-image/blacklist/pubring.gpg ] && \
[ -e /data/system-data/var/lib/system-image/blacklist.tar.xz ] && \
[ -e /data/system-data/var/lib/system-image/blacklist.tar.xz.asc ]; then
echo "Loading blacklist keyring"
install_keyring /data/system-data/var/lib/system-image/blacklist.tar.xz /data/system-data/var/lib/system-image/blacklist.tar.xz.asc
fi
;;
mount)
case "$2" in
system)
mkdir -p /cache/system
aloopmount mount ext4 /data/system.img /cache/system/
;;
*)
echo "Unknown mount target: $2"
;;
esac
;;
unmount)
case "$2" in
system)
aloopmount umount /cache/system && echo .
rmdir /cache/system
;;
*)
echo "Unknown mount target: $2"
;;
esac
;;
update)
if [ ! -e "$UPDATE_FOLDER/$2" ] || [ ! -e "$UPDATE_FOLDER/$3" ]; then
echo "Skipping missing file: $2"
continue
fi
REMOVE_LIST="$REMOVE_LIST $UPDATE_FOLDER/$2 $UPDATE_FOLDER/$3"
if ! verify_signature device-signing $UPDATE_FOLDER/$2 && \
! verify_signature image-signing $UPDATE_FOLDER/$2; then
echo "Invalid signature"
continue
fi
echo "Applying update: $2"
cd /cache
rm -Rf partitions
# Start by removing any file listed in "removed"
if [ "$FULL_IMAGE" != "1" ]; then
cat $UPDATE_FOLDER/$2 | busybox unxz | tar xf - removed >/dev/null 2>&1 || true
if [ -e removed ]; then
while read file; do
rm -Rf $file
done < removed
fi
rm -f removed
fi
# Unpack everything else on top of the system partition
cat $UPDATE_FOLDER/$2 | busybox unxz | tar --checkpoint=200 -xf -
rm -f removed
if [ -e "partitions/boot.img" ]; then
# mako only, need better solution
cat partitions/boot.img > /dev/block/platform/msm_sdcc.1/by-name/recovery
mv partitions/boot.img /cache/ubuntu-boot.img
fi
if false; then #
# Process partition images
while read line; do
set -- $line
part=${1##/}
path=$3
if [ -e partitions/${part}.img ] && [ -e $path ]; then
echo "Flashing ${part} at ${path}"
cat partitions/${part}.img > ${path}
rm partitions/${part}.img
fi
done < /etc/recovery.fstab
fi
;;
*)
echo "Unknown command: $1"
;;
esac
done < $COMMAND_FILE
# Remove the update files
for file in $REMOVE_LIST; do
echo rm $file
done
# Create the SWAP image if missing
if [ ! -e /data/SWAP.img ]; then
echo "Creating SWAP device."
echo "SWAP Checkpoint 0" >&2
# create swap file in private folder, so service can watch progress
dd if=/dev/zero of=$PRIVATE_DIR/SWAP.img bs=4096 count=131072
echo "Moving SWAP device to final destination" >&2
mv $PRIVATE_DIR/SWAP.img /data/
echo "Calling mkswap on /data/SWAP.img" >&2
busybox mkswap /data/SWAP.img
fi
touch /data/.last_update || true
sync
echo "Done upgrading: $(date)"
exit 0
|