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 "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
28 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
29 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
30 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
31 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
32 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
33 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
40 magic
=$
(get_magic_long
"$1")
56 magic
=$
(get_magic_long_at
"$1" 14)
57 [ "$magic" = "55324e44" ] && {
65 platform_check_image
() {
66 [ "$#" -gt 1 ] && return 1
68 local file_type
=$
(platform_identify
"$1")
74 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
75 local board_id_len
=$
(($header_len - 40))
76 local board_id
=$
(dd if="$1" skip
=40 bs
=1 count
=$board_id_len 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
77 local dev_board_id
=$
(platform_expected_image
)
78 echo "Found CHK image with device board_id $board_id"
80 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
81 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
85 if ! otrx check
"$1" -o "$header_len"; then
86 echo "No valid TRX firmware in the CHK image"
91 local pattern
=$
(dd if="$1" bs
=1 count
=4 2>/dev
/null |
hexdump -v -e '1/1 "%c"')
92 local dev_pattern
=$
(platform_expected_image
)
93 echo "Found CyberTAN image with device pattern: $pattern"
95 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
96 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
100 if ! otrx check
"$1" -o 32; then
101 echo "No valid TRX firmware in the CyberTAN image"
106 local img_signature
=$
(oseama info
"$1" |
grep "Meta entry:.*signature=" |
sed "s/.*=//")
107 local dev_signature
=$
(platform_expected_image
)
108 echo "Found Seama image with device signature: $img_signature"
110 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
111 echo "Firmware signature doesn't match device signature ($dev_signature)"
115 $
(oseama info
"$1" -e 0 |
grep -q "Meta entry:.*type=firmware") ||
{
116 echo "Seama container doesn't have firmware entity"
121 if ! otrx check
"$1"; then
122 echo "Invalid (corrupted?) TRX firmware"
127 echo "Invalid image type. Please use only .trx files"
135 # $(1): image for upgrade (with possible extra header)
136 # $(2): offset of trx in image
137 platform_pre_upgrade_trx
() {
138 local dir
="/tmp/sysupgrade-bcm53xx"
142 # Extract partitions from trx
145 otrx extract
"$trx" \
146 ${offset:+-o $offset} \
150 echo "Failed to extract TRX partitions."
154 # Firmwares without UBI image should be flashed "normally"
155 local root_type
=$
(identify
$dir/root
)
156 [ "$root_type" != "ubi" ] && {
157 echo "Provided firmware doesn't use UBI for rootfs."
161 # Prepare TRX file with just a kernel that will replace current one
162 local linux_length
=$
(grep "\"linux\"" /proc
/mtd |
sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
163 [ -z "$linux_length" ] && {
164 echo "Unable to find \"linux\" partition size"
167 linux_length
=$
((0x
$linux_length))
168 local kernel_length
=$
(wc -c $dir/kernel | cut
-d ' ' -f 1)
169 [ $kernel_length -gt $linux_length ] && {
170 echo "New kernel doesn't fit \"linux\" partition."
174 rm -f /tmp
/kernel.trx
176 otrx create
/tmp
/kernel.trx \
177 -f $dir/kernel
-b $
(($linux_length + 28)) \
180 echo "Failed to create simple TRX with new kernel."
184 # Prepare UBI image (drop unwanted extra blocks)
186 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
187 ubi_length
=$
(($ubi_length + 131072))
189 dd if=$dir/root of
=/tmp
/root.ubi bs
=131072 count
=$
((ubi_length
/ 131072)) 2>/dev
/null
191 echo "Failed to prepare new UBI image."
196 mtd
write /tmp
/kernel.trx firmware
197 nand_do_upgrade
/tmp
/root.ubi
200 platform_pre_upgrade
() {
201 export RAMFS_COPY_BIN
="${RAMFS_COPY_BIN} /usr/bin/oseama /bin/sed"
203 local file_type
=$
(platform_identify
"$1")
205 [ "$(platform_flash_type)" != "nand" ] && return
209 "chk") platform_pre_upgrade_trx
"$1" $
((0x$
(get_magic_long_at
"$1" 4)));;
210 "cybertan") platform_pre_upgrade_trx
"$1" 32;;
212 "trx") platform_pre_upgrade_trx
"$1";;
216 platform_trx_from_chk_cmd
() {
217 local header_len
=$
((0x$
(get_magic_long_at
"$1" 4)))
219 echo -n dd bs
=$header_len skip
=1
222 platform_trx_from_cybertan_cmd
() {
223 echo -n dd bs
=32 skip
=1
226 platform_img_from_seama
() {
227 local dir
="/tmp/sysupgrade-bcm53xx"
228 local offset
=$
(oseama info
"$1" -e 0 |
grep "Entity offset:" |
sed "s/.*:\s*//")
229 local size
=$
(oseama info
"$1" -e 0 |
grep "Entity size:" |
sed "s/.*:\s*//")
231 # Busybox doesn't support required iflag-s
232 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
236 dd if="$1" of
=$dir/image-noheader.bin bs
=$offset skip
=1
237 dd if=$dir/image-noheader.bin of
=$dir/image-entity.bin bs
=$size count
=1
239 echo -n $dir/image-entity.bin
242 platform_do_upgrade
() {
243 local file_type
=$
(platform_identify
"$1")
247 [ "$(platform_flash_type)" == "nand" ] && {
248 echo "Writing whole image to NAND flash. All erase counters will be lost."
252 "chk") cmd
=$
(platform_trx_from_chk_cmd
"$trx");;
253 "cybertan") cmd
=$
(platform_trx_from_cybertan_cmd
"$trx");;
254 "seama") trx
=$
(platform_img_from_seama
"$trx");;
257 default_do_upgrade
"$trx" "$cmd"