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