procd: update to latest git HEAD
[openwrt/openwrt.git] / package / system / procd / files / nand.sh
index 9131ddaa7bb949d30ae39ebff8ed3bbc53e05df7..9c831df3b4247d12d7e882da349eafea5894fabc 100644 (file)
@@ -4,8 +4,23 @@
 
 . /lib/functions.sh
 
-# 'data' partition on NAND contains UBI
-CI_UBIPART="ubi"
+# 'kernel' partition on NAND contains the kernel
+CI_KERNPART="${CI_KERNPART:-kernel}"
+
+# 'ubi' partition on NAND contains UBI
+CI_UBIPART="${CI_UBIPART:-ubi}"
+
+ubi_mknod() {
+       local dir="$1"
+       local dev="/dev/$(basename $dir)"
+
+       [ -e "$dev" ] && return 0
+
+       local devid="$(cat $dir/dev)"
+       local major="${devid%%:*}"
+       local minor="${devid##*:}"
+       mknod "$dev" c $major $minor
+}
 
 nand_find_volume() {
        local ubidevdir ubivoldir
@@ -15,6 +30,7 @@ nand_find_volume() {
                [ ! -d "$ubivoldir" ] && continue
                if [ "$( cat $ubivoldir/name )" = "$2" ]; then
                        basename $ubivoldir
+                       ubi_mknod "$ubivoldir"
                        return 0
                fi
        done
@@ -30,13 +46,14 @@ nand_find_ubi() {
                [ ! "$mtdnum" ] && continue
                if [ "$mtdnum" = "$cmtdnum" ]; then
                        ubidev=$( basename $ubidevdir )
+                       ubi_mknod "$ubidevdir"
                        echo $ubidev
                        return 0
                fi
        done
 }
 
-get_magic_long() {
+nand_get_magic_long() {
        dd if="$1" skip=$2 bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
 }
 
@@ -70,7 +87,7 @@ identify_magic() {
 
 
 identify() {
-       identify_magic $(get_magic_long "$1" "${2:-0}")
+       identify_magic $(nand_get_magic_long "$1" "${2:-0}")
 }
 
 identify_tar() {
@@ -97,9 +114,9 @@ nand_restore_config() {
 
 nand_upgrade_prepare_ubi() {
        local rootfs_length="$1"
-       local rootfs_type="$1"
-       local has_kernel="${2:-0}"
-       local has_env="${3:-0}"
+       local rootfs_type="$2"
+       local has_kernel="${3:-0}"
+       local has_env="${4:-0}"
 
        local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
        if [ ! "$mtdnum" ]; then
@@ -119,7 +136,7 @@ nand_upgrade_prepare_ubi() {
                ubiattach -m "$mtdnum"
                sync
                ubidev="$( nand_find_ubi "$CI_UBIPART" )"
-               [ -z "$has_env" ] || {
+               [ "$has_env" -gt 0 ] && {
                        ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
                        ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
                }
@@ -177,19 +194,27 @@ nand_upgrade_prepare_ubi() {
 
 nand_do_upgrade_success() {
        local conf_tar="/tmp/sysupgrade.tgz"
-       
+
        sync
        [ -f "$conf_tar" ] && nand_restore_config "$conf_tar"
-       echo "sysupgrade successfull"
+       echo "sysupgrade successful"
+       umount -a
        reboot -f
 }
 
+# Flash the UBI image to MTD partition
 nand_upgrade_ubinized() {
        local ubi_file="$1"
        local mtdnum="$(find_mtd_index "$CI_UBIPART")"
 
+       [ ! "$mtdnum" ] && {
+               CI_UBIPART="rootfs"
+               mtdnum="$(find_mtd_index "$CI_UBIPART")"
+       }
+
        if [ ! "$mtdnum" ]; then
                echo "cannot find mtd device $CI_UBIPART"
+               umount -a
                reboot -f
        fi
 
@@ -201,25 +226,45 @@ nand_upgrade_ubinized() {
        nand_do_upgrade_success
 }
 
-nand_do_upgrade_stage2() {
-       [ "$(identify $1)" == "ubi" ] && nand_upgrade_ubinized $1
+# Write the UBIFS image to UBI volume
+nand_upgrade_ubifs() {
+       local rootfs_length=`(cat $1 | wc -c) 2> /dev/null`
+
+       nand_upgrade_prepare_ubi "$rootfs_length" "ubifs" "0" "0"
+
+       local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
+       local root_ubivol="$(nand_find_volume $ubidev rootfs)"
+       ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
+
+       nand_do_upgrade_success
+}
+
+nand_board_name() {
+       if type 'platform_nand_board_name' >/dev/null 2>/dev/null; then
+               platform_nand_board_name
+               return
+       fi
 
+       cat /tmp/sysinfo/board_name
+}
+
+nand_upgrade_tar() {
        local tar_file="$1"
-       local board_name="$(cat /tmp/sysinfo/board_name)"
-       local kernel_mtd="$(find_mtd_index kernel)"
+       local board_name="$(nand_board_name)"
+       local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
 
        local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
        local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
 
-       local rootfs_type="$(identify_tar "$tar_file" root)"
+       local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
 
        local has_kernel=1
        local has_env=0
 
-       [ "kernel_length" = 0 -o -z "$kernel_mtd" ] || {
-               tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - kernel
+       [ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
+               tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
        }
-       [ "kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
+       [ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
 
        nand_upgrade_prepare_ubi "$rootfs_length" "$rootfs_type" "$has_kernel" "$has_env"
 
@@ -227,7 +272,7 @@ nand_do_upgrade_stage2() {
        [ "$has_kernel" = "1" ] && {
                local kern_ubivol="$(nand_find_volume $ubidev kernel)"
                tar xf $tar_file sysupgrade-$board_name/kernel -O | \
-                       ubiupdatevol /dev/$kern_ubivol -s $kern_length -
+                       ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
        }
 
        local root_ubivol="$(nand_find_volume $ubidev rootfs)"
@@ -237,6 +282,23 @@ nand_do_upgrade_stage2() {
        nand_do_upgrade_success
 }
 
+# Recognize type of passed file and start the upgrade process
+nand_do_upgrade_stage2() {
+       local file_type=$(identify $1)
+
+       if type 'platform_nand_pre_upgrade' >/dev/null 2>/dev/null; then
+               platform_nand_pre_upgrade "$1"
+       fi
+
+       [ ! "$(find_mtd_index "$CI_UBIPART")" ] && CI_UBIPART="rootfs"
+
+       case "$file_type" in
+               "ubi")          nand_upgrade_ubinized $1;;
+               "ubifs")        nand_upgrade_ubifs $1;;
+               *)              nand_upgrade_tar $1;;
+       esac
+}
+
 nand_upgrade_stage2() {
        [ $1 = "nand" ] && {
                [ -f "$2" ] && {
@@ -271,24 +333,44 @@ nand_upgrade_stage1() {
                [ "$SAVE_CONFIG" != 1 -a -f "$CONF_TAR" ] &&
                        rm $CONF_TAR
 
-               ubus call system nandupgrade "{\"path\": \"$path\" }"
+               ubus call system nandupgrade "{\"prefix\": \"$RAM_ROOT\", \"path\": \"$path\" }"
                exit 0
        }
 }
-append sysupgrade_pre_upgrade nand_upgrade_stage1
 
+# Check if passed file is a valid one for NAND sysupgrade. Currently it accepts
+# 3 types of files:
+# 1) UBI - should contain an ubinized image, header is checked for the proper
+#    MAGIC
+# 2) UBIFS - should contain UBIFS partition that will replace "rootfs" volume,
+#    header is checked for the proper MAGIC
+# 3) TAR - archive has to include "sysupgrade-BOARD" directory with a non-empty
+#    "CONTROL" file (at this point its content isn't verified)
+#
+# You usually want to call this function in platform_check_image.
+#
+# $(1): board name, used in case of passing TAR file
+# $(2): file to be checked
 nand_do_platform_check() {
        local board_name="$1"
        local tar_file="$2"
        local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
-       
-       [ "$control_length" = 0 -a "$(identify $2)" != "ubi" ] && {
+       local file_type="$(identify $2)"
+
+       [ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
                echo "Invalid sysupgrade file."
                return 1
        }
 
-       echo -n $2 > /tmp/sysupgrade-nand-path
-       cp /sbin/upgraded /tmp/
-
        return 0
 }
+
+# Start NAND upgrade process
+#
+# $(1): file to be used for upgrade
+nand_do_upgrade() {
+       echo -n $1 > /tmp/sysupgrade-nand-path
+       install_bin /sbin/upgraded
+       ln -s "$RAM_ROOT"/sbin/upgraded /tmp/upgraded
+       nand_upgrade_stage1
+}