blob: ad9748e6518611d3be8cfeaaf1136a185d438b19 [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
Isaac Kurth50031952021-09-07 22:04:14 +000013mmcdev="/dev/mmcblk0"
14rwfsdev="/dev/disk/by-partlabel/rwfs"
15
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050016cd /
17mkdir -p $fslist
18mount dev dev -tdevtmpfs
19mount sys sys -tsysfs
20mount proc proc -tproc
21mount tmpfs run -t tmpfs -o mode=755,nodev
22
Adriana Kobylak331a3692020-08-17 12:40:23 -050023# Wait up to 5s for the mmc device to appear. Continue even if the count is
24# exceeded. A failure will be caught later like in the mount command.
Adriana Kobylak331a3692020-08-17 12:40:23 -050025count=0
26while [ $count -lt 5 ]; do
27 if [ -e "${mmcdev}" ]; then
28 break
29 fi
30 sleep 1
31 count=$((count + 1))
32done
33
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050034# Move the secondary GPT to the end of the device if needed. Look for the GPT
35# header signature "EFI PART" located 512 bytes from the end of the device.
Andrew Jeffery9e08ff42021-04-01 22:28:43 +103036if ! tail -c 512 "${mmcdev}" | hexdump -C -n 8 | grep -q "EFI PART"; then
Adriana Kobylak331a3692020-08-17 12:40:23 -050037 sgdisk -e "${mmcdev}"
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050038 partprobe
39fi
40
41# There eMMC GPT labels for the rootfs are rofs-a and rofs-b, and the label for
42# the read-write partition is rwfs. Run udev to make the partition labels show
43# up. Mounting by label allows for partition numbers to change if needed.
44udevd --daemon
45udevadm trigger --type=devices --action=add
46udevadm settle --timeout=10
47
48mkdir -p $rodir
49if ! mount /dev/disk/by-partlabel/"$(get_root)" $rodir -t ext4 -o ro; then
50 /bin/sh
51fi
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050052
Isaac Kurth50031952021-09-07 22:04:14 +000053# Determine if a factory reset has been requested
Isaac Kurth315698e2021-06-25 11:33:46 -050054mkdir -p /var/lock
Isaac Kurth50031952021-09-07 22:04:14 +000055resetval=$(fw_printenv -n rwreset 2>/dev/null)
56gpiopresent=$(gpiofind factory-reset-toggle)
57if [ $? -eq 0 ]; then
58 gpioval=$(gpioget $gpiopresent)
59else
60 gpioval=""
61fi
62# Prevent unnecessary resets on first boot
63if [ -n "$gpioval" -a -z "$resetval" ]; then
64 fw_setenv rwreset $gpioval
65 resetval=$gpioval
66fi
67if [ "$resetval" = "true" -o -n "$gpioval" -a "$resetval" != "$gpioval" ]; then
Isaac Kurth315698e2021-06-25 11:33:46 -050068 echo "Factory reset requested."
69 if ! mkfs.ext4 -F "${rwfsdev}"; then
70 echo "Reformat for factory reset failed."
71 /bin/sh
72 else
Isaac Kurth50031952021-09-07 22:04:14 +000073 # gpioval will be an empty string if factory-reset-toggle was not found
74 fw_setenv rwreset $gpioval
75 echo "rwfs has been formatted."
Isaac Kurth315698e2021-06-25 11:33:46 -050076 fi
77fi
78
Adriana Kobylak1f6ac832020-08-24 15:20:04 -050079fsck.ext4 -p "${rwfsdev}"
80if ! mount "${rwfsdev}" $rodir/var -t ext4 -o rw; then
Adriana Kobylak0998d1e2020-06-03 16:22:42 -050081 /bin/sh
82fi
83
84rm -rf $rodir/var/persist/etc-work/
85mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root
86mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work
87
88for f in $fslist; do
89 mount --move $f $rodir/$f
90done
91
Andrew Jefferyb5cbe9b2021-04-01 22:36:14 +103092exec switch_root $rodir /sbin/init