blob: 060073b58ff4d5429920e4c895767c182445c5aa [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
58rwdir=rw
59upper=$rwdir/cow
60save=save/${upper##*/}
Milton D. Miller II0e775142016-01-20 14:57:54 -060061
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060062doclean=
63
64while test "$1" != "${1#-}"
65do
66 case "$1" in
67 --no-clean-saved-files)
68 doclean=
69 shift ;;
70 --clean-saved-files)
71 doclean=y
72 shift ;;
73 *)
74 echo 2>&1 "Unknown option $1"
75 exit 1 ;;
76 esac
77done
78
Milton D. Miller II0e775142016-01-20 14:57:54 -060079if test -n "$rwfs" && test -s whitelist
80then
81
Milton Millerba65b7b2016-02-05 12:07:53 -060082 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +103083 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
Milton D. Miller II0e775142016-01-20 14:57:54 -060084
85 while read f
86 do
Milton Millerba65b7b2016-02-05 12:07:53 -060087 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -060088 then
89 continue
90 fi
Milton Millerba65b7b2016-02-05 12:07:53 -060091 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -060092 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -060093 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller II0e775142016-01-20 14:57:54 -060094 done < whitelist
95
Milton Millerba65b7b2016-02-05 12:07:53 -060096 umount $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -060097fi
98
Milton Miller36d336c2016-02-05 11:19:58 -060099image=/run/initramfs/image-
100for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600101do
Milton Miller36d336c2016-02-05 11:19:58 -0600102 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600103 if test -z "$m"
104 then
Milton Millerba65b7b2016-02-05 12:07:53 -0600105 echo 1>&2 "Unable to find mtd partiton for ${f##*/}."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600106 exec /bin/sh
107 fi
108done
109
Milton Miller36d336c2016-02-05 11:19:58 -0600110for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600111do
Milton Miller36d336c2016-02-05 11:19:58 -0600112 m=$(findmtd ${f#$image})
113 echo "Updating ${f#$image}..."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600114 # flasheraseall /dev/$m && dd if=$f of=/dev/$m
115 flashcp -v $f /dev/$m
116done
117
Milton Millerba65b7b2016-02-05 12:07:53 -0600118if test -d $save
Milton D. Miller II0e775142016-01-20 14:57:54 -0600119then
Andrew Jeffery25a50222016-02-23 23:47:23 +1030120 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton Millerba65b7b2016-02-05 12:07:53 -0600121 cp -rp $save/. $upper/
122 umount $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600123fi
124
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600125if test "x$doclean" = xy
126then
127 rm -rf $save
128fi
129
Milton Millerdbacf102016-02-05 13:56:18 -0600130exit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600131
Milton Millerdbacf102016-02-05 13:56:18 -0600132# NOT REACHED without edit
133# NOT REACHED without edit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600134
Milton Millerdbacf102016-02-05 13:56:18 -0600135echo "Flash completed. Inspect, cleanup and reboot -f to continue."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600136
137export PS1=update-sh#\
138exec /bin/sh