3ebde77d3f944c56579d148052f09f5e317cca05
[openwrt/staging/hauke.git] / target / linux / bcm53xx / base-files / lib / upgrade / platform.sh
1 RAMFS_COPY_BIN='osafeloader oseama otrx truncate'
2
3 PART_NAME=firmware
4
5 BCM53XX_FW_FORMAT=
6 BCM53XX_FW_BOARD_ID=
7 BCM53XX_FW_INT_IMG_FORMAT=
8 BCM53XX_FW_INT_IMG_TRX_OFFSET=
9 BCM53XX_FW_INT_IMG_EXTRACT_CMD=
10
11 LXL_FLAGS_VENDOR_LUXUL=0x00000001
12
13 # $(1): file to read magic from
14 # $(2): offset in bytes
15 get_magic_long_at() {
16 dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
17 }
18
19 # $(1): file to read LE long number from
20 # $(2): offset in bytes
21 get_le_long_at() {
22 echo $((0x$(dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/4 "%02x"')))
23 }
24
25 platform_flash_type() {
26 # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
27 grep -q "\"rootfs\"" /proc/mtd && {
28 echo "serial"
29 return
30 }
31
32 echo "nand"
33 }
34
35 platform_expected_image() {
36 local machine=$(board_name)
37
38 case "$machine" in
39 "dlink,dir-885l") echo "seamaseal wrgac42_dlink.2015_dir885l"; return;;
40 "luxul,abr-4500-v1") echo "lxl ABR-4500"; return;;
41 "luxul,xap-810-v1") echo "lxl XAP-810"; return;;
42 "luxul,xap-1410v1") echo "lxl XAP-1410"; return;;
43 "luxul,xap-1440-v1") echo "lxl XAP-1440"; return;;
44 "luxul,xap-1510v1") echo "lxl XAP-1510"; return;;
45 "luxul,xap-1610-v1") echo "lxl XAP-1610"; return;;
46 "luxul,xbr-4500-v1") echo "lxl XBR-4500"; return;;
47 "luxul,xwc-1000") echo "lxl XWC-1000"; return;;
48 "luxul,xwc-2000-v1") echo "lxl XWC-2000"; return;;
49 "luxul,xwr-1200v1") echo "lxl XWR-1200"; return;;
50 "luxul,xwr-3100v1") echo "lxl XWR-3100"; return;;
51 "luxul,xwr-3150-v1") echo "lxl XWR-3150"; return;;
52 "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
53 "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
54 "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
55 "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
56 "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
57 "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
58 "tplink,archer-c9-v1") echo "safeloader"; return;;
59 esac
60 }
61
62 platform_identify() {
63 local magic
64
65 magic=$(get_magic_long "$1")
66 case "$magic" in
67 "48445230")
68 BCM53XX_FW_FORMAT="trx"
69 return
70 ;;
71 "2a23245e")
72 local header_len=$((0x$(get_magic_long_at "$1" 4)))
73 local board_id_len=$(($header_len - 40))
74
75 BCM53XX_FW_FORMAT="chk"
76 BCM53XX_FW_BOARD_ID=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
77 BCM53XX_FW_INT_IMG_FORMAT="trx"
78 BCM53XX_FW_INT_IMG_TRX_OFFSET="$header_len"
79 BCM53XX_FW_INT_IMG_EXTRACT_CMD="dd skip=$header_len iflag=skip_bytes"
80 return
81 ;;
82 "4c584c23")
83 local hdr_len=$(get_le_long_at "$1" 8)
84 local flags=$(get_le_long_at "$1" 12)
85
86 [ $((flags & LXL_FLAGS_VENDOR_LUXUL)) -gt 0 ] && notify_firmware_no_backup
87
88 BCM53XX_FW_FORMAT="lxl"
89 BCM53XX_FW_BOARD_ID=$(dd if="$1" skip=16 bs=1 count=16 2>/dev/null | hexdump -v -e '1/1 "%c"')
90 BCM53XX_FW_INT_IMG_FORMAT="trx"
91 BCM53XX_FW_INT_IMG_TRX_OFFSET="$hdr_len"
92 BCM53XX_FW_INT_IMG_EXTRACT_CMD="dd skip=$hdr_len iflag=skip_bytes"
93
94 return
95 ;;
96 "5ea3a417")
97 BCM53XX_FW_FORMAT="seamaseal"
98 BCM53XX_FW_BOARD_ID=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
99 BCM53XX_FW_INT_IMG_EXTRACT_CMD="oseama extract - -e 0"
100 return
101 ;;
102 esac
103
104 magic=$(get_magic_long_at "$1" 14)
105 [ "$magic" = "55324e44" ] && {
106 BCM53XX_FW_FORMAT="cybertan"
107 BCM53XX_FW_BOARD_ID=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
108 BCM53XX_FW_INT_IMG_FORMAT="trx"
109 BCM53XX_FW_INT_IMG_TRX_OFFSET="32"
110 BCM53XX_FW_INT_IMG_EXTRACT_CMD="dd skip=32 iflag=skip_bytes"
111 return
112 }
113
114 magic=$(get_magic_long_at "$1" 60)
115 [ "$magic" = "4c584c23" ] && {
116 notify_firmware_no_backup
117
118 BCM53XX_FW_FORMAT="lxlold"
119 BCM53XX_FW_BOARD_ID=$(dd if="$1" skip=48 bs=1 count=12 2>/dev/null | hexdump -v -e '1/1 "%c"')
120 BCM53XX_FW_INT_IMG_FORMAT="trx"
121 BCM53XX_FW_INT_IMG_TRX_OFFSET="64"
122 BCM53XX_FW_INT_IMG_EXTRACT_CMD="dd skip=64 iflag=skip_bytes"
123 return
124 }
125
126 if osafeloader info "$1" > /dev/null 2>&1; then
127 BCM53XX_FW_FORMAT="safeloader"
128 return
129 fi
130 }
131
132 platform_other_check_image() {
133 [ "$#" -gt 1 ] && return 1
134
135 local error=0
136
137 platform_identify "$1"
138 [ -z "$BCM53XX_FW_FORMAT" ] && {
139 echo "Invalid image type. Please use firmware specific for this device."
140 notify_firmware_broken
141 return 1
142 }
143 echo "Found $BCM53XX_FW_FORMAT firmware for device $BCM53XX_FW_BOARD_ID"
144
145 local expected_image="$(platform_expected_image)"
146 local tmp_format=$BCM53XX_FW_FORMAT
147 [ "$tmp_format" = "lxlold" ] && tmp_format="lxl"
148 [ -n "$expected_image" -a -n "$BCM53XX_FW_BOARD_ID" -a "$expected_image" != "$tmp_format $BCM53XX_FW_BOARD_ID" ] && {
149 echo "Firmware doesn't match device ($expected_image)"
150 error=1
151 }
152
153 case "$BCM53XX_FW_FORMAT" in
154 "seamaseal")
155 $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
156 echo "Seama seal doesn't contain firmware entity"
157 error=1
158 }
159 ;;
160 "trx")
161 if ! otrx check "$1"; then
162 echo "Failed to find a valid TRX in firmware"
163 notify_firmware_test_result "trx_valid" 0
164 error=1
165 else
166 notify_firmware_test_result "trx_valid" 1
167 fi
168
169 [ "$expected_image" == "safeloader" ] && {
170 echo "This device expects SafeLoader format and may not work with TRX"
171 error=1
172 }
173 ;;
174 *)
175 case "$BCM53XX_FW_INT_IMG_FORMAT" in
176 "trx")
177 # Make sure that both ways of extracting TRX work.
178 # platform_do_upgrade() may use any of them.
179 if ! otrx check "$1" -o "$BCM53XX_FW_INT_IMG_TRX_OFFSET" || \
180 ! $BCM53XX_FW_INT_IMG_EXTRACT_CMD < $1 | otrx check -; then
181 echo "Invalid (corrupted?) TRX firmware"
182 notify_firmware_test_result "trx_valid" 0
183 error=1
184 else
185 notify_firmware_test_result "trx_valid" 1
186 fi
187 ;;
188 esac
189 ;;
190 esac
191
192 return $error
193 }
194
195 platform_check_image() {
196 local board
197
198 board="$(board_name)"
199 case "$board" in
200 # Ideally, REQUIRE_IMAGE_METADATA=1 would suffice
201 # but this would require converting all other
202 # devices too.
203 meraki,mr26 | \
204 meraki,mr32)
205 nand_do_platform_check "${board//,/_}" "$1"
206 return $?
207 ;;
208 *)
209 platform_other_check_image "$1"
210 return $?
211 ;;
212 esac
213
214 return 1
215 }
216
217
218 # $(1): TRX image or firmware containing TRX
219 # $(2): offset of TRX in firmware (optional)
220 platform_do_upgrade_nand_trx() {
221 local dir="/tmp/sysupgrade-bcm53xx"
222 local trx="$1"
223 local offset="$2"
224
225 # Extract partitions from trx
226 rm -fR $dir
227 mkdir -p $dir
228 otrx extract "$trx" \
229 ${offset:+-o $offset} \
230 -1 $dir/kernel \
231 -2 $dir/root
232 [ $? -ne 0 ] && {
233 echo "Failed to extract TRX partitions."
234 return
235 }
236
237 # Firmwares without UBI image should be flashed "normally"
238 local root_type=$(identify $dir/root)
239 [ "$root_type" != "ubi" ] && {
240 echo "Provided firmware doesn't use UBI for rootfs."
241 return
242 }
243
244 # Prepare TRX file with just a kernel that will replace current one
245 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
246 [ -z "$linux_length" ] && {
247 echo "Unable to find \"linux\" partition size"
248 exit 1
249 }
250 linux_length=$((0x$linux_length))
251 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
252 [ $kernel_length -gt $linux_length ] && {
253 echo "New kernel doesn't fit \"linux\" partition."
254 return
255 }
256 rm -f /tmp/null.bin
257 rm -f /tmp/kernel.trx
258 touch /tmp/null.bin
259 otrx create /tmp/kernel.trx \
260 -f $dir/kernel -b $(($linux_length + 28)) \
261 -f /tmp/null.bin
262 [ $? -ne 0 ] && {
263 echo "Failed to create simple TRX with new kernel."
264 return
265 }
266
267 # Prepare UBI image (drop unwanted extra blocks)
268 local ubi_length=0
269 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
270 ubi_length=$(($ubi_length + 131072))
271 done
272 truncate -s $ubi_length $dir/root
273 [ $? -ne 0 ] && {
274 echo "Failed to prepare new UBI image."
275 return
276 }
277
278 # Flash
279 mtd write /tmp/kernel.trx firmware || exit 1
280 nand_do_upgrade $dir/root
281 }
282
283 platform_do_upgrade_nand_seamaseal() {
284 local dir="/tmp/sysupgrade-bcm53xx"
285 local seamaseal="$1"
286 local tmp
287
288 # Extract Seama entity from Seama seal
289 rm -fR $dir
290 mkdir -p $dir
291 oseama extract "$seamaseal" \
292 -e 0 \
293 -o $dir/seama.entity
294 [ $? -ne 0 ] && {
295 echo "Failed to extract Seama entity."
296 return
297 }
298 local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
299
300 local ubi_offset=0
301 tmp=0
302 while [ 1 ]; do
303 [ $tmp -ge $entity_size ] && break
304 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
305 ubi_offset=$tmp
306 break
307 }
308 tmp=$(($tmp + 131072))
309 done
310 [ $ubi_offset -eq 0 ] && {
311 echo "Failed to find UBI in Seama entity."
312 return
313 }
314
315 local ubi_length=0
316 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
317 ubi_length=$(($ubi_length + 131072))
318 done
319
320 dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
321 dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
322
323 # Flash
324 local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
325 mtd write $dir/kernel.seama firmware || exit 1
326 mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
327 nand_do_upgrade $dir/root.ubi
328 }
329
330 platform_img_from_safeloader() {
331 local dir="/tmp/sysupgrade-bcm53xx"
332
333 # Extract partitions from SafeLoader
334 rm -fR $dir
335 mkdir -p $dir
336 osafeloader extract "$1" \
337 -p "os-image" \
338 -o $dir/os-image
339 osafeloader extract "$1" \
340 -p "file-system" \
341 -o $dir/file-system
342
343 mtd write $dir/file-system rootfs
344
345 echo -n $dir/os-image
346 }
347
348 platform_other_do_upgrade() {
349 platform_identify "$1"
350
351 [ "$(platform_flash_type)" == "nand" ] && {
352 # Try NAND-aware upgrade
353 case "$BCM53XX_FW_FORMAT" in
354 "seamaseal")
355 platform_do_upgrade_nand_seamaseal "$1"
356 ;;
357 "trx")
358 platform_do_upgrade_nand_trx "$1"
359 ;;
360 *)
361 case "$BCM53XX_FW_INT_IMG_FORMAT" in
362 "trx")
363 platform_do_upgrade_nand_trx "$1" "$BCM53XX_FW_INT_IMG_TRX_OFFSET"
364 ;;
365 *)
366 echo "NAND aware sysupgrade is unsupported for $BCM53XX_FW_FORMAT format"
367 ;;
368 esac
369 ;;
370 esac
371
372 # Above calls exit on success.
373 # If we got here something went wrong.
374 echo "Writing whole image to NAND flash. All erase counters will be lost."
375 }
376
377 case "$BCM53XX_FW_FORMAT" in
378 "safeloader")
379 PART_NAME=os-image
380 img=$(platform_img_from_safeloader "$1")
381 default_do_upgrade "$img"
382 ;;
383 "seamaseal")
384 default_do_upgrade "$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
385 ;;
386 "trx")
387 default_do_upgrade "$1"
388 ;;
389 *)
390 case "$BCM53XX_FW_INT_IMG_FORMAT" in
391 "trx")
392 default_do_upgrade "$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
393 ;;
394 esac
395 ;;
396 esac
397 }
398
399 platform_do_upgrade() {
400 case "$(board_name)" in
401 meraki,mr26 | \
402 meraki,mr32)
403 CI_KERNPART="part.safe"
404 nand_do_upgrade "$1"
405 ;;
406 *)
407 platform_other_do_upgrade "$1"
408 ;;
409 esac
410 }