blob: 575b7605aa6d1925aba5cbfede52d8b758b475ad [file] [log] [blame]
Adriana Kobylak0998d1e2020-06-03 16:22:42 -05001#!/bin/sh
2
3# Get the value of the root env variable found in /proc/cmdline
4get_root() {
5 local root="$(cat /proc/cmdline)"
6 root="${root##* root=PARTLABEL=}"
7 root="${root%% *}"
8 [ "${root}" != "" ] && echo "${root}"
9}
10
11fslist="proc sys dev run"
12rodir=/mnt/rofs
13cd /
14mkdir -p $fslist
15mount dev dev -tdevtmpfs
16mount sys sys -tsysfs
17mount proc proc -tproc
18mount tmpfs run -t tmpfs -o mode=755,nodev
19
Adriana Kobylak331a3692020-08-17 12:40:23 -050020# Wait up to 5s for the mmc device to appear. Continue even if the count is
21# exceeded. A failure will be caught later like in the mount command.
22mmcdev="/dev/mmcblk0"
23count=0
24while [ $count -lt 5 ]; do
25 if [ -e "${mmcdev}" ]; then
26 break
27 fi
28 sleep 1
29 count=$((count + 1))
30done
31
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050032# Move the secondary GPT to the end of the device if needed. Look for the GPT
33# header signature "EFI PART" located 512 bytes from the end of the device.
Andrew Jeffery9e08ff42021-04-01 22:28:43 +103034if ! tail -c 512 "${mmcdev}" | hexdump -C -n 8 | grep -q "EFI PART"; then
Adriana Kobylak331a3692020-08-17 12:40:23 -050035 sgdisk -e "${mmcdev}"
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050036 partprobe
37fi
38
39# There eMMC GPT labels for the rootfs are rofs-a and rofs-b, and the label for
40# the read-write partition is rwfs. Run udev to make the partition labels show
41# up. Mounting by label allows for partition numbers to change if needed.
42udevd --daemon
43udevadm trigger --type=devices --action=add
44udevadm settle --timeout=10
45
46mkdir -p $rodir
47if ! mount /dev/disk/by-partlabel/"$(get_root)" $rodir -t ext4 -o ro; then
48 /bin/sh
49fi
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050050
51rwfsdev="/dev/disk/by-partlabel/rwfs"
Isaac Kurth315698e2021-06-25 11:33:46 -050052mkdir -p /var/lock
53if test $(fw_printenv -n rwreset) = "true"; then
54 echo "Factory reset requested."
55 if ! mkfs.ext4 -F "${rwfsdev}"; then
56 echo "Reformat for factory reset failed."
57 /bin/sh
58 else
59 fw_setenv rwreset
60 echo "Formatting of rwfs is complete."
61 fi
62fi
63
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050064fsck.ext4 -p "${rwfsdev}"
65if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050066 /bin/sh
67fi
68
69rm -rf $rodir/var/persist/etc-work/
70mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root
71mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work
72
73for f in $fslist; do
74 mount --move $f $rodir/$f
75done
76
Andrew Jefferyb5cbe9b2021-04-01 22:36:14 +103077exec switch_root $rodir /sbin/init