Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
2 | |||||
3 | echo update: "$@" | ||||
4 | |||||
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 5 | echoerr() { |
6 | echo 1>&2 "ERROR: $@" | ||||
7 | } | ||||
8 | |||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 9 | cd / |
10 | if ! test -r /proc/mounts || ! test -f /proc/mounts | ||||
11 | then | ||||
12 | mkdir -p /proc | ||||
13 | mount -t proc proc proc | ||||
14 | fi | ||||
15 | if ! test -d /sys/class | ||||
16 | then | ||||
17 | mkdir -p /sys | ||||
18 | mount -t sysfs sys sys | ||||
19 | fi | ||||
20 | if ! test -c /dev/null | ||||
21 | then | ||||
22 | mkdir -p /dev | ||||
23 | mount -t devtmpfs dev dev | ||||
24 | fi | ||||
Milton Miller | e1cbebe | 2016-05-23 16:00:19 -0500 | [diff] [blame] | 25 | |
Milton Miller | 810dc70 | 2016-05-23 19:17:55 -0500 | [diff] [blame] | 26 | # mtd number N with mtd name Name can be mounted via mtdN, or mtd:Name |
27 | # (with a mtd aware fs) or by /dev/mtdblockN (with a mtd or block fs). | ||||
28 | mtdismounted() { | ||||
29 | m=${1##mtd} | ||||
30 | if grep -s "mtdblock$m " /proc/mounts || grep -s "mtd$m " /proc/mounts | ||||
31 | then | ||||
32 | return 0 | ||||
33 | fi | ||||
34 | n=$(cat /sys/class/mtd/mtd$m/name) | ||||
35 | if test -n "$n" && grep -s "mtd:$n " /proc/mounts | ||||
36 | then | ||||
37 | return 0 | ||||
38 | fi | ||||
39 | return 1 | ||||
40 | } | ||||
41 | |||||
42 | # Detect child partitions when the whole flash is to be updated. | ||||
43 | # Ignore mtdNro and mtdblockN names in the class subsystem directory. | ||||
44 | childmtds() { | ||||
45 | for m in /sys/class/mtd/$1/mtd* | ||||
46 | do | ||||
47 | m=${m##*/} | ||||
48 | if test "${m%ro}" = "${m#mtdblock}" | ||||
49 | then | ||||
50 | echo $m | ||||
51 | fi | ||||
52 | done | ||||
53 | } | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 54 | |
Milton Miller | 1944aac | 2016-05-23 18:22:11 -0500 | [diff] [blame^] | 55 | toobig() { |
56 | if test $(stat -L -c "%s" "$1") -gt $(cat /sys/class/mtd/"$2"/size) | ||||
57 | then | ||||
58 | return 0 | ||||
59 | fi | ||||
60 | return 1 | ||||
61 | } | ||||
62 | |||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 63 | findmtd() { |
64 | m=$(grep -xl "$1" /sys/class/mtd/*/name) | ||||
65 | m=${m%/name} | ||||
66 | m=${m##*/} | ||||
67 | echo $m | ||||
68 | } | ||||
69 | |||||
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 70 | blkid_fs_type() { |
71 | # Emulate util-linux's `blkid -s TYPE -o value $1` | ||||
72 | # Example busybox blkid output: | ||||
73 | # # blkid /dev/mtdblock5 | ||||
74 | # /dev/mtdblock5: TYPE="squashfs" | ||||
75 | # Process output to extract TYPE value "squashfs". | ||||
76 | blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//' | ||||
77 | } | ||||
78 | |||||
79 | probe_fs_type() { | ||||
80 | fst=$(blkid_fs_type $1) | ||||
81 | echo ${fst:=jffs2} | ||||
82 | } | ||||
83 | |||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 84 | rwfs=$(findmtd rwfs) |
85 | |||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 86 | rwdev=/dev/mtdblock${rwfs#mtd} |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 87 | rwopts=rw |
88 | rorwopts=ro${rwopts#rw} | ||||
89 | |||||
Milton D. Miller II | c3697de | 2016-02-28 16:07:46 -0600 | [diff] [blame] | 90 | rwdir=/run/initramfs/rw |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 91 | upper=$rwdir/cow |
Milton D. Miller II | fa8316d | 2016-02-29 10:32:58 -0600 | [diff] [blame] | 92 | save=/run/save/${upper##*/} |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 93 | |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 94 | mounted= |
Milton Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame] | 95 | doflash=y |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 96 | doclean= |
Milton D. Miller II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 97 | dosave=y |
98 | dorestore=y | ||||
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 99 | toram= |
Milton Miller | 1944aac | 2016-05-23 18:22:11 -0500 | [diff] [blame^] | 100 | checksize=y |
Milton Miller | 810dc70 | 2016-05-23 19:17:55 -0500 | [diff] [blame] | 101 | checkmount=y |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 102 | |
Milton D. Miller II | ee91f8d | 2016-02-29 11:39:11 -0600 | [diff] [blame] | 103 | whitelist=/run/initramfs/whitelist |
104 | image=/run/initramfs/image- | ||||
105 | |||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 106 | while test "$1" != "${1#-}" |
107 | do | ||||
108 | case "$1" in | ||||
109 | --no-clean-saved-files) | ||||
110 | doclean= | ||||
111 | shift ;; | ||||
112 | --clean-saved-files) | ||||
113 | doclean=y | ||||
114 | shift ;; | ||||
Milton D. Miller II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 115 | --no-save-files) |
116 | dosave= | ||||
117 | shift ;; | ||||
118 | --save-files) | ||||
119 | dosave=y | ||||
120 | shift ;; | ||||
121 | --no-restore-files) | ||||
122 | dorestore= | ||||
123 | shift ;; | ||||
124 | --restore-files) | ||||
125 | dorestore=y | ||||
126 | shift ;; | ||||
Milton Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame] | 127 | --no-flash) |
128 | doflash= | ||||
129 | shift ;; | ||||
Milton Miller | 1944aac | 2016-05-23 18:22:11 -0500 | [diff] [blame^] | 130 | --ignore-size) |
131 | checksize= | ||||
132 | shift ;; | ||||
Milton Miller | 810dc70 | 2016-05-23 19:17:55 -0500 | [diff] [blame] | 133 | --ignore-mount) |
134 | checkmount= | ||||
135 | doflash= | ||||
136 | shift ;; | ||||
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 137 | --copy-files) |
138 | toram=y | ||||
139 | shift ;; | ||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 140 | *) |
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 141 | echoerr "Unknown option $1" |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 142 | exit 1 ;; |
143 | esac | ||||
144 | done | ||||
145 | |||||
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 146 | if test "x$dosave" = xy |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 147 | then |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 148 | if test ! -d $upper -a -n "$rwfs" |
149 | then | ||||
150 | mkdir -p $rwdir | ||||
151 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts | ||||
152 | mounted=$rwdir | ||||
153 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 154 | |
155 | while read f | ||||
156 | do | ||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 157 | if ! test -e $upper/$f |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 158 | then |
159 | continue | ||||
160 | fi | ||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 161 | d="$save/$f" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 162 | mkdir -p "${d%/*}" |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 163 | cp -rp $upper/$f "${d%/*}/" |
Milton D. Miller II | ee91f8d | 2016-02-29 11:39:11 -0600 | [diff] [blame] | 164 | done < $whitelist |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 165 | |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 166 | if test -n "$mounted" |
167 | then | ||||
168 | umount $mounted | ||||
169 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 170 | fi |
171 | |||||
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 172 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 173 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 174 | m=$(findmtd ${f#$image}) |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 175 | if test -z "$m" |
176 | then | ||||
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 177 | echoerr "Unable to find mtd partiton for ${f##*/}." |
Milton Miller | e1cbebe | 2016-05-23 16:00:19 -0500 | [diff] [blame] | 178 | exit 1 |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 179 | fi |
Milton Miller | 1944aac | 2016-05-23 18:22:11 -0500 | [diff] [blame^] | 180 | if test -n "$checksize" && toobig "$f" "$m" |
181 | then | ||||
182 | echoerr "Image ${f##*/} too big for $m." | ||||
183 | exit 1 | ||||
184 | fi | ||||
Milton Miller | 810dc70 | 2016-05-23 19:17:55 -0500 | [diff] [blame] | 185 | for s in $m $(childmtds $m) |
186 | do | ||||
187 | if test -n "$checkmount" && mtdismounted $s | ||||
188 | then | ||||
189 | echoerr "Device $s is mounted, ${f##*/} is busy." | ||||
190 | exit 1 | ||||
191 | fi | ||||
192 | done | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 193 | done |
194 | |||||
Milton Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame] | 195 | if test -n "$doflash" |
196 | then | ||||
197 | for f in $image* | ||||
198 | do | ||||
199 | if test ! -s $f | ||||
200 | then | ||||
201 | echo "Skipping empty update of ${f#$image}." | ||||
202 | rm $f | ||||
203 | continue | ||||
204 | fi | ||||
205 | m=$(findmtd ${f#$image}) | ||||
206 | echo "Updating ${f#$image}..." | ||||
207 | flashcp -v $f /dev/$m && rm $f | ||||
208 | done | ||||
209 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 210 | |
Milton Miller | a67cc44 | 2016-05-23 18:35:21 -0500 | [diff] [blame] | 211 | if test -d $save -a "x$toram" = xy |
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 212 | then |
213 | mkdir -p $upper | ||||
214 | cp -rp $save/. $upper/ | ||||
215 | fi | ||||
216 | |||||
Milton D. Miller II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 217 | if test -d $save -a "x$dorestore" = xy |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 218 | then |
Milton D. Miller II | facb718 | 2016-02-28 16:20:19 -0600 | [diff] [blame] | 219 | odir=$rwdir |
220 | rwdir=/run/rw | ||||
221 | upper=$rwdir${upper#$odir} | ||||
222 | |||||
223 | mkdir -p $rwdir | ||||
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 224 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts |
Milton D. Miller II | ba2b7c9 | 2016-02-28 18:17:02 -0600 | [diff] [blame] | 225 | mkdir -p $upper |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 226 | cp -rp $save/. $upper/ |
227 | umount $rwdir | ||||
Milton D. Miller II | facb718 | 2016-02-28 16:20:19 -0600 | [diff] [blame] | 228 | rmdir $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 229 | fi |
230 | |||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 231 | if test "x$doclean" = xy |
232 | then | ||||
233 | rm -rf $save | ||||
234 | fi | ||||
235 | |||||
Milton Miller | dbacf10 | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 236 | exit |