platform.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #
  2. # Copyright (C) 2021 OpenWrt.org
  3. #
  4. if [ -x /usr/sbin/blkid ]; then
  5. RAMFS_COPY_BIN="/usr/sbin/blkid"
  6. fi
  7. platform_get_rootfs() {
  8. local rootfsdev
  9. local rootpartuuid
  10. if read cmdline < /proc/cmdline; then
  11. case "$cmdline" in
  12. *root=PARTUUID=*)
  13. rootpartuuid="${cmdline##*root=PARTUUID=}"
  14. rootpartuuid="${rootpartuuid%% *}"
  15. rootfsdev="$(blkid -o device -t PARTUUID="${rootpartuuid}")"
  16. ;;
  17. *root=*)
  18. rootfsdev="${cmdline##*root=}"
  19. rootfsdev="${rootfsdev%% *}"
  20. ;;
  21. esac
  22. echo "${rootfsdev}"
  23. fi
  24. }
  25. platform_get_n821_disk() {
  26. local partnum=$1
  27. local DEVNAME
  28. while read line; do
  29. export -n "${line}"
  30. done < $(find /sys/bus/platform/devices/16f0000000000.ehci/ -path \*block/sd[a-z]/uevent)
  31. echo "/dev/${DEVNAME}${partnum}"
  32. }
  33. platform_copy_config_helper() {
  34. local device=$1
  35. local fstype=$2
  36. mount -t "${fstype}" "$device" /mnt
  37. cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
  38. umount /mnt
  39. }
  40. platform_copy_config() {
  41. case "$(board_name)" in
  42. ubnt,erlite|\
  43. ubnt,usg)
  44. platform_copy_config_helper /dev/sda1 vfat
  45. ;;
  46. itus,shield-router)
  47. platform_copy_config_helper /dev/mmcblk1p1 vfat
  48. ;;
  49. er|\
  50. ubnt,edgerouter-4|\
  51. ubnt,edgerouter-6p)
  52. platform_copy_config_helper /dev/mmcblk0p1 vfat
  53. ;;
  54. cisco,vedge1000)
  55. platform_copy_config_helper "$(platform_get_n821_disk 1)" ext2
  56. ;;
  57. esac
  58. }
  59. platform_do_flash() {
  60. local tar_file=$1
  61. local board=$2
  62. local kernel=$3
  63. local rootfs=$4
  64. local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  65. board_dir=${board_dir%/}
  66. [ -n "$board_dir" ] || return 1
  67. mkdir -p /boot
  68. if [ $board = "itus,shield-router" ]; then
  69. # mmcblk1p1 (fat) contains all ELF-bin images for the Shield
  70. mount /dev/mmcblk1p1 /boot
  71. echo "flashing Itus Kernel to /boot/$kernel (/dev/mmblk1p1)"
  72. tar -Oxf $tar_file "$board_dir/kernel" > /boot/$kernel
  73. else
  74. if [ "${board}" = "cisco,vedge1000" ]; then
  75. local rootpartuuid
  76. rootpartuuid="$(/usr/sbin/blkid -o value -s PARTUUID "${rootfs}")"
  77. if [ -n "${rootpartuuid}" ]; then
  78. echo "setting root partition to PARTUUID=${rootpartuuid}"
  79. fw_setenv bootcmd 'usb start; ext2load usb 0:1 $loadaddr vmlinux.64; bootoctlinux $loadaddr coremask=f endbootargs rootfstype=squashfs rootwait root=PARTUUID='"${rootpartuuid}"
  80. else
  81. echo "WARNING: unable to figure out root partition UUID, leaving bootcmd unchanged"
  82. fi
  83. mount -t ext2 "${kernel}" /boot
  84. else
  85. mount -t vfat "${kernel}" /boot
  86. fi
  87. [ -f /boot/vmlinux.64 -a ! -L /boot/vmlinux.64 ] && {
  88. mv /boot/vmlinux.64 /boot/vmlinux.64.previous
  89. mv /boot/vmlinux.64.md5 /boot/vmlinux.64.md5.previous
  90. }
  91. echo "flashing kernel to $(awk '/\/boot/ {print $1}' /proc/mounts)"
  92. tar xf $tar_file $board_dir/kernel -O > /boot/vmlinux.64
  93. md5sum /boot/vmlinux.64 | cut -f1 -d " " > /boot/vmlinux.64.md5
  94. fi
  95. echo "flashing rootfs to ${rootfs}"
  96. tar xf $tar_file $board_dir/root -O | dd of="${rootfs}" bs=4096
  97. sync
  98. umount /boot
  99. }
  100. platform_do_upgrade() {
  101. local tar_file="$1"
  102. local board=$(board_name)
  103. local rootfs="$(platform_get_rootfs)"
  104. local kernel=
  105. if [ ! -b "${rootfs}" ] && [ "${board}" = "cisco,vedge1000" ]; then
  106. # Default to the built-in USB disk for N821
  107. rootfs="$(platform_get_n821_disk 2)"
  108. fi
  109. [ -b "${rootfs}" ] || return 1
  110. case "$board" in
  111. er | \
  112. ubnt,edgerouter-4 | \
  113. ubnt,edgerouter-6p)
  114. kernel=/dev/mmcblk0p1
  115. ;;
  116. ubnt,erlite|\
  117. ubnt,usg)
  118. kernel=/dev/sda1
  119. ;;
  120. itus,shield-router)
  121. kernel=ItusrouterImage
  122. ;;
  123. cisco,vedge1000)
  124. kernel="$(platform_get_n821_disk 1)"
  125. ;;
  126. *)
  127. return 1
  128. esac
  129. platform_do_flash $tar_file $board $kernel $rootfs
  130. return 0
  131. }
  132. platform_check_image() {
  133. local board=$(board_name)
  134. local tar_file="$1"
  135. local board_dir=$(tar tf "$tar_file" | grep -m 1 '^sysupgrade-.*/$')
  136. board_dir=${board_dir%/}
  137. [ -n "$board_dir" ] || return 1
  138. case "$board" in
  139. er | \
  140. itus,shield-router | \
  141. ubnt,edgerouter-4 | \
  142. ubnt,edgerouter-6p | \
  143. ubnt,erlite | \
  144. ubnt,usg | \
  145. cisco,vedge1000)
  146. local kernel_length=$(tar xf $tar_file $board_dir/kernel -O | wc -c 2> /dev/null)
  147. local rootfs_length=$(tar xf $tar_file $board_dir/root -O | wc -c 2> /dev/null)
  148. [ "$kernel_length" = 0 -o "$rootfs_length" = 0 ] && {
  149. echo "The upgrade image is corrupt."
  150. return 1
  151. }
  152. return 0
  153. ;;
  154. esac
  155. echo "Sysupgrade is not yet supported on $board."
  156. return 1
  157. }