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