ipq40xx: add support for Netgear SRR60/SRS60 and RBR50/RBS50
[openwrt/staging/ldir.git] / target / linux / ipq40xx / base-files / lib / upgrade / netgear.sh
1 # SPDX-License-Identifier: GPL-2.0-only
2 . /lib/functions.sh
3
4 platform_do_upgrade_netgear_orbi_upgrade() {
5 command -v losetup >/dev/null || {
6 logger -s "Upgrade failed: 'losetup' not installed."
7 return 1
8 }
9
10 local tar_file=$1
11 local kernel=$2
12 local rootfs=$3
13
14 [ -z "$kernel" ] && kernel=$(find_mmc_part "kernel")
15 [ -z "$rootfs" ] && rootfs=$(find_mmc_part "rootfs")
16
17 [ -z "$kernel" ] && echo "Upgrade failed: kernel partition not found! Rebooting..." && reboot -f
18 [ -z "$rootfs" ] && echo "Upgrade failed: rootfs partition not found! Rebooting..." && reboot -f
19
20 netgear_orbi_do_flash $tar_file $kernel $rootfs
21
22 echo "sysupgrade successful"
23 umount -a
24 reboot -f
25 }
26
27 netgear_orbi_do_flash() {
28 local tar_file=$1
29 local kernel=$2
30 local rootfs=$3
31
32 # keep sure its unbound
33 losetup --detach-all || {
34 echo "Failed to detach all loop devices. Skip this try."
35 reboot -f
36 }
37
38 # use the first found directory in the tar archive
39 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
40 board_dir=${board_dir%/}
41
42 echo "flashing kernel to $kernel"
43 tar xf $tar_file ${board_dir}/kernel -O >$kernel
44
45 echo "flashing rootfs to ${rootfs}"
46 tar xf $tar_file ${board_dir}/root -O >"${rootfs}"
47
48 # a padded rootfs is needed for overlay fs creation
49 local offset=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
50 [ $offset -lt 65536 ] && {
51 echo "Wrong size for rootfs: $offset"
52 sleep 10
53 reboot -f
54 }
55
56 # Mount loop for rootfs_data
57 local loopdev="$(losetup -f)"
58 losetup -o $offset $loopdev $rootfs || {
59 echo "Failed to mount looped rootfs_data."
60 sleep 10
61 reboot -f
62 }
63
64 echo "Format new rootfs_data at position ${offset}."
65 mkfs.ext4 -F -L rootfs_data $loopdev
66 mkdir /tmp/new_root
67 mount -t ext4 $loopdev /tmp/new_root && {
68 echo "Saving config to rootfs_data at position ${offset}."
69 cp -v "$UPGRADE_BACKUP" "/tmp/new_root/$BACKUP_FILE"
70 umount /tmp/new_root
71 }
72
73 # Cleanup
74 losetup -d $loopdev >/dev/null 2>&1
75 sync
76 }