84533d642a1c472dfdcc66cf0f014c3dc1d1e714
[openwrt/staging/ansuel.git] / target / linux / octeon / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright (C) 2021 OpenWrt.org
3 #
4
5 platform_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 platform_copy_config_helper() {
21 local device=$1
22
23 mount -t vfat "$device" /mnt
24 cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
25 umount /mnt
26 }
27
28 platform_copy_config() {
29 case "$(board_name)" in
30 erlite)
31 platform_copy_config_helper /dev/sda1
32 ;;
33 itus,shield-router)
34 platform_copy_config_helper /dev/mmcblk1p1
35 ;;
36 ubnt,edgerouter-4|\
37 ubnt,edgerouter-6p)
38 platform_copy_config_helper /dev/mmcblk0p1
39 ;;
40 esac
41 }
42
43 platform_do_flash() {
44 local tar_file=$1
45 local board=$2
46 local kernel=$3
47 local rootfs=$4
48
49 local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
50 board_dir=${board_dir%/}
51 [ -n "$board_dir" ] || return 1
52
53 mkdir -p /boot
54
55 if [ $board = "itus,shield-router" ]; then
56 # mmcblk1p1 (fat) contains all ELF-bin images for the Shield
57 mount /dev/mmcblk1p1 /boot
58
59 echo "flashing Itus Kernel to /boot/$kernel (/dev/mmblk1p1)"
60 tar -Oxf $tar_file "$board_dir/kernel" > /boot/$kernel
61 else
62 mount -t vfat /dev/$kernel /boot
63
64 [ -f /boot/vmlinux.64 -a ! -L /boot/vmlinux.64 ] && {
65 mv /boot/vmlinux.64 /boot/vmlinux.64.previous
66 mv /boot/vmlinux.64.md5 /boot/vmlinux.64.md5.previous
67 }
68
69 echo "flashing kernel to /dev/$kernel"
70 tar xf $tar_file $board_dir/kernel -O > /boot/vmlinux.64
71 md5sum /boot/vmlinux.64 | cut -f1 -d " " > /boot/vmlinux.64.md5
72 fi
73
74 echo "flashing rootfs to ${rootfs}"
75 tar xf $tar_file $board_dir/root -O | dd of="${rootfs}" bs=4096
76
77 sync
78 umount /boot
79 }
80
81 platform_do_upgrade() {
82 local tar_file="$1"
83 local board=$(board_name)
84 local rootfs="$(platform_get_rootfs)"
85 local kernel=
86
87 [ -b "${rootfs}" ] || return 1
88 case "$board" in
89 er | \
90 ubnt,edgerouter-4 | \
91 ubnt,edgerouter-6p)
92 kernel=mmcblk0p1
93 ;;
94 erlite)
95 kernel=sda1
96 ;;
97 itus,shield-router)
98 kernel=ItusrouterImage
99 ;;
100 *)
101 return 1
102 esac
103
104 platform_do_flash $tar_file $board $kernel $rootfs
105
106 return 0
107 }
108
109 platform_check_image() {
110 local board=$(board_name)
111 local tar_file="$1"
112
113 local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
114 board_dir=${board_dir%/}
115 [ -n "$board_dir" ] || return 1
116
117 case "$board" in
118 er | \
119 erlite | \
120 itus,shield-router | \
121 ubnt,edgerouter-4 | \
122 ubnt,edgerouter-6p)
123 local kernel_length=$(tar xf $tar_file $board_dir/kernel -O | wc -c 2> /dev/null)
124 local rootfs_length=$(tar xf $tar_file $board_dir/root -O | wc -c 2> /dev/null)
125 [ "$kernel_length" = 0 -o "$rootfs_length" = 0 ] && {
126 echo "The upgrade image is corrupt."
127 return 1
128 }
129 return 0
130 ;;
131 esac
132
133 echo "Sysupgrade is not yet supported on $board."
134 return 1
135 }