ar71xx: add userspace support for WD My Net N750
[openwrt.git] / target / linux / ar71xx / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2011 OpenWrt.org
3 #
4
5 . /lib/ar71xx.sh
6
7 PART_NAME=firmware
8 RAMFS_COPY_DATA=/lib/ar71xx.sh
9
10 CI_BLKSZ=65536
11 CI_LDADR=0x80060000
12
13 platform_find_partitions() {
14         local first dev size erasesize name
15         while read dev size erasesize name; do
16                 name=${name#'"'}; name=${name%'"'}
17                 case "$name" in
18                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
19                                 if [ -z "$first" ]; then
20                                         first="$name"
21                                 else
22                                         echo "$erasesize:$first:$name"
23                                         break
24                                 fi
25                         ;;
26                 esac
27         done < /proc/mtd
28 }
29
30 platform_find_kernelpart() {
31         local part
32         for part in "${1%:*}" "${1#*:}"; do
33                 case "$part" in
34                         vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
35                                 echo "$part"
36                                 break
37                         ;;
38                 esac
39         done
40 }
41
42 platform_do_upgrade_combined() {
43         local partitions=$(platform_find_partitions)
44         local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
45         local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
46         local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
47         local kern_blocks=$(($kern_length / $CI_BLKSZ))
48         local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
49
50         if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
51            [ ${kern_blocks:-0} -gt 0 ] && \
52            [ ${root_blocks:-0} -gt 0 ] && \
53            [ ${erase_size:-0} -gt 0 ];
54         then
55                 local append=""
56                 [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
57
58                 ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
59                   dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
60                         mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
61         fi
62 }
63
64 tplink_get_image_hwid() {
65         get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
66 }
67
68 tplink_get_image_boot_size() {
69         get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
70 }
71
72 seama_get_type_magic() {
73         get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
74 }
75
76 cybertan_get_image_magic() {
77         get_image "$@" | dd bs=8 count=1 skip=0  2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
78 }
79
80 cybertan_check_image() {
81         local magic="$(cybertan_get_image_magic "$1")"
82         local fw_magic="$(cybertan_get_hw_magic)"
83
84         [ "$fw_magic" != "$magic" ] && {
85                 echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
86                 return 1
87         }
88
89         return 0
90 }
91
92 platform_check_image() {
93         local board=$(ar71xx_board_name)
94         local magic="$(get_magic_word "$1")"
95         local magic_long="$(get_magic_long "$1")"
96
97         [ "$ARGC" -gt 1 ] && return 1
98
99         case "$board" in
100         all0315n | \
101         all0258n | \
102         cap4200ag)
103                 platform_check_image_allnet "$1" && return 0
104                 return 1
105                 ;;
106         alfa-ap96 | \
107         alfa-nx | \
108         ap113 | \
109         ap121 | \
110         ap121-mini | \
111         ap136-010 | \
112         ap136-020 | \
113         ap135-020 | \
114         ap96 | \
115         db120 | \
116         hornet-ub | \
117         bxu2000n-2-a1 | \
118         zcn-1523h-2 | \
119         zcn-1523h-5)
120                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
121                         echo "Invalid image type."
122                         return 1
123                 }
124                 return 0
125                 ;;
126         ap81 | \
127         ap83 | \
128         ap132 | \
129         dir-505-a1 | \
130         dir-600-a1 | \
131         dir-615-c1 | \
132         dir-615-e4 | \
133         dir-825-c1 | \
134         dir-835-a1 | \
135         ew-dorin | \
136         ew-dorin-router | \
137         hornet-ub-x2 | \
138         mzk-w04nu | \
139         mzk-w300nh | \
140         tew-632brp | \
141         tew-712br | \
142         tew-732br | \
143         wrt400n | \
144         airrouter | \
145         bullet-m | \
146         nanostation-m | \
147         rocket-m | \
148         rw2458n | \
149         wndap360 | \
150         wzr-hp-g300nh2 | \
151         wzr-hp-g300nh | \
152         wzr-hp-g450h | \
153         wzr-hp-ag300h | \
154         whr-g301n | \
155         whr-hp-g300n | \
156         whr-hp-gn | \
157         wlae-ag300n | \
158         nbg460n_550n_550nh | \
159         unifi | \
160         unifi-outdoor | \
161         carambola2 )
162                 [ "$magic" != "2705" ] && {
163                         echo "Invalid image type."
164                         return 1
165                 }
166                 return 0
167                 ;;
168
169         dir-825-b1 | \
170         tew-673gru)
171                 dir825b_check_image "$1" && return 0
172                 ;;
173
174         mynet-rext|\
175         wrt160nl)
176                 cybertan_check_image "$1" && return 0
177                 return 1
178                 ;;
179
180         mynet-n600 | \
181         mynet-n750)
182                 [ "$magic_long" != "5ea3a417" ] && {
183                         echo "Invalid image, bad magic: $magic_long"
184                         return 1
185                 }
186
187                 local typemagic=$(seama_get_type_magic "$1")
188                 [ "$typemagic" != "6669726d" ] && {
189                         echo "Invalid image, bad type: $typemagic"
190                         return 1
191                 }
192
193                 return 0;
194                 ;;
195         mr600 | \
196         mr600v2 | \
197         om2p | \
198         om2p-hs | \
199         om2p-lc)
200                 platform_check_image_openmesh "$magic_long" "$1" && return 0
201                 return 1
202                 ;;
203
204         archer-c7 | \
205         tl-mr10u | \
206         tl-mr11u | \
207         tl-mr13u | \
208         tl-mr3020 | \
209         tl-mr3040 | \
210         tl-mr3040-v2 | \
211         tl-mr3220 | \
212         tl-mr3220-v2 | \
213         tl-mr3420 | \
214         tl-mr3420-v2 | \
215         tl-wa7510n | \
216         tl-wa750re | \
217         tl-wa850re | \
218         tl-wa801nd-v2 | \
219         tl-wa901nd | \
220         tl-wa901nd-v2 | \
221         tl-wa901nd-v3 | \
222         tl-wdr3500 | \
223         tl-wdr4300 | \
224         tl-wr703n | \
225         tl-wr710n | \
226         tl-wr720n-v3 | \
227         tl-wr741nd | \
228         tl-wr741nd-v4 | \
229         tl-wr841n-v1 | \
230         tl-wr841n-v7 | \
231         tl-wr841n-v8 | \
232         tl-wr842n-v2 | \
233         tl-wr941nd | \
234         tl-wr1041n-v2 | \
235         tl-wr1043nd | \
236         tl-wr1043nd-v2 | \
237         tl-wr2543n)
238                 [ "$magic" != "0100" ] && {
239                         echo "Invalid image type."
240                         return 1
241                 }
242
243                 local hwid
244                 local imageid
245
246                 hwid=$(tplink_get_hwid)
247                 imageid=$(tplink_get_image_hwid "$1")
248
249                 [ "$hwid" != "$imageid" ] && {
250                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
251                         return 1
252                 }
253
254                 local boot_size
255
256                 boot_size=$(tplink_get_image_boot_size "$1")
257                 [ "$boot_size" != "00000000" ] && {
258                         echo "Invalid image, it contains a bootloader."
259                         return 1
260                 }
261
262                 return 0
263                 ;;
264         uap-pro)
265                 [ "$magic_long" != "19852003" ] && {
266                         echo "Invalid image type."
267                         return 1
268                 }
269                 return 0
270                 ;;
271         wndr3700 | \
272         wnr612-v2)
273                 local hw_magic
274
275                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
276                 [ "$magic_long" != "$hw_magic" ] && {
277                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
278                         return 1
279                 }
280                 return 0
281                 ;;
282         routerstation | \
283         routerstation-pro | \
284         ls-sr71 | \
285         pb42 | \
286         pb44 | \
287         all0305 | \
288         eap7660d | \
289         ja76pf | \
290         ja76pf2 | \
291         jwap003)
292                 [ "$magic" != "4349" ] && {
293                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
294                         return 1
295                 }
296
297                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
298                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
299
300                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
301                         return 0
302                 else
303                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
304                         return 1
305                 fi
306                 return 0
307                 ;;
308         esac
309
310         echo "Sysupgrade is not yet supported on $board."
311         return 1
312 }
313
314 platform_do_upgrade() {
315         local board=$(ar71xx_board_name)
316
317         case "$board" in
318         routerstation | \
319         routerstation-pro | \
320         ls-sr71 | \
321         all0305 | \
322         eap7660d | \
323         pb42 | \
324         pb44 | \
325         ja76pf | \
326         ja76pf2 | \
327         jwap003)
328                 platform_do_upgrade_combined "$ARGV"
329                 ;;
330         all0258n )
331                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
332                 ;;
333         all0315n )
334                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
335                 ;;
336         cap4200ag)
337                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
338                 ;;
339         dir-825-b1 |\
340         tew-673gru)
341                 platform_do_upgrade_dir825b "$ARGV"
342                 ;;
343         mr600 | \
344         mr600v2 | \
345         om2p | \
346         om2p-hs | \
347         om2p-lc)
348                 platform_do_upgrade_openmesh "$ARGV"
349                 ;;
350         uap-pro)
351                 MTD_CONFIG_ARGS="-s 0x180000"
352                 default_do_upgrade "$ARGV"
353                 ;;
354         *)
355                 default_do_upgrade "$ARGV"
356                 ;;
357         esac
358 }
359
360 disable_watchdog() {
361         killall watchdog
362         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
363                 echo 'Could not disable watchdog'
364                 return 1
365         }
366 }
367
368 append sysupgrade_pre_upgrade disable_watchdog