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