procd: various improvements to nand.sh
[openwrt/openwrt.git] / package / system / procd / files / nand.sh
1 #!/bin/sh
2 # Copyright (C) 2014 OpenWrt.org
3 #
4
5 . /lib/functions.sh
6
7 # 'data' partition on NAND contains UBI
8 CI_UBIPART="ubi"
9
10 nand_find_volume() {
11 local ubidevdir ubivoldir
12 ubidevdir="/sys/devices/virtual/ubi/$1"
13 [ ! -d "$ubidevdir" ] && return 1
14 for ubivoldir in $ubidevdir/${1}_*; do
15 [ ! -d "$ubivoldir" ] && continue
16 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
17 basename $ubivoldir
18 return 0
19 fi
20 done
21 }
22
23 nand_find_ubi() {
24 local ubidevdir ubidev mtdnum
25 mtdnum="$( find_mtd_index $1 )"
26 [ ! "$mtdnum" ] && return 1
27 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
28 [ ! -d "$ubidevdir" ] && continue
29 cmtdnum="$( cat $ubidevdir/mtd_num )"
30 [ ! "$mtdnum" ] && continue
31 if [ "$mtdnum" = "$cmtdnum" ]; then
32 ubidev=$( basename $ubidevdir )
33 echo $ubidev
34 return 0
35 fi
36 done
37 }
38
39 get_magic_long() {
40 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
41 }
42
43 get_magic_long_tar() {
44 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
45 }
46
47 identify_magic() {
48 local magic=$1
49 case "$magic" in
50 "55424923")
51 echo "ubi"
52 ;;
53 "31181006")
54 echo "ubifs"
55 ;;
56 "68737173")
57 echo "squashfs"
58 ;;
59 "d00dfeed")
60 echo "fit"
61 ;;
62 "4349"*)
63 echo "combined"
64 ;;
65 *)
66 echo "unknown $magic"
67 ;;
68 esac
69 }
70
71
72 identify() {
73 identify_magic $(get_magic_long "$1" "${2:-0}")
74 }
75
76 identify_tar() {
77 identify_magic $(get_magic_long_tar "$1" "$2")
78 }
79
80 nand_restore_config() {
81 sync
82 local ubidev=$( nand_find_ubi $CI_UBIPART )
83 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
84 [ ! "$ubivol" ] &&
85 ubivol="$( nand_find_volume $ubidev rootfs )"
86 mkdir /tmp/new_root
87 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
88 echo "mounting ubifs $ubivol failed"
89 rmdir /tmp/new_root
90 return 1
91 fi
92 mv "$1" "/tmp/new_root/sysupgrade.tgz"
93 umount /tmp/new_root
94 sync
95 rmdir /tmp/new_root
96 }
97
98 nand_upgrade_prepare_ubi() {
99 local rootfs_length="$1"
100 local rootfs_type="$2"
101 local has_kernel="${3:-0}"
102 local has_env="${4:-0}"
103
104 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
105 if [ ! "$mtdnum" ]; then
106 echo "cannot find ubi mtd partition $CI_UBIPART"
107 return 1
108 fi
109
110 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
111 if [ ! "$ubidev" ]; then
112 ubiattach -m "$mtdnum"
113 sync
114 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
115 fi
116
117 if [ ! "$ubidev" ]; then
118 ubiformat /dev/mtd$mtdnum -y
119 ubiattach -m "$mtdnum"
120 sync
121 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
122 [ -z "$has_env" ] || {
123 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
124 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
125 }
126 fi
127
128 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
129 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
130 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
131
132 # remove ubiblock device of rootfs
133 local root_ubiblk="ubiblock${root_ubivol:3}"
134 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
135 echo "removing $root_ubiblk"
136 if ! ubiblock -r /dev/$root_ubivol; then
137 echo "cannot remove $root_ubiblk"
138 return 1;
139 fi
140 fi
141
142 # kill volumes
143 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
144 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
145 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
146
147 # update kernel
148 if [ "$has_kernel" = "1" ]; then
149 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
150 echo "cannot create kernel volume"
151 return 1;
152 fi
153 fi
154
155 # update rootfs
156 local root_size_param
157 if [ "$rootfs_type" = "ubifs" ]; then
158 root_size_param="-m"
159 else
160 root_size_param="-s $rootfs_length"
161 fi
162 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
163 echo "cannot create rootfs volume"
164 return 1;
165 fi
166
167 # create rootfs_data for non-ubifs rootfs
168 if [ "$rootfs_type" != "ubifs" ]; then
169 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
170 echo "cannot initialize rootfs_data volume"
171 return 1
172 fi
173 fi
174 sync
175 return 0
176 }
177
178 nand_do_upgrade_success() {
179 local conf_tar="/tmp/sysupgrade.tgz"
180
181 sync
182 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
183 echo "sysupgrade successfull"
184 reboot -f
185 }
186
187 nand_upgrade_ubinized() {
188 local ubi_file="$1"
189 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
190
191 [ ! "$mtdnum" ] && {
192 CI_UBIPART="rootfs"
193 mtdnum="$(find_mtd_index "$CI_UBIPART")"
194 }
195
196 if [ ! "$mtdnum" ]; then
197 echo "cannot find mtd device $CI_UBIPART"
198 reboot -f
199 fi
200
201 local mtddev="/dev/mtd${mtdnum}"
202 ubidetach -p "${mtddev}" || true
203 sync
204 ubiformat "${mtddev}" -y -f "${ubi_file}"
205 ubiattach -p "${mtddev}"
206 nand_do_upgrade_success
207 }
208
209 nand_upgrade_ubifs() {
210 local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
211
212 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
213
214 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
215 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
216 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
217
218 nand_do_upgrade_success
219 }
220
221 nand_upgrade_tar() {
222 local tar_file="$1"
223 local board_name="$(cat /tmp/sysinfo/board_name)"
224 local kernel_mtd="$(find_mtd_index kernel)"
225
226 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
227 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
228
229 local rootfs_type="$(identify_tar "$tar_file" root)"
230
231 local has_kernel=1
232 local has_env=0
233
234 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
235 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - kernel
236 }
237 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
238
239 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
240
241 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
242 [ "$has_kernel" = "1" ] && {
243 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
244 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
245 ubiupdatevol /dev/$kern_ubivol -s $kern_length -
246 }
247
248 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
249 tar xf $tar_file sysupgrade-$board_name/root -O | \
250 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
251
252 nand_do_upgrade_success
253 }
254
255 nand_do_upgrade_stage2() {
256 local file_type=$(identify $1)
257
258 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
259
260 [ "$file_type" == "ubi" ] && nand_upgrade_ubinized $1
261 [ "$file_type" == "ubifs" ] && nand_upgrade_ubifs $1
262 nand_upgrade_tar $1
263 }
264
265 nand_upgrade_stage2() {
266 [ $1 = "nand" ] && {
267 [ -f "$2" ] && {
268 touch /tmp/sysupgrade
269
270 killall -9 telnetd
271 killall -9 dropbear
272 killall -9 ash
273
274 kill_remaining TERM
275 sleep 3
276 kill_remaining KILL
277
278 sleep 1
279
280 if [ -n "$(rootfs_type)" ]; then
281 v "Switching to ramdisk..."
282 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
283 else
284 nand_do_upgrade_stage2 $2
285 fi
286 return 0
287 }
288 echo "Nand upgrade failed"
289 exit 1
290 }
291 }
292
293 nand_upgrade_stage1() {
294 [ -f /tmp/sysupgrade-nand-path ] && {
295 path="$(cat /tmp/sysupgrade-nand-path)"
296 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
297 rm $CONF_TAR
298
299 ubus call system nandupgrade "{\"path\": \"$path\" }"
300 exit 0
301 }
302 }
303 append sysupgrade_pre_upgrade nand_upgrade_stage1
304
305 nand_do_platform_check() {
306 local board_name="$1"
307 local tar_file="$2"
308 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
309 local file_type="$(identify $2)"
310
311 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
312 echo "Invalid sysupgrade file."
313 return 1
314 }
315
316 echo -n $2 > /tmp/sysupgrade-nand-path
317 cp /sbin/upgraded /tmp/
318
319 return 0
320 }