sdcard.sh 2.3 KB

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