linksys.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. linksys_get_target_firmware() {
  2. local cur_boot_part mtd_ubi0
  3. cur_boot_part="$(/usr/sbin/fw_printenv -n boot_part)"
  4. if [ -z "${cur_boot_part}" ]; then
  5. mtd_ubi0=$(cat /sys/class/ubi/ubi0/mtd_num)
  6. case "$(grep -E "^mtd${mtd_ubi0}:" /proc/mtd | cut -d '"' -f 2)" in
  7. kernel|rootfs)
  8. cur_boot_part=1
  9. ;;
  10. alt_kernel|alt_rootfs)
  11. cur_boot_part=2
  12. ;;
  13. esac
  14. >&2 printf "Current boot_part='%s' selected from ubi0/mtd_num='%s'" \
  15. "${cur_boot_part}" "${mtd_ubi0}"
  16. fi
  17. # OEM U-Boot for EA6350v3, EA8300 and MR8300; bootcmd=
  18. # if test $auto_recovery = no;
  19. # then bootipq;
  20. # elif test $boot_part = 1;
  21. # then run bootpart1;
  22. # else run bootpart2;
  23. # fi
  24. case "$cur_boot_part" in
  25. 1)
  26. fw_setenv -s - <<-EOF
  27. boot_part 2
  28. auto_recovery yes
  29. EOF
  30. printf "alt_kernel"
  31. return
  32. ;;
  33. 2)
  34. fw_setenv -s - <<-EOF
  35. boot_part 1
  36. auto_recovery yes
  37. EOF
  38. printf "kernel"
  39. return
  40. ;;
  41. *)
  42. return
  43. ;;
  44. esac
  45. }
  46. linksys_is_factory_image() {
  47. local board=$(board_name)
  48. board=${board##*,}
  49. # check matching footer signature
  50. tail -c 256 $1 | grep -q -i "\.LINKSYS\.........${board}"
  51. }
  52. platform_do_upgrade_linksys() {
  53. local magic_long="$(get_magic_long "$1")"
  54. local rm_oem_fw_vols="squashfs ubifs" # from OEM [alt_]rootfs UBI
  55. local vol
  56. mkdir -p /var/lock
  57. local part_label="$(linksys_get_target_firmware)"
  58. touch /var/lock/fw_printenv.lock
  59. if [ -z "$part_label" ]; then
  60. echo "cannot find target partition"
  61. exit 1
  62. fi
  63. local target_mtd=$(find_mtd_part "$part_label")
  64. [ "$magic_long" = "73797375" ] && {
  65. CI_KERNPART="$part_label"
  66. if [ "$part_label" = "kernel" ]; then
  67. CI_UBIPART="rootfs"
  68. else
  69. CI_UBIPART="alt_rootfs"
  70. fi
  71. local mtdnum="$(find_mtd_index "$CI_UBIPART")"
  72. if [ ! "$mtdnum" ]; then
  73. echo "cannot find ubi mtd partition $CI_UBIPART"
  74. return 1
  75. fi
  76. local ubidev="$(nand_find_ubi "$CI_UBIPART")"
  77. if [ ! "$ubidev" ]; then
  78. ubiattach -m "$mtdnum"
  79. sync
  80. ubidev="$(nand_find_ubi "$CI_UBIPART")"
  81. fi
  82. if [ "$ubidev" ]; then
  83. for vol in $rm_oem_fw_vols; do
  84. ubirmvol "/dev/$ubidev" -N "$vol" 2>/dev/null
  85. done
  86. fi
  87. # complete std upgrade
  88. nand_upgrade_tar "$1"
  89. }
  90. [ "$magic_long" = "27051956" ] && {
  91. echo "writing \"$1\" image to \"$part_label\""
  92. get_image "$1" | mtd write - "$part_label"
  93. }
  94. [ "$magic_long" = "d00dfeed" ] && {
  95. if ! linksys_is_factory_image "$1"; then
  96. echo "factory image doesn't match device"
  97. return 1
  98. fi
  99. echo "writing \"$1\" factory image to \"$part_label\""
  100. get_image "$1" | mtd -e "$part_label" write - "$part_label"
  101. }
  102. }