treewide: use new procd sysupgrade $UPGRADE_BACKUP variable
[openwrt/openwrt.git] / target / linux / apm821xx / base-files / lib / upgrade / wdbook.sh
1 . /lib/functions.sh
2
3 # copied from x86's platform.sh
4
5 mbl_do_platform_check() {
6 local diskdev partdev diff
7
8 [ "$#" -gt 1 ] && return 1
9
10 export_bootdevice && export_partdevice diskdev 0 || {
11 echo "Unable to determine upgrade device"
12 return 1
13 }
14
15 get_partitions "/dev/$diskdev" bootdisk
16
17 #extract the boot sector from the image
18 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null
19
20 get_partitions /tmp/image.bs image
21
22 #compare tables
23 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
24
25 rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
26
27 if [ -n "$diff" ]; then
28 echo "Partition layout has changed. Full image will be written."
29 ask_bool 0 "Abort" && exit 1
30 return 0
31 fi
32
33 return 0;
34 }
35
36 mbl_do_upgrade() {
37 local diskdev partdev diff
38
39 export_bootdevice && export_partdevice diskdev 0 || {
40 echo "Unable to determine upgrade device"
41 return 1
42 }
43
44 sync
45
46 if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
47 get_partitions "/dev/$diskdev" bootdisk
48
49 #extract the boot sector from the image
50 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
51
52 get_partitions /tmp/image.bs image
53
54 #compare tables
55 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
56 else
57 diff=1
58 fi
59
60 if [ -n "$diff" ]; then
61 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
62
63 # Separate removal and addtion is necessary; otherwise, partition 1
64 # will be missing if it overlaps with the old partition 2
65 partx -d - "/dev/$diskdev"
66 partx -a - "/dev/$diskdev"
67
68 return 0
69 fi
70
71 #iterate over each partition from the image and write it to the boot disk
72 while read part start size; do
73 if export_partdevice partdev $part; then
74 echo "Writing image to /dev/$partdev..."
75 get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
76 else
77 echo "Unable to find partition $part device, skipped."
78 fi
79 done < /tmp/partmap.image
80
81 #copy partition uuid
82 echo "Writing new UUID to /dev/$diskdev..."
83 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
84 }
85
86 mbl_copy_config() {
87 local partdev
88
89 if export_partdevice partdev 1; then
90 mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
91 cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
92 umount /mnt
93 fi
94 }