62a36dac49d23a9cd0dad56f16c89e69a08f6755
[openwrt/staging/nbd.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 case "$(board_name)" in
197 meraki,mr32)
198 # Ideally, REQUIRE_IMAGE_METADATA=1 would suffice
199 # but this would require converting all other
200 # devices too.
201 nand_do_platform_check meraki-mr32 "$1"
202 return $?
203 ;;
204 *)
205 platform_other_check_image "$1"
206 return $?
207 ;;
208 esac
209
210 return 1
211 }
212
213
214 # $(1): TRX image or firmware containing TRX
215 # $(2): offset of TRX in firmware (optional)
216 platform_do_upgrade_nand_trx() {
217 local dir="/tmp/sysupgrade-bcm53xx"
218 local trx="$1"
219 local offset="$2"
220
221 # Extract partitions from trx
222 rm -fR $dir
223 mkdir -p $dir
224 otrx extract "$trx" \
225 ${offset:+-o $offset} \
226 -1 $dir/kernel \
227 -2 $dir/root
228 [ $? -ne 0 ] && {
229 echo "Failed to extract TRX partitions."
230 return
231 }
232
233 # Firmwares without UBI image should be flashed "normally"
234 local root_type=$(identify $dir/root)
235 [ "$root_type" != "ubi" ] && {
236 echo "Provided firmware doesn't use UBI for rootfs."
237 return
238 }
239
240 # Prepare TRX file with just a kernel that will replace current one
241 local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
242 [ -z "$linux_length" ] && {
243 echo "Unable to find \"linux\" partition size"
244 exit 1
245 }
246 linux_length=$((0x$linux_length))
247 local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
248 [ $kernel_length -gt $linux_length ] && {
249 echo "New kernel doesn't fit \"linux\" partition."
250 return
251 }
252 rm -f /tmp/null.bin
253 rm -f /tmp/kernel.trx
254 touch /tmp/null.bin
255 otrx create /tmp/kernel.trx \
256 -f $dir/kernel -b $(($linux_length + 28)) \
257 -f /tmp/null.bin
258 [ $? -ne 0 ] && {
259 echo "Failed to create simple TRX with new kernel."
260 return
261 }
262
263 # Prepare UBI image (drop unwanted extra blocks)
264 local ubi_length=0
265 while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
266 ubi_length=$(($ubi_length + 131072))
267 done
268 truncate -s $ubi_length $dir/root
269 [ $? -ne 0 ] && {
270 echo "Failed to prepare new UBI image."
271 return
272 }
273
274 # Flash
275 mtd write /tmp/kernel.trx firmware || exit 1
276 nand_do_upgrade $dir/root
277 }
278
279 platform_do_upgrade_nand_seamaseal() {
280 local dir="/tmp/sysupgrade-bcm53xx"
281 local seamaseal="$1"
282 local tmp
283
284 # Extract Seama entity from Seama seal
285 rm -fR $dir
286 mkdir -p $dir
287 oseama extract "$seamaseal" \
288 -e 0 \
289 -o $dir/seama.entity
290 [ $? -ne 0 ] && {
291 echo "Failed to extract Seama entity."
292 return
293 }
294 local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
295
296 local ubi_offset=0
297 tmp=0
298 while [ 1 ]; do
299 [ $tmp -ge $entity_size ] && break
300 [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
301 ubi_offset=$tmp
302 break
303 }
304 tmp=$(($tmp + 131072))
305 done
306 [ $ubi_offset -eq 0 ] && {
307 echo "Failed to find UBI in Seama entity."
308 return
309 }
310
311 local ubi_length=0
312 while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
313 ubi_length=$(($ubi_length + 131072))
314 done
315
316 dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
317 dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
318
319 # Flash
320 local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
321 mtd write $dir/kernel.seama firmware || exit 1
322 mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
323 nand_do_upgrade $dir/root.ubi
324 }
325
326 platform_img_from_safeloader() {
327 local dir="/tmp/sysupgrade-bcm53xx"
328
329 # Extract partitions from SafeLoader
330 rm -fR $dir
331 mkdir -p $dir
332 osafeloader extract "$1" \
333 -p "os-image" \
334 -o $dir/os-image
335 osafeloader extract "$1" \
336 -p "file-system" \
337 -o $dir/file-system
338
339 mtd write $dir/file-system rootfs
340
341 echo -n $dir/os-image
342 }
343
344 platform_other_do_upgrade() {
345 platform_identify "$1"
346
347 [ "$(platform_flash_type)" == "nand" ] && {
348 # Try NAND-aware upgrade
349 case "$BCM53XX_FW_FORMAT" in
350 "seamaseal")
351 platform_do_upgrade_nand_seamaseal "$1"
352 ;;
353 "trx")
354 platform_do_upgrade_nand_trx "$1"
355 ;;
356 *)
357 case "$BCM53XX_FW_INT_IMG_FORMAT" in
358 "trx")
359 platform_do_upgrade_nand_trx "$1" "$BCM53XX_FW_INT_IMG_TRX_OFFSET"
360 ;;
361 *)
362 echo "NAND aware sysupgrade is unsupported for $BCM53XX_FW_FORMAT format"
363 ;;
364 esac
365 ;;
366 esac
367
368 # Above calls exit on success.
369 # If we got here something went wrong.
370 echo "Writing whole image to NAND flash. All erase counters will be lost."
371 }
372
373 case "$BCM53XX_FW_FORMAT" in
374 "safeloader")
375 PART_NAME=os-image
376 img=$(platform_img_from_safeloader "$1")
377 default_do_upgrade "$img"
378 ;;
379 "seamaseal")
380 default_do_upgrade "$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
381 ;;
382 "trx")
383 default_do_upgrade "$1"
384 ;;
385 *)
386 case "$BCM53XX_FW_INT_IMG_FORMAT" in
387 "trx")
388 default_do_upgrade "$1" "$BCM53XX_FW_INT_IMG_EXTRACT_CMD"
389 ;;
390 esac
391 ;;
392 esac
393 }
394
395 platform_do_upgrade() {
396 case "$(board_name)" in
397 meraki,mr32)
398 CI_KERNPART="part.safe"
399 nand_do_upgrade "$1"
400 ;;
401 *)
402 platform_other_do_upgrade "$1"
403 ;;
404 esac
405 }