ar71xx: add GL-Domino Pi support V3
[openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/functions/system.sh
6 . /lib/ar71xx.sh
7
8 PART_NAME=firmware
9 RAMFS_COPY_DATA=/lib/ar71xx.sh
10
11 CI_BLKSZ=65536
12 CI_LDADR=0x80060000
13
14 platform_find_partitions() {
15         local first dev size erasesize name
16         while read dev size erasesize name; do
17                 name=${name#'"'}; name=${name%'"'}
18                 case "$name" in
19                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
20                                 if [ -z "$first" ]; then
21                                         first="$name"
22                                 else
23                                         echo "$erasesize:$first:$name"
24                                         break
25                                 fi
26                         ;;
27                 esac
28         done < /proc/mtd
29 }
30
31 platform_find_kernelpart() {
32         local part
33         for part in "${1%:*}" "${1#*:}"; do
34                 case "$part" in
35                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
36                                 echo "$part"
37                                 break
38                         ;;
39                 esac
40         done
41 }
42
43 platform_do_upgrade_combined() {
44         local partitions=$(platform_find_partitions)
45         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
46         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
47         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
48         local kern_blocks=$(($kern_length / $CI_BLKSZ))
49         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
50
51         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
52            [ ${kern_blocks:-0} -gt 0 ] && \
53            [ ${root_blocks:-0} -gt 0 ] && \
54            [ ${erase_size:-0} -gt 0 ];
55         then
56                 local append=""
57                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
58
59                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
60                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
61                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
62         fi
63 }
64
65 tplink_get_image_hwid() {
66         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
67 }
68
69 tplink_get_image_boot_size() {
70         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
71 }
72
73 tplink_pharos_check_image() {
74         local magic_long="$(get_magic_long "$1")"
75         [ "$magic_long" != "7f454c46" ] && {
76                 echo "Invalid image magic '$magic_long'"
77                 return 1
78         }
79
80         local model_string="$(tplink_pharos_get_model_string)"
81         local line
82
83         # Here $1 is given to dd directly instead of get_image as otherwise the skip
84         # will take almost a second (as dd can't seek then)
85         #
86         # This will fail if the image isn't local, but that's fine: as the
87         # read loop won't be executed at all, it will return true, so the image
88         # is accepted (loading the first 1.5M of a remote image for this check seems
89         # a bit extreme)
90         dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
91                 [ "$line" == "$model_string" ] && break
92         done || {
93                 echo "Unsupported image (model not in support-list)"
94                 return 1
95         }
96
97         return 0
98 }
99
100 seama_get_type_magic() {
101         get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
102 }
103
104 cybertan_get_image_magic() {
105         get_image "$@" | dd bs=8 count=1 skip=0  2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
106 }
107
108 cybertan_check_image() {
109         local magic="$(cybertan_get_image_magic "$1")"
110         local fw_magic="$(cybertan_get_hw_magic)"
111
112         [ "$fw_magic" != "$magic" ] && {
113                 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
114                 return 1
115         }
116
117         return 0
118 }
119
120 platform_do_upgrade_compex() {
121         local fw_file=$1
122         local fw_part=$PART_NAME
123         local fw_mtd=$(find_mtd_part $fw_part)
124         local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
125         local fw_blocks=$(($fw_length / 65536))
126
127         if [ -n "$fw_mtd" ] &&  [ ${fw_blocks:-0} -gt 0 ]; then
128                 local append=""
129                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
130
131                 sync
132                 dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
133                         mtd $append write - "$fw_part"
134         fi
135 }
136
137 alfa_check_image() {
138         local magic_long="$(get_magic_long "$1")"
139         local fw_part_size=$(mtd_get_part_size firmware)
140
141         case "$magic_long" in
142         "27051956")
143                 [ "$fw_part_size" != "16318464" ] && {
144                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
145                         return 1
146                 }
147                 ;;
148
149         "68737173")
150                 [ "$fw_part_size" != "7929856" ] && {
151                         echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
152                         return 1
153                 }
154                 ;;
155         esac
156
157         return 0
158 }
159
160 platform_check_image() {
161         local board=$(ar71xx_board_name)
162         local magic="$(get_magic_word "$1")"
163         local magic_long="$(get_magic_long "$1")"
164
165         [ "$#" -gt 1 ] && return 1
166
167         case "$board" in
168         all0315n | \
169         all0258n | \
170         cap4200ag)
171                 platform_check_image_allnet "$1" && return 0
172                 return 1
173                 ;;
174         alfa-ap96 | \
175         alfa-nx | \
176         arduino-yun | \
177         ap113 | \
178         ap121 | \
179         ap121-mini | \
180         ap136-010 | \
181         ap136-020 | \
182         ap135-020 | \
183         ap147-010 | \
184         ap96 | \
185         bxu2000n-2-a1 | \
186         db120 | \
187         f9k1115v2 |\
188         hornet-ub | \
189         mr12 | \
190         mr16 | \
191         wpj558 | \
192         zcn-1523h-2 | \
193         zcn-1523h-5)
194                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
195                         echo "Invalid image type."
196                         return 1
197                 }
198                 return 0
199                 ;;
200         ap81 | \
201         ap83 | \
202         ap132 | \
203         cf-e316n-v2 | \
204         dgl-5500-a1 |\
205         dhp-1565-a1 |\
206         dir-505-a1 | \
207         dir-600-a1 | \
208         dir-615-c1 | \
209         dir-615-e1 | \
210         dir-615-e4 | \
211         dir-615-i1 | \
212         dir-825-c1 | \
213         dir-835-a1 | \
214         dlan-hotspot | \
215         dlan-pro-500-wp | \
216         dlan-pro-1200-ac | \
217         dragino2 | \
218         epg5000 | \
219         esr1750 | \
220         esr900 | \
221         ew-dorin | \
222         ew-dorin-router | \
223         gl-ar150 | \
224         gl-ar300 | \
225         gl-domino | \
226         hiwifi-hc6361 | \
227         hornet-ub-x2 | \
228         mzk-w04nu | \
229         mzk-w300nh | \
230         tew-632brp | \
231         tew-712br | \
232         tew-732br | \
233         wrt400n | \
234         airgateway | \
235         airgatewaypro | \
236         airrouter | \
237         bullet-m | \
238         loco-m-xw | \
239         nanostation-m | \
240         rocket-m | \
241         rocket-m-xw | \
242         rocket-m-ti | \
243         nanostation-m-xw | \
244         rw2458n | \
245         wpj531 | \
246         wndap360 | \
247         wpj344 | \
248         wzr-hp-g300nh2 | \
249         wzr-hp-g300nh | \
250         wzr-hp-g450h | \
251         wzr-hp-ag300h | \
252         wzr-450hp2 | \
253         whr-g301n | \
254         whr-hp-g300n | \
255         whr-hp-gn | \
256         wlae-ag300n | \
257         nbg460n_550n_550nh | \
258         unifi | \
259         unifi-outdoor | \
260         carambola2 | \
261         weio )
262                 [ "$magic" != "2705" ] && {
263                         echo "Invalid image type."
264                         return 1
265                 }
266                 return 0
267                 ;;
268
269         cpe510)
270                 tplink_pharos_check_image "$1" && return 0
271                 return 1
272                 ;;
273
274         bsb | \
275         dir-825-b1 | \
276         tew-673gru)
277                 dir825b_check_image "$1" && return 0
278                 ;;
279
280         mynet-rext|\
281         wrt160nl)
282                 cybertan_check_image "$1" && return 0
283                 return 1
284                 ;;
285
286         qihoo-c301 | \
287         mynet-n600 | \
288         mynet-n750)
289                 [ "$magic_long" != "5ea3a417" ] && {
290                         echo "Invalid image, bad magic: $magic_long"
291                         return 1
292                 }
293
294                 local typemagic=$(seama_get_type_magic "$1")
295                 [ "$typemagic" != "6669726d" ] && {
296                         echo "Invalid image, bad type: $typemagic"
297                         return 1
298                 }
299
300                 return 0;
301                 ;;
302         mr1750 | \
303         mr600 | \
304         mr600v2 | \
305         mr900 | \
306         mr900v2 | \
307         om2p | \
308         om2pv2 | \
309         om2p-hs | \
310         om2p-hsv2 | \
311         om2p-lc | \
312         om5p | \
313         om5p-an)
314                 platform_check_image_openmesh "$magic_long" "$1" && return 0
315                 return 1
316                 ;;
317
318         antminer-s1 | \
319         antminer-s3 | \
320         archer-c5 | \
321         archer-c7 | \
322         el-m150 | \
323         el-mini | \
324         gl-inet | \
325         mc-mac1200r | \
326         minibox-v1 |\
327         onion-omega | \
328         oolite | \
329         smart-300 | \
330         tl-mr10u | \
331         tl-mr11u | \
332         tl-mr12u | \
333         tl-mr13u | \
334         tl-mr3020 | \
335         tl-mr3040 | \
336         tl-mr3040-v2 | \
337         tl-mr3220 | \
338         tl-mr3220-v2 | \
339         tl-mr3420 | \
340         tl-mr3420-v2 | \
341         tl-wa701nd-v2 | \
342         tl-wa7210n-v2 | \
343         tl-wa7510n | \
344         tl-wa750re | \
345         tl-wa850re | \
346         tl-wa860re | \
347         tl-wa801nd-v2 | \
348         tl-wa901nd | \
349         tl-wa901nd-v2 | \
350         tl-wa901nd-v3 | \
351         tl-wdr3320-v2 | \
352         tl-wdr3500 | \
353         tl-wdr4300 | \
354         tl-wdr4900-v2 | \
355         tl-wdr6500-v2 | \
356         tl-wr703n | \
357         tl-wr710n | \
358         tl-wr720n-v3 | \
359         tl-wr741nd | \
360         tl-wr741nd-v4 | \
361         tl-wr841n-v1 | \
362         tl-wa830re-v2 | \
363         tl-wr841n-v7 | \
364         tl-wr841n-v8 | \
365         tl-wr841n-v9 | \
366         tl-wr842n-v2 | \
367         tl-wr941nd | \
368         tl-wr941nd-v5 | \
369         tl-wr941nd-v6 | \
370         tl-wr1041n-v2 | \
371         tl-wr1043nd | \
372         tl-wr1043nd-v2 | \
373         tl-wr2543n)
374                 local magic_ver="0100"
375
376                 case "$board" in
377                 tl-wdr6500-v2)
378                         magic_ver="0200"
379                         ;;
380                 esac
381
382                 [ "$magic" != "$magic_ver" ] && {
383                         echo "Invalid image type."
384                         return 1
385                 }
386
387                 local hwid
388                 local imageid
389
390                 hwid=$(tplink_get_hwid)
391                 imageid=$(tplink_get_image_hwid "$1")
392
393                 [ "$hwid" != "$imageid" ] && {
394                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
395                         return 1
396                 }
397
398                 local boot_size
399
400                 boot_size=$(tplink_get_image_boot_size "$1")
401                 [ "$boot_size" != "00000000" ] && {
402                         echo "Invalid image, it contains a bootloader."
403                         return 1
404                 }
405
406                 return 0
407                 ;;
408
409         tube2h)
410                 alfa_check_image "$1" && return 0
411                 return 1
412                 ;;
413
414         nbg6616 | \
415         unifi-outdoor-plus | \
416         uap-pro)
417                 [ "$magic_long" != "19852003" ] && {
418                         echo "Invalid image type."
419                         return 1
420                 }
421                 return 0
422                 ;;
423         wndr3700 | \
424         wnr2000-v3 | \
425         wnr612-v2 | \
426         wnr1000-v2)
427                 local hw_magic
428
429                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
430                 [ "$magic_long" != "$hw_magic" ] && {
431                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
432                         return 1
433                 }
434                 return 0
435                 ;;
436         nbg6716 | \
437         r6100 | \
438         wndr3700v4 | \
439         wndr4300 )
440                 nand_do_platform_check $board $1
441                 return $?;
442                 ;;
443         routerstation | \
444         routerstation-pro | \
445         ls-sr71 | \
446         pb42 | \
447         pb44 | \
448         all0305 | \
449         eap300v2 | \
450         eap7660d | \
451         ja76pf | \
452         ja76pf2 | \
453         jwap003 | \
454         wp543 | \
455         wpe72)
456                 [ "$magic" != "4349" ] && {
457                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
458                         return 1
459                 }
460
461                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
462                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
463
464                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
465                         return 0
466                 else
467                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
468                         return 1
469                 fi
470                 return 0
471                 ;;
472     wnr2000-v4)
473                 [ "$magic_long" != "32303034" ] && {
474                         echo "Invalid image type."
475                         return 1
476                 }
477                 return 0
478                 ;;
479
480         esac
481
482         echo "Sysupgrade is not yet supported on $board."
483         return 1
484 }
485
486 platform_pre_upgrade() {
487         local board=$(ar71xx_board_name)
488
489         case "$board" in
490         nbg6716 | \
491         r6100 | \
492         wndr3700v4 | \
493         wndr4300 )
494                 nand_do_upgrade "$1"
495                 ;;
496         esac
497 }
498
499 platform_do_upgrade() {
500         local board=$(ar71xx_board_name)
501
502         case "$board" in
503         routerstation | \
504         routerstation-pro | \
505         ls-sr71 | \
506         all0305 | \
507         eap7660d | \
508         pb42 | \
509         pb44 | \
510         ja76pf | \
511         ja76pf2 | \
512         jwap003)
513                 platform_do_upgrade_combined "$ARGV"
514                 ;;
515         wp543|\
516         wpe72)
517                 platform_do_upgrade_compex "$ARGV"
518                 ;;
519         all0258n )
520                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
521                 ;;
522         all0315n )
523                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
524                 ;;
525         eap300v2 |\
526         cap4200ag)
527                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
528                 ;;
529         dir-825-b1 |\
530         tew-673gru)
531                 platform_do_upgrade_dir825b "$ARGV"
532                 ;;
533         mr1750 | \
534         mr600 | \
535         mr600v2 | \
536         mr900 | \
537         mr900v2 | \
538         om2p | \
539         om2pv2 | \
540         om2p-hs | \
541         om2p-hsv2 | \
542         om2p-lc | \
543         om5p | \
544         om5p-an)
545                 platform_do_upgrade_openmesh "$ARGV"
546                 ;;
547         unifi-outdoor-plus | \
548         uap-pro)
549                 MTD_CONFIG_ARGS="-s 0x180000"
550                 default_do_upgrade "$ARGV"
551                 ;;
552         *)
553                 default_do_upgrade "$ARGV"
554                 ;;
555         esac
556 }
557
558 disable_watchdog() {
559         killall watchdog
560         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
561                 echo 'Could not disable watchdog'
562                 return 1
563         }
564 }
565
566 append sysupgrade_pre_upgrade disable_watchdog