platform.sh 2.2 KB

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