platform.sh 2.5 KB

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