-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-functions
272 lines (209 loc) · 4.95 KB
/
stream-functions
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
#! /bin/sh
. /usr/share/elito-rescue/functions
exit_fn() {
log_info "apply-stream-hunk failed"
_umount_all true
}
_umount_all() {
test -d mnt || return 0
for i in mnt/*; do
umount "$i" || umount -l "$i" || :
done 2>/dev/null
rm -f mounted-*
"$1" && rmdir mnt/* mnt || :
}
_mount() {
local src=$1
local dst=$2
test -e mounted-"$dst" && return
log_info "Mounting '$src'..."
mkdir -p mnt/"$dst"
mount "$src" mnt/"$dst"
touch mounted-"$dst"
log_success ' done'
}
_TARGET_DEVICE=
## _set_target_device <device>
_set_target_device() {
_TARGET_DEVICE=$1
}
## _fn_copy_data <source-name> <dest-name> <partition>
_fn_copy_data() {
local src=$1
local dst=${2:-$1}
local dev=/dev/${_TARGET_DEVICE}$3
_mount "$dev" "part$3"
log_info "Copying data '$src' to '$dst' (partition #$3)..."
mv "$src" mnt/"part$3"/"$dst"
log_success ' done'
_fn_mark_valid "$src"
}
_fn_recv_script() {
log_info "Receiving script '$1'..."
cat > "$1"
echo " done"
}
_fn_recv_data() {
log_info "Receiving data '$1'..."
cat > "$1"
echo " done"
}
_fn_skip_data() {
log_info "Skipping data '$1'..."
cat > /dev/null
echo " done"
}
_fn_wait_dev() {
local dev=/dev/$1
local out_fn=true
if ! test -e wait-"$1" -a ! -b "$dev"; then
log_info "Waiting for target device $1..."
out_fn=echo
local now=`date +%s`
local next=$(( now + $BLOCKDEV_DELAY ))
touch -d@$next wait_tm-"$1"
touch wait_now
while test ! -b "$dev" -a wait_tm-"$1" -nt wait_now; do
usleep $BLOCKDEV_PROGRESS_STEP
echo -n '.'
touch wait_now
done
touch wait-"$1"
fi
if ! test -b "$dev"; then
$out_fn ' timeout'
return 1
else
$out_fn ' done'
return 0
fi
}
_fn_dd_data() {
local type=$1
local dev=/dev/${_TARGET_DEVICE}$2
local resize=$3
local ok=true
_fn_wait_dev "${_TARGET_DEVICE}$2" || panic "no such partition while copying '$type' stream to partition #$2..."
log_info "Copying '$type' stream to partition #$2..."
dd of="$dev" bs=$(( 1024*1024 )) 2> "out-$$.txt" && log_success " done" || {
log_error " failed ($?)"
ok=false
}
sed 's!^! | !' < "out-$$.txt"
rm -f "out-$$.txt"
$ok
if test -n "$resize"; then
log_info "Resizing filesystem..."
echo
resize2fs -p "$dev"
log_success ' done'
fi
}
_fn_exec_script() {
log_info "Executing '$1'..."
chmod +x "$1"
./"$1"
rm -f "$1"
echo " done"
}
_fn_mark_valid() {
touch "valid-$1"
}
_fn_set_trigger() {
test -n "$LED_NAME" || return 0
echo "$1" > /sys/class/leds/"${LED_NAME}"/trigger
}
emit_u8() {
local v=`printf '%x' "$1"`
printf "\\x$v"
}
emit_u16() {
emit_u8 $(( ($1 >> 0) & 0xff ))
emit_u8 $(( ($1 >> 8) & 0xff ))
}
emit_u32() {
emit_u16 $(( ($1 >> 0) & 0xffff ))
emit_u16 $(( ($1 >> 16) & 0xffff ))
}
write_mx28_bootimage() {
local part=$1
local dev=/dev/${_TARGET_DEVICE}$1
local s0=$2
local s1=$3
local sz0
local sz1
local st0
local st1
local geo
local physdrv=`readlink -f /dev/${_TARGET_DEVICE}`
physdrv=${physdrv##/dev/}
read geo < /sys/class/block/${physdrv}p${part}/start
sz0=`stat -c %s "$s0"`
sz1=`stat -c %s "$s1"`
sz0=$(( (sz0 + 511) / 512 ))
sz1=$(( (sz1 + 511) / 512 ))
st0=$(( geo + 1 ))
st1=$(( st0 + sz0 ))
{
emit_u32 0x00112233 # signature
emit_u32 0 # primary boot tag
emit_u32 1 # secondary boot tag
emit_u32 2 # num entries
# bootstream0
emit_u32 0 # chip select; unused
emit_u32 0 # drive type; unused
emit_u32 0 # drive tag
emit_u32 $st0
emit_u32 $sz0
# bootstream1
emit_u32 0 # chip select; unused
emit_u32 0 # drive type; unused
emit_u32 1 # drive tag
emit_u32 $st1
emit_u32 $sz1
} | dd of="$dev" bs=512
dd if="$s0" of="$dev" bs=512 seek=1
dd if="$s1" of="$dev" bs=512 seek=$(( sz0 + 1 ))
}
write_mx6_bootimage() {
local part=$1
local dev=/dev/${_TARGET_DEVICE}$1
local s0=$2
dd if="$s0" of="$dev" bs=512 skip=1
}
_stage_common_start() {
rm -f tmp-* valid-* flag-* mtab
rm -f mounted-* wait*
echo
if test -n "$STREAM_BUILD_TIME"; then
build_time=`date -d @"$STREAM_BUILD_TIME"`
log_info "Stream built at ${build_time}"
echo
fi
_fn_set_trigger "$LED_TRIGGER"
if test -n "${_TARGET_DEVICE}"; then
_fn_wait_dev "$UPDATE_DST" || panic "Target block device does not exist"
fi
}
_stage_common_end() {
_umount_all true
sync
_fn_set_trigger timer
}
main() {
local stage=$1
local type=`printf '0x%08x' "$2"`
local sz=$3
if test -n "${UPDATE_DST}"; then
_set_target_device "$UPDATE_DST"
fi
log_debug "stage=$stage, type=$type, sz=$sz"
trap "exit_fn" EXIT
case $stage in
start|stream|finish|end)
fn_stage_$stage "$type" "$sz" ;;
*)
panic "Unsupported stage $stage" ;;
esac
trap "" EXIT
}