f5b9bda6901fc391746fb46a60f98d95c0c10bd3
[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 # $(1): image that should contain trx
81 # $(2): trx offset in image
82 platform_check_image_trx() {
83 local magic=$(get_magic_long_at "$1" $2)
84
85 [ "$magic" != "48445230" ] && {
86 return 1
87 }
88
89 # TODO: Check crc32
90
91 return 0
92 }
93
94 platform_check_image() {
95 [ "$#" -gt 1 ] && return 1
96
97 local file_type=$(brcm47xx_identify "$1")
98 local magic
99 local error=0
100
101 case "$file_type" in
102 "chk")
103 local header_len=$((0x$(get_magic_long_at "$1" 4)))
104 local board_id_len=$(($header_len - 40))
105 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
106 local dev_board_id=$(platform_expected_image)
107 echo "Found CHK image with device board_id $board_id"
108
109 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
110 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
111 error=1
112 }
113
114 if ! platform_check_image_trx "$1" "$header_len"; then
115 echo "No valid TRX firmware in the CHK image"
116 error=1
117 fi
118 ;;
119 "cybertan")
120 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
121 local dev_pattern=$(platform_expected_image)
122 echo "Found CyberTAN image with device pattern: $pattern"
123
124 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
125 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
126 error=1
127 }
128
129 if ! platform_check_image_trx "$1" 32; then
130 echo "No valid TRX firmware in the CyberTAN image"
131 error=1
132 fi
133 ;;
134 "trx")
135 ;;
136 *)
137 echo "Invalid image type. Please use only .trx files"
138 error=1
139 ;;
140 esac
141
142 return $error
143 }
144
145 platform_do_upgrade_chk() {
146 local header_len=$((0x$(get_magic_long_at "$1" 4)))
147 local trx="/tmp/$1.trx"
148
149 dd if="$1" of="$trx" bs=$header_len skip=1
150 shift
151 default_do_upgrade "$trx" "$@"
152 }
153
154 platform_do_upgrade_cybertan() {
155 local trx="/tmp/$1.trx"
156
157 dd if="$1" of="$trx" bs=32 skip=1
158 shift
159 default_do_upgrade "$trx" "$@"
160 }
161
162 platform_do_upgrade() {
163 local file_type=$(brcm47xx_identify "$1")
164
165 case "$file_type" in
166 "chk") platform_do_upgrade_chk "$ARGV";;
167 "cybertan") platform_do_upgrade_cybertan "$ARGV";;
168 *) default_do_upgrade "$ARGV";;
169 esac
170 }