buffalo.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # The mtd partitions "firmware" and "Kernel2" on NAND flash are os-image
  2. # partitions. These partitions are called as "Image1/Image2" in U-Boot
  3. # on WSR-2533DHPLx devices, and they are checked conditions when booting.
  4. # "Image1" is always used for booting.
  5. #
  6. # == U-Boot Behaviors ==
  7. #
  8. # - "Image1"/"Image2" images are good, images are different or
  9. # "Image2" image is broken
  10. # -> copy os-image to "Image2" from "Image1"
  11. #
  12. # - "Image1" image is broken
  13. # -> copy os-image to "Image1" from "Image2"
  14. #
  15. # - "Image1"/"Image2" images are broken
  16. # -> fall to U-Boot command line
  17. # TRX magic numbers of each model
  18. case "$(board_name)" in
  19. buffalo,wsr-2533dhpl2)
  20. BUFFALO_TRX_MAGIC="50484c32" # "PHL2"
  21. ;;
  22. buffalo,wsr-2533dhpls)
  23. BUFFALO_TRX_MAGIC="44484c53" # "DHLS"
  24. ;;
  25. esac
  26. buffalo_check_image() {
  27. local board="$1"
  28. local boardname="$(echo $board | tr ',' '_')"
  29. local magic="$2"
  30. local fw_image="$3"
  31. # return error state if TRX + UBI formatted image specified
  32. # to notify about configurations
  33. if [ "$magic" = "$BUFFALO_TRX_MAGIC" ]; then
  34. echo "Your configurations won't be saved if factory-uboot.bin image specified."
  35. echo "But if you want to upgrade, please execute sysupgrade with \"-F\" option."
  36. return 1
  37. fi
  38. # check if valid tar file specifed
  39. if ! tar tf "$fw_image" &>/dev/null; then
  40. echo "Specified file is not a tar archive: $fw_image"
  41. return 1
  42. fi
  43. local control_len=$( (tar xf $fw_image sysupgrade-$boardname/CONTROL -O | wc -c) 2> /dev/null)
  44. # check if valid sysupgrade tar archive
  45. if [ "$control_len" = "0" ]; then
  46. echo "Invalid sysupgrade file: $fw_image"
  47. return 1
  48. fi
  49. local kern_part_len=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
  50. [ -z "$kern_part_len" ] && {
  51. echo "Unable to get \"linux\" partition size"
  52. return 1
  53. }
  54. kern_part_len=$((0x$kern_part_len))
  55. # this also checks if the sysupgrade image is for correct models
  56. local kern_bin_len=$( (tar xf $fw_image sysupgrade-${boardname}/kernel -O | wc -c) 2> /dev/null)
  57. if [ -z "$kern_bin_len" ]; then
  58. echo "Failed to get new kernel size, is valid sysupgrade image specified for the device?"
  59. return 1
  60. fi
  61. # kernel binary has a trx header (len: 28 (0x1c))
  62. kern_bin_len=$((kern_bin_len - 28))
  63. if [ "$kern_bin_len" != "$kern_part_len" ]; then
  64. echo -n "The length of new kernel is invalid for current "
  65. echo "\"linux\" partition, please use factory-uboot.bin image."
  66. echo "\"linux\" partition: $kern_part_len, new kernel: $kern_bin_len"
  67. return 1
  68. fi
  69. }
  70. # for TRX + UBI formatted image
  71. buffalo_upgrade_ubinized() {
  72. sync
  73. echo 3 > /proc/sys/vm/drop_caches
  74. local mtdnum="$( find_mtd_index "ubi" )"
  75. # if no "ubi", don't return error for the purpose of recovery
  76. # ex: recovery after accidental erasing "firmware" partition
  77. if [ ! "$mtdnum" ]; then
  78. echo "cannot find ubi mtd partition \"ubi\", skip detachment"
  79. else
  80. ubidetach -m "$mtdnum"
  81. fi
  82. # erase all data in "firmware"
  83. mtd erase "${PART_NAME}"
  84. # write TRX + UBI formatted image to "firmware"
  85. get_image "$1" | mtd $MTD_ARGS write - "${PART_NAME:-firmware}"
  86. if [ $? -ne 0 ]; then
  87. echo "Failed to write the specified image."
  88. exit 1
  89. fi
  90. }
  91. buffalo_do_upgrade() {
  92. if [ "$(get_magic_long "$1")" = "$BUFFALO_TRX_MAGIC" ]; then
  93. buffalo_upgrade_ubinized "$1"
  94. else
  95. CI_KERNPART="firmware"
  96. nand_do_upgrade "$1"
  97. fi
  98. }