ipq806x: convert to dt-based board-detection
[openwrt/openwrt.git] / target / linux / ipq806x / base-files / lib / upgrade / zyxel.sh
1 #
2 # Copyright (C) 2016 lede-project.org
3 #
4
5 zyxel_get_rootfs() {
6 local rootfsdev
7
8 if read cmdline < /proc/cmdline; then
9 case "$cmdline" in
10 *root=*)
11 rootfsdev="${cmdline##*root=}"
12 rootfsdev="${rootfsdev%% *}"
13 ;;
14 esac
15
16 echo "${rootfsdev}"
17 fi
18 }
19
20 zyxel_do_flash() {
21 local tar_file=$1
22 local board=$2
23 local kernel=$3
24 local rootfs=$4
25
26 # keep sure its unbound
27 losetup --detach-all || {
28 echo Failed to detach all loop devices. Skip this try.
29 reboot -f
30 }
31
32 echo "flashing kernel to /dev/${kernel}"
33 tar xf $tar_file sysupgrade-$board/kernel -O >/dev/$kernel
34
35 echo "flashing rootfs to ${rootfs}"
36 tar xf $tar_file sysupgrade-$board/root -O >"${rootfs}"
37
38 # a padded rootfs is needed for overlay fs creation
39 local offset=$(tar xf $tar_file sysupgrade-$board/root -O | wc -c)
40 [ $offset -lt 65536 ] && {
41 echo Wrong size for rootfs: $offset
42 sleep 10
43 reboot -f
44 }
45
46 # Mount loop for rootfs_data
47 losetup -o $offset /dev/loop0 "${rootfs}" || {
48 echo "Failed to mount looped rootfs_data."
49 sleep 10
50 reboot -f
51 }
52
53 echo "Format new rootfs_data at position ${offset}."
54 mkfs.ext4 -F -L rootfs_data /dev/loop0
55 mkdir /tmp/new_root
56 mount -t ext4 /dev/loop0 /tmp/new_root && {
57 echo "Saving config to rootfs_data at position ${offset}."
58 cp -v /tmp/sysupgrade.tgz /tmp/new_root/
59 umount /tmp/new_root
60 }
61
62 # Cleanup
63 losetup -d /dev/loop0 >/dev/null 2>&1
64 sync
65 umount -a
66 reboot -f
67 }
68
69 zyxel_do_upgrade() {
70 local tar_file="$1"
71 local board=$(board_name)
72 local rootfs="$(zyxel_get_rootfs)"
73 local kernel=
74
75 [ -b "${rootfs}" ] || return 1
76 case "$board" in
77 zyxel,nbg6817)
78 case "$rootfs" in
79 "/dev/mmcblk0p5")
80 kernel=mmcblk0p4
81 ;;
82 "/dev/mmcblk0p8")
83 kernel=mmcblk0p7
84 ;;
85 *)
86 return 1
87 ;;
88 esac
89 ;;
90 *)
91 return 1
92 ;;
93 esac
94
95 zyxel_do_flash $tar_file $board $kernel $rootfs
96
97 return 0
98 }