mvebu: use SOC to derive DEVICE_DTS
[openwrt/openwrt.git] / target / linux / mvebu / base-files / lib / upgrade / uDPU.sh
1 udpu_check_emmc() {
2 # uDPU uses combined ext4 and f2fs partitions.
3 # partition layout:
4 # 1. boot (ext4)
5 # 2. recovery (ext4)
6 # 3. rootfs (f2fs)
7 # 4. misc (f2fs)
8
9 # Check which device is available, depending on the board revision
10 if [ -b "/dev/mmcblk1" ]; then
11 emmc_dev=/dev/mmcblk1
12 elif [ -b "/dev/mmcblk0" ]; then
13 emmc_dev=/dev/mmcblk0
14 else
15 echo "Cannot detect eMMC flash, aborting.."
16 exit 1
17 fi
18 }
19
20 udpu_part_prep() {
21 if [ "$(grep $1 /proc/mounts)" ]; then
22 mounted_part="$(grep $1 /proc/mounts | awk '{print $2}' | head -1)"
23 umount $mounted_part
24 [ "$(grep -wo $mounted_part /proc/mounts)" ] && umount -l $mounted_part
25 fi
26 }
27
28 udpu_do_part_check() {
29 local emmc_parts="1 2 3 4"
30 local part_valid="1"
31
32 # Check if the block devices exist
33 for num in ${emmc_parts}; do
34 [[ ! -b ${emmc_dev}p${num} ]] && part_valid="0"
35 done
36
37 # If partitions are missing create a new partition table
38 if [ "$part_valid" != "1" ]; then
39 printf "Invalid partition table, creating a new one\n"
40 printf "o\nn\np\n1\n\n+256M\nn\np\n2\n\n+256M\nn\np\n3\n\n+1536M\nn\np\n\n\nw\n" | fdisk -W always $emmc_dev > /dev/null 2>&1
41
42 # Format the /misc part right away as we will need it for the firmware
43 printf "Formating /misc partition, this make take a while..\n"
44 udpu_part_prep ${emmc_dev}p4
45 mkfs.f2fs -q -l misc ${emmc_dev}p4
46 [ $? -eq 0 ] && printf "/misc partition formated successfully\n" || printf "/misc partition formatting failed\n"
47
48 udpu_do_initial_setup
49 else
50 printf "Partition table looks ok\n"
51 fi
52 }
53
54 udpu_do_misc_prep() {
55 if [ ! "$(grep -wo /misc /proc/mounts)" ]; then
56 mkdir -p /misc
57 mount ${emmc_dev}p4 /misc
58
59 # If the mount fails, try to reformat partition
60 # Leaving possiblity for multiple iterations
61 if [ $? -ne 0 ]; then
62 printf "Error while mounting /misc, trying to reformat..\n"
63
64 format_count=0
65 while [ "$format_count" -lt "1" ]; do
66 udpu_part_prep ${emmc_dev}p4
67 mkfs.f2fs -q -l misc ${emmc_dev}p4
68 mount ${emmc_dev}p4 /misc
69 if [ $? -ne 0 ]; then
70 umount -l /misc
71 printf "Failed while mounting /misc\n"
72 format_count=$((format_count +1))
73 else
74 printf "Mounted /misc successfully\n"
75 break
76 fi
77 done
78 fi
79 fi
80 }
81
82 udpu_do_initial_setup() {
83 # Prepare /recovery parition
84 udpu_part_prep ${emmc_dev}p2
85 mkfs.ext4 -q ${emmc_dev}p2 | echo y &> /dev/null
86
87 # Prepare /boot partition
88 udpu_part_prep ${emmc_dev}p1
89 mkfs.ext4 -q ${emmc_dev}p1 | echo y &> /dev/null
90
91 # Prepare /root partition
92 printf "Formating /root partition, this may take a while..\n"
93 udpu_part_prep ${emmc_dev}p3
94 mkfs.f2fs -q -l rootfs ${emmc_dev}p3
95 [ $? -eq 0 ] && printf "/root partition reformated\n"
96 }
97
98 udpu_do_regular_upgrade() {
99 # Clean /boot partition - mfks.ext4 is not available in chroot
100 [ "$(grep -wo /boot /proc/mounts)" ] && umount /boot
101 mkdir -p /tmp/boot
102 mount ${emmc_dev}p1 /tmp/boot
103 rm -rf /tmp/boot/*
104
105 # Clean /root partition - mkfs.f2fs is not available in chroot
106 [ "$(grep -wo /dev/root /proc/mounts)" ] && umount /
107 mkdir -p /tmp/rootpart
108 mount ${emmc_dev}p3 /tmp/rootpart
109 rm -rf /tmp/rootpart/*
110 }
111
112 platform_do_upgrade_uDPU() {
113 udpu_check_emmc
114
115 # Prepare and extract firmware on /misc partition
116 udpu_do_misc_prep
117
118 [ -f "/misc/firmware" ] && rm -r /misc/firmware
119 mkdir -p /misc/firmware
120 tar xzf "$1" -C /misc/firmware/
121
122 udpu_do_regular_upgrade
123
124 printf "Updating /boot partition\n"
125 tar xzf /misc/firmware/boot.tgz -C /tmp/boot
126 [ $? -eq 0 ] && printf "/boot partition updated successfully\n" || printf "/boot partition update failed\n"
127 sync
128
129 printf "Updating /root partition\n"
130 tar xzf /misc/firmware/rootfs.tgz -C /tmp/rootpart
131 [ $? -eq 0 ] && printf "/root partition updated successfully\n" || printf "/root partition update failed\n"
132 sync
133
134 # Saving configuration files over sysupgrade
135 platform_copy_config_uDPU
136
137 # Remove tmp mounts
138 tmp_parts=$(grep "${emmc_dev}" /proc/mounts | awk '{print $2}')
139 for part in ${tmp_parts}; do
140 umount $part
141 # Force umount is necessary
142 [ "$(grep "${part}" /proc/mounts)" ] && umount -l $part
143 done
144
145 # Sysupgrade complains about /tmp and /dev, so we can detach them here
146 umount -l /tmp
147 umount -l /dev
148 }
149
150 platform_copy_config_uDPU() {
151 # Config is saved on the /misc partition and copied on the rootfs after the reboot
152 if [ -f "$UPGRADE_BACKUP" ]; then
153 cp -f "$UPGRADE_BACKUP" "/misc/$BACKUP_FILE"
154 sync
155 fi
156 }