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