-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.sh
481 lines (418 loc) · 15.3 KB
/
init.sh
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/sh
#
# Copyright (C) 2018 Ultimaker B.V.
# Author: 2018 Olliver Schinagl <[email protected]>
# Author: 2019 Raymond Siudak <[email protected]>
#
set -eu
ROOT_MOUNT="/mnt/root"
UPDATE_IMAGE="um-update.swu"
UPDATE_IMG_MOUNT="/mnt/update_img"
UPDATE_SRC_MOUNT="/mnt/update"
PROVISIONING_USB_MOUNT="/mnt/usb"
RESCUE_SHELL="no"
FORCE_RESTORE_MODE="no"
PREFIX="${PREFIX:-/usr/}"
EXEC_PREFIX="${PREFIX}"
SBINDIR="${EXEC_PREFIX}/sbin"
EMMC_DEV="/dev/mmcblk2"
SPLASH_SCREEN_PARTITION="${EMMC_DEV}p2"
SYSTEM_UPDATE_ENTRYPOINT="start_update.sh"
UPDATE_DEVICES="/dev/mmcblk[0-9]p[0-9]"
UM_SPLASH="/var/lib/splash_screen/umsplash.fb"
FB_DEVICE="/dev/fb0"
BOM_NUMBER="227380"
BB_BIN="/bin/busybox"
CMDS=" \
[ \
break \
continue \
echo \
exec \
findfs \
i2ctransfer \
mktemp \
modprobe \
mount \
mv \
poweroff \
readlink \
reboot \
shutdown \
sleep \
switch_root \
umount \
watchdog \
"
WATCHDOG_DEV="/dev/watchdog"
init="/sbin/init"
root=""
nfs_root=""
rootflags=""
rootfstype="auto"
rwmode=""
update_tmpfs_mount=""
shutdown()
{
while true; do
poweroff
echo "Please remove power to complete shutdown."
sleep 10s
done
}
restart()
{
echo "Rebooting in 5 seconds ..."
sleep 5s
reboot
modprobe imx2_wdt || true
if [ -w "${WATCHDOG_DEV}" ]; then
watchdog -T 1 -t 60 -F "${WATCHDOG_DEV}"
fi
echo "Failed to reboot, shutting down instead."
shutdown
}
restore_complete_loop()
{
while true; do
echo "Restore complete, remove the recovery SD card and powercycle the printer."
sleep 30s
done
}
rescue_shell()
{
set +eu
${BB_BIN} echo ""
${BB_BIN} echo "##################################################"
${BB_BIN} echo "# ▄▄▄ ▄▄▄ #"
${BB_BIN} echo "# ███████▄▄███▄ #"
${BB_BIN} echo "# ▄███ ▄▄ ▄▄ ▐█ #"
${BB_BIN} echo "# ▄██ ██ ▀▀▀ ▀▀▀▐█▄ #"
${BB_BIN} echo "# ███▄▄██ ▄▄▄▄▄▄▄ ▐██ #"
${BB_BIN} echo "# ██████ ▀ ▀ ▐█ #"
${BB_BIN} echo "# ▄████▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█▄ #"
${BB_BIN} echo "# ████▐█ ▐████████████████▌▐██▄ #"
${BB_BIN} echo "# ▐███▌▄▄█ ▐████████████████▌▐█▄█ #"
${BB_BIN} echo "# ███████ ███████▀▀ ▀██████▌▐██▌ #"
${BB_BIN} echo "# ███████ █████████ ██▐████▌▐██▌ #"
${BB_BIN} echo "# ▄███████ ██████▄██████████▌▐█▀█ #"
${BB_BIN} echo "# ████ ▐█ ▐████████████████▌▐█ █▌ #"
${BB_BIN} echo "# ▀███ ███ ▀▀▀▀ ▐██▀ #"
${BB_BIN} echo "# ▀▀████▄▄████████████████▀ #"
${BB_BIN} echo "# ▐████████▀▀████▀▀▌ █▌ #"
${BB_BIN} echo "# ▄██████ █ ▐███▄▄█▄▄██ #"
${BB_BIN} echo "# ████████▀▀▀█▀████▀ ██ #"
${BB_BIN} echo "# █████████ ▐███ ██ #"
${BB_BIN} echo "# ▀███▄█▄▄▄▄▄███▀▀▀▀▀▀▀ #"
${BB_BIN} echo "# #"
${BB_BIN} echo "# Starting busybox rescue and recovery shell. #"
${BB_BIN} echo "# Tip: type help<enter> for available commands. #"
${BB_BIN} echo "# #"
${BB_BIN} echo "##################################################"
exec ${BB_BIN} sh
}
critical_error()
{
echo "A critical error has occurred, entering recovery mode."
rescue_shell
shutdown
}
boot_root()
{
root_device="${root}"
if [ "${rootfstype}" = "nfs" ]
then
root_device="${nfs_root}"
fi
echo "Mounting ${root_device}."
mount -t "${rootfstype}" -o exec,suid,dev,noatime,"${rootflags},${rwmode}" "${root_device}" "${ROOT_MOUNT}"
kernel_umount
test_init="${init}"
if [ -L "${ROOT_MOUNT}/${init}" ]; then
test_init="${ROOT_MOUNT}/$(readlink "${ROOT_MOUNT}/${init}")"
fi
if [ ! -x "${test_init}" ]; then
echo "Error, no such file '${test_init}'."
critical_error
restart
fi
echo "Starting linux on ${root_device} of type ${rootfstype} with init=${init}."
exec switch_root "${ROOT_MOUNT}" "${init}"
}
set_display_splash()
{
echo "Setting display image."
TMP_MOUNT="/splash"
# Mount the splash screen partition
if [ ! -d ${TMP_MOUNT} ]; then
mkdir -p "${TMP_MOUNT}"
fi
if ! mount -o ro "${SPLASH_SCREEN_PARTITION}" "${TMP_MOUNT}"; then
echo "- Error mounting splash screen partition ${SPLASH_SCREEN_PARTITION} at ${TMP_MOUNT}."
rmdir "${TMP_MOUNT}"
return 0
fi
echo "Sending picture to framebuffer..."
if [ -f "${TMP_MOUNT}/${UM_SPLASH}" ] && [ -c "${FB_DEVICE}" ]; then
cat "${TMP_MOUNT}/${UM_SPLASH}" > "${FB_DEVICE}" || true
else
echo "Unable to output image: '${UM_SPLASH}' to: '${FB_DEVICE}'."
fi
# Unmount the splash screen partition
umount "${TMP_MOUNT}"
rmdir "${TMP_MOUNT}"
}
isBootingRestoreImage()
{
# The partition label 'recovery_data' is an interface between, the recover image creator and executor,
# i.e. jedi-build and um-kernel initrd.
findfs LABEL=recovery_data
}
check_and_set_eeprom_data()
{
# The printer's mainboard EEPROM can be "provisioned" by inserting a USB drive during boot.
# The USB drive must contain a single ext4 partition, containing one or more of the following
# files:
#
# - article_number
# A single-line text file, containing the printer's BOM number in hex notation, exactly 4 bytes.
# For instance, for an S5R2 printer, the file should contain the following line:
# 0x00 0x03 0x45 0xCB
# (this is equivalent to decimal 214475).
#
# - country_code_lock
# A single line text file, containing the printer's locked country code, in hex notation,
# exactly 2 bytes.
# For instance, to lock the printer to the AU country code, the file should contain the line:
# 0x41 0x55
# To clear the lock, the file should contain:
# 0xFF 0xFF
#
# If either of these files is missing, no data is written to that part of the EEPROM.
#
# Relevant EEPROM layout here:
# (See opinicus for full details on EEPROM layout)
# 0x0100 - 0x0104 BOM number / article number
# ...
# 0x0118 - 0x0120 Locked country code
#
# The primary location to record and use the addresses is the opinucus/libEeprom/mainBoard.py file.
dev="/dev/sda1"
article_number_file="${PROVISIONING_USB_MOUNT}/article_number"
country_code_lock_file="${PROVISIONING_USB_MOUNT}/country_code_lock"
# Get the article number from EEPROM
art_num=$(i2ctransfer -y 1 w2@0x57 0x01 0x00 r4)
echo "---> Article number read from EEPROM: >${art_num}<"
art_num_hex=""
for hex in $art_num; do
art_num_hex="${art_num_hex}${hex#0x}" # Remove the "0x" prefix
done
BOM_NUMBER=$(printf "%d\n" "0x$art_num_hex")
echo "---> Article number in decimal: >${BOM_NUMBER}<"
# Get the country code lock from EEPROM
country_code_lock=$(i2ctransfer -y 1 w2@0x57 0x01 0x18 r2)
echo "--> Country code lock read from EEPROM: >${country_code_lock}<"
# Wait for the USB drive to become visible; max 5 seconds.
retries=5
while [ ! -b "${dev}" ] && [ "${retries}" -gt 0 ]; do
retries="$((retries - 1))"
sleep 1
done
echo "Attempting to mount '${dev}'."
if ! mount -t f2fs,ext4,vfat,auto -o ro,exec,noatime "${dev}" "${PROVISIONING_USB_MOUNT}"; then
return 0
fi
if [ -r "${article_number_file}" ]; then
article_number="$(cat "${article_number_file}")"
echo "Trying to write article nr: '${article_number}'."
# shellcheck disable=SC2086
if ! i2ctransfer -y 1 w6@0x57 0x01 0x00 ${article_number}; then
echo "Failed to write article number to EEPROM, skipping."
fi
else
echo "No article number file ${article_number_file} found, skipping."
fi
if [ -r "${country_code_lock_file}" ]; then
country_code="$(cat "${country_code_lock_file}")"
echo "Trying to write country code lock: '${country_code}'."
# shellcheck disable=SC2086
if ! i2ctransfer -y 1 w4@0x57 0x01 0x18 ${country_code}; then
echo "Failed to write country code lock to EEPROM, skipping."
fi
else
echo "No country code lock file ${country_code_lock_file} found, skipping."
fi
umount "${dev}"
}
find_and_run_update()
{
SOFTWARE_INSTALL_MODE="update"
if isBootingRestoreImage || [ "${FORCE_RESTORE_MODE}" = "yes" ]; then
SOFTWARE_INSTALL_MODE="restore"
fi
echo "Checking for updates ..."
for dev in ${UPDATE_DEVICES}; do
if [ ! -b "${dev}" ]; then
continue
fi
echo "Attempting to mount '${dev}'."
if ! mount -t f2fs,ext4,vfat,auto -o exec,noatime "${dev}" "${UPDATE_SRC_MOUNT}"; then
continue
fi
if [ ! -r "${UPDATE_SRC_MOUNT}/${UPDATE_IMAGE}" ]; then
umount "${dev}"
echo "No update image '${UPDATE_IMAGE}' found on '${dev}', trying next."
continue
fi
update_tmpfs_mount="$(mktemp -d)"
echo "Found '${UPDATE_IMAGE}' on '${dev}', moving to tmpfs."
# When we are restoring we want to keep the update image on the SD card.
if [ "${SOFTWARE_INSTALL_MODE}" = "restore" ]; then
if ! cp "${UPDATE_SRC_MOUNT}/${UPDATE_IMAGE}" "${update_tmpfs_mount}"; then
echo "Error, update failed: unable to copy ${UPDATE_IMAGE} to ${update_tmpfs_mount}."
critical_error
break
fi
else
if ! mv "${UPDATE_SRC_MOUNT}/${UPDATE_IMAGE}" "${update_tmpfs_mount}"; then
echo "Error, update failed: unable to move ${UPDATE_IMAGE} to ${update_tmpfs_mount}."
critical_error
break
fi
fi
echo "Attempting to unmount '${UPDATE_SRC_MOUNT}' before performing the update."
if ! umount "${UPDATE_SRC_MOUNT}"; then
echo "Error, update failed: unable to unmount ${UPDATE_SRC_MOUNT}."
critical_error
break
fi
echo "Attempting to mount '${update_tmpfs_mount}/${UPDATE_IMAGE}' to '${UPDATE_IMG_MOUNT}'."
if ! mount "${update_tmpfs_mount}/${UPDATE_IMAGE}" ${UPDATE_IMG_MOUNT}; then
echo "Error, update failed: unable to mount '${update_tmpfs_mount}/${UPDATE_IMAGE}'."
critical_error
break;
fi
echo "Successfully mounted '${UPDATE_IMAGE}', looking for '${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}' script."
if [ ! -x "${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}" ]; then
echo "Error, update failed: no '${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}' script found on '${UPDATE_IMG_MOUNT}'."
critical_error
break
fi
echo "Copying '${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}' from the update image."
if ! cp "${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}" "${update_tmpfs_mount}/"; then
echo "Error, copy of '${UPDATE_IMG_MOUNT}/${SBINDIR}/${SYSTEM_UPDATE_ENTRYPOINT}' to '${update_tmpfs_mount}' failed."
critical_error
break
fi
echo "Copied update script from image, cleaning up."
if ! umount "${UPDATE_IMG_MOUNT}"; then
echo "Warning: unable to unmount '${UPDATE_IMG_MOUNT}'."
fi
echo "Got '${SYSTEM_UPDATE_ENTRYPOINT}' script, trying to execute, with ${BOM_NUMBER}."
if ! "${update_tmpfs_mount}/${SYSTEM_UPDATE_ENTRYPOINT}" "${update_tmpfs_mount}/${UPDATE_IMAGE}" "${EMMC_DEV}" "${BOM_NUMBER}" "${SOFTWARE_INSTALL_MODE}"; then
echo "Error, update failed: executing '${update_tmpfs_mount}/${SYSTEM_UPDATE_ENTRYPOINT} ${update_tmpfs_mount}/${UPDATE_IMAGE} ${EMMC_DEV} ${BOM_NUMBER} ${SOFTWARE_INSTALL_MODE}'."
critical_error
break
fi
# After restore do not remove the file and loop endlessly
if [ "${SOFTWARE_INSTALL_MODE}" = "restore" ]; then
restore_complete_loop
fi
if ! rm "${update_tmpfs_mount}/${UPDATE_IMAGE:?}"; then
echo "Warning, unable to remove '${update_tmpfs_mount}/${UPDATE_IMAGE}'."
fi
restart
done
echo "No updates found."
}
parse_cmdline()
{
if [ ! -r "/proc/cmdline" ]; then
echo "Unable to read /proc/cmdline to parse boot arguments."
exit 1
fi
# shellcheck disable=SC2013
# Disabled because it is not possible in a while read loop
for cmd in $(cat /proc/cmdline); do
case "${cmd}" in
nfsroot=*)
nfs_root="${cmd#*=}"
;;
rescue)
RESCUE_SHELL="yes"
;;
restore)
FORCE_RESTORE_MODE="yes"
;;
ro)
rwmode="ro"
;;
rw)
rwmode="rw"
;;
rootdelay=*)
sleep "${cmd#*=}"
;;
root=*)
_root="${cmd#*=}"
_prefix="${_root%%=*}"
if [ "${_prefix}" = "UUID" ] || \
[ "${_prefix}" = "PARTUUID" ] || \
[ "${_prefix}" = "LABEL" ] || \
[ "${_prefix}" = "PARTLABEL" ]; then
root=$(findfs "${_root}")
else
root="${cmd#*=}"
fi
;;
rootflags=*)
rootflags="${cmd#*=}"
;;
rootfstype=*)
rootfstype="${cmd#*=}"
;;
init=*)
init="${cmd#*=}"
;;
esac
done
}
kernel_mount()
{
mount -t devtmpfs -o nosuid,mode=0755 udev /dev
mount -t proc -o nodev,noexec,nosuid proc /proc
mount -t sysfs -o nodev,noexec,nosuid sysfs /sys
}
kernel_umount()
{
umount /sys
umount /proc
umount /dev
}
toolcheck()
{
echo "Checking command availability."
for cmd in ${CMDS}; do
command -V "${cmd}"
done
}
busybox_setup()
{
"${BB_BIN}" --install -s
}
trap critical_error EXIT
busybox_setup
toolcheck
kernel_mount
parse_cmdline
if [ "${RESCUE_SHELL}" = "yes" ]; then
rescue_shell
fi
set_display_splash
check_and_set_eeprom_data
find_and_run_update
echo
echo "INITRAMFS: Handing over to the main system:"
boot_root
critical_error
exit 1