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