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"
152 local mtdnum
="$( find_mtd_index "$ubipart" )"
153 if [ ! "$mtdnum" ]; then
154 echo "cannot find ubi mtd partition $ubipart"
158 local ubidev
="$( nand_find_ubi "$ubipart" )"
159 if [ "$ubidev" ]; then
160 for ubivol
in $
(find /dev
-name "${ubidev}_*" -maxdepth 1 |
sort); do
162 nand_remove_ubiblock
"$ubivol" ||
:
163 umount
"/dev/$ubivol" && echo "unmounted /dev/$ubivol" ||
:
165 if ! ubidetach
-m "$mtdnum"; then
166 echo "cannot detach ubi mtd partition $ubipart"
172 nand_upgrade_prepare_ubi
() {
173 local rootfs_length
="$1"
174 local rootfs_type
="$2"
175 local rootfs_data_max
="$(fw_printenv -n rootfs_data_max 2> /dev/null)"
176 [ -n "$rootfs_data_max" ] && rootfs_data_max
=$
((rootfs_data_max
))
178 local kernel_length
="$3"
179 local has_env
="${4:-0}"
181 [ -n "$rootfs_length" -o -n "$kernel_length" ] ||
return 1
183 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
184 if [ ! "$mtdnum" ]; then
185 echo "cannot find ubi mtd partition $CI_UBIPART"
189 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
190 if [ ! "$ubidev" ]; then
191 ubiattach
-m "$mtdnum"
192 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
194 if [ ! "$ubidev" ]; then
195 ubiformat
/dev
/mtd
$mtdnum -y
196 ubiattach
-m "$mtdnum"
197 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
199 if [ ! "$ubidev" ]; then
200 echo "cannot attach ubi mtd partition $CI_UBIPART"
204 if [ "$has_env" -gt 0 ]; then
205 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
206 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
211 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
212 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
213 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
214 [ "$root_ubivol" = "$kern_ubivol" ] && root_ubivol
=
217 [ "$kern_ubivol" ] && { nand_remove_ubiblock
$kern_ubivol ||
return 1; }
218 [ "$root_ubivol" ] && { nand_remove_ubiblock
$root_ubivol ||
return 1; }
219 [ "$data_ubivol" ] && { nand_remove_ubiblock
$data_ubivol ||
return 1; }
222 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_KERNPART" ||
:
223 [ "$root_ubivol" ] && ubirmvol
/dev
/$ubidev -N "$CI_ROOTPART" ||
:
224 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data ||
:
227 if [ -n "$kernel_length" ]; then
228 if ! ubimkvol
/dev
/$ubidev -N "$CI_KERNPART" -s $kernel_length; then
229 echo "cannot create kernel volume"
235 if [ -n "$rootfs_length" ]; then
236 local rootfs_size_param
237 if [ "$rootfs_type" = "ubifs" ]; then
238 rootfs_size_param
="-m"
240 rootfs_size_param
="-s $rootfs_length"
242 if ! ubimkvol
/dev
/$ubidev -N "$CI_ROOTPART" $rootfs_size_param; then
243 echo "cannot create rootfs volume"
248 # create rootfs_data vol for non-ubifs rootfs
249 if [ "$rootfs_type" != "ubifs" ]; then
250 local rootfs_data_size_param
="-m"
251 if [ -n "$rootfs_data_max" ]; then
252 rootfs_data_size_param
="-s $rootfs_data_max"
254 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
$rootfs_data_size_param; then
255 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
256 echo "cannot initialize rootfs_data volume"
265 # Write the UBI image to MTD ubi partition
266 nand_upgrade_ubinized
() {
270 nand_detach_ubi
"$CI_UBIPART" ||
return 1
272 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
273 ${gz}cat "$ubi_file" | ubiformat
"/dev/mtd$mtdnum" -y -f - && ubiattach
-m "$mtdnum"
276 # Write the UBIFS image to UBI rootfs volume
277 nand_upgrade_ubifs
() {
278 local ubifs_file
="$1"
281 local ubifs_length
=$
( (${gz}cat "$ubifs_file" |
wc -c) 2> /dev
/null
)
283 nand_upgrade_prepare_ubi
"$ubifs_length" "ubifs" "" "" ||
return 1
285 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
286 local root_ubivol
="$(nand_find_volume $ubidev "$CI_ROOTPART")"
287 ${gz}cat "$ubifs_file" | ubiupdatevol
/dev
/$root_ubivol -s "$ubifs_length" -
290 # Write the FIT image to UBI kernel volume
295 local fit_length
=$
( (${gz}cat "$fit_file" |
wc -c) 2> /dev
/null
)
297 nand_upgrade_prepare_ubi
"" "" "$fit_length" "1" ||
return 1
299 local fit_ubidev
="$(nand_find_ubi "$CI_UBIPART")"
300 local fit_ubivol
="$(nand_find_volume $fit_ubidev "$CI_KERNPART")"
301 ${gz}cat "$fit_file" | ubiupdatevol
/dev
/$fit_ubivol -s "$fit_length" -
304 # Write images in the TAR file to MTD partitions and/or UBI volumes as required
309 # WARNING: This fails if tar contains more than one 'sysupgrade-*' directory.
310 local board_dir
="$(tar t${gz}f "$tar_file" | grep -m 1 '^sysupgrade-.*/$')"
311 board_dir
="${board_dir%/}"
313 local kernel_mtd kernel_length
314 if [ "$CI_KERNPART" != "none" ]; then
315 kernel_mtd
="$(find_mtd_index "$CI_KERNPART")"
316 kernel_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/kernel" |
wc -c) 2> /dev
/null
)
317 [ "$kernel_length" = 0 ] && kernel_length
=
319 local rootfs_length
=$
( (tar xO
${gz}f
"$tar_file" "$board_dir/root" |
wc -c) 2> /dev
/null
)
320 [ "$rootfs_length" = 0 ] && rootfs_length
=
322 [ "$rootfs_length" ] && rootfs_type
="$(identify_tar "$tar_file" "$board_dir/root
" "$gz")"
324 local ubi_kernel_length
325 if [ "$kernel_length" ]; then
326 if [ "$kernel_mtd" ]; then
327 # On some devices, the raw kernel and ubi partitions overlap.
328 # These devices brick if the kernel partition is erased.
329 # Hence only invalidate kernel for now.
330 dd if=/dev
/zero bs
=4096 count
=1 2> /dev
/null | \
331 mtd
write - "$CI_KERNPART"
333 ubi_kernel_length
="$kernel_length"
337 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$ubi_kernel_length" "$has_env" ||
return 1
339 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
340 if [ "$rootfs_length" ]; then
341 local root_ubivol
="$( nand_find_volume $ubidev "$CI_ROOTPART" )"
342 tar xO
${gz}f
"$tar_file" "$board_dir/root" | \
343 ubiupdatevol
/dev
/$root_ubivol -s "$rootfs_length" -
345 if [ "$kernel_length" ]; then
346 if [ "$kernel_mtd" ]; then
347 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
348 mtd
write - "$CI_KERNPART"
350 local kern_ubivol
="$( nand_find_volume $ubidev "$CI_KERNPART" )"
351 tar xO
${gz}f
"$tar_file" "$board_dir/kernel" | \
352 ubiupdatevol
/dev
/$kern_ubivol -s "$kernel_length" -
359 nand_verify_if_gzip_file
() {
363 if [ "$gz" = z
]; then
364 echo "verifying compressed sysupgrade file integrity"
365 if ! gzip -t "$file"; then
366 echo "corrupted compressed sysupgrade file"
372 nand_verify_tar_file
() {
376 echo "verifying sysupgrade tar file integrity"
377 if ! tar xO
${gz}f
"$file" > /dev
/null
; then
378 echo "corrupted sysupgrade tar file"
383 nand_do_flash_file
() {
386 local gz
="$(identify_if_gzip "$file")"
387 local file_type
="$(identify "$file" "" "$gz")"
389 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
=rootfs
393 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
394 nand_upgrade_fit
"$file" "$gz"
397 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
398 nand_upgrade_ubinized
"$file" "$gz"
401 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
402 nand_upgrade_ubifs
"$file" "$gz"
405 nand_verify_tar_file
"$file" "$gz" ||
return 1
406 nand_upgrade_tar
"$file" "$gz"
411 nand_do_restore_config
() {
412 local conf_tar
="/tmp/sysupgrade.tgz"
413 [ ! -f "$conf_tar" ] || nand_restore_config
"$conf_tar"
416 # Recognize type of passed file and start the upgrade process
421 if nand_do_flash_file
"$file" && nand_do_restore_config
&& sync
; then
422 echo "sysupgrade successful"
428 echo "sysupgrade failed"
429 # Should we reboot or bring up some failsafe mode instead?
434 # Check if passed file is a valid one for NAND sysupgrade.
435 # Currently it accepts 4 types of files:
436 # 1) UBI: a ubinized image containing required UBI volumes.
437 # 2) UBIFS: a UBIFS rootfs volume image.
438 # 3) FIT: a FIT image containing kernel and rootfs.
439 # 4) TAR: an archive that includes directory "sysupgrade-${BOARD_NAME}" containing
440 # a non-empty "CONTROL" file and required partition and/or volume images.
442 # You usually want to call this function in platform_check_image.
444 # $(1): board name, used in case of passing TAR file
445 # $(2): file to be checked
446 nand_do_platform_check
() {
447 local board_name
="$1"
450 local gz
="$(identify_if_gzip "$file")"
451 local file_type
="$(identify "$file" "" "$gz")"
452 local control_length
=$
( (tar xO
${gz}f
"$file" "sysupgrade-$board_name/CONTROL" |
wc -c) 2> /dev
/null
)
454 if [ "$control_length" != 0 ]; then
455 nand_verify_tar_file
"$file" "$gz" ||
return 1
457 nand_verify_if_gzip_file
"$file" "$gz" ||
return 1
458 if [ "$file_type" != "fit" -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ]; then
459 echo "invalid sysupgrade file"