fc403332bd38fc521a056ab85783abd90629a1cf
[openwrt/openwrt.git] / target / linux / mvebu / 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 fw_setenv bootcmd "run altnandboot"
14 elif [ "$cur_boot_part" = "2" ]
15 then
16 # current alt boot - update primary boot
17 target_firmware="kernel1"
18 fw_setenv boot_part 1
19 fw_setenv bootcmd "run nandboot"
20 fi
21
22 # re-enable recovery so we get back if the new firmware is broken
23 fw_setenv auto_recovery yes
24
25 echo "$target_firmware"
26 }
27
28 linksys_get_root_magic() {
29 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
30 }
31
32 platform_do_upgrade_linksys() {
33 local magic_long="$(get_magic_long "$1")"
34
35 mkdir -p /var/lock
36 local part_label="$(linksys_get_target_firmware)"
37 touch /var/lock/fw_printenv.lock
38
39 if [ ! -n "$part_label" ]
40 then
41 echo "cannot find target partition"
42 exit 1
43 fi
44
45 local target_mtd=$(find_mtd_part $part_label)
46
47 [ "$magic_long" = "73797375" ] && {
48 CI_KERNPART="$part_label"
49 if [ "$part_label" = "kernel1" ]
50 then
51 CI_UBIPART="rootfs1"
52 else
53 CI_UBIPART="rootfs2"
54 fi
55
56 nand_upgrade_tar "$1"
57 }
58 [ "$magic_long" = "27051956" ] && {
59 # check firmwares' rootfs types
60 local target_mtd=$(find_mtd_part $part_label)
61 local oldroot="$(linksys_get_root_magic $target_mtd)"
62 local newroot="$(linksys_get_root_magic "$1")"
63
64 if [ "$newroot" = "55424923" -a "$oldroot" = "55424923" ]
65 # we're upgrading from a firmware with UBI to one with UBI
66 then
67 # erase everything to be safe
68 mtd erase $part_label
69 get_image "$1" | mtd -n write - $part_label
70 else
71 get_image "$1" | mtd write - $part_label
72 fi
73 }
74 }
75
76 linksys_preupgrade() {
77 export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/sbin/fw_printenv /usr/sbin/fw_setenv"
78 export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /bin/mkdir /bin/touch"
79 export RAMFS_COPY_DATA="${RAMFS_COPY_DATA} /etc/fw_env.config /var/lock/fw_printenv.lock"
80
81 [ -f /tmp/sysupgrade.tgz ] && {
82 cp /tmp/sysupgrade.tgz /tmp/syscfg/sysupgrade.tgz
83 }
84 }
85
86 append sysupgrade_pre_upgrade linksys_preupgrade