mtd: base-files: Unify dual-firmware devices (Linksys)
[openwrt/openwrt.git] / target / linux / kirkwood / base-files / lib / upgrade / linksys.sh
1 #
2 # Copyright (C) 2014 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 kernel|rootfs)
14 cur_boot_part=1
15 ;;
16 alt_kernel|alt_rootfs)
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 case $cur_boot_part in
25 1)
26 fw_setenv -s - <<-EOF
27 boot_part 2
28 bootcmd "run altnandboot"
29 EOF
30 printf "kernel2"
31 return
32 ;;
33 2)
34 fw_setenv -s - <<-EOF
35 boot_part 1
36 bootcmd "run nandboot"
37 EOF
38 printf "kernel1"
39 return
40 ;;
41 *)
42 return
43 ;;
44 esac
45 }
46
47 linksys_get_root_magic() {
48 (get_image "$@" | dd skip=786432 bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
49 }
50
51 platform_do_upgrade_linksys() {
52 local magic_long="$(get_magic_long "$1")"
53
54 mkdir -p /var/lock
55 local part_label="$(linksys_get_target_firmware)"
56 touch /var/lock/fw_printenv.lock
57
58 if [ ! -n "$part_label" ]
59 then
60 echo "cannot find target partition"
61 exit 1
62 fi
63
64 local target_mtd=$(find_mtd_part $part_label)
65
66 [ "$magic_long" = "73797375" ] && {
67 CI_KERNPART="$part_label"
68 if [ "$part_label" = "kernel1" ]
69 then
70 CI_UBIPART="rootfs1"
71 else
72 CI_UBIPART="rootfs2"
73 fi
74
75 nand_upgrade_tar "$1"
76 }
77 [ "$magic_long" = "27051956" ] && {
78 # check firmwares' rootfs types
79 local target_mtd=$(find_mtd_part $part_label)
80 local oldroot="$(linksys_get_root_magic $target_mtd)"
81 local newroot="$(linksys_get_root_magic "$1")"
82
83 if [ "$newroot" = "55424923" -a "$oldroot" = "55424923" ]
84 # we're upgrading from a firmware with UBI to one with UBI
85 then
86 # erase everything to be safe
87 mtd erase $part_label
88 get_image "$1" | mtd -n write - $part_label
89 else
90 get_image "$1" | mtd write - $part_label
91 fi
92 }
93 }