mtd: base-files: Unify dual-firmware devices (Linksys)
[openwrt/staging/dedeckeh.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 cur_boot_part=`/usr/sbin/fw_printenv -n boot_part`
25
26 case $cur_boot_part in
27 1)
28 fw_setenv -s - <<-EOF
29 boot_part 2
30 auto_recovery yes
31 EOF
32 printf "kernel2"
33 return
34 ;;
35 2)
36 fw_setenv -s - <<-EOF
37 boot_part 1
38 auto_recovery yes
39 EOF
40 printf "kernel1"
41 return
42 ;;
43 *)
44 return
45 ;;
46 esac
47 }
48
49 linksys_get_root_magic() {
50 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
51 }
52
53 platform_do_upgrade_linksys() {
54 local magic_long="$(get_magic_long "$1")"
55
56 mkdir -p /var/lock
57 local part_label="$(linksys_get_target_firmware)"
58 touch /var/lock/fw_printenv.lock
59
60 if [ ! -n "$part_label" ]
61 then
62 echo "cannot find target partition"
63 exit 1
64 fi
65
66 local target_mtd=$(find_mtd_part $part_label)
67
68 [ "$magic_long" = "73797375" ] && {
69 CI_KERNPART="$part_label"
70 if [ "$part_label" = "kernel1" ]
71 then
72 CI_UBIPART="rootfs1"
73 else
74 CI_UBIPART="rootfs2"
75 fi
76
77
78 # remove "squashfs" vol (in case we are flashing over a stock image, which is also UBI)
79
80 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
81 if [ ! "$mtdnum" ]; then
82 echo "cannot find ubi mtd partition $CI_UBIPART"
83 return 1
84 fi
85
86 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
87 if [ ! "$ubidev" ]; then
88 ubiattach -m "$mtdnum"
89 sync
90 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
91 fi
92
93 if [ "$ubidev" ]; then
94
95 local squash_ubivol="$( nand_find_volume $ubidev squashfs )"
96
97 # kill volume
98 [ "$squash_ubivol" ] && ubirmvol /dev/$ubidev -N squashfs || true
99 fi
100
101
102 # complete std upgrade
103 nand_upgrade_tar "$1"
104 }
105 [ "$magic_long" = "27051956" ] && {
106 # check firmwares' rootfs types
107 local oldroot="$(linksys_get_root_magic $target_mtd)"
108 local newroot="$(linksys_get_root_magic "$1")"
109
110 if [ "$newroot" = "55424923" -a "$oldroot" = "55424923" ]
111 # we're upgrading from a firmware with UBI to one with UBI
112 then
113 # erase everything to be safe
114 mtd erase $part_label
115 get_image "$1" | mtd -n write - $part_label
116 else
117 get_image "$1" | mtd write - $part_label
118 fi
119 }
120 }