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