29f476345118298801e6e6ff07df7ddf6d33b371
[openwrt/openwrt.git] / target / linux / ipq806x / base-files / lib / upgrade / linksys.sh
1 #
2 # Copyright (C) 2014-2015 OpenWrt.org
3 #
4
5 linksys_get_target_firmware() {
6
7 local cur_boot_part mtd_ubi0
8
9 cur_boot_part=$(/usr/sbin/fw_printenv -n boot_part)
10 if [ -z "${cur_boot_part}" ] ; then
11 mtd_ubi0=$(cat /sys/devices/virtual/ubi/ubi0/mtd_num)
12 case $(egrep ^mtd${mtd_ubi0}: /proc/mtd | cut -d '"' -f 2) in
13 kernel1|rootfs1)
14 cur_boot_part=1
15 ;;
16 kernel2|rootfs2)
17 cur_boot_part=2
18 ;;
19 esac
20 >&2 printf "Current boot_part='%s' selected from ubi0/mtd_num='%s'" \
21 "${cur_boot_part}" "${mtd_ubi0}"
22 fi
23
24 case $cur_boot_part in
25 1)
26 fw_setenv -s - <<-EOF
27 boot_part 2
28 auto_recovery yes
29 EOF
30 printf "kernel2"
31 return
32 ;;
33 2)
34 fw_setenv -s - <<-EOF
35 boot_part 1
36 auto_recovery yes
37 EOF
38 printf "kernel1"
39 return
40 ;;
41 *)
42 return
43 ;;
44 esac
45 }
46
47 platform_do_upgrade_linksys() {
48 local magic_long="$(get_magic_long "$1")"
49
50 mkdir -p /var/lock
51 local part_label="$(linksys_get_target_firmware)"
52 touch /var/lock/fw_printenv.lock
53
54 if [ ! -n "$part_label" ]
55 then
56 echo "cannot find target partition"
57 exit 1
58 fi
59
60 local target_mtd=$(find_mtd_part $part_label)
61
62 [ "$magic_long" = "73797375" ] && {
63 CI_KERNPART="$part_label"
64 if [ "$part_label" = "kernel1" ]
65 then
66 CI_UBIPART="rootfs1"
67 else
68 CI_UBIPART="rootfs2"
69 fi
70
71
72 # remove "squashfs" vol (in case we are flashing over a stock image, which is also UBI)
73
74 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
75 if [ ! "$mtdnum" ]; then
76 echo "cannot find ubi mtd partition $CI_UBIPART"
77 return 1
78 fi
79
80 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
81 if [ ! "$ubidev" ]; then
82 ubiattach -m "$mtdnum"
83 sync
84 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
85 fi
86
87 if [ "$ubidev" ]; then
88
89 local squash_ubivol="$( nand_find_volume $ubidev squashfs )"
90
91 # kill volume
92 [ "$squash_ubivol" ] && ubirmvol /dev/$ubidev -N squashfs || true
93 fi
94
95
96 # complete std upgrade
97 nand_upgrade_tar "$1"
98 }
99 [ "$magic_long" = "27051956" ] && {
100 get_image "$1" | mtd write - $part_label
101 }
102 }