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.

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