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