blob: 12cf53a228147289cf538cfd2d11cbea4e4d5528 [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() {
Patrick Williams2345ace2023-04-14 14:03:05 -05005 _cmdline="$(cat /proc/cmdline)"
Andrew Jefferya2e2aea2023-01-25 12:35:00 +10306 root=
Patrick Williams2345ace2023-04-14 14:03:05 -05007 for opt in $_cmdline
Andrew Jefferya2e2aea2023-01-25 12:35:00 +10308 do
9 case $opt in
10 root=PARTLABEL=*)
11 root=${opt##root=PARTLABEL=}
12 ;;
13 *)
14 ;;
15 esac
16 done
Patrick Williams2345ace2023-04-14 14:03:05 -050017 [ -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 /
Patrick Williams2345ace2023-04-14 14:03:05 -050026mkdir -p "$fslist"
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050027mount 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
Patrick Williams2345ace2023-04-14 14:03:05 -050051# the read-write partition is rwfs. Run udev to make the partition labels show
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050052# 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)
Patrick Williams2345ace2023-04-14 14:03:05 -050065if gpiopresent=$(gpiofind factory-reset-toggle) ; then
Zev Weissf307a2f2023-04-18 15:56:22 -070066 # gpiopresent contains both the gpiochip and line number as
67 # separate words, and gpioget needs to see them as such.
68 # shellcheck disable=SC2086
69 gpioval=$(gpioget $gpiopresent)
Isaac Kurth50031952021-09-07 22:04:14 +000070else
71 gpioval=""
72fi
73# Prevent unnecessary resets on first boot
Patrick Williams2345ace2023-04-14 14:03:05 -050074if [ -n "$gpioval" ] && [ -z "$resetval" ]; then
75 fw_setenv rwreset "$gpioval"
Isaac Kurth50031952021-09-07 22:04:14 +000076 resetval=$gpioval
77fi
Patrick Williams2345ace2023-04-14 14:03:05 -050078if [ "$resetval" = "true" ] || [ -n "$gpioval" ] && [ "$resetval" != "$gpioval" ]; then
Isaac Kurth315698e2021-06-25 11:33:46 -050079 echo "Factory reset requested."
80 if ! mkfs.ext4 -F "${rwfsdev}"; then
81 echo "Reformat for factory reset failed."
82 /bin/sh
83 else
Isaac Kurth50031952021-09-07 22:04:14 +000084 # gpioval will be an empty string if factory-reset-toggle was not found
Patrick Williams2345ace2023-04-14 14:03:05 -050085 fw_setenv rwreset "$gpioval"
Isaac Kurth50031952021-09-07 22:04:14 +000086 echo "rwfs has been formatted."
Isaac Kurth315698e2021-06-25 11:33:46 -050087 fi
88fi
89
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050090fsck.ext4 -p "${rwfsdev}"
91if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050092 /bin/sh
93fi
94
95rm -rf $rodir/var/persist/etc-work/
96mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root
97mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work
98
99for f in $fslist; do
Patrick Williams2345ace2023-04-14 14:03:05 -0500100 mount --move "$f" "$rodir/$f"
Adriana Kobylak0998d1e2020-06-03 16:22:42 -0500101done
102
Andrew Jefferyb5cbe9b2021-04-01 22:36:14 +1030103exec switch_root $rodir /sbin/init