blob: 268c6ad0e7a28e3239eaba7e34538aa53eeabc19 [file] [log] [blame]
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06001#!/bin/sh
2
3echo update: "$@"
4
Milton Millera5d44e42016-06-15 18:47:38 -05005echoerr() {
6 echo 1>&2 "ERROR: $@"
7}
8
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06009cd /
10if ! test -r /proc/mounts || ! test -f /proc/mounts
11then
12 mkdir -p /proc
13 mount -t proc proc proc
14fi
15if ! test -d /sys/class
16then
17 mkdir -p /sys
18 mount -t sysfs sys sys
19fi
20if ! test -c /dev/null
21then
22 mkdir -p /dev
23 mount -t devtmpfs dev dev
24fi
Milton Millerc88f9272016-05-23 16:00:19 -050025
Milton Miller7dcd1602016-05-23 19:17:55 -050026# 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).
28mtdismounted() {
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.
44childmtds() {
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 IId89d5e02016-01-20 14:57:54 -060054
Milton Miller7a7ee9c2016-05-23 18:22:11 -050055toobig() {
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 IId89d5e02016-01-20 14:57:54 -060063findmtd() {
64 m=$(grep -xl "$1" /sys/class/mtd/*/name)
65 m=${m%/name}
66 m=${m##*/}
67 echo $m
68}
69
Andrew Jefferyacc2c852016-02-23 23:47:23 +103070blkid_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
79probe_fs_type() {
80 fst=$(blkid_fs_type $1)
81 echo ${fst:=jffs2}
82}
83
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060084rwfs=$(findmtd rwfs)
85
Milton Miller54d882e2016-02-05 12:07:53 -060086rwdev=/dev/mtdblock${rwfs#mtd}
Milton Miller54d882e2016-02-05 12:07:53 -060087rwopts=rw
88rorwopts=ro${rwopts#rw}
89
Milton D. Miller II8ad2be52016-02-28 16:07:46 -060090rwdir=/run/initramfs/rw
Milton Miller54d882e2016-02-05 12:07:53 -060091upper=$rwdir/cow
Milton D. Miller II07b26cd2016-02-29 10:32:58 -060092save=/run/save/${upper##*/}
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060093
Milton D. Miller II01fe1342016-02-29 21:34:43 -060094mounted=
Milton Miller3d445402016-05-23 17:41:34 -050095doflash=y
Milton D. Miller II30ef9632016-02-28 16:32:14 -060096doclean=
Milton D. Miller II08afa532016-02-29 23:11:27 -060097dosave=y
98dorestore=y
Milton D. Miller II20b647e2016-02-29 21:44:22 -060099toram=
Milton Miller7a7ee9c2016-05-23 18:22:11 -0500100checksize=y
Milton Miller7dcd1602016-05-23 19:17:55 -0500101checkmount=y
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600102
Milton D. Miller II1790e042016-02-29 11:39:11 -0600103whitelist=/run/initramfs/whitelist
104image=/run/initramfs/image-
105
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600106while test "$1" != "${1#-}"
107do
108 case "$1" in
Milton Millerd7174862016-06-15 16:02:23 -0500109 --help)
110 cat <<HERE
111Usage: $0 [options] -- Write images in /run/initramfs to flash (/dev/mtd*)
112 --help Show this message
113 --no-flash Don't attempt to write images to flash
114 --ignore-size Don't compare image size to mtd device size
115 --ignore-mount Don't check if destination is mounted
116 --save-files Copy whitelisted files to save directory in RAM
117 --no-save-files Don't copy whitelisted files to save directory
118 --copy-files Copy files from save directory to rwfs mountpoint
119 --restore-files Restore files from save directory to rwfs layer
120 --no-restore-files Don't restore saved files from ram to rwfs layer
121 --clean-saved-files Delete saved whitelisted files from RAM
122 --no-clean-saved-files Retain saved whitelisted files in RAM
123HERE
124
125 exit 0 ;;
126
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600127 --no-clean-saved-files)
128 doclean=
129 shift ;;
130 --clean-saved-files)
131 doclean=y
132 shift ;;
Milton D. Miller II08afa532016-02-29 23:11:27 -0600133 --no-save-files)
134 dosave=
135 shift ;;
136 --save-files)
137 dosave=y
138 shift ;;
139 --no-restore-files)
140 dorestore=
141 shift ;;
142 --restore-files)
143 dorestore=y
144 shift ;;
Milton Miller3d445402016-05-23 17:41:34 -0500145 --no-flash)
146 doflash=
147 shift ;;
Milton Miller7a7ee9c2016-05-23 18:22:11 -0500148 --ignore-size)
149 checksize=
150 shift ;;
Milton Miller7dcd1602016-05-23 19:17:55 -0500151 --ignore-mount)
152 checkmount=
153 doflash=
154 shift ;;
Milton D. Miller II20b647e2016-02-29 21:44:22 -0600155 --copy-files)
156 toram=y
157 shift ;;
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600158 *)
Milton Millerd7174862016-06-15 16:02:23 -0500159 echoerr "Unknown option $1. Try $0 --help."
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600160 exit 1 ;;
161 esac
162done
163
Milton D. Miller II01fe1342016-02-29 21:34:43 -0600164if test "x$dosave" = xy
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600165then
Milton D. Miller II01fe1342016-02-29 21:34:43 -0600166 if test ! -d $upper -a -n "$rwfs"
167 then
168 mkdir -p $rwdir
169 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
170 mounted=$rwdir
171 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600172
173 while read f
174 do
Milton Miller54d882e2016-02-05 12:07:53 -0600175 if ! test -e $upper/$f
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600176 then
177 continue
178 fi
Milton Miller54d882e2016-02-05 12:07:53 -0600179 d="$save/$f"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600180 mkdir -p "${d%/*}"
Milton Miller54d882e2016-02-05 12:07:53 -0600181 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller II1790e042016-02-29 11:39:11 -0600182 done < $whitelist
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600183
Milton D. Miller II01fe1342016-02-29 21:34:43 -0600184 if test -n "$mounted"
185 then
186 umount $mounted
187 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600188fi
189
Milton Millerd93b48e2016-02-05 11:19:58 -0600190for f in $image*
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600191do
Milton Millerd93b48e2016-02-05 11:19:58 -0600192 m=$(findmtd ${f#$image})
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600193 if test -z "$m"
194 then
Milton Millera5d44e42016-06-15 18:47:38 -0500195 echoerr "Unable to find mtd partiton for ${f##*/}."
Milton Millerc88f9272016-05-23 16:00:19 -0500196 exit 1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600197 fi
Milton Miller7a7ee9c2016-05-23 18:22:11 -0500198 if test -n "$checksize" && toobig "$f" "$m"
199 then
200 echoerr "Image ${f##*/} too big for $m."
201 exit 1
202 fi
Milton Miller7dcd1602016-05-23 19:17:55 -0500203 for s in $m $(childmtds $m)
204 do
205 if test -n "$checkmount" && mtdismounted $s
206 then
207 echoerr "Device $s is mounted, ${f##*/} is busy."
208 exit 1
209 fi
210 done
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600211done
212
Milton Miller3d445402016-05-23 17:41:34 -0500213if test -n "$doflash"
214then
215 for f in $image*
216 do
217 if test ! -s $f
218 then
219 echo "Skipping empty update of ${f#$image}."
220 rm $f
221 continue
222 fi
223 m=$(findmtd ${f#$image})
224 echo "Updating ${f#$image}..."
225 flashcp -v $f /dev/$m && rm $f
226 done
227fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600228
Milton Miller02104482016-05-23 18:35:21 -0500229if test -d $save -a "x$toram" = xy
Milton D. Miller II20b647e2016-02-29 21:44:22 -0600230then
231 mkdir -p $upper
232 cp -rp $save/. $upper/
233fi
234
Milton D. Miller II08afa532016-02-29 23:11:27 -0600235if test -d $save -a "x$dorestore" = xy
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600236then
Milton D. Miller IId6e25842016-02-28 16:20:19 -0600237 odir=$rwdir
238 rwdir=/run/rw
239 upper=$rwdir${upper#$odir}
240
241 mkdir -p $rwdir
Andrew Jefferyacc2c852016-02-23 23:47:23 +1030242 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller II10aee812016-02-28 18:17:02 -0600243 mkdir -p $upper
Milton Miller54d882e2016-02-05 12:07:53 -0600244 cp -rp $save/. $upper/
245 umount $rwdir
Milton D. Miller IId6e25842016-02-28 16:20:19 -0600246 rmdir $rwdir
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600247fi
248
Milton D. Miller II30ef9632016-02-28 16:32:14 -0600249if test "x$doclean" = xy
250then
251 rm -rf $save
252fi
253
Milton Miller39b6faa2016-02-05 13:56:18 -0600254exit