d3e9f360aadedad0995da55205364940c9884ba4
[openwrt/openwrt.git] / target / linux / x86 / base-files / lib / upgrade / platform.sh
1 platform_check_image() {
2 [ "$#" -gt 1 ] && return 1
3
4 case "$(get_magic_word "$1")" in
5 eb48|eb63) return 0;;
6 *)
7 echo "Invalid image type"
8 return 1
9 ;;
10 esac
11 }
12
13 platform_copy_config() {
14 local partdev
15
16 if export_partdevice partdev 1; then
17 mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
18 cp -af "$CONF_TAR" /mnt/
19 umount /mnt
20 fi
21 }
22
23 platform_do_upgrade() {
24 local diskdev partdev diff
25
26 if export_bootdevice && export_partdevice diskdev 0; then
27 sync
28 if [ "$SAVE_PARTITIONS" = "1" ]; then
29 get_partitions "/dev/$diskdev" bootdisk
30
31 #extract the boot sector from the image
32 get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
33
34 get_partitions /tmp/image.bs image
35
36 #compare tables
37 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
38 if [ -n "$diff" ]; then
39 echo "Partition layout is changed. Full image will be written."
40 ask_bool 0 "Abort" && exit
41
42 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
43 return 0
44 fi
45
46 #iterate over each partition from the image and write it to the boot disk
47 while read part start size; do
48 if export_partdevice partdev $part; then
49 echo "Writing image to /dev/$partdev..."
50 get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
51 else
52 echo "Unable to find partition $part device, skipped."
53 fi
54 done < /tmp/partmap.image
55
56 #copy partition uuid
57 echo "Writing new UUID to /dev/$diskdev..."
58 get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
59 else
60 get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
61 fi
62
63 sleep 1
64 fi
65 }