2 # Copyright (C) 2014 OpenWrt.org
7 # combined-image uses 64k blocks
9 # 'data' partition on NAND contains UBI
13 local ubidevdir ubivoldir
14 ubidevdir
="/sys/devices/virtual/ubi/$1"
15 [ ! -d "$ubidevdir" ] && return 1
16 for ubivoldir
in $ubidevdir/${1}_
*; do
17 [ ! -d "$ubivoldir" ] && continue
18 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
26 local ubidevdir ubidev mtdnum
27 mtdnum
="$( find_mtd_index $1 )"
28 [ ! "$mtdnum" ] && return 1
29 for ubidevdir
in /sys
/devices
/virtual
/ubi
/ubi
*; do
30 [ ! -d "$ubidevdir" ] && continue
31 cmtdnum
="$( cat $ubidevdir/mtd_num )"
32 [ ! "$mtdnum" ] && continue
33 if [ "$mtdnum" = "$cmtdnum" ]; then
34 ubidev
=$
( basename $ubidevdir )
42 dd if="$1" skip
=$2 bs
=4 count
=1 2>/dev
/null |
hexdump -v -n 4 -e '1/1 "%02x"'
45 get_magic_long_tar
() {
46 ( tar xf
$1 $2 -O |
dd bs
=4 count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev
/null
75 identify_magic $
(get_magic_long
"$1" "${2:-0}")
79 identify_magic $
(get_magic_long_tar
"$1" "$2")
82 nand_restore_config
() {
84 local ubidev
=$
( nand_find_ubi
$CI_UBIPART )
85 local ubivol
="$( nand_find_volume $ubidev rootfs_data )"
87 ubivol
="$( nand_find_volume $ubidev rootfs )"
89 if ! mount
-t ubifs
/dev
/$ubivol /tmp
/new_root
; then
90 echo "mounting ubifs $ubivol failed"
94 mv "$1" "/tmp/new_root/sysupgrade.tgz"
100 nand_upgrade_prepare_ubi
() {
101 local rootfs_length
="$1"
102 local rootfs_type
="$1"
103 local has_kernel
="${2:-0}"
104 local has_env
="${3:-0}"
106 local mtdnum
="$( find_mtd_index "$CI_UBIPART" )"
107 if [ ! "$mtdnum" ]; then
108 echo "cannot find ubi mtd partition $CI_UBIPART"
112 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
113 if [ ! "$ubidev" ]; then
114 ubiattach
-m "$mtdnum"
116 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
119 if [ ! "$ubidev" ]; then
120 ubiformat
/dev
/mtd
$mtdnum -y
121 ubiattach
-m "$mtdnum"
123 ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
124 [ -z "$has_env" ] ||
{
125 ubimkvol
/dev
/$ubidev -n 0 -N ubootenv
-s 1MiB
126 ubimkvol
/dev
/$ubidev -n 1 -N ubootenv2
-s 1MiB
130 local kern_ubivol
="$( nand_find_volume $ubidev kernel )"
131 local root_ubivol
="$( nand_find_volume $ubidev rootfs )"
132 local data_ubivol
="$( nand_find_volume $ubidev rootfs_data )"
134 # remove ubiblock device of rootfs
135 local root_ubiblk
="ubiblock${root_ubivol:3}"
136 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
137 echo "removing $root_ubiblk"
138 if ! ubiblock
-r /dev
/$root_ubivol; then
139 echo "cannot remove $root_ubiblk"
145 [ "$kern_ubivol" ] && ubirmvol
/dev
/$ubidev -N kernel || true
146 [ "$root_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs || true
147 [ "$data_ubivol" ] && ubirmvol
/dev
/$ubidev -N rootfs_data || true
150 if [ "$has_kernel" = "1" ]; then
151 if ! ubimkvol
/dev
/$ubidev -N kernel
-s $kernel_length; then
152 echo "cannot create kernel volume"
158 local root_size_param
159 if [ "$rootfs_type" = "ubifs" ]; then
162 root_size_param
="-s $rootfs_length"
164 if ! ubimkvol
/dev
/$ubidev -N rootfs
$root_size_param; then
165 echo "cannot create rootfs volume"
169 # create rootfs_data for non-ubifs rootfs
170 if [ "$rootfs_type" != "ubifs" ]; then
171 if ! ubimkvol
/dev
/$ubidev -N rootfs_data
-m; then
172 echo "cannot initialize rootfs_data volume"
180 nand_do_upgrade_success
() {
181 local conf_tar
="/tmp/sysupgrade.tgz"
184 [ -f "$conf_tar" ] && nand_restore_config
"$conf_tar"
185 echo "sysupgrade successfull"
189 nand_upgrade_ubinized
() {
191 local mtdnum
="$(find_mtd_index "$CI_UBIPART")"
193 if [ ! "$mtdnum" ]; then
194 echo "cannot find mtd device $CI_UBIPART"
198 local mtddev
="/dev/mtd${mtdnum}"
199 ubidetach
-p "${mtddev}" || true
201 ubiformat
"${mtddev}" -y -f "${ubi_file}"
202 ubiattach
-p "${mtddev}"
203 nand_do_upgrade_success
206 nand_do_upgrade_stage2
() {
207 [ "$(identify $1)" == "ubi" ] && nand_upgrade_ubinized
$1
210 local board_name
="$(cat /tmp/sysinfo/board_name)"
211 local kernel_mtd
="$(find_mtd_index kernel)"
213 local kernel_length
=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
214 local rootfs_length
=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
215 local ubi_length
=`(tar xf $tar_file sysupgrade-$board_name/ubi -O | wc -c) 2> /dev/null`
217 local rootfs_type
="$(identify_tar "$tar_file" root)"
222 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] ||
{
223 tar xf
$tar_file sysupgrade-
$board_name/kernel
-O | mtd
write - kernel
225 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel
=0
227 nand_upgrade_prepare_ubi
"$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
229 local ubidev
="$( nand_find_ubi "$CI_UBIPART" )"
230 [ "$has_kernel" = "1" ] && {
231 local kern_ubivol
="$(nand_find_volume $ubidev kernel)"
232 tar xf
$tar_file sysupgrade-
$board_name/kernel
-O | \
233 ubiupdatevol
/dev
/$kern_ubivol -s $kern_length -
236 local root_ubivol
="$(nand_find_volume $ubidev rootfs)"
237 tar xf
$tar_file sysupgrade-
$board_name/root
-O | \
238 ubiupdatevol
/dev
/$root_ubivol -s $rootfs_length -
240 nand_do_upgrade_success
243 nand_upgrade_stage2
() {
246 touch /tmp
/sysupgrade
258 if [ -n "$(rootfs_type)" ]; then
259 v
"Switching to ramdisk..."
260 run_ramfs
". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
262 nand_do_upgrade_stage2
$2
266 echo "Nand upgrade failed"
271 nand_upgrade_stage1
() {
272 [ -f /tmp
/sysupgrade-nand-path
] && {
273 path
="$(cat /tmp/sysupgrade-nand-path)"
274 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
277 ubus call system nandupgrade
"{\"path\": \"$path\" }"
281 append sysupgrade_pre_upgrade nand_upgrade_stage1
283 nand_do_platform_check
() {
284 local board_name
="$1"
286 local control_length
=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
288 [ "$control_length" = 0 -a "$(identify $2)" != "ubi" ] && {
289 echo "Invalid sysupgrade file."
293 echo -n $2 > /tmp
/sysupgrade-nand-path
294 cp /sbin
/upgraded
/tmp
/