258483fbf4520cbd2b696430cee26de6fe0f4a2c
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 CI_UBIPART
="${CI_UBIPART:-ubi}"
12 # 'rootfs' UBI volume on NAND contains the rootfs
13 CI_ROOTPART
="${CI_ROOTPART:-rootfs}"
17 local dev
="/dev/$(basename $dir)"
19 [ -e "$dev" ] && return 0
21 local devid
="$(cat $dir/dev)"
22 local major
="${devid%%:*}"
23 local minor
="${devid##*:}"
24 mknod
"$dev" c
$major $minor
28 local ubidevdir ubivoldir
29 ubidevdir
="/sys/devices/virtual/ubi/$1"
30 [ ! -d "$ubidevdir" ] && return 1
31 for ubivoldir
in $ubidevdir/${1}_
*; do
32 [ ! -d "$ubivoldir" ] && continue
33 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
35 ubi_mknod
"$ubivoldir"
42 local ubidevdir ubidev mtdnum
43 mtdnum
="$( find_mtd_index $1 )"
44 [ ! "$mtdnum" ] && return 1
45 for ubidevdir
in /sys
/devices
/virtual
/ubi
/ubi
*; do
46 [ ! -d "$ubidevdir" ] && continue
47 cmtdnum
="$( cat $ubidevdir/mtd_num )"
48 [ ! "$mtdnum" ] && continue
49 if [ "$mtdnum" = "$cmtdnum" ]; then
50 ubidev
=$
( basename $ubidevdir )
51 ubi_mknod
"$ubidevdir"
58 nand_get_magic_long
() {
59 (${3}cat "$1" |
dd bs
=4 "skip=${2:-0}" count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
62 get_magic_long_tar
() {
63 (tar xO
${3}f
"$1" "$2" |
dd bs
=4 count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
95 identify_magic $
(nand_get_magic_long
"$@")
99 identify_magic $
(get_magic_long_tar
"$@")
103 if [ "$(identify "$1")" = gzip ]; then echo -n z
; fi
106 nand_restore_config
() {
107 local ubidev
=$
( nand_find_ubi
"$CI_UBIPART" )
108 local ubivol
="$( nand_find_volume $ubidev rootfs_data )"
109 if [ ! "$ubivol" ]; then
110 ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
111 if [ ! "$ubivol" ]; then
112 echo "cannot find ubifs data volume"
117 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
118 echo "cannot mount ubifs volume $ubivol"
122 if mv "$1" "/tmp/new_root/$BACKUP_FILE"; then
123 if umount
/tmp
/new_root
; then
124 echo "configuration saved"
131 echo "could not save configuration to ubifs volume $ubivol"
136 nand_remove_ubiblock
() {
139 local ubiblk
="ubiblock${ubivol:3}"
140 if [ -e "/dev/$ubiblk" ]; then
141 umount
"/dev/$ubiblk" && echo "unmounted /dev/$ubiblk" ||
:
142 if ! ubiblock
-r "/dev/$ubivol"; then
143 echo "cannot remove $ubiblk"
151 local has_env
="${2:-0}"
153 local mtdnum
="$( find_mtd_index "$ubipart" )"
154 if [ ! "$mtdnum" ]; then
155 >&2 echo "cannot find ubi mtd partition $ubipart"
159 local ubidev
="$( nand_find_ubi "$ubipart" )"
160 if [ ! "$ubidev" ]; then
161 >&2 ubiattach
-m "$mtdnum"
162 ubidev
="$( nand_find_ubi "$ubipart" )"
164 if [ ! "$ubidev" ]; then
165 >&2 ubiformat
/dev
/mtd
$mtdnum -y
166 >&2 ubiattach
-m "$mtdnum"
167 ubidev
="$( nand_find_ubi "$ubipart" )"
169 if [ ! "$ubidev" ]; then
170 >&2 echo "cannot attach ubi mtd partition $ubipart"
174 if [ "$has_env" -gt 0 ]; then
175 >&2 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
176 >&2 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
188 local mtdnum
="$( find_mtd_index "$ubipart" )"
189 if [ ! "$mtdnum" ]; then
190 echo "cannot find ubi mtd partition $ubipart"
194 local ubidev
="$( nand_find_ubi "$ubipart" )"
195 if [ "$ubidev" ]; then
196 for ubivol
in $
(find /dev
-name "${ubidev}_*" -maxdepth 1 |
sort); do
198 nand_remove_ubiblock
"$ubivol" ||
:
199 umount
"/dev/$ubivol" && echo "unmounted /dev/$ubivol" ||
:
201 if ! ubidetach
-m "$mtdnum"; then
202 echo "cannot detach ubi mtd partition $ubipart"
208 nand_upgrade_prepare_ubi
() {
209 local rootfs_length
="$1"
210 local rootfs_type
="$2"
211 local rootfs_data_max
="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
212 [ -n "$rootfs_data_max" ] && rootfs_data_max
=$
((rootfs_data_max
))
214 local kernel_length
="$3"
215 local has_env
="${4:-0}"
217 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
219 local ubidev
="$( nand_attach_ubi "$CI_UBIPART" "$has_env" )"
220 [ -n "$ubidev" ] ||
return 1
222 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
223 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
224 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
225 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol
=
228 [ "$kern_ubivol" ] && { nand_remove_ubiblock
$kern_ubivol ||
return 1; }
229 [ "$root_ubivol" ] && { nand_remove_ubiblock
$root_ubivol ||
return 1; }
230 [ "$data_ubivol" ] && { nand_remove_ubiblock
$data_ubivol ||
return 1; }
233 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_KERNPART" ||
:
234 [ "$root_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_ROOTPART" ||
:
235 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data ||
:
238 if [ -n "$kernel_length" ]; then
239 if ! ubimkvol
/dev
/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
240 echo "cannot create kernel volume"
246 if [ -n "$rootfs_length" ]; then
247 local rootfs_size_param
248 if [ "$rootfs_type" = "ubifs" ]; then
249 rootfs_size_param
="-m"
251 rootfs_size_param
="-s $rootfs_length"
253 if ! ubimkvol
/dev
/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
254 echo "cannot create rootfs volume"
259 # create rootfs_data vol for non-ubifs rootfs
260 if [ "$rootfs_type" != "ubifs" ]; then
261 local rootfs_data_size_param
="-m"
262 if [ -n "$rootfs_data_max" ]; then
263 rootfs_data_size_param
="-s $rootfs_data_max"
265 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
$rootfs_data_size_param; then
266 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
267 echo "cannot initialize rootfs_data volume"
276 # Write the UBI image to MTD ubi partition
277 nand_upgrade_ubinized
() {
281 nand_detach_ubi
"$CI_UBIPART" ||
return 1
283 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
284 ${gz}cat "$ubi_file" | ubiformat
"/dev/mtd$mtdnum" -y -f - && ubiattach
-m "$mtdnum"
287 # Write the UBIFS image to UBI rootfs volume
288 nand_upgrade_ubifs
() {
289 local ubifs_file
="$1"
292 local ubifs_length
=$
( (${gz}cat "$ubifs_file" |
wc -c) 2> /dev
/null
)
294 nand_upgrade_prepare_ubi
"$ubifs_length" "ubifs" "" "" ||
return 1
296 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
297 local root_ubivol
="$(nand_find_volume $ubidev "$CI_ROOTPART")"
298 ${gz}cat "$ubifs_file" | ubiupdatevol
/dev
/$root_ubivol -s "$ubifs_length" -
301 # Write the FIT image to UBI kernel volume
306 local fit_length
=$
( (${gz}cat "$fit_file" |
wc -c) 2> /dev
/null
)
308 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1" ||
return 1
310 local fit_ubidev
="$(nand_find_ubi "$CI_UBIPART")"
311 local fit_ubivol
="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
312 ${gz}cat "$fit_file" | ubiupdatevol
/dev
/$fit_ubivol -s "$fit_length" -
315 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
320 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
321 local board_dir
="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
322 board_dir
="${board_dir%/}"
324 local kernel_mtd kernel_length
325 if [ "$CI_KERNPART" != "none" ]; then
326 kernel_mtd
="$(find_mtd_index "$CI_KERNPART")"
327 kernel_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/kernel" |
wc -c) 2> /dev
/null
)
328 [ "$kernel_length" = 0 ] && kernel_length
=
330 local rootfs_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/root" |
wc -c) 2> /dev
/null
)
331 [ "$rootfs_length" = 0 ] && rootfs_length
=
333 [ "$rootfs_length" ] && rootfs_type
="$(identify_tar "$tar_file" "$board_dir/root
" "$gz")"
335 local ubi_kernel_length
336 if [ "$kernel_length" ]; then
337 if [ "$kernel_mtd" ]; then
338 # On some devices, the raw kernel and ubi partitions overlap.
339 # These devices brick if the kernel partition is erased.
340 # Hence only invalidate kernel for now.
341 dd if=/dev
/zero bs
=4096 count
=1 2> /dev
/null | \
342 mtd
write - "$CI_KERNPART"
344 ubi_kernel_length
="$kernel_length"
348 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" ||
return 1
350 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
351 if [ "$rootfs_length" ]; then
352 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
353 tar xO
${gz}f
"$tar_file" "$board_dir/root" | \
354 ubiupdatevol
/dev
/$root_ubivol -s "$rootfs_length" -
356 if [ "$kernel_length" ]; then
357 if [ "$kernel_mtd" ]; then
358 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
359 mtd
write - "$CI_KERNPART"
361 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
362 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
363 ubiupdatevol
/dev
/$kern_ubivol -s "$kernel_length" -
370 nand_verify_if_gzip_file
() {
374 if [ "$gz" = z
]; then
375 echo "verifying compressed sysupgrade file integrity"
376 if ! gzip -t "$file"; then
377 echo "corrupted compressed sysupgrade file"
383 nand_verify_tar_file
() {
387 echo "verifying sysupgrade tar file integrity"
388 if ! tar xO
${gz}f
"$file" > /dev
/null
; then
389 echo "corrupted sysupgrade tar file"
394 nand_do_flash_file
() {
397 local gz
="$(identify_if_gzip "$file")"
398 local file_type
="$(identify "$file" "" "$gz")"
400 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
=rootfs
404 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
405 nand_upgrade_fit
"$file" "$gz"
408 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
409 nand_upgrade_ubinized
"$file" "$gz"
412 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
413 nand_upgrade_ubifs
"$file" "$gz"
416 nand_verify_tar_file
"$file" "$gz" ||
return 1
417 nand_upgrade_tar
"$file" "$gz"
422 nand_do_restore_config
() {
423 local conf_tar
="/tmp/sysupgrade.tgz"
424 [ ! -f "$conf_tar" ] || nand_restore_config
"$conf_tar"
427 # Recognize type of passed file and start the upgrade process
432 nand_do_flash_file
"$file" && nand_do_upgrade_success
433 nand_do_upgrade_failed
436 nand_do_upgrade_success
() {
437 if nand_do_restore_config
&& sync
; then
438 echo "sysupgrade successful"
442 nand_do_upgrade_failed
445 nand_do_upgrade_failed
() {
447 echo "sysupgrade failed"
448 # Should we reboot or bring up some failsafe mode instead?
453 # Check if passed file is a valid one for NAND sysupgrade.
454 # Currently it accepts 4 types of files:
455 # 1) UBI: a ubinized image containing required UBI volumes.
456 # 2) UBIFS: a UBIFS rootfs volume image.
457 # 3) FIT: a FIT image containing kernel and rootfs.
458 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
459 # a non-empty "CONTROL" file and required partition and/or volume images.
461 # You usually want to call this function in platform_check_image.
463 # $(1): board name, used in case of passing TAR file
464 # $(2): file to be checked
465 nand_do_platform_check
() {
466 local board_name
="$1"
469 local gz
="$(identify_if_gzip "$file")"
470 local file_type
="$(identify "$file" "" "$gz")"
471 local control_length
=$
( (tar xO
${gz}f
"$file" "sysupgrade-$board_name/CONTROL" |
wc -c) 2> /dev
/null
)
473 if [ "$control_length" != 0 ]; then
474 nand_verify_tar_file
"$file" "$gz" ||
return 1
476 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
477 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
478 echo "invalid sysupgrade file"