Repository navigation
Expand file tree
/
Copy pathxl
More file actions
executable file
·376 lines (362 loc) · 21.1 KB
/
Copy pathxl
File metadata and controls
executable file
·376 lines (362 loc) · 21.1 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
# xl — drive the XNU loader on the phone over USB (fastboot protocol).
#
# xl enter get the phone into the resident loader (from Android or stock fastboot)
# xl run [N] upload the current kernel, run it (optionally stop at checkpoint N), fetch the log
# xl ramdisk [FILE] upload the big system image (default userland/system.apfs) once; it stays in
# the phone's RAM across kernel runs and is the root filesystem (/dev/md0)
# ("xl ramdisk none" forgets it; the image sent with the kernel is then the root)
# xl screenshot [F] save what is in the phone's framebuffer as a PNG (default logs/screen.png);
# works during a run too (from another terminal)
# xl recording [F] fetch the screen recording the last run made (rec_secs=N among XL_CMDLINE) and
# make a video of it (default logs/recording.mp4)
# xl sound [F] fetch the sound the last run recorded (rec_sound=N among XL_CMDLINE: the first N
# seconds of what Darwin played) and make a WAV file of it (default logs/sound.wav)
# xl camera [P] fetch the frames the last run's camera left (cam=3 and up among XL_CMDLINE) and make
# pictures of them (P-0.png, ...; default prefix logs/camera)
# xl selftest exercise only the kernel->loader return path
# xl log fetch the loader's current log; during a run, the kernel's log so far as well
# xl stop end the run in progress now (the kernel returns as when its time is up)
# xl hang watchdog test: make the loader hang on purpose
# xl reboot cold-reset the phone (back to Android)
# xl reload restart the phone into a freshly built loader (after changing xnuloader/)
# xl memmap the phone's memory map as its firmware reports it
# xl state where is the phone right now
#
# Logs land in logs/. Environment for `run`:
# XL_CMDLINE extra kernel boot-args; default "-v": the boot is shown as text on the phone's screen
# (XL_CMDLINE="" for a blank screen, XL_CMDLINE="-v systrace=1" to trace failing syscalls)
# XL_SECS seconds after which the kernel returns to the loader on its own (default 8)
# XL_FOLLOW=N fetch the kernel's log every N seconds during a run (default 10; XL_FOLLOW= for never), so that a kernel
# that hangs leaves what it said in logs/<time>-run.log.live; after a hang `run` brings the phone back to
# Android itself, or says what to press if it is stuck on the logo
# XL_HOME=fastboot after a hang, keep the phone in its bootloader instead of sending it to Android (and send it
# there from Android): a long session of runs never waits for Android to start
# XL_REBOOT_AFTER=1 when the run is over, send the phone back to Android (it charges there; in the loader it does not,
# and its screen stays on: a phone left in the loader overnight is a flat one by morning)
# XL_FIRMWARE=u-boot|silicium what the phone's bootloader starts the loader with (default: Silicium's UEFI when its
# image is built, else HakiOS's U-Boot: docs/u-boot.md); either way from RAM, nothing is flashed
# XL_SB_DELAY seconds SpringBoard waits before it starts (default 8; see userland/src/inject_sb.c)
# Among XL_CMDLINE, for the phone: bootcpu=7 runs the kernel on the biggest core instead of
# the small one the firmware starts on (the loader then watches from the first core, and a
# kernel that hangs still gets its log out); cpufreq=1 sets that core to its top speed;
# touchscreen=1 turns the touch screen on.
# XL_RAMDISK the image sent with the kernel: the small one that is mounted on /redwood
# (default userland/root.apfs; "none" sends none). Without a system image in the
# phone's RAM it is the root itself (e.g. XL_RAMDISK=userland/hello).
# One phone, several working copies: a run holds a lock, and `stop` and `reboot` from another working
# copy than the one it was started from are refused (XL_FORCE=1 to end it all the same). While a run
# is in progress every other command is refused too, `ramdisk` among them.
# The lock says whether a run is live; it says nothing about the image resident in the phone's memory,
# which outlives both the lock and any note beside it and belongs to whoever uploaded it last. So
# `ramdisk` records who uploaded what, and how big it was, in $PHONE.ramdisk, and `run` prints that
# when the last upload was not from this copy. The record starts empty -- an upload by a copy without
# this change leaves none -- and `run` says so rather than assuming.
cd "$(dirname "$(readlink -f "$0")")" || exit 1
# On a Mac: flock, GNU timeout and stat, and Android's platform-tools, from tools/macos-bin (see its README)
[ "$(uname)" = Darwin ] && PATH=$PWD/tools/macos-bin:$PWD/tools/macos-bin/platform-tools:$PATH
# the kernel and the loader built here, or else the ones that come with the repository
K=hakios-kernel # make kernel makes it
[ -f $K ] || K=prebuilt/hakios-kernel
L=xnuloader/loader.efi
if [ ! -f $L ]; then
L=prebuilt/loader.efi
echo "xl: no xnuloader/loader.efi: using prebuilt/loader.efi, which has no iPhone identity in it (Darwin will not start; make loader builds one from your own IPSW)" >&2
fi
mkdir -p logs
# There is one phone, whichever working copy (tools/new-workspace.sh) talks to it: the locks are
# outside all of them.
PHONE=${XDG_RUNTIME_DIR:-/tmp}/darwin-hakios-phone
# adb starts a server of its own the first time, and the server inherits every open descriptor: started during a run it
# kept the run's lock (descriptor 8, below) for as long as it lived, and every later command said a run was in progress
# (27 October). So adb never gets that descriptor.
adb() { command adb "$@" 8>&-; }
# one fastboot at a time (a run in one terminal, "xl screenshot" in another): each takes the lock for itself
fb() { flock -w 90 $PHONE.lock timeout "${FB_TIMEOUT:-10}" fastboot "$@" 2>&1; }
# does the loader answer while its kernel runs (0.5 and later)? an older one answers only when the run is over
live_loader() { fb getvar version | grep -Eq 'version: 0\.([5-9]|[1-9][0-9])'; }
in_fastboot() { fastboot devices 2>/dev/null | grep -q fastboot; }
is_loader() { in_fastboot && fb getvar product | grep -q 'product: xnuloader'; }
state() {
if is_loader; then echo loader
elif in_fastboot; then echo fastboot
elif [ "$(adb get-state 2>/dev/null)" = device ]; then echo android
else echo absent; fi
}
wait_for() { # wait_for <state> <seconds>
local i
for ((i = 0; i < $2; i += 2)); do [ "$(state)" = "$1" ] && return 0; sleep 2; done
return 1
}
# After a hang the watchdog resets the phone, and what comes up is not always Android: it can stop in its bootloader (it
# charges badly there and a person has to notice), or on the logo, where it does not show on USB at all. Wait for Android;
# a phone that appears in the bootloader or in this loader is sent on to Android at once; if nothing has come back after
# two and a half minutes, say what a person has to do, because nothing here can reach a phone that is not on USB.
# XL_HOME=fastboot: the other way round, for a session of many runs: the phone is kept in its bootloader, where the next
# run starts at once, and one that comes up in Android is sent there (the logo is the one place nothing here can help).
recover_phone() {
local i s
for ((i = 0; i < 150; i += 3)); do
s=$(state)
if [ "${XL_HOME:-android}" = fastboot ]; then
case "$s" in
fastboot) echo "xl: phone is back in its bootloader (XL_HOME=fastboot)"; return 0 ;;
android) echo "xl: the phone came up in Android: sending it to its bootloader (XL_HOME=fastboot)"; adb reboot bootloader ;;
loader) echo "xl: the phone is in this loader: restarting it (it comes up in Android, and goes on from there)"; fb reboot >/dev/null ;;
esac
else
case "$s" in
android) echo "xl: phone is back in Android"; return 0 ;;
fastboot) echo "xl: the phone stopped in its bootloader: sending it on to Android"; timeout 10 fastboot reboot >/dev/null 2>&1 ;;
loader) echo "xl: the phone is in this loader: sending it on to Android"; fb reboot >/dev/null ;;
esac
fi
sleep 3
done
echo "xl: the phone has not come back (state: $(state)): it is on the logo, off, or not on this cable."
echo "xl: hold the power button for about 15 seconds; it restarts into Android."
echo "xl: for the bootloader (fastboot) hold power and volume-down while it restarts."
return 1
}
# What the phone's bootloader starts the loader with: Project Silicium's UEFI (mkxnuimg.py) or HakiOS's U-Boot
# (tools/mkubootimg.py, docs/u-boot.md). XL_FIRMWARE says which; without it, Silicium's when its image has been built
# (it is made from the phone's own vendor drivers and is not in the repository), else U-Boot's.
firmware() {
case "${XL_FIRMWARE:-}" in
u-boot|silicium) echo "$XL_FIRMWARE"; return ;;
esac
if [ -f Mu-Silicium/Build/redwoodPkg/RELEASE_CLANGPDB/FV/SILICIUM_UEFI.fd ] || [ -f devices/redwood/prebuilt/SILICIUM_UEFI.fd ]; then
echo silicium
else
echo u-boot
fi
}
enter() {
case "$(state)" in
loader) return 0 ;;
android) adb reboot bootloader; wait_for fastboot 60 || { echo "xl: phone did not reach fastboot"; return 1; } ;;
fastboot) ;;
absent) echo "xl: waiting for the phone (Android or fastboot)..."
local i; for ((i = 0; i < 180; i += 3)); do [ "$(state)" != absent ] && break; sleep 3; done
[ "$(state)" = absent ] && { echo "xl: phone not reachable"; return 1; }
enter; return ;;
esac
xnuloader/mkpayload.py -o usb-payload.bin loader.efi=$L --usb >/dev/null || { echo "xl: payload build failed"; return 1; }
case "$(firmware)" in
u-boot) tools/mkubootimg.py -d "${DEVICE:-redwood}" -l $L -p usb-payload.bin -o xnu-usb.img >/dev/null ;;
*) ./mkxnuimg.py -p usb-payload.bin -o xnu-usb.img >/dev/null ;;
esac || { echo "xl: image build failed ($(firmware))"; return 1; }
# Right after the phone enters fastboot the first "boot" is sometimes not taken: try twice.
local try
for try in 1 2; do
FB_TIMEOUT=40 fb boot xnu-usb.img | grep -q OKAY || { sleep 3; continue; }
sleep 6
wait_for loader 30 && { rm -f $PHONE.ran; return 0; }
[ "$(state)" = fastboot ] || break
done
echo "xl: loader did not come up on USB (state: $(state))"; return 1
}
fetch_log() { # fetch_log <file>
rm -f "$1"
FB_TIMEOUT=20 fb get_staged "$1" >/dev/null
[ -s "$1" ] && tr -d '\0' < "$1" > "$1.tmp" && mv "$1.tmp" "$1"
}
# A second kernel in one session of the loader dies at once (27 October, four runs: every run after one that returned,
# whatever its arguments, gave no answer within 5 s; every first run of a session lived). Restart the phone into its own
# bootloader between runs ("xl run" warns when this session has run a kernel already).
run() { # run <oem command> <label>
if [ "$(state)" = loader ] && [ -f $PHONE.ran ]; then
echo "xl: warning: a kernel has already run in this session of the loader, and a second one has died at once every time it was tried"
fi
enter || return 1
exec 8> $PHONE.run
flock -n 8 || { echo "xl: a run is in progress, started from $(cat $PHONE.who 2>/dev/null) (xl stop ends it)"; return 1; }
pwd > $PHONE.who
local stamp log result
stamp=$(date +%H%M%S); log="logs/$stamp-$2.log"
if [ "$1" != "oem selftest" ]; then
# The kernel hands control back after XL_SECS seconds even if it is just waiting.
local rd=${XL_RAMDISK:-userland/root.apfs}
case "$rd" in
none|resident) rd= ;;
*) [ -f "$rd" ] || { echo "xl: ramdisk $rd not found"; return 1; } ;;
esac
case $K in prebuilt/*) echo "xl: no kernel built here, running $K" >&2 ;; esac
# The resident image: whose it is and how big, when the last upload was not from here.
# Both values and not just "not from here", because a foreign image of the same kind
# changes nothing, while a foreign one of another kind makes the boot fail in a way that
# names neither it nor its owner -- /sbin/launchd.darwin: No such file or directory, with a
# perfectly good image on this side. The warning is not a refusal: relying on another
# copy's upload is legitimate when you know what it is, and this says what it is.
if [ -s $PHONE.ramdisk ]; then
local rwho rwhat rsize
IFS=$'\t' read -r rwho rwhat rsize < $PHONE.ramdisk
if [ "$rwho" != "$(pwd)" ]; then
echo "xl: the resident image was uploaded from $rwho:" \
"$rwhat, $(( rsize / 1048576 )) MB. Upload your own" \
"(./xl ramdisk FILE) unless that is the one you want" >&2
fi
else
echo "xl: nothing here recorded who uploaded the resident image;" \
"if this run needs a particular one, upload it" >&2
fi
# A run about Wi-Fi is given the radio's own address, which is this phone's and so is kept in a
# file that is not in the repository: fw-wifi/mac, one line, six bytes in hexadecimal with colons.
local args=${XL_CMDLINE--v}
case " $args " in
*" wifi="*|*" wifi_step="*) [ -s fw-wifi/mac ] && args="$args wifi_mac=$(tr -d ' \n\r' < fw-wifi/mac)" ;;
esac
xnuloader/mkpayload.py -o run-payload.bin kernel=$K ${rd:+ramdisk=$rd} --cmdline "loader_return_after=${XL_SECS:-8} epoch=$(date +%s) sb_delay=${XL_SB_DELAY:-8} $args" >/dev/null || return 1
FB_TIMEOUT=30 fb stage run-payload.bin | grep -q OKAY || { echo "xl: upload failed"; return 1; }
fi
local live=; live_loader && live=1
fb $1 >/dev/null
if [ -n "$live" ] && [ "$1" != "oem selftest" ]; then
# the loader goes on answering: ask every two seconds until the result is no longer "running".
# XL_FOLLOW=N: fetch the log as it stands every N seconds as well, so that a phone that
# resets without a word leaves all it said up to then ($log.live)
local XL_FOLLOW=${XL_FOLLOW-10} # (a run that hangs leaves $log.live: what the kernel said up to a few seconds before)
local deadline=$(( $(date +%s) + ${XL_SECS:-8} + 100 )) followed=$(date +%s)
while :; do
result=$(FB_TIMEOUT=15 fb getvar result | grep '^result:')
case "$result" in ""|*running*) ;; *) break ;; esac
[ "$(date +%s)" -ge "$deadline" ] && { result=; break; }
if [ -n "$XL_FOLLOW" ] && [ $(( $(date +%s) - followed )) -ge "$XL_FOLLOW" ]; then
followed=$(date +%s)
# (XL_FOLLOW_KEEP=1: every one kept, $log.live.1, .2, ...: a long run's middle, which the log
# itself loses once more than a megabyte follows it, is then all there)
followed_n=$(( ${followed_n:-0} + 1 ))
fb oem log | grep -q OKAY && fetch_log "$log.live" && { [ -z "$XL_FOLLOW_KEEP" ] || cp "$log.live" "$log.live.$followed_n"; }
fi
sleep 2
done
else
result=$(FB_TIMEOUT=$(( ${XL_SECS:-8} + 100 )) fb getvar result | grep '^result:')
fi
if [ -z "$result" ]; then
echo "xl: no answer — the kernel hung; waiting for the watchdog to reset the phone"
[ -f "$log.live" ] && echo "xl: what the kernel said before it stopped answering: $log.live"
recover_phone
return 2
fi
fetch_log "$log"
touch $PHONE.ran # (this session of the loader has run its kernel: see fresh_session)
ln -sf "$(basename "$log")" logs/last.log
echo "$result [$log]"
case "$result" in
*hung*|*no-such-core*)
# (bootcpu=N) the kernel is stuck on the core it was given and nothing can take the core back
echo "xl: the kernel did not come back from its core; restarting the phone"
fb reboot >/dev/null
return 2 ;;
esac
# XL_REBOOT_AFTER=1: the run is over and the phone goes back to Android, which charges it. A phone left in the loader
# keeps its screen on and charges nothing, so a battery is flat by morning; for a run nobody waits for.
if [ -n "$XL_REBOOT_AFTER" ]; then
echo "xl: XL_REBOOT_AFTER: the phone goes back to Android (it charges there)"
fb reboot >/dev/null
fi
}
recording() { # recording [file.mp4]
enter || return 1
local out=${1:-logs/recording.mp4}
fb oem recording | grep -q OKAY || { echo "xl: the last run left no recording (boot-arg rec_secs=N)"; return 1; }
FB_TIMEOUT=120 fb get_staged logs/recording.raw >/dev/null
# (XL_REC_KEEP=1: the recording itself stays in logs/recording.raw, for tools/recording.py --frames)
tools/recording.py logs/recording.raw "$out" && { [ -n "$XL_REC_KEEP" ] || rm -f logs/recording.raw; }
}
sound() { # sound [file.wav]
enter || return 1
local out=${1:-logs/sound.wav}
fb oem sound | grep -q OKAY || { echo "xl: the last run left no sound recording (boot-arg rec_sound=N)"; return 1; }
FB_TIMEOUT=120 fb get_staged logs/sound.raw >/dev/null
tools/sound.py logs/sound.raw "$out" && rm -f logs/sound.raw
}
camera() { # camera [prefix]: the frames the last run's camera left (boot-arg cam=3 and up), as pictures
enter || return 1
local out=${1:-logs/camera}
fb oem recording | grep -q OKAY || { echo "xl: the last run left no frames (boot-arg cam=3)"; return 1; }
FB_TIMEOUT=120 fb get_staged logs/camera.raw >/dev/null
tools/camraw.py logs/camera.raw "$out"
}
ramdisk() { # ramdisk [<file>|none]
enter || return 1
if [ "$1" = none ]; then
fb oem ramdisk none >/dev/null; rm -f $PHONE.ramdisk
echo "xl: resident ramdisk dropped"; return
fi
set -- "${1:-userland/system.apfs}"
[ -f "$1" ] || { echo "xl: $1 not found"; return 1; }
local t0=$(date +%s) size=$(stat -c %s "$1")
fb oem ramdisk | grep -q OKAY || { echo "xl: the loader refused (no memory window?)"; return 1; }
FB_TIMEOUT=1800 fb stage "$1" | grep -q OKAY || { echo "xl: upload failed"; return 1; }
local dt=$(( $(date +%s) - t0 )); [ $dt -lt 1 ] && dt=1
echo "xl: $(( size / 1048576 )) MB uploaded in $dt s ($(( size / 1048576 / dt )) MB/s)"
# Who put what there, for `run` to warn about below. The resident image is a third state of
# the phone that neither the lock nor the note describes: it stays in memory across runs and
# across working copies, so a run that uploads nothing boots against whatever the last upload
# left -- which may be another copy's, of another kind. That cost two runs on 20 October, and
# the only thing that distinguished them on sight was the loader's own "resident image at pa
# ... size 400000" against "size 97308000". The size is recorded for exactly that reason: a
# foreign image of the same kind is harmless, a foreign one of another kind kills the run and
# says nothing.
printf '%s\t%s\t%s\n' "$(pwd)" "$1" "$size" > $PHONE.ramdisk 2>/dev/null
}
screenshot() { # screenshot [file.png]
enter || return 1
local out=${1:-logs/screen.png} geom
geom=$(fb getvar screen | sed -n 's/^screen: //p')
[ -n "$geom" ] && fb oem screenshot | grep -q OKAY || { echo "xl: the loader has no display"; return 1; }
FB_TIMEOUT=60 fb get_staged logs/screen.raw >/dev/null
python3 - logs/screen.raw "$out" $geom <<'PY' && rm -f logs/screen.raw && echo "xl: $out ($geom)"
import struct, sys, zlib
raw, out, width, height, stride = sys.argv[1], sys.argv[2], *map(int, sys.argv[3:6])
data = open(raw, 'rb').read()
rows = bytearray()
for y in range(height):
line = data[y * stride:y * stride + width * 4]
rows.append(0) # PNG filter type: none
rgb = bytearray(width * 3)
rgb[0::3], rgb[1::3], rgb[2::3] = line[2::4], line[1::4], line[0::4] # the panel is BGRX
rows += rgb
def chunk(kind, body):
return struct.pack('>I', len(body)) + kind + body + struct.pack('>I', zlib.crc32(kind + body))
with open(out, 'wb') as f:
f.write(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)) +
chunk(b'IDAT', zlib.compress(bytes(rows), 6)) + chunk(b'IEND', b''))
PY
}
# stop and reboot end a run. One that was started from another working copy is somebody else's work
# (an agent's measurement, a long session): not ended from here unless XL_FORCE=1 says so.
not_anothers_run() { # not_anothers_run <command>
flock -n $PHONE.run true 2>/dev/null && return 0
[ "$(cat $PHONE.who 2>/dev/null)" = "$(pwd)" ] || [ -n "$XL_FORCE" ] && return 0
echo "xl: a run started from $(cat $PHONE.who 2>/dev/null) is in progress; XL_FORCE=1 ./xl $1 ends it all the same" >&2
return 1
}
# While a run is in progress the loader takes only what does not disturb the kernel: state, screenshot,
# log, stop, reboot. Anything else would wait for nothing.
case "$1" in
""|run|selftest|state|screenshot|log|stop|reboot) ;;
*) if ! flock -n $PHONE.run true 2>/dev/null; then
echo "xl: a run is in progress: only state, screenshot, log, stop and reboot now" >&2; exit 1
fi ;;
esac
case "$1" in
enter) enter && echo "xl: loader is up" ;;
screenshot) screenshot "$2" ;;
recording) recording "$2" ;;
camera) camera "$2" ;;
sound) sound "$2" ;;
ramdisk) ramdisk "$2" ;;
run) run "oem run $2" "run${2:+-cp$2}" ;;
selftest) run "oem selftest" selftest ;;
log) enter && fb oem log >/dev/null && fetch_log logs/loader.log && cat logs/loader.log ;;
stop) not_anothers_run stop && fb oem stop | grep -q OKAY && echo "xl: asked the kernel to come back" ;;
memmap) enter && fb oem memmap >/dev/null && fb oem log >/dev/null && fetch_log logs/loader.log && sed -n '/^memory map/,$p' logs/loader.log ;;
hang) enter && fb oem hang; echo "xl: waiting for the watchdog..."; t0=$(date +%s); wait_for android 150 && echo "xl: back in Android after $(( $(date +%s) - t0 )) s" || echo "xl: not back (state: $(state))" ;;
reboot) not_anothers_run reboot && fb reboot ;;
reload) [ "$(state)" = loader ] && { fb reboot >/dev/null; wait_for android 120; }; enter && echo "xl: loader is up" ;;
state) state ;;
*) awk 'NR > 1 && /^#/ { print; next } NR > 1 { exit }' "$0" ;;
esac