From ce06d2bd01a3dad1ac32230cd60bb779c96cec82 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 4 May 2017 07:39:20 +0200 Subject: [PATCH] sunxi: sysupgrade: sync with x86 sunxi sysupgrade was based on the x86 implementation; sync fixes and other changes from the current x86 version: x86: fix sysupgrades on disks with 4k block size x86: sysupgrade: move partition table change check to platform_check_image x86: sysupgrade: refactor platform_do_upgrade x86: sysupgrade: explicitly rescan disk after writing partition table Signed-off-by: Matthias Schiffer --- target/linux/sunxi/Makefile | 2 +- .../sunxi/base-files/lib/upgrade/platform.sh | 112 +++++++++++------- 2 files changed, 68 insertions(+), 46 deletions(-) diff --git a/target/linux/sunxi/Makefile b/target/linux/sunxi/Makefile index 3c2f14b841..f8b8aa750a 100644 --- a/target/linux/sunxi/Makefile +++ b/target/linux/sunxi/Makefile @@ -28,6 +28,6 @@ KERNELNAME:=zImage dtbs include $(INCLUDE_DIR)/target.mk DEFAULT_PACKAGES += uboot-envtools -DEFAULT_PACKAGES += mkf2fs e2fsprogs +DEFAULT_PACKAGES += partx-utils mkf2fs e2fsprogs $(eval $(call BuildTarget)) diff --git a/target/linux/sunxi/base-files/lib/upgrade/platform.sh b/target/linux/sunxi/base-files/lib/upgrade/platform.sh index 776bdf53bf..88ef4790e9 100644 --- a/target/linux/sunxi/base-files/lib/upgrade/platform.sh +++ b/target/linux/sunxi/base-files/lib/upgrade/platform.sh @@ -1,5 +1,28 @@ platform_check_image() { - true + local diskdev partdev diff + + export_bootdevice && export_partdevice diskdev -2 || { + echo "Unable to determine upgrade device" + return 1 + } + + get_partitions "/dev/$diskdev" bootdisk + + #extract the boot sector from the image + get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null + + get_partitions /tmp/image.bs image + + #compare tables + diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)" + + rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image + + if [ -n "$diff" ]; then + echo "Partition layout has changed. Full image will be written." + ask_bool 0 "Abort" && exit 1 + return 0 + fi } platform_copy_config() { @@ -13,55 +36,54 @@ platform_copy_config() { } platform_do_upgrade() { - local diskdev partdev ibs diff + local diskdev partdev diff - if export_bootdevice && export_partdevice diskdev -2; then - sync - if [ "$SAVE_PARTITIONS" = "1" ]; then - get_partitions "/dev/$diskdev" bootdisk + export_bootdevice && export_partdevice diskdev -2 || { + echo "Unable to determine upgrade device" + return 1 + } - #get block size - if [ -f "/sys/block/$diskdev/queue/physical_block_size" ]; then - ibs="$(cat "/sys/block/$diskdev/queue/physical_block_size")" - else - ibs=512 - fi + sync - #extract the boot sector from the image - get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b + if [ "$SAVE_PARTITIONS" = "1" ]; then + get_partitions "/dev/$diskdev" bootdisk - get_partitions /tmp/image.bs image + #extract the boot sector from the image + get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b - #compare tables - diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)" - if [ -n "$diff" ]; then - echo "Partition layout is changed. Full image will be written." - ask_bool 0 "Abort" && exit + get_partitions /tmp/image.bs image - get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync - return 0 - fi - - #write uboot image - get_image "$@" | dd of="$diskdev" bs=1024 skip=8 seek=8 count=1016 conv=fsync - #iterate over each partition from the image and write it to the boot disk - while read part start size; do - part="$(($part - 2))" - if export_partdevice partdev $part; then - echo "Writing image to /dev/$partdev..." - get_image "$@" | dd of="/dev/$partdev" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync - else - echo "Unable to find partition $part device, skipped." - fi - done < /tmp/partmap.image - - #copy partition uuid - echo "Writing new UUID to /dev/$diskdev..." - get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync - else - get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync - fi - - sleep 1 + #compare tables + diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)" + else + diff=1 fi + + if [ -n "$diff" ]; then + get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync + + # Separate removal and addtion is necessary; otherwise, partition 1 + # will be missing if it overlaps with the old partition 2 + partx -d - "/dev/$diskdev" + partx -a - "/dev/$diskdev" + + return 0 + fi + + #write uboot image + get_image "$@" | dd of="$diskdev" bs=1024 skip=8 seek=8 count=1016 conv=fsync + #iterate over each partition from the image and write it to the boot disk + while read part start size; do + part="$(($part - 2))" + if export_partdevice partdev $part; then + echo "Writing image to /dev/$partdev..." + get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync + else + echo "Unable to find partition $part device, skipped." + fi + done < /tmp/partmap.image + + #copy partition uuid + echo "Writing new UUID to /dev/$diskdev..." + get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync }