ar71xx: verify hardware id of firmware images on TP-Link boards
[openwrt/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|kernel|linux|rootfs)
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|kernel|linux)
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 platform_check_image() {
69 local board=$(ar71xx_board_name)
70 local magic="$(get_magic_word "$1")"
71 local magic_long="$(get_magic_long "$1")"
72
73 [ "$ARGC" -gt 1 ] && return 1
74
75 case "$board" in
76 all0258n )
77 platform_check_image_all0258n "$1" && return 0
78 return 1
79 ;;
80 ap121 | \
81 ap121-mini | \
82 ap96 | \
83 db120 | \
84 zcn-1523h-2 | \
85 zcn-1523h-5)
86 [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
87 echo "Invalid image type."
88 return 1
89 }
90 return 0
91 ;;
92 ap81 | \
93 ap83 | \
94 dir-600-a1 | \
95 dir-615-c1 | \
96 dir-825-b1 | \
97 mzk-w04nu | \
98 mzk-w300nh | \
99 tew-632brp | \
100 wrt400n | \
101 bullet-m | \
102 nanostation-m | \
103 rocket-m | \
104 wzr-hp-g300nh | \
105 wzr-hp-g450h | \
106 wzr-hp-ag300h | \
107 whr-g301n | \
108 whr-hp-g300n | \
109 whr-hp-gn | \
110 nbg460n_550n_550nh | \
111 unifi )
112 [ "$magic" != "2705" ] && {
113 echo "Invalid image type."
114 return 1
115 }
116 return 0
117 ;;
118 tl-mr3220 | \
119 tl-mr3420 | \
120 tl-wa901nd | \
121 tl-wa901nd-v2 | \
122 tl-wr703n | \
123 tl-wr741nd | \
124 tl-wr741nd-v4 | \
125 tl-wr841n-v1 | \
126 tl-wr941nd | \
127 tl-wr1043nd)
128 [ "$magic" != "0100" ] && {
129 echo "Invalid image type."
130 return 1
131 }
132
133 local hwid
134 local imageid
135
136 hwid=$(tplink_get_hwid)
137 imageid=$(tplink_get_image_hwid "$1")
138
139 [ "$hwid" != "$imageid" ] && {
140 echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
141 return 1
142 }
143
144 return 0
145 ;;
146 wndr3700)
147 local hw_magic
148
149 hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
150 [ "$magic_long" != "$hw_magic" ] && {
151 echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
152 return 1
153 }
154 return 0
155 ;;
156 wrt160nl)
157 [ "$magic" != "4e4c" ] && {
158 echo "Invalid image type."
159 return 1
160 }
161 return 0
162 ;;
163 routerstation | \
164 routerstation-pro | \
165 ls-sr71 | \
166 pb42 | \
167 pb44 | \
168 eap7660d | \
169 ja76pf )
170 [ "$magic" != "4349" ] && {
171 echo "Invalid image. Use *-sysupgrade.bin files on this board"
172 return 1
173 }
174
175 local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
176 local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
177
178 if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
179 return 0
180 else
181 echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
182 return 1
183 fi
184 return 0
185 ;;
186 esac
187
188 echo "Sysupgrade is not yet supported on $board."
189 return 1
190 }
191
192 platform_do_upgrade() {
193 local board=$(ar71xx_board_name)
194
195 case "$board" in
196 routerstation | \
197 routerstation-pro | \
198 ls-sr71 | \
199 eap7660d | \
200 ja76pf)
201 platform_do_upgrade_combined "$ARGV"
202 ;;
203 all0258n )
204 platform_do_upgrade_all0258n "$ARGV"
205 ;;
206 *)
207 default_do_upgrade "$ARGV"
208 ;;
209 esac
210 }
211
212 disable_watchdog() {
213 killall watchdog
214 ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
215 echo 'Could not disable watchdog'
216 return 1
217 }
218 }
219
220 append sysupgrade_pre_upgrade disable_watchdog