e885f460e9a5d9bd433749c60d83469d72fa26fd
[openwrt/staging/mkresin.git] / target / linux / mediatek / mt7623 / base-files / lib / upgrade / platform.sh
1
2 REQUIRE_IMAGE_METADATA=1
3 RAMFS_COPY_BIN='fwtool'
4
5 # Legacy full system upgrade including preloader for MediaTek SoCs on eMMC or SD
6 legacy_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 [ "$UPGRADE_OPT_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 unielec,u7623-02)
88 export_bootdevice
89 export_partdevice fitpart 3
90 [ "$fitpart" ] || return 1
91 EMMC_KERN_DEV="/dev/$fitpart"
92 emmc_do_upgrade "$1"
93 ;;
94 unielec,u7623-02-emmc-512m)
95 local magic="$(get_magic_long "$1")"
96 if [ "$magic" = "53444d4d" ]; then
97 legacy_mtk_mmc_full_upgrade "$1"
98 else # Old partial image starting with uImage
99 # Keep the persistent random mac address (if it exists)
100 recoverydev=mmcblk0p1
101 mkdir -p /tmp/recovery
102 mount -o rw,noatime /dev/$recoverydev /tmp/recovery
103 [ -f "/tmp/recovery/mac_addr" ] && \
104 mv -f /tmp/recovery/mac_addr /tmp/
105 umount /tmp/recovery
106
107 # 1310720 is the offset in bytes from the start of eMMC and to
108 # the location of the kernel (2560 512 byte sectors)
109 get_image "$1" | dd of=/dev/mmcblk0 bs=1310720 seek=1 conv=fsync
110
111 mount -o rw,noatime /dev/$recoverydev /tmp/recovery
112 [ -f "/tmp/mac_addr" ] && mv -f /tmp/mac_addr /tmp/recovery
113 sync
114 umount /tmp/recovery
115 fi
116 ;;
117 *)
118 default_do_upgrade "$1"
119 ;;
120 esac
121 }
122
123 platform_check_image() {
124 local magic="$(get_magic_long "$1")"
125
126 [ "$#" -gt 1 ] && return 1
127
128 case "$(board_name)" in
129 bananapi,bpi-r2|\
130 unielec,u7623-02)
131 [ "$magic" != "d00dfeed" ] && {
132 echo "Invalid image type."
133 return 1
134 }
135 ;;
136 unielec,u7623-02-emmc-512m)
137 # Can always upgrade to the new-style full image
138 [ "$magic" = "53444d4d" ] && return 0
139
140 # need to update to new bootchain via full image first
141 [ "$magic" = "d00dfeed" ] && {
142 echo "Please use full eMMC image to update bootloader first."
143 return 1
144 }
145
146 # Legacy uImage directly at 0xA00 on the eMMC.
147 [ "$magic" != "27051956" ] && {
148 echo "Invalid image type."
149 return 1
150 }
151 ;;
152 *)
153 echo "Sysupgrade is not supported on your board yet."
154 return 1
155 ;;
156 esac
157
158 return 0
159 }
160
161 platform_copy_config() {
162 case "$(board_name)" in
163 bananapi,bpi-r2|\
164 unielec,u7623-02)
165 emmc_copy_config
166 ;;
167 unielec,u7623-02-emmc-512m)
168 # platform_do_upgrade() will have set $recoverydev
169 if [ -n "$recoverydev" ]; then
170 mkdir -p /tmp/recovery
171 mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery
172 cp -af "$UPGRADE_BACKUP" "/tmp/recovery/$BACKUP_FILE"
173 sync
174 umount /tmp/recovery
175 fi
176 ;;
177 esac
178 }