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