blob: 6b0d7a025893a47f7eee5f4a4de7977e1989e222 [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 II7141eb02016-02-29 21:44:22 -060066toram=
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060067
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -060068whitelist=/run/initramfs/whitelist
69image=/run/initramfs/image-
70
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060071while test "$1" != "${1#-}"
72do
73 case "$1" in
74 --no-clean-saved-files)
75 doclean=
76 shift ;;
77 --clean-saved-files)
78 doclean=y
79 shift ;;
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060080 --no-save-files)
81 dosave=
82 shift ;;
83 --save-files)
84 dosave=y
85 shift ;;
86 --no-restore-files)
87 dorestore=
88 shift ;;
89 --restore-files)
90 dorestore=y
91 shift ;;
Milton D. Miller II7141eb02016-02-29 21:44:22 -060092 --copy-files)
93 toram=y
94 shift ;;
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060095 *)
96 echo 2>&1 "Unknown option $1"
97 exit 1 ;;
98 esac
99done
100
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600101if test "x$dosave" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600102then
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600103 if test ! -d $upper -a -n "$rwfs"
104 then
105 mkdir -p $rwdir
106 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
107 mounted=$rwdir
108 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600109
110 while read f
111 do
Milton Millerba65b7b2016-02-05 12:07:53 -0600112 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600113 then
114 continue
115 fi
Milton Millerba65b7b2016-02-05 12:07:53 -0600116 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600117 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -0600118 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -0600119 done < $whitelist
Milton D. Miller II0e775142016-01-20 14:57:54 -0600120
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600121 if test -n "$mounted"
122 then
123 umount $mounted
124 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600125fi
126
Milton Miller36d336c2016-02-05 11:19:58 -0600127for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600128do
Milton Miller36d336c2016-02-05 11:19:58 -0600129 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600130 if test -z "$m"
131 then
Milton Millerba65b7b2016-02-05 12:07:53 -0600132 echo 1>&2 "Unable to find mtd partiton for ${f##*/}."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600133 exec /bin/sh
134 fi
135done
136
Milton Miller36d336c2016-02-05 11:19:58 -0600137for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600138do
Milton Miller36d336c2016-02-05 11:19:58 -0600139 m=$(findmtd ${f#$image})
140 echo "Updating ${f#$image}..."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600141 # flasheraseall /dev/$m && dd if=$f of=/dev/$m
142 flashcp -v $f /dev/$m
143done
144
Milton D. Miller II7141eb02016-02-29 21:44:22 -0600145if test "x$toram" = xy
146then
147 mkdir -p $upper
148 cp -rp $save/. $upper/
149fi
150
Milton D. Miller IIecf68d52016-02-29 23:11:27 -0600151if test -d $save -a "x$dorestore" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600152then
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600153 odir=$rwdir
154 rwdir=/run/rw
155 upper=$rwdir${upper#$odir}
156
157 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +1030158 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller IIba2b7c92016-02-28 18:17:02 -0600159 mkdir -p $upper
Milton Millerba65b7b2016-02-05 12:07:53 -0600160 cp -rp $save/. $upper/
161 umount $rwdir
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600162 rmdir $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600163fi
164
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600165if test "x$doclean" = xy
166then
167 rm -rf $save
168fi
169
Milton Millerdbacf102016-02-05 13:56:18 -0600170exit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600171
Milton Millerdbacf102016-02-05 13:56:18 -0600172# NOT REACHED without edit
173# NOT REACHED without edit
Milton D. Miller II0e775142016-01-20 14:57:54 -0600174
Milton Millerdbacf102016-02-05 13:56:18 -0600175echo "Flash completed. Inspect, cleanup and reboot -f to continue."
Milton D. Miller II0e775142016-01-20 14:57:54 -0600176
177export PS1=update-sh#\
178exec /bin/sh