platform.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. REQUIRE_IMAGE_METADATA=1
  2. # Legacy full system upgrade including preloader for MediaTek SoCs on eMMC or SD
  3. legacy_mtk_mmc_full_upgrade() {
  4. local diskdev partdev diff oldrecovery
  5. if grep -q root=/dev/mmcblk0p2 /proc/cmdline; then
  6. oldrecovery=1
  7. else
  8. oldrecovery=2
  9. fi
  10. export_bootdevice && export_partdevice diskdev 0 || {
  11. echo "Unable to determine upgrade device"
  12. return 1
  13. }
  14. #Keep the persistent random mac address (if it exists)
  15. mkdir -p /tmp/recovery
  16. export_partdevice recoverydev $oldrecovery
  17. if mount -o rw,noatime "/dev/$recoverydev" -tvfat /tmp/recovery; then
  18. [ -f "/tmp/recovery/mac_addr" ] && cp /tmp/recovery/mac_addr /tmp/
  19. umount /tmp/recovery
  20. fi
  21. sync
  22. if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
  23. get_partitions "/dev/$diskdev" bootdisk
  24. #extract the boot sector from the image
  25. get_image "$@" | dd of=/tmp/image.bs count=1 bs=512b
  26. get_partitions /tmp/image.bs image
  27. #compare tables
  28. diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
  29. else
  30. diff=1
  31. fi
  32. if [ -n "$diff" ]; then
  33. get_image "$@" | dd of="/dev/$diskdev" bs=4096 conv=fsync
  34. # Separate removal and addition is necessary; otherwise, partition 1
  35. # will be missing if it overlaps with the old partition 2
  36. partx -d - "/dev/$diskdev"
  37. partx -a - "/dev/$diskdev"
  38. else
  39. # iterate over each partition from the image and write it to the boot disk
  40. while read part start size; do
  41. part="$(($part - 2))"
  42. if export_partdevice partdev $part; then
  43. echo "Writing image to /dev/$partdev..."
  44. get_image "$@" | dd of="/dev/$partdev" ibs="512" obs=1M skip="$start" count="$size" conv=fsync
  45. else
  46. echo "Unable to find partition $part device, skipped."
  47. fi
  48. done < /tmp/partmap.image
  49. #copy partition uuid
  50. echo "Writing new UUID to /dev/$diskdev..."
  51. get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
  52. fi
  53. export_partdevice recoverydev 2
  54. if mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery; then
  55. [ -f "/tmp/mac_addr" ] && cp /tmp/mac_addr /tmp/recovery
  56. if [ "$diskdev" = "mmcblk0" -a -r /tmp/recovery/eMMCboot.bin ]; then
  57. echo 0 > /sys/block/mmcblk0boot0/force_ro
  58. dd if=/tmp/recovery/eMMCboot.bin of=/dev/mmcblk0boot0 conv=fsync
  59. sync
  60. echo 1 > /sys/block/mmcblk0boot0/force_ro
  61. fi
  62. sync
  63. umount /tmp/recovery
  64. fi
  65. }
  66. platform_do_upgrade() {
  67. local board=$(board_name)
  68. case "$board" in
  69. bananapi,bpi-r2|\
  70. unielec,u7623-02)
  71. [ -e /dev/fit0 ] && fitblk /dev/fit0
  72. [ -e /dev/fitrw ] && fitblk /dev/fitrw
  73. bootdev="$(fitblk_get_bootdev)"
  74. EMMC_KERN_DEV="/dev/$bootdev"
  75. emmc_do_upgrade "$1"
  76. ;;
  77. unielec,u7623-02-emmc-512m)
  78. local magic="$(get_magic_long "$1")"
  79. if [ "$magic" = "53444d4d" ]; then
  80. legacy_mtk_mmc_full_upgrade "$1"
  81. else # Old partial image starting with uImage
  82. # Keep the persistent random mac address (if it exists)
  83. recoverydev=mmcblk0p1
  84. mkdir -p /tmp/recovery
  85. mount -o rw,noatime /dev/$recoverydev /tmp/recovery
  86. [ -f "/tmp/recovery/mac_addr" ] && \
  87. mv -f /tmp/recovery/mac_addr /tmp/
  88. umount /tmp/recovery
  89. # 1310720 is the offset in bytes from the start of eMMC and to
  90. # the location of the kernel (2560 512 byte sectors)
  91. get_image "$1" | dd of=/dev/mmcblk0 bs=1310720 seek=1 conv=fsync
  92. mount -o rw,noatime /dev/$recoverydev /tmp/recovery
  93. [ -f "/tmp/mac_addr" ] && mv -f /tmp/mac_addr /tmp/recovery
  94. sync
  95. umount /tmp/recovery
  96. fi
  97. ;;
  98. *)
  99. default_do_upgrade "$1"
  100. ;;
  101. esac
  102. }
  103. platform_check_image() {
  104. local magic="$(get_magic_long "$1")"
  105. [ "$#" -gt 1 ] && return 1
  106. case "$(board_name)" in
  107. bananapi,bpi-r2|\
  108. unielec,u7623-02)
  109. [ "$magic" != "d00dfeed" ] && {
  110. echo "Invalid image type."
  111. return 1
  112. }
  113. ;;
  114. unielec,u7623-02-emmc-512m)
  115. # Can always upgrade to the new-style full image
  116. [ "$magic" = "53444d4d" ] && return 0
  117. # need to update to new bootchain via full image first
  118. [ "$magic" = "d00dfeed" ] && {
  119. echo "Please use full eMMC image to update bootloader first."
  120. return 1
  121. }
  122. # Legacy uImage directly at 0xA00 on the eMMC.
  123. [ "$magic" != "27051956" ] && {
  124. echo "Invalid image type."
  125. return 1
  126. }
  127. ;;
  128. *)
  129. echo "Sysupgrade is not supported on your board yet."
  130. return 1
  131. ;;
  132. esac
  133. return 0
  134. }
  135. platform_copy_config() {
  136. case "$(board_name)" in
  137. bananapi,bpi-r2|\
  138. unielec,u7623-02)
  139. emmc_copy_config
  140. ;;
  141. unielec,u7623-02-emmc-512m)
  142. # platform_do_upgrade() will have set $recoverydev
  143. if [ -n "$recoverydev" ]; then
  144. mkdir -p /tmp/recovery
  145. mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery
  146. cp -af "$UPGRADE_BACKUP" "/tmp/recovery/$BACKUP_FILE"
  147. sync
  148. umount /tmp/recovery
  149. fi
  150. ;;
  151. esac
  152. }