8c09706ee89cfcc2f22ece2fe482d1bc2b01a269
[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 magic=$(get_magic_long_at "$1" "$header_len")
101 [ "$magic" != "48445230" ] && {
102 echo "No valid TRX firmware in the CHK image"
103 error=1
104 }
105 ;;
106 "cybertan")
107 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
108 local dev_pattern=$(platform_expected_image)
109 echo "Found CyberTAN image with device pattern: $pattern"
110
111 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
112 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
113 error=1
114 }
115
116 magic=$(get_magic_long_at "$1" 32)
117 [ "$magic" != "48445230" ] && {
118 echo "No valid TRX firmware in the CyberTAN image"
119 error=1
120 }
121 ;;
122 "trx")
123 ;;
124 *)
125 echo "Invalid image type. Please use only .trx files"
126 error=1
127 ;;
128 esac
129
130 return $error
131 }
132
133 platform_do_upgrade_chk() {
134 local header_len=$((0x$(get_magic_long_at "$1" 4)))
135 local trx="/tmp/$1.trx"
136
137 dd if="$1" of="$trx" bs=$header_len skip=1
138 shift
139 default_do_upgrade "$trx" "$@"
140 }
141
142 platform_do_upgrade_cybertan() {
143 local trx="/tmp/$1.trx"
144
145 dd if="$1" of="$trx" bs=32 skip=1
146 shift
147 default_do_upgrade "$trx" "$@"
148 }
149
150 platform_do_upgrade() {
151 local file_type=$(brcm47xx_identify "$1")
152
153 case "$file_type" in
154 "chk") platform_do_upgrade_chk "$ARGV";;
155 "cybertan") platform_do_upgrade_cybertan "$ARGV";;
156 *) default_do_upgrade "$ARGV";;
157 esac
158 }