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 [ "$(platform_flash_type)" = "nand" ] && {
61 echo "Firmware upgrade on NAND devices is not implemented."
65 local file_type=$(platform_identify "$1")
71 local header_len=$((0x$(get_magic_long_at "$1" 4)))
72 local board_id_len=$(($header_len - 40))
73 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
74 local dev_board_id=$(platform_expected_image)
75 echo "Found CHK image with device board_id $board_id"
77 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
78 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
82 magic=$(get_magic_long_at "$1" "$header_len")
83 [ "$magic" != "48445230" ] && {
84 echo "No valid TRX firmware in the CHK image"
89 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
90 local dev_pattern=$(platform_expected_image)
91 echo "Found CyberTAN image with device pattern: $pattern"
93 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
94 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
98 magic=$(get_magic_long_at "$1" 32)
99 [ "$magic" != "48445230" ] && {
100 echo "No valid TRX firmware in the CyberTAN image"
107 echo "Invalid image type. Please use only .trx files"
115 # Extract TRX and use stadard upgrade method
116 platform_do_upgrade_chk() {
117 local header_len=$((0x$(get_magic_long_at "$1" 4)))
118 local trx="/tmp/$1.trx"
120 dd if="$1" of="$trx" bs=$header_len skip=1
122 platform_do_upgrade_trx "$trx" "$@"
125 # Extract TRX and use stadard upgrade method
126 platform_do_upgrade_cybertan() {
127 local trx="/tmp/$1.trx"
129 dd if="$1" of="$trx" bs=32 skip=1
131 platform_do_upgrade_trx "$trx" "$@"
134 platform_do_upgrade_trx() {
135 local flash_type=$(platform_flash_type)
137 case "$flash_type" in
139 default_do_upgrade "$@"
142 echo "Firmware upgrade on NAND devices is REALLY unsupported."
147 platform_do_upgrade() {
148 local file_type=$(platform_identify "$1")
151 "chk") platform_do_upgrade_chk "$ARGV";;
152 "cybertan") platform_do_upgrade_cybertan "$ARGV";;
153 *) platform_do_upgrade_trx "$ARGV";;