ipq40xx: add support for Linksys EA6350v3
[openwrt/openwrt.git] / target / linux / ipq40xx / base-files / lib / upgrade / linksys.sh
1 linksys_get_target_firmware() {
2 cur_boot_part=$(/usr/sbin/fw_printenv -n boot_part)
3 target_firmware=""
4 if [ "$cur_boot_part" = "1" ]; then
5 # current primary boot - update alt boot
6 target_firmware="alt_kernel"
7 fw_setenv boot_part 2
8 # In the Linksys EA6350v3, it is enough to set the boot_part as the boot command line is
9 # bootcmd=if test $boot_part = 1; then run bootpart1; else run bootpart2; fi
10 # - You probably want to use that if your device's uboot does not eval bootcmd
11 #fw_setenv bootcmd "run altnandboot"
12 elif [ "$cur_boot_part" = "2" ]; then
13 # current alt boot - update primary boot
14 target_firmware="kernel"
15 fw_setenv boot_part 1
16 #fw_setenv bootcmd "run nandboot"
17 fi
18
19 # re-enable recovery so we get back if the new firmware is broken
20 fw_setenv auto_recovery yes
21 # see /etc/init.d/zlinksys_recovery
22
23 echo "$target_firmware"
24 }
25
26 linksys_get_root_magic() {
27 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
28 }
29
30 platform_do_upgrade_linksys() {
31 local magic_long="$(get_magic_long "$1")"
32
33 mkdir -p /var/lock
34 local part_label="$(linksys_get_target_firmware)"
35 touch /var/lock/fw_printenv.lock
36
37 if [ ! -n "$part_label" ]; then
38 echo "cannot find target partition"
39 exit 1
40 fi
41
42 local target_mtd=$(find_mtd_part $part_label)
43
44 [ "$magic_long" = "73797375" ] && {
45 CI_KERNPART="$part_label"
46 if [ "$part_label" = "kernel" ]; then
47 CI_UBIPART="rootfs"
48 else
49 CI_UBIPART="alt_rootfs"
50 fi
51
52 # remove "squashfs" vol (in case we are flashing over a stock image, which is also UBI)
53
54 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
55 if [ ! "$mtdnum" ]; then
56 echo "cannot find ubi mtd partition $CI_UBIPART"
57 return 1
58 fi
59
60 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
61 if [ ! "$ubidev" ]; then
62 ubiattach -m "$mtdnum"
63 sync
64 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
65 fi
66
67 if [ "$ubidev" ]; then
68 local squash_ubivol="$( nand_find_volume $ubidev squashfs )"
69 # kill volume
70 [ "$squash_ubivol" ] && ubirmvol /dev/$ubidev -N squashfs || true
71 fi
72
73 # complete std upgrade
74 nand_upgrade_tar "$1"
75 }
76 [ "$magic_long" = "27051956" ] && {
77 # This magic is for a uImage (which is a sysupgrade image)
78 # check firmwares' rootfs types
79 local oldroot="$(linksys_get_root_magic $target_mtd)"
80 local newroot="$(linksys_get_root_magic "$1")"
81
82 if [ "$newroot" = "55424923" -a "$oldroot" = "55424923" ]; then
83 # we're upgrading from a firmware with UBI to one with UBI
84 # erase everything to be safe
85 # - Is that really needed? Won't remove (or comment) the if, because it may be needed in a future device.
86 #mtd erase $part_label
87 #get_image "$1" | mtd -n write - $part_label
88 echo "writing \"$1\" UBI image to \"$part_label\" (UBI)..."
89 get_image "$1" | mtd write - $part_label
90 else
91 echo "writing \"$1\" image to \"$part_label\""
92 get_image "$1" | mtd write - $part_label
93 fi
94 }
95 }