bcm53xx: detect Seama sysupgrade format
[openwrt/svn-archive/archive.git] / target / linux / bcm53xx / 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 -e '1/1 "%02x"'
7 }
8
9 platform_machine() {
10 cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
11 }
12
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 && {
16 echo "serial"
17 return
18 }
19
20 echo "nand"
21 }
22
23 platform_expected_image() {
24 local machine=$(platform_machine)
25
26 case "$machine" in
27 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
28 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
29 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
30 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
31 esac
32 }
33
34 platform_identify() {
35 local magic
36
37 magic=$(get_magic_long "$1")
38 case "$magic" in
39 "48445230")
40 echo "trx"
41 return
42 ;;
43 "2a23245e")
44 echo "chk"
45 return
46 ;;
47 "5ea3a417")
48 echo "seama"
49 return
50 ;;
51 esac
52
53 magic=$(get_magic_long_at "$1" 14)
54 [ "$magic" = "55324e44" ] && {
55 echo "cybertan"
56 return
57 }
58
59 echo "unknown"
60 }
61
62 platform_check_image() {
63 [ "$#" -gt 1 ] && return 1
64
65 local file_type=$(platform_identify "$1")
66 local magic
67 local error=0
68
69 case "$file_type" in
70 "chk")
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"
76
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)"
79 error=1
80 }
81
82 if ! otrx check "$1" -o "$header_len"; then
83 echo "No valid TRX firmware in the CHK image"
84 error=1
85 fi
86 ;;
87 "cybertan")
88 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
89 local dev_pattern=$(platform_expected_image)
90 echo "Found CyberTAN image with device pattern: $pattern"
91
92 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
93 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
94 error=1
95 }
96
97 if ! otrx check "$1" -o 32; then
98 echo "No valid TRX firmware in the CyberTAN image"
99 error=1
100 fi
101 ;;
102 "seama")
103 echo "Seama firmware format is unsupported"
104 error=1
105 ;;
106 "trx")
107 if ! otrx check "$1"; then
108 echo "Invalid (corrupted?) TRX firmware"
109 error=1
110 fi
111 ;;
112 *)
113 echo "Invalid image type. Please use only .trx files"
114 error=1
115 ;;
116 esac
117
118 return $error
119 }
120
121 platform_pre_upgrade() {
122 local file_type=$(platform_identify "$1")
123 local dir="/tmp/sysupgrade-bcm53xx"
124 local trx="$1"
125 local offset
126
127 [ "$(platform_flash_type)" != "nand" ] && return
128
129 # Find trx offset
130 case "$file_type" in
131 "chk") offset=$((0x$(get_magic_long_at "$1" 4)));;
132 "cybertan") offset=32;;
133 esac
134
135 # Extract partitions from trx
136 rm -fR $dir
137 mkdir -p $dir
138 otrx extract "$trx" \
139 ${offset:+-o $offset} \
140 -1 $dir/kernel \
141 -2 $dir/root
142 [ $? -ne 0 ] && {
143 echo "Failed to extract TRX partitions."
144 return
145 }
146
147 # Firmwares without UBI image should be flashed "normally"
148 local root_type=$(identify $dir/root)
149 [ "$root_type" != "ubi" ] && {
150 echo "Provided firmware doesn't use UBI for rootfs."
151 return
152 }
153
154 # Prepare TRX file with just a kernel that will replace current one
155 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
156 [ -z "$linux_length" ] && {
157 echo "Unable to find \"linux\" partition size"
158 exit 1
159 }
160 linux_length=$((0x$linux_length))
161 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
162 [ $kernel_length -gt $linux_length ] && {
163 echo "New kernel doesn't fit \"linux\" partition."
164 return
165 }
166 rm -f /tmp/null.bin
167 rm -f /tmp/kernel.trx
168 touch /tmp/null.bin
169 otrx create /tmp/kernel.trx \
170 -f $dir/kernel -b $(($linux_length + 28)) \
171 -f /tmp/null.bin
172 [ $? -ne 0 ] && {
173 echo "Failed to create simple TRX with new kernel."
174 return
175 }
176
177 # Prepare UBI image (drop unwanted extra blocks)
178 local ubi_length=0
179 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
180 ubi_length=$(($ubi_length + 131072))
181 done
182 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
183 [ $? -ne 0 ] && {
184 echo "Failed to prepare new UBI image."
185 return
186 }
187
188 # Flash
189 mtd write /tmp/kernel.trx firmware
190 nand_do_upgrade /tmp/root.ubi
191 }
192
193 platform_trx_from_chk_cmd() {
194 local header_len=$((0x$(get_magic_long_at "$1" 4)))
195
196 echo -n dd bs=$header_len skip=1
197 }
198
199 platform_trx_from_cybertan_cmd() {
200 echo -n dd bs=32 skip=1
201 }
202
203 platform_do_upgrade() {
204 local file_type=$(platform_identify "$1")
205 local trx="$1"
206 local cmd=
207
208 [ "$(platform_flash_type)" == "nand" ] && {
209 echo "Writing whole image to NAND flash. All erase counters will be lost."
210 }
211
212 case "$file_type" in
213 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
214 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
215 esac
216
217 default_do_upgrade "$trx" "$cmd"
218 }