ar71xx: add user-space support for UniFi AP Pro
[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 | \
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-mr3420 | \
155         tl-wa7510n | \
156         tl-wa901nd | \
157         tl-wa901nd-v2 | \
158         tl-wdr4300 | \
159         tl-wr703n | \
160         tl-wr741nd | \
161         tl-wr741nd-v4 | \
162         tl-wr841n-v1 | \
163         tl-wr841n-v7 | \
164         tl-wr841n-v8 | \
165         tl-wr941nd | \
166         tl-wr1041n-v2 | \
167         tl-wr1043nd | \
168         tl-wr2543n)
169                 [ "$magic" != "0100" ] && {
170                         echo "Invalid image type."
171                         return 1
172                 }
173
174                 local hwid
175                 local imageid
176
177                 hwid=$(tplink_get_hwid)
178                 imageid=$(tplink_get_image_hwid "$1")
179
180                 [ "$hwid" != "$imageid" ] && {
181                         echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
182                         return 1
183                 }
184
185                 local boot_size
186
187                 boot_size=$(tplink_get_image_boot_size "$1")
188                 [ "$boot_size" != "00000000" ] && {
189                         echo "Invalid image, it contains a bootloader."
190                         return 1
191                 }
192
193                 return 0
194                 ;;
195         uap-pro)
196                 [ "$magic_long" != "19852003" ] && {
197                         echo "Invalid image type."
198                         return 1
199                 }
200                 return 0
201                 ;;
202         wndr3700)
203                 local hw_magic
204
205                 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
206                 [ "$magic_long" != "$hw_magic" ] && {
207                         echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
208                         return 1
209                 }
210                 return 0
211                 ;;
212         wrt160nl)
213                 [ "$magic" != "4e4c" ] && {
214                         echo "Invalid image type."
215                         return 1
216                 }
217                 return 0
218                 ;;
219         routerstation | \
220         routerstation-pro | \
221         ls-sr71 | \
222         pb42 | \
223         pb44 | \
224         all0305 | \
225         eap7660d | \
226         ja76pf | \
227         ja76pf2 | \
228         jwap003)
229                 [ "$magic" != "4349" ] && {
230                         echo "Invalid image. Use *-sysupgrade.bin files on this board"
231                         return 1
232                 }
233
234                 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
235                 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
236
237                 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
238                         return 0
239                 else
240                         echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
241                         return 1
242                 fi
243                 return 0
244                 ;;
245         esac
246
247         echo "Sysupgrade is not yet supported on $board."
248         return 1
249 }
250
251 platform_do_upgrade() {
252         local board=$(ar71xx_board_name)
253
254         case "$board" in
255         routerstation | \
256         routerstation-pro | \
257         ls-sr71 | \
258         all0305 | \
259         eap7660d | \
260         pb42 | \
261         pb44 | \
262         ja76pf | \
263         ja76pf2 | \
264         jwap003)
265                 platform_do_upgrade_combined "$ARGV"
266                 ;;
267         all0258n )
268                 platform_do_upgrade_allnet "0x9f050000" "$ARGV"
269                 ;;
270         all0315n )
271                 platform_do_upgrade_allnet "0x9f080000" "$ARGV"
272                 ;;
273         cap4200ag)
274                 platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
275                 ;;
276         dir-825-b1 |\
277         tew-673gru)
278                 platform_do_upgrade_dir825b "$ARGV"
279                 ;;
280         mr600 | \
281         om2p | \
282         om2p-hs | \
283         om2p-lc)
284                 platform_do_upgrade_openmesh "$ARGV"
285                 ;;
286         *)
287                 default_do_upgrade "$ARGV"
288                 ;;
289         esac
290 }
291
292 disable_watchdog() {
293         killall watchdog
294         ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
295                 echo 'Could not disable watchdog'
296                 return 1
297         }
298 }
299
300 append sysupgrade_pre_upgrade disable_watchdog