a8e3cab0b8b112d052f27c08c4e25791cd8ba57b
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / nand.sh
1 # Copyright (C) 2014 OpenWrt.org
2 #
3
4 . /lib/functions.sh
5
6 # 'kernel' partition or UBI volume on NAND contains the kernel
7 CI_KERNPART="${CI_KERNPART:-kernel}"
8
9 # 'ubi' partition on NAND contains UBI
10 # There are also CI_KERN_UBIPART and CI_ROOT_UBIPART if kernel
11 # and rootfs are on separated UBIs.
12 CI_UBIPART="${CI_UBIPART:-ubi}"
13
14 # 'rootfs' UBI volume on NAND contains the rootfs
15 CI_ROOTPART="${CI_ROOTPART:-rootfs}"
16
17 ubi_mknod() {
18 local dir="$1"
19 local dev="/dev/$(basename $dir)"
20
21 [ -e "$dev" ] && return 0
22
23 local devid="$(cat $dir/dev)"
24 local major="${devid%%:*}"
25 local minor="${devid##*:}"
26 mknod "$dev" c $major $minor
27 }
28
29 nand_find_volume() {
30 local ubidevdir ubivoldir
31 ubidevdir="/sys/devices/virtual/ubi/$1"
32 [ ! -d "$ubidevdir" ] && return 1
33 for ubivoldir in $ubidevdir/${1}_*; do
34 [ ! -d "$ubivoldir" ] && continue
35 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
36 basename $ubivoldir
37 ubi_mknod "$ubivoldir"
38 return 0
39 fi
40 done
41 }
42
43 nand_find_ubi() {
44 local ubidevdir ubidev mtdnum
45 mtdnum="$( find_mtd_index $1 )"
46 [ ! "$mtdnum" ] && return 1
47 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
48 [ ! -d "$ubidevdir" ] && continue
49 cmtdnum="$( cat $ubidevdir/mtd_num )"
50 [ ! "$mtdnum" ] && continue
51 if [ "$mtdnum" = "$cmtdnum" ]; then
52 ubidev=$( basename $ubidevdir )
53 ubi_mknod "$ubidevdir"
54 echo $ubidev
55 return 0
56 fi
57 done
58 }
59
60 nand_get_magic_long() {
61 (${3}cat "$1" | dd bs=4 "skip=${2:-0}" count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
62 }
63
64 get_magic_long_tar() {
65 (tar xO${3}f "$1" "$2" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
66 }
67
68 identify_magic() {
69 local magic=$1
70 case "$magic" in
71 "55424923")
72 echo "ubi"
73 ;;
74 "31181006")
75 echo "ubifs"
76 ;;
77 "68737173")
78 echo "squashfs"
79 ;;
80 "d00dfeed")
81 echo "fit"
82 ;;
83 "4349"*)
84 echo "combined"
85 ;;
86 "1f8b"*)
87 echo "gzip"
88 ;;
89 *)
90 echo "unknown $magic"
91 ;;
92 esac
93 }
94
95
96 identify() {
97 identify_magic $(nand_get_magic_long "$@")
98 }
99
100 identify_tar() {
101 identify_magic $(get_magic_long_tar "$@")
102 }
103
104 identify_if_gzip() {
105 if [ "$(identify "$1")" = gzip ]; then echo -n z; fi
106 }
107
108 nand_restore_config() {
109 local ubidev=$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )
110 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
111 if [ ! "$ubivol" ]; then
112 ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
113 if [ ! "$ubivol" ]; then
114 echo "cannot find ubifs data volume"
115 return 1
116 fi
117 fi
118 mkdir /tmp/new_root
119 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
120 echo "cannot mount ubifs volume $ubivol"
121 rmdir /tmp/new_root
122 return 1
123 fi
124 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
125 if umount /tmp/new_root; then
126 echo "configuration saved"
127 rmdir /tmp/new_root
128 return 0
129 fi
130 else
131 umount /tmp/new_root
132 fi
133 echo "could not save configuration to ubifs volume $ubivol"
134 rmdir /tmp/new_root
135 return 1
136 }
137
138 nand_remove_ubiblock() {
139 local ubivol="$1"
140
141 local ubiblk="ubiblock${ubivol:3}"
142 if [ -e "/dev/$ubiblk" ]; then
143 umount "/dev/$ubiblk" && echo "unmounted /dev/$ubiblk" || :
144 if ! ubiblock -r "/dev/$ubivol"; then
145 echo "cannot remove $ubiblk"
146 return 1
147 fi
148 fi
149 }
150
151 nand_attach_ubi() {
152 local ubipart="$1"
153 local has_env="${2:-0}"
154
155 local mtdnum="$( find_mtd_index "$ubipart" )"
156 if [ ! "$mtdnum" ]; then
157 >&2 echo "cannot find ubi mtd partition $ubipart"
158 return 1
159 fi
160
161 local ubidev="$( nand_find_ubi "$ubipart" )"
162 if [ ! "$ubidev" ]; then
163 >&2 ubiattach -m "$mtdnum"
164 ubidev="$( nand_find_ubi "$ubipart" )"
165
166 if [ ! "$ubidev" ]; then
167 >&2 ubiformat /dev/mtd$mtdnum -y
168 >&2 ubiattach -m "$mtdnum"
169 ubidev="$( nand_find_ubi "$ubipart" )"
170
171 if [ ! "$ubidev" ]; then
172 >&2 echo "cannot attach ubi mtd partition $ubipart"
173 return 1
174 fi
175
176 if [ "$has_env" -gt 0 ]; then
177 >&2 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
178 >&2 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
179 fi
180 fi
181 fi
182
183 echo "$ubidev"
184 return 0
185 }
186
187 nand_detach_ubi() {
188 local ubipart="$1"
189
190 local mtdnum="$( find_mtd_index "$ubipart" )"
191 if [ ! "$mtdnum" ]; then
192 echo "cannot find ubi mtd partition $ubipart"
193 return 1
194 fi
195
196 local ubidev="$( nand_find_ubi "$ubipart" )"
197 if [ "$ubidev" ]; then
198 for ubivol in $(find /dev -name "${ubidev}_*" -maxdepth 1 | sort); do
199 ubivol="${ubivol:5}"
200 nand_remove_ubiblock "$ubivol" || :
201 umount "/dev/$ubivol" && echo "unmounted /dev/$ubivol" || :
202 done
203 if ! ubidetach -m "$mtdnum"; then
204 echo "cannot detach ubi mtd partition $ubipart"
205 return 1
206 fi
207 fi
208 }
209
210 nand_upgrade_prepare_ubi() {
211 local rootfs_length="$1"
212 local rootfs_type="$2"
213 local rootfs_data_max="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
214 [ -n "$rootfs_data_max" ] && rootfs_data_max=$((rootfs_data_max))
215
216 local kernel_length="$3"
217 local has_env="${4:-0}"
218 local kern_ubidev
219 local root_ubidev
220
221 [ -n "$rootfs_length" -o -n "$kernel_length" ] || return 1
222
223 if [ -n "$CI_KERN_UBIPART" -a -n "$CI_ROOT_UBIPART" ]; then
224 kern_ubidev="$( nand_attach_ubi "$CI_KERN_UBIPART" "$has_env" )"
225 [ -n "$kern_ubidev" ] || return 1
226 root_ubidev="$( nand_attach_ubi "$CI_ROOT_UBIPART" )"
227 [ -n "$root_ubidev" ] || return 1
228 else
229 kern_ubidev="$( nand_attach_ubi "$CI_UBIPART" "$has_env" )"
230 [ -n "$kern_ubidev" ] || return 1
231 root_ubidev="$kern_ubidev"
232 fi
233
234 local kern_ubivol="$( nand_find_volume $kern_ubidev "$CI_KERNPART" )"
235 local root_ubivol="$( nand_find_volume $root_ubidev "$CI_ROOTPART" )"
236 local data_ubivol="$( nand_find_volume $root_ubidev rootfs_data )"
237 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol=
238
239 # remove ubiblocks
240 [ "$kern_ubivol" ] && { nand_remove_ubiblock $kern_ubivol || return 1; }
241 [ "$root_ubivol" ] && { nand_remove_ubiblock $root_ubivol || return 1; }
242 [ "$data_ubivol" ] && { nand_remove_ubiblock $data_ubivol || return 1; }
243
244 # kill volumes
245 [ "$kern_ubivol" ] && ubirmvol /dev/$kern_ubidev -N "$CI_KERNPART" || :
246 [ "$root_ubivol" ] && ubirmvol /dev/$root_ubidev -N "$CI_ROOTPART" || :
247 [ "$data_ubivol" ] && ubirmvol /dev/$root_ubidev -N rootfs_data || :
248
249 # create kernel vol
250 if [ -n "$kernel_length" ]; then
251 if ! ubimkvol /dev/$kern_ubidev -N "$CI_KERNPART" -s $kernel_length; then
252 echo "cannot create kernel volume"
253 return 1;
254 fi
255 fi
256
257 # create rootfs vol
258 if [ -n "$rootfs_length" ]; then
259 local rootfs_size_param
260 if [ "$rootfs_type" = "ubifs" ]; then
261 rootfs_size_param="-m"
262 else
263 rootfs_size_param="-s $rootfs_length"
264 fi
265 if ! ubimkvol /dev/$root_ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
266 echo "cannot create rootfs volume"
267 return 1;
268 fi
269 fi
270
271 # create rootfs_data vol for non-ubifs rootfs
272 if [ "$rootfs_type" != "ubifs" ]; then
273 local rootfs_data_size_param="-m"
274 if [ -n "$rootfs_data_max" ]; then
275 rootfs_data_size_param="-s $rootfs_data_max"
276 fi
277 if ! ubimkvol /dev/$root_ubidev -N rootfs_data $rootfs_data_size_param; then
278 if ! ubimkvol /dev/$root_ubidev -N rootfs_data -m; then
279 echo "cannot initialize rootfs_data volume"
280 return 1
281 fi
282 fi
283 fi
284
285 return 0
286 }
287
288 # Write the UBI image to MTD ubi partition
289 nand_upgrade_ubinized() {
290 local ubi_file="$1"
291 local gz="$2"
292
293 nand_detach_ubi "$CI_UBIPART" || return 1
294
295 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
296 ${gz}cat "$ubi_file" | ubiformat "/dev/mtd$mtdnum" -y -f - && ubiattach -m "$mtdnum"
297 }
298
299 # Write the UBIFS image to UBI rootfs volume
300 nand_upgrade_ubifs() {
301 local ubifs_file="$1"
302 local gz="$2"
303
304 local ubifs_length=$( (${gz}cat "$ubifs_file" | wc -c) 2> /dev/null)
305
306 nand_upgrade_prepare_ubi "$ubifs_length" "ubifs" "" "" || return 1
307
308 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
309 local root_ubivol="$(nand_find_volume $ubidev "$CI_ROOTPART")"
310 ${gz}cat "$ubifs_file" | ubiupdatevol /dev/$root_ubivol -s "$ubifs_length" -
311 }
312
313 # Write the FIT image to UBI kernel volume
314 nand_upgrade_fit() {
315 local fit_file="$1"
316 local gz="$2"
317
318 local fit_length=$( (${gz}cat "$fit_file" | wc -c) 2> /dev/null)
319
320 nand_upgrade_prepare_ubi "" "" "$fit_length" "1" || return 1
321
322 local fit_ubidev="$(nand_find_ubi "$CI_UBIPART")"
323 local fit_ubivol="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
324 ${gz}cat "$fit_file" | ubiupdatevol /dev/$fit_ubivol -s "$fit_length" -
325 }
326
327 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
328 nand_upgrade_tar() {
329 local tar_file="$1"
330 local gz="$2"
331
332 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
333 local board_dir="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
334 board_dir="${board_dir%/}"
335
336 local kernel_mtd kernel_length
337 if [ "$CI_KERNPART" != "none" ]; then
338 kernel_mtd="$(find_mtd_index "$CI_KERNPART")"
339 kernel_length=$( (tar xO${gz}f "$tar_file" "$board_dir/kernel" | wc -c) 2> /dev/null)
340 [ "$kernel_length" = 0 ] && kernel_length=
341 fi
342 local rootfs_length=$( (tar xO${gz}f "$tar_file" "$board_dir/root" | wc -c) 2> /dev/null)
343 [ "$rootfs_length" = 0 ] && rootfs_length=
344 local rootfs_type
345 [ "$rootfs_length" ] && rootfs_type="$(identify_tar "$tar_file" "$board_dir/root" "$gz")"
346
347 local ubi_kernel_length
348 if [ "$kernel_length" ]; then
349 if [ "$kernel_mtd" ]; then
350 # On some devices, the raw kernel and ubi partitions overlap.
351 # These devices brick if the kernel partition is erased.
352 # Hence only invalidate kernel for now.
353 dd if=/dev/zero bs=4096 count=1 2> /dev/null | \
354 mtd write - "$CI_KERNPART"
355 else
356 ubi_kernel_length="$kernel_length"
357 fi
358 fi
359 local has_env=0
360 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" || return 1
361
362 if [ "$rootfs_length" ]; then
363 local ubidev="$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )"
364 local root_ubivol="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
365 tar xO${gz}f "$tar_file" "$board_dir/root" | \
366 ubiupdatevol /dev/$root_ubivol -s "$rootfs_length" -
367 fi
368 if [ "$kernel_length" ]; then
369 if [ "$kernel_mtd" ]; then
370 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
371 mtd write - "$CI_KERNPART"
372 else
373 local ubidev="$( nand_find_ubi "${CI_KERN_UBIPART:-$CI_UBIPART}" )"
374 local kern_ubivol="$( nand_find_volume $ubidev "$CI_KERNPART" )"
375 tar xO${gz}f "$tar_file" "$board_dir/kernel" | \
376 ubiupdatevol /dev/$kern_ubivol -s "$kernel_length" -
377 fi
378 fi
379
380 return 0
381 }
382
383 nand_verify_if_gzip_file() {
384 local file="$1"
385 local gz="$2"
386
387 if [ "$gz" = z ]; then
388 echo "verifying compressed sysupgrade file integrity"
389 if ! gzip -t "$file"; then
390 echo "corrupted compressed sysupgrade file"
391 return 1
392 fi
393 fi
394 }
395
396 nand_verify_tar_file() {
397 local file="$1"
398 local gz="$2"
399
400 echo "verifying sysupgrade tar file integrity"
401 if ! tar xO${gz}f "$file" > /dev/null; then
402 echo "corrupted sysupgrade tar file"
403 return 1
404 fi
405 }
406
407 nand_do_flash_file() {
408 local file="$1"
409
410 local gz="$(identify_if_gzip "$file")"
411 local file_type="$(identify "$file" "" "$gz")"
412
413 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART=rootfs
414
415 case "$file_type" in
416 "fit")
417 nand_verify_if_gzip_file "$file" "$gz" || return 1
418 nand_upgrade_fit "$file" "$gz"
419 ;;
420 "ubi")
421 nand_verify_if_gzip_file "$file" "$gz" || return 1
422 nand_upgrade_ubinized "$file" "$gz"
423 ;;
424 "ubifs")
425 nand_verify_if_gzip_file "$file" "$gz" || return 1
426 nand_upgrade_ubifs "$file" "$gz"
427 ;;
428 *)
429 nand_verify_tar_file "$file" "$gz" || return 1
430 nand_upgrade_tar "$file" "$gz"
431 ;;
432 esac
433 }
434
435 nand_do_restore_config() {
436 local conf_tar="/tmp/sysupgrade.tgz"
437 [ ! -f "$conf_tar" ] || nand_restore_config "$conf_tar"
438 }
439
440 # Recognize type of passed file and start the upgrade process
441 nand_do_upgrade() {
442 local file="$1"
443
444 sync
445 nand_do_flash_file "$file" && nand_do_upgrade_success
446 nand_do_upgrade_failed
447 }
448
449 nand_do_upgrade_success() {
450 if nand_do_restore_config && sync; then
451 echo "sysupgrade successful"
452 umount -a
453 reboot -f
454 fi
455 nand_do_upgrade_failed
456 }
457
458 nand_do_upgrade_failed() {
459 sync
460 echo "sysupgrade failed"
461 # Should we reboot or bring up some failsafe mode instead?
462 umount -a
463 reboot -f
464 }
465
466 # Check if passed file is a valid one for NAND sysupgrade.
467 # Currently it accepts 4 types of files:
468 # 1) UBI: a ubinized image containing required UBI volumes.
469 # 2) UBIFS: a UBIFS rootfs volume image.
470 # 3) FIT: a FIT image containing kernel and rootfs.
471 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
472 # a non-empty "CONTROL" file and required partition and/or volume images.
473 #
474 # You usually want to call this function in platform_check_image.
475 #
476 # $(1): board name, used in case of passing TAR file
477 # $(2): file to be checked
478 nand_do_platform_check() {
479 local board_name="$1"
480 local file="$2"
481
482 local gz="$(identify_if_gzip "$file")"
483 local file_type="$(identify "$file" "" "$gz")"
484 local control_length=$( (tar xO${gz}f "$file" "sysupgrade-$board_name/CONTROL" | wc -c) 2> /dev/null)
485
486 if [ "$control_length" != 0 ]; then
487 nand_verify_tar_file "$file" "$gz" || return 1
488 else
489 nand_verify_if_gzip_file "$file" "$gz" || return 1
490 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
491 echo "invalid sysupgrade file"
492 return 1
493 fi
494 fi
495
496 return 0
497 }