ipq806x: fix ZyXEL NBG6817 sysupgrade
[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 kernel=$2
23 local rootfs=$3
24
25 # keep sure its unbound
26 losetup --detach-all || {
27 echo Failed to detach all loop devices. Skip this try.
28 reboot -f
29 }
30
31 # use the first found directory in the tar archive
32 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
33 board_dir=${board_dir%/}
34
35 echo "flashing kernel to /dev/${kernel}"
36 tar xf $tar_file ${board_dir}/kernel -O >/dev/$kernel
37
38 echo "flashing rootfs to ${rootfs}"
39 tar xf $tar_file ${board_dir}/root -O >"${rootfs}"
40
41 # a padded rootfs is needed for overlay fs creation
42 local offset=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
43 [ $offset -lt 65536 ] && {
44 echo Wrong size for rootfs: $offset
45 sleep 10
46 reboot -f
47 }
48
49 # Mount loop for rootfs_data
50 losetup -o $offset /dev/loop0 "${rootfs}" || {
51 echo "Failed to mount looped rootfs_data."
52 sleep 10
53 reboot -f
54 }
55
56 echo "Format new rootfs_data at position ${offset}."
57 mkfs.ext4 -F -L rootfs_data /dev/loop0
58 mkdir /tmp/new_root
59 mount -t ext4 /dev/loop0 /tmp/new_root && {
60 echo "Saving config to rootfs_data at position ${offset}."
61 cp -v /tmp/sysupgrade.tgz /tmp/new_root/
62 umount /tmp/new_root
63 }
64
65 # Cleanup
66 losetup -d /dev/loop0 >/dev/null 2>&1
67 sync
68 umount -a
69 reboot -f
70 }
71
72 zyxel_do_upgrade() {
73 local tar_file="$1"
74 local board=$(board_name)
75 local rootfs="$(zyxel_get_rootfs)"
76 local kernel=
77
78 [ -b "${rootfs}" ] || return 1
79 case "$board" in
80 zyxel,nbg6817)
81 case "$rootfs" in
82 "/dev/mmcblk0p5")
83 kernel=mmcblk0p4
84 ;;
85 "/dev/mmcblk0p8")
86 kernel=mmcblk0p7
87 ;;
88 *)
89 return 1
90 ;;
91 esac
92 ;;
93 *)
94 return 1
95 ;;
96 esac
97
98 zyxel_do_flash $tar_file $kernel $rootfs
99
100 return 0
101 }