platform.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #
  2. # Copyright (C) 2021 OpenWrt.org
  3. #
  4. platform_get_rootfs() {
  5. local rootfsdev
  6. if read cmdline < /proc/cmdline; then
  7. case "$cmdline" in
  8. *root=*)
  9. rootfsdev="${cmdline##*root=}"
  10. rootfsdev="${rootfsdev%% *}"
  11. ;;
  12. esac
  13. echo "${rootfsdev}"
  14. fi
  15. }
  16. platform_copy_config_helper() {
  17. local device=$1
  18. mount -t vfat "$device" /mnt
  19. cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
  20. umount /mnt
  21. }
  22. platform_copy_config() {
  23. case "$(board_name)" in
  24. erlite|\
  25. ubnt,usg)
  26. platform_copy_config_helper /dev/sda1
  27. ;;
  28. itus,shield-router)
  29. platform_copy_config_helper /dev/mmcblk1p1
  30. ;;
  31. ubnt,edgerouter-4|\
  32. ubnt,edgerouter-6p)
  33. platform_copy_config_helper /dev/mmcblk0p1
  34. ;;
  35. esac
  36. }
  37. platform_do_flash() {
  38. local tar_file=$1
  39. local board=$2
  40. local kernel=$3
  41. local rootfs=$4
  42. local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  43. board_dir=${board_dir%/}
  44. [ -n "$board_dir" ] || return 1
  45. mkdir -p /boot
  46. if [ $board = "itus,shield-router" ]; then
  47. # mmcblk1p1 (fat) contains all ELF-bin images for the Shield
  48. mount /dev/mmcblk1p1 /boot
  49. echo "flashing Itus Kernel to /boot/$kernel (/dev/mmblk1p1)"
  50. tar -Oxf $tar_file "$board_dir/kernel" > /boot/$kernel
  51. else
  52. mount -t vfat /dev/$kernel /boot
  53. [ -f /boot/vmlinux.64 -a ! -L /boot/vmlinux.64 ] && {
  54. mv /boot/vmlinux.64 /boot/vmlinux.64.previous
  55. mv /boot/vmlinux.64.md5 /boot/vmlinux.64.md5.previous
  56. }
  57. echo "flashing kernel to /dev/$kernel"
  58. tar xf $tar_file $board_dir/kernel -O > /boot/vmlinux.64
  59. md5sum /boot/vmlinux.64 | cut -f1 -d " " > /boot/vmlinux.64.md5
  60. fi
  61. echo "flashing rootfs to ${rootfs}"
  62. tar xf $tar_file $board_dir/root -O | dd of="${rootfs}" bs=4096
  63. sync
  64. umount /boot
  65. }
  66. platform_do_upgrade() {
  67. local tar_file="$1"
  68. local board=$(board_name)
  69. local rootfs="$(platform_get_rootfs)"
  70. local kernel=
  71. [ -b "${rootfs}" ] || return 1
  72. case "$board" in
  73. er | \
  74. ubnt,edgerouter-4 | \
  75. ubnt,edgerouter-6p)
  76. kernel=mmcblk0p1
  77. ;;
  78. erlite|\
  79. ubnt,usg)
  80. kernel=sda1
  81. ;;
  82. itus,shield-router)
  83. kernel=ItusrouterImage
  84. ;;
  85. *)
  86. return 1
  87. esac
  88. platform_do_flash $tar_file $board $kernel $rootfs
  89. return 0
  90. }
  91. platform_check_image() {
  92. local board=$(board_name)
  93. local tar_file="$1"
  94. local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  95. board_dir=${board_dir%/}
  96. [ -n "$board_dir" ] || return 1
  97. case "$board" in
  98. er | \
  99. erlite | \
  100. itus,shield-router | \
  101. ubnt,edgerouter-4 | \
  102. ubnt,edgerouter-6p | \
  103. ubnt,usg)
  104. local kernel_length=$(tar xf $tar_file $board_dir/kernel -O | wc -c 2> /dev/null)
  105. local rootfs_length=$(tar xf $tar_file $board_dir/root -O | wc -c 2> /dev/null)
  106. [ "$kernel_length" = 0 -o "$rootfs_length" = 0 ] && {
  107. echo "The upgrade image is corrupt."
  108. return 1
  109. }
  110. return 0
  111. ;;
  112. esac
  113. echo "Sysupgrade is not yet supported on $board."
  114. return 1
  115. }