You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.9 KiB

  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-3.0+
  3. # Copyright (C) 2018, Martin Kepplinger <martink@posteo.de>
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. NC='\033[0m'
  7. set -e
  8. have_input_image=0
  9. have_chipname=0
  10. have_backupname=0
  11. usage()
  12. {
  13. echo "Usage: $0 -i <image.rom> [-c <chipname>] [-k <backup_filename>]"
  14. echo ""
  15. echo " -i <path to image to flash>"
  16. echo " -c <chipname> to use for flashrom"
  17. echo " -k <path to backup to save>"
  18. }
  19. args=$(getopt -o i:c:k:h -- "$@")
  20. if [ $? -ne 0 ] ; then
  21. usage
  22. exit 1
  23. fi
  24. eval set -- "$args"
  25. while [ $# -gt 0 ]
  26. do
  27. case "$1" in
  28. -i)
  29. INPUT_IMAGE_PATH=$2
  30. have_input_image=1
  31. shift
  32. ;;
  33. -c)
  34. CHIPNAME=$2
  35. have_chipname=1
  36. shift
  37. ;;
  38. -k)
  39. BACKUPNAME=$2
  40. have_backupname=1
  41. shift
  42. ;;
  43. -h)
  44. usage
  45. exit 1
  46. ;;
  47. --)
  48. shift
  49. break
  50. ;;
  51. *)
  52. echo "Invalid option: $1"
  53. exit 1
  54. ;;
  55. esac
  56. shift
  57. done
  58. if [ ! "$have_input_image" -gt 0 ] ; then
  59. echo "no input image provided"
  60. usage
  61. exit 1
  62. fi
  63. TEMP_DIR=`mktemp -d`
  64. if [ ! "$have_chipname" -gt 0 ] ; then
  65. echo "trying to detect the chip..."
  66. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 &> ${TEMP_DIR}/chips || true
  67. flashrom_error=""
  68. flashrom_error=$(cat ${TEMP_DIR}/chips | grep -i error || true)
  69. if [ ! -z "${flashrom_error}" ] ; then
  70. cat ${TEMP_DIR}/chips
  71. rm -rf ${TEMP_DIR}
  72. exit 1
  73. fi
  74. CHIPNAME=""
  75. chip_found=0
  76. CHIPNAME=$(cat ${TEMP_DIR}/chips | grep Found | grep "MX25L3206E" | grep -o '".*"' || true)
  77. if [ ! -z "${CHIPNAME}" ] ; then
  78. chip_found=1
  79. fi
  80. if [ ! "$chip_found" -gt 0 ] ; then
  81. echo "chip not detected."
  82. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128
  83. rm -rf ${TEMP_DIR}
  84. echo "Please find it manually in the list above and rerun with the -c parameter."
  85. exit 1
  86. else
  87. echo -e "Detected ${GREEN}${CHIPNAME}${NC}."
  88. fi
  89. fi
  90. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  91. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  92. reference_filesize=4194304
  93. if [ ! "$INPUT_IMAGE_SIZE" -eq "$reference_filesize" ] ; then
  94. echo -e "${RED}Error:${NC} input file must be 4MB of size"
  95. exit 1
  96. fi
  97. echo "verifying SPI connection by reading 2 times. please wait."
  98. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  99. echo "Could not create temp dir"
  100. exit 1
  101. fi
  102. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  103. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  104. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  105. if [ "$have_backupname" -gt 0 ] ; then
  106. cp ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  107. echo "current image saved as ${BACKUPNAME}"
  108. fi
  109. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  110. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  111. echo -e "${RED}Error:${NC} read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  112. exit 1
  113. fi
  114. rm -rf ${TEMP_DIR}
  115. echo -e "${GREEN}connection ok${NC}. flashing ${INPUT_IMAGE_NAME}"
  116. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}
  117. echo -e "${GREEN}DONE${NC}"