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