308b4f14b0213ea58fbdb82f7a05a0787dbfd55b
[15.05/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 ${kern_blocks:-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 platform_check_image() {
73         local board=$(ar71xx_board_name)
74         local magic="$(get_magic_word "$1")"
75         local magic_long="$(get_magic_long "$1")"
76
77         [ "$ARGC" -gt 1 ] && return 1
78
79         case "$board" in
80         all0315n | \
81         all0258n | \
82         cap4200ag)
83                 platform_check_image_allnet "$1" && return 0
84                 return 1
85                 ;;
86         alfa-ap96 | \
87         alfa-nx | \
88         ap113 | \
89         ap121 | \
90         ap121-mini | \
91         ap136-010 | \
92         ap136-020 | \
93         ap135-020 | \
94         ap96 | \
95         db120 | \
96         hornet-ub | \
97         zcn-1523h-2 | \
98         zcn-1523h-5)
99                 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
100                         echo "Invalid image type."
101                         return 1
102                 }
103                 return 0
104                 ;;
105         ap81 | \
106         ap83 | \
107         ap132 | \
108         dir-600-a1 | \
109         dir-615-c1 | \
110         dir-615-e4 | \
111         dir-825-c1 | \
112         dir-835-a1 | \
113         ew-dorin | \
114         ew-dorin-router | \
115         mzk-w04nu | \
116         mzk-w300nh | \
117         tew-632brp | \
118         tew-712br | \
119         wrt400n | \
120         airrouter | \
121         bullet-m | \
122         nanostation-m | \
123         rocket-m | \
124         rw2458n | \
125         wndap360 | \
126         wzr-hp-g300nh2 | \
127         wzr-hp-g300nh | \
128         wzr-hp-g450h | \
129         wzr-hp-ag300h | \
130         whr-g301n | \
131         whr-hp-g300n | \
132         whr-hp-gn | \
133         wlae-ag300n | \
134         nbg460n_550n_550nh | \
135         unifi | \
136         unifi-outdoor | \
137         carambola2 )
138                 [ "$magic" != "2705" ] && {
139                         echo "Invalid image type."
140                         return 1
141                 }
142                 return 0
143                 ;;
144
145         dir-825-b1 | \
146         tew-673gru)
147                 dir825b_check_image "$1" && return 0
148                 ;;
149
150         mr600 | \
151         mr600v2 | \
152         om2p | \
153         om2p-hs | \
154         om2p-lc)
155                 platform_check_image_openmesh "$magic_long" "$1" && return 0
156                 return 1
157                 ;;
158
159         archer-c7 | \
160         tl-mr10u | \
161         tl-mr11u | \
162         tl-mr3020 | \
163         tl-mr3040 | \
164         tl-mr3220 | \
165         tl-mr3220-v2 | \
166         tl-mr3420 | \
167         tl-mr3420-v2 | \
168         tl-wa7510n | \
169         tl-wa901nd | \
170         tl-wa901nd-v2 | \
171         tl-wdr3500 | \
172         tl-wdr4300 | \
173         tl-wr703n | \
174         tl-wr710n | \
175         tl-wr720n-v3 | \
176         tl-wr741nd | \
177         tl-wr741nd-v4 | \
178         tl-wr841n-v1 | \
179         tl-wr841n-v7 | \
180         tl-wr841n-v8 | \
181         tl-wr941nd | \
182         tl-wr1041n-v2 | \
183         tl-wr1043nd | \
184         tl-wr2543n)
185                 [ "$magic" != "0100" ] && {
186                         echo "Invalid image type."
187                         return 1
188                 }
189
190                 local hwid
191                 local imageid
192
193                 hwid=$(tplink_get_hwid)
194                 imageid=$(tplink_get_image_hwid "$1")
195
196                 [ "$hwid" != "$imageid" ] && {
197                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
198                         return 1
199                 }
200
201                 local boot_size
202
203                 boot_size=$(tplink_get_image_boot_size "$1")
204                 [ "$boot_size" != "00000000" ] && {
205                         echo "Invalid image, it contains a bootloader."
206                         return 1
207                 }
208
209                 return 0
210                 ;;
211         uap-pro)
212                 [ "$magic_long" != "19852003" ] && {
213                         echo "Invalid image type."
214                         return 1
215                 }
216                 return 0
217                 ;;
218         wndr3700 | \
219         wnr612-v2)
220                 local hw_magic
221
222                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
223                 [ "$magic_long" != "$hw_magic" ] && {
224                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
225                         return 1
226                 }
227                 return 0
228                 ;;
229         wrt160nl)
230                 [ "$magic" != "4e4c" ] && {
231                         echo "Invalid image type."
232                         return 1
233                 }
234                 return 0
235                 ;;
236         routerstation | \
237         routerstation-pro | \
238         ls-sr71 | \
239         pb42 | \
240         pb44 | \
241         all0305 | \
242         eap7660d | \
243         ja76pf | \
244         ja76pf2 | \
245         jwap003)
246                 [ "$magic" != "4349" ] && {
247                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
248                         return 1
249                 }
250
251                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
252                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
253
254                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
255                         return 0
256                 else
257                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
258                         return 1
259                 fi
260                 return 0
261                 ;;
262         esac
263
264         echo "Sysupgrade is not yet supported on $board."
265         return 1
266 }
267
268 platform_do_upgrade() {
269         local board=$(ar71xx_board_name)
270
271         case "$board" in
272         routerstation | \
273         routerstation-pro | \
274         ls-sr71 | \
275         all0305 | \
276         eap7660d | \
277         pb42 | \
278         pb44 | \
279         ja76pf | \
280         ja76pf2 | \
281         jwap003)
282                 platform_do_upgrade_combined "$ARGV"
283                 ;;
284         all0258n )
285                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
286                 ;;
287         all0315n )
288                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
289                 ;;
290         cap4200ag)
291                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
292                 ;;
293         dir-825-b1 |\
294         tew-673gru)
295                 platform_do_upgrade_dir825b "$ARGV"
296                 ;;
297         mr600 | \
298         mr600v2 | \
299         om2p | \
300         om2p-hs | \
301         om2p-lc)
302                 platform_do_upgrade_openmesh "$ARGV"
303                 ;;
304         *)
305                 default_do_upgrade "$ARGV"
306                 ;;
307         esac
308 }
309
310 disable_watchdog() {
311         killall watchdog
312         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
313                 echo 'Could not disable watchdog'
314                 return 1
315         }
316 }
317
318 append sysupgrade_pre_upgrade disable_watchdog