mediatek: mt7623: make use of find_mmc_part
[openwrt/openwrt.git] / target / linux / mediatek / mt7623 / base-files / lib / upgrade / platform.sh
1
2 REQUIRE_IMAGE_METADATA=1
3 RAMFS_COPY_BIN='blockdev'
4
5 # Full system upgrade including preloader for MediaTek SoCs on eMMC or SD
6 mtk_mmc_full_upgrade() {
7 local diskdev partdev diff oldrecovery
8
9 if grep -q root=/dev/mmcblk0p2 /proc/cmdline; then
10 oldrecovery=1
11 else
12 oldrecovery=2
13 fi
14
15 export_bootdevice && export_partdevice diskdev 0 || {
16 echo "Unable to determine upgrade device"
17 return 1
18 }
19
20 #Keep the persistent random mac address (if it exists)
21 mkdir -p /tmp/recovery
22 export_partdevice recoverydev $oldrecovery
23 if mount -o rw,noatime "/dev/$recoverydev" -tvfat /tmp/recovery; then
24 [ -f "/tmp/recovery/mac_addr" ] && cp /tmp/recovery/mac_addr /tmp/
25 umount /tmp/recovery
26 fi
27 sync
28
29 if [ "$SAVE_PARTITIONS" = "1" ]; then
30 get_partitions "/dev/$diskdev" bootdisk
31
32 #extract the boot sector from the image
33 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
34
35 get_partitions /tmp/image.bs image
36
37 #compare tables
38 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
39 else
40 diff=1
41 fi
42
43 if [ -n "$diff" ]; then
44 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
45
46 # Separate removal and addition is necessary; otherwise, partition 1
47 # will be missing if it overlaps with the old partition 2
48 partx -d - "/dev/$diskdev"
49 partx -a - "/dev/$diskdev"
50 else
51 # iterate over each partition from the image and write it to the boot disk
52 while read part start size; do
53 part="$(($part - 2))"
54 if export_partdevice partdev $part; then
55 echo "Writing image to /dev/$partdev..."
56 get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
57 else
58 echo "Unable to find partition $part device, skipped."
59 fi
60 done < /tmp/partmap.image
61
62 #copy partition uuid
63 echo "Writing new UUID to /dev/$diskdev..."
64 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
65 fi
66
67 export_partdevice recoverydev 2
68 if mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery; then
69 [ -f "/tmp/mac_addr" ] && cp /tmp/mac_addr /tmp/recovery
70
71 if [ "$diskdev" = "mmcblk0" -a -r /tmp/recovery/eMMCboot.bin ]; then
72 echo 0 > /sys/block/mmcblk0boot0/force_ro
73 dd if=/tmp/recovery/eMMCboot.bin of=/dev/mmcblk0boot0 conv=fsync
74 sync
75 echo 1 > /sys/block/mmcblk0boot0/force_ro
76 fi
77 sync
78 umount /tmp/recovery
79 fi
80 }
81
82 platform_do_upgrade() {
83 local board=$(board_name)
84
85 case "$board" in
86 bananapi,bpi-r2)
87 export_bootdevice
88 export_partdevice rootdev 0
89 blockdev --rereadpt /dev/$rootdev || return 1
90 export_partdevice fitpart 3
91 [ "$fitpart" ] || return 1
92 dd if=/dev/zero of=$fitpart bs=4096 count=1 2>/dev/null
93 blockdev --rereadpt /dev/$rootdev
94 get_image "$1" | dd of=$fitpart
95 blockdev --rereadpt /dev/$rootdev
96 local datapart=$(find_mmc_part "rootfs_data" $rootdev)
97 [ "$datapart" ] || return 0
98 dd if=/dev/zero of=$datapart bs=4096 count=1 2>/dev/null
99 echo $datapart > /tmp/sysupgrade.datapart
100 ;;
101
102 unielec,u7623-02-emmc-512m)
103 local magic="$(get_magic_long "$1")"
104 if [ "$magic" = "53444d4d" ]; then
105 mtk_mmc_full_upgrade "$1"
106 else # Old partial image starting with uImage
107 # Keep the persistent random mac address (if it exists)
108 recoverydev=mmcblk0p1
109 mkdir -p /tmp/recovery
110 mount -o rw,noatime /dev/$recoverydev /tmp/recovery
111 [ -f "/tmp/recovery/mac_addr" ] && \
112 mv -f /tmp/recovery/mac_addr /tmp/
113 umount /tmp/recovery
114
115 # 1310720 is the offset in bytes from the start of eMMC and to
116 # the location of the kernel (2560 512 byte sectors)
117 get_image "$1" | dd of=/dev/mmcblk0 bs=1310720 seek=1 conv=fsync
118
119 mount -o rw,noatime /dev/$recoverydev /tmp/recovery
120 [ -f "/tmp/mac_addr" ] && mv -f /tmp/mac_addr /tmp/recovery
121 sync
122 umount /tmp/recovery
123 fi
124 ;;
125 *)
126 default_do_upgrade "$1"
127 ;;
128 esac
129 }
130
131 PART_NAME=firmware
132
133 platform_check_image() {
134 local board=$(board_name)
135 local magic="$(get_magic_long "$1")"
136 local diskdev partdev diff
137
138 [ "$#" -gt 1 ] && return 1
139
140 case "$board" in
141 bananapi,bpi-r2)
142 [ "$magic" != "d00dfeed" ] && {
143 echo "Invalid image type."
144 return 1
145 }
146 ;;
147 unielec,u7623-02-emmc-512m)
148 # Can always upgrade to the new-style full image
149 [ "$magic" = "53444d4d" ] && return 0
150
151 # Legacy uImage directly at 0xA00 on the eMMC.
152 [ "$magic" != "27051956" ] && {
153 echo "Invalid image type."
154 return 1
155 }
156 rootpart=$(cat /proc/cmdline)
157 rootpart="${rootpart##*root=}"
158 rootpart="${rootpart%% *}"
159 [ "$rootpart" != "/dev/mmcblk0p2" ] && {
160 echo "Cannot downgrade to legacy image."
161 return 1
162 }
163 return 0
164 ;;
165 *)
166 echo "Sysupgrade is not supported on your board yet."
167 return 1
168 ;;
169 esac
170
171 return 0
172 }
173
174 platform_copy_config_mmc() {
175 [ -e "$UPGRADE_BACKUP" ] || return
176 local datapart=$(cat /tmp/sysupgrade.datapart)
177 [ "$datapart" ] || echo "no rootfs_data partition, cannot keep configuration." >&2
178 dd if="$UPGRADE_BACKUP" of=$datapart
179 sync
180 }
181
182 platform_copy_config() {
183 case "$(board_name)" in
184 bananapi,bpi-r2)
185 platform_copy_config_mmc
186 ;;
187 unielec,u7623-02-emmc-512m)
188 # platform_do_upgrade() will have set $recoverydev
189 if [ -n "$recoverydev" ]; then
190 mkdir -p /tmp/recovery
191 mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery
192 cp -af "$UPGRADE_BACKUP" "/tmp/recovery/$BACKUP_FILE"
193 sync
194 umount /tmp/recovery
195 fi
196 ;;
197 esac
198 }