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/class/ubi/"
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 cmtdnum
45 mtdnum
="$( find_mtd_index $1 )"
46 [ ! "$mtdnum" ] && return 1
47 for ubidevdir
in /sys
/class
/ubi
/ubi
*; do
48 [ ! -e "$ubidevdir/mtd_num" ] && continue
49 cmtdnum
="$( cat $ubidevdir/mtd_num )"
50 if [ "$mtdnum" = "$cmtdnum" ]; then
51 ubidev
=$
( basename $ubidevdir )
52 ubi_mknod
"$ubidevdir"
59 nand_get_magic_long
() {
60 (${3}cat "$1" |
dd bs
=4 "skip=${2:-0}" count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
63 get_magic_long_tar
() {
64 (tar xO
${3}f
"$1" "$2" |
dd bs
=4 count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
68 identify_magic_long $
(nand_get_magic_long
"$@")
72 identify_magic_long $
(get_magic_long_tar
"$@")
76 if [ "$(identify "$1")" = gzip ]; then echo -n z
; fi
79 nand_restore_config
() {
80 local ubidev
=$
( nand_find_ubi
"${CI_ROOT_UBIPART:-$CI_UBIPART}" )
81 local ubivol
="$( nand_find_volume $ubidev rootfs_data )"
82 if [ ! "$ubivol" ]; then
83 ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
84 if [ ! "$ubivol" ]; then
85 echo "cannot find ubifs data volume"
90 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
91 echo "cannot mount ubifs volume $ubivol"
95 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
96 if umount
/tmp
/new_root
; then
97 echo "configuration saved"
104 echo "could not save configuration to ubifs volume $ubivol"
109 nand_remove_ubiblock
() {
112 local ubiblk
="ubiblock${ubivol:3}"
113 if [ -e "/dev/$ubiblk" ]; then
114 umount
"/dev/$ubiblk" && echo "unmounted /dev/$ubiblk" ||
:
115 if ! ubiblock
-r "/dev/$ubivol"; then
116 echo "cannot remove $ubiblk"
124 local has_env
="${2:-0}"
126 local mtdnum
="$( find_mtd_index "$ubipart" )"
127 if [ ! "$mtdnum" ]; then
128 >&2 echo "cannot find ubi mtd partition $ubipart"
132 local ubidev
="$( nand_find_ubi "$ubipart" )"
133 if [ ! "$ubidev" ]; then
134 >&2 ubiattach
-m "$mtdnum"
135 ubidev
="$( nand_find_ubi "$ubipart" )"
137 if [ ! "$ubidev" ]; then
138 >&2 ubiformat
/dev
/mtd
$mtdnum -y
139 >&2 ubiattach
-m "$mtdnum"
140 ubidev
="$( nand_find_ubi "$ubipart" )"
142 if [ ! "$ubidev" ]; then
143 >&2 echo "cannot attach ubi mtd partition $ubipart"
147 if [ "$has_env" -gt 0 ]; then
148 >&2 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
149 >&2 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
161 local mtdnum
="$( find_mtd_index "$ubipart" )"
162 if [ ! "$mtdnum" ]; then
163 echo "cannot find ubi mtd partition $ubipart"
167 local ubidev
="$( nand_find_ubi "$ubipart" )"
168 if [ "$ubidev" ]; then
169 for ubivol
in $
(find /dev
-name "${ubidev}_*" -maxdepth 1 |
sort); do
171 nand_remove_ubiblock
"$ubivol" ||
:
172 umount
"/dev/$ubivol" && echo "unmounted /dev/$ubivol" ||
:
174 if ! ubidetach
-m "$mtdnum"; then
175 echo "cannot detach ubi mtd partition $ubipart"
181 nand_upgrade_prepare_ubi
() {
182 local rootfs_length
="$1"
183 local rootfs_type
="$2"
184 local rootfs_data_max
="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
185 [ -n "$rootfs_data_max" ] && rootfs_data_max
=$
((rootfs_data_max
))
187 local kernel_length
="$3"
188 local has_env
="${4:-0}"
192 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
194 if [ -n "$CI_KERN_UBIPART" -a -n "$CI_ROOT_UBIPART" ]; then
195 kern_ubidev
="$( nand_attach_ubi "$CI_KERN_UBIPART" "$has_env" )"
196 [ -n "$kern_ubidev" ] ||
return 1
197 root_ubidev
="$( nand_attach_ubi "$CI_ROOT_UBIPART" )"
198 [ -n "$root_ubidev" ] ||
return 1
200 kern_ubidev
="$( nand_attach_ubi "$CI_UBIPART" "$has_env" )"
201 [ -n "$kern_ubidev" ] ||
return 1
202 root_ubidev
="$kern_ubidev"
205 local kern_ubivol
="$( nand_find_volume $kern_ubidev "$CI_KERNPART" )"
206 local root_ubivol
="$( nand_find_volume $root_ubidev "$CI_ROOTPART" )"
207 local data_ubivol
="$( nand_find_volume $root_ubidev rootfs_data )"
208 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol
=
211 [ "$kern_ubivol" ] && { nand_remove_ubiblock
$kern_ubivol ||
return 1; }
212 [ "$root_ubivol" ] && { nand_remove_ubiblock
$root_ubivol ||
return 1; }
213 [ "$data_ubivol" ] && { nand_remove_ubiblock
$data_ubivol ||
return 1; }
216 [ "$kern_ubivol" ] && ubirmvol
/dev
/$kern_ubidev -N "$CI_KERNPART" ||
:
217 [ "$root_ubivol" ] && ubirmvol
/dev
/$root_ubidev -N "$CI_ROOTPART" ||
:
218 [ "$data_ubivol" ] && ubirmvol
/dev
/$root_ubidev -N rootfs_data ||
:
221 if [ -n "$kernel_length" ]; then
222 if ! ubimkvol
/dev
/$kern_ubidev -N "$CI_KERNPART" -s $kernel_length; then
223 echo "cannot create kernel volume"
229 if [ -n "$rootfs_length" ]; then
230 local rootfs_size_param
231 if [ "$rootfs_type" = "ubifs" ]; then
232 rootfs_size_param
="-m"
234 rootfs_size_param
="-s $rootfs_length"
236 if ! ubimkvol
/dev
/$root_ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
237 echo "cannot create rootfs volume"
242 # create rootfs_data vol for non-ubifs rootfs
243 if [ "$rootfs_type" != "ubifs" ]; then
244 local rootfs_data_size_param
="-m"
245 if [ -n "$rootfs_data_max" ]; then
246 rootfs_data_size_param
="-s $rootfs_data_max"
248 if ! ubimkvol
/dev
/$root_ubidev -N rootfs_data
$rootfs_data_size_param; then
249 if ! ubimkvol
/dev
/$root_ubidev -N rootfs_data
-m; then
250 echo "cannot initialize rootfs_data volume"
259 # Write the UBI image to MTD ubi partition
260 nand_upgrade_ubinized
() {
264 local ubi_length
=$
( (${gz}cat "$ubi_file" |
wc -c) 2> /dev
/null
)
266 nand_detach_ubi
"$CI_UBIPART" ||
return 1
268 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
269 ${gz}cat "$ubi_file" | ubiformat
"/dev/mtd$mtdnum" -S "$ubi_length" -y -f - && ubiattach
-m "$mtdnum"
272 # Write the UBIFS image to UBI rootfs volume
273 nand_upgrade_ubifs
() {
274 local ubifs_file
="$1"
277 local ubifs_length
=$
( (${gz}cat "$ubifs_file" |
wc -c) 2> /dev
/null
)
279 nand_upgrade_prepare_ubi
"$ubifs_length" "ubifs" "" "" ||
return 1
281 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
282 local root_ubivol
="$(nand_find_volume $ubidev "$CI_ROOTPART")"
283 ${gz}cat "$ubifs_file" | ubiupdatevol
/dev
/$root_ubivol -s "$ubifs_length" -
286 # Write the FIT image to UBI kernel volume
291 local fit_length
=$
( (${gz}cat "$fit_file" |
wc -c) 2> /dev
/null
)
293 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1" ||
return 1
295 local fit_ubidev
="$(nand_find_ubi "$CI_UBIPART")"
296 local fit_ubivol
="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
297 ${gz}cat "$fit_file" | ubiupdatevol
/dev
/$fit_ubivol -s "$fit_length" -
300 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
305 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
306 local board_dir
="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
307 board_dir
="${board_dir%/}"
309 local kernel_mtd kernel_length
310 if [ "$CI_KERNPART" != "none" ]; then
311 kernel_mtd
="$(find_mtd_index "$CI_KERNPART")"
312 kernel_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/kernel" |
wc -c) 2> /dev
/null
)
313 [ "$kernel_length" = 0 ] && kernel_length
=
315 local rootfs_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/root" |
wc -c) 2> /dev
/null
)
316 [ "$rootfs_length" = 0 ] && rootfs_length
=
318 [ "$rootfs_length" ] && rootfs_type
="$(identify_tar "$tar_file" "$board_dir/root
" "$gz")"
320 local ubi_kernel_length
321 if [ "$kernel_length" ]; then
322 if [ "$kernel_mtd" ]; then
323 # On some devices, the raw kernel and ubi partitions overlap.
324 # These devices brick if the kernel partition is erased.
325 # Hence only invalidate kernel for now.
326 dd if=/dev
/zero bs
=4096 count
=1 2> /dev
/null | \
327 mtd
write - "$CI_KERNPART"
329 ubi_kernel_length
="$kernel_length"
333 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" ||
return 1
335 if [ "$rootfs_length" ]; then
336 local ubidev
="$( nand_find_ubi "${CI_ROOT_UBIPART:-$CI_UBIPART}" )"
337 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
338 tar xO
${gz}f
"$tar_file" "$board_dir/root" | \
339 ubiupdatevol
/dev
/$root_ubivol -s "$rootfs_length" -
341 if [ "$kernel_length" ]; then
342 if [ "$kernel_mtd" ]; then
343 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
344 mtd
write - "$CI_KERNPART"
346 local ubidev
="$( nand_find_ubi "${CI_KERN_UBIPART:-$CI_UBIPART}" )"
347 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
348 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
349 ubiupdatevol
/dev
/$kern_ubivol -s "$kernel_length" -
356 nand_verify_if_gzip_file
() {
360 if [ "$gz" = z
]; then
361 echo "verifying compressed sysupgrade file integrity"
362 if ! gzip -t "$file"; then
363 echo "corrupted compressed sysupgrade file"
369 nand_verify_tar_file
() {
373 echo "verifying sysupgrade tar file integrity"
374 if ! tar xO
${gz}f
"$file" > /dev
/null
; then
375 echo "corrupted sysupgrade tar file"
380 nand_do_flash_file
() {
383 local gz
="$(identify_if_gzip "$file")"
384 local file_type
="$(identify "$file" "" "$gz")"
386 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
=rootfs
390 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
391 nand_upgrade_fit
"$file" "$gz"
394 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
395 nand_upgrade_ubinized
"$file" "$gz"
398 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
399 nand_upgrade_ubifs
"$file" "$gz"
402 nand_verify_tar_file
"$file" "$gz" ||
return 1
403 nand_upgrade_tar
"$file" "$gz"
408 nand_do_restore_config
() {
409 local conf_tar
="/tmp/sysupgrade.tgz"
410 [ ! -f "$conf_tar" ] || nand_restore_config
"$conf_tar"
413 # Recognize type of passed file and start the upgrade process
418 nand_do_flash_file
"$file" && nand_do_upgrade_success
419 nand_do_upgrade_failed
422 nand_do_upgrade_success
() {
423 if nand_do_restore_config
&& sync
; then
424 echo "sysupgrade successful"
428 nand_do_upgrade_failed
431 nand_do_upgrade_failed
() {
433 echo "sysupgrade failed"
434 # Should we reboot or bring up some failsafe mode instead?
439 # Check if passed file is a valid one for NAND sysupgrade.
440 # Currently it accepts 4 types of files:
441 # 1) UBI: a ubinized image containing required UBI volumes.
442 # 2) UBIFS: a UBIFS rootfs volume image.
443 # 3) FIT: a FIT image containing kernel and rootfs.
444 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
445 # a non-empty "CONTROL" file and required partition and/or volume images.
447 # You usually want to call this function in platform_check_image.
449 # $(1): board name, used in case of passing TAR file
450 # $(2): file to be checked
451 nand_do_platform_check
() {
452 local board_name
="$1"
455 local gz
="$(identify_if_gzip "$file")"
456 local file_type
="$(identify "$file" "" "$gz")"
457 local control_length
=$
( (tar xO
${gz}f
"$file" "sysupgrade-${board_name//,/_}/CONTROL" |
wc -c) 2> /dev
/null
)
459 if [ "$control_length" = 0 ]; then
460 control_length
=$
( (tar xO
${gz}f
"$file" "sysupgrade-${board_name//_/,}/CONTROL" |
wc -c) 2> /dev
/null
)
463 if [ "$control_length" != 0 ]; then
464 nand_verify_tar_file
"$file" "$gz" ||
return 1
466 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
467 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
468 echo "invalid sysupgrade file"