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