blob: e51dbf879f73022da6f739fce98ecb335769cb21 [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
3echo update: "$@"
4
Milton Millerd16a7b02016-06-15 18:47:38 -05005echoerr() {
6 echo 1>&2 "ERROR: $@"
7}
8
Milton D. Miller II0e775142016-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 Millere1cbebe2016-05-23 16:00:19 -050025
26if grep mtd /proc/mounts
27then
Milton Millerd16a7b02016-06-15 18:47:38 -050028 echoerr "A mtd device is mounted."
Milton Millere1cbebe2016-05-23 16:00:19 -050029 exit 1
30fi
Milton D. Miller II0e775142016-01-20 14:57:54 -060031
32findmtd() {
33 m=$(grep -xl "$1" /sys/class/mtd/*/name)
34 m=${m%/name}
35 m=${m##*/}
36 echo $m
37}
38
Andrew Jeffery25a50222016-02-23 23:47:23 +103039blkid_fs_type() {
40 # Emulate util-linux's `blkid -s TYPE -o value $1`
41 # Example busybox blkid output:
42 # # blkid /dev/mtdblock5
43 # /dev/mtdblock5: TYPE="squashfs"
44 # Process output to extract TYPE value "squashfs".
45 blkid $1 | sed -e 's/^.*TYPE="//' -e 's/".*$//'
46}
47
48probe_fs_type() {
49 fst=$(blkid_fs_type $1)
50 echo ${fst:=jffs2}
51}
52
Milton D. Miller II0e775142016-01-20 14:57:54 -060053rwfs=$(findmtd rwfs)
54
Milton Millerba65b7b2016-02-05 12:07:53 -060055rwdev=/dev/mtdblock${rwfs#mtd}
Milton Millerba65b7b2016-02-05 12:07:53 -060056rwopts=rw
57rorwopts=ro${rwopts#rw}
58
Milton D. Miller IIc3697de2016-02-28 16:07:46 -060059rwdir=/run/initramfs/rw
Milton Millerba65b7b2016-02-05 12:07:53 -060060upper=$rwdir/cow
Milton D. Miller IIfa8316d2016-02-29 10:32:58 -060061save=/run/save/${upper##*/}
Milton D. Miller II0e775142016-01-20 14:57:54 -060062
Milton D. Miller IIa987d622016-02-29 21:34:43 -060063mounted=
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060064doclean=
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060065dosave=y
66dorestore=y
Milton D. Miller II7141eb02016-02-29 21:44:22 -060067toram=
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060068
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -060069whitelist=/run/initramfs/whitelist
70image=/run/initramfs/image-
71
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060072while test "$1" != "${1#-}"
73do
74 case "$1" in
75 --no-clean-saved-files)
76 doclean=
77 shift ;;
78 --clean-saved-files)
79 doclean=y
80 shift ;;
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060081 --no-save-files)
82 dosave=
83 shift ;;
84 --save-files)
85 dosave=y
86 shift ;;
87 --no-restore-files)
88 dorestore=
89 shift ;;
90 --restore-files)
91 dorestore=y
92 shift ;;
Milton D. Miller II7141eb02016-02-29 21:44:22 -060093 --copy-files)
94 toram=y
95 shift ;;
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060096 *)
Milton Millerd16a7b02016-06-15 18:47:38 -050097 echoerr "Unknown option $1"
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060098 exit 1 ;;
99 esac
100done
101
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600102if test "x$dosave" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600103then
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600104 if test ! -d $upper -a -n "$rwfs"
105 then
106 mkdir -p $rwdir
107 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
108 mounted=$rwdir
109 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600110
111 while read f
112 do
Milton Millerba65b7b2016-02-05 12:07:53 -0600113 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600114 then
115 continue
116 fi
Milton Millerba65b7b2016-02-05 12:07:53 -0600117 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600118 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -0600119 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -0600120 done < $whitelist
Milton D. Miller II0e775142016-01-20 14:57:54 -0600121
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600122 if test -n "$mounted"
123 then
124 umount $mounted
125 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600126fi
127
Milton Miller36d336c2016-02-05 11:19:58 -0600128for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600129do
Milton Miller36d336c2016-02-05 11:19:58 -0600130 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600131 if test -z "$m"
132 then
Milton Millerd16a7b02016-06-15 18:47:38 -0500133 echoerr "Unable to find mtd partiton for ${f##*/}."
Milton Millere1cbebe2016-05-23 16:00:19 -0500134 exit 1
Milton D. Miller II0e775142016-01-20 14:57:54 -0600135 fi
136done
137
Milton Miller36d336c2016-02-05 11:19:58 -0600138for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600139do
Milton D. Miller II5d187bf2016-02-28 16:53:53 -0600140 if test ! -s $f
141 then
142 echo "Skipping empty update of ${f#$image}."
143 rm $f
144 continue
145 fi
Milton Miller36d336c2016-02-05 11:19:58 -0600146 m=$(findmtd ${f#$image})
147 echo "Updating ${f#$image}..."
Milton D. Miller IIec10f892016-02-29 07:55:49 -0600148 flashcp -v $f /dev/$m && rm $f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600149done
150
Milton Millera67cc442016-05-23 18:35:21 -0500151if test -d $save -a "x$toram" = xy
Milton D. Miller II7141eb02016-02-29 21:44:22 -0600152then
153 mkdir -p $upper
154 cp -rp $save/. $upper/
155fi
156
Milton D. Miller IIecf68d52016-02-29 23:11:27 -0600157if test -d $save -a "x$dorestore" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600158then
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600159 odir=$rwdir
160 rwdir=/run/rw
161 upper=$rwdir${upper#$odir}
162
163 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +1030164 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller IIba2b7c92016-02-28 18:17:02 -0600165 mkdir -p $upper
Milton Millerba65b7b2016-02-05 12:07:53 -0600166 cp -rp $save/. $upper/
167 umount $rwdir
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600168 rmdir $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600169fi
170
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600171if test "x$doclean" = xy
172then
173 rm -rf $save
174fi
175
Milton Millerdbacf102016-02-05 13:56:18 -0600176exit