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