a8e3cab0b8b112d052f27c08c4e25791cd8ba57b
1 # Copyright (C) 2014 OpenWrt.org
6 # 'kernel' partition or UBI volume on NAND contains the kernel
7 CI_KERNPART
="${CI_KERNPART:-kernel}"
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}"
14 # 'rootfs' UBI volume on NAND contains the rootfs
15 CI_ROOTPART
="${CI_ROOTPART:-rootfs}"
19 local dev
="/dev/$(basename $dir)"
21 [ -e "$dev" ] && return 0
23 local devid
="$(cat $dir/dev)"
24 local major
="${devid%%:*}"
25 local minor
="${devid##*:}"
26 mknod
"$dev" c
$major $minor
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
37 ubi_mknod
"$ubivoldir"
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"
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
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
97 identify_magic $
(nand_get_magic_long
"$@")
101 identify_magic $
(get_magic_long_tar
"$@")
105 if [ "$(identify "$1")" = gzip ]; then echo -n z
; fi
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"
119 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
120 echo "cannot mount ubifs volume $ubivol"
124 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
125 if umount
/tmp
/new_root
; then
126 echo "configuration saved"
133 echo "could not save configuration to ubifs volume $ubivol"
138 nand_remove_ubiblock
() {
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"
153 local has_env
="${2:-0}"
155 local mtdnum
="$( find_mtd_index "$ubipart" )"
156 if [ ! "$mtdnum" ]; then
157 >&2 echo "cannot find ubi mtd partition $ubipart"
161 local ubidev
="$( nand_find_ubi "$ubipart" )"
162 if [ ! "$ubidev" ]; then
163 >&2 ubiattach
-m "$mtdnum"
164 ubidev
="$( nand_find_ubi "$ubipart" )"
166 if [ ! "$ubidev" ]; then
167 >&2 ubiformat
/dev
/mtd
$mtdnum -y
168 >&2 ubiattach
-m "$mtdnum"
169 ubidev
="$( nand_find_ubi "$ubipart" )"
171 if [ ! "$ubidev" ]; then
172 >&2 echo "cannot attach ubi mtd partition $ubipart"
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
190 local mtdnum
="$( find_mtd_index "$ubipart" )"
191 if [ ! "$mtdnum" ]; then
192 echo "cannot find ubi mtd partition $ubipart"
196 local ubidev
="$( nand_find_ubi "$ubipart" )"
197 if [ "$ubidev" ]; then
198 for ubivol
in $
(find /dev
-name "${ubidev}_*" -maxdepth 1 |
sort); do
200 nand_remove_ubiblock
"$ubivol" ||
:
201 umount
"/dev/$ubivol" && echo "unmounted /dev/$ubivol" ||
:
203 if ! ubidetach
-m "$mtdnum"; then
204 echo "cannot detach ubi mtd partition $ubipart"
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
))
216 local kernel_length
="$3"
217 local has_env
="${4:-0}"
221 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
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
229 kern_ubidev
="$( nand_attach_ubi "$CI_UBIPART" "$has_env" )"
230 [ -n "$kern_ubidev" ] ||
return 1
231 root_ubidev
="$kern_ubidev"
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
=
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; }
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 ||
:
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"
258 if [ -n "$rootfs_length" ]; then
259 local rootfs_size_param
260 if [ "$rootfs_type" = "ubifs" ]; then
261 rootfs_size_param
="-m"
263 rootfs_size_param
="-s $rootfs_length"
265 if ! ubimkvol
/dev
/$root_ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
266 echo "cannot create rootfs volume"
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"
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"
288 # Write the UBI image to MTD ubi partition
289 nand_upgrade_ubinized
() {
293 nand_detach_ubi
"$CI_UBIPART" ||
return 1
295 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
296 ${gz}cat "$ubi_file" | ubiformat
"/dev/mtd$mtdnum" -y -f - && ubiattach
-m "$mtdnum"
299 # Write the UBIFS image to UBI rootfs volume
300 nand_upgrade_ubifs
() {
301 local ubifs_file
="$1"
304 local ubifs_length
=$
( (${gz}cat "$ubifs_file" |
wc -c) 2> /dev
/null
)
306 nand_upgrade_prepare_ubi
"$ubifs_length" "ubifs" "" "" ||
return 1
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" -
313 # Write the FIT image to UBI kernel volume
318 local fit_length
=$
( (${gz}cat "$fit_file" |
wc -c) 2> /dev
/null
)
320 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1" ||
return 1
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" -
327 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
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%/}"
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
=
342 local rootfs_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/root" |
wc -c) 2> /dev
/null
)
343 [ "$rootfs_length" = 0 ] && rootfs_length
=
345 [ "$rootfs_length" ] && rootfs_type
="$(identify_tar "$tar_file" "$board_dir/root
" "$gz")"
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"
356 ubi_kernel_length
="$kernel_length"
360 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" ||
return 1
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" -
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"
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" -
383 nand_verify_if_gzip_file
() {
387 if [ "$gz" = z
]; then
388 echo "verifying compressed sysupgrade file integrity"
389 if ! gzip -t "$file"; then
390 echo "corrupted compressed sysupgrade file"
396 nand_verify_tar_file
() {
400 echo "verifying sysupgrade tar file integrity"
401 if ! tar xO
${gz}f
"$file" > /dev
/null
; then
402 echo "corrupted sysupgrade tar file"
407 nand_do_flash_file
() {
410 local gz
="$(identify_if_gzip "$file")"
411 local file_type
="$(identify "$file" "" "$gz")"
413 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
=rootfs
417 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
418 nand_upgrade_fit
"$file" "$gz"
421 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
422 nand_upgrade_ubinized
"$file" "$gz"
425 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
426 nand_upgrade_ubifs
"$file" "$gz"
429 nand_verify_tar_file
"$file" "$gz" ||
return 1
430 nand_upgrade_tar
"$file" "$gz"
435 nand_do_restore_config
() {
436 local conf_tar
="/tmp/sysupgrade.tgz"
437 [ ! -f "$conf_tar" ] || nand_restore_config
"$conf_tar"
440 # Recognize type of passed file and start the upgrade process
445 nand_do_flash_file
"$file" && nand_do_upgrade_success
446 nand_do_upgrade_failed
449 nand_do_upgrade_success
() {
450 if nand_do_restore_config
&& sync
; then
451 echo "sysupgrade successful"
455 nand_do_upgrade_failed
458 nand_do_upgrade_failed
() {
460 echo "sysupgrade failed"
461 # Should we reboot or bring up some failsafe mode instead?
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.
474 # You usually want to call this function in platform_check_image.
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"
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
)
486 if [ "$control_length" != 0 ]; then
487 nand_verify_tar_file
"$file" "$gz" ||
return 1
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"