treewide: don't hardcode "sysupgrade.tgz" file name
[openwrt/staging/luka.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 local dualflagmtd=$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 # use the first found directory in the tar archive
33 local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
34 board_dir=${board_dir%/}
35
36 echo "flashing kernel to $kernel"
37 tar xf $tar_file ${board_dir}/kernel -O >$kernel
38
39 echo "flashing rootfs to ${rootfs}"
40 tar xf $tar_file ${board_dir}/root -O >"${rootfs}"
41
42 # a padded rootfs is needed for overlay fs creation
43 local offset=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
44 [ $offset -lt 65536 ] && {
45 echo Wrong size for rootfs: $offset
46 sleep 10
47 reboot -f
48 }
49
50 # Mount loop for rootfs_data
51 local loopdev="$(losetup -f)"
52 losetup -o $offset $loopdev $rootfs || {
53 echo "Failed to mount looped rootfs_data."
54 sleep 10
55 reboot -f
56 }
57
58 echo "Format new rootfs_data at position ${offset}."
59 mkfs.ext4 -F -L rootfs_data $loopdev
60 mkdir /tmp/new_root
61 mount -t ext4 $loopdev /tmp/new_root && {
62 echo "Saving config to rootfs_data at position ${offset}."
63 cp -v "$CONF_TAR" /tmp/new_root/
64 umount /tmp/new_root
65 }
66
67 # flashing successful, toggle the dualflag
68 case "$rootfs" in
69 "/dev/mmcblk0p5")
70 printf "\xff" >$dualflagmtd
71 ;;
72 "/dev/mmcblk0p8")
73 printf "\x01" >$dualflagmtd
74 ;;
75 esac
76
77 # Cleanup
78 losetup -d $loopdev >/dev/null 2>&1
79 sync
80 umount -a
81 reboot -f
82 }
83
84 zyxel_do_upgrade() {
85 local tar_file="$1"
86 local board=$(board_name)
87 local rootfs="$(zyxel_get_rootfs)"
88 local kernel=
89
90 [ -b "${rootfs}" ] || return 1
91 case "$board" in
92 zyxel,nbg6817)
93 local dualflagmtd="$(find_mtd_part 0:DUAL_FLAG)"
94 [ -b $dualflagmtd ] || return 1
95
96 case "$rootfs" in
97 "/dev/mmcblk0p5")
98 # booted from the primary partition set
99 # write to the alternative set
100 kernel="/dev/mmcblk0p7"
101 rootfs="/dev/mmcblk0p8"
102 ;;
103 "/dev/mmcblk0p8")
104 # booted from the alternative partition set
105 # write to the primary set
106 kernel="/dev/mmcblk0p4"
107 rootfs="/dev/mmcblk0p5"
108 ;;
109 *)
110 return 1
111 ;;
112 esac
113 ;;
114 *)
115 return 1
116 ;;
117 esac
118
119 zyxel_do_flash $tar_file $kernel $rootfs $dualflagmtd
120
121 nand_do_upgrade "$1"
122
123 return 0
124 }