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 | |
26 | if grep mtd /proc/mounts | ||||
27 | then | ||||
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 28 | echoerr "A mtd device is mounted." |
Milton Miller | e1cbebe | 2016-05-23 16:00:19 -0500 | [diff] [blame] | 29 | exit 1 |
30 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 31 | |
32 | findmtd() { | ||||
33 | m=$(grep -xl "$1" /sys/class/mtd/*/name) | ||||
34 | m=${m%/name} | ||||
35 | m=${m##*/} | ||||
36 | echo $m | ||||
37 | } | ||||
38 | |||||
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 39 | blkid_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 | |||||
48 | probe_fs_type() { | ||||
49 | fst=$(blkid_fs_type $1) | ||||
50 | echo ${fst:=jffs2} | ||||
51 | } | ||||
52 | |||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 53 | rwfs=$(findmtd rwfs) |
54 | |||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 55 | rwdev=/dev/mtdblock${rwfs#mtd} |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 56 | rwopts=rw |
57 | rorwopts=ro${rwopts#rw} | ||||
58 | |||||
Milton D. Miller II | c3697de | 2016-02-28 16:07:46 -0600 | [diff] [blame] | 59 | rwdir=/run/initramfs/rw |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 60 | upper=$rwdir/cow |
Milton D. Miller II | fa8316d | 2016-02-29 10:32:58 -0600 | [diff] [blame] | 61 | save=/run/save/${upper##*/} |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 62 | |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 63 | mounted= |
Milton Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame^] | 64 | doflash=y |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 65 | doclean= |
Milton D. Miller II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 66 | dosave=y |
67 | dorestore=y | ||||
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 68 | toram= |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 69 | |
Milton D. Miller II | ee91f8d | 2016-02-29 11:39:11 -0600 | [diff] [blame] | 70 | whitelist=/run/initramfs/whitelist |
71 | image=/run/initramfs/image- | ||||
72 | |||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 73 | while test "$1" != "${1#-}" |
74 | do | ||||
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 II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 82 | --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 Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame^] | 94 | --no-flash) |
95 | doflash= | ||||
96 | shift ;; | ||||
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 97 | --copy-files) |
98 | toram=y | ||||
99 | shift ;; | ||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 100 | *) |
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 101 | echoerr "Unknown option $1" |
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 102 | exit 1 ;; |
103 | esac | ||||
104 | done | ||||
105 | |||||
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 106 | if test "x$dosave" = xy |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 107 | then |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 108 | 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 II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 114 | |
115 | while read f | ||||
116 | do | ||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 117 | if ! test -e $upper/$f |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 118 | then |
119 | continue | ||||
120 | fi | ||||
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 121 | d="$save/$f" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 122 | mkdir -p "${d%/*}" |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 123 | cp -rp $upper/$f "${d%/*}/" |
Milton D. Miller II | ee91f8d | 2016-02-29 11:39:11 -0600 | [diff] [blame] | 124 | done < $whitelist |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 125 | |
Milton D. Miller II | a987d62 | 2016-02-29 21:34:43 -0600 | [diff] [blame] | 126 | if test -n "$mounted" |
127 | then | ||||
128 | umount $mounted | ||||
129 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 130 | fi |
131 | |||||
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 132 | for f in $image* |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 133 | do |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 134 | m=$(findmtd ${f#$image}) |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 135 | if test -z "$m" |
136 | then | ||||
Milton Miller | d16a7b0 | 2016-06-15 18:47:38 -0500 | [diff] [blame] | 137 | echoerr "Unable to find mtd partiton for ${f##*/}." |
Milton Miller | e1cbebe | 2016-05-23 16:00:19 -0500 | [diff] [blame] | 138 | exit 1 |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 139 | fi |
140 | done | ||||
141 | |||||
Milton Miller | 4015b7a | 2016-05-23 17:41:34 -0500 | [diff] [blame^] | 142 | if test -n "$doflash" |
143 | then | ||||
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 | ||||
156 | fi | ||||
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 157 | |
Milton Miller | a67cc44 | 2016-05-23 18:35:21 -0500 | [diff] [blame] | 158 | if test -d $save -a "x$toram" = xy |
Milton D. Miller II | 7141eb0 | 2016-02-29 21:44:22 -0600 | [diff] [blame] | 159 | then |
160 | mkdir -p $upper | ||||
161 | cp -rp $save/. $upper/ | ||||
162 | fi | ||||
163 | |||||
Milton D. Miller II | ecf68d5 | 2016-02-29 23:11:27 -0600 | [diff] [blame] | 164 | if test -d $save -a "x$dorestore" = xy |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 165 | then |
Milton D. Miller II | facb718 | 2016-02-28 16:20:19 -0600 | [diff] [blame] | 166 | odir=$rwdir |
167 | rwdir=/run/rw | ||||
168 | upper=$rwdir${upper#$odir} | ||||
169 | |||||
170 | mkdir -p $rwdir | ||||
Andrew Jeffery | 25a5022 | 2016-02-23 23:47:23 +1030 | [diff] [blame] | 171 | mount $rwdev $rwdir -t $(probe_fs_type $rwdev) -o $rwopts |
Milton D. Miller II | ba2b7c9 | 2016-02-28 18:17:02 -0600 | [diff] [blame] | 172 | mkdir -p $upper |
Milton Miller | ba65b7b | 2016-02-05 12:07:53 -0600 | [diff] [blame] | 173 | cp -rp $save/. $upper/ |
174 | umount $rwdir | ||||
Milton D. Miller II | facb718 | 2016-02-28 16:20:19 -0600 | [diff] [blame] | 175 | rmdir $rwdir |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 176 | fi |
177 | |||||
Milton D. Miller II | d0b0c6a | 2016-02-28 16:32:14 -0600 | [diff] [blame] | 178 | if test "x$doclean" = xy |
179 | then | ||||
180 | rm -rf $save | ||||
181 | fi | ||||
182 | |||||
Milton Miller | dbacf10 | 2016-02-05 13:56:18 -0600 | [diff] [blame] | 183 | exit |