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