blob: 316bffce8a3bcdc8559f01b5d3781628566759e4 [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() {
Andrew Jefferya2e2aea2023-01-25 12:35:00 +10305 local cmdline="$(cat /proc/cmdline)"
6 root=
7 for opt in $cmdline
8 do
9 case $opt in
10 root=PARTLABEL=*)
11 root=${opt##root=PARTLABEL=}
12 ;;
13 *)
14 ;;
15 esac
16 done
17 [ -n "$root" ] && echo $root
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050018}
19
20fslist="proc sys dev run"
21rodir=/mnt/rofs
Isaac Kurth50031952021-09-07 22:04:14 +000022mmcdev="/dev/mmcblk0"
23rwfsdev="/dev/disk/by-partlabel/rwfs"
24
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050025cd /
26mkdir -p $fslist
27mount dev dev -tdevtmpfs
28mount sys sys -tsysfs
29mount proc proc -tproc
30mount tmpfs run -t tmpfs -o mode=755,nodev
31
Adriana Kobylak331a3692020-08-17 12:40:23 -050032# Wait up to 5s for the mmc device to appear. Continue even if the count is
33# exceeded. A failure will be caught later like in the mount command.
Adriana Kobylak331a3692020-08-17 12:40:23 -050034count=0
35while [ $count -lt 5 ]; do
36 if [ -e "${mmcdev}" ]; then
37 break
38 fi
39 sleep 1
40 count=$((count + 1))
41done
42
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050043# Move the secondary GPT to the end of the device if needed. Look for the GPT
44# header signature "EFI PART" located 512 bytes from the end of the device.
Andrew Jeffery9e08ff42021-04-01 22:28:43 +103045if ! tail -c 512 "${mmcdev}" | hexdump -C -n 8 | grep -q "EFI PART"; then
Adriana Kobylak331a3692020-08-17 12:40:23 -050046 sgdisk -e "${mmcdev}"
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050047 partprobe
48fi
49
50# There eMMC GPT labels for the rootfs are rofs-a and rofs-b, and the label for
51# the read-write partition is rwfs. Run udev to make the partition labels show
52# up. Mounting by label allows for partition numbers to change if needed.
53udevd --daemon
54udevadm trigger --type=devices --action=add
55udevadm settle --timeout=10
56
57mkdir -p $rodir
58if ! mount /dev/disk/by-partlabel/"$(get_root)" $rodir -t ext4 -o ro; then
59 /bin/sh
60fi
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050061
Isaac Kurth50031952021-09-07 22:04:14 +000062# Determine if a factory reset has been requested
Isaac Kurth315698e2021-06-25 11:33:46 -050063mkdir -p /var/lock
Isaac Kurth50031952021-09-07 22:04:14 +000064resetval=$(fw_printenv -n rwreset 2>/dev/null)
65gpiopresent=$(gpiofind factory-reset-toggle)
66if [ $? -eq 0 ]; then
67 gpioval=$(gpioget $gpiopresent)
68else
69 gpioval=""
70fi
71# Prevent unnecessary resets on first boot
72if [ -n "$gpioval" -a -z "$resetval" ]; then
73 fw_setenv rwreset $gpioval
74 resetval=$gpioval
75fi
76if [ "$resetval" = "true" -o -n "$gpioval" -a "$resetval" != "$gpioval" ]; then
Isaac Kurth315698e2021-06-25 11:33:46 -050077 echo "Factory reset requested."
78 if ! mkfs.ext4 -F "${rwfsdev}"; then
79 echo "Reformat for factory reset failed."
80 /bin/sh
81 else
Isaac Kurth50031952021-09-07 22:04:14 +000082 # gpioval will be an empty string if factory-reset-toggle was not found
83 fw_setenv rwreset $gpioval
84 echo "rwfs has been formatted."
Isaac Kurth315698e2021-06-25 11:33:46 -050085 fi
86fi
87
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050088fsck.ext4 -p "${rwfsdev}"
89if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050090 /bin/sh
91fi
92
93rm -rf $rodir/var/persist/etc-work/
94mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root
95mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work
96
97for f in $fslist; do
98 mount --move $f $rodir/$f
99done
100
Andrew Jefferyb5cbe9b2021-04-01 22:36:14 +1030101exec switch_root $rodir /sbin/init