aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ipq806x/base-files/lib/upgrade/zyxel.sh
blob: 466d8c616c7e35616dad476d0981c7577a9bc5cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#
# Copyright (C) 2016 lede-project.org
#

zyxel_get_rootfs() {
	local rootfsdev

	if read cmdline < /proc/cmdline; then
		case "$cmdline" in
			*root=*)
				rootfsdev="${cmdline##*root=}"
				rootfsdev="${rootfsdev%% *}"
			;;
		esac

		echo "${rootfsdev}"
	fi
}

zyxel_do_flash() {
	local tar_file=$1
	local board=$2
	local kernel=$3
	local rootfs=$4

	# keep sure its unbound
	losetup --detach-all || {
		echo Failed to detach all loop devices. Skip this try.
		reboot -f
	}

	echo "flashing kernel to /dev/${kernel}"
	tar xf $tar_file sysupgrade-$board/kernel -O >/dev/$kernel

	echo "flashing rootfs to ${rootfs}"
	tar xf $tar_file sysupgrade-$board/root -O >"${rootfs}"

	# a padded rootfs is needed for overlay fs creation
	local offset=$(tar xf $tar_file sysupgrade-$board/root -O | wc -c)
	[ $offset -lt 65536 ] && {
		echo Wrong size for rootfs: $offset
		sleep 10
		reboot -f
	}

	# Mount loop for rootfs_data
	losetup -o $offset /dev/loop0 "${rootfs}" || {
		echo "Failed to mount looped rootfs_data."
		sleep 10
		reboot -f
	}

	echo "Format new rootfs_data at position ${offset}."
	mkfs.ext4 -F -L rootfs_data /dev/loop0
	mkdir /tmp/new_root
	mount -t ext4 /dev/loop0 /tmp/new_root && {
		echo "Saving config to rootfs_data at position ${offset}."
		cp -v /tmp/sysupgrade.tgz /tmp/new_root/
		umount /tmp/new_root
	}

	# Cleanup
	losetup -d /dev/loop0 >/dev/null 2>&1
	sync
	umount -a
	reboot -f
}

zyxel_do_upgrade() {
	local tar_file="$1"
	local board=$(board_name)
	local rootfs="$(zyxel_get_rootfs)"
	local kernel=

	[ -b "${rootfs}" ] || return 1
	case "$board" in
	zyxel,nbg6817)
		case "$rootfs" in
			"/dev/mmcblk0p5")
				kernel=mmcblk0p4
			;;
			"/dev/mmcblk0p8")
				kernel=mmcblk0p7
			;;
			*)
				return 1
			;;
		esac
		;;
	*)
		return 1
		;;
	esac

	zyxel_do_flash $tar_file $board $kernel $rootfs

	return 0
}