platform.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. platform_export_partdevice() {
  2. local var="$1" offset="$2"
  3. local uevent MAJOR MINOR DEVNAME DEVTYPE
  4. for uevent in /sys/class/block/*/uevent; do
  5. . "$uevent"
  6. if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  7. export "$var=$DEVNAME"
  8. return 0
  9. fi
  10. done
  11. return 1
  12. }
  13. platform_export_bootdevice() {
  14. local cmdline uuid disk uevent
  15. local MAJOR MINOR DEVNAME DEVTYPE
  16. if read cmdline < /proc/cmdline; then
  17. case "$cmdline" in
  18. *block2mtd=*)
  19. disk="${cmdline##*block2mtd=}"
  20. disk="${disk%%,*}"
  21. ;;
  22. *root=*)
  23. disk="${cmdline##*root=}"
  24. disk="${disk%% *}"
  25. ;;
  26. esac
  27. case "$disk" in
  28. PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-02)
  29. uuid="${disk#PARTUUID=}"
  30. uuid="${uuid%-02}"
  31. for disk in $(find /dev -type b); do
  32. set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  33. if [ "$4$3$2$1" = "$uuid" ]; then
  34. uevent="/sys/class/block/${disk##*/}/uevent"
  35. break
  36. fi
  37. done
  38. ;;
  39. /dev/*)
  40. uevent="/sys/class/block/${disk##*/}/uevent"
  41. ;;
  42. esac
  43. if [ -e "$uevent" ]; then
  44. . "$uevent"
  45. export BOOTDEV_MAJOR=$MAJOR
  46. export BOOTDEV_MINOR=$MINOR
  47. return 0
  48. fi
  49. fi
  50. return 1
  51. }
  52. platform_check_image() {
  53. [ "$#" -gt 1 ] && return 1
  54. case "$(get_magic_word "$1")" in
  55. eb48|eb63) return 0;;
  56. *)
  57. echo "Invalid image type"
  58. return 1
  59. ;;
  60. esac
  61. }
  62. platform_copy_config() {
  63. local partdev
  64. if platform_export_partdevice partdev 1; then
  65. mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
  66. cp -af "$CONF_TAR" /mnt/
  67. umount /mnt
  68. fi
  69. }
  70. get_partitions() { # <device> <filename>
  71. local disk="$1"
  72. local filename="$2"
  73. if [ -b "$disk" -o -f "$disk" ]; then
  74. echo "Reading partition table from $filename..."
  75. local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
  76. if [ "$magic" != 0xAA55 ]; then
  77. echo "Invalid partition table on $disk"
  78. exit
  79. fi
  80. rm -f "/tmp/partmap.$filename"
  81. local part
  82. for part in 1 2 3 4; do
  83. set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  84. local type="$(($1 % 256))"
  85. local lba="$(($2))"
  86. local num="$(($3))"
  87. [ $type -gt 0 ] || continue
  88. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  89. done
  90. fi
  91. }
  92. platform_do_upgrade() {
  93. local diskdev partdev ibs diff
  94. if platform_export_bootdevice && platform_export_partdevice diskdev 0; then
  95. sync
  96. if [ "$SAVE_PARTITIONS" = "1" ]; then
  97. get_partitions "/dev/$diskdev" bootdisk
  98. #get block size
  99. if [ -f "/sys/block/$diskdev/queue/physical_block_size" ]; then
  100. ibs="$(cat "/sys/block/$diskdev/queue/physical_block_size")"
  101. else
  102. ibs=512
  103. fi
  104. #extract the boot sector from the image
  105. get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
  106. get_partitions /tmp/image.bs image
  107. #compare tables
  108. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  109. if [ -n "$diff" ]; then
  110. echo "Partition layout is changed. Full image will be written."
  111. ask_bool 0 "Abort" && exit
  112. get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  113. return 0
  114. fi
  115. #iterate over each partition from the image and write it to the boot disk
  116. while read part start size; do
  117. if platform_export_partdevice partdev $part; then
  118. echo "Writing image to /dev/$partdev..."
  119. get_image "$@" | dd of="/dev/$partdev" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
  120. else
  121. echo "Unable to find partition $part device, skipped."
  122. fi
  123. done < /tmp/partmap.image
  124. #copy partition uuid
  125. echo "Writing new UUID to /dev/$diskdev..."
  126. get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  127. else
  128. get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  129. fi
  130. sleep 1
  131. fi
  132. }