2 # Copyright (C) 2014 OpenWrt.org
7 # 'kernel' partition on NAND contains the kernel
8 CI_KERNPART
="${CI_KERNPART:-kernel}"
10 # 'ubi' partition on NAND contains UBI
11 CI_UBIPART
="${CI_UBIPART:-ubi}"
13 # 'rootfs' partition on NAND contains the rootfs
14 CI_ROOTPART
="${CI_ROOTPART:-rootfs}"
18 local dev
="/dev/$(basename $dir)"
20 [ -e "$dev" ] && return 0
22 local devid
="$(cat $dir/dev)"
23 local major
="${devid%%:*}"
24 local minor
="${devid##*:}"
25 mknod
"$dev" c
$major $minor
29 local ubidevdir ubivoldir
30 ubidevdir
="/sys/devices/virtual/ubi/$1"
31 [ ! -d "$ubidevdir" ] && return 1
32 for ubivoldir
in $ubidevdir/${1}_
*; do
33 [ ! -d "$ubivoldir" ] && continue
34 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
36 ubi_mknod
"$ubivoldir"
43 local ubidevdir ubidev mtdnum
44 mtdnum
="$( find_mtd_index $1 )"
45 [ ! "$mtdnum" ] && return 1
46 for ubidevdir
in /sys
/devices
/virtual
/ubi
/ubi
*; do
47 [ ! -d "$ubidevdir" ] && continue
48 cmtdnum
="$( cat $ubidevdir/mtd_num )"
49 [ ! "$mtdnum" ] && continue
50 if [ "$mtdnum" = "$cmtdnum" ]; then
51 ubidev
=$
( basename $ubidevdir )
52 ubi_mknod
"$ubidevdir"
59 nand_get_magic_long
() {
60 dd if="$1" skip
=$2 bs
=4 count
=1 2>/dev
/null |
hexdump -v -n 4 -e '1/1 "%02x"'
63 get_magic_long_tar
() {
64 ( tar xf
$1 $2 -O |
dd bs
=4 count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
93 identify_magic $
(nand_get_magic_long
"$1" "${2:-0}")
97 identify_magic $
(get_magic_long_tar
"$1" "$2")
100 nand_restore_config
() {
102 local ubidev
=$
( nand_find_ubi
$CI_UBIPART )
103 local ubivol
="$( nand_find_volume $ubidev rootfs_data )"
105 ubivol
="$( nand_find_volume $ubidev $CI_ROOTPART )"
107 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
108 echo "mounting ubifs $ubivol failed"
112 mv "$1" "/tmp/new_root/$BACKUP_FILE"
118 nand_upgrade_prepare_ubi
() {
119 local rootfs_length
="$1"
120 local rootfs_type
="$2"
121 local has_kernel
="${3:-0}"
122 local has_env
="${4:-0}"
124 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
125 if [ ! "$mtdnum" ]; then
126 echo "cannot find ubi mtd partition $CI_UBIPART"
130 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
131 if [ ! "$ubidev" ]; then
132 ubiattach
-m "$mtdnum"
134 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
137 if [ ! "$ubidev" ]; then
138 ubiformat
/dev
/mtd
$mtdnum -y
139 ubiattach
-m "$mtdnum"
141 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
142 [ "$has_env" -gt 0 ] && {
143 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
144 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
148 local kern_ubivol
="$( nand_find_volume $ubidev $CI_KERNPART )"
149 local root_ubivol
="$( nand_find_volume $ubidev $CI_ROOTPART )"
150 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
152 # remove ubiblock device of rootfs
153 local root_ubiblk
="ubiblock${root_ubivol:3}"
154 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
155 echo "removing $root_ubiblk"
156 if ! ubiblock
-r /dev
/$root_ubivol; then
157 echo "cannot remove $root_ubiblk"
163 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N $CI_KERNPART || true
164 [ "$root_ubivol" ] && ubirmvol
/dev
/$ubidev -N $CI_ROOTPART || true
165 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data || true
168 if [ "$has_kernel" = "1" ]; then
169 if ! ubimkvol
/dev
/$ubidev -N $CI_KERNPART -s $kernel_length; then
170 echo "cannot create kernel volume"
176 local root_size_param
177 if [ "$rootfs_type" = "ubifs" ]; then
180 root_size_param
="-s $rootfs_length"
182 if ! ubimkvol
/dev
/$ubidev -N $CI_ROOTPART $root_size_param; then
183 echo "cannot create rootfs volume"
187 # create rootfs_data for non-ubifs rootfs
188 if [ "$rootfs_type" != "ubifs" ]; then
189 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
190 echo "cannot initialize rootfs_data volume"
198 nand_do_upgrade_success
() {
199 local conf_tar
="/tmp/sysupgrade.tgz"
202 [ -f "$conf_tar" ] && nand_restore_config
"$conf_tar"
203 echo "sysupgrade successful"
208 # Flash the UBI image to MTD partition
209 nand_upgrade_ubinized
() {
211 local mtdnum
="$(find_mtd_index "$CI_UBIPART")"
215 mtdnum
="$(find_mtd_index "$CI_UBIPART")"
218 if [ ! "$mtdnum" ]; then
219 echo "cannot find mtd device $CI_UBIPART"
224 local mtddev
="/dev/mtd${mtdnum}"
225 ubidetach
-p "${mtddev}" || true
227 ubiformat
"${mtddev}" -y -f "${ubi_file}"
228 ubiattach
-p "${mtddev}"
229 nand_do_upgrade_success
232 # Write the UBIFS image to UBI volume
233 nand_upgrade_ubifs
() {
234 local rootfs_length
=`(cat $1 | wc -c) 2> /dev/null`
236 nand_upgrade_prepare_ubi
"$rootfs_length" "ubifs" "0" "0"
238 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
239 local root_ubivol
="$(nand_find_volume $ubidev $CI_ROOTPART)"
240 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length $1
242 nand_do_upgrade_success
247 local kernel_mtd
="$(find_mtd_index $CI_KERNPART)"
249 local board_dir
=$
(tar tf
$tar_file |
grep -m 1 '^sysupgrade-.*/$')
250 board_dir
=${board_dir%/}
252 local kernel_length
=`(tar xf $tar_file ${board_dir}/kernel -O | wc -c) 2> /dev/null`
253 local rootfs_length
=`(tar xf $tar_file ${board_dir}/root -O | wc -c) 2> /dev/null`
255 local rootfs_type
="$(identify_tar "$tar_file" ${board_dir}/root)"
260 [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
261 tar xf
$tar_file ${board_dir}/kernel
-O | mtd
write - $CI_KERNPART
263 [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel
=0
265 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
267 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
268 [ "$has_kernel" = "1" ] && {
269 local kern_ubivol
="$(nand_find_volume $ubidev $CI_KERNPART)"
270 tar xf
$tar_file ${board_dir}/kernel
-O | \
271 ubiupdatevol
/dev
/$kern_ubivol -s $kernel_length -
274 local root_ubivol
="$(nand_find_volume $ubidev $CI_ROOTPART)"
275 tar xf
$tar_file ${board_dir}/root
-O | \
276 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length -
278 nand_do_upgrade_success
281 # Recognize type of passed file and start the upgrade process
283 local file_type
=$
(identify
$1)
285 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART
="rootfs"
288 "ubi") nand_upgrade_ubinized
$1;;
289 "ubifs") nand_upgrade_ubifs
$1;;
290 *) nand_upgrade_tar
$1;;
294 # Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
296 # 1) UBI - should contain an ubinized image, header is checked for the proper
298 # 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
299 # header is checked for the proper MAGIC
300 # 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
301 # "CONTROL" file (at this point its content isn't verified)
303 # You usually want to call this function in platform_check_image.
305 # $(1): board name, used in case of passing TAR file
306 # $(2): file to be checked
307 nand_do_platform_check
() {
308 local board_name
="$1"
310 local control_length
=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
311 local file_type
="$(identify $2)"
313 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
314 echo "Invalid sysupgrade file."