mvebu: use device-tree board detection
[openwrt/staging/dedeckeh.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 [ "$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
74 return 0
75 fi
76
77 #write uboot image
78 get_image "$@" | dd of="$diskdev" bs=512 skip=1 seek=1 count=2048 conv=fsync
79 #iterate over each partition from the image and write it to the boot disk
80 while read part start size; do
81 if export_partdevice partdev $part; then
82 echo "Writing image to /dev/$partdev..."
83 get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
84 else
85 echo "Unable to find partition $part device, skipped."
86 fi
87 done < /tmp/partmap.image
88
89 #copy partition uuid
90 echo "Writing new UUID to /dev/$diskdev..."
91 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
92
93 case "$board" in
94 cznic,turris-omnia)
95 fw_setenv openwrt_bootargs 'earlyprintk console=ttyS0,115200 root=/dev/mmcblk0p2 rootfstype=auto rootwait'
96 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'
97 fw_setenv factory_mmcload 'setenv bootargs "$bootargs cfg80211.freg=$regdomain"; btrload mmc 0 0x01000000 boot/zImage @; btrload mmc 0 0x02000000 boot/dtb @'
98 fw_setenv mmcboot 'run openwrt_mmcload || run factory_mmcload; bootz 0x01000000 - 0x02000000'
99 ;;
100 esac
101
102 sleep 1
103 }
104
105 platform_copy_config_sdcard() {
106 local partdev
107
108 if export_partdevice partdev 1; then
109 mkdir -p /boot
110 [ -f /boot/kernel.img ] || mount -o rw,noatime /dev/$partdev /boot
111 cp -af "$CONF_TAR" /boot/
112 sync
113 umount /boot
114 fi
115 }