ar71xx: PowerCloud CR5000 openwrt configuration
[oweals/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         cap324 | \
171         cap4200ag | \
172         cr3000 |\
173         cr5000)
174                 platform_check_image_allnet "$1" && return 0
175                 return 1
176                 ;;
177         alfa-ap96 | \
178         alfa-nx | \
179         arduino-yun | \
180         ap113 | \
181         ap121 | \
182         ap121-mini | \
183         ap136-010 | \
184         ap136-020 | \
185         ap135-020 | \
186         ap147-010 | \
187         ap96 | \
188         bxu2000n-2-a1 | \
189         db120 | \
190         dr344 | \
191         f9k1115v2 |\
192         hornet-ub | \
193         mr12 | \
194         mr16 | \
195         wpj558 | \
196         zcn-1523h-2 | \
197         zcn-1523h-5)
198                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
199                         echo "Invalid image type."
200                         return 1
201                 }
202                 return 0
203                 ;;
204         ap81 | \
205         ap83 | \
206         ap132 | \
207         cf-e316n-v2 | \
208         dgl-5500-a1 |\
209         dhp-1565-a1 |\
210         dir-505-a1 | \
211         dir-600-a1 | \
212         dir-615-c1 | \
213         dir-615-e1 | \
214         dir-615-e4 | \
215         dir-615-i1 | \
216         dir-825-c1 | \
217         dir-835-a1 | \
218         dlan-hotspot | \
219         dlan-pro-500-wp | \
220         dlan-pro-1200-ac | \
221         dragino2 | \
222         epg5000 | \
223         esr1750 | \
224         esr900 | \
225         ew-dorin | \
226         ew-dorin-router | \
227         gl-ar150 | \
228         gl-ar300 | \
229         gl-domino | \
230         hiwifi-hc6361 | \
231         hornet-ub-x2 | \
232         mzk-w04nu | \
233         mzk-w300nh | \
234         tew-632brp | \
235         tew-712br | \
236         tew-732br | \
237         tew-823dru | \
238         wrt400n | \
239         airgateway | \
240         airgatewaypro | \
241         airrouter | \
242         bullet-m | \
243         loco-m-xw | \
244         nanostation-m | \
245         rocket-m | \
246         rocket-m-xw | \
247         rocket-m-ti | \
248         nanostation-m-xw | \
249         rw2458n | \
250         wpj531 | \
251         wndap360 | \
252         wpj344 | \
253         wzr-hp-g300nh2 | \
254         wzr-hp-g300nh | \
255         wzr-hp-g450h | \
256         wzr-hp-ag300h | \
257         wzr-450hp2 | \
258         whr-g301n | \
259         whr-hp-g300n | \
260         whr-hp-gn | \
261         wlae-ag300n | \
262         nbg460n_550n_550nh | \
263         unifi | \
264         unifi-outdoor | \
265         carambola2 | \
266         weio )
267                 [ "$magic" != "2705" ] && {
268                         echo "Invalid image type."
269                         return 1
270                 }
271                 return 0
272                 ;;
273
274         cpe510)
275                 tplink_pharos_check_image "$1" && return 0
276                 return 1
277                 ;;
278
279         bsb | \
280         dir-825-b1 | \
281         tew-673gru)
282                 dir825b_check_image "$1" && return 0
283                 ;;
284
285         mynet-rext|\
286         wrt160nl)
287                 cybertan_check_image "$1" && return 0
288                 return 1
289                 ;;
290
291         qihoo-c301 | \
292         mynet-n600 | \
293         mynet-n750)
294                 [ "$magic_long" != "5ea3a417" ] && {
295                         echo "Invalid image, bad magic: $magic_long"
296                         return 1
297                 }
298
299                 local typemagic=$(seama_get_type_magic "$1")
300                 [ "$typemagic" != "6669726d" ] && {
301                         echo "Invalid image, bad type: $typemagic"
302                         return 1
303                 }
304
305                 return 0;
306                 ;;
307         mr1750 | \
308         mr600 | \
309         mr600v2 | \
310         mr900 | \
311         mr900v2 | \
312         om2p | \
313         om2pv2 | \
314         om2p-hs | \
315         om2p-hsv2 | \
316         om2p-lc | \
317         om5p | \
318         om5p-an)
319                 platform_check_image_openmesh "$magic_long" "$1" && return 0
320                 return 1
321                 ;;
322
323         antminer-s1 | \
324         antminer-s3 | \
325         archer-c5 | \
326         archer-c7 | \
327         el-m150 | \
328         el-mini | \
329         gl-inet | \
330         mc-mac1200r | \
331         minibox-v1 |\
332         onion-omega | \
333         oolite | \
334         smart-300 | \
335         tl-mr10u | \
336         tl-mr11u | \
337         tl-mr12u | \
338         tl-mr13u | \
339         tl-mr3020 | \
340         tl-mr3040 | \
341         tl-mr3040-v2 | \
342         tl-mr3220 | \
343         tl-mr3220-v2 | \
344         tl-mr3420 | \
345         tl-mr3420-v2 | \
346         tl-wa701nd-v2 | \
347         tl-wa7210n-v2 | \
348         tl-wa7510n | \
349         tl-wa750re | \
350         tl-wa850re | \
351         tl-wa860re | \
352         tl-wa801nd-v2 | \
353         tl-wa901nd | \
354         tl-wa901nd-v2 | \
355         tl-wa901nd-v3 | \
356         tl-wdr3320-v2 | \
357         tl-wdr3500 | \
358         tl-wdr4300 | \
359         tl-wdr4900-v2 | \
360         tl-wdr6500-v2 | \
361         tl-wr703n | \
362         tl-wr710n | \
363         tl-wr720n-v3 | \
364         tl-wr741nd | \
365         tl-wr741nd-v4 | \
366         tl-wr841n-v1 | \
367         tl-wa830re-v2 | \
368         tl-wr841n-v7 | \
369         tl-wr841n-v8 | \
370         tl-wr841n-v9 | \
371         tl-wr842n-v2 | \
372         tl-wr941nd | \
373         tl-wr941nd-v5 | \
374         tl-wr941nd-v6 | \
375         tl-wr1041n-v2 | \
376         tl-wr1043nd | \
377         tl-wr1043nd-v2 | \
378         tl-wr2543n)
379                 local magic_ver="0100"
380
381                 case "$board" in
382                 tl-wdr6500-v2)
383                         magic_ver="0200"
384                         ;;
385                 esac
386
387                 [ "$magic" != "$magic_ver" ] && {
388                         echo "Invalid image type."
389                         return 1
390                 }
391
392                 local hwid
393                 local imageid
394
395                 hwid=$(tplink_get_hwid)
396                 imageid=$(tplink_get_image_hwid "$1")
397
398                 [ "$hwid" != "$imageid" ] && {
399                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
400                         return 1
401                 }
402
403                 local boot_size
404
405                 boot_size=$(tplink_get_image_boot_size "$1")
406                 [ "$boot_size" != "00000000" ] && {
407                         echo "Invalid image, it contains a bootloader."
408                         return 1
409                 }
410
411                 return 0
412                 ;;
413
414         tube2h)
415                 alfa_check_image "$1" && return 0
416                 return 1
417                 ;;
418
419         nbg6616 | \
420         unifi-outdoor-plus | \
421         uap-pro)
422                 [ "$magic_long" != "19852003" ] && {
423                         echo "Invalid image type."
424                         return 1
425                 }
426                 return 0
427                 ;;
428         wndr3700 | \
429         wnr2000-v3 | \
430         wnr612-v2 | \
431         wnr1000-v2)
432                 local hw_magic
433
434                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
435                 [ "$magic_long" != "$hw_magic" ] && {
436                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
437                         return 1
438                 }
439                 return 0
440                 ;;
441         mr18)
442                 merakinand_do_platform_check $board $1
443                 return $?;
444                 ;;
445         nbg6716 | \
446         r6100 | \
447         wndr3700v4 | \
448         wndr4300 )
449                 nand_do_platform_check $board $1
450                 return $?;
451                 ;;
452         routerstation | \
453         routerstation-pro | \
454         ls-sr71 | \
455         pb42 | \
456         pb44 | \
457         all0305 | \
458         eap300v2 | \
459         eap7660d | \
460         ja76pf | \
461         ja76pf2 | \
462         jwap003 | \
463         wp543 | \
464         wpe72)
465                 [ "$magic" != "4349" ] && {
466                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
467                         return 1
468                 }
469
470                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
471                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
472
473                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
474                         return 0
475                 else
476                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
477                         return 1
478                 fi
479                 return 0
480                 ;;
481     wnr2000-v4)
482                 [ "$magic_long" != "32303034" ] && {
483                         echo "Invalid image type."
484                         return 1
485                 }
486                 return 0
487                 ;;
488
489         esac
490
491         echo "Sysupgrade is not yet supported on $board."
492         return 1
493 }
494
495 platform_pre_upgrade() {
496         local board=$(ar71xx_board_name)
497
498         case "$board" in
499         nbg6716 | \
500         r6100 | \
501         wndr3700v4 | \
502         wndr4300 )
503                 nand_do_upgrade "$1"
504                 ;;
505         mr18)
506                 merakinand_do_upgrade "$1"
507                 ;;
508         esac
509 }
510
511 platform_do_upgrade() {
512         local board=$(ar71xx_board_name)
513
514         case "$board" in
515         routerstation | \
516         routerstation-pro | \
517         ls-sr71 | \
518         all0305 | \
519         eap7660d | \
520         pb42 | \
521         pb44 | \
522         ja76pf | \
523         ja76pf2 | \
524         jwap003)
525                 platform_do_upgrade_combined "$ARGV"
526                 ;;
527         wp543|\
528         wpe72)
529                 platform_do_upgrade_compex "$ARGV"
530                 ;;
531         all0258n )
532                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
533                 ;;
534         all0315n )
535                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
536                 ;;
537         eap300v2 |\
538         cap4200ag)
539                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
540                 ;;
541         dir-825-b1 |\
542         tew-673gru)
543                 platform_do_upgrade_dir825b "$ARGV"
544                 ;;
545         mr1750 | \
546         mr600 | \
547         mr600v2 | \
548         mr900 | \
549         mr900v2 | \
550         om2p | \
551         om2pv2 | \
552         om2p-hs | \
553         om2p-hsv2 | \
554         om2p-lc | \
555         om5p | \
556         om5p-an)
557                 platform_do_upgrade_openmesh "$ARGV"
558                 ;;
559         unifi-outdoor-plus | \
560         uap-pro)
561                 MTD_CONFIG_ARGS="-s 0x180000"
562                 default_do_upgrade "$ARGV"
563                 ;;
564         *)
565                 default_do_upgrade "$ARGV"
566                 ;;
567         esac
568 }
569
570 disable_watchdog() {
571         killall watchdog
572         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
573                 echo 'Could not disable watchdog'
574                 return 1
575         }
576 }
577
578 append sysupgrade_pre_upgrade disable_watchdog