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