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