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