blob: 6f265502a47834b645c9fdcccb59bf8374691c9f [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 IIa987d622016-02-29 21:34:43 -060062mounted=
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060063doclean=
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060064dosave=y
65dorestore=y
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060066
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -060067whitelist=/run/initramfs/whitelist
68image=/run/initramfs/image-
69
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060070while test "$1" != "${1#-}"
71do
72 case "$1" in
73 --no-clean-saved-files)
74 doclean=
75 shift ;;
76 --clean-saved-files)
77 doclean=y
78 shift ;;
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060079 --no-save-files)
80 dosave=
81 shift ;;
82 --save-files)
83 dosave=y
84 shift ;;
85 --no-restore-files)
86 dorestore=
87 shift ;;
88 --restore-files)
89 dorestore=y
90 shift ;;
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060091 *)
92 echo 2>&1 "Unknown option $1"
93 exit 1 ;;
94 esac
95done
96
Milton D. Miller IIa987d622016-02-29 21:34:43 -060097if test "x$dosave" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -060098then
Milton D. Miller IIa987d622016-02-29 21:34:43 -060099 if test ! -d $upper -a -n "$rwfs"
100 then
101 mkdir -p $rwdir
102 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
103 mounted=$rwdir
104 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600105
106 while read f
107 do
Milton Millerba65b7b2016-02-05 12:07:53 -0600108 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600109 then
110 continue
111 fi
Milton Millerba65b7b2016-02-05 12:07:53 -0600112 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600113 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -0600114 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -0600115 done < $whitelist
Milton D. Miller II0e775142016-01-20 14:57:54 -0600116
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600117 if test -n "$mounted"
118 then
119 umount $mounted
120 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600121fi
122
Milton Miller36d336c2016-02-05 11:19:58 -0600123for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600124do
Milton Miller36d336c2016-02-05 11:19:58 -0600125 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600126 if test -z "$m"
127 then
Milton Millerba65b7b2016-02-05 12:07:53 -0600128 echo 1>&2 "Unable to find mtd partiton for ${f##*/}."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600129 exec /bin/sh
130 fi
131done
132
Milton Miller36d336c2016-02-05 11:19:58 -0600133for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600134do
Milton Miller36d336c2016-02-05 11:19:58 -0600135 m=$(findmtd ${f#$image})
136 echo "Updating ${f#$image}..."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600137 # flasheraseall /dev/$m && dd if=$f of=/dev/$m
138 flashcp -v $f /dev/$m
139done
140
Milton D. Miller IIecf68d52016-02-29 23:11:27 -0600141if test -d $save -a "x$dorestore" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600142then
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600143 odir=$rwdir
144 rwdir=/run/rw
145 upper=$rwdir${upper#$odir}
146
147 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +1030148 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller IIba2b7c92016-02-28 18:17:02 -0600149 mkdir -p $upper
Milton Millerba65b7b2016-02-05 12:07:53 -0600150 cp -rp $save/. $upper/
151 umount $rwdir
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600152 rmdir $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600153fi
154
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600155if test "x$doclean" = xy
156then
157 rm -rf $save
158fi
159
Milton Millerdbacf102016-02-05 13:56:18 -0600160exit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600161
Milton Millerdbacf102016-02-05 13:56:18 -0600162# NOT REACHED without edit
163# NOT REACHED without edit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600164
Milton Millerdbacf102016-02-05 13:56:18 -0600165echo "Flash completed. Inspect, cleanup and reboot -f to continue."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600166
167export PS1=update-sh#\
168exec /bin/sh