platform.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. REQUIRE_IMAGE_METADATA=1
  2. RAMFS_COPY_BIN='fwtool'
  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. export_bootdevice
  73. export_partdevice fitpart 3
  74. [ "$fitpart" ] || return 1
  75. EMMC_KERN_DEV="/dev/$fitpart"
  76. emmc_do_upgrade "$1"
  77. ;;
  78. unielec,u7623-02-emmc-512m)
  79. local magic="$(get_magic_long "$1")"
  80. if [ "$magic" = "53444d4d" ]; then
  81. legacy_mtk_mmc_full_upgrade "$1"
  82. else # Old partial image starting with uImage
  83. # Keep the persistent random mac address (if it exists)
  84. recoverydev=mmcblk0p1
  85. mkdir -p /tmp/recovery
  86. mount -o rw,noatime /dev/$recoverydev /tmp/recovery
  87. [ -f "/tmp/recovery/mac_addr" ] && \
  88. mv -f /tmp/recovery/mac_addr /tmp/
  89. umount /tmp/recovery
  90. # 1310720 is the offset in bytes from the start of eMMC and to
  91. # the location of the kernel (2560 512 byte sectors)
  92. get_image "$1" | dd of=/dev/mmcblk0 bs=1310720 seek=1 conv=fsync
  93. mount -o rw,noatime /dev/$recoverydev /tmp/recovery
  94. [ -f "/tmp/mac_addr" ] && mv -f /tmp/mac_addr /tmp/recovery
  95. sync
  96. umount /tmp/recovery
  97. fi
  98. ;;
  99. *)
  100. default_do_upgrade "$1"
  101. ;;
  102. esac
  103. }
  104. platform_check_image() {
  105. local magic="$(get_magic_long "$1")"
  106. [ "$#" -gt 1 ] && return 1
  107. case "$(board_name)" in
  108. bananapi,bpi-r2|\
  109. unielec,u7623-02)
  110. [ "$magic" != "d00dfeed" ] && {
  111. echo "Invalid image type."
  112. return 1
  113. }
  114. ;;
  115. unielec,u7623-02-emmc-512m)
  116. # Can always upgrade to the new-style full image
  117. [ "$magic" = "53444d4d" ] && return 0
  118. # need to update to new bootchain via full image first
  119. [ "$magic" = "d00dfeed" ] && {
  120. echo "Please use full eMMC image to update bootloader first."
  121. return 1
  122. }
  123. # Legacy uImage directly at 0xA00 on the eMMC.
  124. [ "$magic" != "27051956" ] && {
  125. echo "Invalid image type."
  126. return 1
  127. }
  128. ;;
  129. *)
  130. echo "Sysupgrade is not supported on your board yet."
  131. return 1
  132. ;;
  133. esac
  134. return 0
  135. }
  136. platform_copy_config() {
  137. case "$(board_name)" in
  138. bananapi,bpi-r2|\
  139. unielec,u7623-02)
  140. emmc_copy_config
  141. ;;
  142. unielec,u7623-02-emmc-512m)
  143. # platform_do_upgrade() will have set $recoverydev
  144. if [ -n "$recoverydev" ]; then
  145. mkdir -p /tmp/recovery
  146. mount -o rw,noatime "/dev/$recoverydev" -t vfat /tmp/recovery
  147. cp -af "$UPGRADE_BACKUP" "/tmp/recovery/$BACKUP_FILE"
  148. sync
  149. umount /tmp/recovery
  150. fi
  151. ;;
  152. esac
  153. }