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,r7000") echo "chk U12H270T00_NETGEAR"; return;;
30 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
37 magic
=$
(get_magic_long
"$1")
49 magic
=$
(get_magic_long_at
"$1" 14)
50 [ "$magic" = "55324e44" ] && {
58 platform_check_image
() {
59 [ "$#" -gt 1 ] && return 1
61 local file_type
=$
(platform_identify
"$1")
67 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
68 local board_id_len
=$
(($header_len - 40))
69 local board_id
=$
(dd if="$1" skip
=40 bs
=1 count
=$board_id_len 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
70 local dev_board_id
=$
(platform_expected_image
)
71 echo "Found CHK image with device board_id $board_id"
73 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
74 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
78 if ! otrx check
"$1" -o "$header_len"; then
79 echo "No valid TRX firmware in the CHK image"
84 local pattern
=$
(dd if="$1" bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
85 local dev_pattern
=$
(platform_expected_image
)
86 echo "Found CyberTAN image with device pattern: $pattern"
88 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
89 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
93 if ! otrx check
"$1" -o 32; then
94 echo "No valid TRX firmware in the CyberTAN image"
99 if ! otrx check
"$1"; then
100 echo "Invalid (corrupted?) TRX firmware"
105 echo "Invalid image type. Please use only .trx files"
113 platform_pre_upgrade
() {
114 local file_type
=$
(platform_identify
"$1")
115 local dir
="/tmp/sysupgrade-bcm53xx"
119 [ "$(platform_flash_type)" != "nand" ] && return
123 "chk") offset
=$
((0x$
(get_magic_long_at
"$1" 4)));;
124 "cybertan") offset
=32;;
127 # Extract partitions from trx
130 otrx extract
"$trx" \
131 ${offset:+-o $offset} \
135 echo "Failed to extract TRX partitions."
139 # Firmwares without UBI image should be flashed "normally"
140 local root_type
=$
(identify
$dir/root
)
141 [ "$root_type" != "ubi" ] && {
142 echo "Provided firmware doesn't use UBI for rootfs."
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))
153 local kernel_length
=$
(wc -c $dir/kernel | cut
-d ' ' -f 1)
154 [ $kernel_length -gt $linux_length ] && {
155 echo "New kernel doesn't fit \"linux\" partition."
159 rm -f /tmp
/kernel.trx
161 otrx create
/tmp
/kernel.trx \
162 -f $dir/kernel
-b $
(($linux_length + 28)) \
165 echo "Failed to create simple TRX with new kernel."
169 # Prepare UBI image (drop unwanted extra blocks)
171 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
172 ubi_length
=$
(($ubi_length + 131072))
174 dd if=$dir/root of
=/tmp
/root.ubi bs
=131072 count
=$
((ubi_length
/ 131072)) 2>/dev
/null
176 echo "Failed to prepare new UBI image."
181 mtd
write /tmp
/kernel.trx firmware
182 nand_do_upgrade
/tmp
/root.ubi
185 platform_trx_from_chk_cmd
() {
186 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
188 echo -n dd bs
=$header_len skip
=1
191 platform_trx_from_cybertan_cmd
() {
192 echo -n dd bs
=32 skip
=1
195 platform_do_upgrade
() {
196 local file_type
=$
(platform_identify
"$1")
200 [ "$(platform_flash_type)" == "nand" ] && {
201 echo "Writing whole image to NAND flash. All erase counters will be lost."
205 "chk") cmd
=$
(platform_trx_from_chk_cmd
"$trx");;
206 "cybertan") cmd
=$
(platform_trx_from_cybertan_cmd
"$trx");;
209 default_do_upgrade
"$trx" "$cmd"