3 # $(1): file to read magic from
4 # $(2): offset in bytes
6 dd if="$1" skip
=$2 bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%02x"'
10 cat /proc
/device-tree
/compatible |
tr '\0' '\t' | cut
-f 1
13 platform_flash_type
() {
14 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
15 grep -q "\"rootfs\"" /proc
/mtd
&& {
23 platform_expected_image
() {
24 local machine
=$
(platform_machine
)
27 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
28 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
29 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
36 magic
=$
(get_magic_long
"$1")
48 magic
=$
(get_magic_long_at
"$1" 14)
49 [ "$magic" = "55324e44" ] && {
57 platform_check_image
() {
58 [ "$#" -gt 1 ] && return 1
60 local file_type
=$
(platform_identify
"$1")
66 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
67 local board_id_len
=$
(($header_len - 40))
68 local board_id
=$
(dd if="$1" skip
=40 bs
=1 count
=$board_id_len 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
69 local dev_board_id
=$
(platform_expected_image
)
70 echo "Found CHK image with device board_id $board_id"
72 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
73 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
77 if ! otrx check
"$1" -o "$header_len"; then
78 echo "No valid TRX firmware in the CHK image"
83 local pattern
=$
(dd if="$1" bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
84 local dev_pattern
=$
(platform_expected_image
)
85 echo "Found CyberTAN image with device pattern: $pattern"
87 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
88 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
92 if ! otrx check
"$1" -o 32; then
93 echo "No valid TRX firmware in the CyberTAN image"
98 if ! otrx check
"$1"; then
99 echo "Invalid (corrupted?) TRX firmware"
104 echo "Invalid image type. Please use only .trx files"
112 platform_extract_trx_from_chk
() {
113 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
115 dd if="$1" of
="$2" bs
=$header_len skip
=1
118 platform_extract_trx_from_cybertan
() {
119 dd if="$1" of
="$2" bs
=32 skip
=1
122 platform_pre_upgrade
() {
123 local file_type
=$
(platform_identify
"$1")
124 local dir
="/tmp/sysupgrade-bcm53xx"
127 [ "$(platform_flash_type)" != "nand" ] && return
131 "chk") trx
="/tmp/$(basename $1).trx"; platform_extract_trx_from_chk
"$1" "$trx";;
132 "cybertan") trx
="/tmp/$(basename $1).trx"; platform_extract_trx_from_cybertan
"$1" "$trx";;
135 # Extract partitions from trx
138 otrx extract
"$trx" \
142 # Firmwares without UBI image should be flashed "normally"
143 local root_type
=$
(identify
$dir/root
)
144 [ "$root_type" != "ubi" ] && return
146 # Prepare TRX file with just a kernel that will replace current one
147 local linux_length
=$
(grep "\"linux\"" /proc
/mtd |
sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
148 [ -z "$linux_length" ] && {
149 echo "Unable to find \"linux\" partition size"
152 linux_length
=$
((0x
$linux_length + 28))
154 rm -f /tmp
/kernel.trx
156 otrx create
/tmp
/kernel.trx \
157 -f $dir/kernel
-b $linux_length \
160 # Prepare UBI image (drop unwanted extra blocks)
162 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
163 ubi_length
=$
(($ubi_length + 131072))
165 dd if=$dir/root of
=/tmp
/root.ubi bs
=131072 count
=$
((ubi_length
/ 131072)) 2>/dev
/null
168 mtd
write /tmp
/kernel.trx firmware
169 nand_do_upgrade
/tmp
/root.ubi
172 platform_do_upgrade
() {
173 local file_type
=$
(platform_identify
"$1")
176 [ "$(platform_flash_type)" == "nand" ] && {
177 echo "Flashing firmware without UBI for rootfs. All erase counters will be lost."
181 "chk") trx
="/tmp/$(basename $1).trx"; platform_extract_trx_from_chk
"$1" "$trx";;
182 "cybertan") trx
="/tmp/$(basename $1).trx"; platform_extract_trx_from_cybertan
"$1" "$trx";;
186 default_do_upgrade
"$trx" "$@"