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