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