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