ipq40xx: Add support for Linksys MR8300 (Dallas)
[openwrt/openwrt.git] / target / linux / ipq40xx / base-files / lib / upgrade / linksys.sh
1 linksys_get_target_firmware() {
2 local cur_boot_part mtd_ubi0
3
4 cur_boot_part="$(/usr/sbin/fw_printenv -n boot_part)"
5 if [ -z "${cur_boot_part}" ]; then
6 mtd_ubi0=$(cat /sys/devices/virtual/ubi/ubi0/mtd_num)
7 case "$(grep -E "^mtd${mtd_ubi0}:" /proc/mtd | cut -d '"' -f 2)" in
8 kernel|rootfs)
9 cur_boot_part=1
10 ;;
11 alt_kernel|alt_rootfs)
12 cur_boot_part=2
13 ;;
14 esac
15 >&2 printf "Current boot_part='%s' selected from ubi0/mtd_num='%s'" \
16 "${cur_boot_part}" "${mtd_ubi0}"
17 fi
18
19 # OEM U-Boot for EA6350v3, EA8300 and MR8300; bootcmd=
20 # if test $auto_recovery = no;
21 # then bootipq;
22 # elif test $boot_part = 1;
23 # then run bootpart1;
24 # else run bootpart2;
25 # fi
26
27 case "$cur_boot_part" in
28 1)
29 fw_setenv -s - <<-EOF
30 boot_part 2
31 auto_recovery yes
32 EOF
33 printf "alt_kernel"
34 return
35 ;;
36 2)
37 fw_setenv -s - <<-EOF
38 boot_part 1
39 auto_recovery yes
40 EOF
41 printf "kernel"
42 return
43 ;;
44 *)
45 return
46 ;;
47 esac
48 }
49
50 linksys_get_root_magic() {
51 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
52 }
53
54 platform_do_upgrade_linksys() {
55 local magic_long="$(get_magic_long "$1")"
56
57 local rm_oem_fw_vols="squashfs ubifs" # from OEM [alt_]rootfs UBI
58 local vol
59
60 mkdir -p /var/lock
61 local part_label="$(linksys_get_target_firmware)"
62 touch /var/lock/fw_printenv.lock
63
64 if [ -z "$part_label" ]; then
65 echo "cannot find target partition"
66 exit 1
67 fi
68
69 local target_mtd=$(find_mtd_part "$part_label")
70
71 [ "$magic_long" = "73797375" ] && {
72 CI_KERNPART="$part_label"
73 if [ "$part_label" = "kernel" ]; then
74 CI_UBIPART="rootfs"
75 else
76 CI_UBIPART="alt_rootfs"
77 fi
78
79 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
80 if [ ! "$mtdnum" ]; then
81 echo "cannot find ubi mtd partition $CI_UBIPART"
82 return 1
83 fi
84
85 local ubidev="$(nand_find_ubi "$CI_UBIPART")"
86 if [ ! "$ubidev" ]; then
87 ubiattach -m "$mtdnum"
88 sync
89 ubidev="$(nand_find_ubi "$CI_UBIPART")"
90 fi
91
92 if [ "$ubidev" ]; then
93 for vol in $rm_oem_fw_vols; do
94 ubirmvol "/dev/$ubidev" -N "$vol" 2>/dev/null
95 done
96 fi
97
98 # complete std upgrade
99 nand_upgrade_tar "$1"
100 }
101
102 [ "$magic_long" = "27051956" ] && {
103 # This magic is for a uImage (which is a sysupgrade image)
104 # check firmwares' rootfs types
105 local oldroot="$(linksys_get_root_magic "$target_mtd")"
106 local newroot="$(linksys_get_root_magic "$1")"
107
108 if [ "$newroot" = "55424923" ] && [ "$oldroot" = "55424923" ]; then
109 # we're upgrading from a firmware with UBI to one with UBI
110 # erase everything to be safe
111 # - Is that really needed? Won't remove (or comment) the if,
112 # because it may be needed in a future device.
113 #mtd erase $part_label
114 #get_image "$1" | mtd -n write - $part_label
115 echo "writing \"$1\" UBI image to \"$part_label\" (UBI)..."
116 get_image "$1" | mtd write - "$part_label"
117 else
118 echo "writing \"$1\" image to \"$part_label\""
119 get_image "$1" | mtd write - "$part_label"
120 fi
121 }
122 }