blob: 9f5bede430e4f92025e20b6e59313aa6a61dc66b [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 Miller4015b7a2016-05-23 17:41:34 -050064doflash=y
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060065doclean=
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060066dosave=y
67dorestore=y
Milton D. Miller II7141eb02016-02-29 21:44:22 -060068toram=
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060069
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -060070whitelist=/run/initramfs/whitelist
71image=/run/initramfs/image-
72
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -060073while test "$1" != "${1#-}"
74do
75 case "$1" in
76 --no-clean-saved-files)
77 doclean=
78 shift ;;
79 --clean-saved-files)
80 doclean=y
81 shift ;;
Milton D. Miller IIecf68d52016-02-29 23:11:27 -060082 --no-save-files)
83 dosave=
84 shift ;;
85 --save-files)
86 dosave=y
87 shift ;;
88 --no-restore-files)
89 dorestore=
90 shift ;;
91 --restore-files)
92 dorestore=y
93 shift ;;
Milton Miller4015b7a2016-05-23 17:41:34 -050094 --no-flash)
95 doflash=
96 shift ;;
Milton D. Miller II7141eb02016-02-29 21:44:22 -060097 --copy-files)
98 toram=y
99 shift ;;
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600100 *)
Milton Millerd16a7b02016-06-15 18:47:38 -0500101 echoerr "Unknown option $1"
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600102 exit 1 ;;
103 esac
104done
105
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600106if test "x$dosave" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600107then
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600108 if test ! -d $upper -a -n "$rwfs"
109 then
110 mkdir -p $rwdir
111 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rorwopts
112 mounted=$rwdir
113 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600114
115 while read f
116 do
Milton Millerba65b7b2016-02-05 12:07:53 -0600117 if ! test -e $upper/$f
Milton D. Miller II0e775142016-01-20 14:57:54 -0600118 then
119 continue
120 fi
Milton Millerba65b7b2016-02-05 12:07:53 -0600121 d="$save/$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600122 mkdir -p "${d%/*}"
Milton Millerba65b7b2016-02-05 12:07:53 -0600123 cp -rp $upper/$f "${d%/*}/"
Milton D. Miller IIee91f8d2016-02-29 11:39:11 -0600124 done < $whitelist
Milton D. Miller II0e775142016-01-20 14:57:54 -0600125
Milton D. Miller IIa987d622016-02-29 21:34:43 -0600126 if test -n "$mounted"
127 then
128 umount $mounted
129 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600130fi
131
Milton Miller36d336c2016-02-05 11:19:58 -0600132for f in $image*
Milton D. Miller II0e775142016-01-20 14:57:54 -0600133do
Milton Miller36d336c2016-02-05 11:19:58 -0600134 m=$(findmtd ${f#$image})
Milton D. Miller II0e775142016-01-20 14:57:54 -0600135 if test -z "$m"
136 then
Milton Millerd16a7b02016-06-15 18:47:38 -0500137 echoerr "Unable to find mtd partiton for ${f##*/}."
Milton Millere1cbebe2016-05-23 16:00:19 -0500138 exit 1
Milton D. Miller II0e775142016-01-20 14:57:54 -0600139 fi
140done
141
Milton Miller4015b7a2016-05-23 17:41:34 -0500142if test -n "$doflash"
143then
144 for f in $image*
145 do
146 if test ! -s $f
147 then
148 echo "Skipping empty update of ${f#$image}."
149 rm $f
150 continue
151 fi
152 m=$(findmtd ${f#$image})
153 echo "Updating ${f#$image}..."
154 flashcp -v $f /dev/$m && rm $f
155 done
156fi
Milton D. Miller II0e775142016-01-20 14:57:54 -0600157
Milton Millera67cc442016-05-23 18:35:21 -0500158if test -d $save -a "x$toram" = xy
Milton D. Miller II7141eb02016-02-29 21:44:22 -0600159then
160 mkdir -p $upper
161 cp -rp $save/. $upper/
162fi
163
Milton D. Miller IIecf68d52016-02-29 23:11:27 -0600164if test -d $save -a "x$dorestore" = xy
Milton D. Miller II0e775142016-01-20 14:57:54 -0600165then
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600166 odir=$rwdir
167 rwdir=/run/rw
168 upper=$rwdir${upper#$odir}
169
170 mkdir -p $rwdir
Andrew Jeffery25a50222016-02-23 23:47:23 +1030171 mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts
Milton D. Miller IIba2b7c92016-02-28 18:17:02 -0600172 mkdir -p $upper
Milton Millerba65b7b2016-02-05 12:07:53 -0600173 cp -rp $save/. $upper/
174 umount $rwdir
Milton D. Miller IIfacb7182016-02-28 16:20:19 -0600175 rmdir $rwdir
Milton D. Miller II0e775142016-01-20 14:57:54 -0600176fi
177
Milton D. Miller IId0b0c6a2016-02-28 16:32:14 -0600178if test "x$doclean" = xy
179then
180 rm -rf $save
181fi
182
Milton Millerdbacf102016-02-05 13:56:18 -0600183exit