brcm47xx: switch to the otrx for verifying TRX images
[openwrt/openwrt.git] / target / linux / brcm47xx / base-files / lib / upgrade / platform.sh
1 PART_NAME=firmware
2
3 # $(1): file to read magic from
4 # $(2): offset in bytes
5 get_magic_long_at() {
6 dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
7 }
8
9 platform_machine() {
10 grep "machine" /proc/cpuinfo | sed "s/.*:[ \t]*//"
11 }
12
13 platform_expected_image() {
14 local machine=$(platform_machine)
15
16 case "$machine" in
17 "Netgear WGR614 V8") echo "chk U12H072T00_NETGEAR"; return;;
18 "Netgear WGR614 V9") echo "chk U12H094T00_NETGEAR"; return;;
19 "Netgear WGR614 V10") echo "chk U12H139T01_NETGEAR"; return;;
20 "Netgear WNDR3300") echo "chk U12H093T00_NETGEAR"; return;;
21 "Netgear WNDR3400 V1") echo "chk U12H155T00_NETGEAR"; return;;
22 "Netgear WNDR3400 V2") echo "chk U12H187T00_NETGEAR"; return;;
23 "Netgear WNDR3400 V3") echo "chk U12H208T00_NETGEAR"; return;;
24 "Netgear WNDR3400 Vcna") echo "chk U12H155T01_NETGEAR"; return;;
25 "Netgear WNDR3700 V3") echo "chk U12H194T00_NETGEAR"; return;;
26 "Netgear WNDR4000") echo "chk U12H181T00_NETGEAR"; return;;
27 "Netgear WNDR4500 V1") echo "chk U12H189T00_NETGEAR"; return;;
28 "Netgear WNDR4500 V2") echo "chk U12H224T00_NETGEAR"; return;;
29 "Netgear WNR2000 V2") echo "chk U12H114T00_NETGEAR"; return;;
30 "Netgear WNR3500L") echo "chk U12H136T99_NETGEAR"; return;;
31 "Netgear WNR3500U") echo "chk U12H136T00_NETGEAR"; return;;
32 "Netgear WNR3500 V2") echo "chk U12H127T00_NETGEAR"; return;;
33 "Netgear WNR3500 V2vc") echo "chk U12H127T70_NETGEAR"; return;;
34 "Netgear WNR834B V2") echo "chk U12H081T00_NETGEAR"; return;;
35 "Linksys E900 V1") echo "cybertan E900"; return;;
36 "Linksys E1000 V1") echo "cybertan E100"; return;;
37 "Linksys E1000 V2") echo "cybertan E100"; return;;
38 "Linksys E1000 V2.1") echo "cybertan E100"; return;;
39 "Linksys E1200 V2") echo "cybertan E122"; return;;
40 "Linksys E2000 V1") echo "cybertan 32XN"; return;;
41 "Linksys E3000 V1") echo "cybertan 61XN"; return;;
42 "Linksys E3200 V1") echo "cybertan 3200"; return;;
43 "Linksys E4200 V1") echo "cybertan 4200"; return;;
44 "Linksys WRT150N V1.1") echo "cybertan N150"; return;;
45 "Linksys WRT150N V1") echo "cybertan N150"; return;;
46 "Linksys WRT160N V1") echo "cybertan N150"; return;;
47 "Linksys WRT160N V3") echo "cybertan N150"; return;;
48 "Linksys WRT300N V1.1") echo "cybertan EWCB"; return;;
49 "Linksys WRT310N V1") echo "cybertan 310N"; return;;
50 "Linksys WRT310N V2") echo "cybertan 310N"; return;;
51 "Linksys WRT610N V1") echo "cybertan 610N"; return;;
52 "Linksys WRT610N V2") echo "cybertan 610N"; return;;
53 esac
54 }
55
56 brcm47xx_identify() {
57 local magic
58
59 magic=$(get_magic_long "$1")
60 case "$magic" in
61 "48445230")
62 echo "trx"
63 return
64 ;;
65 "2a23245e")
66 echo "chk"
67 return
68 ;;
69 esac
70
71 magic=$(get_magic_long_at "$1" 14)
72 [ "$magic" = "55324e44" ] && {
73 echo "cybertan"
74 return
75 }
76
77 echo "unknown"
78 }
79
80 platform_check_image() {
81 [ "$#" -gt 1 ] && return 1
82
83 local file_type=$(brcm47xx_identify "$1")
84 local magic
85 local error=0
86
87 case "$file_type" in
88 "chk")
89 local header_len=$((0x$(get_magic_long_at "$1" 4)))
90 local board_id_len=$(($header_len - 40))
91 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
92 local dev_board_id=$(platform_expected_image)
93 echo "Found CHK image with device board_id $board_id"
94
95 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
96 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
97 error=1
98 }
99
100 if ! otrx -c "$1" -o "$header_len"; then
101 echo "No valid TRX firmware in the CHK image"
102 error=1
103 fi
104 ;;
105 "cybertan")
106 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
107 local dev_pattern=$(platform_expected_image)
108 echo "Found CyberTAN image with device pattern: $pattern"
109
110 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
111 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
112 error=1
113 }
114
115 if ! otrx -c "$1" -o 32; then
116 echo "No valid TRX firmware in the CyberTAN image"
117 error=1
118 fi
119 ;;
120 "trx")
121 ;;
122 *)
123 echo "Invalid image type. Please use only .trx files"
124 error=1
125 ;;
126 esac
127
128 return $error
129 }
130
131 platform_do_upgrade_chk() {
132 local header_len=$((0x$(get_magic_long_at "$1" 4)))
133 local trx="/tmp/$1.trx"
134
135 dd if="$1" of="$trx" bs=$header_len skip=1
136 shift
137 default_do_upgrade "$trx" "$@"
138 }
139
140 platform_do_upgrade_cybertan() {
141 local trx="/tmp/$1.trx"
142
143 dd if="$1" of="$trx" bs=32 skip=1
144 shift
145 default_do_upgrade "$trx" "$@"
146 }
147
148 platform_do_upgrade() {
149 local file_type=$(brcm47xx_identify "$1")
150
151 case "$file_type" in
152 "chk") platform_do_upgrade_chk "$ARGV";;
153 "cybertan") platform_do_upgrade_cybertan "$ARGV";;
154 *) default_do_upgrade "$ARGV";;
155 esac
156 }