1 RAMFS_COPY_BIN
='osafeloader oseama otrx truncate'
7 BCM53XX_FW_INT_IMG_FORMAT
=
8 BCM53XX_FW_INT_IMG_TRX_OFFSET
=
9 BCM53XX_FW_INT_IMG_EXTRACT_CMD
=
11 LXL_FLAGS_VENDOR_LUXUL
=0x00000001
13 # $(1): file to read magic from
14 # $(2): offset in bytes
16 dd if="$1" skip
=$2 bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%02x"'
19 # $(1): file to read LE long number from
20 # $(2): offset in bytes
22 echo $
((0x$
(dd if="$1" skip
=$2 bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/4 "%02x"')))
25 platform_flash_type
() {
26 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
27 grep -q "\"rootfs\"" /proc
/mtd
&& {
35 platform_expected_image
() {
36 local machine
=$
(board_name
)
39 "dlink,dir-885l") echo "seamaseal wrgac42_dlink.2015_dir885l"; return;;
40 "luxul,abr-4500-v1") echo "lxl ABR-4500"; return;;
41 "luxul,xap-810-v1") echo "lxl XAP-810"; return;;
42 "luxul,xap-1410-v1") echo "lxl XAP-1410"; return;;
43 "luxul,xap-1440-v1") echo "lxl XAP-1440"; return;;
44 "luxul,xap-1510-v1") echo "lxl XAP-1510"; return;;
45 "luxul,xap-1610-v1") echo "lxl XAP-1610"; return;;
46 "luxul,xbr-4500-v1") echo "lxl XBR-4500"; return;;
47 "luxul,xwc-1000") echo "lxl XWC-1000"; return;;
48 "luxul,xwc-2000-v1") echo "lxl XWC-2000"; return;;
49 "luxul,xwr-1200-v1") echo "lxl XWR-1200"; return;;
50 "luxul,xwr-3100-v1") echo "lxl XWR-3100"; return;;
51 "luxul,xwr-3150-v1") echo "lxl XWR-3150"; return;;
52 "netgear,r6250-v1") echo "chk U12H245T00_NETGEAR"; return;;
53 "netgear,r6300-v2") echo "chk U12H240T00_NETGEAR"; return;;
54 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
55 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
56 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
57 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
58 "tplink,archer-c9-v1") echo "safeloader"; return;;
65 magic
=$
(get_magic_long
"$1")
68 BCM53XX_FW_FORMAT
="trx"
72 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
73 local board_id_len
=$
(($header_len - 40))
75 BCM53XX_FW_FORMAT
="chk"
76 BCM53XX_FW_BOARD_ID
=$
(dd if="$1" skip
=40 bs
=1 count
=$board_id_len 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
77 BCM53XX_FW_INT_IMG_FORMAT
="trx"
78 BCM53XX_FW_INT_IMG_TRX_OFFSET
="$header_len"
79 BCM53XX_FW_INT_IMG_EXTRACT_CMD
="dd skip=$header_len iflag=skip_bytes"
83 local hdr_len
=$
(get_le_long_at
"$1" 8)
84 local flags
=$
(get_le_long_at
"$1" 12)
86 [ $
((flags
& LXL_FLAGS_VENDOR_LUXUL
)) -gt 0 ] && notify_firmware_no_backup
88 BCM53XX_FW_FORMAT
="lxl"
89 BCM53XX_FW_BOARD_ID
=$
(dd if="$1" skip
=16 bs
=1 count
=16 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
90 BCM53XX_FW_INT_IMG_FORMAT
="trx"
91 BCM53XX_FW_INT_IMG_TRX_OFFSET
="$hdr_len"
92 BCM53XX_FW_INT_IMG_EXTRACT_CMD
="dd skip=$hdr_len iflag=skip_bytes"
97 BCM53XX_FW_FORMAT
="seamaseal"
98 BCM53XX_FW_BOARD_ID
=$
(oseama info
"$1" |
grep "Meta entry:.*signature=" |
sed "s/.*=//")
99 BCM53XX_FW_INT_IMG_EXTRACT_CMD
="oseama extract - -e 0"
104 magic
=$
(get_magic_long_at
"$1" 14)
105 [ "$magic" = "55324e44" ] && {
106 BCM53XX_FW_FORMAT
="cybertan"
107 BCM53XX_FW_BOARD_ID
=$
(dd if="$1" bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
108 BCM53XX_FW_INT_IMG_FORMAT
="trx"
109 BCM53XX_FW_INT_IMG_TRX_OFFSET
="32"
110 BCM53XX_FW_INT_IMG_EXTRACT_CMD
="dd skip=32 iflag=skip_bytes"
114 magic
=$
(get_magic_long_at
"$1" 60)
115 [ "$magic" = "4c584c23" ] && {
116 notify_firmware_no_backup
118 BCM53XX_FW_FORMAT
="lxlold"
119 BCM53XX_FW_BOARD_ID
=$
(dd if="$1" skip
=48 bs
=1 count
=12 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
120 BCM53XX_FW_INT_IMG_FORMAT
="trx"
121 BCM53XX_FW_INT_IMG_TRX_OFFSET
="64"
122 BCM53XX_FW_INT_IMG_EXTRACT_CMD
="dd skip=64 iflag=skip_bytes"
126 if osafeloader info
"$1" > /dev
/null
2>&1; then
127 BCM53XX_FW_FORMAT
="safeloader"
132 platform_other_check_image
() {
133 [ "$#" -gt 1 ] && return 1
137 platform_identify
"$1"
138 [ -z "$BCM53XX_FW_FORMAT" ] && {
139 echo "Invalid image type. Please use firmware specific for this device."
140 notify_firmware_broken
143 echo "Found $BCM53XX_FW_FORMAT firmware for device $BCM53XX_FW_BOARD_ID"
145 local expected_image
="$(platform_expected_image)"
146 local tmp_format
=$BCM53XX_FW_FORMAT
147 [ "$tmp_format" = "lxlold" ] && tmp_format
="lxl"
148 [ -n "$expected_image" -a -n "$BCM53XX_FW_BOARD_ID" -a "$expected_image" != "$tmp_format $BCM53XX_FW_BOARD_ID" ] && {
149 echo "Firmware doesn't match device ($expected_image)"
153 case "$BCM53XX_FW_FORMAT" in
155 $
(oseama info
"$1" -e 0 |
grep -q "Meta entry:.*type=firmware") ||
{
156 echo "Seama seal doesn't contain firmware entity"
161 if ! otrx check
"$1"; then
162 echo "Failed to find a valid TRX in firmware"
163 notify_firmware_test_result
"trx_valid" 0
166 notify_firmware_test_result
"trx_valid" 1
169 [ "$expected_image" == "safeloader" ] && {
170 echo "This device expects SafeLoader format and may not work with TRX"
175 case "$BCM53XX_FW_INT_IMG_FORMAT" in
177 # Make sure that both ways of extracting TRX work.
178 # platform_do_upgrade() may use any of them.
179 if ! otrx check
"$1" -o "$BCM53XX_FW_INT_IMG_TRX_OFFSET" || \
180 ! $BCM53XX_FW_INT_IMG_EXTRACT_CMD < $1 | otrx check
-; then
181 echo "Invalid (corrupted?) TRX firmware"
182 notify_firmware_test_result
"trx_valid" 0
185 notify_firmware_test_result
"trx_valid" 1
195 platform_check_image
() {
198 board
="$(board_name)"
200 # Ideally, REQUIRE_IMAGE_METADATA=1 would suffice
201 # but this would require converting all other
205 nand_do_platform_check
"${board//,/_}" "$1"
209 platform_other_check_image
"$1"
218 # $(1): TRX image or firmware containing TRX
219 # $(2): offset of TRX in firmware (optional)
220 platform_do_upgrade_nand_trx
() {
221 local dir
="/tmp/sysupgrade-bcm53xx"
225 # Extract partitions from trx
228 otrx extract
"$trx" \
229 ${offset:+-o $offset} \
233 echo "Failed to extract TRX partitions."
237 # Firmwares without UBI image should be flashed "normally"
238 local root_type
=$
(identify
$dir/root
)
239 [ "$root_type" != "ubi" ] && {
240 echo "Provided firmware doesn't use UBI for rootfs."
244 # Prepare TRX file with just a kernel that will replace current one
245 local linux_length
=$
(grep "\"linux\"" /proc
/mtd |
sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
246 [ -z "$linux_length" ] && {
247 echo "Unable to find \"linux\" partition size"
250 linux_length
=$
((0x
$linux_length))
251 local kernel_length
=$
(wc -c $dir/kernel | cut
-d ' ' -f 1)
252 [ $kernel_length -gt $linux_length ] && {
253 echo "New kernel doesn't fit \"linux\" partition."
257 rm -f /tmp
/kernel.trx
259 otrx create
/tmp
/kernel.trx \
260 -f $dir/kernel
-b $
(($linux_length + 28)) \
263 echo "Failed to create simple TRX with new kernel."
267 # Prepare UBI image (drop unwanted extra blocks)
269 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
270 ubi_length
=$
(($ubi_length + 131072))
272 truncate
-s $ubi_length $dir/root
274 echo "Failed to prepare new UBI image."
279 mtd
write /tmp
/kernel.trx firmware ||
exit 1
280 nand_do_upgrade
$dir/root
283 platform_do_upgrade_nand_seamaseal
() {
284 local dir
="/tmp/sysupgrade-bcm53xx"
288 # Extract Seama entity from Seama seal
291 oseama extract
"$seamaseal" \
295 echo "Failed to extract Seama entity."
298 local entity_size
=$
(wc -c $dir/seama.entity | cut
-d ' ' -f 1)
303 [ $tmp -ge $entity_size ] && break
304 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
308 tmp
=$
(($tmp + 131072))
310 [ $ubi_offset -eq 0 ] && {
311 echo "Failed to find UBI in Seama entity."
316 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
317 ubi_length
=$
(($ubi_length + 131072))
320 dd if=$dir/seama.entity of
=$dir/kernel.seama bs
=131072 count
=$
(($ubi_offset / 131072)) 2>/dev
/null
321 dd if=$dir/seama.entity of
=$dir/root.ubi bs
=131072 skip
=$
(($ubi_offset / 131072)) count
=$
(($ubi_length / 131072)) 2>/dev
/null
324 local kernel_size
=$
(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc
/mtd
)
325 mtd
write $dir/kernel.seama firmware ||
exit 1
326 mtd
${kernel_size:+-c 0x$kernel_size} fixseama firmware
327 nand_do_upgrade
$dir/root.ubi
330 platform_img_from_safeloader
() {
331 local dir
="/tmp/sysupgrade-bcm53xx"
333 # Extract partitions from SafeLoader
336 osafeloader extract
"$1" \
339 osafeloader extract
"$1" \
343 mtd
write $dir/file-system rootfs
345 echo -n $dir/os-image
348 platform_other_do_upgrade
() {
349 platform_identify
"$1"
351 [ "$(platform_flash_type)" == "nand" ] && {
352 # Try NAND-aware upgrade
353 case "$BCM53XX_FW_FORMAT" in
355 platform_do_upgrade_nand_seamaseal
"$1"
358 platform_do_upgrade_nand_trx
"$1"
361 case "$BCM53XX_FW_INT_IMG_FORMAT" in
363 platform_do_upgrade_nand_trx
"$1" "$BCM53XX_FW_INT_IMG_TRX_OFFSET"
366 echo "NAND aware sysupgrade is unsupported for $BCM53XX_FW_FORMAT format"
372 # Above calls exit on success.
373 # If we got here something went wrong.
374 echo "Writing whole image to NAND flash. All erase counters will be lost."
377 case "$BCM53XX_FW_FORMAT" in
380 img
=$
(platform_img_from_safeloader
"$1")
381 default_do_upgrade
"$img"
384 default_do_upgrade
"$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
387 default_do_upgrade
"$1"
390 case "$BCM53XX_FW_INT_IMG_FORMAT" in
392 default_do_upgrade
"$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
399 platform_do_upgrade
() {
400 case "$(board_name)" in
403 CI_KERNPART
="part.safe"
407 platform_other_do_upgrade
"$1"