bcm53xx: build image for TP-LINK Archer C9 v1
[openwrt/openwrt.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 "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
28 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
29 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
30 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
31 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
32 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
33 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
34 "tplink,archer-c9-v1") echo "safeloader"; return;;
35 esac
36 }
37
38 platform_identify() {
39 local magic
40
41 magic=$(get_magic_long "$1")
42 case "$magic" in
43 "48445230")
44 echo "trx"
45 return
46 ;;
47 "2a23245e")
48 echo "chk"
49 return
50 ;;
51 "5ea3a417")
52 echo "seama"
53 return
54 ;;
55 esac
56
57 magic=$(get_magic_long_at "$1" 14)
58 [ "$magic" = "55324e44" ] && {
59 echo "cybertan"
60 return
61 }
62
63 if osafeloader info "$1" > /dev/null; then
64 echo "safeloader"
65 return
66 fi
67
68 echo "unknown"
69 }
70
71 platform_check_image() {
72 [ "$#" -gt 1 ] && return 1
73
74 local file_type=$(platform_identify "$1")
75 local magic
76 local error=0
77
78 case "$file_type" in
79 "chk")
80 local header_len=$((0x$(get_magic_long_at "$1" 4)))
81 local board_id_len=$(($header_len - 40))
82 local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
83 local dev_board_id=$(platform_expected_image)
84 echo "Found CHK image with device board_id $board_id"
85
86 [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
87 echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
88 error=1
89 }
90
91 if ! otrx check "$1" -o "$header_len"; then
92 echo "No valid TRX firmware in the CHK image"
93 error=1
94 fi
95 ;;
96 "cybertan")
97 local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
98 local dev_pattern=$(platform_expected_image)
99 echo "Found CyberTAN image with device pattern: $pattern"
100
101 [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
102 echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
103 error=1
104 }
105
106 if ! otrx check "$1" -o 32; then
107 echo "No valid TRX firmware in the CyberTAN image"
108 error=1
109 fi
110 ;;
111 "safeloader")
112 ;;
113 "seama")
114 local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
115 local dev_signature=$(platform_expected_image)
116 echo "Found Seama image with device signature: $img_signature"
117
118 [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
119 echo "Firmware signature doesn't match device signature ($dev_signature)"
120 error=1
121 }
122
123 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
124 echo "Seama container doesn't have firmware entity"
125 error=1
126 }
127 ;;
128 "trx")
129 local expected=$(platform_expected_image)
130
131 [ "$expected" == "safeloader" ] && {
132 echo "This device expects SafeLoader format and may not work with TRX"
133 error=1
134 }
135
136 if ! otrx check "$1"; then
137 echo "Invalid (corrupted?) TRX firmware"
138 error=1
139 fi
140 ;;
141 *)
142 echo "Invalid image type. Please use only .trx files"
143 error=1
144 ;;
145 esac
146
147 return $error
148 }
149
150 # $(1): image for upgrade (with possible extra header)
151 # $(2): offset of trx in image
152 platform_pre_upgrade_trx() {
153 local dir="/tmp/sysupgrade-bcm53xx"
154 local trx="$1"
155 local offset="$2"
156
157 # Extract partitions from trx
158 rm -fR $dir
159 mkdir -p $dir
160 otrx extract "$trx" \
161 ${offset:+-o $offset} \
162 -1 $dir/kernel \
163 -2 $dir/root
164 [ $? -ne 0 ] && {
165 echo "Failed to extract TRX partitions."
166 return
167 }
168
169 # Firmwares without UBI image should be flashed "normally"
170 local root_type=$(identify $dir/root)
171 [ "$root_type" != "ubi" ] && {
172 echo "Provided firmware doesn't use UBI for rootfs."
173 return
174 }
175
176 # Prepare TRX file with just a kernel that will replace current one
177 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
178 [ -z "$linux_length" ] && {
179 echo "Unable to find \"linux\" partition size"
180 exit 1
181 }
182 linux_length=$((0x$linux_length))
183 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
184 [ $kernel_length -gt $linux_length ] && {
185 echo "New kernel doesn't fit \"linux\" partition."
186 return
187 }
188 rm -f /tmp/null.bin
189 rm -f /tmp/kernel.trx
190 touch /tmp/null.bin
191 otrx create /tmp/kernel.trx \
192 -f $dir/kernel -b $(($linux_length + 28)) \
193 -f /tmp/null.bin
194 [ $? -ne 0 ] && {
195 echo "Failed to create simple TRX with new kernel."
196 return
197 }
198
199 # Prepare UBI image (drop unwanted extra blocks)
200 local ubi_length=0
201 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
202 ubi_length=$(($ubi_length + 131072))
203 done
204 dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
205 [ $? -ne 0 ] && {
206 echo "Failed to prepare new UBI image."
207 return
208 }
209
210 # Flash
211 mtd write /tmp/kernel.trx firmware
212 nand_do_upgrade /tmp/root.ubi
213 }
214
215 platform_pre_upgrade_seama() {
216 local dir="/tmp/sysupgrade-bcm53xx"
217 local seama="$1"
218 local tmp
219
220 # Extract Seama entity from Seama seal
221 rm -fR $dir
222 mkdir -p $dir
223 oseama extract "$seama" \
224 -e 0 \
225 -o $dir/seama.entity
226 [ $? -ne 0 ] && {
227 echo "Failed to extract Seama entity."
228 return
229 }
230 local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
231
232 local ubi_offset=0
233 tmp=0
234 while [ 1 ]; do
235 [ $tmp -ge $entity_size ] && break
236 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
237 ubi_offset=$tmp
238 break
239 }
240 tmp=$(($tmp + 131072))
241 done
242 [ $ubi_offset -eq 0 ] && {
243 echo "Failed to find UBI in Seama entity."
244 return
245 }
246
247 local ubi_length=0
248 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
249 ubi_length=$(($ubi_length + 131072))
250 done
251
252 dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
253 dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
254
255 # Flash
256 local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
257 mtd write $dir/kernel.seama firmware
258 mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
259 nand_do_upgrade $dir/root.ubi
260 }
261
262 platform_pre_upgrade() {
263 export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/osafeloader /usr/bin/oseama /bin/sed"
264
265 local file_type=$(platform_identify "$1")
266
267 [ "$(platform_flash_type)" != "nand" ] && return
268
269 # Find trx offset
270 case "$file_type" in
271 "chk") platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
272 "cybertan") platform_pre_upgrade_trx "$1" 32;;
273 "seama") platform_pre_upgrade_seama "$1";;
274 "trx") platform_pre_upgrade_trx "$1";;
275 esac
276 }
277
278 platform_trx_from_chk_cmd() {
279 local header_len=$((0x$(get_magic_long_at "$1" 4)))
280
281 echo -n dd bs=$header_len skip=1
282 }
283
284 platform_trx_from_cybertan_cmd() {
285 echo -n dd bs=32 skip=1
286 }
287
288 platform_img_from_safeloader() {
289 local dir="/tmp/sysupgrade-bcm53xx"
290
291 # Extract partitions from SafeLoader
292 rm -fR $dir
293 mkdir -p $dir
294 osafeloader extract "$1" \
295 -p "os-image" \
296 -o $dir/os-image
297 osafeloader extract "$1" \
298 -p "file-system" \
299 -o $dir/file-system
300
301 mtd write $dir/file-system rootfs
302
303 echo -n $dir/os-image
304 }
305
306 platform_img_from_seama() {
307 local dir="/tmp/sysupgrade-bcm53xx"
308 local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
309 local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
310
311 # Busybox doesn't support required iflag-s
312 # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
313
314 rm -fR $dir
315 mkdir -p $dir
316 dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
317 dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
318
319 echo -n $dir/image-entity.bin
320 }
321
322 platform_do_upgrade() {
323 local file_type=$(platform_identify "$1")
324 local trx="$1"
325 local cmd=
326
327 [ "$(platform_flash_type)" == "nand" ] && {
328 echo "Writing whole image to NAND flash. All erase counters will be lost."
329 }
330
331 case "$file_type" in
332 "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
333 "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
334 "safeloader") trx=$(platform_img_from_safeloader "$trx");;
335 "seama") trx=$(platform_img_from_seama "$trx");;
336 esac
337
338 default_do_upgrade "$trx" "$cmd"
339 }