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