2 # Copyright (C) 2014 OpenWrt.org
7 # 'data' partition on NAND contains UBI
11 local ubidevdir ubivoldir
12 ubidevdir="/sys/devices/virtual/ubi/$1"
13 [ ! -d "$ubidevdir" ] && return 1
14 for ubivoldir in $ubidevdir/${1}_*; do
15 [ ! -d "$ubivoldir" ] && continue
16 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
24 local ubidevdir ubidev mtdnum
25 mtdnum="$( find_mtd_index $1 )"
26 [ ! "$mtdnum" ] && return 1
27 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
28 [ ! -d "$ubidevdir" ] && continue
29 cmtdnum="$( cat $ubidevdir/mtd_num )"
30 [ ! "$mtdnum" ] && continue
31 if [ "$mtdnum" = "$cmtdnum" ]; then
32 ubidev=$( basename $ubidevdir )
40 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
43 get_magic_long_tar() {
44 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
73 identify_magic $(get_magic_long "$1" "${2:-0}")
77 identify_magic $(get_magic_long_tar "$1" "$2")
80 nand_restore_config() {
82 local ubidev=$( nand_find_ubi $CI_UBIPART )
83 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
85 ubivol="$( nand_find_volume $ubidev rootfs )"
87 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
88 echo "mounting ubifs $ubivol failed"
92 mv "$1" "/tmp/new_root/sysupgrade.tgz"
98 nand_upgrade_prepare_ubi() {
99 local rootfs_length="$1"
100 local rootfs_type="$1"
101 local has_kernel="${2:-0}"
102 local has_env="${3:-0}"
104 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
105 if [ ! "$mtdnum" ]; then
106 echo "cannot find ubi mtd partition $CI_UBIPART"
110 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
111 if [ ! "$ubidev" ]; then
112 ubiattach -m "$mtdnum"
114 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
117 if [ ! "$ubidev" ]; then
118 ubiformat /dev/mtd$mtdnum -y
119 ubiattach -m "$mtdnum"
121 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
122 [ -z "$has_env" ] || {
123 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
124 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
128 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
129 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
130 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
132 # remove ubiblock device of rootfs
133 local root_ubiblk="ubiblock${root_ubivol:3}"
134 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
135 echo "removing $root_ubiblk"
136 if ! ubiblock -r /dev/$root_ubivol; then
137 echo "cannot remove $root_ubiblk"
143 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
144 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
145 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
148 if [ "$has_kernel" = "1" ]; then
149 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
150 echo "cannot create kernel volume"
156 local root_size_param
157 if [ "$rootfs_type" = "ubifs" ]; then
160 root_size_param="-s $rootfs_length"
162 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
163 echo "cannot create rootfs volume"
167 # create rootfs_data for non-ubifs rootfs
168 if [ "$rootfs_type" != "ubifs" ]; then
169 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
170 echo "cannot initialize rootfs_data volume"
178 nand_do_upgrade_success() {
179 local conf_tar="/tmp/sysupgrade.tgz"
182 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
183 echo "sysupgrade successfull"
187 nand_upgrade_ubinized() {
189 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
191 if [ ! "$mtdnum" ]; then
192 echo "cannot find mtd device $CI_UBIPART"
196 local mtddev="/dev/mtd${mtdnum}"
197 ubidetach -p "${mtddev}" || true
199 ubiformat "${mtddev}" -y -f "${ubi_file}"
200 ubiattach -p "${mtddev}"
201 nand_do_upgrade_success
204 nand_do_upgrade_stage2() {
205 [ "$(identify $1)" == "ubi" ] && nand_upgrade_ubinized $1
208 local board_name="$(cat /tmp/sysinfo/board_name)"
209 local kernel_mtd="$(find_mtd_index kernel)"
211 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
212 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
214 local rootfs_type="$(identify_tar "$tar_file" root)"
219 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
220 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - kernel
222 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
224 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
226 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
227 [ "$has_kernel" = "1" ] && {
228 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
229 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
230 ubiupdatevol /dev/$kern_ubivol -s $kern_length -
233 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
234 tar xf $tar_file sysupgrade-$board_name/root -O | \
235 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
237 nand_do_upgrade_success
240 nand_upgrade_stage2() {
243 touch /tmp/sysupgrade
255 if [ -n "$(rootfs_type)" ]; then
256 v "Switching to ramdisk..."
257 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
259 nand_do_upgrade_stage2 $2
263 echo "Nand upgrade failed"
268 nand_upgrade_stage1() {
269 [ -f /tmp/sysupgrade-nand-path ] && {
270 path="$(cat /tmp/sysupgrade-nand-path)"
271 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
274 ubus call system nandupgrade "{\"path\": \"$path\" }"
278 append sysupgrade_pre_upgrade nand_upgrade_stage1
280 nand_do_platform_check() {
281 local board_name="$1"
283 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
285 [ "$control_length" = 0 -a "$(identify $2)" != "ubi" ] && {
286 echo "Invalid sysupgrade file."
290 echo -n $2 > /tmp/sysupgrade-nand-path
291 cp /sbin/upgraded /tmp/