ar71xx: user space support for the TL-WA7510N
[15.05/openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/ar71xx.sh
6
7 PART_NAME=firmware
8 RAMFS_COPY_DATA=/lib/ar71xx.sh
9
10 CI_BLKSZ=65536
11 CI_LDADR=0x80060000
12
13 platform_find_partitions() {
14         local first dev size erasesize name
15         while read dev size erasesize name; do
16                 name=${name#'"'}; name=${name%'"'}
17                 case "$name" in
18                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
19                                 if [ -z "$first" ]; then
20                                         first="$name"
21                                 else
22                                         echo "$erasesize:$first:$name"
23                                         break
24                                 fi
25                         ;;
26                 esac
27         done < /proc/mtd
28 }
29
30 platform_find_kernelpart() {
31         local part
32         for part in "${1%:*}" "${1#*:}"; do
33                 case "$part" in
34                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
35                                 echo "$part"
36                                 break
37                         ;;
38                 esac
39         done
40 }
41
42 platform_do_upgrade_combined() {
43         local partitions=$(platform_find_partitions)
44         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
45         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
46         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
47         local kern_blocks=$(($kern_length / $CI_BLKSZ))
48         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
49
50         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
51            [ ${kern_blocks:-0} -gt 0 ] && \
52            [ ${root_blocks:-0} -gt ${kern_blocks:-0} ] && \
53            [ ${erase_size:-0} -gt 0 ];
54         then
55                 local append=""
56                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
57
58                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
59                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
60                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
61         fi
62 }
63
64 tplink_get_image_hwid() {
65         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
66 }
67
68 tplink_get_image_boot_size() {
69         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
70 }
71
72 platform_check_image() {
73         local board=$(ar71xx_board_name)
74         local magic="$(get_magic_word "$1")"
75         local magic_long="$(get_magic_long "$1")"
76
77         [ "$ARGC" -gt 1 ] && return 1
78
79         case "$board" in
80         all0315n | \
81         all0258n | \
82         cap4200ag)
83                 platform_check_image_allnet "$1" && return 0
84                 return 1
85                 ;;
86         alfa-ap96 | \
87         alfa-nx | \
88         ap113 | \
89         ap121 | \
90         ap121-mini | \
91         ap136 | \
92         ap96 | \
93         db120 | \
94         hornet-ub | \
95         zcn-1523h-2 | \
96         zcn-1523h-5)
97                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
98                         echo "Invalid image type."
99                         return 1
100                 }
101                 return 0
102                 ;;
103         ap81 | \
104         ap83 | \
105         dir-600-a1 | \
106         dir-615-c1 | \
107         dir-615-e4 | \
108         ew-dorin | \
109         ew-dorin-router | \
110         mzk-w04nu | \
111         mzk-w300nh | \
112         tew-632brp | \
113         tew-712br | \
114         wrt400n | \
115         airrouter | \
116         bullet-m | \
117         nanostation-m | \
118         rocket-m | \
119         rw2458n | \
120         wzr-hp-g300nh2 | \
121         wzr-hp-g300nh | \
122         wzr-hp-g450h | \
123         wzr-hp-ag300h | \
124         whr-g301n | \
125         whr-hp-g300n | \
126         whr-hp-gn | \
127         wlae-ag300n | \
128         nbg460n_550n_550nh | \
129         unifi )
130                 [ "$magic" != "2705" ] && {
131                         echo "Invalid image type."
132                         return 1
133                 }
134                 return 0
135                 ;;
136
137         dir-825-b1 | \
138         tew-673gru)
139                 dir825b_check_image "$1" && return 0
140                 ;;
141
142         om2p | \
143         om2p-hs | \
144         om2p-lc)
145                 platform_check_image_om2p "$magic_long" "$1" && return 0
146                 return 1
147                 ;;
148         tl-mr11u | \
149         tl-mr3020 | \
150         tl-mr3040 | \
151         tl-mr3220 | \
152         tl-mr3420 | \
153         tl-wa7510n | \
154         tl-wa901nd | \
155         tl-wa901nd-v2 | \
156         tl-wdr4300 | \
157         tl-wr703n | \
158         tl-wr741nd | \
159         tl-wr741nd-v4 | \
160         tl-wr841n-v1 | \
161         tl-wr841n-v7 | \
162         tl-wr841n-v8 | \
163         tl-wr941nd | \
164         tl-wr1041n-v2 | \
165         tl-wr1043nd | \
166         tl-wr2543n)
167                 [ "$magic" != "0100" ] && {
168                         echo "Invalid image type."
169                         return 1
170                 }
171
172                 local hwid
173                 local imageid
174
175                 hwid=$(tplink_get_hwid)
176                 imageid=$(tplink_get_image_hwid "$1")
177
178                 [ "$hwid" != "$imageid" ] && {
179                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
180                         return 1
181                 }
182
183                 local boot_size
184
185                 boot_size=$(tplink_get_image_boot_size "$1")
186                 [ "$boot_size" != "00000000" ] && {
187                         echo "Invalid image, it contains a bootloader."
188                         return 1
189                 }
190
191                 return 0
192                 ;;
193         wndr3700)
194                 local hw_magic
195
196                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
197                 [ "$magic_long" != "$hw_magic" ] && {
198                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
199                         return 1
200                 }
201                 return 0
202                 ;;
203         wrt160nl)
204                 [ "$magic" != "4e4c" ] && {
205                         echo "Invalid image type."
206                         return 1
207                 }
208                 return 0
209                 ;;
210         routerstation | \
211         routerstation-pro | \
212         ls-sr71 | \
213         pb42 | \
214         pb44 | \
215         all0305 | \
216         eap7660d | \
217         ja76pf | \
218         ja76pf2 | \
219         jwap003)
220                 [ "$magic" != "4349" ] && {
221                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
222                         return 1
223                 }
224
225                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
226                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
227
228                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
229                         return 0
230                 else
231                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
232                         return 1
233                 fi
234                 return 0
235                 ;;
236         esac
237
238         echo "Sysupgrade is not yet supported on $board."
239         return 1
240 }
241
242 platform_do_upgrade() {
243         local board=$(ar71xx_board_name)
244
245         case "$board" in
246         routerstation | \
247         routerstation-pro | \
248         ls-sr71 | \
249         all0305 | \
250         eap7660d | \
251         pb42 | \
252         pb44 | \
253         ja76pf | \
254         ja76pf2 | \
255         jwap003)
256                 platform_do_upgrade_combined "$ARGV"
257                 ;;
258         all0258n )
259                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
260                 ;;
261         all0315n )
262                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
263                 ;;
264         cap4200ag)
265                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
266                 ;;
267         dir-825-b1 |\
268         tew-673gru)
269                 platform_do_upgrade_dir825b "$ARGV"
270                 ;;
271         om2p | \
272         om2p-hs | \
273         om2p-lc)
274                 platform_do_upgrade_om2p "$ARGV"
275                 ;;
276         *)
277                 default_do_upgrade "$ARGV"
278                 ;;
279         esac
280 }
281
282 disable_watchdog() {
283         killall watchdog
284         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
285                 echo 'Could not disable watchdog'
286                 return 1
287         }
288 }
289
290 append sysupgrade_pre_upgrade disable_watchdog