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