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.

96 lines
1.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. }
  15. args=$(getopt -o i:c:k:h -- "$@")
  16. if [ $? -ne 0 ] ; then
  17. usage
  18. exit 1
  19. fi
  20. eval set -- "$args"
  21. while [ $# -gt 0 ]
  22. do
  23. case "$1" in
  24. -i)
  25. INPUT_IMAGE_PATH=$2
  26. have_input_image=1
  27. shift
  28. ;;
  29. -c)
  30. CHIPNAME=$2
  31. have_chipname=1
  32. shift
  33. ;;
  34. -k)
  35. BACKUPNAME=$2
  36. have_backupname=1
  37. shift
  38. ;;
  39. -h)
  40. usage
  41. exit 1
  42. ;;
  43. --)
  44. shift
  45. break
  46. ;;
  47. *)
  48. echo "Invalid option: $1"
  49. exit 1
  50. ;;
  51. esac
  52. shift
  53. done
  54. if [ ! "$have_input_image" -gt 0 ] ; then
  55. echo "no input image provided"
  56. usage
  57. exit 1
  58. fi
  59. if [ ! "$have_chipname" -gt 0 ] ; then
  60. echo "no chipname provided. to find it out, run flashrom without arguments"
  61. usage
  62. exit 1
  63. fi
  64. INPUT_IMAGE_NAME=$(basename ${INPUT_IMAGE_PATH})
  65. INPUT_IMAGE_SIZE=$(wc -c <"$INPUT_IMAGE_PATH")
  66. echo "verifying SPI connection by reading"
  67. TEMP_DIR=`mktemp -d`
  68. if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
  69. echo "Could not create temp dir"
  70. exit 1
  71. fi
  72. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test1.rom
  73. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -r ${TEMP_DIR}/test2.rom
  74. cmp --silent ${TEMP_DIR}/test1.rom ${TEMP_DIR}/test2.rom
  75. if [ "$have_backupname" -gt 0 ] ; then
  76. mv ${TEMP_DIR}/test1.rom ${BACKUPNAME}
  77. echo "current image saved as ${BACKUPIMAGE}"
  78. fi
  79. TEMP_SIZE=$(wc -c <"$TEMP_DIR/test1.rom")
  80. if [ ! "$INPUT_IMAGE_SIZE" -eq "$TEMP_SIZE" ] ; then
  81. echo "Error: read image (${TEMP_SIZE}) has different size that new image $INPUT_IMAGE_NAME (${INPUT_IMAGE_SIZE})"
  82. exit 1
  83. fi
  84. rm -rf ${TEMP_DIR}
  85. echo -e "connection ok. flashing ${GREEN}${INPUT_IMAGE_NAME}${NC}"
  86. flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=128 -c ${CHIPNAME} -w ${INPUT_IMAGE_PATH}