ipq40xx: add support for Linksys WHW03 V1
[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/class/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_is_factory_image() {
51 local board=$(board_name)
52 board=${board##*,}
53
54 # check matching footer signature
55 tail -c 256 $1 | grep -q -i "\.LINKSYS\.........${board}"
56 }
57
58 platform_do_upgrade_linksys() {
59 local magic_long="$(get_magic_long "$1")"
60
61 local rm_oem_fw_vols="squashfs ubifs" # from OEM [alt_]rootfs UBI
62 local vol
63
64 mkdir -p /var/lock
65 local part_label="$(linksys_get_target_firmware)"
66 touch /var/lock/fw_printenv.lock
67
68 if [ -z "$part_label" ]; then
69 echo "cannot find target partition"
70 exit 1
71 fi
72
73 local target_mtd=$(find_mtd_part "$part_label")
74
75 [ "$magic_long" = "73797375" ] && {
76 CI_KERNPART="$part_label"
77 if [ "$part_label" = "kernel" ]; then
78 CI_UBIPART="rootfs"
79 else
80 CI_UBIPART="alt_rootfs"
81 fi
82
83 local mtdnum="$(find_mtd_index "$CI_UBIPART")"
84 if [ ! "$mtdnum" ]; then
85 echo "cannot find ubi mtd partition $CI_UBIPART"
86 return 1
87 fi
88
89 local ubidev="$(nand_find_ubi "$CI_UBIPART")"
90 if [ ! "$ubidev" ]; then
91 ubiattach -m "$mtdnum"
92 sync
93 ubidev="$(nand_find_ubi "$CI_UBIPART")"
94 fi
95
96 if [ "$ubidev" ]; then
97 for vol in $rm_oem_fw_vols; do
98 ubirmvol "/dev/$ubidev" -N "$vol" 2>/dev/null
99 done
100 fi
101
102 # complete std upgrade
103 if nand_upgrade_tar "$1" ; then
104 nand_do_upgrade_success
105 else
106 nand_do_upgrade_failed
107 fi
108
109 }
110
111 [ "$magic_long" = "27051956" ] && {
112 echo "writing \"$1\" image to \"$part_label\""
113 get_image "$1" | mtd write - "$part_label"
114 }
115
116 [ "$magic_long" = "d00dfeed" ] && {
117 if ! linksys_is_factory_image "$1"; then
118 echo "factory image doesn't match device"
119 return 1
120 fi
121
122 echo "writing \"$1\" factory image to \"$part_label\""
123 get_image "$1" | mtd -e "$part_label" write - "$part_label"
124 }
125 }
126
127 linksys_get_cmdline_rootfs_device() {
128 if read cmdline < /proc/cmdline; then
129 case "$cmdline" in
130 *root=*)
131 local str="${cmdline##*root=}"
132 echo "${str%% *}"
133 return
134 ;;
135 esac
136 fi
137 return 1
138 }
139
140 linksys_get_current_boot_part_emmc() {
141 local boot_part="$(fw_printenv -n boot_part)"
142 if [ "$boot_part" = 1 ] || [ "$boot_part" = 2 ]; then
143 v "Current boot_part=$boot_part selected from bootloader environment"
144 else
145 local rootfs_device="$(linksys_get_cmdline_rootfs_device)"
146 if [ "$rootfs_device" = "$(find_mmc_part "rootfs")" ]; then
147 boot_part=1
148 elif [ "$rootfs_device" = "$(find_mmc_part "alt_rootfs")" ]; then
149 boot_part=2
150 else
151 v "Could not determine current boot_part"
152 return 1
153 fi
154 v "Current boot_part=$boot_part selected from cmdline rootfs=$rootfs_device"
155 fi
156 echo $boot_part
157 }
158
159 linksys_set_target_partitions_emmc() {
160 local current_boot_part="$1"
161
162 if [ "$current_boot_part" = 1 ]; then
163 CI_KERNPART="alt_kernel"
164 CI_ROOTPART="alt_rootfs"
165 fw_setenv -s - <<-EOF
166 boot_part 2
167 auto_recovery yes
168 EOF
169 elif [ "$current_boot_part" = 2 ]; then
170 CI_KERNPART="kernel"
171 CI_ROOTPART="rootfs"
172 fw_setenv -s - <<-EOF
173 boot_part 1
174 auto_recovery yes
175 EOF
176 else
177 v "Could not set target eMMC partitions"
178 return 1
179 fi
180
181 v "Target eMMC partitions: $CI_KERNPART, $CI_ROOTPART"
182 }
183
184 platform_do_upgrade_linksys_emmc() {
185 local file="$1"
186
187 mkdir -p /var/lock
188 local current_boot_part="$(linksys_get_current_boot_part_emmc)"
189 linksys_set_target_partitions_emmc "$current_boot_part" || exit 1
190 touch /var/lock/fw_printenv.lock
191
192 emmc_do_upgrade "$file"
193 }