92102e8d14f36a7a3cfa77f6ef44b08df241ccf6
[openwrt/openwrt.git] / target / linux / mvebu / cortexa53 / 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 -q "$1" /proc/mounts; then
22 mounted_part="$(grep -m 1 $1 /proc/mounts | awk '{print $2}')"
23 umount $mounted_part
24 grep -woq "$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 if mkfs.f2fs -q -l misc ${emmc_dev}p4; then
46 printf "/misc partition formated successfully\n"
47 else
48 printf "/misc partition formatting failed\n"
49 fi
50
51 udpu_do_initial_setup
52 else
53 printf "Partition table looks ok\n"
54 fi
55 }
56
57 udpu_do_misc_prep() {
58 if ! grep -woq /misc /proc/mounts; then
59 mkdir -p /misc
60
61 # If the mount fails, try to reformat partition
62 # Leaving possiblity for multiple iterations
63 if ! mount ${emmc_dev}p4 /misc; then
64 printf "Error while mounting /misc, trying to reformat..\n"
65
66 format_count=0
67 while [ "$format_count" -lt "1" ]; do
68 udpu_part_prep ${emmc_dev}p4
69 mkfs.f2fs -q -l misc ${emmc_dev}p4
70 if ! mount ${emmc_dev}p4 /misc; then
71 umount -l /misc
72 printf "Failed while mounting /misc\n"
73 format_count=$((format_count +1))
74 else
75 printf "Mounted /misc successfully\n"
76 break
77 fi
78 done
79 fi
80 fi
81 }
82
83 udpu_do_initial_setup() {
84 # Prepare /recovery parition
85 udpu_part_prep ${emmc_dev}p2
86 mkfs.ext4 -qF ${emmc_dev}p2 2>&1 /dev/null
87
88 # Prepare /boot partition
89 udpu_part_prep ${emmc_dev}p1
90 mkfs.ext4 -qF ${emmc_dev}p1 2>&1 /dev/null
91
92 # Prepare /root partition
93 printf "Formating /root partition, this may take a while..\n"
94 udpu_part_prep ${emmc_dev}p3
95 mkfs.f2fs -q -l rootfs ${emmc_dev}p3 && 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 -woq /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 -woq /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 if tar xzf /misc/firmware/boot.tgz -C /tmp/boot; then
126 printf "/boot partition updated successfully\n"
127 else
128 printf "/boot partition update failed\n"
129 fi
130 sync
131
132 printf "Updating /root partition\n"
133 if tar xzf /misc/firmware/rootfs.tgz -C /tmp/rootpart; then
134 printf "/root partition updated successfully\n"
135 else
136 printf "/root partition update failed\n"
137 fi
138 sync
139
140 # Saving configuration files over sysupgrade
141 platform_copy_config_uDPU
142
143 # Remove tmp mounts
144 tmp_parts=$(grep "${emmc_dev}" /proc/mounts | awk '{print $2}')
145 for part in ${tmp_parts}; do
146 umount $part
147 # Force umount is necessary
148 grep -q "${part}" /proc/mounts && umount -l "$part"
149 done
150
151 # Sysupgrade complains about /tmp and /dev, so we can detach them here
152 umount -l /tmp
153 umount -l /dev
154 }
155
156 platform_copy_config_uDPU() {
157 # Config is saved on the /misc partition and copied on the rootfs after the reboot
158 if [ -f "$UPGRADE_BACKUP" ]; then
159 cp -f "$UPGRADE_BACKUP" "/misc/$BACKUP_FILE"
160 sync
161 fi
162 }