ubnt.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # Copyright (C) 2015 OpenWrt.org
  3. #
  4. . /lib/functions.sh
  5. #Note: this code also uses some functions from nand.sh, but it is expected to be run by nand.sh, so we are not
  6. #sourcing it explicitly here
  7. UBNT_ERX_KERNEL_INDEX_OFFSET=160
  8. ubnt_get_target_kernel() {
  9. local factory_mtd=$1
  10. local current_kernel_index=$(hexdump -s $UBNT_ERX_KERNEL_INDEX_OFFSET -n 1 -e '/1 "%X "' ${factory_mtd})
  11. if [ $current_kernel_index == "0" ]; then
  12. echo 'kernel2'
  13. elif [ $current_kernel_index == "1" ]; then
  14. echo 'kernel1'
  15. fi
  16. }
  17. ubnt_update_target_kernel() {
  18. local factory_mtd=$1
  19. local kernel_part=$2
  20. local new_kernel_index
  21. if [ $kernel_part == "kernel1" ]; then
  22. new_kernel_index="\x00"
  23. elif [ $kernel_part == "kernel2" ]; then
  24. new_kernel_index="\x01"
  25. else
  26. echo 'Unknown kernel image index' >&2
  27. return 1
  28. fi
  29. if ! (echo -e $new_kernel_index | dd of=${factory_mtd} bs=1 count=1 seek=$UBNT_ERX_KERNEL_INDEX_OFFSET); then
  30. echo 'Failed to update kernel bootup index' >&2
  31. return 1
  32. fi
  33. }
  34. platform_upgrade_ubnt_erx() {
  35. local factory_mtd=$(find_mtd_part factory)
  36. if [ -z "$factory_mtd" ]; then
  37. echo "cannot find factory partition" >&2
  38. exit 1
  39. fi
  40. local kernel_part="$(ubnt_get_target_kernel ${factory_mtd})"
  41. if [ -z "$kernel_part" ]; then
  42. echo "cannot find factory partition" >&2
  43. exit 1
  44. fi
  45. # This is a global defined in nand.sh, sets partition kernel will be flashed into
  46. CI_KERNPART=${kernel_part}
  47. #Remove volume possibly left over from stock firmware
  48. local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  49. if [ -z "$ubidev" ]; then
  50. local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
  51. if [ -z "$mtdnum" ]; then
  52. echo "cannot find ubi mtd partition $CI_UBIPART" >&2
  53. exit 1
  54. fi
  55. ubiattach -m "$mtdnum"
  56. sync
  57. ubidev="$( nand_find_ubi "$CI_UBIPART" )"
  58. fi
  59. if [ -n "$ubidev" ]; then
  60. local troot_ubivol="$( nand_find_volume $ubidev troot )"
  61. [ -n "$troot_ubivol" ] && ubirmvol /dev/$ubidev -N troot || true
  62. fi
  63. ubnt_update_target_kernel ${factory_mtd} ${kernel_part} || exit 1
  64. }