platform.sh 4.4 KB

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