blob: 367c302c72a2dad6f52e765054ede612e1a8344d [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
62if test -n "$rwfs" && test -s whitelist
63then
64
Milton Millerba65b7b2016-02-05 12:07:53 -060065 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +103066 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
Milton D. Miller II0e775142016-01-20 14:57:54 -060067
68 while read f
69 do
Milton Millerba65b7b2016-02-05 12:07:53 -060070 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -060071 then
72 continue
73 fi
Milton Millerba65b7b2016-02-05 12:07:53 -060074 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -060075 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -060076 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller II0e775142016-01-20 14:57:54 -060077 done < whitelist
78
Milton Millerba65b7b2016-02-05 12:07:53 -060079 umount $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -060080fi
81
Milton Miller36d336c2016-02-05 11:19:58 -060082image=/run/initramfs/image-
83for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -060084do
Milton Miller36d336c2016-02-05 11:19:58 -060085 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -060086 if test -z "$m"
87 then
Milton Millerba65b7b2016-02-05 12:07:53 -060088 echo 1>&2 "Unable to find mtd partiton for ${f##*/}."
Milton D. Miller II0e775142016-01-20 14:57:54 -060089 exec /bin/sh
90 fi
91done
92
Milton Miller36d336c2016-02-05 11:19:58 -060093for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -060094do
Milton Miller36d336c2016-02-05 11:19:58 -060095 m=$(findmtd ${f#$image})
96 echo "Updating ${f#$image}..."
Milton D. Miller II0e775142016-01-20 14:57:54 -060097 # flasheraseall /dev/$m && dd if=$f of=/dev/$m
98 flashcp -v $f /dev/$m
99done
100
Milton Millerba65b7b2016-02-05 12:07:53 -0600101if test -d $save
Milton D. Miller II0e775142016-01-20 14:57:54 -0600102then
Andrew Jeffery25a50222016-02-23 23:47:23 +1030103 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton Millerba65b7b2016-02-05 12:07:53 -0600104 cp -rp $save/. $upper/
105 umount $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600106fi
107
Milton Millerdbacf102016-02-05 13:56:18 -0600108exit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600109
Milton Millerdbacf102016-02-05 13:56:18 -0600110# NOT REACHED without edit
111# NOT REACHED without edit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600112
Milton Millerdbacf102016-02-05 13:56:18 -0600113echo "Flash completed. Inspect, cleanup and reboot -f to continue."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600114
115export PS1=update-sh#\
116exec /bin/sh