blob: e58056109791cac75c981f98e890eb691831765a [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
3echo update: "$@"
4
5export PS1=update-sh#\
6# exec /bin/sh
7
8cd /
9if ! test -r /proc/mounts || ! test -f /proc/mounts
10then
11 mkdir -p /proc
12 mount -t proc proc proc
13fi
14if ! test -d /sys/class
15then
16 mkdir -p /sys
17 mount -t sysfs sys sys
18fi
19if ! test -c /dev/null
20then
21 mkdir -p /dev
22 mount -t devtmpfs dev dev
23fi
24while grep mtd /proc/mounts
25do
26 echo 1>&2 "Error: A mtd device is mounted."
27 sulogin
28 # exec /bin/sh
29done
30
31findmtd() {
32 m=$(grep -xl "$1" /sys/class/mtd/*/name)
33 m=${m%/name}
34 m=${m##*/}
35 echo $m
36}
37
Andrew Jeffery25a50222016-02-23 23:47:23 +103038blkid_fs_type() {
39 # Emulate util-linux's `blkid -s TYPE -o value $1`
40 # Example busybox blkid output:
41 # # blkid /dev/mtdblock5
42 # /dev/mtdblock5: TYPE="squashfs"
43 # Process output to extract TYPE value "squashfs".
44 blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//'
45}
46
47probe_fs_type() {
48 fst=$(blkid_fs_type $1)
49 echo ${fst:=jffs2}
50}
51
Milton D. Miller II0e775142016-01-20 14:57:54 -060052rwfs=$(findmtd rwfs)
53
Milton Millerba65b7b2016-02-05 12:07:53 -060054rwdev=/dev/mtdblock${rwfs#mtd}
Milton Millerba65b7b2016-02-05 12:07:53 -060055rwopts=rw
56rorwopts=ro${rwopts#rw}
57
Milton D. Miller IIc3697de2016-02-28 16:07:46 -060058rwdir=/run/initramfs/rw
Milton Millerba65b7b2016-02-05 12:07:53 -060059upper=$rwdir/cow
Milton D. Miller IIfa8316d2016-02-29 10:32:58 -060060save=/run/save/${upper##*/}
Milton D. Miller II0e775142016-01-20 14:57:54 -060061
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060062doclean=
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060063dosave=y
64dorestore=y
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060065
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -060066whitelist=/run/initramfs/whitelist
67image=/run/initramfs/image-
68
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060069while test "$1" != "${1#-}"
70do
71 case "$1" in
72 --no-clean-saved-files)
73 doclean=
74 shift ;;
75 --clean-saved-files)
76 doclean=y
77 shift ;;
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060078 --no-save-files)
79 dosave=
80 shift ;;
81 --save-files)
82 dosave=y
83 shift ;;
84 --no-restore-files)
85 dorestore=
86 shift ;;
87 --restore-files)
88 dorestore=y
89 shift ;;
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060090 *)
91 echo 2>&1 "Unknown option $1"
92 exit 1 ;;
93 esac
94done
95
Milton D. Miller II517f9d72016-02-28 15:54:53 -060096if test "x$dosave" = xy -a -n "$rwfs"
Milton D. Miller II0e775142016-01-20 14:57:54 -060097then
Milton Millerba65b7b2016-02-05 12:07:53 -060098 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +103099 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
Milton D. Miller II0e775142016-01-20 14:57:54 -0600100
101 while read f
102 do
Milton Millerba65b7b2016-02-05 12:07:53 -0600103 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600104 then
105 continue
106 fi
Milton Millerba65b7b2016-02-05 12:07:53 -0600107 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600108 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -0600109 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -0600110 done < $whitelist
Milton D. Miller II0e775142016-01-20 14:57:54 -0600111
Milton Millerba65b7b2016-02-05 12:07:53 -0600112 umount $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600113fi
114
Milton Miller36d336c2016-02-05 11:19:58 -0600115for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600116do
Milton Miller36d336c2016-02-05 11:19:58 -0600117 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600118 if test -z "$m"
119 then
Milton Millerba65b7b2016-02-05 12:07:53 -0600120 echo 1>&2 "Unable to find mtd partiton for ${f##*/}."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600121 exec /bin/sh
122 fi
123done
124
Milton Miller36d336c2016-02-05 11:19:58 -0600125for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600126do
Milton Miller36d336c2016-02-05 11:19:58 -0600127 m=$(findmtd ${f#$image})
128 echo "Updating ${f#$image}..."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600129 # flasheraseall /dev/$m && dd if=$f of=/dev/$m
130 flashcp -v $f /dev/$m
131done
132
Milton D. Miller IIecf68d52016-02-29 23:11:27 -0600133if test -d $save -a "x$dorestore" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600134then
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600135 odir=$rwdir
136 rwdir=/run/rw
137 upper=$rwdir${upper#$odir}
138
139 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +1030140 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller IIba2b7c92016-02-28 18:17:02 -0600141 mkdir -p $upper
Milton Millerba65b7b2016-02-05 12:07:53 -0600142 cp -rp $save/. $upper/
143 umount $rwdir
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600144 rmdir $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600145fi
146
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600147if test "x$doclean" = xy
148then
149 rm -rf $save
150fi
151
Milton Millerdbacf102016-02-05 13:56:18 -0600152exit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600153
Milton Millerdbacf102016-02-05 13:56:18 -0600154# NOT REACHED without edit
155# NOT REACHED without edit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600156
Milton Millerdbacf102016-02-05 13:56:18 -0600157echo "Flash completed. Inspect, cleanup and reboot -f to continue."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600158
159export PS1=update-sh#\
160exec /bin/sh