bcm53xx: extend firmware validation
[openwrt/staging/jogo.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
1 RAMFS_COPY_BIN='osafeloader oseama otrx'
2
3 PART_NAME=firmware
4
5 # $(1): file to read magic from
6 # $(2): offset in bytes
7 get_magic_long_at() {
8 dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
9 }
10
11 platform_flash_type() {
12 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
13 grep -q "\"rootfs\"" /proc/mtd && {
14 echo "serial"
15 return
16 }
17
18 echo "nand"
19 }
20
21 platform_expected_image() {
22 local machine=$(board_name)
23
24 case "$machine" in
25 "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
26 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
27 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
28 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
29 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
30 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
31 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
32 "tplink,archer-c9-v1") echo "safeloader"; return;;
33 esac
34 }
35
36 platform_identify() {
37 local magic
38
39 magic=$(get_magic_long "$1")
40 case "$magic" in
41 "48445230")
42 echo "trx"
43 return
44 ;;
45 "2a23245e")
46 echo "chk"
47 return
48 ;;
49 "5ea3a417")
50 echo "seama"
51 return
52 ;;
53 esac
54
55 magic=$(get_magic_long_at "$1" 14)
56 [ "$magic" = "55324e44" ] && {
57 echo "cybertan"
58 return
59 }
60
61 if osafeloader info "$1" > /dev/null 2>&1; then
62 echo "safeloader"
63 return
64 fi
65
66 echo "unknown"
67 }
68
69 platform_check_image() {
70 [ "$#" -gt 1 ] && return 1
71
72 local file_type=$(platform_identify "$1")
73 local magic
74 local error=0
75
76 case "$file_type" in
77 "chk")
78 local header_len=$((0x$(get_magic_long_at "$1" 4)))
79 local board_id_len=$(($header_len - 40))
80 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
81 local dev_board_id=$(platform_expected_image)
82 echo "Found CHK image with device board_id $board_id"
83
84 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
85 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
86 error=1
87 }
88
89 if ! otrx check "$1" -o "$header_len"; then
90 echo "No valid TRX firmware in the CHK image"
91 notify_firmware_test_result "trx_valid" 0
92 error=1
93 else
94 notify_firmware_test_result "trx_valid" 1
95 fi
96 ;;
97 "cybertan")
98 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
99 local dev_pattern=$(platform_expected_image)
100 echo "Found CyberTAN image with device pattern: $pattern"
101
102 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
103 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
104 error=1
105 }
106
107 if ! otrx check "$1" -o 32; then
108 echo "No valid TRX firmware in the CyberTAN image"
109 notify_firmware_test_result "trx_valid" 0
110 error=1
111 else
112 notify_firmware_test_result "trx_valid" 1
113 fi
114 ;;
115 "safeloader")
116 ;;
117 "seama")
118 local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
119 local dev_signature=$(platform_expected_image)
120 echo "Found Seama image with device signature: $img_signature"
121
122 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
123 echo "Firmware signature doesn't match device signature ($dev_signature)"
124 error=1
125 }
126
127 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
128 echo "Seama container doesn't have firmware entity"
129 error=1
130 }
131 ;;
132 "trx")
133 local expected=$(platform_expected_image)
134
135 [ "$expected" == "safeloader" ] && {
136 echo "This device expects SafeLoader format and may not work with TRX"
137 error=1
138 }
139
140 if ! otrx check "$1"; then
141 echo "Invalid (corrupted?) TRX firmware"
142 notify_firmware_test_result "trx_valid" 0
143 error=1
144 else
145 notify_firmware_test_result "trx_valid" 1
146 fi
147 ;;
148 *)
149 echo "Invalid image type. Please use firmware specific for this device."
150 notify_firmware_broken
151 error=1
152 ;;
153 esac
154
155 return $error
156 }
157
158 # $(1): image for upgrade (with possible extra header)
159 # $(2): offset of trx in image
160 platform_do_upgrade_nand_trx() {
161 local dir="/tmp/sysupgrade-bcm53xx"
162 local trx="$1"
163 local offset="$2"
164
165 # Extract partitions from trx
166 rm -fR $dir
167 mkdir -p $dir
168 otrx extract "$trx" \
169 ${offset:+-o $offset} \
170 -1 $dir/kernel \
171 -2 $dir/root
172 [ $? -ne 0 ] && {
173 echo "Failed to extract TRX partitions."
174 return
175 }
176
177 # Firmwares without UBI image should be flashed "normally"
178 local root_type=$(identify $dir/root)
179 [ "$root_type" != "ubi" ] && {
180 echo "Provided firmware doesn't use UBI for rootfs."
181 return
182 }
183
184 # Prepare TRX file with just a kernel that will replace current one
185 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
186 [ -z "$linux_length" ] && {
187 echo "Unable to find \"linux\" partition size"
188 exit 1
189 }
190 linux_length=$((0x$linux_length))
191 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
192 [ $kernel_length -gt $linux_length ] && {
193 echo "New kernel doesn't fit \"linux\" partition."
194 return
195 }
196 rm -f /tmp/null.bin
197 rm -f /tmp/kernel.trx
198 touch /tmp/null.bin
199 otrx create /tmp/kernel.trx \
200 -f $dir/kernel -b $(($linux_length + 28)) \
201 -f /tmp/null.bin
202 [ $? -ne 0 ] && {
203 echo "Failed to create simple TRX with new kernel."
204 return
205 }
206
207 # Prepare UBI image (drop unwanted extra blocks)
208 local ubi_length=0
209 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
210 ubi_length=$(($ubi_length + 131072))
211 done
212 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
213 [ $? -ne 0 ] && {
214 echo "Failed to prepare new UBI image."
215 return
216 }
217
218 # Flash
219 mtd write /tmp/kernel.trx firmware || exit 1
220 nand_do_upgrade /tmp/root.ubi
221 }
222
223 platform_do_upgrade_nand_seama() {
224 local dir="/tmp/sysupgrade-bcm53xx"
225 local seama="$1"
226 local tmp
227
228 # Extract Seama entity from Seama seal
229 rm -fR $dir
230 mkdir -p $dir
231 oseama extract "$seama" \
232 -e 0 \
233 -o $dir/seama.entity
234 [ $? -ne 0 ] && {
235 echo "Failed to extract Seama entity."
236 return
237 }
238 local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
239
240 local ubi_offset=0
241 tmp=0
242 while [ 1 ]; do
243 [ $tmp -ge $entity_size ] && break
244 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
245 ubi_offset=$tmp
246 break
247 }
248 tmp=$(($tmp + 131072))
249 done
250 [ $ubi_offset -eq 0 ] && {
251 echo "Failed to find UBI in Seama entity."
252 return
253 }
254
255 local ubi_length=0
256 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
257 ubi_length=$(($ubi_length + 131072))
258 done
259
260 dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
261 dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
262
263 # Flash
264 local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
265 mtd write $dir/kernel.seama firmware || exit 1
266 mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
267 nand_do_upgrade $dir/root.ubi
268 }
269
270 platform_trx_from_chk_cmd() {
271 local header_len=$((0x$(get_magic_long_at "$1" 4)))
272
273 echo -n dd skip=$header_len iflag=skip_bytes
274 }
275
276 platform_trx_from_cybertan_cmd() {
277 echo -n dd skip=32 iflag=skip_bytes
278 }
279
280 platform_img_from_safeloader() {
281 local dir="/tmp/sysupgrade-bcm53xx"
282
283 # Extract partitions from SafeLoader
284 rm -fR $dir
285 mkdir -p $dir
286 osafeloader extract "$1" \
287 -p "os-image" \
288 -o $dir/os-image
289 osafeloader extract "$1" \
290 -p "file-system" \
291 -o $dir/file-system
292
293 mtd write $dir/file-system rootfs
294
295 echo -n $dir/os-image
296 }
297
298 platform_img_from_seama() {
299 local dir="/tmp/sysupgrade-bcm53xx"
300 local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
301 local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
302
303 # Busybox doesn't support required iflag-s
304 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
305
306 rm -fR $dir
307 mkdir -p $dir
308 dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
309 dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
310
311 echo -n $dir/image-entity.bin
312 }
313
314 platform_do_upgrade() {
315 local file_type=$(platform_identify "$1")
316 local trx="$1"
317 local cmd=
318
319 [ "$(platform_flash_type)" == "nand" ] && {
320 case "$file_type" in
321 "chk") platform_do_upgrade_nand_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
322 "cybertan") platform_do_upgrade_nand_trx "$1" 32;;
323 "seama") platform_do_upgrade_nand_seama "$1";;
324 "trx") platform_do_upgrade_nand_trx "$1";;
325 esac
326
327 # Above calls exit on success.
328 # If we got here something went wrong.
329 echo "Writing whole image to NAND flash. All erase counters will be lost."
330 }
331
332 case "$file_type" in
333 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
334 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
335 "safeloader") trx=$(platform_img_from_safeloader "$trx"); PART_NAME=os-image;;
336 "seama") trx=$(platform_img_from_seama "$trx");;
337 esac
338
339 default_do_upgrade "$trx" "$cmd"
340 }