mvebu: Add basic support for WRT1900AC (v1) and Turris Omnia (pre 2019)
[librecmc/librecmc.git] / target / linux / mvebu / base-files / lib / upgrade / sdcard.sh
1 get_magic_at() {
2         local file="$1"
3         local pos="$2"
4         get_image "$file" | dd bs=1 count=2 skip="$pos" 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
5 }
6
7 platform_check_image_sdcard() {
8         local file="$1"
9         local magic diskdev partdev diff
10
11         magic=$(get_magic_at "$file" 510)
12         [ "$magic" != "55aa" ] && {
13                 echo "Failed to verify MBR boot signature."
14                 return 1
15         }
16
17         export_bootdevice && export_partdevice diskdev 0 || {
18                 echo "Unable to determine upgrade device"
19         return 1
20         }
21
22         get_partitions "/dev/$diskdev" bootdisk
23
24         #extract the boot sector from the image
25         get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null
26
27         get_partitions /tmp/image.bs image
28
29         #compare tables
30         diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
31
32         rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
33
34         if [ -n "$diff" ]; then
35                 echo "Partition layout has changed. Full image will be written."
36                 ask_bool 0 "Abort" && exit 1
37                 return 0
38         fi
39 }
40
41 platform_do_upgrade_sdcard() {
42         local board=$(board_name)
43         local diskdev partdev diff
44
45         export_bootdevice && export_partdevice diskdev 0 || {
46                 echo "Unable to determine upgrade device"
47         return 1
48         }
49
50         sync
51
52         if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
53                 get_partitions "/dev/$diskdev" bootdisk
54
55                 #extract the boot sector from the image
56                 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
57
58                 get_partitions /tmp/image.bs image
59
60                 #compare tables
61                 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
62         else
63                 diff=1
64         fi
65
66         if [ -n "$diff" ]; then
67                 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
68
69                 # Separate removal and addtion is necessary; otherwise, partition 1
70                 # will be missing if it overlaps with the old partition 2
71                 partx -d - "/dev/$diskdev"
72                 partx -a - "/dev/$diskdev"
73         else
74                 #write uboot image
75                 get_image "$@" | dd of="$diskdev" bs=512 skip=1 seek=1 count=2048 conv=fsync
76                 #iterate over each partition from the image and write it to the boot disk
77                 while read part start size; do
78                         if export_partdevice partdev $part; then
79                                 echo "Writing image to /dev/$partdev..."
80                                 get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
81                         else
82                                 echo "Unable to find partition $part device, skipped."
83                         fi
84                 done < /tmp/partmap.image
85
86                 #copy partition uuid
87                 echo "Writing new UUID to /dev/$diskdev..."
88                 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
89         fi
90
91         case "$board" in
92         cznic,turris-omnia)
93                 fw_setenv openwrt_bootargs 'earlyprintk console=ttyS0,115200 root=/dev/mmcblk0p2 rootfstype=auto rootwait'
94                 fw_setenv openwrt_mmcload 'setenv bootargs "$openwrt_bootargs cfg80211.freg=$regdomain"; fatload mmc 0 0x01000000 zImage; fatload mmc 0 0x02000000 armada-385-turris-omnia.dtb'
95                 fw_setenv factory_mmcload 'setenv bootargs "$bootargs cfg80211.freg=$regdomain"; btrload mmc 0 0x01000000 boot/zImage @; btrload mmc 0 0x02000000 boot/dtb @'
96                 fw_setenv mmcboot 'run openwrt_mmcload || run factory_mmcload; bootz 0x01000000 - 0x02000000'
97                 ;;
98         esac
99
100         sleep 1
101 }
102
103 platform_copy_config_sdcard() {
104         local partdev
105
106         if export_partdevice partdev 1; then
107                 mkdir -p /boot
108                 [ -f /boot/kernel.img ] || mount -o rw,noatime /dev/$partdev /boot
109                 cp -af "$UPGRADE_BACKUP" "/boot/$BACKUP_FILE"
110                 sync
111                 umount /boot
112         fi
113 }