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