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