sdcard.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. get_magic_at() {
  2. local file="$1"
  3. local pos="$2"
  4. get_image "$file" | dd bs=1 count=2 skip="$pos" 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
  5. }
  6. platform_check_image_sdcard() {
  7. local file="$1"
  8. local magic diskdev partdev diff
  9. magic=$(get_magic_at "$file" 510)
  10. [ "$magic" != "55aa" ] && {
  11. echo "Failed to verify MBR boot signature."
  12. return 1
  13. }
  14. export_bootdevice && export_partdevice diskdev 0 || {
  15. echo "Unable to determine upgrade device"
  16. return 1
  17. }
  18. get_partitions "/dev/$diskdev" bootdisk
  19. #extract the boot sector from the image
  20. get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b 2>/dev/null
  21. get_partitions /tmp/image.bs image
  22. #compare tables
  23. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  24. rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
  25. if [ -n "$diff" ]; then
  26. echo "Partition layout has changed. Full image will be written."
  27. ask_bool 0 "Abort" && exit 1
  28. return 0
  29. fi
  30. }
  31. platform_do_upgrade_sdcard() {
  32. local board=$(board_name)
  33. local diskdev partdev diff
  34. export_bootdevice && export_partdevice diskdev 0 || {
  35. echo "Unable to determine upgrade device"
  36. return 1
  37. }
  38. sync
  39. if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
  40. get_partitions "/dev/$diskdev" bootdisk
  41. #extract the boot sector from the image
  42. get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
  43. get_partitions /tmp/image.bs image
  44. #compare tables
  45. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  46. else
  47. diff=1
  48. fi
  49. if [ -n "$diff" ]; then
  50. get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  51. # Separate removal and addtion is necessary; otherwise, partition 1
  52. # will be missing if it overlaps with the old partition 2
  53. partx -d - "/dev/$diskdev"
  54. partx -a - "/dev/$diskdev"
  55. else
  56. #write uboot image
  57. get_image "$@" | dd of="$diskdev" bs=512 skip=1 seek=1 count=2048 conv=fsync
  58. #iterate over each partition from the image and write it to the boot disk
  59. while read part start size; do
  60. if export_partdevice partdev $part; then
  61. echo "Writing image to /dev/$partdev..."
  62. get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
  63. else
  64. echo "Unable to find partition $part device, skipped."
  65. fi
  66. done < /tmp/partmap.image
  67. #copy partition uuid
  68. echo "Writing new UUID to /dev/$diskdev..."
  69. get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  70. fi
  71. case "$board" in
  72. cznic,turris-omnia)
  73. fw_setenv openwrt_bootargs 'earlyprintk console=ttyS0,115200 root=/dev/mmcblk0p2 rootfstype=auto rootwait'
  74. fw_setenv openwrt_mmcload 'setenv bootargs "$openwrt_bootargs cfg80211.freg=$regdomain"; fatload mmc 0 0x01000000 zImage; fatload mmc 0 0x02000000 armada-385-turris-omnia.dtb'
  75. fw_setenv factory_mmcload 'setenv bootargs "$bootargs cfg80211.freg=$regdomain"; btrload mmc 0 0x01000000 boot/zImage @; btrload mmc 0 0x02000000 boot/dtb @'
  76. fw_setenv mmcboot 'run openwrt_mmcload || run factory_mmcload; bootz 0x01000000 - 0x02000000'
  77. ;;
  78. esac
  79. sleep 1
  80. }
  81. platform_copy_config_sdcard() {
  82. local partdev
  83. if export_partdevice partdev 1; then
  84. mkdir -p /boot
  85. [ -f /boot/kernel.img ] || mount -o rw,noatime /dev/$partdev /boot
  86. cp -af "$UPGRADE_BACKUP" "/boot/$BACKUP_FILE"
  87. sync
  88. umount /boot
  89. fi
  90. }