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