0234ce0a51a0a92dcd719102f83e19686442574a
[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 cur_boot_part=`/usr/sbin/fw_printenv -n boot_part`
7 target_firmware=""
8 if [ "$cur_boot_part" = "1" ]
9 then
10 # current primary boot - update alt boot
11 target_firmware="kernel2"
12 fw_setenv boot_part 2
13 #In EA8500 bootcmd is always "bootipq", so don't change
14 #fw_setenv bootcmd "run altnandboot"
15 elif [ "$cur_boot_part" = "2" ]
16 then
17 # current alt boot - update primary boot
18 target_firmware="kernel1"
19 fw_setenv boot_part 1
20 #In EA8500 bootcmd is always "bootipq", so don't change
21 #fw_setenv bootcmd "run nandboot"
22 fi
23
24 # re-enable recovery so we get back if the new firmware is broken
25 fw_setenv auto_recovery yes
26
27 echo "$target_firmware"
28 }
29
30 linksys_get_root_magic() {
31 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
32 }
33
34 platform_do_upgrade_linksys() {
35 local magic_long="$(get_magic_long "$1")"
36
37 mkdir -p /var/lock
38 local part_label="$(linksys_get_target_firmware)"
39 touch /var/lock/fw_printenv.lock
40
41 if [ ! -n "$part_label" ]
42 then
43 echo "cannot find target partition"
44 exit 1
45 fi
46
47 local target_mtd=$(find_mtd_part $part_label)
48
49 [ "$magic_long" = "73797375" ] && {
50 CI_KERNPART="$part_label"
51 if [ "$part_label" = "kernel1" ]
52 then
53 CI_UBIPART="rootfs1"
54 else
55 CI_UBIPART="rootfs2"
56 fi
57
58
59 # remove "squashfs" vol (in case we are flashing over a stock image, which is also UBI)
60
61 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
62 if [ ! "$mtdnum" ]; then
63 echo "cannot find ubi mtd partition $CI_UBIPART"
64 return 1
65 fi
66
67 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
68 if [ ! "$ubidev" ]; then
69 ubiattach -m "$mtdnum"
70 sync
71 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
72 fi
73
74 if [ "$ubidev" ]; then
75
76 local squash_ubivol="$( nand_find_volume $ubidev squashfs )"
77
78 # kill volume
79 [ "$squash_ubivol" ] && ubirmvol /dev/$ubidev -N squashfs || true
80 fi
81
82
83 # complete std upgrade
84 nand_upgrade_tar "$1"
85 }
86 [ "$magic_long" = "27051956" ] && {
87 # check firmwares' rootfs types
88 local oldroot="$(linksys_get_root_magic $target_mtd)"
89 local newroot="$(linksys_get_root_magic "$1")"
90
91 if [ "$newroot" = "55424923" -a "$oldroot" = "55424923" ]
92 # we're upgrading from a firmware with UBI to one with UBI
93 then
94 # erase everything to be safe
95 mtd erase $part_label
96 get_image "$1" | mtd -n write - $part_label
97 else
98 get_image "$1" | mtd write - $part_label
99 fi
100 }
101 }