2 # Copyright (C) 2014 OpenWrt.org
7 # 'kernel' partition on NAND contains the kernel
10 # 'ubi' partition on NAND contains UBI
14 local ubidevdir ubivoldir
15 ubidevdir="/sys/devices/virtual/ubi/$1"
16 [ ! -d "$ubidevdir" ] && return 1
17 for ubivoldir in $ubidevdir/${1}_*; do
18 [ ! -d "$ubivoldir" ] && continue
19 if [ "$( cat $ubivoldir/name )" = "$2" ]; then
27 local ubidevdir ubidev mtdnum
28 mtdnum="$( find_mtd_index $1 )"
29 [ ! "$mtdnum" ] && return 1
30 for ubidevdir in /sys/devices/virtual/ubi/ubi*; do
31 [ ! -d "$ubidevdir" ] && continue
32 cmtdnum="$( cat $ubidevdir/mtd_num )"
33 [ ! "$mtdnum" ] && continue
34 if [ "$mtdnum" = "$cmtdnum" ]; then
35 ubidev=$( basename $ubidevdir )
42 nand_get_magic_long() {
43 dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
46 get_magic_long_tar() {
47 ( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
76 identify_magic $(nand_get_magic_long "$1" "${2:-0}")
80 identify_magic $(get_magic_long_tar "$1" "$2")
83 nand_restore_config() {
85 local ubidev=$( nand_find_ubi $CI_UBIPART )
86 local ubivol="$( nand_find_volume $ubidev rootfs_data )"
88 ubivol="$( nand_find_volume $ubidev rootfs )"
90 if ! mount -t ubifs /dev/$ubivol /tmp/new_root; then
91 echo "mounting ubifs $ubivol failed"
95 mv "$1" "/tmp/new_root/sysupgrade.tgz"
101 nand_upgrade_prepare_ubi() {
102 local rootfs_length="$1"
103 local rootfs_type="$2"
104 local has_kernel="${3:-0}"
105 local has_env="${4:-0}"
107 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
108 if [ ! "$mtdnum" ]; then
109 echo "cannot find ubi mtd partition $CI_UBIPART"
113 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
114 if [ ! "$ubidev" ]; then
115 ubiattach -m "$mtdnum"
117 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
120 if [ ! "$ubidev" ]; then
121 ubiformat /dev/mtd$mtdnum -y
122 ubiattach -m "$mtdnum"
124 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
125 [ -z "$has_env" ] || {
126 ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
127 ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
131 local kern_ubivol="$( nand_find_volume $ubidev kernel )"
132 local root_ubivol="$( nand_find_volume $ubidev rootfs )"
133 local data_ubivol="$( nand_find_volume $ubidev rootfs_data )"
135 # remove ubiblock device of rootfs
136 local root_ubiblk="ubiblock${root_ubivol:3}"
137 if [ "$root_ubivol" -a -e "/dev/$root_ubiblk" ]; then
138 echo "removing $root_ubiblk"
139 if ! ubiblock -r /dev/$root_ubivol; then
140 echo "cannot remove $root_ubiblk"
146 [ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
147 [ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
148 [ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
151 if [ "$has_kernel" = "1" ]; then
152 if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
153 echo "cannot create kernel volume"
159 local root_size_param
160 if [ "$rootfs_type" = "ubifs" ]; then
163 root_size_param="-s $rootfs_length"
165 if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
166 echo "cannot create rootfs volume"
170 # create rootfs_data for non-ubifs rootfs
171 if [ "$rootfs_type" != "ubifs" ]; then
172 if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
173 echo "cannot initialize rootfs_data volume"
181 nand_do_upgrade_success() {
182 local conf_tar="/tmp/sysupgrade.tgz"
185 [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
186 echo "sysupgrade successful"
190 nand_upgrade_ubinized() {
192 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
196 mtdnum="$(find_mtd_index "$CI_UBIPART")"
199 if [ ! "$mtdnum" ]; then
200 echo "cannot find mtd device $CI_UBIPART"
204 local mtddev="/dev/mtd${mtdnum}"
205 ubidetach -p "${mtddev}" || true
207 ubiformat "${mtddev}" -y -f "${ubi_file}"
208 ubiattach -p "${mtddev}"
209 nand_do_upgrade_success
212 nand_upgrade_ubifs() {
213 local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
215 nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
217 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
218 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
219 ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
221 nand_do_upgrade_success
226 local board_name="$(cat /tmp/sysinfo/board_name)"
227 local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
229 local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
230 local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
232 local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
237 [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
238 tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
240 [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
242 nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
244 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
245 [ "$has_kernel" = "1" ] && {
246 local kern_ubivol="$(nand_find_volume $ubidev kernel)"
247 tar xf $tar_file sysupgrade-$board_name/kernel -O | \
248 ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
251 local root_ubivol="$(nand_find_volume $ubidev rootfs)"
252 tar xf $tar_file sysupgrade-$board_name/root -O | \
253 ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
255 nand_do_upgrade_success
258 nand_do_upgrade_stage2() {
259 local file_type=$(identify $1)
261 [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
263 [ "$file_type" == "ubi" ] && nand_upgrade_ubinized $1
264 [ "$file_type" == "ubifs" ] && nand_upgrade_ubifs $1
268 nand_upgrade_stage2() {
271 touch /tmp/sysupgrade
283 if [ -n "$(rootfs_type)" ]; then
284 v "Switching to ramdisk..."
285 run_ramfs ". /lib/functions.sh; include /lib/upgrade; nand_do_upgrade_stage2 $2"
287 nand_do_upgrade_stage2 $2
291 echo "Nand upgrade failed"
296 nand_upgrade_stage1() {
297 [ -f /tmp/sysupgrade-nand-path ] && {
298 path="$(cat /tmp/sysupgrade-nand-path)"
299 [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
302 ubus call system nandupgrade "{\"path\": \"$path\" }"
306 append sysupgrade_pre_upgrade nand_upgrade_stage1
308 nand_do_platform_check() {
309 local board_name="$1"
311 local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
312 local file_type="$(identify $2)"
314 [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
315 echo "Invalid sysupgrade file."
319 echo -n $2 > /tmp/sysupgrade-nand-path
320 cp /sbin/upgraded /tmp/